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