campo-sirio/include/golem.cpp
guy e694e08d1a Aggiunta gestione oggetti esterni (GOLEM)
Corretto errore per cui la stampante non si ricordava il suo tipo.


git-svn-id: svn://10.65.10.50/trunk@1362 c028cbd2-c16b-5b4b-a496-9718f37d4682
1995-05-16 09:35:12 +00:00

206 lines
4.1 KiB
C++
Executable File

#include <config.h>
#include <execp.h>
#include <golem.h>
#include <mask.h>
#include <prefix.h>
#include <relation.h>
#include <urldefid.h>
#include <utility.h>
#include <bagn006.h>
long TGolem::_count = 0;
TFilename* TGolem::_path = NULL;
TConfig* TGolem::_config = NULL;
TRelation* TGolem::_golem = NULL;
TGolem::TGolem(const char* cls, long id)
: _class(cls), _id(id)
{
CHECK(_class.not_empty() && id >= 0, "Invalid Golem creation");
_class.upper();
if (_count == 0)
{
CHECK(_path == NULL, "Golem construction count error");
_path = new TFilename;
_config = new TConfig(CONFIG_GOLEM, _class);
_golem = new TRelation(LF_GOLEM);
}
_count++;
}
TGolem::~TGolem()
{
_count--;
if (_count == 0)
{
CHECK(_path != NULL, "Golem destruction count error");
delete _golem; _golem = NULL;
delete _config; _config = NULL;
delete _path; _path = NULL;
}
}
TConfig& TGolem::config() const
{
_config->set_paragraph(_class);
return *_config;
}
bool TGolem::ok() const
{
return _id > 0 && fexist(path());
}
const char* TGolem::class_name() const
{
*_path = "GOLEM_";
*_path << _class << "_CLASS";
return *_path;
}
word TGolem::class_id() const
{
return CLASS_GOLEM;
}
short TGolem::icon() const
{
const short id = config().get_int("Icon", NULL, -1, DLG_F9);
return id;
}
const char* TGolem::ext() const
{
return config().get("Extension", NULL, -1, _class.left(3));
}
const TFilename& TGolem::path(bool test) const
{
const char* e = ext();
*_path = firm2dir(-1); // C:\PRASSI\DATI
_path->add("golem"); // C:\PRASSI\DATI\GOLEM
if (test && !fexist(*_path))
make_dir(*_path);
_path->add(_class); // C:\PRASSI\DATI\GOLEM\BITMAP
if (test && !fexist(*_path))
make_dir(*_path);
if (_id > 0)
{
_path->add(format("%ld", _id));// C:\PRASSI\DATI\GOLEM\BMP\883
_path->ext(e); // C:\PRASSI\DATI\GOLEM\BMP\883.BMP
}
return *_path;
}
int TGolem::compare(const TSortable& s) const
{
if (s.class_id() != class_id())
return UNDEFINED;
const TGolem& g = (const TGolem&)s;
return int(_id - g._id);
}
bool TGolem::edit()
{
const TFilename& p = path();
bool ok = fexist(p);
if (ok)
{
TFilename e(config().get("Editor"));
e << ' ' << p;
TExternal_app app(e);
ok = app.run(FALSE, FALSE) == 0;
}
return ok;
}
long TGolem::new_id() const
{
long id = 0;
TLocalisamfile& gol = _golem->lfile();
gol.zero();
gol.put("CLASSE", _class);
gol.put("CHIAVE", "99999999");
const int err = gol.read(_isgteq);
switch (err)
{
case NOERR:
id = 0; break;
case _isemptyfile:
id = 1; break;
default:
if (gol.get("CLASSE") != _class)
gol.prev();
if (gol.get("CLASSE") == _class)
id = gol.get_long("CHIAVE")+1;
else
id = 1;
break;
}
return id;
}
bool TGolem::import()
{
FILE_SPEC fs;
const char* const e = ext();
xvt_fsys_convert_str_to_dir(".", &fs.dir);
strcpy(fs.type, e);
sprintf(fs.name, "*.%s", e);
strcpy(fs.creator, "GOLEM");
xvt_fsys_save_dir();
FL_STATUS ok = xvt_dm_post_file_open(&fs, "Selezionare il file ...");
xvt_fsys_restore_dir();
if (ok == FL_OK)
{
TFilename from;
xvt_fsys_convert_dir_to_str(&fs.dir, (char*)(const char*)from, 80);
from.add(fs.name); from.ext(e);
TMask msk("bagn006");
msk.set(F_CLASSE, _class);
msk.set(F_CODICE, new_id());
bool correct = msk.run() == K_ENTER;
if (correct)
{
_id = msk.get_long(F_CODICE);
correct = fcopy(from, path(TRUE));
if (correct)
{
msk.autosave(_golem);
int err = _golem->write();
if (err == _isreinsert)
err = _golem->rewrite();
if (err != NOERR)
{
error_box("Errore nell'aggiornamento del file degli oggetti");
ok = FL_BAD;
}
}
else
{
error_box("Spazio su disco insufficiente per l'oggetto");
ok = FL_BAD;
}
}
else ok = FL_CANCEL;
}
return ok == FL_OK;
}