initial import
[slovak-l10n] / ukeyboard / cpanel / prefs.c
1 /*
2  *  Copyright (c) 2008 Jiri Benc <jbenc@upir.cz>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License version 2 as
6  *  published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <gtk/gtk.h>
22 #include <libosso.h>
23 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
24 #include <gconf/gconf.h>
25 #include <gconf/gconf-client.h>
26 #include "prefs.h"
27 #include "hw.h"
28 #include "onscreen.h"
29 #include "lang.h"
30 #include "about.h"
31
32 #ifdef HAVE_MAEMO5
33 #define GETTEXT_PACKAGE "osso-applet-textinput"
34
35 #include <hildon/hildon.h>
36 #include <glib/gi18n-lib.h>
37 #endif
38
39 static init_func inits[] = { prefs_hw_init, prefs_onscreen_init, prefs_lang_init, prefs_about_init };
40 #define PLUGINS         (sizeof(inits) / sizeof(init_func))
41 static struct prefs prefs[PLUGINS];
42
43 gboolean internal_kbd;
44
45 #define IM_CONF_DIR     "/apps/osso/inputmethod"
46
47 gboolean get_bool(GConfClient *client, char *key)
48 {
49         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
50         gboolean res;
51
52         res = gconf_client_get_bool(client, tmp, NULL);
53         g_free(tmp);
54         return res;
55 }
56
57 void set_bool(GConfClient *client, char *key, gboolean val)
58 {
59         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
60
61         gconf_client_set_bool(client, tmp, val, NULL);
62         g_free(tmp);
63 }
64
65 gint get_int(GConfClient *client, char *key)
66 {
67         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
68         gint res;
69
70         res = gconf_client_get_int(client, tmp, NULL);
71         g_free(tmp);
72         return res;
73 }
74
75 void set_int(GConfClient *client, char *key, gint val)
76 {
77         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
78
79         gconf_client_set_int(client, tmp, val, NULL);
80         g_free(tmp);
81 }
82
83 gchar *get_str(GConfClient *client, char *key)
84 {
85         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
86         gchar *res;
87
88         res = gconf_client_get_string(client, tmp, NULL);
89         g_free(tmp);
90         return res;
91 }
92
93 void set_str(GConfClient *client, char *key, gchar *val)
94 {
95         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
96
97         gconf_client_set_string(client, tmp, val, NULL);
98         g_free(tmp);
99 }
100
101 static GConfClient *init_conf(void)
102 {
103         GConfClient *client;
104
105         client = gconf_client_get_default();
106         if (!client)
107                 return NULL;
108         gconf_client_add_dir(client, IM_CONF_DIR, GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
109         internal_kbd = get_bool(client, "have-internal-keyboard");
110         return client;
111 }
112
113 static void deinit_conf(GConfClient *client)
114 {
115         g_object_unref(G_OBJECT(client));
116 }
117
118 #ifdef HAVE_MAEMO5
119 osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
120 {
121         GConfClient *conf;
122         GtkDialog *dialog, *about;
123         GtkWidget *scroll, *widget, *vbox;
124         gchar *title;
125         void *plugin_data[PLUGINS];
126         unsigned i;
127         int res;
128
129         (void)osso; (void)user_activated;       /* shut up, GCC */
130
131         conf = init_conf();
132         if (!conf)
133                 return OSSO_ERROR;
134
135         for (i = 0; i < PLUGINS; i++) {
136                 inits[i](prefs + i);
137         }
138
139         dialog = GTK_DIALOG(gtk_dialog_new());
140         if (!dialog) {
141                 deinit_conf(conf);
142                 return OSSO_ERROR;
143         }
144         gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(data));
145
146         title = g_strdup_printf("%s (ukeyboard)", _("tein_ti_text_input_title"));
147         gtk_window_set_title(GTK_WINDOW(dialog), title);
148         g_free(title);
149
150
151         gtk_dialog_add_button(GTK_DIALOG(dialog), _HL("ecdg_ti_aboutdialog_title"), GTK_RESPONSE_HELP);
152         gtk_dialog_add_button(GTK_DIALOG(dialog), _HL("wdgt_bd_save"), GTK_RESPONSE_ACCEPT);
153
154         scroll = hildon_pannable_area_new();
155         gtk_widget_set_size_request(GTK_WIDGET (scroll), -1, 345);
156         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), scroll);
157
158         vbox = gtk_vbox_new(FALSE, 0);
159         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA(scroll), vbox);
160
161         gtk_widget_show_all(GTK_WIDGET(dialog));
162
163         for (i = 0; i < PLUGINS - 1; i++) {
164                 widget = prefs[i].start(conf, GTK_WIDGET(dialog), &plugin_data[i]);
165                 if (widget)
166                         gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, TRUE, 0);
167         }
168
169         while ((res = gtk_dialog_run(GTK_DIALOG(dialog))) == GTK_RESPONSE_HELP) {
170                 about = GTK_DIALOG(gtk_dialog_new());
171                 gtk_window_set_title(GTK_WINDOW(about), _HL("ecdg_ti_aboutdialog_title"));
172                 gtk_widget_set_size_request (GTK_WIDGET (about), -1, 300);
173
174                 widget = prefs[3].start(conf, GTK_WIDGET(about), &plugin_data[3]);
175                 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(about)->vbox), widget);
176
177                 gtk_widget_show_all(GTK_WIDGET(about));
178                 gtk_dialog_run(GTK_DIALOG(about));
179                 gtk_widget_destroy(GTK_WIDGET(about));
180
181         }
182         if (res == GTK_RESPONSE_ACCEPT) {
183                 for (i = 0; i < PLUGINS; i++)
184                         if (prefs[i].action)
185                                 prefs[i].action(conf, plugin_data[i]);
186         }
187         gtk_widget_destroy(GTK_WIDGET(dialog));
188
189         for (i = 0; i < PLUGINS; i++)
190                 if (prefs[i].stop)
191                         prefs[i].stop(conf, plugin_data[i]);
192
193         deinit_conf(conf);
194         return OSSO_OK;
195 }
196 #else
197 osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
198 {
199         GConfClient *conf;
200         GtkDialog *dialog;
201         GtkWidget *widget, *notebook;
202         void *plugin_data[PLUGINS];
203         unsigned i;
204         int res;
205
206         (void)osso; (void)user_activated;       /* shut up, GCC */
207
208         conf = init_conf();
209         if (!conf)
210                 return OSSO_ERROR;
211
212         for (i = 0; i < PLUGINS; i++) {
213                 inits[i](prefs + i);
214         }
215
216         dialog = GTK_DIALOG(gtk_dialog_new());
217         if (!dialog) {
218                 deinit_conf(conf);
219                 return OSSO_ERROR;
220         }
221         gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(data));
222         gtk_window_set_title(GTK_WINDOW(dialog), "Text input (ukeyboard)");
223         gtk_dialog_set_has_separator(dialog, FALSE);
224         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
225
226         gtk_dialog_add_action_widget(dialog, gtk_button_new_with_label("OK"), GTK_RESPONSE_ACCEPT);
227         gtk_dialog_add_action_widget(dialog, gtk_button_new_with_label("Cancel"), GTK_RESPONSE_REJECT);
228         notebook = gtk_notebook_new();
229         for (i = 0; i < PLUGINS; i++) {
230                 widget = prefs[i].start(conf, GTK_WIDGET(dialog), &plugin_data[i]);
231                 if (widget)
232                         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), widget, gtk_label_new(prefs[i].name));
233         }
234
235         gtk_container_add(GTK_CONTAINER(dialog->vbox), notebook);
236         gtk_widget_show_all(GTK_WIDGET(dialog));
237         res = gtk_dialog_run(dialog);
238         if (res == GTK_RESPONSE_ACCEPT) {
239                 for (i = 0; i < PLUGINS; i++)
240                         if (prefs[i].action)
241                                 prefs[i].action(conf, plugin_data[i]);
242         }
243         gtk_widget_destroy(GTK_WIDGET(dialog));
244
245         for (i = 0; i < PLUGINS; i++)
246                 if (prefs[i].stop)
247                         prefs[i].stop(conf, plugin_data[i]);
248
249         deinit_conf(conf);
250         return OSSO_OK;
251 }
252 #endif