a1508be2727dc0b765a4a81310929d4e46449495
[tweakr] / maemo-tweaks.c
1 /*
2  * vim:ts=4:sw=4:et:cindent:cino=(0
3  */ 
4
5 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
6 #include <gtk/gtk.h>
7 #include <hildon/hildon-note.h>
8 #include <hildon/hildon-pannable-area.h>
9
10 #include "maemo-tweaks-types.h"
11 #include "libmaemo-tweaks-section/maemo-tweaks-section.h"
12 #include "maemo-tweaks-module-manager.h"
13
14 static gboolean save_ret;
15
16 GtkWidget *create_dialog (GtkWindow *parent)
17 {
18     GtkWidget *dialog;
19
20     dialog = gtk_dialog_new_with_buttons
21         ("Maemo Tweaks",
22          parent,
23          GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
24          GTK_STOCK_SAVE,
25          GTK_RESPONSE_OK,
26          GTK_STOCK_CANCEL,
27          GTK_RESPONSE_CANCEL,
28          NULL);
29
30     return dialog;
31 }
32
33 void _save (MaemoTweaksSection *section, gboolean *requires_restart)
34 {
35     save_ret &= maemo_tweaks_section_save (section, requires_restart);
36 }
37
38 osso_return_t execute (osso_context_t *osso, gpointer data,
39                        gboolean user_activated)
40 {
41     GtkWidget *dialog;
42     GtkWidget *panarea;
43     GtkWidget *box;
44     gint response;
45
46     MaemoTweaksModuleManager *manager;
47     GType *section_types;
48     guint n_sections;
49     GList *sections = NULL;
50     gint i;
51     gboolean requires_restart = FALSE;
52
53     manager = g_object_new (MAEMO_TWEAKS_TYPE_MODULE_MANAGER,
54                             "module-path", MODULES_DIR,
55                             NULL);
56
57     section_types = g_type_children (MAEMO_TWEAKS_TYPE_SECTION, &n_sections);
58
59     dialog = create_dialog (GTK_WINDOW (data));
60     panarea = hildon_pannable_area_new ();
61     box = gtk_vbox_new (FALSE, 0);
62
63     hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA (panarea),
64                                             box);
65     g_object_set (G_OBJECT (panarea), "height-request", 350, NULL);
66
67     for (i = 0; i < n_sections; i++)
68     {
69         MaemoTweaksSection *section;
70         MaemoTweaksSectionClass *klass;
71
72         klass = g_type_class_ref (section_types[i]);
73         section = maemo_tweaks_section_new (section_types[i]);
74
75         sections = g_list_prepend (sections, section);
76         gtk_box_pack_start (GTK_BOX (box),
77                             gtk_label_new (section->name),
78                             FALSE, FALSE, 0);
79         gtk_box_pack_start (GTK_BOX (box),
80                             maemo_tweaks_section_get_widget (section),
81                             FALSE, FALSE, 0);
82
83         g_type_class_unref (klass);
84     }
85
86     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), panarea,
87                         FALSE, FALSE, 0);
88     gtk_widget_show_all (GTK_DIALOG (dialog)->vbox);
89
90     for (;;)
91     {
92         save_ret = TRUE;
93         response = gtk_dialog_run (GTK_DIALOG (dialog));
94         if (response == GTK_RESPONSE_OK)
95         {
96             /* Save all settings */
97             g_list_foreach (sections, (GFunc) _save, &requires_restart);
98         }
99         if (save_ret)
100             break;
101     }
102
103     gtk_widget_destroy (GTK_WIDGET (dialog));
104     g_object_unref (manager);
105     if (sections)
106     {
107         g_list_foreach (sections, (GFunc) g_object_unref, NULL);
108         g_list_free (sections);
109     }
110
111     if (requires_restart)
112     {
113         GtkWidget *note;
114
115         note = hildon_note_new_information
116             (GTK_WINDOW (data), "Some of the settings you have changed"
117              " will take effect only after you restart your device.");
118         gtk_dialog_run (GTK_DIALOG (note));
119         gtk_widget_destroy (note);
120     }
121
122     return OSSO_OK;
123 }
124