Add an Xsession.post script for Fremantle to launch browser-switchboard on session...
[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 #include "configfile.h"
32 #include "config.h"
33
34 extern struct swb_config_option swb_config_options[];
35
36 /* Outputs a config file line for the named option to a file descriptor */
37 static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen,
38                               struct swb_config *cfg, char *name) {
39         struct swb_config_option *opt;
40         ptrdiff_t i;
41
42         for (opt = swb_config_options; opt->name; ++opt) {
43                 if (strcmp(opt->name, name))
44                         continue;
45
46                 i = opt - swb_config_options;
47                 if (!(*oldcfg_seen & opt->set_mask) &&
48                     (cfg->flags & opt->set_mask)) {
49                         switch (opt->type) {
50                           case SWB_CONFIG_OPT_STRING:
51                                 fprintf(fp, "%s = \"%s\"\n",
52                                         opt->name,
53                                         *(char **)cfg->entries[i]);
54                                 *oldcfg_seen |= opt->set_mask;
55                                 break;
56                           case SWB_CONFIG_OPT_INT:
57                                 fprintf(fp, "%s = %d\n",
58                                         opt->name,
59                                         *(int *)cfg->entries[i]);
60                                 *oldcfg_seen |= opt->set_mask;
61                                 break;
62                         }
63                 }
64                 break;
65         }
66 }
67
68 /* Save the settings in the provided swb_config struct to the config file
69    Returns true on success, false otherwise */
70 int swb_config_save(struct swb_config *cfg) {
71         FILE *fp = NULL, *tmpfp = NULL;
72         char *homedir, *tempfile, *newfile;
73         size_t len;
74         int retval = 1;
75         struct swb_config_line line;
76         unsigned int oldcfg_seen = 0;
77         int i;
78
79         /* If CONFIGFILE_DIR doesn't exist already, try to create it */
80         if (!(homedir = getenv("HOME")))
81                 homedir = DEFAULT_HOMEDIR;
82         len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
83         if (!(newfile = calloc(len, sizeof(char))))
84                 return 0;
85         snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
86         if (access(newfile, F_OK) == -1 && errno == ENOENT) {
87                 mkdir(newfile, 0750);
88         }
89         free(newfile);
90
91         /* Put together the path to the new config file and the tempfile */
92         len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
93         if (!(newfile = calloc(len, sizeof(char))))
94                 return 0;
95         /* 4 = strlen(".tmp") */
96         if (!(tempfile = calloc(len+4, sizeof(char)))) {
97                 free(newfile);
98                 return 0;
99         }
100         snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
101         snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
102
103         /* Open the temporary file for writing */
104         if (!(tmpfp = fopen(tempfile, "w"))) {
105                 retval = 0;
106                 goto out;
107         }
108
109         /* Open the old config file, if it exists */
110         if ((fp = open_config_file()) && parse_config_file_begin()) {
111                 /* Copy the old config file over to the new one line by line,
112                    replacing old config values with new ones
113                    TODO: should we handle errors differently than EOF? */
114                 while (!parse_config_file_line(fp, &line)) {
115                         if (line.parsed) {
116                                 /* Is a config line, print the new value here */
117                                 swb_config_output_option(tmpfp, &oldcfg_seen,
118                                                          cfg, line.key);
119                         } else {
120                                 /* Just copy the old line over */
121                                 fprintf(tmpfp, "%s\n", line.key);
122                         }
123                         free(line.key);
124                         free(line.value);
125                 }
126                 parse_config_file_end();
127         }
128
129         /* If we haven't written them yet, write out any new config values */
130         for (i = 0; swb_config_options[i].name; ++i)
131                 swb_config_output_option(tmpfp, &oldcfg_seen, cfg,
132                                          swb_config_options[i].name);
133
134         /* Replace the old config file with the new one */
135         fclose(tmpfp);
136         tmpfp = NULL;
137         rename(tempfile, newfile);
138
139 out:
140         free(newfile);
141         free(tempfile);
142         if (tmpfp)
143                 fclose(tmpfp);
144         if (fp)
145                 fclose(fp);
146         return retval;
147 }