-/*
- * 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 <hildon/hildon-note.h>
-
-#include "libmaemo-tweaks-section/maemo-tweaks-section.h"
-#include "libmaemo-tweaks-section/maemo-tweaks-module.h"
-
-
-#define MAEMO_TWEAKS_TYPE_MCE_SECTION \
- (maemo_tweaks_mce_section_type)
-#define MAEMO_TWEAKS_MCE_SECTION(obj) \
- (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
- MAEMO_TWEAKS_TYPE_MCE_SECTION, \
- MaemoTweaksMceSection))
-#define MAEMO_TWEAKS_MCE_SECTION_CLASS(k) \
- (G_TYPE_CHECK_CLASS_CAST((k), \
- MAEMO_TWEAKS_TYPE_MCE_SECTION, \
- MaemoTweaksMceSectionClass))
-#define MAEMO_TWEAKS_IS_MCE_SECTION(obj) \
- (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
- MAEMO_TWEAKS_TYPE_MCE_SECTION))
-
-#define DISABLE_LABEL "Disable"
-#define SHOW_MENU_LABEL "Show menu"
-#define POWER_OFF_LABEL "Power off"
-#define LOCK_LABEL "Lock"
-
-enum {
- SHORT_POWER_KEY_DISABLED,
- SHORT_POWER_KEY_MENU,
- SHORT_POWER_KEY_OFF,
- SHORT_POWER_KEY_N
-};
-
-enum {
- LONG_POWER_KEY_DISABLED,
- LONG_POWER_KEY_MENU,
- LONG_POWER_KEY_OFF,
- LONG_POWER_KEY_N
-};
-
-enum {
- DOUBLE_POWER_KEY_DISABLED,
- DOUBLE_POWER_KEY_MENU,
- DOUBLE_POWER_KEY_OFF,
- DOUBLE_POWER_KEY_LOCK,
- DOUBLE_POWER_KEY_N
-};
-
-
-typedef struct _picker_t
-{
- guint index;
- const gchar *value;
- const gchar *label;
-} picker_t;
-
-static picker_t spk [] = {
- {SHORT_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
- {SHORT_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
- {SHORT_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL}
-};
-
-static picker_t lpk [] = {
- {LONG_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
- {LONG_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
- {LONG_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL}
-};
-
-static picker_t dpk [] = {
- {DOUBLE_POWER_KEY_DISABLED, "disabled", DISABLE_LABEL},
- {DOUBLE_POWER_KEY_MENU, "menu", SHOW_MENU_LABEL},
- {DOUBLE_POWER_KEY_OFF, "poweroff", POWER_OFF_LABEL},
- {DOUBLE_POWER_KEY_LOCK, "tklock", LOCK_LABEL}
-};
-
-
-typedef struct _MaemoTweaksMceSection MaemoTweaksMceSection;
-typedef struct _MaemoTweaksMceSectionClass
- MaemoTweaksMceSectionClass;
-
-struct _MaemoTweaksMceSection
-{
- MaemoTweaksSection parent_instance;
-
- GKeyFile *ini;
- GtkWidget *short_power_key;
- GtkWidget *long_power_key;
- GtkWidget *double_power_key;
-
- guint value_changed : 1;
-};
-
-struct _MaemoTweaksMceSectionClass
-{
- MaemoTweaksSectionClass parent_class;
-};
-
-
-static GType maemo_tweaks_mce_section_get_type (GTypeModule *module);
-static void maemo_tweaks_mce_section_class_init
- (MaemoTweaksMceSectionClass *class);
-static void maemo_tweaks_mce_section_init
- (MaemoTweaksMceSection *section);
-static void maemo_tweaks_mce_section_dispose (GObject *obj);
-
-static gboolean _save (MaemoTweaksSection *section,
- gboolean *requires_restart);
-
-static GType maemo_tweaks_mce_section_type = 0;
-static MaemoTweaksSectionClass *
- maemo_tweaks_mce_section_parent_class = NULL;
-
-
-G_MODULE_EXPORT void
-maemo_tweaks_module_load (MaemoTweaksModule *module)
-{
- maemo_tweaks_mce_section_get_type (G_TYPE_MODULE (module));
-}
-
-G_MODULE_EXPORT void
-maemo_tweaks_module_unload (MaemoTweaksModule *module)
-{
-}
-
-static GType
-maemo_tweaks_mce_section_get_type (GTypeModule *module)
-{
- if (!maemo_tweaks_mce_section_type)
- {
- static const GTypeInfo section_info =
- {
- sizeof (MaemoTweaksMceSectionClass),
- (GBaseInitFunc) NULL,
- (GBaseFinalizeFunc) NULL,
- (GClassInitFunc) maemo_tweaks_mce_section_class_init,
- NULL, /* class_finalize */
- NULL, /* class_data */
- sizeof (MaemoTweaksMceSection),
- 0, /* n_preallocs */
- (GInstanceInitFunc) maemo_tweaks_mce_section_init
- };
-
- maemo_tweaks_mce_section_type =
- g_type_module_register_type (module, MAEMO_TWEAKS_TYPE_SECTION,
- "MaemoTweaksMceSection",
- §ion_info, 0);
- }
-
- return maemo_tweaks_mce_section_type;
-}
-
-static void
-maemo_tweaks_mce_section_class_init
- (MaemoTweaksMceSectionClass *klass)
-{
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
- MaemoTweaksSectionClass *section_class =
- MAEMO_TWEAKS_SECTION_CLASS (klass);
-
- maemo_tweaks_mce_section_parent_class =
- g_type_class_peek_parent (klass);
-
- section_class->name = "_Mce";
- section_class->save = _save;
-
- object_class->dispose = maemo_tweaks_mce_section_dispose;
-}
-
-static void _value_changed (HildonPickerButton *b, gpointer user_data)
-{
- MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (user_data);
-
- section->value_changed = TRUE;
-}
-
-GtkWidget * _build_short_power_key (MaemoTweaksMceSection *section)
-{
- gint i;
- GtkWidget *button, *selector;
-
- selector = hildon_touch_selector_new_text ();
- for (i = 0; i < SHORT_POWER_KEY_N; i++)
- {
- hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
- spk[i].label);
- }
-
- 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),
- "Power key: short press");
- 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;
-}
-
-GtkWidget * _build_long_power_key (MaemoTweaksMceSection *section)
-{
- gint i;
- GtkWidget *button, *selector;
-
- selector = hildon_touch_selector_new_text ();
- for (i = 0; i < LONG_POWER_KEY_N; i++)
- {
- hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
- lpk[i].label);
- }
-
- 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),
- "Power key: long press");
- 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;
-}
-
-GtkWidget * _build_double_power_key (MaemoTweaksMceSection *section)
-{
- gint i;
- GtkWidget *button, *selector;
-
- selector = hildon_touch_selector_new_text ();
- for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
- {
- hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
- dpk[i].label);
- }
-
- 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),
- "Power key: double press");
- 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
-maemo_tweaks_mce_section_init (MaemoTweaksMceSection *section)
-{
- MaemoTweaksSection *iface;
- gchar *short_power_key_value;
- gchar *long_power_key_value;
- gchar *double_power_key_value;
- gint i;
-
- section->short_power_key = _build_short_power_key (section);
- section->long_power_key = _build_long_power_key (section);
- section->double_power_key = _build_double_power_key (section);
-
- section->ini = g_key_file_new ();
-
- if (!g_key_file_load_from_file (section->ini, MCE,
- G_KEY_FILE_NONE, NULL))
- {
- g_warning ("%s: failed to load %s", G_STRFUNC, MCE);
- return;
- }
-
- short_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
- "PowerKeyShortAction",
- NULL);
- long_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
- "PowerKeyLongAction",
- NULL);
- double_power_key_value = g_key_file_get_string (section->ini, "PowerKey",
- "PowerKeyDoubleAction",
- NULL);
-
- for (i = 0; i < SHORT_POWER_KEY_N; i++)
- {
- if (g_strcmp0 (short_power_key_value, spk[i].value) == 0)
- {
- hildon_picker_button_set_active
- (HILDON_PICKER_BUTTON (section->short_power_key), i);
- }
- }
-
- for (i = 0; i < LONG_POWER_KEY_N; i++)
- {
- if (g_strcmp0 (long_power_key_value, lpk[i].value) == 0)
- {
- hildon_picker_button_set_active
- (HILDON_PICKER_BUTTON (section->long_power_key), i);
- }
- }
-
- for (i = 0; i < DOUBLE_POWER_KEY_N; i++)
- {
- if (g_strcmp0 (double_power_key_value, dpk[i].value) == 0)
- {
- hildon_picker_button_set_active
- (HILDON_PICKER_BUTTON (section->double_power_key), i);
- }
- }
-
- section->value_changed = FALSE;
-
- iface = MAEMO_TWEAKS_SECTION (section);
- iface->name = "Mce";
- iface->widget = gtk_vbox_new (FALSE, 0);
- gtk_box_pack_start (GTK_BOX (iface->widget), section->short_power_key,
- FALSE, FALSE, 0);
- gtk_box_pack_start (GTK_BOX (iface->widget), section->long_power_key,
- FALSE, FALSE, 0);
- gtk_box_pack_start (GTK_BOX (iface->widget), section->double_power_key,
- FALSE, FALSE, 0);
-}
-
-static void
-maemo_tweaks_mce_section_dispose (GObject *obj)
-{
- MaemoTweaksMceSection *section = MAEMO_TWEAKS_MCE_SECTION (obj);
- if (section->ini)
- {
- g_key_file_free (section->ini);
- section->ini = NULL;
- }
-
- G_OBJECT_CLASS (maemo_tweaks_mce_section_parent_class)->dispose
- (obj);
-}
-
-static gboolean _save (MaemoTweaksSection *section,
- gboolean *requires_restart)
-{
- gchar *argv[5];
- gint short_active, long_active, double_active;
- gint i;
-
- if (!MAEMO_TWEAKS_MCE_SECTION (section)->value_changed)
- return TRUE;
-
- short_active = hildon_picker_button_get_active
- (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
- (section)->short_power_key));
-
- long_active = hildon_picker_button_get_active
- (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
- (section)->long_power_key));
-
- double_active = hildon_picker_button_get_active
- (HILDON_PICKER_BUTTON (MAEMO_TWEAKS_MCE_SECTION
- (section)->double_power_key));
-
- if (short_active == SHORT_POWER_KEY_DISABLED &&
- long_active == LONG_POWER_KEY_DISABLED &&
- double_active == DOUBLE_POWER_KEY_DISABLED)
- {
- GtkWidget *note;
- gint retcode;
-
- note = hildon_note_new_confirmation
- (NULL, "Setting all Power Key options to \"Disabled\" means "
- "that the only way to turn the device off is the removal of "
- "the battery. Do you want to continue?");
- retcode = gtk_dialog_run (GTK_DIALOG (note));
- gtk_widget_destroy (note);
-
- if (retcode == GTK_RESPONSE_CANCEL)
- return FALSE;
- }
-
- *requires_restart = TRUE;
-
- argv[0] = g_strdup ("/usr/bin/maemo-tweaks-mce-save");
- argv[1] = g_strdup_printf ("%s", spk[short_active].value);
- argv[2] = g_strdup_printf ("%s", lpk[long_active].value);
- argv[3] = g_strdup_printf ("%s", dpk[double_active].value);
- argv[4] = 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);
-
- for (i = 0; i < (sizeof (argv) / sizeof (gchar *)) - 1; i++)
- g_free (argv[i]);
-
- return TRUE;
-}
-