2 * vim:ts=4:sw=4:et:cindent:cino=(0
8 #include <hildon/hildon-picker-button.h>
9 #include <hildon/hildon-touch-selector.h>
11 #include "libmaemo-tweaks-section/maemo-tweaks-section.h"
12 #include "libmaemo-tweaks-section/maemo-tweaks-module.h"
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))
30 SHORT_POWER_KEY_DISABLED,
37 LONG_POWER_KEY_DISABLED,
44 typedef struct _picker_t
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"}
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"}
64 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
65 typedef struct _MaemoTweaksMceSectionClass
66 MaemoTweaksMceSectionClass;
68 struct _MaemoTweaksMceSection
70 MaemoTweaksSection parent_instance;
73 GtkWidget *short_power_key;
74 GtkWidget *long_power_key;
76 guint value_changed : 1;
79 struct _MaemoTweaksMceSectionClass
81 MaemoTweaksSectionClass parent_class;
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);
92 static void _save (MaemoTweaksSection *section, gboolean *requires_restart);
94 static GType maemo_tweaks_mce_section_type = 0;
95 static MaemoTweaksSectionClass *
96 maemo_tweaks_mce_section_parent_class = NULL;
100 maemo_tweaks_module_load (MaemoTweaksModule *module)
102 maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
106 maemo_tweaks_module_unload (MaemoTweaksModule *module)
111 maemo_tweaks_mce_section_get_type (GTypeModule *module)
113 if (!maemo_tweaks_mce_section_type)
115 static const GTypeInfo section_info =
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),
125 (GInstanceInitFunc) maemo_tweaks_mce_section_init
128 maemo_tweaks_mce_section_type =
129 g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
130 "MaemoTweaksMceSection",
134 return maemo_tweaks_mce_section_type;
138 maemo_tweaks_mce_section_class_init
139 (MaemoTweaksMceSectionClass *klass)
141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
142 MaemoTweaksSectionClass *section_class =
143 MAEMO_TWEAKS_SECTION_CLASS (klass);
145 maemo_tweaks_mce_section_parent_class =
146 g_type_class_peek_parent (klass);
148 section_class->name = "_Mce";
149 section_class->save = _save;
151 object_class->dispose = maemo_tweaks_mce_section_dispose;
154 static void _value_changed (HildonPickerButton *b, gpointer user_data)
156 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
158 section->value_changed = TRUE;
161 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
164 GtkWidget *button, *selector;
166 selector = hildon_touch_selector_new_text ();
167 for (i = 0; i < SHORT_POWER_KEY_N; i++)
169 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
173 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
174 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
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));
182 g_signal_connect (G_OBJECT (button), "value-changed",
183 G_CALLBACK (_value_changed), section);
185 gtk_widget_show (button);
189 GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
192 GtkWidget *button, *selector;
194 selector = hildon_touch_selector_new_text ();
195 for (i = 0; i < LONG_POWER_KEY_N; i++)
197 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
201 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
202 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
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));
210 g_signal_connect (G_OBJECT (button), "value-changed",
211 G_CALLBACK (_value_changed), section);
213 gtk_widget_show (button);
218 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
220 MaemoTweaksSection *iface;
221 gchar *short_power_key_value;
222 gchar *long_power_key_value;
225 section->short_power_key = _build_short_power_key (section);
226 section->long_power_key = _build_long_power_key (section);
228 section->ini = g_key_file_new ();
230 if (!g_key_file_load_from_file (section->ini, MCE,
231 G_KEY_FILE_NONE, NULL))
233 g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
237 short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
238 "PowerKeyShortAction",
240 long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
241 "PowerKeyLongAction",
244 for (i = 0; i < SHORT_POWER_KEY_N; i++)
246 if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
248 hildon_picker_button_set_active
249 (HILDON_PICKER_BUTTON (section->short_power_key), i);
253 for (i = 0; i < LONG_POWER_KEY_N; i++)
255 if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
257 hildon_picker_button_set_active
258 (HILDON_PICKER_BUTTON (section->long_power_key), i);
262 section->value_changed = FALSE;
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,
268 gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
274 maemo_tweaks_mce_section_dispose (GObject *obj)
276 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
279 g_key_file_free (section->ini);
283 G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
287 static void _save (MaemoTweaksSection *section, gboolean *requires_restart)
290 gint short_active, long_active;
292 if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
295 *requires_restart = TRUE;
297 short_active = hildon_picker_button_get_active
298 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
299 (section)->short_power_key));
301 long_active = hildon_picker_button_get_active
302 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
303 (section)->long_power_key));
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);
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);