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,
36 typedef struct _picker_t
43 static picker_t spk [] = {
44 {SHORT_POWER_KEY_DISABLED, "disabled", "Disabled"},
45 {SHORT_POWER_KEY_MENU, "menu", "Show menu"},
46 {SHORT_POWER_KEY_OFF, "poweroff", "Power off"}
49 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
50 typedef struct _MaemoTweaksMceSectionClass
51 MaemoTweaksMceSectionClass;
53 struct _MaemoTweaksMceSection
55 MaemoTweaksSection parent_instance;
58 GtkWidget *short_power_key;
60 guint value_changed : 1;
63 struct _MaemoTweaksMceSectionClass
65 MaemoTweaksSectionClass parent_class;
69 static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
70 static void maemo_tweaks_mce_section_class_init
71 (MaemoTweaksMceSectionClass *class);
72 static void maemo_tweaks_mce_section_init
73 (MaemoTweaksMceSection *section);
74 static void maemo_tweaks_mce_section_dispose (GObject *obj);
76 static void _save (MaemoTweaksSection *section, gboolean *requires_restart);
78 static GType maemo_tweaks_mce_section_type = 0;
79 static MaemoTweaksSectionClass *
80 maemo_tweaks_mce_section_parent_class = NULL;
84 maemo_tweaks_module_load (MaemoTweaksModule *module)
86 maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
90 maemo_tweaks_module_unload (MaemoTweaksModule *module)
95 maemo_tweaks_mce_section_get_type (GTypeModule *module)
97 if (!maemo_tweaks_mce_section_type)
99 static const GTypeInfo section_info =
101 sizeof (MaemoTweaksMceSectionClass),
102 (GBaseInitFunc) NULL,
103 (GBaseFinalizeFunc) NULL,
104 (GClassInitFunc) maemo_tweaks_mce_section_class_init,
105 NULL, /* class_finalize */
106 NULL, /* class_data */
107 sizeof (MaemoTweaksMceSection),
109 (GInstanceInitFunc) maemo_tweaks_mce_section_init
112 maemo_tweaks_mce_section_type =
113 g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
114 "MaemoTweaksMceSection",
118 return maemo_tweaks_mce_section_type;
122 maemo_tweaks_mce_section_class_init
123 (MaemoTweaksMceSectionClass *klass)
125 GObjectClass *object_class = G_OBJECT_CLASS (klass);
126 MaemoTweaksSectionClass *section_class =
127 MAEMO_TWEAKS_SECTION_CLASS (klass);
129 maemo_tweaks_mce_section_parent_class =
130 g_type_class_peek_parent (klass);
132 section_class->name = "_Mce";
133 section_class->save = _save;
135 object_class->dispose = maemo_tweaks_mce_section_dispose;
138 static void _value_changed (HildonPickerButton *b, gpointer user_data)
140 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
142 section->value_changed = TRUE;
145 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
148 GtkWidget *button, *selector;
150 selector = hildon_touch_selector_new_text ();
151 for (i = 0; i < SHORT_POWER_KEY_N; i++)
153 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
157 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
158 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
160 hildon_button_set_title (HILDON_BUTTON (button),
161 "Power button short press");
162 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
163 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
164 HILDON_TOUCH_SELECTOR (selector));
166 g_signal_connect (G_OBJECT (button), "value-changed",
167 G_CALLBACK (_value_changed), section);
169 gtk_widget_show (button);
174 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
176 MaemoTweaksSection *iface;
177 gchar *short_power_key_value;
180 section->short_power_key = _build_short_power_key (section);
182 section->ini = g_key_file_new ();
184 if (!g_key_file_load_from_file (section->ini, MCE,
185 G_KEY_FILE_NONE, NULL))
187 g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
191 short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
192 "PowerKeyShortAction",
195 for (i = 0; i < SHORT_POWER_KEY_N; i++)
197 if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
199 hildon_picker_button_set_active
200 (HILDON_PICKER_BUTTON (section->short_power_key), i);
204 section->value_changed = FALSE;
206 iface = MAEMO_TWEAKS_SECTION (section);
207 iface->widget = gtk_vbox_new (FALSE, 0);
208 gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
213 maemo_tweaks_mce_section_dispose (GObject *obj)
215 MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
218 g_key_file_free (section->ini);
222 G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
227 static void _save (MaemoTweaksSection *section, gboolean *requires_restart)
232 if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
235 *requires_restart = TRUE;
237 active = hildon_picker_button_get_active
238 (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
239 (section)->short_power_key));
241 argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
242 argv[1] = g_strdup_printf ("%s", spk[active].value);
245 g_spawn_sync ("/home/user", argv, NULL,
246 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
247 NULL, NULL, NULL, NULL, NULL, NULL);