ukeyboard updated to version 2.0
[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 <hildon/hildon.h>
26 #include <gconf/gconf.h>
27 #include <gconf/gconf-client.h>
28 #include "prefs.h"
29 #include "hw.h"
30 #include "onscreen.h"
31 #include "lang.h"
32 #include "about.h"
33
34 #define GETTEXT_PACKAGE "osso-applet-textinput"
35 #include <glib/gi18n-lib.h>
36
37 static init_func inits[] = { prefs_hw_init, prefs_onscreen_init, prefs_lang_init, prefs_about_init };
38 #define PLUGINS         (sizeof(inits) / sizeof(init_func))
39 static struct prefs prefs[PLUGINS];
40
41 gboolean internal_kbd;
42
43 #define IM_CONF_DIR     "/apps/osso/inputmethod"
44
45 gboolean get_bool(GConfClient *client, char *key)
46 {
47         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
48         gboolean res;
49
50         res = gconf_client_get_bool(client, tmp, NULL);
51         g_free(tmp);
52         return res;
53 }
54
55 void set_bool(GConfClient *client, char *key, gboolean val)
56 {
57         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
58
59         gconf_client_set_bool(client, tmp, val, NULL);
60         g_free(tmp);
61 }
62
63 gint get_int(GConfClient *client, char *key)
64 {
65         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
66         gint res;
67
68         res = gconf_client_get_int(client, tmp, NULL);
69         g_free(tmp);
70         return res;
71 }
72
73 void set_int(GConfClient *client, char *key, gint val)
74 {
75         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
76
77         gconf_client_set_int(client, tmp, val, NULL);
78         g_free(tmp);
79 }
80
81 gchar *get_str(GConfClient *client, char *key)
82 {
83         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
84         gchar *res;
85
86         res = gconf_client_get_string(client, tmp, NULL);
87         g_free(tmp);
88         return res;
89 }
90
91 void set_str(GConfClient *client, char *key, gchar *val)
92 {
93         char *tmp = g_strjoin("/", IM_CONF_DIR, key, NULL);
94
95         gconf_client_set_string(client, tmp, val, NULL);
96         g_free(tmp);
97 }
98
99 static GConfClient *init_conf(void)
100 {
101         GConfClient *client;
102
103         client = gconf_client_get_default();
104         if (!client)
105                 return NULL;
106         gconf_client_add_dir(client, IM_CONF_DIR, GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
107         internal_kbd = get_bool(client, "have-internal-keyboard");
108         return client;
109 }
110
111 static void deinit_conf(GConfClient *client)
112 {
113         g_object_unref(G_OBJECT(client));
114 }
115
116 osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
117 {
118         GConfClient *conf;
119         GtkDialog *dialog, *about;
120         GtkWidget *scroll, *widget, *vbox;
121         gchar *title;
122         void *plugin_data[PLUGINS];
123         unsigned i;
124         int res;
125
126         (void)osso; (void)user_activated;       /* shut up, GCC */
127
128         conf = init_conf();
129         if (!conf)
130                 return OSSO_ERROR;
131
132         for (i = 0; i < PLUGINS; i++) {
133                 inits[i](prefs + i);
134         }
135
136         dialog = GTK_DIALOG(gtk_dialog_new());
137         if (!dialog) {
138                 deinit_conf(conf);
139                 return OSSO_ERROR;
140         }
141         gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(data));
142
143         title = g_strdup_printf("%s (ukeyboard)", _("tein_ti_text_input_title"));
144         gtk_window_set_title(GTK_WINDOW(dialog), title);
145         g_free(title);
146
147
148         gtk_dialog_add_button(GTK_DIALOG(dialog), "About", GTK_RESPONSE_HELP);
149         gtk_dialog_add_button(GTK_DIALOG(dialog), _HL("wdgt_bd_save"), GTK_RESPONSE_ACCEPT);
150
151         scroll = hildon_pannable_area_new();
152         gtk_widget_set_size_request(GTK_WIDGET (scroll), -1, 345);
153         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), scroll);
154
155         vbox = gtk_vbox_new(FALSE, 0);
156         hildon_pannable_area_add_with_viewport (HILDON_PANNABLE_AREA(scroll), vbox);
157
158         gtk_widget_show_all(GTK_WIDGET(dialog));
159
160         for (i = 0; i < PLUGINS - 1; i++) {
161                 widget = prefs[i].start(conf, GTK_WIDGET(dialog), &plugin_data[i]);
162                 if (widget)
163                         gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, TRUE, 0);
164         }
165
166         while ((res = gtk_dialog_run(GTK_DIALOG(dialog))) == GTK_RESPONSE_HELP) {
167                 about = GTK_DIALOG(gtk_dialog_new());
168                 gtk_window_set_title(GTK_WINDOW(about), _HL("ecdg_ti_aboutdialog_title"));
169                 gtk_widget_set_size_request (GTK_WIDGET (about), -1, 300);
170
171                 widget = prefs[3].start(conf, GTK_WIDGET(about), &plugin_data[3]);
172                 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(about)->vbox), widget);
173
174                 gtk_widget_show_all(GTK_WIDGET(about));
175                 gtk_dialog_run(GTK_DIALOG(about));
176                 gtk_widget_destroy(GTK_WIDGET(about));
177
178         }
179         if (res == GTK_RESPONSE_ACCEPT) {
180                 for (i = 0; i < PLUGINS; i++)
181                         if (prefs[i].action)
182                                 prefs[i].action(conf, plugin_data[i]);
183         }
184         gtk_widget_destroy(GTK_WIDGET(dialog));
185
186         for (i = 0; i < PLUGINS; i++)
187                 if (prefs[i].stop)
188                         prefs[i].stop(conf, plugin_data[i]);
189
190         deinit_conf(conf);
191         return OSSO_OK;
192 }