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