e7013b9d94ec83417b07d972c825ab2b854f1934
[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
11 #include "libmaemo-tweaks-section/maemo-tweaks-section.h"
12 #include "libmaemo-tweaks-section/maemo-tweaks-module.h"
13
14
15 #define MAEMO_TWEAKS_TYPE_MCE_SECTION \
16         (maemo_tweaks_mce_section_type)
17 #define MAEMO_TWEAKS_MCE_SECTION(obj) \
18         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
19         MAEMO_TWEAKS_TYPE_MCE_SECTION, \
20         MaemoTweaksMceSection))
21 #define MAEMO_TWEAKS_MCE_SECTION_CLASS(k) \
22         (G_TYPE_CHECK_CLASS_CAST((k), \
23         MAEMO_TWEAKS_TYPE_MCE_SECTION, \
24         MaemoTweaksMceSectionClass))
25 #define MAEMO_TWEAKS_IS_MCE_SECTION(obj) \
26         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
27         MAEMO_TWEAKS_TYPE_MCE_SECTION))
28
29 enum {
30     SHORT_POWER_KEY_DISABLED,
31     SHORT_POWER_KEY_MENU,
32     SHORT_POWER_KEY_OFF,
33     SHORT_POWER_KEY_N
34 };
35
36 enum {
37     LONG_POWER_KEY_DISABLED,
38     LONG_POWER_KEY_MENU,
39     LONG_POWER_KEY_OFF,
40     LONG_POWER_KEY_N
41 };
42
43
44 typedef struct _picker_t
45 {
46     guint index;
47     const gchar *value;
48     const gchar *label;
49 } picker_t;
50
51 static picker_t spk [] = {
52     {SHORT_POWER_KEY_DISABLED, "disabled", "Disabled"},
53     {SHORT_POWER_KEY_MENU, "menu", "Show menu"},
54     {SHORT_POWER_KEY_OFF, "poweroff", "Power off"}
55 };
56
57 static picker_t lpk [] = {
58     {SHORT_POWER_KEY_DISABLED, "disabled", "Disabled"},
59     {SHORT_POWER_KEY_MENU, "menu", "Show menu"},
60     {SHORT_POWER_KEY_OFF, "poweroff", "Power off"}
61 };    
62
63
64 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
65 typedef struct _MaemoTweaksMceSectionClass
66                MaemoTweaksMceSectionClass;
67
68 struct _MaemoTweaksMceSection
69 {
70     MaemoTweaksSection parent_instance;
71
72     GKeyFile *ini;
73     GtkWidget *short_power_key;
74     GtkWidget *long_power_key;
75
76     guint value_changed : 1;
77 };
78
79 struct _MaemoTweaksMceSectionClass
80 {
81     MaemoTweaksSectionClass parent_class;
82 };
83
84
85 static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
86 static void maemo_tweaks_mce_section_class_init
87     (MaemoTweaksMceSectionClass *class);
88 static void maemo_tweaks_mce_section_init
89     (MaemoTweaksMceSection *section);
90 static void maemo_tweaks_mce_section_dispose (GObject *obj);
91
92 static void _save (MaemoTweaksSection *section, gboolean *requires_restart);
93
94 static GType maemo_tweaks_mce_section_type = 0;
95 static MaemoTweaksSectionClass *
96     maemo_tweaks_mce_section_parent_class = NULL;
97
98
99 G_MODULE_EXPORT void
100 maemo_tweaks_module_load (MaemoTweaksModule *module)
101 {
102     maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
103 }
104
105 G_MODULE_EXPORT void
106 maemo_tweaks_module_unload (MaemoTweaksModule *module)
107 {
108 }
109
110 static GType
111 maemo_tweaks_mce_section_get_type (GTypeModule *module)
112 {
113     if (!maemo_tweaks_mce_section_type)
114     {
115         static const GTypeInfo section_info =
116         {
117             sizeof (MaemoTweaksMceSectionClass),
118             (GBaseInitFunc) NULL,
119             (GBaseFinalizeFunc) NULL,
120             (GClassInitFunc) maemo_tweaks_mce_section_class_init,
121             NULL,           /* class_finalize */
122             NULL,           /* class_data     */
123             sizeof (MaemoTweaksMceSection),
124             0,              /* n_preallocs    */
125             (GInstanceInitFunc) maemo_tweaks_mce_section_init
126         };
127
128         maemo_tweaks_mce_section_type =
129             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
130                                          "MaemoTweaksMceSection",
131                                          &section_info, 0);
132     }
133
134     return maemo_tweaks_mce_section_type;
135 }
136
137 static void
138 maemo_tweaks_mce_section_class_init
139     (MaemoTweaksMceSectionClass *klass)
140 {
141     GObjectClass *object_class = G_OBJECT_CLASS (klass);
142     MaemoTweaksSectionClass *section_class =
143         MAEMO_TWEAKS_SECTION_CLASS (klass);
144
145     maemo_tweaks_mce_section_parent_class =
146         g_type_class_peek_parent (klass);
147
148     section_class->name   = "_Mce";
149     section_class->save = _save;
150
151     object_class->dispose = maemo_tweaks_mce_section_dispose;
152 }
153
154 static void _value_changed (HildonPickerButton *b, gpointer user_data)
155 {
156     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
157
158     section->value_changed = TRUE;
159 }
160
161 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
162 {
163     gint i;
164     GtkWidget *button, *selector;
165
166     selector = hildon_touch_selector_new_text ();
167     for (i = 0; i < SHORT_POWER_KEY_N; i++)
168     {
169         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
170                                            spk[i].label);
171     }
172
173     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
174                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
175
176     hildon_button_set_title (HILDON_BUTTON (button),
177                              "Power key: short press");
178     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
179     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
180                                        HILDON_TOUCH_SELECTOR (selector));
181
182     g_signal_connect (G_OBJECT (button), "value-changed",
183                       G_CALLBACK (_value_changed), section);
184
185     gtk_widget_show (button);
186     return button;
187 }
188
189 GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
190 {
191     gint i;
192     GtkWidget *button, *selector;
193
194     selector = hildon_touch_selector_new_text ();
195     for (i = 0; i < LONG_POWER_KEY_N; i++)
196     {
197         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
198                                            spk[i].label);
199     }
200
201     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
202                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
203
204     hildon_button_set_title (HILDON_BUTTON (button),
205                              "Power key: long press");
206     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
207     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
208                                        HILDON_TOUCH_SELECTOR (selector));
209
210     g_signal_connect (G_OBJECT (button), "value-changed",
211                       G_CALLBACK (_value_changed), section);
212
213     gtk_widget_show (button);
214     return button;
215 }
216
217 static void
218 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
219 {
220     MaemoTweaksSection *iface;
221     gchar *short_power_key_value;
222     gchar *long_power_key_value;
223     gint i;
224
225     section->short_power_key = _build_short_power_key (section);
226     section->long_power_key = _build_long_power_key (section);
227
228     section->ini = g_key_file_new ();
229
230     if (!g_key_file_load_from_file (section->ini, MCE,
231                                     G_KEY_FILE_NONE, NULL))
232     {
233         g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
234         return;
235     }
236
237     short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
238                                                    "PowerKeyShortAction",
239                                                    NULL);
240     long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
241                                                   "PowerKeyLongAction",
242                                                   NULL);
243
244     for (i = 0; i < SHORT_POWER_KEY_N; i++)
245     {
246         if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
247         {
248             hildon_picker_button_set_active
249                 (HILDON_PICKER_BUTTON (section->short_power_key), i);
250         }
251     }
252
253     for (i = 0; i < LONG_POWER_KEY_N; i++)
254     {
255         if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
256         {
257             hildon_picker_button_set_active
258                 (HILDON_PICKER_BUTTON (section->long_power_key), i);
259         }
260     }
261
262     section->value_changed = FALSE;
263
264     iface = MAEMO_TWEAKS_SECTION (section);
265     iface->widget = gtk_vbox_new (FALSE, 0);
266     gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
267                         TRUE, TRUE, 0);
268     gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
269                         TRUE, TRUE, 0);
270
271 }
272
273 static void
274 maemo_tweaks_mce_section_dispose (GObject *obj)
275 {
276     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
277     if (section->ini)
278     {
279         g_key_file_free (section->ini);
280         section->ini = NULL;
281     }
282
283     G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
284         (obj);
285 }
286
287 static void _save (MaemoTweaksSection *section, gboolean *requires_restart)
288 {
289     gchar *argv[4];
290     gint short_active, long_active;
291
292     if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
293         return;
294
295     *requires_restart = TRUE;
296
297     short_active = hildon_picker_button_get_active
298         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
299                                (section)->short_power_key));
300
301     long_active = hildon_picker_button_get_active
302         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
303                                (section)->long_power_key));
304
305     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
306     argv[1] = g_strdup_printf ("%s", spk[short_active].value);
307     argv[2] = g_strdup_printf ("%s", lpk[long_active].value);
308     argv[3] = NULL;
309
310     g_spawn_sync ("/home/user", argv, NULL,
311                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
312                   NULL, NULL, NULL, NULL, NULL, NULL);
313
314     g_free (argv[0]);
315     g_free (argv[1]);
316     g_free (argv[2]);
317 }
318