Better description.
[tweakr] / modules / tweakr-profile.c
index 61e5d60..128035c 100644 (file)
@@ -12,6 +12,7 @@
 #include <hildon/hildon-entry.h>
 #include <hildon/hildon-defines.h>
 #include <hildon/hildon-banner.h>
+#include <hildon/hildon-pannable-area.h>
 
 #include <gconf/gconf-client.h>
 
@@ -35,6 +36,8 @@
         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
         TWEAKR_TYPE_PROFILE_SECTION))
 
+#define GCONF_PATH "/system/tweakr"
+
 typedef struct _TweakrProfileSection TweakrProfileSection;
 typedef struct _TweakrProfileSectionClass
                TweakrProfileSectionClass;
@@ -43,10 +46,11 @@ struct _TweakrProfileSection
 {
     TweakrSection parent_instance;
 
-    GtkWidget *profile_button;
-    GHashTable *profile_map;
-    guint value_changed : 1;
-    gint previous_selected;
+    GtkWidget *preset_button;
+    GtkWidget *delete_button;
+    GtkWidget *delete_dialog;
+    GtkWidget *delete_box;
+    GConfClient *gconf;
 };
 
 struct _TweakrProfileSectionClass
@@ -126,131 +130,204 @@ tweakr_profile_section_class_init
 }
 
 static void
-_value_changed (HildonPickerButton *button, TweakrProfileSection *section)
+_save_preset_clicked (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);
+    GtkWidget *dialog, *entry;
+    const gchar* text = NULL;
 
     parent = GTK_WINDOW (gtk_widget_get_ancestor (tweakr_section_get_widget
                                                   (TWEAKR_SECTION (section)),
                                                   GTK_TYPE_WINDOW));
-#if 0
-    if (active == 0)
+    dialog = gtk_dialog_new_with_buttons
+        (_("Preset 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')
     {
-        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')
+        ret = gtk_dialog_run (GTK_DIALOG (dialog));
+        if (ret == GTK_RESPONSE_OK)
         {
-            GtkTreeModel *model;
-            gint index;
+            GtkWidget *banner;
+            profileval_t *values, *values_iter;
+            GConfClient *gconf;
 
-            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')
             {
-                text = hildon_entry_get_text (HILDON_ENTRY (entry));
-                if (text == NULL || text[0] == '\0')
-                {
-                    GtkWidget *banner;
+                GtkWidget *banner;
 
-                    banner = hildon_banner_show_information
-                        (dialog, NULL, _("Enter the name first."));
+                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);
+                continue;
             }
-            else
+
+            /* Save the settings to our own gconf directory. */
+
+            gconf = gconf_client_get_default ();
+            gconf_client_set_string (gconf, GCONF_PATH "/current-preset",
+                                     _("None"), NULL);
+            values = profile_get_values ("general");
+            for (values_iter = values;
+                 values_iter->pv_key != NULL;
+                 values_iter++)
             {
-                gtk_widget_destroy (dialog);
-                hildon_touch_selector_set_active (selector, 0,
-                                                  section->previous_selected);
-                break;
+                gchar key[128];
+
+                g_snprintf (key, 128, "%s/%s/%s", GCONF_PATH, text,
+                            values_iter->pv_key);
+                gconf_client_set_string (gconf, key, values_iter->pv_val,
+                                         NULL);
             }
+            profile_free_values (values);
+            g_object_unref (gconf);
+
+            banner = hildon_banner_show_information
+                (GTK_WIDGET (parent), NULL,
+                 _("Preset saved. Use the status menu to select it."));
+        }
+        else
+        {
             gtk_widget_destroy (dialog);
+            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)
+_build_save_preset_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];
+    GtkWidget *button;
 
-        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));
+    button = hildon_button_new (HILDON_SIZE_AUTO |
+                                HILDON_SIZE_FINGER_HEIGHT,
+                                HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+    hildon_button_set_title (HILDON_BUTTON (button),
+                             _("Save current General profile to new preset"));
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
 
+    g_signal_connect (G_OBJECT (button), "clicked",
+                      G_CALLBACK (_save_preset_clicked), section);
+
+    gtk_widget_show (button);
+    return button;
+}
+
+static void
+_preset_clicked (HildonButton *button, TweakrProfileSection *section)
+{
+    const gchar *path = g_object_get_data (G_OBJECT (button), "path");
+    gchar *basename;
+    gchar *current;
+
+    current = gconf_client_get_string (section->gconf,
+                                       GCONF_PATH "/current-preset",
+                                       NULL);
+    basename = g_path_get_basename (path);
+    if (g_strcmp0 (current, basename) == 0)
+    {
+        gconf_client_set_string (section->gconf,
+                                 GCONF_PATH "/current-preset", _("None"),
+                                 NULL);
     }
 
-    current_profile = profile_get_profile ();
-    for (i = 0; profiles[i] && profiles[i] != '\0'; i++)
+    gconf_client_recursive_unset (section->gconf, path, 0, NULL);
+    g_free (basename);
+    g_free (current);
+
+    gtk_dialog_response (GTK_DIALOG (section->delete_dialog),
+                         GTK_RESPONSE_OK);
+    hildon_banner_show_information (NULL, NULL,
+                                    _("Preset deleted."));
+}
+
+static void
+_add_preset (gchar *preset, TweakrProfileSection *section)
+{
+    GtkWidget *button;
+    gchar *basename;
+
+    basename = g_path_get_basename (preset);
+    button = hildon_button_new_with_text
+        (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL,
+         basename, NULL);
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
+    gtk_box_pack_start (GTK_BOX (section->delete_box), button, FALSE, FALSE,
+                        0);
+    g_object_set_data_full (G_OBJECT (button), "path", preset,
+                            (GDestroyNotify) g_free);
+    g_signal_connect (button, "clicked", G_CALLBACK (_preset_clicked),
+                      section);
+}
+
+static void
+_delete_preset_clicked (HildonButton *b, TweakrProfileSection *section)
+{
+    GtkWidget *panarea;
+    gint ret;
+    GSList *presets;
+    GtkWidget *parent;
+
+    parent = gtk_widget_get_ancestor (GTK_WIDGET (b), GTK_TYPE_WINDOW);
+
+    section->delete_dialog = gtk_dialog_new ();
+    gtk_window_set_modal (GTK_WINDOW (section->delete_dialog), TRUE);
+    gtk_window_set_title (GTK_WINDOW (section->delete_dialog),
+                          _("Select preset"));
+
+    panarea = hildon_pannable_area_new ();
+    section->delete_box = gtk_vbox_new (FALSE, 0);
+
+    hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (panarea),
+                                            section->delete_box);
+    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (section->delete_dialog)->vbox),
+                        panarea, TRUE, TRUE, 0);
+
+    if (section->gconf == NULL)
     {
-        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*/;
-        }
+        section->gconf = gconf_client_get_default ();
     }
