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