Finalize support for profiles.
[tweakr] / modules / tweakr-profile.c
index 749d018..61e5d60 100644 (file)
@@ -9,12 +9,14 @@
 #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 <hildon/hildon-banner.h>
+
 #include <gconf/gconf-client.h>
 
+#include <libprofile.h>
+
 #include "libtweakr-section/tweakr-section.h"
 #include "libtweakr-section/tweakr-module.h"
 
@@ -40,6 +42,11 @@ typedef struct _TweakrProfileSectionClass
 struct _TweakrProfileSection
 {
     TweakrSection parent_instance;
+
+    GtkWidget *profile_button;
+    GHashTable *profile_map;
+    guint value_changed : 1;
+    gint previous_selected;
 };
 
 struct _TweakrProfileSectionClass
@@ -119,22 +126,189 @@ tweakr_profile_section_class_init
 }
 
 static void
+_value_changed (HildonPickerButton *button, TweakrProfileSection *section)
+{
+    HildonTouchSelector *selector;
+    gint active;
+    GtkWindow *parent;
+
+    selector = hildon_picker_button_get_selector (button);
+    active = hildon_touch_selector_get_active (selector, 0);
+
+    parent = GTK_WINDOW (gtk_widget_get_ancestor (tweakr_section_get_widget
+                                                  (TWEAKR_SECTION (section)),
+                                                  GTK_TYPE_WINDOW));
+#if 0
+    if (active == 0)
+    {
+        GtkWidget *dialog, *entry;
+        gint ret;
+        const gchar *text = NULL;
+
+        dialog = gtk_dialog_new_with_buttons
+            (_("Save current profile with new name"),
+             parent,
+             GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
+             GTK_STOCK_OK, GTK_RESPONSE_OK,
+             NULL);
+
+        entry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
+        gtk_widget_show (entry);
+
+        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry,
+                            TRUE, TRUE, 0);
+        while (text == NULL || text [0] == '\0')
+        {
+            GtkTreeModel *model;
+            gint index;
+
+            ret = gtk_dialog_run (GTK_DIALOG (dialog));
+            if (ret == GTK_RESPONSE_OK)
+            {
+                text = hildon_entry_get_text (HILDON_ENTRY (entry));
+                if (text == NULL || text[0] == '\0')
+                {
+                    GtkWidget *banner;
+
+                    banner = hildon_banner_show_information
+                        (dialog, NULL, _("Enter the name first."));
+
+                    continue;
+                }
+
+                hildon_touch_selector_append_text (selector, text);
+                model = hildon_touch_selector_get_model (selector, 0);
+                index = gtk_tree_model_iter_n_children (model, NULL);
+                hildon_touch_selector_set_active (selector, 0, index);
+            }
+            else
+            {
+                gtk_widget_destroy (dialog);
+                hildon_touch_selector_set_active (selector, 0,
+                                                  section->previous_selected);
+                break;
+            }
+            gtk_widget_destroy (dialog);
+        }
+    }
+#endif
+    section->previous_selected = hildon_touch_selector_get_active (selector,
+                                                                   0);
+    section->value_changed = TRUE;
+}
+
+static GtkWidget *
+_build_profile_select_button (TweakrProfileSection *section)
+{
+    GtkWidget *button, *selector;
+    char **profiles;
+    gint i;
+    char *current_profile;
+
+    selector = hildon_touch_selector_new_text ();
+    /*
+    hildon_touch_selector_append_text
+        (HILDON_TOUCH_SELECTOR (selector),
+         _("Save current profile with new name"));
+    */
+
+    profiles = profile_get_profiles ();
+    for (i = 0; profiles && profiles[i] && profiles[i] != '\0'; i++)
+    {
+        /*
+         * Gotta make some l10n substitution here.
+         */
+        const gchar *p = profiles[i];
+
+        hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
+                                           _(p));
+        g_hash_table_insert (section->profile_map,
+                             GINT_TO_POINTER (i /*+ 1*/), g_strdup (p));
+
+    }
+
+    current_profile = profile_get_profile ();
+    for (i = 0; profiles[i] && profiles[i] != '\0'; i++)
+    {
+        if (g_strcmp0 (profiles[i], current_profile) == 0)
+        {
+            hildon_touch_selector_set_active
+                (HILDON_TOUCH_SELECTOR (selector), 0, i/* + 1*/);
+            section->previous_selected = i/* + 1*/;
+        }
+    }
+    g_free (current_profile);
+    profile_free_profiles (profiles);
+
+    button = hildon_picker_button_new (HILDON_SIZE_AUTO |
+                                       HILDON_SIZE_FINGER_HEIGHT,
+                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+    hildon_button_set_title (HILDON_BUTTON (button),
+                             _("Use custom profile"));
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
+    hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
+                                       HILDON_TOUCH_SELECTOR (selector));
+
+    g_signal_connect (G_OBJECT (button), "value-changed",
+                      G_CALLBACK (_value_changed), section);
+
+    gtk_widget_show (button);
+    return button;
+}
+
+static void
 tweakr_profile_section_init (TweakrProfileSection *section)
 {
     TweakrSection *iface;
 
     iface = TWEAKR_SECTION (section);
-    iface->name = _("Profile");
+    iface->name = _("Profiles");
+    iface->widget = gtk_vbox_new (FALSE, 0);
+
+    section->profile_map = g_hash_table_new_full (g_direct_hash,
+                                                  g_direct_equal,
+                                                  NULL, g_free);
+    section->profile_button = _build_profile_select_button (section);
+
+    gtk_box_pack_start (GTK_BOX (iface->widget), section->profile_button,
+                        FALSE, FALSE, 0);
 }
 
 static void
 tweakr_profile_section_dispose (GObject *obj)
 {
+    TweakrProfileSection *section = TWEAKR_PROFILE_SECTION (obj);
+
+    if (section->profile_map)
+    {
+        g_hash_table_destroy (section->profile_map);
+        section->profile_map = NULL;
+    }
+
     G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
 }
 
 static gboolean _save (TweakrSection *section, gboolean *requires_restart)
 {
+    if (TWEAKR_PROFILE_SECTION (section)->value_changed)
+    {
+        gint active;
+        const gchar *profile;
+
+        active =
+            hildon_picker_button_get_active (HILDON_PICKER_BUTTON
+                                             (TWEAKR_PROFILE_SECTION
+                                              (section)->profile_button));
+        profile = g_hash_table_lookup
+            (TWEAKR_PROFILE_SECTION (section)->profile_map,
+             GINT_TO_POINTER (active));
+
+        if (profile != NULL && profile[0] != '\0')
+        {
+            profile_set_profile (profile);
+        }
+    }
+
     return TRUE;
 }