Update debian/changelog, bump version number
[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         { "midori", "Midori" },
71         { "other", "Other" },
72         { NULL, NULL },
73 };
74
75 struct swb_config orig_cfg;
76
77 struct config_widgets {
78 #if defined(HILDON) && defined(FREMANTLE)
79         GtkWidget *continuous_mode_selector;
80         GtkWidget *default_browser_selector;
81 #else
82         GtkWidget *continuous_mode_off_radio;
83         GtkWidget *continuous_mode_on_radio;
84         GtkWidget *default_browser_combo;
85 #endif
86         GtkWidget *other_browser_cmd_entry;
87         GtkWidget *other_browser_cmd_entry_label;
88 };
89 struct config_widgets cw;
90 GtkWidget *dialog;
91
92
93 /**********************************************************************
94  * Configuration routines
95  **********************************************************************/
96
97 #if defined(HILDON) && defined(FREMANTLE)
98
99 static inline int get_continuous_mode(void) {
100         return hildon_touch_selector_get_active(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), 0);
101 }
102 static inline void set_continuous_mode(int state) {
103         hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), 0, state);
104 }
105
106 static inline char *get_default_browser(void) {
107         return browsers[hildon_touch_selector_get_active(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), 0)].config;
108 }
109
110 #else /* !defined(HILDON) || !defined(FREMANTLE) */
111
112 static inline int get_continuous_mode(void) {
113         return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_on_radio));
114 }
115 static inline void set_continuous_mode(int state) {
116         if (state)
117                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_on_radio), TRUE);
118         else
119                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_off_radio), TRUE);
120 }
121
122 static inline char *get_default_browser(void) {
123         return browsers[gtk_combo_box_get_active(GTK_COMBO_BOX(cw.default_browser_combo))].config;
124 }
125
126 #endif /* defined(HILDON) && defined(FREMANTLE) */
127
128 static void set_default_browser(char *browser) {
129         gint i;
130
131         /* Loop through browsers looking for a match */
132         for (i = 0; browsers[i].config && strcmp(browsers[i].config, browser);
133                         ++i);
134
135         if (!browsers[i].config)
136                 /* No match found, set to the default browser */
137                 i = 0;
138
139 #if defined(HILDON) && defined(FREMANTLE)
140         hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), 0, i);
141 #else
142         gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), i);
143 #endif
144 }
145
146 static inline char *get_other_browser_cmd(void) {
147         return (char *)gtk_entry_get_text(GTK_ENTRY(cw.other_browser_cmd_entry));
148 }
149 static inline void set_other_browser_cmd(char *cmd) {
150         gtk_entry_set_text(GTK_ENTRY(cw.other_browser_cmd_entry), cmd);
151 }
152
153 static void load_config(void) {
154         swb_config_init(&orig_cfg);
155         
156         swb_config_load(&orig_cfg);
157
158         set_continuous_mode(orig_cfg.continuous_mode);
159         set_default_browser(orig_cfg.default_browser);
160         if (orig_cfg.other_browser_cmd)
161                 set_other_browser_cmd(orig_cfg.other_browser_cmd);
162 }
163
164 static void save_config(void) {
165         struct swb_config new_cfg;
166
167         swb_config_copy(&new_cfg, &orig_cfg);
168
169         if (get_continuous_mode() != orig_cfg.continuous_mode) {
170                 new_cfg.continuous_mode = get_continuous_mode();
171                 new_cfg.flags |= SWB_CONFIG_CONTINUOUS_MODE_SET;
172         }
173         if (strcmp(get_default_browser(), orig_cfg.default_browser)) {
174                 new_cfg.default_browser = get_default_browser();
175                 new_cfg.flags |= SWB_CONFIG_DEFAULT_BROWSER_SET;
176         }
177         if (strlen(get_other_browser_cmd()) == 0) {
178                 new_cfg.other_browser_cmd = NULL;
179                 new_cfg.flags &= ~SWB_CONFIG_OTHER_BROWSER_CMD_SET;
180         } else if (!(orig_cfg.other_browser_cmd &&
181                      !strcmp(get_other_browser_cmd(),
182                              orig_cfg.other_browser_cmd))) {
183                 new_cfg.other_browser_cmd = get_other_browser_cmd();
184                 new_cfg.flags |= SWB_CONFIG_OTHER_BROWSER_CMD_SET;
185         }
186
187         swb_config_save(&new_cfg);
188 }
189
190 static void do_reconfig(void) {
191         save_config();
192
193         /* Try to send SIGHUP to any running browser-switchboard process
194            This causes it to reread config files if in continuous_mode, or
195            die so that the config will be reloaded on next start otherwise */
196         system("kill -HUP `pidof browser-switchboard` > /dev/null 2>&1");
197 }
198
199
200 /**********************************************************************
201  * Callbacks
202  **********************************************************************/
203
204 #if defined(HILDON) && defined(FREMANTLE)
205 static void default_browser_selector_callback(GtkWidget *widget,
206                 gint column, gpointer data) {
207 #else
208 static void default_browser_combo_callback(GtkWidget *widget, gpointer data) {
209 #endif
210         if (!strcmp(get_default_browser(), "other")) {
211                 gtk_editable_set_editable(GTK_EDITABLE(cw.other_browser_cmd_entry), TRUE);
212                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, TRUE);
213                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, TRUE);
214         } else {
215                 gtk_editable_set_editable(GTK_EDITABLE(cw.other_browser_cmd_entry), FALSE); /* FREMANTLE: give the text the greyed-out look */
216                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
217                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
218         }
219 }
220
221
222 /**********************************************************************
223  * Interface
224  **********************************************************************/
225
226 #if defined(HILDON) && defined(FREMANTLE)
227 /*
228  * Fremantle Hildon dialog
229  */
230 static GtkDialog *swb_config_dialog(gpointer cp_window) {
231         GtkWidget *dialog_vbox;
232
233         GtkWidget *default_browser_selector_button;
234         GtkWidget *continuous_mode_selector_button;
235         int i;
236         HildonGtkInputMode input_mode;
237
238         dialog = gtk_dialog_new_with_buttons(
239                 "Browser Switchboard",
240                 GTK_WINDOW(cp_window),
241                 GTK_DIALOG_MODAL,
242                 GTK_STOCK_OK,
243                 GTK_RESPONSE_OK,
244                 GTK_STOCK_CANCEL,
245                 GTK_RESPONSE_CANCEL,
246                 NULL);
247
248         dialog_vbox = GTK_DIALOG(dialog)->vbox;
249
250         /* Config options */
251         cw.default_browser_selector = hildon_touch_selector_new_text();
252         for (i = 0; browsers[i].config; ++i)
253                 hildon_touch_selector_append_text(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), browsers[i].displayname);
254         hildon_touch_selector_set_active(HILDON_TOUCH_SELECTOR(cw.default_browser_selector), 0, 0);
255         default_browser_selector_button = hildon_picker_button_new(_HILDON_SIZE_DEFAULT, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL);
256         hildon_button_set_title(HILDON_BUTTON(default_browser_selector_button),
257                                 "Default browser:");
258         hildon_picker_button_set_selector(HILDON_PICKER_BUTTON(default_browser_selector_button), HILDON_TOUCH_SELECTOR(cw.default_browser_selector));
259         hildon_button_set_alignment(HILDON_BUTTON(default_browser_selector_button),
260                                     0, 0.5, 0, 0);
261         g_signal_connect(G_OBJECT(cw.default_browser_selector), "changed",
262                          G_CALLBACK(default_browser_selector_callback), NULL);
263         gtk_box_pack_start(GTK_BOX(dialog_vbox),
264                            default_browser_selector_button, FALSE, FALSE, 0);
265
266         cw.other_browser_cmd_entry = hildon_entry_new(_HILDON_SIZE_DEFAULT);
267         /* Disable autocapitalization and dictionary features for the entry */
268         input_mode = hildon_gtk_entry_get_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry));
269         input_mode &= ~(HILDON_GTK_INPUT_MODE_AUTOCAP |
270                         HILDON_GTK_INPUT_MODE_DICTIONARY);
271         hildon_gtk_entry_set_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry), input_mode);
272
273         cw.other_browser_cmd_entry_label = hildon_caption_new(NULL,
274                         "Command (%s for URI):",
275                         cw.other_browser_cmd_entry,
276                         NULL, HILDON_CAPTION_OPTIONAL);
277         gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
278         gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
279         hildon_gtk_widget_set_theme_size(cw.other_browser_cmd_entry_label, _HILDON_SIZE_DEFAULT);
280         gtk_box_pack_start(GTK_BOX(dialog_vbox),
281                            cw.other_browser_cmd_entry_label, FALSE, FALSE, 0);
282
283         cw.continuous_mode_selector = hildon_touch_selector_new_text();
284         hildon_touch_selector_append_text(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), "Lower memory usage");
285         hildon_touch_selector_append_text(HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector), "Faster browser startup time");
286
287         continuous_mode_selector_button = hildon_picker_button_new(_HILDON_SIZE_DEFAULT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
288         hildon_button_set_title(HILDON_BUTTON(continuous_mode_selector_button),
289                                 "Optimize Browser Switchboard for:");
290         hildon_picker_button_set_selector(HILDON_PICKER_BUTTON(continuous_mode_selector_button), HILDON_TOUCH_SELECTOR(cw.continuous_mode_selector));
291         hildon_button_set_alignment(HILDON_BUTTON(continuous_mode_selector_button),
292                                     0, 0, 0, 0);
293         set_continuous_mode(CONTINUOUS_MODE_DEFAULT);
294         gtk_box_pack_start(GTK_BOX(dialog_vbox),
295                            continuous_mode_selector_button, FALSE, FALSE, 0);
296
297         gtk_widget_show_all(dialog);
298         return GTK_DIALOG(dialog);
299 }
300
301 #else /* !defined(HILDON) || !defined(FREMANTLE) */
302 /*
303  * GTK+/Diablo Hildon dialog
304  */
305 static GtkDialog *swb_config_dialog(gpointer cp_window) {
306         GtkWidget *dialog_vbox;
307
308         GtkWidget *options_table;
309         GtkWidget *default_browser_combo_label;
310         GtkWidget *continuous_mode_label;
311         int i;
312 #ifdef HILDON
313         HildonGtkInputMode input_mode;
314 #endif
315
316         dialog = gtk_dialog_new_with_buttons(
317                 "Browser Switchboard",
318                 GTK_WINDOW(cp_window),
319                 GTK_DIALOG_MODAL,
320                 GTK_STOCK_OK,
321                 GTK_RESPONSE_OK,
322                 GTK_STOCK_CANCEL,
323                 GTK_RESPONSE_CANCEL,
324                 NULL);
325
326         dialog_vbox = GTK_DIALOG(dialog)->vbox;
327
328         /* Config options */
329         options_table = gtk_table_new(3, 2, FALSE);
330         gtk_table_set_row_spacings(GTK_TABLE(options_table), 5);
331         gtk_box_pack_start(GTK_BOX(dialog_vbox), options_table, FALSE, FALSE, 0);
332
333         cw.default_browser_combo = gtk_combo_box_new_text();
334         for (i = 0; browsers[i].config; ++i)
335                 gtk_combo_box_append_text(GTK_COMBO_BOX(cw.default_browser_combo),
336                                           browsers[i].displayname);
337         gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), 0);
338         default_browser_combo_label = gtk_label_new("Default browser:");
339         gtk_misc_set_alignment(GTK_MISC(default_browser_combo_label), 1, 0.5);
340         g_signal_connect(G_OBJECT(cw.default_browser_combo), "changed",
341                          G_CALLBACK(default_browser_combo_callback), NULL);
342         gtk_table_attach(GTK_TABLE(options_table),
343                         default_browser_combo_label,
344                         0, 1,
345                         0, 1,
346                         GTK_FILL, GTK_FILL|GTK_EXPAND,
347                         5, 0);
348         gtk_table_attach(GTK_TABLE(options_table),
349                         cw.default_browser_combo,
350                         1, 2,
351                         0, 1,
352                         GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND,
353                         5, 0);
354
355         cw.other_browser_cmd_entry = gtk_entry_new();
356 #ifdef HILDON
357         /* Disable autocapitalization and dictionary features for the entry */
358         input_mode = hildon_gtk_entry_get_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry));
359         input_mode &= ~(HILDON_GTK_INPUT_MODE_AUTOCAP |
360                         HILDON_GTK_INPUT_MODE_DICTIONARY);
361         hildon_gtk_entry_set_input_mode(GTK_ENTRY(cw.other_browser_cmd_entry), input_mode);
362 #endif
363         cw.other_browser_cmd_entry_label = gtk_label_new("Command (%s for URI):");
364         gtk_misc_set_alignment(GTK_MISC(cw.other_browser_cmd_entry_label), 1, 0.5);
365         gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
366         gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
367         gtk_table_attach(GTK_TABLE(options_table),
368                         cw.other_browser_cmd_entry_label,
369                         0, 1,
370                         1, 2,
371                         GTK_FILL, GTK_FILL|GTK_EXPAND,
372                         5, 0);
373         gtk_table_attach(GTK_TABLE(options_table),
374                         cw.other_browser_cmd_entry,
375                         1, 2,
376                         1, 2,
377                         GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND,
378                         5, 0);
379         gtk_table_set_row_spacing(GTK_TABLE(options_table), 1, 15);
380
381         continuous_mode_label = gtk_label_new("Optimize Browser Switchboard for:");
382         gtk_misc_set_alignment(GTK_MISC(continuous_mode_label), 0, 0.5);
383         cw.continuous_mode_off_radio = gtk_radio_button_new_with_label(NULL,
384                         "Lower memory usage");
385         cw.continuous_mode_on_radio = gtk_radio_button_new_with_label_from_widget(
386                         GTK_RADIO_BUTTON(cw.continuous_mode_off_radio),
387                         "Faster browser startup time");
388         set_continuous_mode(CONTINUOUS_MODE_DEFAULT);
389         gtk_table_attach(GTK_TABLE(options_table),
390                         continuous_mode_label,
391                         0, 2,
392                         2, 3,
393                         GTK_FILL, GTK_FILL|GTK_EXPAND,
394                         5, 0);
395         gtk_table_attach(GTK_TABLE(options_table),
396                         cw.continuous_mode_off_radio,
397                         0, 2,
398                         3, 4,
399                         GTK_FILL, GTK_FILL|GTK_EXPAND,
400                         20, 0);
401         gtk_table_attach(GTK_TABLE(options_table),
402                         cw.continuous_mode_on_radio,
403                         0, 2,
404                         4, 5,
405                         GTK_FILL, GTK_FILL|GTK_EXPAND,
406                         20, 5);
407         gtk_table_set_row_spacing(GTK_TABLE(options_table), 3, 0);
408
409
410         gtk_widget_show_all(dialog);
411         return GTK_DIALOG(dialog);
412 }
413
414 #endif /* defined(HILDON) && defined(FREMANTLE) */
415
416
417 /**********************************************************************
418  * Entry
419  **********************************************************************/
420
421 #ifdef HILDON_CP_APPLET
422 /*
423  * Application was started from control panel.
424  */
425 osso_return_t execute(osso_context_t *osso,
426                       gpointer userdata, gboolean user_activated) {
427         GtkDialog *dialog;
428         gint response;
429
430         if (osso == NULL)
431                 return OSSO_ERROR;
432
433         dialog = GTK_DIALOG(swb_config_dialog(userdata));
434         load_config();
435
436         response = gtk_dialog_run(dialog);
437         if (response == GTK_RESPONSE_OK)
438                 do_reconfig();
439
440         gtk_widget_destroy(GTK_WIDGET(dialog));
441
442         return OSSO_OK;
443 }
444 #else
445 /*
446  * Application was started from command line.
447  */
448 int main(int argc, char *argv[]) {
449         GtkDialog *dialog;
450         gint response;
451 #ifdef HILDON
452         HildonProgram *program = NULL;
453 #endif
454
455         gtk_init(&argc, &argv);
456 #ifdef HILDON
457         program = HILDON_PROGRAM(hildon_program_get_instance());
458 #endif
459
460         g_set_application_name("Browser Switchboard");
461
462         dialog = GTK_DIALOG(swb_config_dialog(NULL));
463         load_config();
464
465         response = gtk_dialog_run(dialog);
466         if (response == GTK_RESPONSE_OK)
467                 do_reconfig();
468
469         gtk_widget_destroy(GTK_WIDGET(dialog));
470
471         exit(0);
472 }
473 #endif /* HILDON_CP_APPLET */