15c40d49bc3d9b9a7bdc1c7c9e8ab3fd9826e498
[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 <hildon/hildon.h>
25 #include <libosso.h>
26 #include <gconf/gconf.h>
27 #include <gconf/gconf-client.h>
28 #include "prefs.h"
29 #include "hw.h"
30
31 #define GETTEXT_PACKAGE "osso-applet-textinput"
32 #include <glib/gi18n-lib.h>
33
34 struct layout {
35         gchar *model;
36         gchar *layout;
37         gchar *name;
38 };
39
40 struct data {
41         GList *layouts;
42         HildonTouchSelector *combo;
43 };
44
45 static char *strip(char *s)
46 {
47         while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
48                 s++;
49         return s;
50 }
51
52 static GList *get_layouts(gchar *path, gchar *model, GList *list)
53 {
54         FILE *f;
55         char *buf, *s, *s2;
56         gchar *layout = NULL;
57         struct layout *lay;
58
59         f = fopen(path, "r");
60         if (!f)
61                 return list;
62         buf = g_malloc(512);
63         if (!buf) {
64                 fclose(f);
65                 return list;
66         }
67         while (fgets(buf, 512, f)) {
68                 s = strip(buf);
69                 if (!strncmp(s, "xkb_symbols", 11)) {
70                         if (layout) {
71                                 g_free(layout);
72                                 layout = NULL;
73                         }
74                         s = strip(s + 11);
75                         if (*s != '"')
76                                 continue;
77                         s++;
78                         s2 = strchr(s, '"');
79                         if (!s2)
80                                 continue;
81                         *s2 = '\0';
82                         layout = g_strdup(s);
83                 } else if (!strncmp(s, "name", 4) && layout) {
84                         s = strip(s + 4);
85                         if (*s != '[')
86                                 continue;
87                         s2 = strchr(s, ']');
88                         if (!s2)
89                                 continue;
90                         s = strip(s2 + 1);
91                         if (*s != '=')
92                                 continue;
93                         s = strip(s + 1);
94                         if (*s != '"')
95                                 continue;
96                         s++;
97                         s2 = strchr(s, '"');
98                         if (!s2)
99                                 continue;
100                         *s2 = '\0';
101                         lay = g_malloc(sizeof(struct layout));
102                         lay->model = g_strdup(model);
103                         lay->layout = layout;
104                         lay->name = g_strdup(s);
105                         layout = NULL;
106                         list = g_list_append(list, lay);
107                 }
108         }
109         fclose(f);
110         return list;
111 }
112
113 static void free_layouts(GList *list)
114 {
115         GList *item;
116         struct layout *lay;
117
118         for (item = list; item; item = g_list_next(item)) {
119                 lay = item->data;
120                 g_free(lay->model);
121                 g_free(lay->layout);
122                 g_free(lay->name);
123                 g_free(lay);
124         }
125         g_list_free(list);
126 }
127
128 static GtkWidget *start(GConfClient *client, GtkWidget *win, void **data)
129 {
130         struct data *d;
131         GList *item;
132         gchar *omodel, *olayout;
133         struct layout *lay;
134         unsigned i;
135
136         GtkWidget *button;
137
138         (void)win;
139
140         if (!internal_kbd) {
141                 *data = NULL;
142                 return NULL;
143         }
144
145         d = g_malloc(sizeof(struct data));
146
147         omodel = get_str(client, "int_kb_model");
148         olayout = get_str(client, "int_kb_layout");
149         d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/rx-51", "nokiarx51", NULL);
150         d->layouts = get_layouts("/usr/share/X11/xkb/symbols/nokia_vndr/ukeyboard", "ukeyboard", d->layouts);
151
152         d->combo = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
153
154         button = hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
155         hildon_button_set_title(HILDON_BUTTON(button), _("tein_fi_keyboard_layout"));
156         hildon_picker_button_set_selector(HILDON_PICKER_BUTTON (button), d->combo);
157         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
158         hildon_button_set_title_alignment(HILDON_BUTTON(button), 0.0, 0.5);
159         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
160
161         for (item = d->layouts, i = 0; item; item = g_list_next(item), i++) {
162                 lay = item->data;
163                 hildon_touch_selector_append_text(d->combo, lay->name);
164                 if (omodel && olayout && !strcmp(lay->model, omodel) && !strcmp(lay->layout, olayout))
165                         hildon_touch_selector_set_active(d->combo, 0, i);
166         }
167
168         g_free(olayout);
169         g_free(omodel);
170
171         *data = d;
172
173         gtk_widget_show(button);
174
175         return button;
176 }
177
178 static void action(GConfClient *client, void *data)
179 {
180         struct data *d = data;
181         struct layout *lay;
182         int res;
183
184         if (!d)
185                 return;
186         res = hildon_touch_selector_get_active(d->combo, 0);
187         if (res >= 0) {
188                 lay = g_list_nth_data(d->layouts, res);
189                 if (lay) {
190                         set_str(client, "int_kb_model", lay->model);
191                         set_str(client, "int_kb_layout", lay->layout);
192                 }
193         }
194 }
195
196 static void stop(GConfClient *client, void *data)
197 {
198         struct data *d = data;
199
200         (void)client;
201         if (d) {
202                 free_layouts(d->layouts);
203                 g_free(d);
204         }
205 }
206
207 void prefs_hw_init(struct prefs *prefs)
208 {
209         prefs->start = start;
210         prefs->action = action;
211         prefs->stop = stop;
212         prefs->name = "Hardware";
213 }