2 * vim:ts=4:sw=4:et:cindent:cino=(0
8 #include <hildon/hildon-picker-button.h>
9 #include <hildon/hildon-touch-selector.h>
10 #include <hildon/hildon-note.h>
12 #include "libmaemo-tweaks-section/maemo-tweaks-section.h"
13 #include "libmaemo-tweaks-section/maemo-tweaks-module.h"
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))
30 #define DISABLE_LABEL "Disable"
31 #define SHOW_MENU_LABEL "Show menu"
32 #define POWER_OFF_LABEL "Power off"
33 #define LOCK_LABEL "Lock"
36 SHORT_POWER_KEY_DISABLED,
43 LONG_POWER_KEY_DISABLED,
50 DOUBLE_POWER_KEY_DISABLED,
51 DOUBLE_POWER_KEY_MENU,
53 DOUBLE_POWER_KEY_LOCK,
58 typedef struct _picker_t
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}
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}
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}
85 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
86 typedef struct _MaemoTweaksMceSectionClass
87 MaemoTweaksMceSectionClass;
89 struct _MaemoTweaksMceSection
91 MaemoTweaksSection parent_instance;
94 GtkWidget *short_power_key;
95 GtkWidget *long_power_key;
96 GtkWidget *double_power_key;
98 guint value_changed : 1;
101 struct _MaemoTweaksMceSectionClass
103 MaemoTweaksSectionClass parent_class;
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);
114 static gboolean _save (MaemoTweaksSection *section,
115 gboolean *requires_restart);
117 static GType maemo_tweaks_mce_section_type = 0;
118 static MaemoTweaksSectionClass *
119 maemo_tweaks_mce_section_parent_class = NULL;
123 maemo_tweaks_module_load (MaemoTweaksModule *module)
125 maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
129 maemo_tweaks_module_unload (MaemoTweaksModule *module)
134 maemo_tweaks_mce_section_get_type (GTypeModule *module)
136 if (!maemo_tweaks_mce_section_type)
138 static const GTypeInfo section_info =
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),
148 (GInstanceInitFunc) maemo_tweaks_mce_section_init
151 maemo_tweaks_mce_section_type =
152 g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
153 "MaemoTweaksMceSection",
157 return maemo_tweaks_mce_section_type;
161 maemo_tweaks_mce_section_class_init
162 (MaemoTweaksMceSectionClass *klass)
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 MaemoTweaksSectionClass *section_class =
166 MAEMO_TWEAKS_SECTION_CLASS (klass);
168 maemo_tweaks_mce_section_parent_class =
169 g_type_class_peek_parent (klass);
171 section_class->name = "_Mce";
172 section_class->save = _save;
174 object_class->dispose = maemo_tweaks_mce_section_dispose;
177 static void _value_changed (HildonPickerButton *b, gpointer user_data)
179 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
181 section->value_changed = TRUE;
184 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
187 GtkWidget *button, *selector;
189 selector = hildon_touch_selector_new_text ();
190 for (i = 0; i < SHORT_POWER_KEY_N; i++)
192 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
196 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
197 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
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));
205 g_signal_connect (G_OBJECT (button), "value-changed",
206 G_CALLBACK (_value_changed), section);
208 gtk_widget_show (button);
212 GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
215 GtkWidget *button, *selector;
217 selector = hildon_touch_selector_new_text ();
218 for (i = 0; i < LONG_POWER_KEY_N; i++)
220 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
224 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
225 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
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));
233 g_signal_connect (G_OBJECT (button), "value-changed",
234 G_CALLBACK (_value_changed), section);
236 gtk_widget_show (button);
240 GtkWidget * _build_double_power_key (MaemoTweaksMceSection *section)
243 GtkWidget *button, *selector;
245 selector = hildon_touch_selector_new_text ();
246 for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
248 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
252 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
253 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
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));
261 g_signal_connect (G_OBJECT (button), "value-changed",
262 G_CALLBACK (_value_changed), section);
264 gtk_widget_show (button);
270 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
272 MaemoTweaksSection *iface;
273 gchar *short_power_key_value;
274 gchar *long_power_key_value;
275 gchar *double_power_key_value;
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);
282 section->ini = g_key_file_new ();
284 if (!g_key_file_load_from_file (section->ini, MCE,
285 G_KEY_FILE_NONE, NULL))
287 g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
291 short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
292 "PowerKeyShortAction",
294 long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
295 "PowerKeyLongAction",
297 double_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
298 "PowerKeyDoubleAction",
301 for (i = 0; i < SHORT_POWER_KEY_N; i++)
303 if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
305 hildon_picker_button_set_active
306 (HILDON_PICKER_BUTTON (section->short_power_key), i);
310 for (i = 0; i < LONG_POWER_KEY_N; i++)
312 if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
314 hildon_picker_button_set_active
315 (HILDON_PICKER_BUTTON (section->long_power_key), i);
319 for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
321 if (g_strcmp0 (double_power_key_value, dpk[i].value) == 0)
323 hildon_picker_button_set_active
324 (HILDON_PICKER_BUTTON (section->double_power_key), i);
328 section->value_changed = FALSE;
330 iface = MAEMO_TWEAKS_SECTION (section);
332 iface->widget = gtk_vbox_new (FALSE, 0);
333 gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
335 gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
337 gtk_box_pack_start (GTK_BOX (iface->widget), section->double_power_key,
342 maemo_tweaks_mce_section_dispose (GObject *obj)
344 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
347 g_key_file_free (section->ini);
351 G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
355 static gboolean _save (MaemoTweaksSection *section,
356 gboolean *requires_restart)
359 gint short_active, long_active, double_active;
362 if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
365 short_active = hildon_picker_button_get_active
366 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
367 (section)->short_power_key));
369 long_active = hildon_picker_button_get_active
370 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
371 (section)->long_power_key));
373 double_active = hildon_picker_button_get_active
374 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
375 (section)->double_power_key));
377 if (short_active == SHORT_POWER_KEY_DISABLED &&
378 long_active == LONG_POWER_KEY_DISABLED &&
379 double_active == DOUBLE_POWER_KEY_DISABLED)
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);
391 if (retcode == GTK_RESPONSE_CANCEL)
395 *requires_restart = TRUE;
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);
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);
407 for (i = 0; i < (sizeof (argv) / sizeof (gchar *)) - 1; i++)