X-Git-Url: http://git.maemo.org/git/?p=browser-switch;a=blobdiff_plain;f=config-ui%2Fbrowser-switchboard-cp.c;h=6866c84cb0de80fe0601e9c7bd92fc667a171eb6;hp=1846a460f4e483e47d26f3baa2e1a96d2576c5df;hb=ec8b58af1b1cf22f34478faf476047e7ce996245;hpb=18bad5e9d35f3418a9f53a730cf5877c91c0b366 diff --git a/config-ui/browser-switchboard-cp.c b/config-ui/browser-switchboard-cp.c index 1846a46..6866c84 100644 --- a/config-ui/browser-switchboard-cp.c +++ b/config-ui/browser-switchboard-cp.c @@ -2,7 +2,8 @@ * browser-switchboard-cp.c -- a hildon-control-panel applet for * selecting the default browser for Browser Switchboard * - * Copyright (C) 2009 Steven Luo + * Copyright (C) 2009-2010 Steven Luo + * Copyright (C) 2009-2010 Faheem Pervez * * Derived from services-cp.c from maemo-control-services * Copyright (c) 2008 Janne Kataja @@ -50,7 +51,7 @@ #endif /* HILDON_CP_APPLET */ #endif /* HILDON */ -#include "configfile.h" +#include "config.h" #define CONTINUOUS_MODE_DEFAULT 0 @@ -66,11 +67,14 @@ struct browser_entry browsers[] = { { "microb", "MicroB" }, /* First entry is the default! */ { "tear", "Tear" }, { "fennec", "Mobile Firefox (Fennec)" }, + { "opera", "Opera Mobile" }, { "midori", "Midori" }, { "other", "Other" }, { NULL, NULL }, }; +struct swb_config orig_cfg; + struct config_widgets { #if defined(HILDON) && defined(FREMANTLE) GtkWidget *continuous_mode_selector; @@ -148,160 +152,43 @@ static inline void set_other_browser_cmd(char *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(); - -out: - fclose(fp); - return; + swb_config_init(&orig_cfg); + + swb_config_load(&orig_cfg); + + set_continuous_mode(orig_cfg.continuous_mode); + set_default_browser(orig_cfg.default_browser); + if (orig_cfg.other_browser_cmd) + set_other_browser_cmd(orig_cfg.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; - - /* If CONFIGFILE_DIR doesn't exist already, try to create it */ - if (!(homedir = getenv("HOME"))) - homedir = DEFAULT_HOMEDIR; - len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1; - if (!(newfile = calloc(len, sizeof(char)))) - return; - snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR); - if (access(newfile, F_OK) == -1 && errno == ENOENT) { - mkdir(newfile, 0750); + struct swb_config new_cfg = orig_cfg; + + if (get_continuous_mode() != orig_cfg.continuous_mode) { + new_cfg.continuous_mode = get_continuous_mode(); + new_cfg.flags |= SWB_CONFIG_CONTINUOUS_MODE_SET; } - free(newfile); - - /* Put together the path to the new config file and the tempfile */ - 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; + if (strcmp(get_default_browser(), orig_cfg.default_browser)) { + new_cfg.default_browser = get_default_browser(); + new_cfg.flags |= SWB_CONFIG_DEFAULT_BROWSER_SET; } - 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(); + if ((orig_cfg.other_browser_cmd && + strcmp(get_other_browser_cmd(), orig_cfg.other_browser_cmd)) || + (!orig_cfg.other_browser_cmd && + strlen(get_other_browser_cmd()) > 0)) { + new_cfg.other_browser_cmd = get_other_browser_cmd(); + new_cfg.flags |= SWB_CONFIG_OTHER_BROWSER_CMD_SET; } - /* 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; + swb_config_save(&new_cfg); } static void do_reconfig(void) { save_config(); /* Try to send SIGHUP to any running browser-switchboard process - This causes it to reread config files if in continuous_mode, and + This causes it to reread config files if in continuous_mode, or die so that the config will be reloaded on next start otherwise */ system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1"); } @@ -374,9 +261,10 @@ static GtkDialog *swb_config_dialog(gpointer cp_window) { default_browser_selector_button, FALSE, FALSE, 0); cw.other_browser_cmd_entry = hildon_entry_new(_HILDON_SIZE_DEFAULT); + /* Disable autocapitalization and dictionary features for the entry */ input_mode = hildon_gtk_entry_get_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry)); - input_mode &= ~HILDON_GTK_INPUT_MODE_AUTOCAP; - input_mode &= ~HILDON_GTK_INPUT_MODE_DICTIONARY; + input_mode &= ~(HILDON_GTK_INPUT_MODE_AUTOCAP | + HILDON_GTK_INPUT_MODE_DICTIONARY); hildon_gtk_entry_set_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry), input_mode); cw.other_browser_cmd_entry_label = hildon_caption_new(NULL, @@ -418,6 +306,9 @@ static GtkDialog *swb_config_dialog(gpointer cp_window) { GtkWidget *default_browser_combo_label; GtkWidget *continuous_mode_label; int i; +#ifdef HILDON + HildonGtkInputMode input_mode; +#endif dialog = gtk_dialog_new_with_buttons( "Browser Switchboard", @@ -459,6 +350,13 @@ static GtkDialog *swb_config_dialog(gpointer cp_window) { 5, 0); cw.other_browser_cmd_entry = gtk_entry_new(); +#ifdef HILDON + /* Disable autocapitalization and dictionary features for the entry */ + input_mode = hildon_gtk_entry_get_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry)); + input_mode &= ~(HILDON_GTK_INPUT_MODE_AUTOCAP | + HILDON_GTK_INPUT_MODE_DICTIONARY); + hildon_gtk_entry_set_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry), input_mode); +#endif cw.other_browser_cmd_entry_label = gtk_label_new("Command (%s for URI):"); gtk_misc_set_alignment(GTK_MISC(cw.other_browser_cmd_entry_label), 1, 0.5); gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);