Better description.
[tweakr] / modules / tweakr-profile.c
index d3b0402..128035c 100644 (file)
@@ -9,10 +9,10 @@
 #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 <hildon/hildon-pannable-area.h>
 
 #include <gconf/gconf-client.h>
 
@@ -36,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;
@@ -44,7 +46,11 @@ struct _TweakrProfileSection
 {
     TweakrSection parent_instance;
 
-    GtkWidget *profile_button;
+    GtkWidget *preset_button;
+    GtkWidget *delete_button;
+    GtkWidget *delete_dialog;
+    GtkWidget *delete_box;
+    GConfClient *gconf;
 };
 
 struct _TweakrProfileSectionClass
@@ -123,60 +129,205 @@ tweakr_profile_section_class_init
     object_class->dispose = tweakr_profile_section_dispose;
 }
 
-static GtkWidget *
-_build_profile_select_button (TweakrProfileSection *section)
+static void
+_save_preset_clicked (HildonPickerButton *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++)
+    GtkWindow *parent;
+    GtkWidget *dialog, *entry;
+    const gchar* text = NULL;
+
+    parent = GTK_WINDOW (gtk_widget_get_ancestor (tweakr_section_get_widget
+                                                  (TWEAKR_SECTION (section)),
+                                                  GTK_TYPE_WINDOW));
+    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')
     {
-        /*
-         * Gotta make some l10n substitution here.
-         */
-        const gchar *p = profiles[i];
+        gint ret;
 
-        if (strcmp (p, "general") == 0)
+        ret = gtk_dialog_run (GTK_DIALOG (dialog));
+        if (ret == GTK_RESPONSE_OK)
         {
-            p = _("General");
+            GtkWidget *banner;
+            profileval_t *values, *values_iter;
+            GConfClient *gconf;
+
+            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;
+            }
+
+            /* 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++)
+            {
+                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 if (strcmp (p, "silent") == 0)
+        else
         {
-            p = _("Silent");
+            gtk_widget_destroy (dialog);
+            break;
         }
+        gtk_widget_destroy (dialog);
+    }
+}
+
+static GtkWidget *
+_build_save_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),
+                             _("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);
 
-        hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
-                                           p);
+    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->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), "clicked",
+                      G_CALLBACK (_delete_preset_clicked), section);
 
     gtk_widget_show (button);
     return button;
@@ -188,18 +339,29 @@ tweakr_profile_section_init (TweakrProfileSection *section)
     TweakrSection *iface;
 
     iface = TWEAKR_SECTION (section);
-    iface->name = _("Profile");
+    iface->name = _("Profile presets");
     iface->widget = gtk_vbox_new (FALSE, 0);
 
-    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);
 }
 
 static void
 tweakr_profile_section_dispose (GObject *obj)
 {
+    TweakrProfileSection *section = TWEAKR_PROFILE_SECTION (obj);
+
+    if (section->gconf != NULL)
+    {
+        g_object_unref (section->gconf);
+        section->gconf = NULL;
+    }
+
     G_OBJECT_CLASS (tweakr_profile_section_parent_class)->dispose (obj);
 }