New feature: change bookmark labels.
[tweakr] / modules / tweakr-desktop.c
index 173a2a7..e66b296 100644 (file)
@@ -3,11 +3,17 @@
  */ 
 
 #include <config.h>
+#include <glib.h>
 #include <glib/gi18n-lib.h>
 
 #include <gtk/gtk.h>
 #include <hildon/hildon-picker-button.h>
 #include <hildon/hildon-touch-selector.h>
+#include <hildon/hildon-button.h>
+#include <hildon/hildon-entry.h>
+#include <hildon/hildon-pannable-area.h>
+#include <hildon/hildon-defines.h>
+#include <gconf/gconf-client.h>
 
 #include "libtweakr-section/tweakr-section.h"
 #include "libtweakr-section/tweakr-module.h"
@@ -27,7 +33,7 @@
         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
         TWEAKR_TYPE_DESKTOP_SECTION))
 
-
+#define GCONF_BOOKMARKS "/apps/osso/hildon-home/bookmarks"
 enum
 {
     SNAP_NONE,
@@ -51,6 +57,13 @@ static gint snap_values [] = {
     SNAP_HUGE_VALUE
 };
 
+typedef struct
+{
+    gchar *path;
+    gchar *value;
+    guint modified : 1;
+} bookmark_t;
+
 typedef struct _TweakrDesktopSection TweakrDesktopSection;
 typedef struct _TweakrDesktopSectionClass
                TweakrDesktopSectionClass;
@@ -61,6 +74,13 @@ struct _TweakrDesktopSection
 
     GKeyFile *ini;
     GtkWidget *snap_button;
+
+    GConfClient *gconf;
+    GtkWidget *bookmarks_button;
+    GtkWidget *bookmarks_dialog;
+    GtkWidget *bookmarks_box;
+    GHashTable *bookmarks_table;
+    guint save_bookmarks : 1;
 };
 
 struct _TweakrDesktopSectionClass
@@ -174,6 +194,169 @@ GtkWidget * _build_snap_to_grid (void)
 }
 
 static void
+_bookmark_t_destroy (bookmark_t *b)
+{
+    g_free (b->path);
+    g_free (b->value);
+    g_free (b);
+}
+
+static void
+_bookmark_clicked (HildonButton *button, TweakrDesktopSection *section)
+{
+    GtkWidget *dialog, *entry;
+    gint ret;
+
+    dialog = gtk_dialog_new_with_buttons
+            (_("Edit label"),
+             GTK_WINDOW (section->bookmarks_dialog),
+             GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
+             GTK_STOCK_OK, GTK_RESPONSE_OK,
+             NULL);
+
+    entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
+    hildon_entry_set_text (HILDON_ENTRY (entry),
+                           hildon_button_get_title (button));
+
+    gtk_entry_select_region (GTK_ENTRY (entry), 0, -1);
+    gtk_widget_show (entry);
+
+    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry,
+                        TRUE, TRUE, 0);
+
+    ret = gtk_dialog_run (GTK_DIALOG (dialog));
+    if (ret == GTK_RESPONSE_OK)
+    {
+        bookmark_t *b;
+        const gchar *label;
+
+        label = hildon_entry_get_text (HILDON_ENTRY (entry));
+        if (label && label[0])
+        {
+            hildon_button_set_title (button, label);
+
+            b = g_hash_table_lookup (section->bookmarks_table, button);
+            if (b)
+            {
+                g_free (b->value);
+                b->value = g_strdup (label);
+                b->modified = TRUE;
+            }
+        }
+    }
+
+    gtk_widget_destroy (dialog);
+}
+
+static void
+_add_bookmark (gchar *bookmark, TweakrDesktopSection *section)
+{
+    bookmark_t *b;
+    gchar *title, *value, *url;
+    GtkWidget *button;
+
+    b = g_new0 (bookmark_t, 1);
+    b->path = g_strconcat (bookmark, "/label", NULL);
+
+    url   = g_strconcat (bookmark, "/url",   NULL);
+    title = gconf_client_get_string (section->gconf, b->path, NULL);
+    value = gconf_client_get_string (section->gconf, url,   NULL);
+
+    g_free (url);
+
+    button = hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT,
+                                          HILDON_BUTTON_ARRANGEMENT_VERTICAL,
+                                          title, value);
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
+    gtk_box_pack_start (GTK_BOX (section->bookmarks_box), button,
+                        FALSE, FALSE, 0);
+
+    g_signal_connect (button, "clicked", G_CALLBACK (_bookmark_clicked),
+                                          section);
+
+    if (section->bookmarks_table == NULL)
+    {
+        section->bookmarks_table = g_hash_table_new_full
+            (g_direct_hash, g_direct_equal, NULL,
+             (GDestroyNotify) _bookmark_t_destroy);
+    }
+    g_hash_table_insert (section->bookmarks_table, button, b);
+
+    g_free (title);
+    g_free (value);
+}
+
+static void
+_edit_bookmarks_clicked (GtkButton *button, TweakrDesktopSection *section)
+{
+    GSList *bookmarks;
+
+    bookmarks = gconf_client_all_dirs (section->gconf, GCONF_BOOKMARKS,
+                                       NULL);
+    if (bookmarks && g_slist_length (bookmarks) > 0)
+    {
+        GtkWidget *panarea;
+        gint ret;
+
+        section->bookmarks_dialog = gtk_dialog_new_with_buttons
+            (_("Edit bookmark labels"),
+             GTK_WINDOW (gtk_widget_get_ancestor
+                         (tweakr_section_get_widget
+                          (TWEAKR_SECTION (section)),
+                          GTK_TYPE_WINDOW)),
+             GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
+             GTK_STOCK_OK, GTK_RESPONSE_OK,
+             NULL);
+
+        panarea = hildon_pannable_area_new ();
+        section->bookmarks_box = gtk_vbox_new (FALSE, 0);
+        hildon_pannable_area_add_with_viewport
+            (HILDON_PANNABLE_AREA (panarea), section->bookmarks_box);
+        g_object_set (G_OBJECT (panarea), "height-request",
+                      MIN (350, g_slist_length (bookmarks) * 70), NULL);
+
+        g_slist_foreach (bookmarks, (GFunc) _add_bookmark, section);
+
+        gtk_box_pack_start
+            (GTK_BOX (GTK_DIALOG (section->bookmarks_dialog)->vbox),
+             panarea, TRUE, TRUE, 0);
+
+        gtk_widget_show_all (GTK_DIALOG (section->bookmarks_dialog)->vbox);
+        ret = gtk_dialog_run (GTK_DIALOG (section->bookmarks_dialog));
+        if (ret == GTK_RESPONSE_OK)
+        {
+            section->save_bookmarks = TRUE;
+        }
+        gtk_widget_destroy (section->bookmarks_dialog);
+
+        g_slist_foreach (bookmarks, (GFunc) g_free, NULL);
+        g_slist_free (bookmarks);
+    }
+}
+
+GtkWidget *
+_build_bookmarks_button (TweakrDesktopSection *section)
+{
+    GtkWidget *button;
+
+    if (!gconf_client_dir_exists (section->gconf, GCONF_BOOKMARKS, NULL))
+    {
+        return NULL;
+    }
+
+    button = hildon_button_new_with_text (HILDON_SIZE_AUTO,
+                                          HILDON_BUTTON_ARRANGEMENT_VERTICAL,
+                                          _("Edit bookmarks labels"), "");
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
+    hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
+
+    g_signal_connect (button, "clicked",
+                      G_CALLBACK (_edit_bookmarks_clicked), section);
+    gtk_widget_show (button);
+    return button;
+}
+
+static void
 tweakr_desktop_section_init (TweakrDesktopSection *section)
 {
     TweakrSection *iface;
@@ -215,13 +398,20 @@ tweakr_desktop_section_init (TweakrDesktopSection *section)
             (HILDON_PICKER_BUTTON (section->snap_button), SNAP_HUGE);
     }
 
