DSME option intended under INIT option
[skype-autolauncher:skype-autolauncher.git] / src / control-panel-applet / skype-autolauncher-cp-applet.c
1 /** @skype-autolauncher-cp-applet.c
2  * \brief Hildon Control Panel applet for Automatic Skype Launcher
3  *
4  * This applet let's the user select whether to launch Skype at boot
5  * time and whether to monitor it's life through the dsmetool
6  * watchdog.
7  *
8  * Copyright (C) 2011, Igalia, S.L.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of version 2 of the GNU General Public
12  * License as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  * 02111-1307, USA.
23  *
24  * Author: Andres Gomez <agomez@igalia.com>
25  */
26
27
28 #include <libosso.h>
29 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
30 #include <hildon/hildon-banner.h>
31 #include <gtk/gtk.h>
32 #include <gconf/gconf-client.h>
33 #include <gconf/gconf-value.h>
34
35
36
37 static const gchar *gconf_skype_autolauncher_path = "/system/skype-autolauncher";
38 static const gchar *gconf_fullscreen_active_key = "fullscreen_active";
39 static const gchar *gconf_init_active_key = "init_active";
40 static const gchar *gconf_dsme_active_key = "dsme_active";
41
42
43 static void _get_gconf_settings (gboolean *fullscreen_active,
44                                  gboolean *init_active,
45                                  gboolean *dsme_active);
46 static void _set_gconf_settings (gboolean fullscreen_active,
47                                  gboolean init_active,
48                                  gboolean dsme_active);
49 static void _on_init_check_toggled (GtkToggleButton *toggle_button,
50                                     GtkWidget *widget);
51 static GtkDialog* _skype_autolauncher_dialog_new (GtkWidget **fullscreen_check,
52                                                   GtkWidget **init_check,
53                                                   GtkWidget **dsme_check);
54 static void _skype_autolauncher_dialog_show ();
55
56
57 /* --------------------------------------------------------------------------- */
58
59
60 osso_return_t
61 execute (osso_context_t *osso, gpointer data,
62          gboolean user_activated)
63 {
64     _skype_autolauncher_dialog_show ();
65     return OSSO_OK;
66 }
67
68
69 /* --------------------------------------------------------------------------- */
70
71
72 static void
73 _get_gconf_settings (gboolean *fullscreen_active,
74                      gboolean *init_active,
75                      gboolean *dsme_active)
76 {
77     GConfClient *client = NULL;
78     GConfEntry *fullscreen_active_entry = NULL;
79     GConfEntry *init_active_entry = NULL;
80     GConfEntry *dsme_active_entry = NULL;
81     GConfValue *fullscreen_active_value = NULL;
82     GConfValue *init_active_value = NULL;
83     GConfValue *dsme_active_value = NULL;
84     gchar *tmp_string = NULL;
85
86
87     g_assert (NULL != fullscreen_active);
88     g_assert (NULL != init_active);
89     g_assert (NULL != dsme_active);
90
91
92     client = gconf_client_get_default ();
93
94     if (!gconf_client_dir_exists (client,
95                                   gconf_skype_autolauncher_path,
96                                   NULL)) {
97         goto cleanup;
98     }
99
100     tmp_string = g_strdup_printf ("%s/%s",
101                                   gconf_skype_autolauncher_path,
102                                   gconf_fullscreen_active_key);
103     fullscreen_active_entry = gconf_client_get_entry (client,
104                                                       tmp_string,
105                                                       NULL,
106                                                       TRUE,
107                                                       NULL);
108     g_free (tmp_string);
109
110     tmp_string = g_strdup_printf ("%s/%s",
111                                   gconf_skype_autolauncher_path,
112                                   gconf_init_active_key);
113     init_active_entry = gconf_client_get_entry (client,
114                                                 tmp_string,
115                                                 NULL,
116                                                 TRUE,
117                                                 NULL);
118     g_free (tmp_string);
119
120     tmp_string = g_strdup_printf ("%s/%s",
121                                   gconf_skype_autolauncher_path,
122                                   gconf_dsme_active_key);
123     dsme_active_entry = gconf_client_get_entry (client,
124                                                 tmp_string,
125                                                 NULL,
126                                                 TRUE,
127                                                 NULL);
128     g_free (tmp_string);
129
130     fullscreen_active_value = gconf_entry_get_value (fullscreen_active_entry);
131
132     if (NULL != fullscreen_active_value) {
133         *fullscreen_active = gconf_value_get_bool (fullscreen_active_value);
134     }
135
136     init_active_value = gconf_entry_get_value (init_active_entry);
137
138     if (NULL != init_active_value) {
139         *init_active = gconf_value_get_bool (init_active_value);
140     }
141
142     dsme_active_value = gconf_entry_get_value (dsme_active_entry);
143
144     if (NULL != dsme_active_value) {
145         *dsme_active = gconf_value_get_bool (dsme_active_value);
146     }
147
148
149 cleanup:
150
151     if (NULL != fullscreen_active_entry) {
152         gconf_entry_unref (fullscreen_active_entry);
153     }
154     if (NULL != init_active_entry) {
155         gconf_entry_unref (init_active_entry);
156     }
157     if (NULL != dsme_active_entry) {
158         gconf_entry_unref (dsme_active_entry);
159     }
160     g_object_unref (client);
161 }
162
163
164 static void
165 _set_gconf_settings (gboolean fullscreen_active,
166                      gboolean init_active,
167                      gboolean dsme_active)
168 {
169     GConfClient *client = NULL;
170     gchar *tmp_string = NULL;
171
172
173     client = gconf_client_get_default ();
174
175     tmp_string = g_strdup_printf ("%s/%s",
176                                   gconf_skype_autolauncher_path,
177                                   gconf_fullscreen_active_key);
178     gconf_client_set_bool (client,
179                            tmp_string,
180                            fullscreen_active,
181                            NULL);
182     g_free (tmp_string);
183
184     tmp_string = g_strdup_printf ("%s/%s",
185                                   gconf_skype_autolauncher_path,
186                                   gconf_init_active_key);
187     gconf_client_set_bool (client,
188                            tmp_string,
189                            init_active,
190                            NULL);
191     g_free (tmp_string);
192
193     tmp_string = g_strdup_printf ("%s/%s",
194                                   gconf_skype_autolauncher_path,
195                                   gconf_dsme_active_key);
196     gconf_client_set_bool (client,
197                            tmp_string,
198                            dsme_active,
199                            NULL);
200     g_free (tmp_string);
201
202
203 cleanup:
204
205     g_object_unref (client);
206 }
207
208
209 static void
210 _on_init_check_toggled (GtkToggleButton *toggle_button,
211                         GtkWidget *widget)
212 {
213     g_assert (NULL != toggle_button);
214     g_assert (NULL != widget);
215     g_assert (GTK_IS_TOGGLE_BUTTON (toggle_button));
216     g_assert (GTK_IS_WIDGET (widget));
217
218
219     gtk_widget_set_sensitive (widget,
220                               gtk_toggle_button_get_active (toggle_button));
221 }
222
223
224 static GtkDialog *
225 _skype_autolauncher_dialog_new (GtkWidget **fullscreen_check,
226                                 GtkWidget **init_check,
227                                 GtkWidget **dsme_check)
228 {
229     GtkWidget *dialog = NULL;
230     GtkWidget *hbox = NULL;
231
232     g_assert (NULL != fullscreen_check);
233     g_assert (NULL != init_check);
234     g_assert (NULL != dsme_check);
235
236
237     dialog = gtk_dialog_new_with_buttons ("Automatic Skype Launcher Settings",
238                                           NULL,
239                                           GTK_DIALOG_DESTROY_WITH_PARENT |
240                                           GTK_DIALOG_NO_SEPARATOR,
241                                           GTK_STOCK_OK,
242                                           GTK_RESPONSE_ACCEPT,
243                                           GTK_STOCK_CANCEL,
244                                           GTK_RESPONSE_REJECT,
245                                           NULL);
246
247     *fullscreen_check =
248         gtk_check_button_new_with_label ("Try to set Skype in full screen "
249                                          "mode automatically\njust after "
250                                          "launching?");
251     *init_check =
252         gtk_check_button_new_with_label ("Start Skype automatically on "
253                                          "startup?");
254     *dsme_check =
255         gtk_check_button_new_with_label ("Re-start Skype on eventual crashes "
256                                          "or when closing?");
257
258     g_signal_connect (G_OBJECT (*init_check), "toggled",
259                       G_CALLBACK (_on_init_check_toggled), *dsme_check);
260
261     gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
262                        *init_check);
263     hbox = gtk_hbox_new (FALSE, 30);
264     gtk_container_add (GTK_CONTAINER (hbox),
265                        gtk_hbox_new (FALSE, 0));
266     gtk_container_add (GTK_CONTAINER (hbox),
267                        *dsme_check);
268     gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
269                        hbox);
270     gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
271                        *fullscreen_check);
272     gtk_widget_show_all (dialog);
273
274     return GTK_DIALOG (dialog);
275 }
276
277
278 static void
279 _skype_autolauncher_dialog_show ()
280 {
281     GtkWidget *dialog = NULL;
282     GtkWidget *fullscreen_check = NULL;
283     GtkWidget *init_check = NULL;
284     GtkWidget *dsme_check = NULL;
285     gboolean fullscreen_active = TRUE;
286     gboolean init_active = TRUE;
287     gboolean dsme_active = TRUE;
288
289
290     dialog = GTK_WIDGET (_skype_autolauncher_dialog_new (&fullscreen_check,
291                                                          &init_check,
292                                                          &dsme_check));
293
294     _get_gconf_settings (&fullscreen_active, &init_active, &dsme_active);
295
296     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fullscreen_check),
297                                   fullscreen_active);
298     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dsme_check),
299                                   dsme_active);
300     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (init_check),
301                                   init_active);
302     gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (init_check));
303
304     if (GTK_RESPONSE_ACCEPT == gtk_dialog_run (GTK_DIALOG (dialog))) {
305         gboolean new_fullscreen_active =
306             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fullscreen_check));
307         gboolean new_init_active =
308             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (init_check));
309         gboolean new_dsme_active =
310             gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dsme_check));
311
312         if ((fullscreen_active != new_fullscreen_active) ||
313             (init_active != new_init_active) ||
314             (new_init_active && (dsme_active != new_dsme_active))) {
315             GtkWidget *message_dialog =
316                 gtk_message_dialog_new (dialog,
317                                         GTK_DIALOG_DESTROY_WITH_PARENT,
318                                         GTK_MESSAGE_INFO,
319                                         GTK_BUTTONS_OK,
320                                         "The changes would be applied "
321                                         "on next reboot");
322
323             gtk_dialog_run (GTK_DIALOG (message_dialog));
324             gtk_widget_destroy (message_dialog);
325
326             _set_gconf_settings (new_fullscreen_active,
327                                  new_init_active,
328                                  new_dsme_active);
329         }
330     }
331
332     gtk_widget_destroy (dialog);
333 }