6cb7d5ca6d1983fc761b5458e471915b05a1eba7
[browser-switch] / config-ui / save-config.c
1 /*
2  * save-config.c -- functions to save a Browser Switchboard configuration
3  *
4  * Copyright (C) 2009-2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
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.
11  *
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.
16  *
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,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #ifdef FREMANTLE
31 #include <sys/wait.h>
32 #endif
33
34 #include "configfile.h"
35 #include "config.h"
36
37 extern struct swb_config_option swb_config_options[];
38
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;
43         void *entry;
44
45         for (opt = swb_config_options; opt->name; ++opt) {
46                 if (strcmp(opt->name, name))
47                         continue;
48
49                 entry = (char *)cfg + opt->offset;
50                 if (!(*oldcfg_seen & opt->set_mask) &&
51                     (cfg->flags & opt->set_mask)) {
52                         switch (opt->type) {
53                           case SWB_CONFIG_OPT_STRING:
54                                 fprintf(fp, "%s = \"%s\"\n",
55                                         opt->name,
56                                         *(char **)entry);
57                                 *oldcfg_seen |= opt->set_mask;
58                                 break;
59                           case SWB_CONFIG_OPT_INT:
60                                 fprintf(fp, "%s = %d\n",
61                                         opt->name,
62                                         *(int *)entry);
63                                 *oldcfg_seen |= opt->set_mask;
64                                 break;
65                         }
66                 }
67                 break;
68         }
69 }
70
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;
76         size_t len;
77         int retval = 1;
78         struct swb_config_line line;
79         unsigned int oldcfg_seen = 0;
80         int i;
81
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))))
87                 return 0;
88         snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
89         if (access(newfile, F_OK) == -1 && errno == ENOENT) {
90                 mkdir(newfile, 0750);
91         }
92         free(newfile);
93
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))))
97                 return 0;
98         /* 4 = strlen(".tmp") */
99         if (!(tempfile = calloc(len+4, sizeof(char)))) {
100                 free(newfile);
101                 return 0;
102         }
103         snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
104         snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
105
106         /* Open the temporary file for writing */
107         if (!(tmpfp = fopen(tempfile, "w"))) {
108                 retval = 0;
109                 goto out;
110         }
111
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)) {
118                         if (line.parsed) {
119                                 /* Is a config line, print the new value here */
120                                 swb_config_output_option(tmpfp, &oldcfg_seen,
121                                                          cfg, line.key);
122                         } else {
123                                 /* Just copy the old line over */
124                                 fprintf(tmpfp, "%s\n", line.key);
125                         }
126                         free(line.key);
127                         free(line.value);
128                 }
129                 parse_config_file_end();
130         }
131
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);
136
137         /* Replace the old config file with the new one */
138         fclose(tmpfp);
139         tmpfp = NULL;
140         rename(tempfile, newfile);
141
142 out:
143         free(newfile);
144         free(tempfile);
145         if (tmpfp)
146                 fclose(tmpfp);
147         if (fp)
148                 fclose(fp);
149         return retval;
150 }
151
152 /* Reconfigure a running browser-switchboard process with new settings */
153 void swb_reconfig(struct swb_config *old, struct swb_config *new) {
154 #ifdef FREMANTLE
155         int microb_was_autostarted, microb_should_autostart;
156         pid_t pid;
157         int status;
158 #endif
159
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");
164
165 #ifdef FREMANTLE
166         if (!old || !new)
167                 return;
168
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)
180                                 return;
181
182                         if (!pid) {
183                                 /* Child process, start MicroB */
184                                 execl("/usr/bin/maemo-invoker", "browser",
185                                       (char *)NULL);
186                         }
187                 }
188         }
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 */
193
194         return;
195 }