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