-
+    section->gconf = gconf_client_get_default ();
+    section->bookmarks_button = _build_bookmarks_button (section);
 
     iface = TWEAKR_SECTION (section);
     iface->name = _("Desktop");
     iface->widget = gtk_vbox_new (FALSE, 0);
     gtk_box_pack_start (GTK_BOX (iface->widget), section->snap_button,
                         FALSE, FALSE, 0);
+    if (section->bookmarks_button)
+    {
+        gtk_box_pack_start (GTK_BOX (iface->widget),
+                            section->bookmarks_button,
+                            FALSE, FALSE, 0);
+    }
 }
 
 static void
@@ -234,13 +424,34 @@ tweakr_desktop_section_dispose (GObject *obj)
         section->ini = NULL;
     }
 
+    if (section->gconf)
+    {
+        g_object_unref (section->gconf);
+        section->gconf = NULL;
+    }
+
+    if (section->bookmarks_table)
+    {
+        g_hash_table_destroy (section->bookmarks_table);
+        section->bookmarks_table = NULL;
+    }
+
     G_OBJECT_CLASS (tweakr_desktop_section_parent_class)->dispose
         (obj);
 }
 
+static void
+_save_bookmarks (HildonButton *button, bookmark_t *b,
+                 TweakrDesktopSection *section)
+{
+    if (b->modified)
+    {
+        gconf_client_set_string (section->gconf, b->path, b->value, NULL);
+    }
+}
 
-static gboolean  _save (TweakrSection *section,
-                        gboolean *requires_restart)
+static gboolean _save (TweakrSection *section,
+                       gboolean *requires_restart)
 {
     gchar *argv[3];
     gint active;
@@ -260,6 +471,14 @@ static gboolean  _save (TweakrSection *section,
     g_free (argv[0]);
     g_free (argv[1]);
 
+    if (TWEAKR_DESKTOP_SECTION (section)->save_bookmarks)
+    {
+        g_hash_table_foreach
+            (TWEAKR_DESKTOP_SECTION (section)->bookmarks_table,
+             (GHFunc) _save_bookmarks, section);
+        *requires_restart = TRUE;
+    }
+
     return TRUE;
 }