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