Add l10n support.
[tweakr] / modules / tweakr-desktop.c
1 /*
2  * vim:ts=4:sw=4:et:cindent:cino=(0
3  */ 
4
5 #include <config.h>
6 #include <glib/gi18n-lib.h>
7
8 #include <gtk/gtk.h>
9 #include <hildon/hildon-picker-button.h>
10 #include <hildon/hildon-touch-selector.h>
11
12 #include "libtweakr-section/tweakr-section.h"
13 #include "libtweakr-section/tweakr-module.h"
14
15
16 #define TWEAKR_TYPE_DESKTOP_SECTION \
17         (tweakr_desktop_section_type)
18 #define TWEAKR_DESKTOP_SECTION(obj) \
19         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
20         TWEAKR_TYPE_DESKTOP_SECTION, \
21         TweakrDesktopSection))
22 #define TWEAKR_DESKTOP_SECTION_CLASS(k) \
23         (G_TYPE_CHECK_CLASS_CAST((k), \
24         TWEAKR_TYPE_DESKTOP_SECTION, \
25         TweakrDesktopSectionClass))
26 #define TWEAKR_IS_DESKTOP_SECTION(obj) \
27         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
28         TWEAKR_TYPE_DESKTOP_SECTION))
29
30
31 enum
32 {
33     SNAP_NONE,
34     SNAP_SMALL,
35     SNAP_LARGE,
36     SNAP_HUGE
37 };
38
39 enum
40 {
41     SNAP_NONE_VALUE,
42     SNAP_SMALL_VALUE = 8,
43     SNAP_LARGE_VALUE = 16,
44     SNAP_HUGE_VALUE = 32
45 };
46
47 static gint snap_values [] = {
48     SNAP_NONE_VALUE,
49     SNAP_SMALL_VALUE,
50     SNAP_LARGE_VALUE,
51     SNAP_HUGE_VALUE
52 };
53
54 typedef struct _TweakrDesktopSection TweakrDesktopSection;
55 typedef struct _TweakrDesktopSectionClass
56                TweakrDesktopSectionClass;
57
58 struct _TweakrDesktopSection
59 {
60     TweakrSection parent_instance;
61
62     GKeyFile *ini;
63     GtkWidget *snap_button;
64 };
65
66 struct _TweakrDesktopSectionClass
67 {
68     TweakrSectionClass parent_class;
69 };
70
71
72 static GType tweakr_desktop_section_get_type (GTypeModule *module);
73 static void tweakr_desktop_section_class_init
74     (TweakrDesktopSectionClass *class);
75 static void tweakr_desktop_section_init
76     (TweakrDesktopSection *section);
77 static void tweakr_desktop_section_dispose (GObject *obj);
78
79 static gboolean _save (TweakrSection *section,
80                        gboolean *requires_restart);
81
82 static GType tweakr_desktop_section_type = 0;
83 static TweakrSectionClass *
84     tweakr_desktop_section_parent_class = NULL;
85
86
87 G_MODULE_EXPORT void
88 tweakr_module_load (TweakrModule *module)
89 {
90     tweakr_desktop_section_get_type (G_TYPE_MODULE (module));
91 }
92
93 G_MODULE_EXPORT void
94 tweakr_module_unload (TweakrModule *module)
95 {
96 }
97
98 static GType
99 tweakr_desktop_section_get_type (GTypeModule *module)
100 {
101     if (!tweakr_desktop_section_type)
102     {
103         static const GTypeInfo section_info =
104         {
105             sizeof (TweakrDesktopSectionClass),
106             (GBaseInitFunc) NULL,
107             (GBaseFinalizeFunc) NULL,
108             (GClassInitFunc) tweakr_desktop_section_class_init,
109             NULL,           /* class_finalize */
110             NULL,           /* class_data     */
111             sizeof (TweakrDesktopSection),
112             0,              /* n_preallocs    */
113             (GInstanceInitFunc) tweakr_desktop_section_init
114         };
115
116         tweakr_desktop_section_type =
117             g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
118                                          "TweakrDesktopSection",
119                                          &section_info, 0);
120     }
121
122     return tweakr_desktop_section_type;
123 }
124
125 static void
126 tweakr_desktop_section_class_init
127     (TweakrDesktopSectionClass *klass)
128 {
129     GObjectClass *object_class = G_OBJECT_CLASS (klass);
130     TweakrSectionClass *section_class =
131         TWEAKR_SECTION_CLASS (klass);
132
133     tweakr_desktop_section_parent_class =
134         g_type_class_peek_parent (klass);
135
136     section_class->name   = "_Desktop";
137     section_class->save = _save;
138
139     object_class->dispose = tweakr_desktop_section_dispose;
140 }
141
142 GtkWidget * _build_snap_to_grid (void)
143 {
144     const gchar *options[] =
145     {
146         _("No"),
147         _("Small"),
148         _("Large"),
149         _("Huge"),
150         NULL
151     };
152     gint i = 0;
153     GtkWidget *button, *selector;
154
155     selector = hildon_touch_selector_new_text ();
156     while (options[i] && options[i] != '\0')
157     {
158         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
159                                            options [i++]);
160     }
161
162     button = hildon_picker_button_new (HILDON_SIZE_AUTO,
163                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
164     hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
165
166     hildon_button_set_title (HILDON_BUTTON (button),
167                              _("Snap icons to grid"));
168     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
169     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
170                                        HILDON_TOUCH_SELECTOR (selector));
171
172     gtk_widget_show (button);
173     return button;
174 }
175
176 static void
177 tweakr_desktop_section_init (TweakrDesktopSection *section)
178 {
179     TweakrSection *iface;
180
181     gint snap_value = SNAP_NONE_VALUE;
182
183     section->snap_button = _build_snap_to_grid ();
184
185     section->ini = g_key_file_new ();
186
187     if (!g_key_file_load_from_file (section->ini, TRANSITIONS,
188                                     G_KEY_FILE_NONE, NULL))
189     {
190         g_warning ("%s: failed to load %s", G_STRFUNC, TRANSITIONS);
191         return;
192     }
193
194     snap_value = g_key_file_get_integer (section->ini, "edit_mode",
195                                          "snap_grid_size", NULL);
196
197     if (snap_value < SNAP_SMALL_VALUE)
198     {
199         hildon_picker_button_set_active
200             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_NONE);
201     }
202     else if (snap_value < SNAP_LARGE_VALUE)
203     {
204         hildon_picker_button_set_active
205             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_SMALL);
206     }
207     else if (snap_value < SNAP_HUGE_VALUE)
208     {
209         hildon_picker_button_set_active
210             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_LARGE);
211     }
212     else
213     {
214         hildon_picker_button_set_active
215             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_HUGE);
216     }
217
218
219
220     iface = TWEAKR_SECTION (section);
221     iface->name = _("Desktop");
222     iface->widget = gtk_vbox_new (FALSE, 0);
223     gtk_box_pack_start (GTK_BOX (iface->widget), section->snap_button,
224                         FALSE, FALSE, 0);
225 }
226
227 static void
228 tweakr_desktop_section_dispose (GObject *obj)
229 {
230     TweakrDesktopSection *section = TWEAKR_DESKTOP_SECTION (obj);
231     if (section->ini)
232     {
233         g_key_file_free (section->ini);
234         section->ini = NULL;
235     }
236
237     G_OBJECT_CLASS (tweakr_desktop_section_parent_class)->dispose
238         (obj);
239 }
240
241
242 static gboolean  _save (TweakrSection *section,
243                         gboolean *requires_restart)
244 {
245     gchar *argv[3];
246     gint active;
247
248     active = hildon_picker_button_get_active
249         (HILDON_PICKER_BUTTON (TWEAKR_DESKTOP_SECTION
250                                (section)->snap_button));
251
252     argv[0] = g_strdup ("/usr/bin/tweakr-desktop-save");
253     argv[1] = g_strdup_printf ("%d", snap_values[active]);
254     argv[2] = NULL;
255
256     g_spawn_sync ("/home/user", argv, NULL,
257                   G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
258                   NULL, NULL, NULL, NULL, NULL, NULL);
259
260     g_free (argv[0]);
261     g_free (argv[1]);
262
263     return TRUE;
264 }
265