Make continuous mode operation the default
[browser-switch] / config-ui / browser-switchboard-cp.c
1 /*
2  * browser-switchboard-cp.c -- a hildon-control-panel applet for
3  * selecting the default browser for Browser Switchboard
4  * 
5  * Copyright (C) 2009-2010 Steven Luo
6  * Copyright (C) 2009-2010 Faheem Pervez
7  * 
8  * Derived from services-cp.c from maemo-control-services
9  * Copyright (c) 2008 Janne Kataja <janne.kataja@iki.fi>
10  * Copyright (c) 2008 Nokia Corporation
11  * 
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25  * USA.
26  */
27
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <glib.h>
35 #include <glib/gstdio.h>
36 #include <gtk/gtk.h>
37
38 #ifdef HILDON
39 #include <hildon/hildon-banner.h>
40 #include <hildon/hildon-program.h>
41
42 #ifdef FREMANTLE
43 #include <hildon/hildon-touch-selector.h>
44 #include <hildon/hildon-picker-button.h>
45 #include <hildon/hildon-caption.h>
46 #include <hildon/hildon-entry.h>
47 #endif /* FREMANTLE */
48
49 #ifdef HILDON_CP_APPLET
50 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
51 #endif /* HILDON_CP_APPLET */
52 #endif /* HILDON */
53
54 #include "config.h"
55
56 #define CONTINUOUS_MODE_DEFAULT 0
57
58 #if defined(HILDON) && defined(FREMANTLE)
59 #define _HILDON_SIZE_DEFAULT (HILDON_SIZE_AUTO_WIDTH|HILDON_SIZE_FINGER_HEIGHT)
60 #endif
61
62 struct browser_entry {
63         char *config;
64         char *displayname;
65 };
66 struct browser_entry browsers[] = {
67         { "microb", "MicroB (stock browser)" }, /* First entry is the default! */
68         { "tear", "Tear" },
69         { "fennec", "Mobile Firefox (Fennec)" },
70         { "opera", "Opera Mobile" },
71         { "midori", "Midori" },
72         { "other", "Other" },
73         { NULL, NULL },
74 };
75
76 struct swb_config orig_cfg;
77
78 struct config_widgets {
79 #if defined(HILDON) && defined(FREMANTLE)
80         GtkWidget *continuous_mode_selector;
81         GtkWidget *default_browser_selector;
82 #else
83         GtkWidget *continuous_mode_off_radio;
84         GtkWidget *continuous_mode_on_radio;
85         GtkWidget *default_browser_combo;
86 #endif
87         GtkWidget *other_browser_cmd_entry;
88         GtkWidget *other_browser_cmd_entry_label;
89 };
90 struct config_widgets cw;
91 GtkWidget *dialog;
92
93
94 /**********************************************************************
95  * Configuration routines
96  **********************************************************************/
97
98 #if defined(HILDON) && defined(FREMANTLE)
99
100 static inline int get_continuous_mode(void) {
101         return hildon_touch_selector_get_active(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), 0);
102 }
103 static inline void set_continuous_mode(int state) {
104         hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), 0, state);
105 }
106
107 static inline char *get_default_browser(void) {
108         return browsers[hildon_touch_selector_get_active(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), 0)].config;
109 }
110
111 #else /* !defined(HILDON) || !defined(FREMANTLE) */
112
113 static inline int get_continuous_mode(void) {
114         return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_on_radio));
115 }
116 static inline void set_continuous_mode(int state) {
117         if (state)
118                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_on_radio), TRUE);
119         else
120                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_off_radio), TRUE);
121 }
122
123 static inline char *get_default_browser(void) {
124         return browsers[gtk_combo_box_get_active(GTK_COMBO_BOX(cw.default_browser_combo))].config;
125 }
126
127 #endif /* defined(HILDON) && defined(FREMANTLE) */
128
129 static void set_default_browser(char *browser) {
130         gint i;
131
132         /* Loop through browsers looking for a match */
133         for (i = 0; browsers[i].config && strcmp(browsers[i].config, browser);
134                         ++i);
135
136         if (!browsers[i].config)
137                 /* No match found, set to the default browser */
138                 i = 0;
139
140 #if defined(HILDON) && defined(FREMANTLE)
141         hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), 0, i);
142 #else
143         gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), i);
144 #endif
145 }
146
147 static inline char *get_other_browser_cmd(void) {
148         return (char *)gtk_entry_get_text(GTK_ENTRY(cw.other_browser_cmd_entry));
149 }
150 static inline void set_other_browser_cmd(char *cmd) {
151         gtk_entry_set_text(GTK_ENTRY(cw.other_browser_cmd_entry), cmd);
152 }
153
154 static void load_config(void) {
155         swb_config_init(&orig_cfg);
156         
157         swb_config_load(&orig_cfg);
158
159         set_continuous_mode(orig_cfg.continuous_mode);
160         set_default_browser(orig_cfg.default_browser);
161         if (orig_cfg.other_browser_cmd)
162                 set_other_browser_cmd(orig_cfg.other_browser_cmd);
163 }
164
165 static void save_config(void) {
166         struct swb_config new_cfg;
167
168         swb_config_copy(&new_cfg, &orig_cfg);
169
170         if (get_continuous_mode() != orig_cfg.continuous_mode) {
171                 new_cfg.continuous_mode = get_continuous_mode();
172                 new_cfg.flags |= SWB_CONFIG_CONTINUOUS_MODE_SET;
173         }
174         if (strcmp(get_default_browser(), orig_cfg.default_browser)) {
175                 new_cfg.default_browser = get_default_browser();
176                 new_cfg.flags |= SWB_CONFIG_DEFAULT_BROWSER_SET;
177         }
178         if (strlen(get_other_browser_cmd()) == 0) {
179                 new_cfg.other_browser_cmd = NULL;
180                 new_cfg.flags &= ~SWB_CONFIG_OTHER_BROWSER_CMD_SET;
181         } else if (!(orig_cfg.other_browser_cmd &&
182                      !strcmp(get_other_browser_cmd(),
183                              orig_cfg.other_browser_cmd))) {
184                 new_cfg.other_browser_cmd = get_other_browser_cmd();
185                 new_cfg.flags |= SWB_CONFIG_OTHER_BROWSER_CMD_SET;
186         }
187
188         swb_config_save(&new_cfg);
189 }
190
191 static void do_reconfig(void) {
192         save_config();
193
194         /* Try to send SIGHUP to any running browser-switchboard process
195            This causes it to reread config files if in continuous_mode, or
196            die so that the config will be reloaded on next start otherwise */
197         system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
198 }
199
200
201 /**********************************************************************
202  * Callbacks
203  **********************************************************************/
204
205 #if defined(HILDON) && defined(FREMANTLE)
206 static void default_browser_selector_callback(GtkWidget *widget,
207                 gint column, gpointer data) {
208 #else
209 static void default_browser_combo_callback(GtkWidget *widget, gpointer data) {
210 #endif
211         if (!strcmp(get_default_browser(), "other")) {
212                 gtk_editable_set_editable(GTK_EDITABLE(cw.other_browser_cmd_entry), TRUE);
213                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, TRUE);
214                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, TRUE);
215         } else {
216                 gtk_editable_set_editable(GTK_EDITABLE(cw.other_browser_cmd_entry), FALSE); /* FREMANTLE: give the text the greyed-out look */
217                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
218                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
219         }
220 }
221
222
223 /**********************************************************************
224  * Interface
225  **********************************************************************/
226
227 #if defined(HILDON) && defined(FREMANTLE)
228 /*
229  * Fremantle Hildon dialog
230  */
231 static GtkDialog *swb_config_dialog(gpointer cp_window) {
232         GtkWidget *dialog_vbox;
233
234         GtkWidget *default_browser_selector_button;
235         GtkWidget *continuous_mode_selector_button;
236         int i;
237         HildonGtkInputMode input_mode;
238
239         dialog = gtk_dialog_new_with_buttons(
240                 "Browser Switchboard",
241                 GTK_WINDOW(cp_window),
242                 GTK_DIALOG_MODAL,
243                 GTK_STOCK_OK,
244                 GTK_RESPONSE_OK,
245                 GTK_STOCK_CANCEL,
246                 GTK_RESPONSE_CANCEL,
247                 NULL);
248
249         dialog_vbox = GTK_DIALOG(dialog)->vbox;
250
251         /* Config options */
252         cw.default_browser_selector = hildon_touch_selector_new_text();
253         for (i = 0; browsers[i].config; ++i)
254                 hildon_touch_selector_append_text(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), browsers[i].displayname);
255         hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), 0, 0);
256         default_browser_selector_button = hildon_picker_button_new(_HILDON_SIZE_DEFAULT, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
257         hildon_button_set_title(HILDON_BUTTON(default_browser_selector_button),
258                                 "Default browser:");
259         hildon_picker_button_set_selector(HILDON_PICKER_BUTTON(default_browser_selector_button), HILDON_TOUCH_SELECTOR(cw.default_browser_selector));
260         hildon_button_set_alignment(HILDON_BUTTON(default_browser_selector_button),
261                                     0, 0.5, 0, 0);
262         g_signal_connect(G_OBJECT(cw.default_browser_selector), "changed",
263                          G_CALLBACK(default_browser_selector_callback), NULL);
264         gtk_box_pack_start(GTK_BOX(dialog_vbox),
265                            default_browser_selector_button, FALSE, FALSE, 0);
266
267         cw.other_browser_cmd_entry = hildon_entry_new(_HILDON_SIZE_DEFAULT);
268         /* Disable autocapitalization and dictionary features for the entry */
269         input_mode = hildon_gtk_entry_get_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry));
270         input_mode &= ~(HILDON_GTK_INPUT_MODE_AUTOCAP |
271                         HILDON_GTK_INPUT_MODE_DICTIONARY);
272         hildon_gtk_entry_set_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry), input_mode);
273
274         cw.other_browser_cmd_entry_label = hildon_caption_new(NULL,
275                         "Command (%s for URI):",
276                         cw.other_browser_cmd_entry,
277                         NULL, HILDON_CAPTION_OPTIONAL);
278         gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
279         gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
280         hildon_gtk_widget_set_theme_size(cw.other_browser_cmd_entry_label, _HILDON_SIZE_DEFAULT);
281         gtk_box_pack_start(GTK_BOX(dialog_vbox),
282                            cw.other_browser_cmd_entry_label, FALSE, FALSE, 0);
283
284         cw.continuous_mode_selector = hildon_touch_selector_new_text();
285         hildon_touch_selector_append_text(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), "Lower memory usage");
286         hildon_touch_selector_append_text(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), "Faster browser startup time");
287
288         continuous_mode_selector_button = hildon_picker_button_new(_HILDON_SIZE_DEFAULT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
289         hildon_button_set_title(HILDON_BUTTON(continuous_mode_selector_button),
290                                 "Optimize Browser Switchboard for:");
291         hildon_picker_button_set_selector(HILDON_PICKER_BUTTON(continuous_mode_selector_button), HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector));
292         hildon_button_set_alignment(HILDON_BUTTON(continuous_mode_selector_button),
293                                     0, 0, 0, 0);
294         set_continuous_mode(CONTINUOUS_MODE_DEFAULT);
295         gtk_box_pack_start(GTK_BOX(dialog_vbox),
296                            continuous_mode_selector_button, FALSE, FALSE, 0);
297
298         gtk_widget_show_all(dialog);
299         return GTK_DIALOG(dialog);
300 }
301
302 #else /* !defined(HILDON) || !defined(FREMANTLE) */
303 /*
304  * GTK+/Diablo Hildon dialog
305  */
306 static GtkDialog *swb_config_dialog(gpointer cp_window) {
307         GtkWidget *dialog_vbox;
308
309         GtkWidget *options_table;
310         GtkWidget *default_browser_combo_label;
311         GtkWidget *continuous_mode_label;
312         int i;
313 #ifdef HILDON
314         HildonGtkInputMode input_mode;
315 #endif
316
317         dialog = gtk_dialog_new_with_buttons(
318                 "Browser Switchboard",
319                 GTK_WINDOW(cp_window),
320                 GTK_DIALOG_MODAL,
321                 GTK_STOCK_OK,
322                 GTK_RESPONSE_OK,
323                 GTK_STOCK_CANCEL,
324                 GTK_RESPONSE_CANCEL,
325                 NULL);
326
327         dialog_vbox = GTK_DIALOG(dialog)->vbox;
328
329         /* Config options */
330         options_table = gtk_table_new(3, 2, FALSE);
331         gtk_table_set_row_spacings(GTK_TABLE(options_table), 5);
332         gtk_box_pack_start(GTK_BOX(dialog_vbox), options_table, FALSE, FALSE, 0);
333
334         cw.default_browser_combo = gtk_combo_box_new_text();
335         for (i = 0; browsers[i].config; ++i)
336                 gtk_combo_box_append_text(GTK_COMBO_BOX(cw.default_browser_combo),
337                                           browsers[i].displayname);
338         gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), 0);
339         default_browser_combo_label = gtk_label_new("Default browser:");
340         gtk_misc_set_alignment(GTK_MISC(default_browser_combo_label), 1, 0.5);
341         g_signal_connect(G_OBJECT(cw.default_browser_combo), "changed",
342                          G_CALLBACK(default_browser_combo_callback), NULL);
343         gtk_table_attach(GTK_TABLE(options_table),
344                         default_browser_combo_label,
345                         0, 1,
346                         0, 1,
347                         GTK_FILL, GTK_FILL|GTK_EXPAND,
348                         5, 0);
349         gtk_table_attach(GTK_TABLE(options_table),
350                         cw.default_browser_combo,
351                         1, 2,
352                         0, 1,
353                         GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND,
354                         5, 0);
355
356         cw.other_browser_cmd_entry = gtk_entry_new();
357 #ifdef HILDON
358         /* Disable autocapitalization and dictionary features for the entry */
359         input_mode = hildon_gtk_entry_get_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry));
360         input_mode &= ~(HILDON_GTK_INPUT_MODE_AUTOCAP |
361                         HILDON_GTK_INPUT_MODE_DICTIONARY);
362         hildon_gtk_entry_set_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry), input_mode);
363 #endif
364         cw.other_browser_cmd_entry_label = gtk_label_new("Command (%s for URI):");
365         gtk_misc_set_alignment(GTK_MISC(cw.other_browser_cmd_entry_label), 1, 0.5);
366         gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
367         gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
368         gtk_table_attach(GTK_TABLE(options_table),
369                         cw.other_browser_cmd_entry_label,
370                         0, 1,
371                         1, 2,
372                         GTK_FILL, GTK_FILL|GTK_EXPAND,
373                         5, 0);
374         gtk_table_attach(GTK_TABLE(options_table),
375                         cw.other_browser_cmd_entry,
376                         1, 2,
377                         1, 2,
378                         GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND,
379                         5, 0);
380         gtk_table_set_row_spacing(GTK_TABLE(options_table), 1, 15);
381
382         continuous_mode_label = gtk_label_new("Optimize Browser Switchboard for:");
383         gtk_misc_set_alignment(GTK_MISC(continuous_mode_label), 0, 0.5);
384         cw.continuous_mode_off_radio = gtk_radio_button_new_with_label(NULL,
385                         "Lower memory usage");
386         cw.continuous_mode_on_radio = gtk_radio_button_new_with_label_from_widget(
387                         GTK_RADIO_BUTTON(cw.continuous_mode_off_radio),
388                         "Faster browser startup time");
389         set_continuous_mode(CONTINUOUS_MODE_DEFAULT);
390         gtk_table_attach(GTK_TABLE(options_table),
391                         continuous_mode_label,
392                         0, 2,
393                         2, 3,
394                         GTK_FILL, GTK_FILL|GTK_EXPAND,
395                         5, 0);
396         gtk_table_attach(GTK_TABLE(options_table),
397                         cw.continuous_mode_off_radio,
398                         0, 2,
399                         3, 4,
400                         GTK_FILL, GTK_FILL|GTK_EXPAND,
401                         20, 0);
402         gtk_table_attach(GTK_TABLE(options_table),
403                         cw.continuous_mode_on_radio,
404                         0, 2,
405                         4, 5,
406                         GTK_FILL, GTK_FILL|GTK_EXPAND,
407                         20, 5);
408         gtk_table_set_row_spacing(GTK_TABLE(options_table), 3, 0);
409
410
411         gtk_widget_show_all(dialog);
412         return GTK_DIALOG(dialog);
413 }
414
415 #endif /* defined(HILDON) && defined(FREMANTLE) */
416
417
418 /**********************************************************************
419  * Entry
420  **********************************************************************/
421
422 #ifdef HILDON_CP_APPLET
423 /*
424  * Application was started from control panel.
425  */
426 osso_return_t execute(osso_context_t *osso,
427                       gpointer userdata, gboolean user_activated) {
428         GtkDialog *dialog;
429         gint response;
430
431         if (osso == NULL)
432                 return OSSO_ERROR;
433
434         dialog = GTK_DIALOG(swb_config_dialog(userdata));
435         load_config();
436
437         response = gtk_dialog_run(dialog);
438         if (response == GTK_RESPONSE_OK)
439                 do_reconfig();
440
441         gtk_widget_destroy(GTK_WIDGET(dialog));
442
443         return OSSO_OK;
444 }
445 #else
446 /*
447  * Application was started from command line.
448  */
449 int main(int argc, char *argv[]) {
450         GtkDialog *dialog;
451         gint response;
452 #ifdef HILDON
453         HildonProgram *program = NULL;
454 #endif
455
456         gtk_init(&argc, &argv);
457 #ifdef HILDON
458         program = HILDON_PROGRAM(hildon_program_get_instance());
459 #endif
460
461         g_set_application_name("Browser Switchboard");
462
463         dialog = GTK_DIALOG(swb_config_dialog(NULL));
464         load_config();
465
466         response = gtk_dialog_run(dialog);
467         if (response == GTK_RESPONSE_OK)
468                 do_reconfig();
469
470         gtk_widget_destroy(GTK_WIDGET(dialog));
471
472         exit(0);
473 }
474 #endif /* HILDON_CP_APPLET */