f3e139aac4efaeb4951d69ff0e947d5f641533ad
[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 typedef struct _picker_t
37 {
38     guint index;
39     const gchar *value;
40     const gchar *label;
41 } picker_t;
42
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"}
47 };    
48
49 typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
50 typedef struct _MaemoTweaksMceSectionClass
51                MaemoTweaksMceSectionClass;
52
53 struct _MaemoTweaksMceSection
54 {
55     MaemoTweaksSection parent_instance;
56
57     GKeyFile *ini;
58     GtkWidget *short_power_key;
59
60     guint value_changed : 1;
61 };
62
63 struct _MaemoTweaksMceSectionClass
64 {
65     MaemoTweaksSectionClass parent_class;
66 };
67
68
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);
75
76 static void _save (MaemoTweaksSection *section, gboolean *requires_restart);
77
78 static GType maemo_tweaks_mce_section_type = 0;
79 static MaemoTweaksSectionClass *
80     maemo_tweaks_mce_section_parent_class = NULL;
81
82
83 G_MODULE_EXPORT void
84 maemo_tweaks_module_load (MaemoTweaksModule *module)
85 {
86     maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
87 }
88
89 G_MODULE_EXPORT void
90 maemo_tweaks_module_unload (MaemoTweaksModule *module)
91 {
92 }
93
94 static GType
95 maemo_tweaks_mce_section_get_type (GTypeModule *module)
96 {
97     if (!maemo_tweaks_mce_section_type)
98     {
99         static const GTypeInfo section_info =
100         {
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),
108             0,              /* n_preallocs    */
109             (GInstanceInitFunc) maemo_tweaks_mce_section_init
110         };
111
112         maemo_tweaks_mce_section_type =
113             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
114                                          "MaemoTweaksMceSection",
115                                          &section_info, 0);
116     }
117
118     return maemo_tweaks_mce_section_type;
119 }
120
121 static void
122 maemo_tweaks_mce_section_class_init
123     (MaemoTweaksMceSectionClass *klass)
124 {
125     GObjectClass *object_class = G_OBJECT_CLASS (klass);
126     MaemoTweaksSectionClass *section_class =
127         MAEMO_TWEAKS_SECTION_CLASS (klass);
128
129     maemo_tweaks_mce_section_parent_class =
130         g_type_class_peek_parent (klass);
131
132     section_class->name   = "_Mce";
133     section_class->save = _save;
134
135     object_class->dispose = maemo_tweaks_mce_section_dispose;
136 }
137
138 static void _value_changed (HildonPickerButton *b, gpointer user_data)
139 {
140     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
141
142     section->value_changed = TRUE;
143 }
144
145 GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
146 {
147     gint i;
148     GtkWidget *button, *selector;
149
150     selector = hildon_touch_selector_new_text ();
151     for (i = 0; i < SHORT_POWER_KEY_N; i++)
152     {
153         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
154                                            spk[i].label);
155     }
156
157     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
158                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
159
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));
165
166     g_signal_connect (G_OBJECT (button), "value-changed",
167                       G_CALLBACK (_value_changed), section);
168
169     gtk_widget_show (button);
170     return button;
171 }
172
173 static void
174 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
175 {
176     MaemoTweaksSection *iface;
177     gchar *short_power_key_value;
178     gint i;
179
180     section->short_power_key = _build_short_power_key (section);
181
182     section->ini = g_key_file_new ();
183
184     if (!g_key_file_load_from_file (section->ini, MCE,
185                                     G_KEY_FILE_NONE, NULL))
186     {
187         g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
188         return;
189     }
190
191     short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
192                                                    "PowerKeyShortAction",
193                                                    NULL);
194
195     for (i = 0; i < SHORT_POWER_KEY_N; i++)
196     {
197         if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
198         {
199             hildon_picker_button_set_active
200                 (HILDON_PICKER_BUTTON (section->short_power_key), i);
201         }
202     }
203
204     section->value_changed = FALSE;
205
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,
209                         TRUE, TRUE, 0);
210 }
211
212 static void
213 maemo_tweaks_mce_section_dispose (GObject *obj)
214 {
215     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
216     if (section->ini)
217     {
218         g_key_file_free (section->ini);
219         section->ini = NULL;
220     }
221
222     G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
223         (obj);
224 }
225
226
227 static void _save (MaemoTweaksSection *section, gboolean *requires_restart)
228 {
229     gchar *argv[3];
230     gint active;
231
232     if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
233         return;
234
235     *requires_restart = TRUE;
236
237     active = hildon_picker_button_get_active
238         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
239                                (section)->short_power_key));
240
241     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
242     argv[1] = g_strdup_printf ("%s", spk[active].value);
243     argv[2] = NULL;
244
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);
248
249     g_free (argv[0]);
250     g_free (argv[1]);
251 }
252