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>
16 #include <gconf/gconf-client.h>
18 #include <libprofile.h>
20 #include "libtweakr-section/tweakr-section.h"
21 #include "libtweakr-section/tweakr-module.h"
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))
38 typedef struct _TweakrProfileSection TweakrProfileSection;
39 typedef struct _TweakrProfileSectionClass
40 TweakrProfileSectionClass;
42 struct _TweakrProfileSection
44 TweakrSection parent_instance;
46 GtkWidget *profile_button;
47 GHashTable *profile_map;
48 guint value_changed : 1;
49 gint previous_selected;
52 struct _TweakrProfileSectionClass
54 TweakrSectionClass parent_class;
58 static GType tweakr_profile_section_get_type (GTypeModule *module);
59 static void tweakr_profile_section_class_init
60 (TweakrProfileSectionClass *class);
61 static void tweakr_profile_section_init
62 (TweakrProfileSection *section);
63 static void tweakr_profile_section_dispose (GObject *obj);
65 static gboolean _save (TweakrSection *section,
66 gboolean *requires_restart);
68 static GType tweakr_profile_section_type = 0;
69 static TweakrSectionClass *
70 tweakr_profile_section_parent_class = NULL;
74 tweakr_module_load (TweakrModule *module)
76 tweakr_profile_section_get_type (G_TYPE_MODULE (module));
80 tweakr_module_unload (TweakrModule *module)
85 tweakr_profile_section_get_type (GTypeModule *module)
87 if (!tweakr_profile_section_type)
89 static const GTypeInfo section_info =
91 sizeof (TweakrProfileSectionClass),
93 (GBaseFinalizeFunc) NULL,
94 (GClassInitFunc) tweakr_profile_section_class_init,
95 NULL, /* class_finalize */
96 NULL, /* class_data */
97 sizeof (TweakrProfileSection),
99 (GInstanceInitFunc) tweakr_profile_section_init
102 tweakr_profile_section_type =
103 g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
104 "TweakrProfileSection",
108 return tweakr_profile_section_type;
112 tweakr_profile_section_class_init
113 (TweakrProfileSectionClass *klass)
115 GObjectClass *object_class = G_OBJECT_CLASS (klass);
116 TweakrSectionClass *section_class =
117 TWEAKR_SECTION_CLASS (klass);
119 tweakr_profile_section_parent_class =
120 g_type_class_peek_parent (klass);
122 section_class->name = "_Profile";
123 section_class->save = _save;
125 object_class->dispose = tweakr_profile_section_dispose;
129 _value_changed (HildonPickerButton *button, TweakrProfileSection *section)
131 HildonTouchSelector *selector;
135 selector = hildon_picker_button_get_selector (button);
136 active = hildon_touch_selector_get_active (selector, 0);
138 parent = GTK_WINDOW (gtk_widget_get_ancestor (tweakr_section_get_widget
139 (TWEAKR_SECTION (section)),
144 GtkWidget *dialog, *entry;
146 const gchar *text = NULL;
148 dialog = gtk_dialog_new_with_buttons
149 (_("Save current profile with new name"),
151 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
152 GTK_STOCK_OK, GTK_RESPONSE_OK,
155 entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
156 gtk_widget_show (entry);
158 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry,
160 while (text == NULL || text [0] == '\0')
165 ret = gtk_dialog_run (GTK_DIALOG (dialog));
166 if (ret == GTK_RESPONSE_OK)
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 hildon_touch_selector_append_text (selector, text);
180 model = hildon_touch_selector_get_model (selector, 0);
181 index = gtk_tree_model_iter_n_children (model, NULL);
182 hildon_touch_selector_set_active (selector, 0, index);
186 gtk_widget_destroy (dialog);
187 hildon_touch_selector_set_active (selector, 0,
188 section->previous_selected);
191 gtk_widget_destroy (dialog);
195 section->previous_selected = hildon_touch_selector_get_active (selector,
197 section->value_changed = TRUE;
201 _build_profile_select_button (TweakrProfileSection *section)
203 GtkWidget *button, *selector;
206 char *current_profile;
208 selector = hildon_touch_selector_new_text ();
210 hildon_touch_selector_append_text
211 (HILDON_TOUCH_SELECTOR (selector),
212 _("Save current profile with new name"));
215 profiles = profile_get_profiles ();
216 for (i = 0; profiles && profiles[i] && profiles[i] != '\0'; i++)
219 * Gotta make some l10n substitution here.
221 const gchar *p = profiles[i];
223 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
225 g_hash_table_insert (section->profile_map,
226 GINT_TO_POINTER (i /*+ 1*/), g_strdup (p));
230 current_profile = profile_get_profile ();
231 for (i = 0; profiles[i] && profiles[i] != '\0'; i++)
233 if (g_strcmp0 (profiles[i], current_profile) == 0)
235 hildon_touch_selector_set_active
236 (HILDON_TOUCH_SELECTOR (selector), 0, i/* + 1*/);
237 section->previous_selected = i/* + 1*/;
240 g_free (current_profile);
241 profile_free_profiles (profiles);
243 button = hildon_picker_button_new (HILDON_SIZE_AUTO |
244 HILDON_SIZE_FINGER_HEIGHT,
245 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
246 hildon_button_set_title (HILDON_BUTTON (button),
247 _("Use custom profile"));
248 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
249 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
250 HILDON_TOUCH_SELECTOR (selector));
252 g_signal_connect (G_OBJECT (button), "value-changed",
253 G_CALLBACK (_value_changed), section);
255 gtk_widget_show (button);
260 tweakr_profile_section_init (TweakrProfileSection *section)
262 TweakrSection *iface;
264 iface = TWEAKR_SECTION (section);
265 iface->name = _("Profiles");
266 iface->widget = gtk_vbox_new (FALSE, 0);
268 section->profile_map = g_hash_table_new_full (g_direct_hash,
271 section->profile_button = _build_profile_select_button (section);
273 gtk_box_pack_start (GTK_BOX (iface->widget), section->profile_button,
278 tweakr_profile_section_dispose (GObject *obj)
280 TweakrProfileSection *section = TWEAKR_PROFILE_SECTION (obj);
282 if (section->profile_map)
284 g_hash_table_destroy (section->profile_map);
285 section->profile_map = NULL;
288 G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
291 static gboolean _save (TweakrSection *section, gboolean *requires_restart)
293 if (TWEAKR_PROFILE_SECTION (section)->value_changed)
296 const gchar *profile;
299 hildon_picker_button_get_active (HILDON_PICKER_BUTTON
300 (TWEAKR_PROFILE_SECTION
301 (section)->profile_button));
302 profile = g_hash_table_lookup
303 (TWEAKR_PROFILE_SECTION (section)->profile_map,
304 GINT_TO_POINTER (active));
306 if (profile != NULL && profile[0] != '\0')
308 profile_set_profile (profile);