Renaming maemo-tweaks to tweakr.
[tweakr] / modules / tweakr-desktop.c
diff --git a/modules/tweakr-desktop.c b/modules/tweakr-desktop.c
new file mode 100644 (file)
index 0000000..0aa8a6e
--- /dev/null
@@ -0,0 +1,257 @@
+/*
+ * vim:ts=4:sw=4:et:cindent:cino=(0
+ */ 
+
+#include <config.h>
+
+#include <gtk/gtk.h>
+#include <hildon/hildon-picker-button.h>
+#include <hildon/hildon-touch-selector.h>
+
+#include "libtweakr-section/tweakr-section.h"
+#include "libtweakr-section/tweakr-module.h"
+
+
+#define TWEAKR_TYPE_DESKTOP_SECTION \
+        (tweakr_desktop_section_type)
+#define TWEAKR_DESKTOP_SECTION(obj) \
+        (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+        TWEAKR_TYPE_DESKTOP_SECTION, \
+        TweakrDesktopSection))
+#define TWEAKR_DESKTOP_SECTION_CLASS(k) \
+        (G_TYPE_CHECK_CLASS_CAST((k), \
+        TWEAKR_TYPE_DESKTOP_SECTION, \
+        TweakrDesktopSectionClass))
+#define TWEAKR_IS_DESKTOP_SECTION(obj) \
+        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+        TWEAKR_TYPE_DESKTOP_SECTION))
+
+
+enum
+{
+    SNAP_NONE,
+    SNAP_SMALL,
+    SNAP_LARGE,
+    SNAP_HUGE
+};
+
+enum
+{
+    SNAP_NONE_VALUE,
+    SNAP_SMALL_VALUE = 8,
+    SNAP_LARGE_VALUE = 16,
+    SNAP_HUGE_VALUE = 32
+};
+
+static gint snap_values [] = {
+    SNAP_NONE_VALUE,
+    SNAP_SMALL_VALUE,
+    SNAP_LARGE_VALUE,
+    SNAP_HUGE_VALUE
+};
+
+typedef struct _TweakrDesktopSection TweakrDesktopSection;
+typedef struct _TweakrDesktopSectionClass
+               TweakrDesktopSectionClass;
+
+struct _TweakrDesktopSection
+{
+    TweakrSection parent_instance;
+
+    GKeyFile *ini;
+    GtkWidget *snap_button;
+};
+
+struct _TweakrDesktopSectionClass
+{
+    TweakrSectionClass parent_class;
+};
+
+
+static GType tweakr_desktop_section_get_type (GTypeModule *module);
+static void tweakr_desktop_section_class_init
+    (TweakrDesktopSectionClass *class);
+static void tweakr_desktop_section_init
+    (TweakrDesktopSection *section);
+static void tweakr_desktop_section_dispose (GObject *obj);
+
+static gboolean _save (TweakrSection *section,
+                       gboolean *requires_restart);
+
+static GType tweakr_desktop_section_type = 0;
+static TweakrSectionClass *
+    tweakr_desktop_section_parent_class = NULL;
+
+
+G_MODULE_EXPORT void
+tweakr_module_load (TweakrModule *module)
+{
+    tweakr_desktop_section_get_type (G_TYPE_MODULE (module));
+}
+
+G_MODULE_EXPORT void
+tweakr_module_unload (TweakrModule *module)
+{
+}
+
+static GType
+tweakr_desktop_section_get_type (GTypeModule *module)
+{
+    if (!tweakr_desktop_section_type)
+    {
+        static const GTypeInfo section_info =
+        {
+            sizeof (TweakrDesktopSectionClass),
+            (GBaseInitFunc) NULL,
+            (GBaseFinalizeFunc) NULL,
+            (GClassInitFunc) tweakr_desktop_section_class_init,
+            NULL,           /* class_finalize */
+            NULL,           /* class_data     */
+            sizeof (TweakrDesktopSection),
+            0,              /* n_preallocs    */
+            (GInstanceInitFunc) tweakr_desktop_section_init
+        };
+
+        tweakr_desktop_section_type =
+            g_type_module_register_type (module, TWEAKR_TYPE_SECTION,
+                                         "TweakrDesktopSection",
+                                         &section_info, 0);
+    }
+
+    return tweakr_desktop_section_type;
+}
+
+static void
+tweakr_desktop_section_class_init
+    (TweakrDesktopSectionClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+    TweakrSectionClass *section_class =
+        TWEAKR_SECTION_CLASS (klass);
+
+    tweakr_desktop_section_parent_class =
+        g_type_class_peek_parent (klass);
+
+    section_class->name   = "_Desktop";
+    section_class->save = _save;
+
+    object_class->dispose = tweakr_desktop_section_dispose;
+}
+
+GtkWidget * _build_snap_to_grid (void)
+{
+    const gchar *options[] = {"No", "Small", "Large", "Huge", NULL};
+    gint i = 0;
+    GtkWidget *button, *selector;
+
+    selector = hildon_touch_selector_new_text ();
+    while (options[i] && options[i] != '\0')
+    {
+        hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
+                                           options [i++]);
+    }
+
+    button = hildon_picker_button_new (HILDON_SIZE_AUTO,
+                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL);
+    hildon_gtk_widget_set_theme_size (button, HILDON_SIZE_FINGER_HEIGHT);
+
+    hildon_button_set_title (HILDON_BUTTON (button),
+                             "Snap icons to grid");
+    gtk_button_set_alignment (GTK_BUTTON (button), 0.0f, 0.5f);
+    hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
+                                       HILDON_TOUCH_SELECTOR (selector));
+
+    gtk_widget_show (button);
+    return button;
+}
+
+static void
+tweakr_desktop_section_init (TweakrDesktopSection *section)
+{
+    TweakrSection *iface;
+
+    gint snap_value = SNAP_NONE_VALUE;
+
+    section->snap_button = _build_snap_to_grid ();
+
+    section->ini = g_key_file_new ();
+
+    if (!g_key_file_load_from_file (section->ini, TRANSITIONS,
+                                    G_KEY_FILE_NONE, NULL))
+    {
+        g_warning ("%s: failed to load %s", G_STRFUNC, TRANSITIONS);
+        return;
+    }
+
+    snap_value = g_key_file_get_integer (section->ini, "edit_mode",
+                                         "snap_grid_size", NULL);
+
+    if (snap_value < SNAP_SMALL_VALUE)
+    {
+        hildon_picker_button_set_active
+            (HILDON_PICKER_BUTTON (section->snap_button), SNAP_NONE);
+    }
+    else if (snap_value < SNAP_LARGE_VALUE)
+    {
+        hildon_picker_button_set_active
+            (HILDON_PICKER_BUTTON (section->snap_button), SNAP_SMALL);
+    }
+    else if (snap_value < SNAP_HUGE_VALUE)
+    {
+        hildon_picker_button_set_active
+            (HILDON_PICKER_BUTTON (section->snap_button), SNAP_LARGE);
+    }
+    else
+    {
+        hildon_picker_button_set_active
+            (HILDON_PICKER_BUTTON (section->snap_button), SNAP_HUGE);
+    }
+
+
+
+    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);
+}
+
+static void
+tweakr_desktop_section_dispose (GObject *obj)
+{
+    TweakrDesktopSection *section = TWEAKR_DESKTOP_SECTION (obj);
+    if (section->ini)
+    {
+        g_key_file_free (section->ini);
+        section->ini = NULL;
+    }
+
+    G_OBJECT_CLASS (tweakr_desktop_section_parent_class)->dispose
+        (obj);
+}
+
+
+static gboolean  _save (TweakrSection *section,
+                        gboolean *requires_restart)
+{
+    gchar *argv[3];
+    gint active;
+
+    active = hildon_picker_button_get_active
+        (HILDON_PICKER_BUTTON (TWEAKR_DESKTOP_SECTION
+                               (section)->snap_button));
+
+    argv[0] = g_strdup ("/usr/bin/tweakr-desktop-save");
+    argv[1] = g_strdup_printf ("%d", snap_values[active]);
+    argv[2] = NULL;
+
+    g_spawn_sync ("/home/user", argv, NULL,
+                  G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL,
+                  NULL, NULL, NULL, NULL, NULL, NULL);
+
+    g_free (argv[0]);
+    g_free (argv[1]);
+
+    return TRUE;
+}
+