From: Steven Luo Date: Thu, 17 Dec 2009 11:08:11 +0000 (-0800) Subject: Implement loading and saving config settings X-Git-Tag: v3.0rc1~7^2~13 X-Git-Url: http://git.maemo.org/git/?p=browser-switch;a=commitdiff_plain;h=e63005644ace28b2af7ac1ecba7ccd87a476d3c3 Implement loading and saving config settings --- diff --git a/config-ui/Makefile b/config-ui/Makefile index f26c158..c154f22 100644 --- a/config-ui/Makefile +++ b/config-ui/Makefile @@ -1,7 +1,7 @@ CC = gcc CFLAGS = -Wall -Os -mcpu=arm1136jf-s -mthumb CFLAGS_PLUGIN = -fPIC -CPPFLAGS = `pkg-config --cflags gtk+-2.0` +CPPFLAGS = -I../ `pkg-config --cflags gtk+-2.0` CPPFLAGS_HILDON = -DHILDON `pkg-config --cflags hildon-1` CPPFLAGS_PLUGIN = $(CPPFLAGS_HILDON) -DHILDON_CP_APPLET \ `pkg-config --cflags dbus-1` `pkg-config --cflags hildon-control-panel` @@ -11,12 +11,14 @@ LDFLAGS_PLUGIN = -shared $(LDFLAGS_HILDON) \ `pkg-config --libs dbus-1` `pkg-config --libs hildon-control-panel` PREFIX = /usr/local +other_obj = ../configfile.o + APP = browser-switchboard-cp -app_obj = $(APP).app.o +app_obj = $(APP).app.o $(other_obj) HILDON_APP = $(APP)-hildon -happ_obj = $(APP).happ.o +happ_obj = $(APP).happ.o $(other_obj) PLUGIN = lib$(APP).so -plugin_obj = $(APP).plugin.o +plugin_obj = $(APP).plugin.o $(other_obj) all: plugin plugin: $(PLUGIN) @@ -53,6 +55,6 @@ install: all install -c -m 0644 $(APP).desktop $(DESTDIR)$(PREFIX)/share/applications/hildon-control-panel clean: - rm -f $(APP) $(HILDON_APP) $(PLUGIN) *.o + rm -f $(APP) $(HILDON_APP) $(PLUGIN) *.o ../configfile.o .PHONY: strip install plugin app hildon-app diff --git a/config-ui/browser-switchboard-cp.c b/config-ui/browser-switchboard-cp.c index 61cd288..06e97aa 100644 --- a/config-ui/browser-switchboard-cp.c +++ b/config-ui/browser-switchboard-cp.c @@ -25,8 +25,10 @@ */ -#include +#include #include +#include +#include #include #include #include @@ -38,6 +40,8 @@ #endif #endif +#include "configfile.h" + struct browser_entry { char *config; char *displayname; @@ -60,27 +64,179 @@ struct config_widgets { struct config_widgets cw; GtkWidget *dialog; + +/********************************************************************** + * Configuration routines + **********************************************************************/ + static inline int get_continuous_mode(void) { return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_check)); } +static inline void set_continuous_mode(int state) { + return gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_check), (gboolean)state); +} + static inline char * get_default_browser(void) { return browsers[gtk_combo_box_get_active(GTK_COMBO_BOX(cw.default_browser_combo))].config; } +static void set_default_browser(char *browser) { + gint i; + + /* Loop through browsers looking for a match */ + for (i = 0; browsers[i].config && strcmp(browsers[i].config, browser); + ++i); + + if (!browsers[i].config) + /* No match found, set to the default browser */ + i = 0; + + gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), i); +} + static inline char * get_other_browser_cmd(void) { return (char *)gtk_entry_get_text(GTK_ENTRY(cw.other_browser_cmd_entry)); } +static inline void set_other_browser_cmd(char *cmd) { + gtk_entry_set_text(GTK_ENTRY(cw.other_browser_cmd_entry), cmd); +} +static void load_config(void) { + FILE *fp; + int continuous_mode_seen = 0; + int default_browser_seen = 0; + int other_browser_cmd_seen = 0; + struct swb_config_line line; + + if (!(fp = open_config_file())) + return; + + /* Parse the config file + TODO: should we handle errors differently than EOF? */ + if (!parse_config_file_begin()) + goto out; + while (!parse_config_file_line(fp, &line)) { + if (line.parsed) { + if (!strcmp(line.key, "continuous_mode")) { + if (!continuous_mode_seen) { + set_continuous_mode(atoi(line.value)); + continuous_mode_seen = 1; + } + } else if (!strcmp(line.key, "default_browser")) { + if (!default_browser_seen) { + set_default_browser(line.value); + default_browser_seen = 1; + } + } else if (!strcmp(line.key, "other_browser_cmd")) { + if (!other_browser_cmd_seen) { + set_other_browser_cmd(line.value); + other_browser_cmd_seen = 1; + } + } + } + free(line.key); + free(line.value); + } + parse_config_file_end(); -static inline void close_dialog(void) { - gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_NONE); +out: + fclose(fp); + return; } -static void do_reconfig(void) { - printf("continuous_mode: %d\n", get_continuous_mode()); - printf("default_browser: %s\n", get_default_browser()); - printf("other_browser_cmd: '%s'\n", get_other_browser_cmd()); +static void save_config(void) { + FILE *fp = NULL, *tmpfp = NULL; + char *homedir, *tempfile, *newfile; + size_t len; + int continuous_mode_seen = 0; + int default_browser_seen = 0; + int other_browser_cmd_seen = 0; + struct swb_config_line line; + + /* Put together the path to the new config file and the tempfile */ + if (!(homedir = getenv("HOME"))) + homedir = DEFAULT_HOMEDIR; + len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1; + if (!(newfile = calloc(len, sizeof(char)))) + return; + /* 4 = strlen(".tmp") */ + if (!(tempfile = calloc(len+4, sizeof(char)))) { + free(newfile); + return; + } + snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC); + snprintf(tempfile, len+4, "%s%s", newfile, ".tmp"); + + /* Open the temporary file for writing */ + if (!(tmpfp = fopen(tempfile, "w"))) + /* TODO: report the error somehow? */ + goto out; + + /* Open the old config file, if it exists */ + if ((fp = open_config_file()) && parse_config_file_begin()) { + /* Copy the old config file over to the new one line by line, + replacing old config values with new ones + TODO: should we handle errors differently than EOF? */ + while (!parse_config_file_line(fp, &line)) { + if (line.parsed) { + /* Is a config line, print the new value here */ + if (!strcmp(line.key, "continuous_mode")) { + if (!continuous_mode_seen) { + fprintf(tmpfp, "%s = %d\n", + line.key, + get_continuous_mode()); + continuous_mode_seen = 1; + } + } else if (!strcmp(line.key, + "default_browser")) { + if (!default_browser_seen) { + fprintf(tmpfp, "%s = \"%s\"\n", + line.key, + get_default_browser()); + default_browser_seen = 1; + } + } else if (!strcmp(line.key, + "other_browser_cmd")) { + if (!other_browser_cmd_seen && + strlen(get_other_browser_cmd())>0) { + fprintf(tmpfp, "%s = \"%s\"\n", + line.key, + get_other_browser_cmd()); + other_browser_cmd_seen = 1; + } + } + } else { + /* Just copy the old line over */ + fprintf(tmpfp, "%s\n", line.key); + } + free(line.key); + free(line.value); + } + parse_config_file_end(); + } - /* TODO: actually implement configuration */ + /* If we haven't written them yet, write out the new config values */ + if (!continuous_mode_seen) + fprintf(tmpfp, "%s = %d\n", + "continuous_mode", get_continuous_mode()); + if (!default_browser_seen) + fprintf(tmpfp, "%s = \"%s\"\n", + "default_browser", get_default_browser()); + if (!other_browser_cmd_seen && strlen(get_other_browser_cmd()) > 0) + fprintf(tmpfp, "%s = \"%s\"\n", + "other_browser_cmd", get_other_browser_cmd()); + + /* Replace the old config file with the new one */ + fclose(tmpfp); + tmpfp = NULL; + rename(tempfile, newfile); + +out: + free(newfile); + free(tempfile); + if (tmpfp) + fclose(tmpfp); + if (fp) + fclose(fp); return; } @@ -98,8 +254,12 @@ static void default_browser_combo_callback(GtkWidget *widget, gpointer data) { } } +static inline void close_dialog(void) { + gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_NONE); +} + static void ok_callback(GtkWidget *widget, gpointer data) { - do_reconfig(); + save_config(); /* TODO: is there any cleanup necessary? */ close_dialog(); } @@ -219,6 +379,8 @@ osso_return_t execute(osso_context_t * osso, /* enable help system on dialog */ GtkDialog * dialog = GTK_DIALOG(swb_config_dialog()); + load_config(); + gtk_dialog_run(dialog); gtk_widget_destroy(GTK_WIDGET(dialog)); @@ -241,9 +403,10 @@ int main(int argc, char *argv[]) g_set_application_name("Browser Switchboard"); dialog = GTK_DIALOG(swb_config_dialog()); + load_config(); gtk_dialog_run(dialog); gtk_widget_destroy(GTK_WIDGET(dialog)); - return 0; + exit(0); } #endif