Add sections' names.
[tweakr] / modules / maemo-tweaks-mce.c
1 /*
2  * vim:ts=4:sw=4:et:cindent:cino=(0
3  */ 
4
5 #include <config.h>
6
7 #include <gtk/gtk.h>
8 #include <hildon/hildon-picker-button.h>
9 #include <hildon/hildon-touch-selector.h>
10 #include <hildon/hildon-note.h>
11
12 #include "libmaemo-tweaks-section/maemo-tweaks-section.h"
13 #include "libmaemo-tweaks-section/maemo-tweaks-module.h"
14
15
16 #define MAEMO_TWEAKS_TYPE_MCE_SECTION \
17         (maemo_tweaks_mce_section_type)
18 #define MAEMO_TWEAKS_MCE_SECTION(obj) \
19         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
20         MAEMO_TWEAKS_TYPE_MCE_SECTION, \
21         MaemoTweaksMceSection))
22 #define MAEMO_TWEAKS_MCE_SECTION_CLASS(k) \
23         (G_TYPE_CHECK_CLASS_CAST((k), \
24         MAEMO_TWEAKS_TYPE_MCE_SECTION, \
25         MaemoTweaksMceSectionClass))
26 #define MAEMO_TWEAKS_IS_MCE_SECTION(obj) \
27         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
28         MAEMO_TWEAKS_TYPE_MCE_SECTION))
29
30 #define DISABLE_LABEL "Disable"
31 #define SHOW_MENU_LABEL "Show menu"
32 #define POWER_OFF_LABEL "Power off"
33 #define LOCK_LABEL "Lock"
34
35 enum {
36     SHORT_POWER_KEY_DISABLED,
37     SHORT_POWER_KEY_MENU,
38     SHORT_POWER_KEY_OFF,
39     SHORT_POWER_KEY_N
40 };
41
42 enum {
43     LONG_POWER_KEY_DISABLED,
44     LONG_POWER_KEY_MENU,
45     LONG_POWER_KEY_OFF,
46     LONG_POWER_KEY_N
47 };
48
49 enum {
50     DOUBLE_POWER_KEY_DISABLED,
51     DOUBLE_POWER_KEY_MENU,
52     DOUBLE_POWER_KEY_OFF,
53     DOUBLE_POWER_KEY_LOCK,
54     DOUBLE_POWER_KEY_N
55 };
56
57
58 typedef struct _picker_t
59 {
60     guint index;
61     const gchar *value;
62     const gchar *label;
63 } picker_t;
64
65 static picker_t spk [] = {
66     {SHORT_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
67     {SHORT_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
68     {SHORT_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL}
69 };
70
71 static picker_t lpk [] = {
72     {LONG_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
73     {LONG_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
74     {LONG_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL}
75 };
76
77 static picker_t dpk [] = {
78     {DOUBLE_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
79     {DOUBLE_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
80     {DOUBLE_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL},
81     {DOUBLE_POWER_KEY_LOCK, "tklock", LOCK_LABEL}
82 };
83
84
85 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
86 typedef struct _MaemoTweaksMceSectionClass
87                MaemoTweaksMceSectionClass;
88
89 struct _MaemoTweaksMceSection
90 {
91     MaemoTweaksSection parent_instance;
92
93     GKeyFile *ini;
94     GtkWidget *short_power_key;
95     GtkWidget *long_power_key;
96     GtkWidget *double_power_key;
97
98     guint value_changed : 1;
99 };
100
101 struct _MaemoTweaksMceSectionClass
102 {
103     MaemoTweaksSectionClass parent_class;
104 };
105
106
107 static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
108 static void maemo_tweaks_mce_section_class_init
109     (MaemoTweaksMceSectionClass *class);
110 static void maemo_tweaks_mce_section_init
111     (MaemoTweaksMceSection *section);
112 static void maemo_tweaks_mce_section_dispose (GObject *obj);
113
114 static gboolean _save (MaemoTweaksSection *section,
115                        gboolean *requires_restart);
116
117 static GType maemo_tweaks_mce_section_type = 0;
118 static MaemoTweaksSectionClass *
119     maemo_tweaks_mce_section_parent_class = NULL;
120
121
122 G_MODULE_EXPORT void
123 maemo_tweaks_module_load (MaemoTweaksModule *module)
124 {
125     maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
126 }
127
128 G_MODULE_EXPORT void
129 maemo_tweaks_module_unload (MaemoTweaksModule *module)
130 {
131 }
132
133 static GType
134 maemo_tweaks_mce_section_get_type (GTypeModule *module)
135 {
136     if (!maemo_tweaks_mce_section_type)
137     {
138         static const GTypeInfo section_info =
139         {
140             sizeof (MaemoTweaksMceSectionClass),
141             (GBaseInitFunc) NULL,
142             (GBaseFinalizeFunc) NULL,
143             (GClassInitFunc) maemo_tweaks_mce_section_class_init,
144             NULL,           /* class_finalize */
145             NULL,           /* class_data     */
146             sizeof (MaemoTweaksMceSection),
147             0,              /* n_preallocs    */
148             (GInstanceInitFunc) maemo_tweaks_mce_section_init
149         };
150
151         maemo_tweaks_mce_section_type =
152             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
153                                          "MaemoTweaksMceSection",
154                                          &section_info, 0);
155     }
156
157     return maemo_tweaks_mce_section_type;
158 }
159
160 static void
161 maemo_tweaks_mce_section_class_init
162     (MaemoTweaksMceSectionClass *klass)
163 {
164     GObjectClass *object_class = G_OBJECT_CLASS (klass);
165     MaemoTweaksSectionClass *section_class =
166         MAEMO_TWEAKS_SECTION_CLASS (klass);
167
168     maemo_tweaks_mce_section_parent_class =
169         g_type_class_peek_parent (klass);
170
171     section_class->name   = "_Mce";
172     section_class->save = _save;
173
174     object_class->dispose = maemo_tweaks_mce_section_dispose;
175 }
176
177 static void _value_changed (HildonPickerButton *b, gpointer user_data)
178 {
179     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
180
181     section->value_changed = TRUE;
182 }
183
184 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
185 {
186     gint i;
187     GtkWidget *button, *selector;
188
189     selector = hildon_touch_selector_new_text ();
190     for (i = 0; i < SHORT_POWER_KEY_N; i++)
191     {
192         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
193                                            spk[i].label);
194     }
195
196     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
197                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
198
199     hildon_button_set_title (HILDON_BUTTON (button),
200                              "Power key: short press");
201     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
202     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
203                                        HILDON_TOUCH_SELECTOR (selector));
204
205     g_signal_connect (G_OBJECT (button), "value-changed",
206                       G_CALLBACK (_value_changed), section);
207
208     gtk_widget_show (button);
209     return button;
210 }
211
212 GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
213 {
214     gint i;
215     GtkWidget *button, *selector;
216
217     selector = hildon_touch_selector_new_text ();
218     for (i = 0; i < LONG_POWER_KEY_N; i++)
219     {
220         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
221                                            lpk[i].label);
222     }
223
224     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
225                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
226
227     hildon_button_set_title (HILDON_BUTTON (button),
228                              "Power key: long press");
229     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
230     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
231                                        HILDON_TOUCH_SELECTOR (selector));
232
233     g_signal_connect (G_OBJECT (button), "value-changed",
234                       G_CALLBACK (_value_changed), section);
235
236     gtk_widget_show (button);
237     return button;
238 }
239
240 GtkWidget * _build_double_power_key (MaemoTweaksMceSection *section)
241 {
242     gint i;
243     GtkWidget *button, *selector;
244
245     selector = hildon_touch_selector_new_text ();
246     for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
247     {
248         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
249                                            dpk[i].label);
250     }
251
252     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
253                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
254
255     hildon_button_set_title (HILDON_BUTTON (button),
256                              "Power key: double press");
257     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
258     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
259                                        HILDON_TOUCH_SELECTOR (selector));
260
261     g_signal_connect (G_OBJECT (button), "value-changed",
262                       G_CALLBACK (_value_changed), section);
263
264     gtk_widget_show (button);
265     return button;
266 }
267
268
269 static void
270 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
271 {
272     MaemoTweaksSection *iface;
273     gchar *short_power_key_value;
274     gchar *long_power_key_value;
275     gchar *double_power_key_value;
276     gint i;
277
278     section->short_power_key = _build_short_power_key (section);
279     section->long_power_key = _build_long_power_key (section);
280     section->double_power_key = _build_double_power_key (section);
281
282     section->ini = g_key_file_new ();
283
284     if (!g_key_file_load_from_file (section->ini, MCE,
285                                     G_KEY_FILE_NONE, NULL))
286     {
287         g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
288         return;
289     }
290
291     short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
292                                                    "PowerKeyShortAction",
293                                                    NULL);
294     long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
295                                                   "PowerKeyLongAction",
296                                                   NULL);
297     double_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
298                                                     "PowerKeyDoubleAction",
299                                                     NULL);
300
301     for (i = 0; i < SHORT_POWER_KEY_N; i++)
302     {
303         if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
304         {
305             hildon_picker_button_set_active
306                 (HILDON_PICKER_BUTTON (section->short_power_key), i);
307         }
308     }
309
310     for (i = 0; i < LONG_POWER_KEY_N; i++)
311     {
312         if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
313         {
314             hildon_picker_button_set_active
315                 (HILDON_PICKER_BUTTON (section->long_power_key), i);
316         }
317     }
318
319     for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
320     {
321         if (g_strcmp0 (double_power_key_value, dpk[i].value) == 0)
322         {
323             hildon_picker_button_set_active
324                 (HILDON_PICKER_BUTTON (section->double_power_key), i);
325         }
326     }
327
328     section->value_changed = FALSE;
329
330     iface = MAEMO_TWEAKS_SECTION (section);
331     iface->name = "Mce";
332     iface->widget = gtk_vbox_new (FALSE, 0);
333     gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
334                         TRUE, TRUE, 0);
335     gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
336                         TRUE, TRUE, 0);
337     gtk_box_pack_start (GTK_BOX (iface->widget), section->double_power_key,
338                         TRUE, TRUE, 0);
339 }
340
341 static void
342 maemo_tweaks_mce_section_dispose (GObject *obj)
343 {
344     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
345     if (section->ini)
346     {
347         g_key_file_free (section->ini);
348         section->ini = NULL;
349     }
350
351     G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
352         (obj);
353 }
354
355 static gboolean _save (MaemoTweaksSection *section,
356                        gboolean *requires_restart)
357 {
358     gchar *argv[5];
359     gint short_active, long_active, double_active;
360     gint i;
361
362     if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
363         return TRUE;
364
365     short_active = hildon_picker_button_get_active
366         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
367                                (section)->short_power_key));
368
369     long_active = hildon_picker_button_get_active
370         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
371                                (section)->long_power_key));
372
373     double_active = hildon_picker_button_get_active
374         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
375                                (section)->double_power_key));
376
377     if (short_active  == SHORT_POWER_KEY_DISABLED &&
378         long_active   == LONG_POWER_KEY_DISABLED  &&
379         double_active == DOUBLE_POWER_KEY_DISABLED)
380     {
381         GtkWidget *note;
382         gint retcode;
383
384         note = hildon_note_new_confirmation
385             (NULL, "Setting all Power Key options to \"Disabled\" means "
386              "that the only way to turn the device off is the removal of "
387              "the battery. Do you want to continue?");
388         retcode = gtk_dialog_run (GTK_DIALOG (note));
389         gtk_widget_destroy (note);
390
391         if (retcode == GTK_RESPONSE_CANCEL)
392             return FALSE;
393     }
394
395     *requires_restart = TRUE;
396
397     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
398     argv[1] = g_strdup_printf ("%s", spk[short_active].value);
399     argv[2] = g_strdup_printf ("%s", lpk[long_active].value);
400     argv[3] = g_strdup_printf ("%s", dpk[double_active].value);
401     argv[4] = NULL;
402
403     g_spawn_sync ("/home/user", argv, NULL,
404                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
405                   NULL, NULL, NULL, NULL, NULL, NULL);
406
407     for (i = 0; i < (sizeof (argv) / sizeof (gchar *)) - 1; i++)
408         g_free (argv[i]);
409
410     return TRUE;
411 }
412