Change the profile concept to presets.
[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-entry.h>
13 #include <hildon/hildon-defines.h>
14 #include <hildon/hildon-banner.h>
15
16 #include <gconf/gconf-client.h>
17
18 #include <libprofile.h>
19
20 #include "libtweakr-section/tweakr-section.h"
21 #include "libtweakr-section/tweakr-module.h"
22
23
24 #define TWEAKR_TYPE_PROFILE_SECTION \
25         (tweakr_profile_section_type)
26 #define TWEAKR_PROFILE_SECTION(obj) \
27         (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
28         TWEAKR_TYPE_PROFILE_SECTION, \
29         TweakrProfileSection))
30 #define TWEAKR_PROFILE_SECTION_CLASS(k) \
31         (G_TYPE_CHECK_CLASS_CAST((k), \
32         TWEAKR_TYPE_PROFILE_SECTION, \
33         TweakrProfileSectionClass))
34 #define TWEAKR_IS_PROFILE_SECTION(obj) \
35         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
36         TWEAKR_TYPE_PROFILE_SECTION))
37
38 #define GCONF_PATH "/system/tweakr"
39
40 typedef struct _TweakrProfileSection TweakrProfileSection;
41 typedef struct _TweakrProfileSectionClass
42                TweakrProfileSectionClass;
43
44 struct _TweakrProfileSection
45 {
46     TweakrSection parent_instance;
47
48     GtkWidget *preset_button;
49 };
50
51 struct _TweakrProfileSectionClass
52 {
53     TweakrSectionClass parent_class;
54 };
55
56
57 static GType tweakr_profile_section_get_type (GTypeModule *module);
58 static void tweakr_profile_section_class_init
59     (TweakrProfileSectionClass *class);
60 static void tweakr_profile_section_init
61     (TweakrProfileSection *section);
62 static void tweakr_profile_section_dispose (GObject *obj);
63
64 static gboolean _save (TweakrSection *section,
65                        gboolean *requires_restart);
66
67 static GType tweakr_profile_section_type = 0;
68 static TweakrSectionClass *
69     tweakr_profile_section_parent_class = NULL;
70
71
72 G_MODULE_EXPORT void
73 tweakr_module_load (TweakrModule *module)
74 {
75     tweakr_profile_section_get_type (G_TYPE_MODULE (module));
76 }
77
78 G_MODULE_EXPORT void
79 tweakr_module_unload (TweakrModule *module)
80 {
81 }
82
83 static GType
84 tweakr_profile_section_get_type (GTypeModule *module)
85 {
86     if (!tweakr_profile_section_type)
87     {
88         static const GTypeInfo section_info =
89         {
90             sizeof (TweakrProfileSectionClass),
91             (GBaseInitFunc) NULL,
92             (GBaseFinalizeFunc) NULL,
93             (GClassInitFunc) tweakr_profile_section_class_init,
94             NULL,           /* class_finalize */
95             NULL,           /* class_data     */
96             sizeof (TweakrProfileSection),
97             0,              /* n_preallocs    */
98             (GInstanceInitFunc) tweakr_profile_section_init
99         };
100
101         tweakr_profile_section_type =
102             g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
103                                          "TweakrProfileSection",
104                                          &section_info, 0);
105     }
106
107     return tweakr_profile_section_type;
108 }
109
110 static void
111 tweakr_profile_section_class_init
112     (TweakrProfileSectionClass *klass)
113 {
114     GObjectClass *object_class = G_OBJECT_CLASS (klass);
115     TweakrSectionClass *section_class =
116         TWEAKR_SECTION_CLASS (klass);
117
118     tweakr_profile_section_parent_class =
119         g_type_class_peek_parent (klass);
120
121     section_class->name   = "_Profile";
122     section_class->save = _save;
123
124     object_class->dispose = tweakr_profile_section_dispose;
125 }
126
127 static void
128 _save_preset_clicked (HildonPickerButton *button,
129                       TweakrProfileSection *section)
130 {
131     GtkWindow *parent;
132     GtkWidget *dialog, *entry;
133     const gchar* text = NULL;
134
135     parent = GTK_WINDOW (gtk_widget_get_ancestor (tweakr_section_get_widget
136                                                   (TWEAKR_SECTION (section)),
137                                                   GTK_TYPE_WINDOW));
138     dialog = gtk_dialog_new_with_buttons
139         (_("Save current profile with new name"),
140          parent,
141          GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
142          GTK_STOCK_OK, GTK_RESPONSE_OK,
143          NULL);
144
145     entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
146     gtk_widget_show (entry);
147
148     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry,
149                         TRUE, TRUE, 0);
150     while (text == NULL || text [0] == '\0')
151     {
152         gint ret;
153
154         ret = gtk_dialog_run (GTK_DIALOG (dialog));
155         if (ret == GTK_RESPONSE_OK)
156         {
157             GtkWidget *banner;
158             profileval_t *values, *values_iter;
159             GConfClient *gconf;
160
161             text = hildon_entry_get_text (HILDON_ENTRY (entry));
162             if (text == NULL || text[0] == '\0')
163             {
164                 GtkWidget *banner;
165
166                 banner = hildon_banner_show_information
167                     (dialog, NULL, _("Enter the name first."));
168
169                 continue;
170             }
171
172             /* Save the settings to our own gconf directory. */
173
174             gconf = gconf_client_get_default ();
175             values = profile_get_values ("general");
176             for (values_iter = values;
177                  values_iter->pv_key != NULL;
178                  values_iter++)
179             {
180                 gchar key[128];
181
182                 g_snprintf (key, 128, "%s/%s/%s", GCONF_PATH, text,
183                             values_iter->pv_key);
184                 gconf_client_set_string (gconf, key, values_iter->pv_val,
185                                          NULL);
186             }
187             profile_free_values (values);
188             g_object_unref (gconf);
189
190             banner = hildon_banner_show_information
191                 (GTK_WIDGET (parent), NULL,
192                  _("Preset saved. Use the status menu to select it."));
193         }
194         else
195         {
196             gtk_widget_destroy (dialog);
197             break;
198         }
199         gtk_widget_destroy (dialog);
200     }
201 }
202
203 static GtkWidget *
204 _build_save_preset_button (TweakrProfileSection *section)
205 {
206     GtkWidget *button;
207
208     button = hildon_button_new (HILDON_SIZE_AUTO |
209                                 HILDON_SIZE_FINGER_HEIGHT,
210                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
211     hildon_button_set_title (HILDON_BUTTON (button),
212                              _("Save current General profile to new preset"));
213     gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
214
215     g_signal_connect (G_OBJECT (button), "clicked",
216                       G_CALLBACK (_save_preset_clicked), section);
217
218     gtk_widget_show (button);
219     return button;
220 }
221
222 static void
223 tweakr_profile_section_init (TweakrProfileSection *section)
224 {
225     TweakrSection *iface;
226
227     iface = TWEAKR_SECTION (section);
228     iface->name = _("Profiles");
229     iface->widget = gtk_vbox_new (FALSE, 0);
230
231     section->preset_button = _build_save_preset_button (section);
232
233     gtk_box_pack_start (GTK_BOX (iface->widget), section->preset_button,
234                         FALSE, FALSE, 0);
235 }
236
237 static void
238 tweakr_profile_section_dispose (GObject *obj)
239 {
240     G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
241 }
242
243 static gboolean _save (TweakrSection *section, gboolean *requires_restart)
244 {
245     return TRUE;
246 }
247