Support for storing/loading items

This commit is contained in:
Sanel Zukan 2011-10-20 16:02:47 +00:00
parent 016c9f8677
commit 688f5e07de
3 changed files with 87 additions and 18 deletions

View File

@ -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;

View File

@ -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

View File

@ -11,10 +11,52 @@
#include <edelib/Window.h>
#include <edelib/Nls.h>
#include <edelib/Ede.h>
#include <edelib/Resource.h>
#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();