updated ukeyboard port for fremantle
[slovak-l10n] / ukeyboard / cpanel / langset.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 <hildon/hildon-caption.h>
24 #include <libosso.h>
25 #include <gconf/gconf.h>
26 #include <gconf/gconf-client.h>
27
28 #ifdef HAVE_MAEMO5
29 #define GETTEXT_PACKAGE "osso-applet-textinput"
30
31 #include <hildon/hildon.h>
32 #include <glib/gi18n-lib.h>
33 #endif
34
35 #include "prefs.h"
36 #include "lang.h"
37 #include "langset.h"
38
39 #define L_CONF_DIR      "/apps/osso/inputmethod/hildon-im-languages"
40
41 #ifdef HAVE_MAEMO5
42 gboolean get_l_bool(GConfClient *client, char *lang, char *key)
43 #else
44 static gboolean get_l_bool(GConfClient *client, char *lang, char *key)
45 #endif
46 {
47         char *tmp = g_strjoin("/", L_CONF_DIR, lang, 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 #ifdef HAVE_MAEMO5
56 void set_l_bool(GConfClient *client, char *lang, char *key, gboolean val)
57 #else
58 static void set_l_bool(GConfClient *client, char *lang, char *key, gboolean val)
59 #endif
60 {
61         char *tmp = g_strjoin("/", L_CONF_DIR, lang, key, NULL);
62
63         g_debug("%s %d\n", tmp, val);
64
65         gconf_client_set_bool(client, tmp, val, NULL);
66         g_free(tmp);
67 }
68
69 #ifdef HAVE_MAEMO5
70 gchar *get_l_str(GConfClient *client, char *lang, char *key)
71 #else
72 static gchar *get_l_str(GConfClient *client, char *lang, char *key)
73 #endif
74 {
75         char *tmp = g_strjoin("/", L_CONF_DIR, lang, key, NULL);
76         GConfValue *val;
77         gchar *res;
78
79         val = gconf_client_get_without_default(client, tmp, NULL);
80         g_free(tmp);
81         if (!val)
82                 return NULL;
83         if (val->type != GCONF_VALUE_STRING) {
84                 gconf_value_free(val);
85                 return NULL;
86         }
87         res = g_strdup(gconf_value_get_string(val));
88         gconf_value_free(val);
89         return res;
90 }
91
92 #ifdef HAVE_MAEMO5
93 void set_l_str(GConfClient *client, char *lang, char *key, gchar *val)
94 #else
95 static void set_l_str(GConfClient *client, char *lang, char *key, gchar *val)
96 #endif
97 {
98         char *tmp = g_strjoin("/", L_CONF_DIR, lang, key, NULL);
99
100         gconf_client_set_string(client, tmp, val, NULL);
101         g_free(tmp);
102 }
103
104 #ifdef HAVE_MAEMO5
105 void fill_dict(HildonTouchSelector *combo, GList *langs, gchar *deflang)
106 {
107         GList *item;
108         struct lang *lang;
109         unsigned i;
110
111         for (item = langs, i = 0; item; item = g_list_next(item)) {
112                 lang = item->data;
113                 if (lang->ext)
114                         continue;
115                 hildon_touch_selector_append_text(combo, lang->desc);
116                 if (deflang && !strcmp(lang->code, deflang))
117                         hildon_touch_selector_set_active(combo, 0, i);
118                 i++;
119         }
120         hildon_touch_selector_append_text(combo, _("tein_fi_word_completion_language_empty"));
121         if (!deflang || !*deflang)
122                 hildon_touch_selector_set_active(combo, 0, i);
123 }
124 #else
125 static void fill_dict(GtkComboBox *combo, GList *langs, gchar *deflang)
126 {
127         GList *item;
128         struct lang *lang;
129         unsigned i;
130
131         for (item = langs, i = 0; item; item = g_list_next(item)) {
132                 lang = item->data;
133                 if (lang->ext)
134                         continue;
135                 gtk_combo_box_append_text(combo, lang->desc);
136                 if (deflang && !strcmp(lang->code, deflang))
137                         gtk_combo_box_set_active(combo, i);
138                 i++;
139         }
140         gtk_combo_box_append_text(combo, "Custom/Empty");
141         if (!deflang || !*deflang)
142                 gtk_combo_box_set_active(combo, i);
143 }
144
145 void lang_settings(GConfClient *client, GtkWidget *win, GList *langs, int n)
146 {
147         GtkDialog *dialog;
148         GtkBox *vbox;
149         GtkSizeGroup *group;
150         GtkToggleButton *auto_cap, *word_compl, *word_pred, *sp_after;
151         GtkComboBox *dict;
152         struct lang *lang;
153         gchar *code, *tmp;
154         int res;
155
156         if (n < 0)
157                 return;
158         lang = g_list_nth_data(langs, n);
159         if (!lang)
160                 return;
161         code = lang->code;
162
163         dialog = GTK_DIALOG(gtk_dialog_new());
164         gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(win));
165         tmp = g_strconcat("On-screen text input: ", lang->desc, NULL);
166         gtk_window_set_title(GTK_WINDOW(dialog), tmp);
167         g_free(tmp);
168         gtk_dialog_set_has_separator(dialog, FALSE);
169         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
170
171         gtk_dialog_add_action_widget(dialog, gtk_button_new_with_label("OK"), GTK_RESPONSE_ACCEPT);
172         gtk_dialog_add_action_widget(dialog, gtk_button_new_with_label("Cancel"), GTK_RESPONSE_REJECT);
173
174         vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
175         group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
176
177         auto_cap = GTK_TOGGLE_BUTTON(gtk_check_button_new());
178         gtk_toggle_button_set_active(auto_cap, get_l_bool(client, code, "auto-capitalisation"));
179         gtk_box_pack_start_defaults(vbox, hildon_caption_new(group, "Auto-capitalisation",
180                 GTK_WIDGET(auto_cap), NULL, HILDON_CAPTION_MANDATORY));
181
182         word_compl = GTK_TOGGLE_BUTTON(gtk_check_button_new());
183         gtk_toggle_button_set_active(word_compl, get_l_bool(client, code, "word-completion"));
184         gtk_box_pack_start_defaults(vbox, hildon_caption_new(group, "Word completion",
185                 GTK_WIDGET(word_compl), NULL, HILDON_CAPTION_MANDATORY));
186
187         word_pred = GTK_TOGGLE_BUTTON(gtk_check_button_new());
188         gtk_toggle_button_set_active(word_pred, get_l_bool(client, code, "next-word-prediction"));
189         gtk_box_pack_start_defaults(vbox, hildon_caption_new(group, "Next word prediction",
190                 GTK_WIDGET(word_pred), NULL, HILDON_CAPTION_MANDATORY));
191
192         sp_after = GTK_TOGGLE_BUTTON(gtk_check_button_new());
193         gtk_toggle_button_set_active(sp_after, get_l_bool(client, code, "insert-space-after-word"));
194         gtk_box_pack_start_defaults(vbox, hildon_caption_new(group, "Insert space after word",
195                 GTK_WIDGET(sp_after), NULL, HILDON_CAPTION_MANDATORY));
196
197         dict = GTK_COMBO_BOX(gtk_combo_box_new_text());
198         tmp = get_l_str(client, code, "dictionary");
199         /* If tmp is NULL (i.e. the gconf key is unset), try to use the same
200          * dictionary as the keyboard. But don't do this if the keyboard is
201          * from our package. */
202         fill_dict(dict, langs, (tmp || lang->ext) ? tmp : code);
203         if (tmp)
204                 g_free(tmp);
205         gtk_box_pack_start_defaults(vbox, hildon_caption_new(group, "Dictionary",
206                 GTK_WIDGET(dict), NULL, HILDON_CAPTION_MANDATORY));
207
208         g_object_unref(G_OBJECT(group));
209
210         gtk_container_add(GTK_CONTAINER(dialog->vbox), GTK_WIDGET(vbox));
211         gtk_widget_show_all(GTK_WIDGET(dialog));
212         res = gtk_dialog_run(dialog);
213         if (res == GTK_RESPONSE_ACCEPT) {
214                 set_l_bool(client, code, "auto-capitalisation", gtk_toggle_button_get_active(auto_cap));
215                 set_l_bool(client, code, "word-completion", gtk_toggle_button_get_active(word_compl));
216                 set_l_bool(client, code, "next-word-prediction", gtk_toggle_button_get_active(word_pred));
217                 set_l_bool(client, code, "insert-space-after-word", gtk_toggle_button_get_active(sp_after));
218                 res = gtk_combo_box_get_active(dict);
219                 if (res >= 0) {
220                         lang = g_list_nth_data(langs, res);
221                         tmp = (lang && !lang->ext) ? lang->code : "";
222                         set_l_str(client, code, "dictionary", tmp);
223                 }
224         }
225         gtk_widget_destroy(GTK_WIDGET(dialog));
226 }
227 #endif