Remove maemo.org contact address from copyright notice
[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 Steven Luo
6  * 
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
10  * 
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)
14  * any later version.
15  *
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.
20  *
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,
24  * USA.
25  */
26
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
31 #include <glib/gstdio.h>
32 #include <gtk/gtk.h>
33 #ifdef HILDON
34 #include <hildon/hildon-banner.h>
35 #include <hildon/hildon-program.h>
36 #ifdef HILDON_CP_APPLET
37 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
38 #endif
39 #endif
40
41 struct browser_entry {
42         char *config;
43         char *displayname;
44 };
45 struct browser_entry browsers[] = {
46         { "microb", "MicroB" }, /* First entry is the default! */
47         { "tear", "Tear" },
48         { "fennec", "Mobile Firefox (Fennec)" },
49         { "midori", "Midori" },
50         { "other", "Other" },
51         { NULL, NULL },
52 };
53
54 struct config_widgets {
55         GtkWidget *continuous_mode_check;
56         GtkWidget *default_browser_combo;
57         GtkWidget *other_browser_cmd_entry;
58         GtkWidget *other_browser_cmd_entry_label;
59 };
60 struct config_widgets cw;
61 GtkWidget *dialog;
62
63 static inline int get_continuous_mode(void) {
64         return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cw.continuous_mode_check));
65 }
66 static inline char * get_default_browser(void) {
67         return browsers[gtk_combo_box_get_active(GTK_COMBO_BOX(cw.default_browser_combo))].config;
68 }
69 static inline char * get_other_browser_cmd(void) {
70         return (char *)gtk_entry_get_text(GTK_ENTRY(cw.other_browser_cmd_entry));
71 }
72
73
74 static inline void close_dialog(void) {
75         gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_NONE);
76 }
77
78 static void do_reconfig(void) {
79         printf("continuous_mode: %d\n", get_continuous_mode());
80         printf("default_browser: %s\n", get_default_browser());
81         printf("other_browser_cmd: '%s'\n", get_other_browser_cmd());
82
83         /* TODO: actually implement configuration */
84         return;
85 }
86
87 /**********************************************************************
88  * Callbacks
89  **********************************************************************/
90
91 static void default_browser_combo_callback(GtkWidget *widget, gpointer data) {
92         if (!strcmp(get_default_browser(), "other")) {
93                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, TRUE);
94                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, TRUE);
95         } else {
96                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
97                 gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
98         }
99 }
100
101 static void ok_callback(GtkWidget *widget, gpointer data) {
102         do_reconfig();
103         /* TODO: is there any cleanup necessary? */
104         close_dialog();
105 }
106
107 static void cancel_callback(GtkWidget *widget, gpointer data) {
108         /* TODO: is there any cleanup necessary? */
109         close_dialog();
110 }
111
112
113 /**********************************************************************
114  * Interface
115  **********************************************************************/
116
117 static GtkDialog * swb_config_dialog(void) {
118         GtkWidget *dialog_vbox;
119
120         GtkWidget *options_table;
121         GtkWidget *default_browser_combo_label;
122         int i;
123
124         GtkWidget *action_area;
125         GtkWidget *okbutton, *cancelbutton;
126
127         dialog = gtk_dialog_new();
128         gtk_widget_set_size_request(GTK_WIDGET(dialog), 580, 240);
129         gtk_window_set_title (GTK_WINDOW(dialog), "Browser Switchboard");
130         gtk_window_set_type_hint (GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
131
132         dialog_vbox = GTK_DIALOG(dialog)->vbox;
133
134         /* Config options */
135         options_table = gtk_table_new(3, 2, FALSE);
136         gtk_table_set_row_spacings(GTK_TABLE(options_table), 10);
137         gtk_table_set_col_spacings(GTK_TABLE(options_table), 10);
138         gtk_box_pack_start(GTK_BOX(dialog_vbox), options_table, FALSE, FALSE, 0);
139
140         cw.default_browser_combo = gtk_combo_box_new_text();
141         for (i = 0; browsers[i].config; ++i)
142                 gtk_combo_box_append_text(GTK_COMBO_BOX(cw.default_browser_combo),
143                                           browsers[i].displayname);
144         gtk_combo_box_set_active(GTK_COMBO_BOX(cw.default_browser_combo), 0);
145         default_browser_combo_label = gtk_label_new("Default browser:");
146         gtk_misc_set_alignment(GTK_MISC(default_browser_combo_label), 0, 0.5);
147         g_signal_connect(G_OBJECT(cw.default_browser_combo), "changed",
148                          G_CALLBACK(default_browser_combo_callback), NULL);
149         gtk_table_attach(GTK_TABLE(options_table),
150                         default_browser_combo_label,
151                         0, 1,
152                         0, 1,
153                         GTK_FILL, GTK_FILL|GTK_EXPAND,
154                         0, 0);
155         gtk_table_attach_defaults(GTK_TABLE(options_table),
156                         cw.default_browser_combo,
157                         1, 2,
158                         0, 1);
159         gtk_table_set_row_spacing(GTK_TABLE(options_table), 0, 5);
160
161         cw.other_browser_cmd_entry = gtk_entry_new();
162         cw.other_browser_cmd_entry_label = gtk_label_new("Command (%s for URI):");
163         gtk_misc_set_alignment(GTK_MISC(cw.other_browser_cmd_entry_label), 0, 0.5);
164         gtk_widget_set_sensitive(cw.other_browser_cmd_entry, FALSE);
165         gtk_widget_set_sensitive(cw.other_browser_cmd_entry_label, FALSE);
166         gtk_table_attach(GTK_TABLE(options_table),
167                         cw.other_browser_cmd_entry_label,
168                         0, 1,
169                         1, 2,
170                         0, GTK_FILL|GTK_EXPAND,
171                         0, 0);
172         gtk_table_attach_defaults(GTK_TABLE(options_table),
173                         cw.other_browser_cmd_entry,
174                         1, 2,
175                         1, 2);
176
177         cw.continuous_mode_check = gtk_check_button_new_with_label("Run browser launcher continuously in the background");
178         gtk_table_attach_defaults(GTK_TABLE(options_table),
179                         cw.continuous_mode_check,
180                         0, 2,
181                         2, 3);
182
183
184         /* Dialog buttons */
185         action_area = GTK_DIALOG(dialog)->action_area;
186
187         okbutton = gtk_button_new_from_stock(GTK_STOCK_OK);
188         g_signal_connect(G_OBJECT(okbutton), "clicked",
189                          G_CALLBACK(ok_callback), NULL);
190         gtk_box_pack_start(GTK_BOX(action_area), okbutton, FALSE, FALSE, 0);
191
192         cancelbutton = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
193         g_signal_connect(G_OBJECT(cancelbutton), "clicked",
194                          G_CALLBACK(cancel_callback), NULL);
195         gtk_box_pack_start(GTK_BOX(action_area), cancelbutton, FALSE, FALSE, 0);
196
197         gtk_widget_show_all(dialog);
198         return GTK_DIALOG(dialog);
199 }
200
201
202 /**********************************************************************
203  * Entry
204  **********************************************************************/
205
206 #ifdef HILDON_CP_APPLET
207 /*
208  * Application was started from control panel.
209  */
210 osso_return_t execute(osso_context_t * osso,
211                       gpointer userdata, gboolean user_activated)
212 {
213         HildonProgram *program;
214         program = HILDON_PROGRAM(hildon_program_get_instance());
215         
216         if (osso == NULL)
217                 return OSSO_ERROR;
218
219         /* enable help system on dialog */
220         GtkDialog * dialog = GTK_DIALOG(swb_config_dialog());
221
222         gtk_dialog_run(dialog);
223         gtk_widget_destroy(GTK_WIDGET(dialog));
224
225         return OSSO_OK;
226 }
227 #else
228 /*
229  * Application was started from command line.
230  */
231 int main(int argc, char *argv[])
232 {
233         GtkDialog *dialog;
234 #ifdef HILDON
235         HildonProgram *program = NULL;
236         program = HILDON_PROGRAM(hildon_program_get_instance());
237 #endif
238
239         gtk_init(&argc, &argv);
240
241         g_set_application_name("Browser Switchboard");
242
243         dialog = GTK_DIALOG(swb_config_dialog());
244         gtk_dialog_run(dialog);
245         gtk_widget_destroy(GTK_WIDGET(dialog));
246
247         return 0;
248 }
249 #endif