2 * Copyright (c) 2008 Jiri Benc <jbenc@upir.cz>
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.
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.
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
18 #include <sys/types.h>
23 #include <glib/gstdio.h>
24 #include <gconf/gconf.h>
25 #include <gconf/gconf-client.h>
26 #include "../vkb_compiler.h"
27 #include "ukbdcreator.h"
29 static gchar
*saved_layout
= NULL
;
30 static gchar
*saved_lang
= NULL
;
31 static gint saved_current
= -1;
32 static gchar
*act_layout
= NULL
;
41 static void error(void *p
, int line
, char *msg
)
44 disp_compile_error(line
, msg
);
47 static int warning(void *p
, int line
, char *msg
)
50 disp_compile_error(line
, msg
);
54 static int get_line(void *p
, void *buf
, size_t size
)
56 struct priv
*priv
= p
;
60 while (priv
->buf
[priv
->pos
]) {
62 dbuf
[dpos
++] = priv
->buf
[priv
->pos
];
63 if (priv
->buf
[priv
->pos
++] == '\n')
67 return (priv
->buf
[priv
->pos
] || dpos
) ? 0 : -1;
70 static int write_buf(void *p
, void *buf
, size_t size
)
72 struct priv
*priv
= p
;
74 if (write(priv
->fout
, buf
, size
) != (ssize_t
)size
) {
75 disp_error("Error writing temporary file");
81 static off_t
tell_buf(void *p
)
83 struct priv
*priv
= p
;
85 return lseek(priv
->fout
, 0, SEEK_CUR
);
88 static void seek_buf(void *p
, off_t pos
)
90 struct priv
*priv
= p
;
92 lseek(priv
->fout
, pos
, SEEK_SET
);
95 static void return_lang(void *p
, char *lang
)
97 struct priv
*priv
= p
;
99 priv
->lang
= g_strdup(lang
);
102 static struct compiler_ops ops
= {
103 .get_line
= get_line
,
109 .return_lang
= return_lang
,
112 gboolean
compile_layout(gchar
*buf
, gchar
**fname
, gchar
**lang
)
121 *fname
= g_build_filename(g_get_tmp_dir(), "ukbdXXXXXX", NULL
);
122 priv
.fout
= g_mkstemp(*fname
);
125 disp_error("Error creating temporary file");
128 res
= compile(&ops
, &priv
);
140 static gchar
*get_lang(GConfClient
*conf
)
142 return gconf_client_get_string(conf
,
143 "/apps/osso/inputmethod/hildon-im-languages/language-0", NULL
);
146 static void set_lang(GConfClient
*conf
, gchar
*val
)
148 gconf_client_set_string(conf
,
149 "/apps/osso/inputmethod/hildon-im-languages/language-0", val
, NULL
);
152 static gint
get_current(GConfClient
*conf
)
154 return gconf_client_get_int(conf
,
155 "/apps/osso/inputmethod/hildon-im-languages/current", NULL
);
158 static void set_current(GConfClient
*conf
, gint val
)
160 gconf_client_set_int(conf
,
161 "/apps/osso/inputmethod/hildon-im-languages/current", val
, NULL
);
164 gboolean
test_layout(GConfClient
*conf
, gchar
*fname
, gchar
*lang
)
170 restore_layout(conf
, FALSE
);
171 if (inside_scratchbox
)
172 cmd
= g_strdup_printf("fakeroot /usr/libexec/ukeyboard-set -s %s %s", fname
, lang
);
174 cmd
= g_strdup_printf("sudo /usr/libexec/ukeyboard-set -s %s %s", fname
, lang
);
177 if (!WIFEXITED(res
)) {
178 disp_error("Cannot execute helper script");
181 res
= WEXITSTATUS(res
);
183 disp_error("Redefining a system language is not possible.\nPlease use diffent language code.");
187 disp_error("Activating of the layout failed");
190 saved_lang
= g_strdup(lang
);
191 saved_layout
= get_lang(conf
);
192 saved_current
= get_current(conf
);
193 set_lang(conf
, "en_GB");
194 set_lang(conf
, lang
);
195 set_current(conf
, 0);
197 g_unlink(act_layout
);
200 act_layout
= g_strdup(fname
);
201 disp_info("Layout activated");
205 gboolean
restore_layout(GConfClient
*conf
, gboolean warn
)
212 disp_info("No layout to restore");
215 if (inside_scratchbox
)
216 cmd
= g_strdup_printf("fakeroot /usr/libexec/ukeyboard-set -r %s", saved_lang
);
218 cmd
= g_strdup_printf("sudo /usr/libexec/ukeyboard-set -r %s", saved_lang
);
221 if (!WIFEXITED(res
)) {
222 disp_error("Cannot execute helper script");
225 res
= WEXITSTATUS(res
);
227 disp_error("Restoring of original layout failed");
230 set_lang(conf
, "en_GB");
231 set_lang(conf
, saved_layout
);
232 set_current(conf
, saved_current
);
234 g_free(saved_layout
);
235 saved_lang
= saved_layout
= NULL
;
238 g_unlink(act_layout
);
243 disp_info("Layout deactivated");