2 * vim:ts=4:sw=4:et:cindent:cino=(0
7 #include <glib/gi18n-lib.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 #include <hildon/hildon-pannable-area.h>
17 #include <gconf/gconf-client.h>
19 #include <libprofile.h>
21 #include "libtweakr-section/tweakr-section.h"
22 #include "libtweakr-section/tweakr-module.h"
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))
39 #define GCONF_PATH "/system/tweakr"
41 typedef struct _TweakrProfileSection TweakrProfileSection;
42 typedef struct _TweakrProfileSectionClass
43 TweakrProfileSectionClass;
45 struct _TweakrProfileSection
47 TweakrSection parent_instance;
49 GtkWidget *preset_button;
50 GtkWidget *delete_button;
51 GtkWidget *delete_dialog;
52 GtkWidget *delete_box;
56 struct _TweakrProfileSectionClass
58 TweakrSectionClass parent_class;
62 static GType tweakr_profile_section_get_type (GTypeModule *module);
63 static void tweakr_profile_section_class_init
64 (TweakrProfileSectionClass *class);
65 static void tweakr_profile_section_init
66 (TweakrProfileSection *section);
67 static void tweakr_profile_section_dispose (GObject *obj);
69 static gboolean _save (TweakrSection *section,
70 gboolean *requires_restart);
72 static GType tweakr_profile_section_type = 0;
73 static TweakrSectionClass *
74 tweakr_profile_section_parent_class = NULL;
78 tweakr_module_load (TweakrModule *module)
80 tweakr_profile_section_get_type (G_TYPE_MODULE (module));
84 tweakr_module_unload (TweakrModule *module)
89 tweakr_profile_section_get_type (GTypeModule *module)
91 if (!tweakr_profile_section_type)
93 static const GTypeInfo section_info =
95 sizeof (TweakrProfileSectionClass),
97 (GBaseFinalizeFunc) NULL,
98 (GClassInitFunc) tweakr_profile_section_class_init,
99 NULL, /* class_finalize */
100 NULL, /* class_data */
101 sizeof (TweakrProfileSection),
103 (GInstanceInitFunc) tweakr_profile_section_init
106 tweakr_profile_section_type =
107 g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
108 "TweakrProfileSection",
112 return tweakr_profile_section_type;
116 tweakr_profile_section_class_init
117 (TweakrProfileSectionClass *klass)
119 GObjectClass *object_class = G_OBJECT_CLASS (klass);
120 TweakrSectionClass *section_class =
121 TWEAKR_SECTION_CLASS (klass);
123 tweakr_profile_section_parent_class =
124 g_type_class_peek_parent (klass);
126 section_class->name = "_Profile";
127 section_class->save = _save;
129 object_class->dispose = tweakr_profile_section_dispose;
133 _save_preset_clicked (HildonPickerButton *button,
134 TweakrProfileSection *section)
137 GtkWidget *dialog, *entry;
138 const gchar* text = NULL;
140 parent = GTK_WINDOW (gtk_widget_get_ancestor (tweakr_section_get_widget
141 (TWEAKR_SECTION (section)),
143 dialog = gtk_dialog_new_with_buttons
146 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
147 GTK_STOCK_OK, GTK_RESPONSE_OK,
150 entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
151 gtk_widget_show (entry);
153 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry,
160 ret = gtk_dialog_run (GTK_DIALOG (dialog));
161 if (ret == GTK_RESPONSE_OK)
164 profileval_t *values, *values_iter;
166 gboolean cont = FALSE;
168 text = hildon_entry_get_text (HILDON_ENTRY (entry));
169 if (text == NULL || text[0] == '\0')
173 banner = hildon_banner_show_information
174 (dialog, NULL, _("Enter the name first."));
179 for (iter = 0; iter < strlen (text); iter++)
181 if (!g_ascii_isalnum (text[iter]) &&
187 banner = hildon_banner_show_information
189 _("Preset name can only contains letters, numbers, - and _."));
198 /* Save the settings to our own gconf directory. */
200 gconf = gconf_client_get_default ();
201 gconf_client_set_string (gconf, GCONF_PATH "/current-preset",
203 values = profile_get_values ("general");
204 for (values_iter = values;
205 values_iter->pv_key != NULL;
210 g_snprintf (key, 128, "%s/%s/%s", GCONF_PATH, text,
211 values_iter->pv_key);
212 gconf_client_set_string (gconf, key, values_iter->pv_val,
215 profile_free_values (values);
216 g_object_unref (gconf);
218 banner = hildon_banner_show_information
219 (GTK_WIDGET (parent), NULL,
220 _("Preset saved. Use the status menu to select it."));
224 gtk_widget_destroy (dialog);
227 gtk_widget_destroy (dialog);
232 _build_save_preset_button (TweakrProfileSection *section)
236 button = hildon_button_new (HILDON_SIZE_AUTO |
237 HILDON_SIZE_FINGER_HEIGHT,
238 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
239 hildon_button_set_title (HILDON_BUTTON (button),
240 _("Save current General profile to new preset"));
241 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
243 g_signal_connect (G_OBJECT (button), "clicked",
244 G_CALLBACK (_save_preset_clicked), section);
246 gtk_widget_show (button);
251 _preset_clicked (HildonButton *button, TweakrProfileSection *section)
253 const gchar *path = g_object_get_data (G_OBJECT (button), "path");
257 current = gconf_client_get_string (section->gconf,
258 GCONF_PATH "/current-preset",
260 basename = g_path_get_basename (path);
261 if (g_strcmp0 (current, basename) == 0)
263 gconf_client_set_string (section->gconf,
264 GCONF_PATH "/current-preset", _("None"),
268 gconf_client_recursive_unset (section->gconf, path, 0, NULL);
272 gtk_dialog_response (GTK_DIALOG (section->delete_dialog),
274 hildon_banner_show_information (NULL, NULL,
275 _("Preset deleted."));
279 _add_preset (gchar *preset, TweakrProfileSection *section)
284 basename = g_path_get_basename (preset);
285 button = hildon_button_new_with_text
286 (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL,
288 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
289 gtk_box_pack_start (GTK_BOX (section->delete_box), button, FALSE, FALSE,
291 g_object_set_data_full (G_OBJECT (button), "path", preset,
292 (GDestroyNotify) g_free);
293 g_signal_connect (button, "clicked", G_CALLBACK (_preset_clicked),
298 _delete_preset_clicked (HildonButton *b, TweakrProfileSection *section)
305 parent = gtk_widget_get_ancestor (GTK_WIDGET (b), GTK_TYPE_WINDOW);
307 section->delete_dialog = gtk_dialog_new ();
308 gtk_window_set_modal (GTK_WINDOW (section->delete_dialog), TRUE);
309 gtk_window_set_title (GTK_WINDOW (section->delete_dialog),
312 panarea = hildon_pannable_area_new ();
313 section->delete_box = gtk_vbox_new (FALSE, 0);
315 hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (panarea),
316 section->delete_box);
317 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (section->delete_dialog)->vbox),
318 panarea, TRUE, TRUE, 0);
320 if (section->gconf == NULL)
322 section->gconf = gconf_client_get_default ();
324 presets = gconf_client_all_dirs (section->gconf, GCONF_PATH, NULL);
326 g_object_set (G_OBJECT (panarea), "height-request",
327 MIN (350, g_slist_length (presets) * 70), NULL);
329 g_slist_foreach (presets, (GFunc) _add_preset, section);
331 gtk_widget_show_all (GTK_DIALOG (section->delete_dialog)->vbox);
332 ret = gtk_dialog_run (GTK_DIALOG (section->delete_dialog));
333 gtk_widget_destroy (section->delete_dialog);
335 g_slist_free (presets);
339 _build_delete_preset_button (TweakrProfileSection *section)
343 button = hildon_button_new (HILDON_SIZE_AUTO |
344 HILDON_SIZE_FINGER_HEIGHT,
345 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
346 hildon_button_set_title (HILDON_BUTTON (button),
347 _("Delete a profile preset"));
348 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
350 g_signal_connect (G_OBJECT (button), "clicked",
351 G_CALLBACK (_delete_preset_clicked), section);
353 gtk_widget_show (button);
358 tweakr_profile_section_init (TweakrProfileSection *section)
360 TweakrSection *iface;
362 iface = TWEAKR_SECTION (section);
363 iface->name = _("Profile presets");
364 iface->widget = gtk_vbox_new (FALSE, 0);
366 section->preset_button = _build_save_preset_button (section);
367 section->delete_button = _build_delete_preset_button (section);
369 gtk_box_pack_start (GTK_BOX (iface->widget), section->preset_button,
371 gtk_box_pack_start (GTK_BOX (iface->widget), section->delete_button,
376 tweakr_profile_section_dispose (GObject *obj)
378 TweakrProfileSection *section = TWEAKR_PROFILE_SECTION (obj);
380 if (section->gconf != NULL)
382 g_object_unref (section->gconf);
383 section->gconf = NULL;
386 G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
389 static gboolean _save (TweakrSection *section, gboolean *requires_restart)