4dab65c19f21d59a15890c8e0a3cf14f18810aa6
[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 gboolean _save (MaemoTweaksSection *section,
76                        gboolean *requires_restart);
77
78 static GType maemo_tweaks_desktop_section_type = 0;
79 static MaemoTweaksSectionClass *
80     maemo_tweaks_desktop_section_parent_class = NULL;
81
82
83 G_MODULE_EXPORT void
84 maemo_tweaks_module_load (MaemoTweaksModule *module)
85 {
86     maemo_tweaks_desktop_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_desktop_section_get_type (GTypeModule *module)
96 {
97     if (!maemo_tweaks_desktop_section_type)
98     {
99         static const GTypeInfo section_info =
100         {
101             sizeof (MaemoTweaksDesktopSectionClass),
102             (GBaseInitFunc) NULL,
103             (GBaseFinalizeFunc) NULL,
104             (GClassInitFunc) maemo_tweaks_desktop_section_class_init,
105             NULL,           /* class_finalize */
106             NULL,           /* class_data     */
107             sizeof (MaemoTweaksDesktopSection),
108             0,              /* n_preallocs    */
109             (GInstanceInitFunc) maemo_tweaks_desktop_section_init
110         };
111
112         maemo_tweaks_desktop_section_type =
113             g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
114                                          "MaemoTweaksDesktopSection",
115                                          &section_info, 0);
116     }
117
118     return maemo_tweaks_desktop_section_type;
119 }
120
121 static void
122 maemo_tweaks_desktop_section_class_init
123     (MaemoTweaksDesktopSectionClass *klass)
124 {
125     GObjectClass *object_class = G_OBJECT_CLASS (klass);
126     MaemoTweaksSectionClass *section_class =
127         MAEMO_TWEAKS_SECTION_CLASS (klass);
128
129     maemo_tweaks_desktop_section_parent_class =
130         g_type_class_peek_parent (klass);
131
132     section_class->name   = "_Desktop";
133     section_class->save = _save;
134
135     object_class->dispose = maemo_tweaks_desktop_section_dispose;
136 }
137
138 GtkWidget * _build_snap_to_grid (void)
139 {
140     const gchar *options[] = {"None", "Small grid", "Large grid", NULL};
141     gint i = 0;
142     GtkWidget *button, *selector;
143
144     selector = hildon_touch_selector_new_text ();
145     while (options[i] && options[i] != '\0')
146     {
147         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
148                                            options [i++]);
149     }
150
151     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
152                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
153
154     hildon_button_set_title (HILDON_BUTTON (button),
155                              "Snap desktop icons to grid");
156     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
157     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
158                                        HILDON_TOUCH_SELECTOR (selector));
159
160     gtk_widget_show (button);
161     return button;
162 }
163
164 static void
165 maemo_tweaks_desktop_section_init (MaemoTweaksDesktopSection *section)
166 {
167     MaemoTweaksSection *iface;
168
169     gint snap_value = SNAP_NONE_VALUE;
170
171     section->snap_button = _build_snap_to_grid ();
172
173     section->ini = g_key_file_new ();
174
175     if (!g_key_file_load_from_file (section->ini, TRANSITIONS,
176                                     G_KEY_FILE_NONE, NULL))
177     {
178         g_warning ("%s: failed to load %s", G_STRFUNC, TRANSITIONS);
179         return;
180     }
181
182     snap_value = g_key_file_get_integer (section->ini, "edit_mode",
183                                          "snap_grid_size", NULL);
184
185     if (snap_value < SNAP_SMALL_VALUE)
186     {
187         hildon_picker_button_set_active
188             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_NONE);
189     }
190     else if (snap_value < SNAP_LARGE_VALUE)
191     {
192         hildon_picker_button_set_active
193             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_SMALL);
194     }
195     else
196     {
197         hildon_picker_button_set_active
198             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_LARGE);
199     }
200
201
202     iface = MAEMO_TWEAKS_SECTION (section);
203     iface->name = "Desktop";
204     iface->widget = gtk_vbox_new (FALSE, 0);
205     gtk_box_pack_start (GTK_BOX (iface->widget), section->snap_button,
206                         TRUE, TRUE, 0);
207 }
208
209 static void
210 maemo_tweaks_desktop_section_dispose (GObject *obj)
211 {
212     MaemoTweaksDesktopSection *section = MAEMO_TWEAKS_DESKTOP_SECTION (obj);
213     if (section->ini)
214     {
215         g_key_file_free (section->ini);
216         section->ini = NULL;
217     }
218
219     G_OBJECT_CLASS (maemo_tweaks_desktop_section_parent_class)->dispose
220         (obj);
221 }
222
223
224 static gboolean  _save (MaemoTweaksSection *section,
225                         gboolean *requires_restart)
226 {
227     gchar *argv[3];
228     gint active;
229
230     active = hildon_picker_button_get_active
231         (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_DESKTOP_SECTION
232                                (section)->snap_button));
233
234     argv[0] = g_strdup ("/usr/bin/maemo-tweaks-desktop-save");
235     argv[1] = g_strdup_printf ("%d", snap_values[active]);
236     argv[2] = NULL;
237
238     g_spawn_sync ("/home/user", argv, NULL,
239                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
240                   NULL, NULL, NULL, NULL, NULL, NULL);
241
242     g_free (argv[0]);
243     g_free (argv[1]);
244
245     return TRUE;
246 }
247