-    g_free (current_profile);
-    profile_free_profiles (profiles);
+    presets = gconf_client_all_dirs (section->gconf, GCONF_PATH, NULL);
+
+    g_object_set (G_OBJECT (panarea), "height-request",
+                  MIN (350, g_slist_length (presets) * 70), NULL);
 
-    button = hildon_picker_button_new (HILDON_SIZE_AUTO |
-                                       HILDON_SIZE_FINGER_HEIGHT,
-                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+    g_slist_foreach (presets, (GFunc) _add_preset, section);
+
+    gtk_widget_show_all (GTK_DIALOG (section->delete_dialog)->vbox);
+    ret = gtk_dialog_run (GTK_DIALOG (section->delete_dialog));
+    gtk_widget_destroy (section->delete_dialog);
+
+    g_slist_free (presets);
+}
+
+static GtkWidget *
+_build_delete_preset_button (TweakrProfileSection *section)
+{
+    GtkWidget *button;
+
+    button = hildon_button_new (HILDON_SIZE_AUTO |
+                                HILDON_SIZE_FINGER_HEIGHT,
+                                HILDON_BUTTON_ARRANGEMENT_VERTICAL);
     hildon_button_set_title (HILDON_BUTTON (button),
-                             _("Use custom profile"));
+                             _("Delete a profile preset"));
     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);
+    g_signal_connect (G_OBJECT (button), "clicked",
+                      G_CALLBACK (_delete_preset_clicked), section);
 
     gtk_widget_show (button);
     return button;
@@ -262,15 +339,15 @@ tweakr_profile_section_init (TweakrProfileSection *section)
     TweakrSection *iface;
 
     iface = TWEAKR_SECTION (section);
-    iface->name = _("Profiles");
+    iface->name = _("Profile presets");
     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);
+    section->preset_button = _build_save_preset_button (section);
+    section->delete_button = _build_delete_preset_button (section);
 
-    gtk_box_pack_start (GTK_BOX (iface->widget), section->profile_button,
+    gtk_box_pack_start (GTK_BOX (iface->widget), section->preset_button,
+                        FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (iface->widget), section->delete_button,
                         FALSE, FALSE, 0);
 }
 
@@ -279,10 +356,10 @@ tweakr_profile_section_dispose (GObject *obj)
 {
     TweakrProfileSection *section = TWEAKR_PROFILE_SECTION (obj);
 
-    if (section->profile_map)
+    if (section->gconf != NULL)
     {
-        g_hash_table_destroy (section->profile_map);
-        section->profile_map = NULL;
+        g_object_unref (section->gconf);
+        section->gconf = NULL;
     }
 
     G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
@@ -290,25 +367,6 @@ tweakr_profile_section_dispose (GObject *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;
 }