From 688f5e07deb5196b2ab4362e2ee0e042adff1fc8 Mon Sep 17 00:00:00 2001 From: Sanel Zukan Date: Thu, 20 Oct 2011 16:02:47 +0000 Subject: [PATCH] Support for storing/loading items --- ede-preferred-applications/Apps.cpp | 14 ++- ede-preferred-applications/Apps.h | 6 +- .../ede-preferred-applications.cpp | 85 +++++++++++++++---- 3 files changed, 87 insertions(+), 18 deletions(-) diff --git a/ede-preferred-applications/Apps.cpp b/ede-preferred-applications/Apps.cpp index b8a7079..38d78bd 100644 --- a/ede-preferred-applications/Apps.cpp +++ b/ede-preferred-applications/Apps.cpp @@ -19,7 +19,7 @@ KnownApp *app_find_by_cmd(KnownApp *lst, const char *name) { E_RETURN_VAL_IF_FAIL(name, 0); for(int i = 0; lst[i].name; i++) - if(strcmp(lst[i].name, name) == 0) + if(strcmp(lst[i].cmd, name) == 0) return &lst[i]; return 0; } @@ -39,6 +39,18 @@ KnownApp *app_get(KnownApp *lst, int index) { return &lst[index]; } +int app_get_index(KnownApp *lst, const char *cmd) { + E_RETURN_VAL_IF_FAIL(lst != 0, -1); + E_RETURN_VAL_IF_FAIL(cmd != 0, -1); + + for(int i = 0; lst[i].name; i++) { + if(strcmp(lst[i].cmd, cmd) == 0) + return i; + } + + return -1; +} + static void populate_menu(const KnownApp &item, void *data) { Fl_Choice *c = (Fl_Choice*)data; const char *cmd = item.cmd; diff --git a/ede-preferred-applications/Apps.h b/ede-preferred-applications/Apps.h index 99c9e53..053651c 100644 --- a/ede-preferred-applications/Apps.h +++ b/ede-preferred-applications/Apps.h @@ -21,7 +21,11 @@ KnownApp *app_find_by_name(KnownApp *lst, const char *name); KnownApp *app_find_by_cmd(KnownApp *lst, const char *cmd); KnownApp *app_get(KnownApp *lst, int index); +int app_get_index(KnownApp *lst, const char *cmd); +inline int app_get_index(KnownApp *lst, KnownApp &a) { return app_get_index(lst, a.cmd); } + void app_populate_menu(KnownApp *lst, Fl_Choice *c); -bool app_is_magic_cmd(const char *cmd); +bool app_is_magic_cmd(const char *cmd); +inline bool app_is_magic_cmd(const KnownApp &a) { return app_is_magic_cmd(a.cmd); } #endif diff --git a/ede-preferred-applications/ede-preferred-applications.cpp b/ede-preferred-applications/ede-preferred-applications.cpp index 7e70597..d9d77e8 100644 --- a/ede-preferred-applications/ede-preferred-applications.cpp +++ b/ede-preferred-applications/ede-preferred-applications.cpp @@ -11,10 +11,52 @@ #include #include #include +#include #include "PredefApps.h" EDELIB_NS_USING_AS(Window, EdeWindow) +EDELIB_NS_USING(Resource) + +Fl_Choice *browser_choice, + *mail_choice, + *filemgr_choice, + *term_choice; + +/* store separate item */ +static void check_and_store(Fl_Choice *c, KnownApp *lst, const char *n, Resource &rc) { + int sel = c->value(); + KnownApp *app = app_get(lst, sel); + if(app_is_magic_cmd(*app)) + return; + + rc.set("Preferred", n, app->cmd); +} + +/* load separate item */ +static void check_and_load(Fl_Choice *c, KnownApp *lst, const char *n, Resource &rc) { + static char buf[128]; + if(!rc.get("Preferred", n, buf, sizeof(buf))) + return; + + KnownApp *app = app_find_by_cmd(lst, buf); + if(!app) return; + + int i = app_get_index(lst, *app); + if(i < 0) return; + + c->value(i); +} + +static void load_settings(void) { + Resource rc; + if(!rc.load("ede-launch")) return; + + check_and_load(browser_choice, app_browsers, "browser", rc); + check_and_load(mail_choice, app_mails, "mail", rc); + check_and_load(filemgr_choice, app_filemanagers, "file_manager", rc); + check_and_load(term_choice, app_terminals, "terminal", rc); +} static void close_cb(Fl_Widget *widget, void *ww) { EdeWindow *win = (EdeWindow*)ww; @@ -22,6 +64,14 @@ static void close_cb(Fl_Widget *widget, void *ww) { } static void ok_cb(Fl_Widget *widget, void *ww) { + Resource rc; + + check_and_store(browser_choice, app_browsers, "browser", rc); + check_and_store(mail_choice, app_mails, "mail", rc); + check_and_store(filemgr_choice, app_filemanagers, "file_manager", rc); + check_and_store(term_choice, app_terminals, "terminal", rc); + + rc.save("ede-launch"); close_cb(widget, ww); } @@ -33,29 +83,29 @@ int main(int argc, char** argv) { tabs->begin(); Fl_Group *gint = new Fl_Group(15, 30, 335, 140, _("Internet")); gint->begin(); - Fl_Choice *cint = new Fl_Choice(20, 65, 320, 25, _("Web browser")); - cint->align(FL_ALIGN_TOP_LEFT); - app_populate_menu(app_browsers, cint); - cint->value(0); + browser_choice = new Fl_Choice(20, 65, 320, 25, _("Web browser")); + browser_choice->align(FL_ALIGN_TOP_LEFT); + app_populate_menu(app_browsers, browser_choice); + browser_choice->value(0); - Fl_Choice *cmail = new Fl_Choice(20, 120, 320, 25, _("Mail reader")); - cmail->align(FL_ALIGN_TOP_LEFT); - app_populate_menu(app_mails, cmail); - cmail->value(0); + mail_choice = new Fl_Choice(20, 120, 320, 25, _("Mail reader")); + mail_choice->align(FL_ALIGN_TOP_LEFT); + app_populate_menu(app_mails, mail_choice); + mail_choice->value(0); gint->end(); Fl_Group *gutil = new Fl_Group(15, 30, 335, 140, _("Utilities")); gutil->hide(); gutil->begin(); - Fl_Choice *ufile = new Fl_Choice(20, 65, 320, 25, _("File manager")); - ufile->align(FL_ALIGN_TOP_LEFT); - app_populate_menu(app_filemanagers, ufile); - ufile->value(0); + filemgr_choice = new Fl_Choice(20, 65, 320, 25, _("File manager")); + filemgr_choice->align(FL_ALIGN_TOP_LEFT); + app_populate_menu(app_filemanagers, filemgr_choice); + filemgr_choice->value(0); - Fl_Choice *uterm = new Fl_Choice(20, 120, 320, 25, _("Terminal")); - uterm->align(FL_ALIGN_TOP_LEFT); - app_populate_menu(app_terminals, uterm); - uterm->value(0); + term_choice = new Fl_Choice(20, 120, 320, 25, _("Terminal")); + term_choice->align(FL_ALIGN_TOP_LEFT); + app_populate_menu(app_terminals, term_choice); + term_choice->value(0); gutil->end(); tabs->end(); @@ -66,6 +116,9 @@ int main(int argc, char** argv) { win->set_modal(); win->end(); + + /* now load all settings before show */ + load_settings(); win->show(argc, argv); return Fl::run();