X-Git-Url: http://git.maemo.org/git/?p=browser-switch;a=blobdiff_plain;f=config-ui%2Fbrowser-switchboard-config.c;h=752725e1dc428e60a64b1d5992671be959c8db38;hp=c14b80fdd9b5a05e6327b94f07338716100a08b8;hb=57b70bf816859abf1de95d4570bcc1b75983b7d9;hpb=6ac42ed781a7735221de8aaa28a6647b32608049 diff --git a/config-ui/browser-switchboard-config.c b/config-ui/browser-switchboard-config.c index c14b80f..752725e 100644 --- a/config-ui/browser-switchboard-config.c +++ b/config-ui/browser-switchboard-config.c @@ -1,5 +1,5 @@ /* - * browser-switchboard-config.c -- command-line configuration utility for + * browser-switchboard-config.c -- command-line configuration utility for * Browser Switchboard * * Copyright (C) 2009-2010 Steven Luo @@ -22,18 +22,21 @@ #include +#include #include #include +#include #include #include "config.h" +#include "browsers.h" extern struct swb_config_option swb_config_options[]; static int get_config_value(char *name) { struct swb_config cfg; - int i; - struct swb_config_option optinfo; + struct swb_config_option *optinfo; + ptrdiff_t i; int retval = 1; swb_config_init(&cfg); @@ -41,14 +44,15 @@ static int get_config_value(char *name) { if (!swb_config_load(&cfg)) return 1; - for (i = 0; swb_config_options[i].name; ++i) { - optinfo = swb_config_options[i]; - if (strcmp(name, optinfo.name)) + for (optinfo = swb_config_options; optinfo->name; ++optinfo) { + if (strcmp(name, optinfo->name)) continue; - switch (optinfo.type) { + i = optinfo - swb_config_options; + switch (optinfo->type) { case SWB_CONFIG_OPT_STRING: - printf("%s\n", *(char **)cfg.entries[i]); + if (*(char **)cfg.entries[i]) + printf("%s\n", *(char **)cfg.entries[i]); break; case SWB_CONFIG_OPT_INT: printf("%d\n", *(int *)cfg.entries[i]); @@ -65,10 +69,43 @@ static int get_config_value(char *name) { return retval; } -static int set_config_value(char *name, char *value) { +static int get_default_browser(void) { struct swb_config cfg; int i; - struct swb_config_option optinfo; + + swb_config_init(&cfg); + + if (!swb_config_load(&cfg)) + return 1; + + /* Check to see if the configured default browser is installed + If not, report the default default browser */ + for (i = 0; browsers[i].config; ++i) { + if (strcmp(browsers[i].config, cfg.default_browser)) + continue; + + if (browsers[i].binary && access(browsers[i].binary, X_OK)) + printf("%s\n", browsers[0].config); + else + printf("%s\n", browsers[i].config); + + break; + } + + if (!browsers[i].config) + /* Unknown browser configured as default, report the default + default browser */ + printf("%s\n", browsers[0].config); + + swb_config_free(&cfg); + + return 0; +} + +static int set_config_value(char *name, char *value) { + struct swb_config cfg; + struct swb_config_option *optinfo; + ptrdiff_t i; int retval = 1; swb_config_init(&cfg); @@ -76,39 +113,39 @@ static int set_config_value(char *name, char *value) { if (!swb_config_load(&cfg)) return 1; - for (i = 0; swb_config_options[i].name; ++i) { - optinfo = swb_config_options[i]; - if (strcmp(name, optinfo.name)) + for (optinfo = swb_config_options; optinfo->name; ++optinfo) { + if (strcmp(name, optinfo->name)) continue; - switch (optinfo.type) { + i = optinfo - swb_config_options; + switch (optinfo->type) { case SWB_CONFIG_OPT_STRING: /* Free any existing string */ - if (cfg.flags & optinfo.set_mask) + if (cfg.flags & optinfo->set_mask) free(*(char **)cfg.entries[i]); if (strlen(value) == 0) { /* If the new value is empty, clear the config setting */ *(char **)cfg.entries[i] = NULL; - cfg.flags &= ~optinfo.set_mask; + cfg.flags &= ~optinfo->set_mask; } else { /* Make a copy of the string -- it's not safe to free value, which comes from argv */ if (!(*(char **)cfg.entries[i] = strdup(value))) exit(1); - cfg.flags |= optinfo.set_mask; + cfg.flags |= optinfo->set_mask; } break; case SWB_CONFIG_OPT_INT: if (strlen(value) == 0) { /* If the new value is empty, clear the config setting */ - cfg.flags &= ~optinfo.set_mask; + cfg.flags &= ~optinfo->set_mask; } else { *(int *)cfg.entries[i] = atoi(value); - cfg.flags |= optinfo.set_mask; + cfg.flags |= optinfo->set_mask; } break; } @@ -146,7 +183,7 @@ int main(int argc, char **argv) { int opt, done = 0; int set = 0; char *selected_opt = NULL; - + while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) { switch (opt) { case 'h': @@ -192,6 +229,9 @@ int main(int argc, char **argv) { exit(1); } return set_config_value(selected_opt, argv[optind]); - } else + } else if (!strcmp(selected_opt, "default_browser")) + /* Default browser value needs special handling */ + return get_default_browser(); + else return get_config_value(selected_opt); }