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