Add MCE plugin.
[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
61 struct _MaemoTweaksMceSectionClass
62 {
63     MaemoTweaksSectionClass parent_class;
64 };
65
66
67 static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
68 static void maemo_tweaks_mce_section_class_init
69     (MaemoTweaksMceSectionClass *class);
70 static void maemo_tweaks_mce_section_init
71     (MaemoTweaksMceSection *section);
72 static void maemo_tweaks_mce_section_dispose (GObject *obj);
73
74 static void _save (MaemoTweaksSection *section);
75
76 static GType maemo_tweaks_mce_section_type = 0;
77 static MaemoTweaksSectionClass *
78     maemo_tweaks_mce_section_parent_class = NULL;
79
80
81 G_MODULE_EXPORT void
82 maemo_tweaks_module_load (MaemoTweaksModule *module)
83 {
84     maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
85 }
86
87 G_MODULE_EXPORT void
88 maemo_tweaks_module_unload (MaemoTweaksModule *module)
89 {
90 }
91
92 static GType
93 maemo_tweaks_mce_section_get_type (GTypeModule *module)
94 {
95     if (!maemo_tweaks_mce_section_type)
96     {
97         static const GTypeInfo section_info =
98         {
99             sizeof (MaemoTweaksMceSectionClass),
100             (GBaseInitFunc) NULL,
101             (GBaseFinalizeFunc) NULL,
102             (GClassInitFunc) maemo_tweaks_mce_section_class_init,
103             NULL,           /* class_finalize */
104             NULL,           /* class_data     */
105             sizeof (MaemoTweaksMceSection),
106             0,              /* n_preallocs    */
107             (GInstanceInitFunc) maemo_tweaks_mce_section_init
108         };
109
110         maemo_tweaks_mce_section_type =
111             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
112                                          "MaemoTweaksMceSection",
113                                          &section_info, 0);
114     }
115
116     return maemo_tweaks_mce_section_type;
117 }
118
119 static void
120 maemo_tweaks_mce_section_class_init
121     (MaemoTweaksMceSectionClass *klass)
122 {
123     GObjectClass *object_class = G_OBJECT_CLASS (klass);
124     MaemoTweaksSectionClass *section_class =
125         MAEMO_TWEAKS_SECTION_CLASS (klass);
126
127     maemo_tweaks_mce_section_parent_class =
128         g_type_class_peek_parent (klass);
129
130     section_class->name   = "_Mce";
131     section_class->save = _save;
132
133     object_class->dispose = maemo_tweaks_mce_section_dispose;
134 }
135
136 GtkWidget * _build_short_power_key (void)
137 {
138     gint i;
139     GtkWidget *button, *selector;
140
141     selector = hildon_touch_selector_new_text ();
142     for (i = 0; i < SHORT_POWER_KEY_N; i++)
143     {
144         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
145                                            spk[i].label);
146     }
147
148     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
149                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
150
151     hildon_button_set_title (HILDON_BUTTON (button),
152                              "Power button short press");
153     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
154     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
155                                        HILDON_TOUCH_SELECTOR (selector));
156
157     gtk_widget_show (button);
158     return button;
159 }
160
161 static void
162 maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
163 {
164     MaemoTweaksSection *iface;
165     gchar *short_power_key_value;
166     gint i;
167
168     section->short_power_key = _build_short_power_key ();
169
170     section->ini = g_key_file_new ();
171
172     if (!g_key_file_load_from_file (section->ini, MCE,
173                                     G_KEY_FILE_NONE, NULL))
174     {
175         g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
176         return;
177     }
178
179     short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
180                                                    "PowerKeyShortAction",
181                                                    NULL);
182
183     for (i = 0; i < SHORT_POWER_KEY_N; i++)
184     {
185         if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
186         {
187             hildon_picker_button_set_active
188                 (HILDON_PICKER_BUTTON (section->short_power_key), i);
189         }
190     }
191
192
193     iface = MAEMO_TWEAKS_SECTION (section);
194     iface->widget = gtk_vbox_new (FALSE, 0);
195     gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
196                         TRUE, TRUE, 0);
197 }
198
199 static void
200 maemo_tweaks_mce_section_dispose (GObject *obj)
201 {
202     MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
203     if (section->ini)
204     {
205         g_key_file_free (section->ini);
206         section->ini = NULL;
207     }
208
209     G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
210         (obj);
211 }
212
213
214 static void _save (MaemoTweaksSection *section)
215 {
216     gchar *argv[3];
217     gint active;
218
219     active = hildon_picker_button_get_active
220         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
221                                (section)->short_power_key));
222
223     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
224     argv[1] = g_strdup_printf ("%s", spk[active].value);
225     argv[2] = NULL;
226
227     g_spawn_sync ("/home/user", argv, NULL,
228                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
229                   NULL, NULL, NULL, NULL, NULL, NULL);
230
231     g_free (argv[0]);
232     g_free (argv[1]);
233 }
234