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);
198 hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
200 hildon_button_set_title (HILDON_BUTTON (button),
201 "Power key: short press");
202 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
203 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
204 HILDON_TOUCH_SELECTOR (selector));
206 g_signal_connect (G_OBJECT (button), "value-changed",
207 G_CALLBACK (_value_changed), section);
209 gtk_widget_show (button);
213 GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
216 GtkWidget *button, *selector;
218 selector = hildon_touch_selector_new_text ();
219 for (i = 0; i < LONG_POWER_KEY_N; i++)
221 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
225 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
226 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
227 hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
229 hildon_button_set_title (HILDON_BUTTON (button),
230 "Power key: long press");
231 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
232 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
233 HILDON_TOUCH_SELECTOR (selector));
235 g_signal_connect (G_OBJECT (button), "value-changed",
236 G_CALLBACK (_value_changed), section);
238 gtk_widget_show (button);
242 GtkWidget * _build_double_power_key (MaemoTweaksMceSection *section)
245 GtkWidget *button, *selector;
247 selector = hildon_touch_selector_new_text ();
248 for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
250 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
254 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
255 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
256 hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
258 hildon_button_set_title (HILDON_BUTTON (button),
259 "Power key: double press");
260 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
261 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
262 HILDON_TOUCH_SELECTOR (selector));
264 g_signal_connect (G_OBJECT (button), "value-changed",
265 G_CALLBACK (_value_changed), section);
267 gtk_widget_show (button);
273 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
275 MaemoTweaksSection *iface;
276 gchar *short_power_key_value;
277 gchar *long_power_key_value;
278 gchar *double_power_key_value;
281 section->short_power_key = _build_short_power_key (section);
282 section->long_power_key = _build_long_power_key (section);
283 section->double_power_key = _build_double_power_key (section);
285 section->ini = g_key_file_new ();
287 if (!g_key_file_load_from_file (section->ini, MCE,
288 G_KEY_FILE_NONE, NULL))
290 g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
294 short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
295 "PowerKeyShortAction",
297 long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
298 "PowerKeyLongAction",
300 double_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
301 "PowerKeyDoubleAction",
304 for (i = 0; i < SHORT_POWER_KEY_N; i++)
306 if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
308 hildon_picker_button_set_active
309 (HILDON_PICKER_BUTTON (section->short_power_key), i);
313 for (i = 0; i < LONG_POWER_KEY_N; i++)
315 if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
317 hildon_picker_button_set_active
318 (HILDON_PICKER_BUTTON (section->long_power_key), i);
322 for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
324 if (g_strcmp0 (double_power_key_value, dpk[i].value) == 0)
326 hildon_picker_button_set_active
327 (HILDON_PICKER_BUTTON (section->double_power_key), i);
331 section->value_changed = FALSE;
333 iface = MAEMO_TWEAKS_SECTION (section);
335 iface->widget = gtk_vbox_new (FALSE, 0);
336 gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
338 gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
340 gtk_box_pack_start (GTK_BOX (iface->widget), section->double_power_key,
345 maemo_tweaks_mce_section_dispose (GObject *obj)
347 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
350 g_key_file_free (section->ini);
354 G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
358 static gboolean _save (MaemoTweaksSection *section,
359 gboolean *requires_restart)
362 gint short_active, long_active, double_active;
365 if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
368 short_active = hildon_picker_button_get_active
369 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
370 (section)->short_power_key));
372 long_active = hildon_picker_button_get_active
373 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
374 (section)->long_power_key));
376 double_active = hildon_picker_button_get_active
377 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
378 (section)->double_power_key));
380 if (short_active == SHORT_POWER_KEY_DISABLED &&
381 long_active == LONG_POWER_KEY_DISABLED &&
382 double_active == DOUBLE_POWER_KEY_DISABLED)
387 note = hildon_note_new_confirmation
388 (NULL, "Setting all Power Key options to \"Disabled\" means "
389 "that the only way to turn the device off is the removal of "
390 "the battery. Do you want to continue?");
391 retcode = gtk_dialog_run (GTK_DIALOG (note));
392 gtk_widget_destroy (note);
394 if (retcode == GTK_RESPONSE_CANCEL)
398 *requires_restart = TRUE;
400 argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
401 argv[1] = g_strdup_printf ("%s", spk[short_active].value);
402 argv[2] = g_strdup_printf ("%s", lpk[long_active].value);
403 argv[3] = g_strdup_printf ("%s", dpk[double_active].value);
406 g_spawn_sync ("/home/user", argv, NULL,
407 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
408 NULL, NULL, NULL, NULL, NULL, NULL);
410 for (i = 0; i < (sizeof (argv) / sizeof (gchar *)) - 1; i++)