d3b0402163c16f047d96be3126e9806461084ffb
[tweakr] / modules / tweakr-profile.c
1 /*
2  * vim:ts=4:sw=4:et:cindent:cino=(0
3  */ 
4
5 #include <config.h>
6 #include <glib.h>
7 #include <glib/gi18n-lib.h>
8
9 #include <gtk/gtk.h>
10 #include <hildon/hildon-picker-button.h>
11 #include <hildon/hildon-touch-selector.h>
12 #include <hildon/hildon-button.h>
13 #include <hildon/hildon-entry.h>
14 #include <hildon/hildon-pannable-area.h>
15 #include <hildon/hildon-defines.h>
16
17 #include <gconf/gconf-client.h>
18
19 #include <libprofile.h>
20
21 #include "libtweakr-section/tweakr-section.h"
22 #include "libtweakr-section/tweakr-module.h"
23
24
25 #define TWEAKR_TYPE_PROFILE_SECTION \
26         (tweakr_profile_section_type)
27 #define TWEAKR_PROFILE_SECTION(obj) \
28         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
29         TWEAKR_TYPE_PROFILE_SECTION, \
30         TweakrProfileSection))
31 #define TWEAKR_PROFILE_SECTION_CLASS(k) \
32         (G_TYPE_CHECK_CLASS_CAST((k), \
33         TWEAKR_TYPE_PROFILE_SECTION, \
34         TweakrProfileSectionClass))
35 #define TWEAKR_IS_PROFILE_SECTION(obj) \
36         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
37         TWEAKR_TYPE_PROFILE_SECTION))
38
39 typedef struct _TweakrProfileSection TweakrProfileSection;
40 typedef struct _TweakrProfileSectionClass
41                TweakrProfileSectionClass;
42
43 struct _TweakrProfileSection
44 {
45     TweakrSection parent_instance;
46
47     GtkWidget *profile_button;
48 };
49
50 struct _TweakrProfileSectionClass
51 {
52     TweakrSectionClass parent_class;
53 };
54
55
56 static GType tweakr_profile_section_get_type (GTypeModule *module);
57 static void tweakr_profile_section_class_init
58     (TweakrProfileSectionClass *class);
59 static void tweakr_profile_section_init
60     (TweakrProfileSection *section);
61 static void tweakr_profile_section_dispose (GObject *obj);
62
63 static gboolean _save (TweakrSection *section,
64                        gboolean *requires_restart);
65
66 static GType tweakr_profile_section_type = 0;
67 static TweakrSectionClass *
68     tweakr_profile_section_parent_class = NULL;
69
70
71 G_MODULE_EXPORT void
72 tweakr_module_load (TweakrModule *module)
73 {
74     tweakr_profile_section_get_type (G_TYPE_MODULE (module));
75 }
76
77 G_MODULE_EXPORT void
78 tweakr_module_unload (TweakrModule *module)
79 {
80 }
81
82 static GType
83 tweakr_profile_section_get_type (GTypeModule *module)
84 {
85     if (!tweakr_profile_section_type)
86     {
87         static const GTypeInfo section_info =
88         {
89             sizeof (TweakrProfileSectionClass),
90             (GBaseInitFunc) NULL,
91             (GBaseFinalizeFunc) NULL,
92             (GClassInitFunc) tweakr_profile_section_class_init,
93             NULL,           /* class_finalize */
94             NULL,           /* class_data     */
95             sizeof (TweakrProfileSection),
96             0,              /* n_preallocs    */
97             (GInstanceInitFunc) tweakr_profile_section_init
98         };
99
100         tweakr_profile_section_type =
101             g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
102                                          "TweakrProfileSection",
103                                          &section_info, 0);
104     }
105
106     return tweakr_profile_section_type;
107 }
108
109 static void
110 tweakr_profile_section_class_init
111     (TweakrProfileSectionClass *klass)
112 {
113     GObjectClass *object_class = G_OBJECT_CLASS (klass);
114     TweakrSectionClass *section_class =
115         TWEAKR_SECTION_CLASS (klass);
116
117     tweakr_profile_section_parent_class =
118         g_type_class_peek_parent (klass);
119
120     section_class->name   = "_Profile";
121     section_class->save = _save;
122
123     object_class->dispose = tweakr_profile_section_dispose;
124 }
125
126 static GtkWidget *
127 _build_profile_select_button (TweakrProfileSection *section)
128 {
129     GtkWidget *button, *selector;
130     char **profiles;
131     gint i;
132     char *current_profile;
133
134     selector = hildon_touch_selector_new_text ();
135     hildon_touch_selector_append_text
136         (HILDON_TOUCH_SELECTOR (selector),
137          _("Save current profile with new name..."));
138
139     profiles = profile_get_profiles ();
140     for (i = 0; profiles && profiles[i] && profiles[i] != '\0'; i++)
141     {
142         /*
143          * Gotta make some l10n substitution here.
144          */
145         const gchar *p = profiles[i];
146
147         if (strcmp (p, "general") == 0)
148         {
149             p = _("General");
150         }
151         else if (strcmp (p, "silent") == 0)
152         {
153             p = _("Silent");
154         }
155
156         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
157                                            p);
158     }
159
160     current_profile = profile_get_profile ();
161     for (i = 0; profiles[i] && profiles[i] != '\0'; i++)
162     {
163         if (g_strcmp0 (profiles[i], current_profile) == 0)
164         {
165             hildon_touch_selector_set_active
166                 (HILDON_TOUCH_SELECTOR (selector), 0, i + 1);
167         }
168     }
169     g_free (current_profile);
170     profile_free_profiles (profiles);
171
172     button = hildon_picker_button_new (HILDON_SIZE_AUTO |
173                                        HILDON_SIZE_FINGER_HEIGHT,
174                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL);
175     hildon_button_set_title (HILDON_BUTTON (button),
176                              _("Use custom profile"));
177     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
178     hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
179                                        HILDON_TOUCH_SELECTOR (selector));
180
181     gtk_widget_show (button);
182     return button;
183 }
184
185 static void
186 tweakr_profile_section_init (TweakrProfileSection *section)
187 {
188     TweakrSection *iface;
189
190     iface = TWEAKR_SECTION (section);
191     iface->name = _("Profile");
192     iface->widget = gtk_vbox_new (FALSE, 0);
193
194     section->profile_button = _build_profile_select_button (section);
195
196     gtk_box_pack_start (GTK_BOX (iface->widget), section->profile_button,
197                         FALSE, FALSE, 0);
198 }
199
200 static void
201 tweakr_profile_section_dispose (GObject *obj)
202 {
203     G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
204 }
205
206 static gboolean _save (TweakrSection *section, gboolean *requires_restart)
207 {
208     return TRUE;
209 }
210