Tweak detection of com.nokia.osso_browser acquisition slightly
[browser-switch] / config-ui / browser-switchboard-config.c
1 /*
2  * browser-switchboard-config.c -- command-line configuration utility for
3  * Browser Switchboard
4  *
5  * Copyright (C) 2009-2010 Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * 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
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <getopt.h>
30
31 #include "config.h"
32 #include "browsers.h"
33
34 extern struct swb_config_option swb_config_options[];
35
36 static int get_config_value(char *name) {
37         struct swb_config cfg;
38         struct swb_config_option *optinfo;
39         ptrdiff_t i;
40         int retval = 1;
41
42         swb_config_init(&cfg);
43
44         if (!swb_config_load(&cfg))
45                 return 1;
46
47         for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
48                 if (strcmp(name, optinfo->name))
49                         continue;
50
51                 i = optinfo - swb_config_options;
52                 switch (optinfo->type) {
53                   case SWB_CONFIG_OPT_STRING:
54                         if (*(char **)cfg.entries[i])
55                                 printf("%s\n", *(char **)cfg.entries[i]);
56                         break;
57                   case SWB_CONFIG_OPT_INT:
58                         printf("%d\n", *(int *)cfg.entries[i]);
59                         break;
60                   default:
61                         break;
62                 }
63                 retval = 0;
64                 break;
65         }
66
67         swb_config_free(&cfg);
68
69         return retval;
70 }
71
72 static int get_default_browser(void) {
73         struct swb_config cfg;
74         int i;
75
76         swb_config_init(&cfg);
77
78         if (!swb_config_load(&cfg))
79                 return 1;
80
81         /* Check to see if the configured default browser is installed
82            If not, report the default default browser */
83         for (i = 0; browsers[i].config; ++i) {
84                 if (strcmp(browsers[i].config, cfg.default_browser))
85                         continue;
86
87                 if (browsers[i].binary && access(browsers[i].binary, X_OK))
88                         printf("%s\n", browsers[0].config);
89                 else
90                         printf("%s\n", browsers[i].config);
91
92                 break;
93         }
94
95         if (!browsers[i].config)
96                 /* Unknown browser configured as default, report the default
97                    default browser */
98                 printf("%s\n", browsers[0].config);
99
100         swb_config_free(&cfg);
101
102         return 0;
103 }
104
105 static int set_config_value(char *name, char *value) {
106         struct swb_config cfg;
107         struct swb_config_option *optinfo;
108         ptrdiff_t i;
109         int retval = 1;
110
111         swb_config_init(&cfg);
112
113         if (!swb_config_load(&cfg))
114                 return 1;
115
116         for (optinfo = swb_config_options; optinfo->name; ++optinfo) {
117                 if (strcmp(name, optinfo->name))
118                         continue;
119
120                 i = optinfo - swb_config_options;
121                 switch (optinfo->type) {
122                   case SWB_CONFIG_OPT_STRING:
123                         /* Free any existing string */
124                         if (cfg.flags & optinfo->set_mask)
125                                 free(*(char **)cfg.entries[i]);
126
127                         if (strlen(value) == 0) {
128                                 /* If the new value is empty, clear the config
129                                    setting */
130                                 *(char **)cfg.entries[i] = NULL;
131                                 cfg.flags &= ~optinfo->set_mask;
132                         } else {
133                                 /* Make a copy of the string -- it's not safe
134                                    to free value, which comes from argv */
135                                 if (!(*(char **)cfg.entries[i] =
136                                       strdup(value)))
137                                         exit(1);
138                                 cfg.flags |= optinfo->set_mask;
139                         }
140                         break;
141                   case SWB_CONFIG_OPT_INT:
142                         if (strlen(value) == 0) {
143                                 /* If the new value is empty, clear the config
144                                    setting */
145                                 cfg.flags &= ~optinfo->set_mask;
146                         } else {
147                                 *(int *)cfg.entries[i] = atoi(value);
148                                 cfg.flags |= optinfo->set_mask;
149                         }
150                         break;
151                 }
152                 retval = 0;
153                 break;
154         }
155
156         if (!retval)
157                 if (!swb_config_save(&cfg))
158                         retval = 1;
159
160         swb_config_free(&cfg);
161
162         /* Try to send SIGHUP to any running browser-switchboard process
163            This causes it to reread config files if in continuous_mode, or
164            die so that the config will be reloaded on next start otherwise */
165         system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
166
167         return retval;
168 }
169
170 void usage(void) {
171         printf("Usage:\n");
172         printf("  browser-switchboard-config -b -- Display default browser\n");
173         printf("  browser-switchboard-config -c -- Display command used when default browser is \"other\"\n");
174         printf("  browser-switchboard-config -m -- Display continuous mode setting\n");
175         printf("  browser-switchboard-config -o option -- Display value of option\n");
176         printf("\n");
177         printf("  browser-switchboard-config -s [-b|-c|-m|-o option] value -- Set the selected option to value\n");
178         printf("\n");
179         printf("  browser-switchboard-config -h -- Show this message\n");
180 }
181
182 int main(int argc, char **argv) {
183         int opt, done = 0;
184         int set = 0;
185         char *selected_opt = NULL;
186
187         while (!done && (opt = getopt(argc, argv, "hsbcmo:")) != -1) {
188                 switch (opt) {
189                   case 'h':
190                         usage();
191                         exit(0);
192                         break;
193                   case 's':
194                         set = 1;
195                         break;
196                   case 'b':
197                         selected_opt = "default_browser";
198                         done = 1;
199                         break;
200                   case 'c':
201                         selected_opt = "other_browser_cmd";
202                         done = 1;
203                         break;
204                   case 'm':
205                         selected_opt = "continuous_mode";
206                         done = 1;
207                         break;
208                   case 'o':
209                         selected_opt = optarg;
210                         done = 1;
211                         break;
212                   default:
213                         usage();
214                         exit(1);
215                         break;
216                 }
217         }
218
219         if (!selected_opt) {
220                 printf("Must specify one of -b, -c, -m, -o\n");
221                 usage();
222                 exit(1);
223         }
224
225         if (set) {
226                 if (optind >= argc) {
227                         printf("Value to set config option to not provided\n");
228                         usage();
229                         exit(1);
230                 }
231                 return set_config_value(selected_opt, argv[optind]);
232         } else if (!strcmp(selected_opt, "default_browser"))
233                 /* Default browser value needs special handling */
234                 return get_default_browser();
235         else
236                 return get_config_value(selected_opt);
237 }