Finalize support for profiles.
[tweakr] / modules / tweakr-profile.c
index d3b0402..61e5d60 100644 (file)
@@ -9,10 +9,9 @@
 #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>
 
@@ -45,6 +44,9 @@ struct _TweakrProfileSection
     TweakrSection parent_instance;
 
     GtkWidget *profile_button;
+    GHashTable *profile_map;
+    guint value_changed : 1;
+    gint previous_selected;
 };
 
 struct _TweakrProfileSectionClass
@@ -123,6 +125,78 @@ tweakr_profile_section_class_init
     object_class->dispose = tweakr_profile_section_dispose;
 }
 
+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)
 {
@@ -132,9 +206,11 @@ _build_profile_select_button (TweakrProfileSection *section)
     char *current_profile;
 
     selector = hildon_touch_selector_new_text ();
+    /*
     hildon_touch_selector_append_text
         (HILDON_TOUCH_SELECTOR (selector),
-         _("Save current profile with new name..."));
+         _("Save current profile with new name"));
+    */
 
     profiles = profile_get_profiles ();
     for (i = 0; profiles && profiles[i] && profiles[i] != '\0'; i++)
@@ -144,17 +220,11 @@ _build_profile_select_button (TweakrProfileSection *section)
          */
         const gchar *p = profiles[i];
 
-        if (strcmp (p, "general") == 0)
-        {
-            p = _("General");
-        }
-        else if (strcmp (p, "silent") == 0)
-        {
-            p = _("Silent");
-        }
-
         hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
-                                           p);
+                                           _(p));
+        g_hash_table_insert (section->profile_map,
+                             GINT_TO_POINTER (i /*+ 1*/), g_strdup (p));
+
     }
 
     current_profile = profile_get_profile ();
@@ -163,7 +233,8 @@ _build_profile_select_button (TweakrProfileSection *section)
         if (g_strcmp0 (profiles[i], current_profile) == 0)
         {
             hildon_touch_selector_set_active
-                (HILDON_TOUCH_SELECTOR (selector), 0, i + 1);
+                (HILDON_TOUCH_SELECTOR (selector), 0, i/* + 1*/);
+            section->previous_selected = i/* + 1*/;
         }
     }
     g_free (current_profile);
@@ -178,6 +249,9 @@ _build_profile_select_button (TweakrProfileSection *section)
     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;
 }
@@ -188,9 +262,12 @@ 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,
@@ -200,11 +277,38 @@ tweakr_profile_section_init (TweakrProfileSection *section)
 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;
 }