2 * save-config.c -- functions to save a Browser Switchboard configuration
4 * Copyright (C) 2009-2010 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 #include <sys/types.h>
35 #include "configfile.h"
38 extern struct swb_config_option swb_config_options[];
40 /* Outputs a config file line for the named option to a file descriptor */
41 static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen,
42 struct swb_config *cfg, char *name) {
43 struct swb_config_option *opt;
46 for (opt = swb_config_options; opt->name; ++opt) {
47 if (strcmp(opt->name, name))
50 i = opt - swb_config_options;
51 if (!(*oldcfg_seen & opt->set_mask) &&
52 (cfg->flags & opt->set_mask)) {
54 case SWB_CONFIG_OPT_STRING:
55 fprintf(fp, "%s = \"%s\"\n",
57 *(char **)cfg->entries[i]);
58 *oldcfg_seen |= opt->set_mask;
60 case SWB_CONFIG_OPT_INT:
61 fprintf(fp, "%s = %d\n",
63 *(int *)cfg->entries[i]);
64 *oldcfg_seen |= opt->set_mask;
72 /* Save the settings in the provided swb_config struct to the config file
73 Returns true on success, false otherwise */
74 int swb_config_save(struct swb_config *cfg) {
75 FILE *fp = NULL, *tmpfp = NULL;
76 char *homedir, *tempfile, *newfile;
79 struct swb_config_line line;
80 unsigned int oldcfg_seen = 0;
83 /* If CONFIGFILE_DIR doesn't exist already, try to create it */
84 if (!(homedir = getenv("HOME")))
85 homedir = DEFAULT_HOMEDIR;
86 len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
87 if (!(newfile = calloc(len, sizeof(char))))
89 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
90 if (access(newfile, F_OK) == -1 && errno == ENOENT) {
95 /* Put together the path to the new config file and the tempfile */
96 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
97 if (!(newfile = calloc(len, sizeof(char))))
99 /* 4 = strlen(".tmp") */
100 if (!(tempfile = calloc(len+4, sizeof(char)))) {
104 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
105 snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
107 /* Open the temporary file for writing */
108 if (!(tmpfp = fopen(tempfile, "w"))) {
113 /* Open the old config file, if it exists */
114 if ((fp = open_config_file()) && parse_config_file_begin()) {
115 /* Copy the old config file over to the new one line by line,
116 replacing old config values with new ones
117 TODO: should we handle errors differently than EOF? */
118 while (!parse_config_file_line(fp, &line)) {
120 /* Is a config line, print the new value here */
121 swb_config_output_option(tmpfp, &oldcfg_seen,
124 /* Just copy the old line over */
125 fprintf(tmpfp, "%s\n", line.key);
130 parse_config_file_end();
133 /* If we haven't written them yet, write out any new config values */
134 for (i = 0; swb_config_options[i].name; ++i)
135 swb_config_output_option(tmpfp, &oldcfg_seen, cfg,
136 swb_config_options[i].name);
138 /* Replace the old config file with the new one */
141 rename(tempfile, newfile);
153 /* Reconfigure a running browser-switchboard process with new settings */
154 void swb_reconfig(struct swb_config *old, struct swb_config *new) {
156 int microb_was_autostarted, microb_should_autostart;
161 /* Try to send SIGHUP to any running browser-switchboard process
162 This causes it to reread config files if in continuous_mode, or
163 die so that the config will be reloaded on next start otherwise */
164 system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
170 microb_was_autostarted = (old->autostart_microb == 1) ||
171 (!strcmp(old->default_browser, "microb") &&
172 old->autostart_microb);
173 microb_should_autostart = (new->autostart_microb == 1) ||
174 (!strcmp(new->default_browser, "microb") &&
175 new->autostart_microb);
176 if (!microb_was_autostarted && microb_should_autostart) {
177 /* MicroB should be started if it's not running */
178 status = system("pidof browser > /dev/null");
179 if (WIFEXITED(status) && WEXITSTATUS(status)) {
180 if ((pid = fork()) == -1)
184 /* Child process, start MicroB */
185 execl("/usr/bin/maemo-invoker", "browser",
190 /* XXX: We'd like to stop MicroB if (microb_was_autostarted &&
191 !microb_should_autostart), but we don't know if the open MicroB
192 process has open windows. */
193 #endif /* FREMANTLE */