2 * browser-switchboard-cp.c -- a hildon-control-panel applet for
3 * selecting the default browser for Browser Switchboard
5 * Copyright (C) 2009 Steven Luo
7 * Derived from services-cp.c from maemo-control-services
8 * Copyright (c) 2008 Janne Kataja <janne.kataja@iki.fi>
9 * Copyright (c) 2008 Nokia Corporation
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
33 #include <glib/gstdio.h>
36 #include <hildon/hildon-banner.h>
37 #include <hildon/hildon-program.h>
38 #ifdef HILDON_CP_APPLET
39 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
43 #include "configfile.h"
45 struct browser_entry {
49 struct browser_entry browsers[] = {
50 { "microb", "MicroB" }, /* First entry is the default! */
52 { "fennec", "Mobile Firefox (Fennec)" },
53 { "midori", "Midori" },
58 struct config_widgets {
59 GtkWidget *continuous_mode_check;
60 GtkWidget *default_browser_combo;
61 GtkWidget *other_browser_cmd_entry;
62 GtkWidget *other_browser_cmd_entry_label;
64 struct config_widgets cw;
68 /**********************************************************************
69 * Configuration routines
70 **********************************************************************/
72 static inline int get_continuous_mode(void) {
73 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_check));
75 static inline void set_continuous_mode(int state) {
76 return gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_check), (gboolean)state);
79 static inline char * get_default_browser(void) {
80 return browsers[gtk_combo_box_get_active(GTK_COMBO_BOX(cw.default_browser_combo))].config;
82 static void set_default_browser(char *browser) {
85 /* Loop through browsers looking for a match */
86 for (i = 0; browsers[i].config && strcmp(browsers[i].config, browser);
89 if (!browsers[i].config)
90 /* No match found, set to the default browser */
93 gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), i);
96 static inline char * get_other_browser_cmd(void) {
97 return (char *)gtk_entry_get_text(GTK_ENTRY(cw.other_browser_cmd_entry));
99 static inline void set_other_browser_cmd(char *cmd) {
100 gtk_entry_set_text(GTK_ENTRY(cw.other_browser_cmd_entry), cmd);
103 static void load_config(void) {
105 int continuous_mode_seen = 0;
106 int default_browser_seen = 0;
107 int other_browser_cmd_seen = 0;
108 struct swb_config_line line;
110 if (!(fp = open_config_file()))
113 /* Parse the config file
114 TODO: should we handle errors differently than EOF? */
115 if (!parse_config_file_begin())
117 while (!parse_config_file_line(fp, &line)) {
119 if (!strcmp(line.key, "continuous_mode")) {
120 if (!continuous_mode_seen) {
121 set_continuous_mode(atoi(line.value));
122 continuous_mode_seen = 1;
124 } else if (!strcmp(line.key, "default_browser")) {
125 if (!default_browser_seen) {
126 set_default_browser(line.value);
127 default_browser_seen = 1;
129 } else if (!strcmp(line.key, "other_browser_cmd")) {
130 if (!other_browser_cmd_seen) {
131 set_other_browser_cmd(line.value);
132 other_browser_cmd_seen = 1;
139 parse_config_file_end();
146 static void save_config(void) {
147 FILE *fp = NULL, *tmpfp = NULL;
148 char *homedir, *tempfile, *newfile;
150 int continuous_mode_seen = 0;
151 int default_browser_seen = 0;
152 int other_browser_cmd_seen = 0;
153 struct swb_config_line line;
155 /* Put together the path to the new config file and the tempfile */
156 if (!(homedir = getenv("HOME")))
157 homedir = DEFAULT_HOMEDIR;
158 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
159 if (!(newfile = calloc(len, sizeof(char))))
161 /* 4 = strlen(".tmp") */
162 if (!(tempfile = calloc(len+4, sizeof(char)))) {
166 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
167 snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
169 /* Open the temporary file for writing */
170 if (!(tmpfp = fopen(tempfile, "w")))
171 /* TODO: report the error somehow? */
174 /* Open the old config file, if it exists */
175 if ((fp = open_config_file()) && parse_config_file_begin()) {
176 /* Copy the old config file over to the new one line by line,
177 replacing old config values with new ones
178 TODO: should we handle errors differently than EOF? */
179 while (!parse_config_file_line(fp, &line)) {
181 /* Is a config line, print the new value here */
182 if (!strcmp(line.key, "continuous_mode")) {
183 if (!continuous_mode_seen) {
184 fprintf(tmpfp, "%s = %d\n",
186 get_continuous_mode());
187 continuous_mode_seen = 1;
189 } else if (!strcmp(line.key,
190 "default_browser")) {
191 if (!default_browser_seen) {
192 fprintf(tmpfp, "%s = \"%s\"\n",
194 get_default_browser());
195 default_browser_seen = 1;
197 } else if (!strcmp(line.key,
198 "other_browser_cmd")) {
199 if (!other_browser_cmd_seen &&
200 strlen(get_other_browser_cmd())>0) {
201 fprintf(tmpfp, "%s = \"%s\"\n",
203 get_other_browser_cmd());
204 other_browser_cmd_seen = 1;
208 /* Just copy the old line over */
209 fprintf(tmpfp, "%s\n", line.key);
214 parse_config_file_end();
217 /* If we haven't written them yet, write out the new config values */
218 if (!continuous_mode_seen)
219 fprintf(tmpfp, "%s = %d\n",
220 "continuous_mode", get_continuous_mode());
221 if (!default_browser_seen)
222 fprintf(tmpfp, "%s = \"%s\"\n",
223 "default_browser", get_default_browser());
224 if (!other_browser_cmd_seen && strlen(get_other_browser_cmd()) > 0)
225 fprintf(tmpfp, "%s = \"%s\"\n",
226 "other_browser_cmd", get_other_browser_cmd());
228 /* Replace the old config file with the new one */
231 rename(tempfile, newfile);
243 /**********************************************************************
245 **********************************************************************/
247 static void default_browser_combo_callback(GtkWidget *widget, gpointer data) {
248 if (!strcmp(get_default_browser(), "other")) {
249 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, TRUE);
250 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, TRUE);
252 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
253 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
257 static inline void close_dialog(void) {
258 gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_NONE);
261 static void ok_callback(GtkWidget *widget, gpointer data) {
263 /* TODO: is there any cleanup necessary? */
267 static void cancel_callback(GtkWidget *widget, gpointer data) {
268 /* TODO: is there any cleanup necessary? */
273 /**********************************************************************
275 **********************************************************************/
277 static GtkDialog * swb_config_dialog(void) {
278 GtkWidget *dialog_vbox;
280 GtkWidget *options_table;
281 GtkWidget *default_browser_combo_label;
284 GtkWidget *action_area;
285 GtkWidget *okbutton, *cancelbutton;
287 dialog = gtk_dialog_new();
288 gtk_widget_set_size_request(GTK_WIDGET(dialog), 580, 240);
289 gtk_window_set_title (GTK_WINDOW(dialog), "Browser Switchboard");
290 gtk_window_set_type_hint (GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
292 dialog_vbox = GTK_DIALOG(dialog)->vbox;
295 options_table = gtk_table_new(3, 2, FALSE);
296 gtk_table_set_row_spacings(GTK_TABLE(options_table), 10);
297 gtk_table_set_col_spacings(GTK_TABLE(options_table), 10);
298 gtk_box_pack_start(GTK_BOX(dialog_vbox), options_table, FALSE, FALSE, 0);
300 cw.default_browser_combo = gtk_combo_box_new_text();
301 for (i = 0; browsers[i].config; ++i)
302 gtk_combo_box_append_text(GTK_COMBO_BOX(cw.default_browser_combo),
303 browsers[i].displayname);
304 gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), 0);
305 default_browser_combo_label = gtk_label_new("Default browser:");
306 gtk_misc_set_alignment(GTK_MISC(default_browser_combo_label), 0, 0.5);
307 g_signal_connect(G_OBJECT(cw.default_browser_combo), "changed",
308 G_CALLBACK(default_browser_combo_callback), NULL);
309 gtk_table_attach(GTK_TABLE(options_table),
310 default_browser_combo_label,
313 GTK_FILL, GTK_FILL|GTK_EXPAND,
315 gtk_table_attach_defaults(GTK_TABLE(options_table),
316 cw.default_browser_combo,
319 gtk_table_set_row_spacing(GTK_TABLE(options_table), 0, 5);
321 cw.other_browser_cmd_entry = gtk_entry_new();
322 cw.other_browser_cmd_entry_label = gtk_label_new("Command (%s for URI):");
323 gtk_misc_set_alignment(GTK_MISC(cw.other_browser_cmd_entry_label), 0, 0.5);
324 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
325 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
326 gtk_table_attach(GTK_TABLE(options_table),
327 cw.other_browser_cmd_entry_label,
330 0, GTK_FILL|GTK_EXPAND,
332 gtk_table_attach_defaults(GTK_TABLE(options_table),
333 cw.other_browser_cmd_entry,
337 cw.continuous_mode_check = gtk_check_button_new_with_label("Run browser launcher continuously in the background");
338 gtk_table_attach_defaults(GTK_TABLE(options_table),
339 cw.continuous_mode_check,
345 action_area = GTK_DIALOG(dialog)->action_area;
347 okbutton = gtk_button_new_from_stock(GTK_STOCK_OK);
348 g_signal_connect(G_OBJECT(okbutton), "clicked",
349 G_CALLBACK(ok_callback), NULL);
350 gtk_box_pack_start(GTK_BOX(action_area), okbutton, FALSE, FALSE, 0);
352 cancelbutton = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
353 g_signal_connect(G_OBJECT(cancelbutton), "clicked",
354 G_CALLBACK(cancel_callback), NULL);
355 gtk_box_pack_start(GTK_BOX(action_area), cancelbutton, FALSE, FALSE, 0);
357 gtk_widget_show_all(dialog);
358 return GTK_DIALOG(dialog);
362 /**********************************************************************
364 **********************************************************************/
366 #ifdef HILDON_CP_APPLET
368 * Application was started from control panel.
370 osso_return_t execute(osso_context_t * osso,
371 gpointer userdata, gboolean user_activated)
373 HildonProgram *program;
374 program = HILDON_PROGRAM(hildon_program_get_instance());
379 /* enable help system on dialog */
380 GtkDialog * dialog = GTK_DIALOG(swb_config_dialog());
384 gtk_dialog_run(dialog);
385 gtk_widget_destroy(GTK_WIDGET(dialog));
391 * Application was started from command line.
393 int main(int argc, char *argv[])
397 HildonProgram *program = NULL;
398 program = HILDON_PROGRAM(hildon_program_get_instance());
401 gtk_init(&argc, &argv);
403 g_set_application_name("Browser Switchboard");
405 dialog = GTK_DIALOG(swb_config_dialog());
407 gtk_dialog_run(dialog);
408 gtk_widget_destroy(GTK_WIDGET(dialog));