4aa15748bf8789449df68f6cf9341a2dd058cf53
[tweakr] / modules / maemo-tweaks-desktop.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_DESKTOP_SECTION \
16         (maemo_tweaks_desktop_section_type)
17 #define MAEMO_TWEAKS_DESKTOP_SECTION(obj) \
18         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
19         MAEMO_TWEAKS_TYPE_DESKTOP_SECTION, \
20         MaemoTweaksDesktopSection))
21 #define MAEMO_TWEAKS_DESKTOP_SECTION_CLASS(k) \
22         (G_TYPE_CHECK_CLASS_CAST((k), \
23         MAEMO_TWEAKS_TYPE_DESKTOP_SECTION, \
24         MaemoTweaksDesktopSectionClass))
25 #define MAEMO_TWEAKS_IS_DESKTOP_SECTION(obj) \
26         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
27         MAEMO_TWEAKS_TYPE_DESKTOP_SECTION))
28
29
30 enum
31 {
32     SNAP_NONE,
33     SNAP_SMALL,
34     SNAP_LARGE
35 };
36
37 enum
38 {
39     SNAP_NONE_VALUE,
40     SNAP_SMALL_VALUE = 8,
41     SNAP_LARGE_VALUE = 16
42 };
43
44 static gint snap_values [] = {
45     SNAP_NONE_VALUE,
46     SNAP_SMALL_VALUE,
47     SNAP_LARGE_VALUE
48 };
49
50 typedef struct _MaemoTweaksDesktopSection MaemoTweaksDesktopSection;
51 typedef struct _MaemoTweaksDesktopSectionClass
52                MaemoTweaksDesktopSectionClass;
53
54 struct _MaemoTweaksDesktopSection
55 {
56     MaemoTweaksSection parent_instance;
57
58     GKeyFile *ini;
59     GtkWidget *snap_button;
60 };
61
62 struct _MaemoTweaksDesktopSectionClass
63 {
64     MaemoTweaksSectionClass parent_class;
65 };
66
67
68 static GType maemo_tweaks_desktop_section_get_type (GTypeModule *module);
69 static void maemo_tweaks_desktop_section_class_init
70     (MaemoTweaksDesktopSectionClass *class);
71 static void maemo_tweaks_desktop_section_init
72     (MaemoTweaksDesktopSection *section);
73 static void maemo_tweaks_desktop_section_dispose (GObject *obj);
74
75 static void _save (MaemoTweaksSection *section);
76
77 static GType maemo_tweaks_desktop_section_type = 0;
78 static MaemoTweaksSectionClass *
79     maemo_tweaks_desktop_section_parent_class = NULL;
80
81
82 G_MODULE_EXPORT void
83 maemo_tweaks_module_load (MaemoTweaksModule *module)
84 {
85     maemo_tweaks_desktop_section_get_type (G_TYPE_MODULE (module));
86 }
87
88 G_MODULE_EXPORT void
89 maemo_tweaks_module_unload (MaemoTweaksModule *module)
90 {
91 }
92
93 static GType
94 maemo_tweaks_desktop_section_get_type (GTypeModule *module)
95 {
96     if (!maemo_tweaks_desktop_section_type)
97     {
98         static const GTypeInfo section_info =
99         {
100             sizeof (MaemoTweaksDesktopSectionClass),
101             (GBaseInitFunc) NULL,
102             (GBaseFinalizeFunc) NULL,
103             (GClassInitFunc) maemo_tweaks_desktop_section_class_init,
104             NULL,           /* class_finalize */
105             NULL,           /* class_data     */
106             sizeof (MaemoTweaksDesktopSection),
107             0,              /* n_preallocs    */
108             (GInstanceInitFunc) maemo_tweaks_desktop_section_init
109         };
110
111         maemo_tweaks_desktop_section_type =
112             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
113                                          "MaemoTweaksDesktopSection",
114                                          &section_info, 0);
115     }
116
117     return maemo_tweaks_desktop_section_type;
118 }
119
120 static void
121 maemo_tweaks_desktop_section_class_init
122     (MaemoTweaksDesktopSectionClass *klass)
123 {
124     GObjectClass *object_class = G_OBJECT_CLASS (klass);
125     MaemoTweaksSectionClass *section_class =
126         MAEMO_TWEAKS_SECTION_CLASS (klass);
127
128     maemo_tweaks_desktop_section_parent_class =
129         g_type_class_peek_parent (klass);
130
131     section_class->name   = "_Desktop";
132     section_class->save = _save;
133
134     object_class->dispose = maemo_tweaks_desktop_section_dispose;
135 }
136
137 GtkWidget * _build_snap_to_grid (void)
138 {
139     const gchar *options[] = {"None", "Small grid", "Large grid", NULL};
140     gint i = 0;
141     GtkWidget *button, *selector;
142
143     selector = hildon_touch_selector_new_text ();
144     while (options[i] && options[i] != '\0')
145     {
146         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
147                                            options [i++]);
148     }
149
150     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
151                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
152
153     hildon_button_set_title (HILDON_BUTTON (button),
154                              "Snap desktop icons to grid");
155
156     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
157                                        HILDON_TOUCH_SELECTOR (selector));
158
159     gtk_widget_show (button);
160     return button;
161 }
162
163 static void
164 maemo_tweaks_desktop_section_init (MaemoTweaksDesktopSection *section)
165 {
166     MaemoTweaksSection *iface;
167
168     gint snap_value = SNAP_NONE_VALUE;
169
170     section->snap_button = _build_snap_to_grid ();
171
172     section->ini = g_key_file_new ();
173
174     if (!g_key_file_load_from_file (section->ini, TRANSITIONS,
175                                     G_KEY_FILE_NONE, NULL))
176     {
177         g_warning ("%s: failed to load %s", G_STRFUNC, TRANSITIONS);
178         return;
179     }
180
181     snap_value = g_key_file_get_integer (section->ini, "edit_mode",
182                                          "snap_grid_size", NULL);
183
184     if (snap_value < SNAP_SMALL_VALUE)
185     {
186         hildon_picker_button_set_active
187             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_NONE);
188     }
189     else if (snap_value < SNAP_LARGE_VALUE)
190     {
191         hildon_picker_button_set_active
192             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_SMALL);
193     }
194     else
195     {
196         hildon_picker_button_set_active
197             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_LARGE);
198     }
199
200
201     iface = MAEMO_TWEAKS_SECTION (section);
202     iface->widget = gtk_vbox_new (FALSE, 0);
203     gtk_box_pack_start (GTK_BOX (iface->widget), section->snap_button,
204                         TRUE, TRUE, 0);
205 }
206
207 static void
208 maemo_tweaks_desktop_section_dispose (GObject *obj)
209 {
210     MaemoTweaksDesktopSection *section = MAEMO_TWEAKS_DESKTOP_SECTION (obj);
211     if (section->ini)
212     {
213         g_key_file_free (section->ini);
214         section->ini = NULL;
215     }
216
217     G_OBJECT_CLASS (maemo_tweaks_desktop_section_parent_class)->dispose
218         (obj);
219 }
220
221
222 static void _save (MaemoTweaksSection *section)
223 {
224     gchar *argv[2];
225     gint active;
226
227     active = hildon_picker_button_get_active
228         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_DESKTOP_SECTION
229                                (section)->snap_button));
230
231     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-desktop-save");
232     argv[1] = g_strdup_printf ("%d", snap_values[active]);
233
234     g_spawn_sync ("/home/user", argv, NULL,
235                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
236                   NULL, NULL, NULL, NULL, NULL, NULL);
237
238     g_free (argv[0]);
239     g_free (argv[1]);
240 }
241