updated ukeyboard port for fremantle
[slovak-l10n] / ukeyboard / cpanel / hw.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 #include "prefs.h"
28 #include "hw.h"
29
30 #ifdef HAVE_MAEMO5
31 #define GETTEXT_PACKAGE "osso-applet-textinput"
32
33 #include <hildon/hildon.h>
34 #include <glib/gi18n-lib.h>
35 #endif
36
37 struct layout {
38         gchar *model;
39         gchar *layout;
40         gchar *name;
41 };
42
43 struct data {
44         GList *layouts;
45 #ifdef HAVE_MAEMO5
46         HildonTouchSelector *combo;
47 #else
48         GtkComboBox *combo;
49 #endif
50 };
51
52 static char *strip(char *s)
53 {
54         while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
55                 s++;
56         return s;
57 }
58
59 static GList *get_layouts(gchar *path, gchar *model, GList *list)
60 {
61         FILE *f;
62         char *buf, *s, *s2;
63         gchar *layout = NULL;
64         struct layout *lay;
65
66         f = fopen(path, "r");
67         if (!f)
68                 return list;
69         buf = g_malloc(512);
70         if (!buf) {
71                 fclose(f);
72                 return list;
73         }
74         while (fgets(buf, 512, f)) {
75                 s = strip(buf);
76                 if (!strncmp(s, "xkb_symbols", 11)) {
77                         if (layout) {
78                                 g_free(layout);
79                                 layout = NULL;
80                         }
81                         s = strip(s + 11);
82                         if (*s != '"')
83                                 continue;
84                         s++;
85                         s2 = strchr(s, '"');
86                         if (!s2)
87                                 continue;
88                         *s2 = '\0';
89                         layout = g_strdup(s);
90                 } else if (!strncmp(s, "name", 4) && layout) {
91                         s = strip(s + 4);
92                         if (*s != '[')
93                                 continue;
94                         s2 = strchr(s, ']');
95                         if (!s2)
96                                 continue;
97                         s = strip(s2 + 1);
98                         if (*s != '=')
99                                 continue;
100                         s = strip(s + 1);
101                         if (*s != '"')
102                                 continue;
103                         s++;
104                         s2 = strchr(s, '"');
105                         if (!s2)
106                                 continue;
107                         *s2 = '\0';
108                         lay = g_malloc(sizeof(struct layout));
109                         lay->model = g_strdup(model);
110                         lay->layout = layout;
111                         lay->name = g_strdup(s);
112                         layout = NULL;
113                         list = g_list_append(list, lay);
114                 }
115         }
116         fclose(f);
117         return list;
118 }
119
120 static void free_layouts(GList *list)
121 {
122         GList *item;
123         struct layout *lay;
124
125         for (item = list; item; item = g_list_next(item)) {
126                 lay = item->data;
127                 g_free(lay->model);
128                 g_free(lay->layout);
129                 g_free(lay->name);
130                 g_free(lay);
131         }
132         g_list_free(list);
133 }
134
135 #ifdef HAVE_MAEMO5
136 static GtkWidget *start(GConfClient *client, GtkWidget *win, void **data)
137 {
138         struct data *d;
139         GList *item;
140         gchar *omodel, *olayout;
141         struct layout *lay;
142         unsigned i;
143
144         GtkWidget *button;
145
146         (void)win;
147
148         if (!internal_kbd) {
149                 *data = NULL;
150                 return NULL;
151         }
152
153         d = g_malloc(sizeof(struct data));
154
155         omodel = get_str(client, "int_kb_model");
156         olayout = get_str(client, "int_kb_layout");
157         d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/rx-51", "nokiarx51", NULL);
158         d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/ukbdrx51", "ukbdrx51", d->layouts);
159
160         d->combo = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
161
162         button = hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
163         hildon_button_set_title(HILDON_BUTTON(button), _("tein_fi_keyboard_layout"));
164         hildon_picker_button_set_selector(HILDON_PICKER_BUTTON (button), d->combo);
165         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
166         hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.0, 0.5);
167         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
168
169         for (item = d->layouts, i = 0; item; item = g_list_next(item), i++) {
170                 lay = item->data;
171                 hildon_touch_selector_append_text(d->combo, lay->name);
172                 if (omodel && olayout && !strcmp(lay->model, omodel) && !strcmp(lay->layout, olayout))
173                         hildon_touch_selector_set_active(d->combo, 0, i);
174         }
175
176         g_free(olayout);
177         g_free(omodel);
178
179         *data = d;
180
181         gtk_widget_show(button);
182
183         return button;
184 }
185 #else
186 static GtkWidget *start(GConfClient *client, GtkWidget *win, void **data)
187 {
188         struct data *d;
189         GList *item;
190         gchar *omodel, *olayout;
191         struct layout *lay;
192         unsigned i;
193
194         GtkBox *vbox;
195         GtkSizeGroup *group;
196         GtkWidget *align;
197
198         (void)win;
199
200         if (!internal_kbd) {
201                 *data = NULL;
202                 return NULL;
203         }
204
205         d = g_malloc(sizeof(struct data));
206
207         omodel = get_str(client, "int_kb_model");
208         olayout = get_str(client, "int_kb_layout");
209         d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/rx-44", "nokiarx44", NULL);
210         d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/ukbdrx44", "ukbdrx44", d->layouts);
211
212         vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
213         group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
214
215         d->combo = GTK_COMBO_BOX(gtk_combo_box_new_text());
216         for (item = d->layouts, i = 0; item; item = g_list_next(item), i++) {
217                 lay = item->data;
218                 gtk_combo_box_append_text(d->combo, lay->name);
219                 if (omodel && olayout && !strcmp(lay->model, omodel) && !strcmp(lay->layout, olayout))
220                         gtk_combo_box_set_active(d->combo, i);
221         }
222         gtk_box_pack_start_defaults(vbox, hildon_caption_new(group, "Keyboard layout",
223                 GTK_WIDGET(d->combo), NULL, HILDON_CAPTION_MANDATORY));
224
225         g_free(olayout);
226         g_free(omodel);
227         g_object_unref(G_OBJECT(group));
228
229         *data = d;
230
231         align = gtk_alignment_new(0, 0, 1, 0);
232         gtk_container_add(GTK_CONTAINER(align), GTK_WIDGET(vbox));
233         return align;
234 }
235 #endif
236
237 static void action(GConfClient *client, void *data)
238 {
239         struct data *d = data;
240         struct layout *lay;
241         int res;
242
243         if (!d)
244                 return;
245 #ifdef HAVE_MAEMO5
246         res = hildon_touch_selector_get_active(d->combo, 0);
247 #else
248         res = gtk_combo_box_get_active(d->combo);
249 #endif
250         if (res >= 0) {
251                 lay = g_list_nth_data(d->layouts, res);
252                 if (lay) {
253                         set_str(client, "int_kb_model", lay->model);
254                         set_str(client, "int_kb_layout", lay->layout);
255                 }
256         }
257 }
258
259 static void stop(GConfClient *client, void *data)
260 {
261         struct data *d = data;
262
263         (void)client;
264         if (d) {
265                 free_layouts(d->layouts);
266                 g_free(d);
267         }
268 }
269
270 void prefs_hw_init(struct prefs *prefs)
271 {
272         prefs->start = start;
273         prefs->action = action;
274         prefs->stop = stop;
275         prefs->name = "Hardware";
276 }