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,
27 #include <sys/types.h>
34 #include "configfile.h"
37 extern struct swb_config_option swb_config_options[];
39 /* Outputs a config file line for the named option to a file descriptor */
40 static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen,
41 struct swb_config *cfg, char *name) {
42 struct swb_config_option *opt;
45 for (opt = swb_config_options; opt->name; ++opt) {
46 if (strcmp(opt->name, name))
49 entry = (char *)cfg + opt->offset;
50 if (!(*oldcfg_seen & opt->set_mask) &&
51 (cfg->flags & opt->set_mask)) {
53 case SWB_CONFIG_OPT_STRING:
54 fprintf(fp, "%s = \"%s\"\n",
57 *oldcfg_seen |= opt->set_mask;
59 case SWB_CONFIG_OPT_INT:
60 fprintf(fp, "%s = %d\n",
63 *oldcfg_seen |= opt->set_mask;
71 /* Save the settings in the provided swb_config struct to the config file
72 Returns true on success, false otherwise */
73 int swb_config_save(struct swb_config *cfg) {
74 FILE *fp = NULL, *tmpfp = NULL;
75 char *homedir, *tempfile, *newfile;
78 struct swb_config_line line;
79 unsigned int oldcfg_seen = 0;
82 /* If CONFIGFILE_DIR doesn't exist already, try to create it */
83 if (!(homedir = getenv("HOME")))
84 homedir = DEFAULT_HOMEDIR;
85 len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
86 if (!(newfile = calloc(len, sizeof(char))))
88 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
89 if (access(newfile, F_OK) == -1 && errno == ENOENT) {
94 /* Put together the path to the new config file and the tempfile */
95 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
96 if (!(newfile = calloc(len, sizeof(char))))
98 /* 4 = strlen(".tmp") */
99 if (!(tempfile = calloc(len+4, sizeof(char)))) {
103 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
104 snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
106 /* Open the temporary file for writing */
107 if (!(tmpfp = fopen(tempfile, "w"))) {
112 /* Open the old config file, if it exists */
113 if ((fp = open_config_file()) && parse_config_file_begin()) {
114 /* Copy the old config file over to the new one line by line,
115 replacing old config values with new ones
116 TODO: should we handle errors differently than EOF? */
117 while (!parse_config_file_line(fp, &line)) {
119 /* Is a config line, print the new value here */
120 swb_config_output_option(tmpfp, &oldcfg_seen,
123 /* Just copy the old line over */
124 fprintf(tmpfp, "%s\n", line.key);
129 parse_config_file_end();
132 /* If we haven't written them yet, write out any new config values */
133 for (i = 0; swb_config_options[i].name; ++i)
134 swb_config_output_option(tmpfp, &oldcfg_seen, cfg,
135 swb_config_options[i].name);
137 /* Replace the old config file with the new one */
140 rename(tempfile, newfile);
152 /* Reconfigure a running browser-switchboard process with new settings */
153 void swb_reconfig(struct swb_config *old, struct swb_config *new) {
155 int microb_was_autostarted, microb_should_autostart;
160 /* Try to send SIGHUP to any running browser-switchboard process
161 This causes it to reread config files if in continuous_mode, or
162 die so that the config will be reloaded on next start otherwise */
163 system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
169 microb_was_autostarted = (old->autostart_microb == 1) ||
170 (!strcmp(old->default_browser, "microb") &&
171 old->autostart_microb);
172 microb_should_autostart = (new->autostart_microb == 1) ||
173 (!strcmp(new->default_browser, "microb") &&
174 new->autostart_microb);
175 if (!microb_was_autostarted && microb_should_autostart) {
176 /* MicroB should be started if it's not running */
177 status = system("pidof browser > /dev/null");
178 if (WIFEXITED(status) && WEXITSTATUS(status)) {
179 if ((pid = fork()) == -1)
183 /* Child process, start MicroB */
184 execl("/usr/bin/maemo-invoker", "browser",
189 /* XXX: We'd like to stop MicroB if (microb_was_autostarted &&
190 !microb_should_autostart), but we don't know if the open MicroB
191 process has open windows. */
192 #endif /* FREMANTLE */