#include #include #include #include #include #include "ba2201.h" #include "ba2300.h" /////////////////////////////////////////////////////////// // Autoform tree /////////////////////////////////////////////////////////// class TAutoform_tree : public TString_tree { TConfig *_ini; protected: virtual TImage* image(bool selected) const; public: TConfig& ini(); void study(const char* module); bool studied(const char* module) const; bool forget(const char* module); TAutoform_tree(); virtual ~TAutoform_tree(); }; TConfig& TAutoform_tree::ini() { if (_ini == NULL) _ini = new TConfig(CONFIG_USER, "Autoform"); return *_ini; } void TAutoform_tree::study(const char* module) { CHECK(module && *module, "Invalid module"); TString4 varname; varname.strncpy(module, 2); varname.lower(); TToken_string str(ini().get(varname), ','); TString16 val = module; val.ltrim(2); if (str.get_pos(val) < 0) { str.add(val); ini().set(varname, str); } } bool TAutoform_tree::studied(const char* module) const { if (module && *module) { TString4 varname; varname.strncpy(module, 2); varname.lower(); TToken_string str(((TAutoform_tree*)this)->ini().get(varname), ','); TString16 val = module; val.ltrim(2); return str.get_pos(val) >= 0; } return FALSE; } bool TAutoform_tree::forget(const char* module) { bool killed = FALSE; if (module && *module) { TString4 varname; varname.strncpy(module, 2); varname.lower(); TToken_string str(ini().get(varname), ','); TString16 val = module; val.ltrim(2); int pos = str.get_pos(val); if (pos >= 0) { str.destroy(pos); ini().set(varname, str); killed = TRUE; } } return killed; } TImage* TAutoform_tree::image(bool selected) const { if (!has_son()) { TString str; get_description(str); str.cut(5); if (studied(str)) return get_res_image(BMP_FILECHK); } return TString_tree::image(selected); } TAutoform_tree::TAutoform_tree() : _ini(NULL) { } TAutoform_tree::~TAutoform_tree() { if (_ini) delete _ini; } /////////////////////////////////////////////////////////// // Autoform mask /////////////////////////////////////////////////////////// class TAutoform_mask : public TAutomask { TAutoform_tree _tree; protected: virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly); static bool forget_lesson(TTree& tree, void* jolly, word flags); void reset_lesson(); public: int list_dirs(const char* filelist, TString_array& result) const; void fill_tree(); TAutoform_tree& tree() { return _tree; } TAutoform_mask(); virtual ~TAutoform_mask(); }; int TAutoform_mask::list_dirs(const char* filelist, TString_array& result) const { TWait_cursor hourglass; result.destroy(); TFilename dir(filelist); int i; for (i = dir.len()-1; i >= 0; i--) if (dir[i] == '/' || dir[i] == '\\' || dir[i] == ':') break; TFilename mask(dir.mid(i+1)); dir.cut(i > 0 ? i+1 : 0); xvt_fsys_save_dir(); if (dir.not_empty()) { DIRECTORY directory; xvt_fsys_convert_str_to_dir(dir.get_buffer(), &directory); BOOLEAN ok = xvt_fsys_set_dir(&directory); if (!ok) return 0; } SLIST files = xvt_fsys_list_files(DIR_TYPE, mask.get_buffer(), TRUE); const int count = xvt_slist_count(files); for (SLIST_ELT e = xvt_slist_get_first(files); e; e = xvt_slist_get_next(files, e)) { char* f = xvt_slist_get(files, e, NULL); if (*f != '.' && f[1] != ':') { if (dir.not_empty()) { mask = dir; mask.add(f); result.add(mask); } else result.add(f); } } xvt_slist_destroy(files); xvt_fsys_restore_dir(); return count; } static int compare_lessons(const TObject** o1, const TObject** o2) { const TFilename f1 = *(const TString*)*o1; const TFilename f2 = *(const TString*)*o2; const TString80 s1 = f1.name(); const TString80 s2 = f2.name(); int cmp = s1.compare(s2, 4, TRUE); if (cmp == 0) cmp = s2[4] - s1[4]; return cmp; } void TAutoform_mask::fill_tree() { TTree_field& tf = tfield(F_TREE); TFilename path = get(F_PATH); TString desc; if (path.exist()) { if (_tree.goto_root()) _tree.kill_node(); _tree.add_son(path); path.add("*"); TString_array a, b; list_dirs(path, a); for (int i = a.items()-1; i >= 0; i--) { path = a.row(i); _tree.goto_root(); _tree.add_son(path.name()); path.add("*"); list_dirs(path, b); b.TArray::sort(compare_lessons); for (int j = b.items()-1; j >= 0; j--) { path = b.row(j); desc = path.name(); path.add("backup.ini"); TConfig ini(path, "Main"); desc << " - " << ini.get("Description"); if (desc.not_empty()) { _tree.add_son(desc); _tree.goto_father(); } } } } tf.set_tree(&_tree); _tree.goto_root(); _tree.expand_all(); tf.win().force_update(); } bool TAutoform_mask::forget_lesson(TTree& tree, void* jolly, word flags) { if (!tree.has_son()) { TAutoform_tree& at = (TAutoform_tree&)tree; TString desc; at.get_description(desc); desc.cut(5); at.forget(desc); } return FALSE; } void TAutoform_mask::reset_lesson() { TTree_field& tf = tfield(F_TREE); if (tf.goto_selected()) { if (_tree.goto_firstson()) _tree.scan_depth_first(forget_lesson, NULL); else forget_lesson(_tree, NULL, 0); tf.win().force_update(); } } bool TAutoform_mask::on_field_event(TOperable_field& o, TField_event e, long jolly) { switch (o.dlg()) { case F_PATH: if (e == fe_init || e == fe_modify) fill_tree(); break; case DLG_RESET: if (e == fe_button) reset_lesson(); break; default: break; } return TRUE; } TAutoform_mask::TAutoform_mask() : TAutomask("ba2300") { TConfig& ini = _tree.ini(); set(F_PATH, ini.get("Path")); set(F_STUDENT, ini.get("Student")); set(F_SOLUTION, ini.get("Solution")); } TAutoform_mask::~TAutoform_mask() { TConfig& ini = _tree.ini(); ini.set("Path", get(F_PATH)); ini.set("Student", get(F_STUDENT)); ini.set("Solution", get(F_SOLUTION)); } /////////////////////////////////////////////////////////// // Autoform app /////////////////////////////////////////////////////////// class TAutoform_app : public TArchive_app { protected: virtual void main_loop(); }; void TAutoform_app::main_loop() { TAutoform_mask msk; if (msk.run() == K_ENTER) { TTree_field& tf = msk.tfield(F_TREE); TAutoform_tree& tree = msk.tree(); if (tf.goto_selected() && !tree.has_son()) { TString desc; tree.get_description(desc); if (yesno_box(FR("Si desidera caricare i dati\n%s?"), (const char*)desc)) { desc.cut(5); const short dlg = desc.right(1) == "I" ? F_STUDENT : F_SOLUTION; const TFilename old_study = prefix().get_studio(); TFilename study = msk.get(dlg); if (study.empty()) msk.set(dlg, study = old_study); if (study != old_study) prefix().set_studio(study); restore(0x3, 0, msk.get(F_PATH)); tree.study(desc); } } } } int ba2300(int argc, char* argv[]) { TAutoform_app app; app.run(argc, argv, TR("Autoformazione")); return 0; }