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