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-button.h>
13 #include <hildon/hildon-entry.h>
14 #include <hildon/hildon-pannable-area.h>
15 #include <hildon/hildon-defines.h>
16 #include <gconf/gconf-client.h>
18 #include "libtweakr-section/tweakr-section.h"
19 #include "libtweakr-section/tweakr-module.h"
22 #define TWEAKR_TYPE_DESKTOP_SECTION \
23 (tweakr_desktop_section_type)
24 #define TWEAKR_DESKTOP_SECTION(obj) \
25 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
26 TWEAKR_TYPE_DESKTOP_SECTION, \
27 TweakrDesktopSection))
28 #define TWEAKR_DESKTOP_SECTION_CLASS(k) \
29 (G_TYPE_CHECK_CLASS_CAST((k), \
30 TWEAKR_TYPE_DESKTOP_SECTION, \
31 TweakrDesktopSectionClass))
32 #define TWEAKR_IS_DESKTOP_SECTION(obj) \
33 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
34 TWEAKR_TYPE_DESKTOP_SECTION))
36 #define GCONF_BOOKMARKS "/apps/osso/hildon-home/bookmarks"
37 #define GCONF_APPLETS "/apps/osso/hildon-desktop/applets"
51 SNAP_LARGE_VALUE = 16,
55 static gint snap_values [] = {
69 typedef struct _TweakrDesktopSection TweakrDesktopSection;
70 typedef struct _TweakrDesktopSectionClass
71 TweakrDesktopSectionClass;
73 struct _TweakrDesktopSection
75 TweakrSection parent_instance;
78 GtkWidget *snap_button;
81 GtkWidget *bookmarks_button;
82 GtkWidget *bookmarks_dialog;
83 GtkWidget *bookmarks_box;
84 GHashTable *bookmarks_table;
85 guint save_bookmarks : 1;
88 struct _TweakrDesktopSectionClass
90 TweakrSectionClass parent_class;
94 static GType tweakr_desktop_section_get_type (GTypeModule *module);
95 static void tweakr_desktop_section_class_init
96 (TweakrDesktopSectionClass *class);
97 static void tweakr_desktop_section_init
98 (TweakrDesktopSection *section);
99 static void tweakr_desktop_section_dispose (GObject *obj);
101 static gboolean _save (TweakrSection *section,
102 gboolean *requires_restart);
104 static GType tweakr_desktop_section_type = 0;
105 static TweakrSectionClass *
106 tweakr_desktop_section_parent_class = NULL;
110 tweakr_module_load (TweakrModule *module)
112 tweakr_desktop_section_get_type (G_TYPE_MODULE (module));
116 tweakr_module_unload (TweakrModule *module)
121 tweakr_desktop_section_get_type (GTypeModule *module)
123 if (!tweakr_desktop_section_type)
125 static const GTypeInfo section_info =
127 sizeof (TweakrDesktopSectionClass),
128 (GBaseInitFunc) NULL,
129 (GBaseFinalizeFunc) NULL,
130 (GClassInitFunc) tweakr_desktop_section_class_init,
131 NULL, /* class_finalize */
132 NULL, /* class_data */
133 sizeof (TweakrDesktopSection),
135 (GInstanceInitFunc) tweakr_desktop_section_init
138 tweakr_desktop_section_type =
139 g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
140 "TweakrDesktopSection",
144 return tweakr_desktop_section_type;
148 tweakr_desktop_section_class_init
149 (TweakrDesktopSectionClass *klass)
151 GObjectClass *object_class = G_OBJECT_CLASS (klass);
152 TweakrSectionClass *section_class =
153 TWEAKR_SECTION_CLASS (klass);
155 tweakr_desktop_section_parent_class =
156 g_type_class_peek_parent (klass);
158 section_class->name = "_Desktop";
159 section_class->save = _save;
161 object_class->dispose = tweakr_desktop_section_dispose;
164 GtkWidget * _build_snap_to_grid (void)
166 const gchar *options[] =
175 GtkWidget *button, *selector;
177 selector = hildon_touch_selector_new_text ();
178 while (options[i] && options[i] != '\0')
180 hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
184 button = hildon_picker_button_new (HILDON_SIZE_AUTO,
185 HILDON_BUTTON_ARRANGEMENT_VERTICAL);
186 hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
188 hildon_button_set_title (HILDON_BUTTON (button),
189 _("Snap icons to grid"));
190 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
191 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
192 HILDON_TOUCH_SELECTOR (selector));
194 gtk_widget_show (button);
199 _bookmark_t_destroy (bookmark_t *b)
207 _bookmark_clicked (HildonButton *button, TweakrDesktopSection *section)
209 GtkWidget *dialog, *entry;
212 dialog = gtk_dialog_new_with_buttons
214 GTK_WINDOW (section->bookmarks_dialog),
215 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
216 GTK_STOCK_OK, GTK_RESPONSE_OK,
219 entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
220 hildon_entry_set_text (HILDON_ENTRY (entry),
221 hildon_button_get_title (button));
223 gtk_entry_select_region (GTK_ENTRY (entry), 0, -1);
224 gtk_widget_show (entry);
226 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry,
229 ret = gtk_dialog_run (GTK_DIALOG (dialog));
230 if (ret == GTK_RESPONSE_OK)
235 label = hildon_entry_get_text (HILDON_ENTRY (entry));
236 if (label && label[0])
238 hildon_button_set_title (button, label);
240 b = g_hash_table_lookup (section->bookmarks_table, button);
244 b->value = g_strdup (label);
250 gtk_widget_destroy (dialog);
254 _add_bookmark (gchar *bookmark, TweakrDesktopSection *section)
257 gchar *title, *value, *url;
260 gchar *bookmark_name;
263 b = g_new0 (bookmark_t, 1);
264 b->path = g_strconcat (bookmark, "/label", NULL);
266 url = g_strconcat (bookmark, "/url", NULL);
267 title = gconf_client_get_string (section->gconf, b->path, NULL);
268 value = gconf_client_get_string (section->gconf, url, NULL);
272 bookmark_name = g_path_get_basename (bookmark);
273 applet_path = g_strconcat (GCONF_APPLETS, "/BookmarkShortcut:",
274 bookmark_name, NULL);
276 gconf_client_dir_exists (section->gconf, applet_path, NULL))
278 button = hildon_button_new_with_text
279 (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL,
281 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
282 gtk_box_pack_start (GTK_BOX (section->bookmarks_box), button,
285 g_signal_connect (button, "clicked", G_CALLBACK (_bookmark_clicked),
288 if (section->bookmarks_table == NULL)
290 section->bookmarks_table = g_hash_table_new_full
291 (g_direct_hash, g_direct_equal, NULL,
292 (GDestroyNotify) _bookmark_t_destroy);
294 g_hash_table_insert (section->bookmarks_table, button, b);
298 _bookmark_t_destroy (b);
303 g_free (bookmark_name);
304 g_free (applet_path);
308 _edit_bookmarks_clicked (GtkButton *button, TweakrDesktopSection *section)
312 bookmarks = gconf_client_all_dirs (section->gconf, GCONF_BOOKMARKS,
314 if (bookmarks && g_slist_length (bookmarks) > 0)
319 if (section->bookmarks_dialog == NULL)
321 section->bookmarks_dialog = gtk_dialog_new_with_buttons
322 (_("Edit desktop bookmarks"),
323 GTK_WINDOW (gtk_widget_get_ancestor
324 (tweakr_section_get_widget
325 (TWEAKR_SECTION (section)),
327 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
328 GTK_STOCK_OK, GTK_RESPONSE_OK,
331 panarea = hildon_pannable_area_new ();
332 section->bookmarks_box = gtk_vbox_new (FALSE, 0);
333 hildon_pannable_area_add_with_viewport
334 (HILDON_PANNABLE_AREA (panarea), section->bookmarks_box);
335 g_object_set (G_OBJECT (panarea), "height-request",
336 MIN (350, g_slist_length (bookmarks) * 70), NULL);
338 g_slist_foreach (bookmarks, (GFunc) _add_bookmark, section);
341 (GTK_BOX (GTK_DIALOG (section->bookmarks_dialog)->vbox),
342 panarea, TRUE, TRUE, 0);
344 gtk_widget_show_all (GTK_DIALOG (section->bookmarks_dialog)->vbox);
345 ret = gtk_dialog_run (GTK_DIALOG (section->bookmarks_dialog));
346 if (ret == GTK_RESPONSE_OK)
348 section->save_bookmarks = TRUE;
350 gtk_widget_hide (section->bookmarks_dialog);
352 g_slist_foreach (bookmarks, (GFunc) g_free, NULL);
353 g_slist_free (bookmarks);
358 _build_bookmarks_button (TweakrDesktopSection *section)
362 if (!gconf_client_dir_exists (section->gconf, GCONF_BOOKMARKS, NULL))
367 button = hildon_button_new_with_text (HILDON_SIZE_AUTO,
368 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
369 _("Edit desktop bookmarks"), "");
370 gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
371 hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
373 g_signal_connect (button, "clicked",
374 G_CALLBACK (_edit_bookmarks_clicked), section);
375 gtk_widget_show (button);
380 tweakr_desktop_section_init (TweakrDesktopSection *section)
382 TweakrSection *iface;
384 gint snap_value = SNAP_NONE_VALUE;
386 section->ini = g_key_file_new ();
388 if (!g_key_file_load_from_file (section->ini, TRANSITIONS,
389 G_KEY_FILE_NONE, NULL))
391 g_warning ("%s: failed to load %s", G_STRFUNC, TRANSITIONS);
395 if (g_key_file_has_key (section->ini, "edit_mode", "snap_grid_size",
398 section->snap_button = _build_snap_to_grid ();
400 snap_value = g_key_file_get_integer (section->ini, "edit_mode",
401 "snap_grid_size", NULL);
403 if (snap_value < SNAP_SMALL_VALUE)
405 hildon_picker_button_set_active
406 (HILDON_PICKER_BUTTON (section->snap_button), SNAP_NONE);
408 else if (snap_value < SNAP_LARGE_VALUE)
410 hildon_picker_button_set_active
411 (HILDON_PICKER_BUTTON (section->snap_button), SNAP_SMALL);
413 else if (snap_value < SNAP_HUGE_VALUE)
415 hildon_picker_button_set_active
416 (HILDON_PICKER_BUTTON (section->snap_button), SNAP_LARGE);
420 hildon_picker_button_set_active
421 (HILDON_PICKER_BUTTON (section->snap_button), SNAP_HUGE);
424 section->gconf = gconf_client_get_default ();
425 section->bookmarks_button = _build_bookmarks_button (section);
427 iface = TWEAKR_SECTION (section);
428 iface->name = _("Desktop");
429 iface->widget = gtk_vbox_new (FALSE, 0);
431 if (section->snap_button)
433 gtk_box_pack_start (GTK_BOX (iface->widget), section->snap_button,
436 if (section->bookmarks_button)
438 gtk_box_pack_start (GTK_BOX (iface->widget),
439 section->bookmarks_button,
445 tweakr_desktop_section_dispose (GObject *obj)
447 TweakrDesktopSection *section = TWEAKR_DESKTOP_SECTION (obj);
450 g_key_file_free (section->ini);
456 g_object_unref (section->gconf);
457 section->gconf = NULL;
460 if (section->bookmarks_table)
462 g_hash_table_destroy (section->bookmarks_table);
463 section->bookmarks_table = NULL;
466 G_OBJECT_CLASS (tweakr_desktop_section_parent_class)->dispose
471 _save_bookmarks (HildonButton *button, bookmark_t *b,
472 TweakrDesktopSection *section)
476 gconf_client_set_string (section->gconf, b->path, b->value, NULL);
480 static gboolean _save (TweakrSection *section,
481 gboolean *requires_restart)
486 active = hildon_picker_button_get_active
487 (HILDON_PICKER_BUTTON (TWEAKR_DESKTOP_SECTION
488 (section)->snap_button));
490 argv[0] = g_strdup ("/usr/bin/tweakr-desktop-save");
491 argv[1] = g_strdup_printf ("%d", snap_values[active]);
494 g_spawn_sync ("/home/user", argv, NULL,
495 G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
496 NULL, NULL, NULL, NULL, NULL, NULL);
501 if (TWEAKR_DESKTOP_SECTION (section)->save_bookmarks)
504 (TWEAKR_DESKTOP_SECTION (section)->bookmarks_table,
505 (GHFunc) _save_bookmarks, section);
506 *requires_restart = TRUE;