campo-sirio/ba/ba0.cpp

592 lines
13 KiB
C++
Raw Normal View History

#include <applicat.h>
#include <colors.h>
#include <execp.h>
#include <mask.h>
#include <isam.h>
#include <prefix.h>
#include <progind.h>
#include <utility.h>
#include <urldefid.h>
/*
#if XVT_OS == XVT_OS_SCOUNIX
#include <sys/stat.h>
#else
#include <direct.h>
#endif
#if XVT_OS == XVT_OS_WIN
#include <dos.h>
#else
#include <dirent.h>
#endif
*/
#include "ba0.h"
#include "ba0100a.h"
///////////////////////////////////////////////////////////
// Picture Mask
///////////////////////////////////////////////////////////
class TPicture_mask : public TMask
{
TImage & _image;
protected:
virtual void handler(WINDOW win, EVENT* ep);
public:
TPicture_mask(const char* name, int dx, int dy, TImage & image, bool remap);
virtual ~TPicture_mask() {}
};
TPicture_mask::TPicture_mask(const char* name, int dx, int dy, TImage & image, bool remap)
: TMask(name, 1, dx, dy), _image(image)
{
if (_image.ok())
{
if (!remap && MASK_BACK_COLOR != COLOR_DKCYAN)
_image.set_clut(6, MASK_BACK_COLOR);
else
_image.set_palette(win());
_image.set_pos(1, 1);
}
}
void TPicture_mask::handler(WINDOW win, EVENT* ep)
{
TMask::handler(win, ep);
if (ep->type == E_UPDATE)
{
#if XVT_OS == XVT_OS_WIN
if (_image.ok())
{
RCT src; xvt_rect_set(&src, 0, 0, _image.width(), _image.height());
const short maxx = 42*CHARX;
const short maxy = short((long)maxx*src.bottom/src.right);
RCT dst; xvt_rect_set(&dst, 1, 1, maxx, maxy);
_image.draw(win, dst);
}
else
#endif
{
const int max = 16;
for (int i = 0; i < max; i++)
{
TTemp_window w(win);
w.rect(i*2, i, max-i*2, max-i);
}
}
}
}
///////////////////////////////////////////////////////////
// Menu application
///////////////////////////////////////////////////////////
class TMenu_application : public TApplication
{
const char* _name;
enum { MAXLEVEL = 1024 };
int _first[MAXLEVEL];
TArray _menu; // TAG|DESCRIPTION|ACTION
TBit_array _enabled;
TArray _modules, _images;
int _level, _max;
bool _ditta_asked;
static int _last_button;
static bool _find_button;
protected:
void test_temp();
void load_menu();
int do_level();
int find_menu(const char* s) const;
bool check_user();
virtual bool create();
virtual bool menu(MENU_TAG m);
virtual bool build_firm_data(long cod, bool flagcom = FALSE);
static bool menu_item_handler(TMask_field&f, KEY k);
static bool menu_find_handler(TMask_field&f, KEY k);
bool module_enabled(const char * program) const;
bool module_enabled(int module) const { return has_module(module);}
public:
TMenu_application(const char* name) : _name(name), _ditta_asked(FALSE) {}
};
int TMenu_application::_last_button = 0;
bool TMenu_application::_find_button = FALSE;
inline TMenu_application& app()
{ return (TMenu_application&)main_app(); }
bool TMenu_application::build_firm_data(long codditta, bool flagcom)
{
const char * const ndir = "/dir.gen";
const char * const ntrc = "/trc.gen";
TFilename s(firm2dir(codditta)); s << ndir;
bool exist = fexist(s);
if (!exist)
{
s = s.path(); s.rtrim(1); s << ntrc;
exist = fexist(s);
}
if (exist)
return message_box("Direttorio dati danneggiato, impossibile attivare la ditta %ld", codditta);
if (!yesno_box("Gli archivi della ditta %ld non esistono: si desidera generarli?", codditta))
return FALSE;
set_autoload_new_files(yesno_box("Si desidera precaricare gli archivi standard"));
s = s.path(); s.rtrim(1);
if (!fexist(s) && !make_dir(s))
return error_box("Impossibile creare il direttorio della ditta %ld (%s)",
codditta, (const char*)s);
s << ndir;
if (!fcopy(&ndir[1], s))
return error_box("Impossibile copiare il file %s della ditta %ld",
&ndir[1], codditta);
s = s.path(); s << ntrc;
if (!fcopy(&ntrc[1], s))
return error_box("Impossibile copiare il file %s della ditta %ld",
ntrc, codditta);
begin_wait();
TDir dir, dir1;
TTrec rec;
prefix().set("");
dir1.get(LF_DIR, _nolock, _nordir, _sysdirop);
const long maxeod0 = dir1.eod();
prefix().set_codditta(codditta);
dir.get(LF_DIR, _nolock, _nordir, _sysdirop);
if (dir.eod() == 0)
{
dir1.eod() = 1L;
dir1.put(LF_DIR, _nordir, _sysdirop);
dir.get(LF_DIR, _nolock, _nordir, _sysdirop);
}
const long maxeod1 = dir.eod();
if (maxeod0 > maxeod1)
{
dir.get(LF_DIR, _nolock, _nordir, _sysdirop);
dir.eod() = maxeod0;
dir.put(LF_DIR, _nordir, _sysdirop);
rec.zero();
}
TString80 mess("Generazione archivi della ditta "); mess << codditta;
TProgind p(maxeod0 ? maxeod0 : 1, mess, TRUE, TRUE, 70);
for (int i = LF_DIR + 1; i <= maxeod0; i++)
{
p.addstatus(1);
prefix().set("");
dir.get(i, _nolock, _nordir, _sysdirop);
rec.get(i);
bool create_now = dir.is_active();
prefix().set_codditta(codditta);
dir.put(i, _nordir, _sysdirop);
rec.put(i);
const char* name = dir.name();
dir.flags() = 0L;
create_now = create_now && (flagcom ? dir.is_com() : dir.is_firm());
if (dir.is_valid() && create_now)
{
TSystemisamfile f(i);
f.build(30);
}
else
{
// if (!flagcom)
// {
// dir.len() = 0;
// rec.zero();
// }
dir.put(i, _nordir, _sysdirop);
rec.put(i);
}
}
set_firm(codditta);
set_autoload_new_files(TRUE);
end_wait();
return TRUE;
}
bool TMenu_application::module_enabled(const char * program) const
{
bool ok = FALSE;
const int nmod = _modules.items();
for (int aut = 0; aut < nmod; aut++)
{
const TString& s = (const TString&) _modules[aut];
if (s.compare(program, 2) == 0) { ok = TRUE; break; }
}
return ok && has_module(aut);
}
void TMenu_application::load_menu()
{
TScanner s(_name);
_max = -1;
while (s.line().not_empty())
{
TToken_string* ts = new TToken_string(s.token());
int l = ts->get_int();
if (l < _max)
{
error_box("Item of level %d while %d was expected)", l, _max);
l = _max;
}
if (l > _max)
{
if (l >= MAXLEVEL)
{
error_box("Too many menu levels: %d", l);
l = _max;
}
_first[_max = l] = _menu.items();
}
_menu.add(ts);
const TString80 action(ts->get(2));
const int last = _menu.items() - 1;
if (atoi(action) > 0)
{
TToken_string list(ts->get(), ',');
const TString& mod = list.get();
int module = atoi(mod);
if (module == 0)
{
bool on = TRUE;
if (mod[0] == 'P')
on = user() == "PRASSI";
_enabled.set(last, on);
}
while(!_enabled[last] && module > 0)
{
if (has_module(module))
_enabled.set(last);
module = list.get_int();
}
}
else
_enabled.set(last, module_enabled(action));
}
_first[++_max] = _menu.items();
}
int TMenu_application::find_menu(const char* s) const
{
TString80 str(s); str.upper();
int found = -1;
for (int i = 0; i < _menu.items(); i++)
{
if (_enabled[i])
{
TToken_string& l = (TToken_string&)_menu[i];
const int m = l.get_int(0);
if (m != _level)
{
TString80 v(l.get()); v.upper();
if (v.find(str) >= 0)
{
found = i;
if (isalpha(l.get_char())) break;
}
}
}
}
return found;
}
bool TMenu_application::menu_item_handler(TMask_field&f, KEY k)
{
if (k == K_SPACE)
{
_last_button = f.dlg();
_find_button = FALSE;
f.mask().stop_run(K_AUTO_ENTER);
}
return TRUE;
}
bool TMenu_application::menu_find_handler(TMask_field&f, KEY k)
{
if (k == K_TAB && f.focusdirty())
{
const TString& v = f.get();
if (v.not_empty())
{
_last_button = app().find_menu(v);
if (_last_button >= 0)
{
_find_button = TRUE;
f.mask().stop_run(K_AUTO_ENTER);
}
else
{
beep();
return FALSE;
}
}
}
return TRUE;
}
int TMenu_application::do_level()
{
const int first = _first[_level];
const int last = _first[_level+1];
TToken_string& row = (TToken_string&)_menu[first];
const TString80 head(row.get(1));
const int width = 78;
const int height = 18;
const int bwidth = 20;
const int x = width-bwidth-12;
short id = (short)row.get_int();
if (_images.objptr(id) == NULL)
{
char* n = format("ba%02d.bmp", id);
if (id > 0 && !fexist(n))
n = format("ba%02d.bmp", id = 0);
TImage * image = new TImage(n);
_images.add(image, id);
}
TPicture_mask menu(head, width, height, (TImage &) _images[id], id != 0);
int y = 1;
#if XVT_OS == XVT_OS_WIN
TString16 t(format("#%d", BMP_STOPREC));
#else
TString16 t;
#endif
for (int i = first+1; i < last; i++, y++)
{
TToken_string& row = (TToken_string&)_menu[i];
TString80 item(row.get(1));
if (isdigit(*row.get())) item << "...";
menu.add_static(-1, 0, item, x+4, y);
const short id = 100+y;
menu.add_button(id, 0, t, x, y, 1, 1);
menu.set_handler(id, menu_item_handler);
if (!_enabled[i]) menu.disable(id);
}
menu.add_static(-1, 0, "Cerca", 1,-3);
menu.add_string(99, 0, "", -12, -3, 50, "", bwidth+1);
menu.set_handler(99, menu_find_handler);
t = first ? "Menu precedente" : "Fine";
menu.add_button(first ? DLG_CANCEL : DLG_QUIT, 0, t, -22, -1, bwidth, 2);
if (first)
menu.add_button(DLG_QUIT, 0, "Fine", -12, -1, bwidth, 2);
if (_find_button && _last_button > first)
menu.first_focus(100+_last_button-first);
_last_button = _find_button = 0;
const int k = menu.run();
int m = 0;
switch (k)
{
case K_ESC:
m = -1; break;
case K_QUIT:
menu.reset();
m = -2; break;
default:
if (_find_button)
m = -1;
else
m = first+_last_button-100;
break;
}
return m;
}
void TMenu_application::test_temp()
{
begin_wait();
TFilename dir; dir.tempdir(); // Directory temporanea
dir << '/' << '*';
TToken_string files(dir);
const int count = list_files(files);
end_wait();
if (count > 0 && yesno_box("Cancellare %d file temporane%c in %s?",
count, (count > 1) ? 'i' : 'o', dir.path()))
{
TProgind bar(count, "Cancellazione file temporanei", TRUE, TRUE);
for (const char* e = files.get(0); e; e = files.get())
{
if (bar.iscancelled()) break;
remove(e);
bar.addstatus(1);
}
}
}
bool TMenu_application::check_user()
{
TMask m("ba0100a");
TLocalisamfile users(LF_USER);
TString16 utente, pwd;
bool ok = FALSE;
for (int i = 0 ; i < 3 && !ok; i++)
{
if (m.run() == K_ESC)
break;
utente = m.get(F_USER);
users.zero();
users.put("USERNAME", utente);
pwd = "";
if (users.read() == NOERR)
pwd = decode(users.get("PASSWORD"));
else
if (utente == "PRASSI")
pwd = "pr.assi";
ok = pwd.not_empty() && pwd == m.get(F_PASSWORD);
if (ok)
user() = utente;
else
{
error_box("Utente e/o password errata:\nfare attenzione alle maiuscole");
m.set(F_PASSWORD,"");
}
}
return ok;
}
bool TMenu_application::create()
{
TApplication::create();
if (!check_user()) return FALSE;
set_perms();
test_temp();
TScanner scanner("prassi.aut");
for (int aut = 0; scanner.line() != ""; aut++)
_modules.add(scanner.token());
load_menu();
dispatch_e_menu(BAR_ITEM(1));
return TRUE;
}
bool TMenu_application::menu(MENU_TAG)
{
int refarray[256];
memset(refarray, 0, sizeof(refarray));
int i = 0;
_level = 0;
while (i >= 0)
{
const int m = do_level();
if (m >= 0)
{
TToken_string& row = (TToken_string&)_menu[m];
const TFilename option(row.get(2));
if (option.not_empty())
{
bool ok = TRUE;
const int l = atoi(option);
if (l > 0 && l < MAXLEVEL)
{
const TString16 flags(row.get());
if (flags.find('F') >= 0)
_ditta_asked = ok = set_firm();
if (ok)
{
refarray[i++] = _level;
if (l < _max) _level = l;
}
}
else
{
const TString16 module(cmd2name(option));
if (!_ditta_asked && module.left(2) == "cg" && module != "cg5100")
_ditta_asked = ok = set_firm();
if (ok)
{
prefix().set("DEF"); // Aggiorna prefix
TExternal_app a(option);
a.run();
}
}
}
}
else
{
if (m < -1) break;
if (_find_button)
{
TToken_string& row = (TToken_string&)_menu[_last_button];
_level = row.get_int(0);
}
else
{
_level = (i > 0) ? refarray[--i] : 0;
if (_level == 0) i = 0;
}
}
}
return FALSE;
}
int main(int argc, char** argv)
{
TApplication::check_parameters(argc, argv);
const char* menu = (argc < 2) ? "prassi.mnu" : argv[1];
TMenu_application ma(menu);
ma.run(argc, argv, "Menu Principale");
return TRUE;
}