Remove vulnerable codes related to system() function
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / xim / preferences.c
1 /*
2  * Copyright (c) 2010 Mike Qin <mikeandmore@gmail.com>
3  *
4  * The contents of this file are subject to the terms of either the GNU Lesser
5  * General Public License Version 2.1 only ("LGPL") or the Common Development and
6  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7  * file except in compliance with the License. You can obtain a copy of the CDDL at
8  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
10  * specific language governing permissions and limitations under the License. When
11  * distributing the software, include this License Header Notice in each file and
12  * include the full text of the License in the License file as well as the
13  * following notice:
14  *
15  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16  * (CDDL)
17  * For Covered Software in this distribution, this License shall be governed by the
18  * laws of the State of California (excluding conflict-of-law provisions).
19  * Any litigation relating to this License shall be subject to the jurisdiction of
20  * the Federal Courts of the Northern District of California and the state courts
21  * of the State of California, with venue lying in Santa Clara County, California.
22  *
23  * Contributor(s):
24  *
25  * If you wish your version of this file to be governed by only the CDDL or only
26  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28  * license." If you don't indicate a single choice of license, a recipient has the
29  * option to distribute your version of this file under either the CDDL or the LGPL
30  * Version 2.1, or to extend the choice of license to its licensees as provided
31  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32  * Version 2 license, then the option applies only if the new code is made subject
33  * to such option by the copyright holder.
34  */
35
36 #include <gtk/gtk.h>
37
38 #include <X11/X.h>
39 #include <X11/Xproto.h>
40 #include <X11/Xlib.h>
41 #include <X11/keysym.h>
42 #include <X11/Xutil.h>
43
44 #include <unistd.h>
45 #include <dirent.h>
46 #include <sys/types.h>
47 #include <stdlib.h>
48
49 #include "settings.h"
50 #include "xmisc.h"
51
52 #define UI_FILE SUNPINYIN_XIM_SETTING_DIR"/settings_ui.xml"
53
54 static GtkWidget* main_wnd = NULL;
55 static GtkToggleButton* trigger_ctrl_check = NULL;
56 static GtkToggleButton* english_ctrl_check = NULL;
57 static GtkToggleButton* trigger_shift_check = NULL;
58 static GtkToggleButton* english_shift_check = NULL;
59 static GtkComboBox* trigger_combo = NULL;
60 static GtkComboBox* english_combo = NULL;
61 static GtkColorButton* background_color_btn = NULL;
62 static GtkFontButton* font_btn = NULL;
63 static GtkColorButton* font_color_btn = NULL;
64 static GtkAdjustment* opacity_value = NULL;
65 static GtkAdjustment* ncandidates = NULL;
66 static GtkToggleButton* minus_plus_check = NULL;
67 static GtkToggleButton* comma_period_check = NULL;
68 static GtkToggleButton* paren_check = NULL;
69 static GtkToggleButton* fuzzy_seg_check = NULL;
70 static GtkToggleButton* fuzzy_inner_seg_check = NULL;
71 static GtkToggleButton* cancel_on_backspace_check = NULL;
72 static GtkToggleButton* smart_punct_check = NULL;
73 static GtkToggleButton* shuangpin_check = NULL;
74 static GtkComboBox* shuangpin_combo = NULL;
75 static GtkComboBox* skin_combo = NULL;
76 static GtkToggleButton* hide_icbar_check = NULL;
77
78 #define RETRIEVE(name, macro)                                   \
79     name = macro(gtk_builder_get_object(builder, # name))
80
81 static const char* ui_shuangpin_schemes[] =
82 {
83     "MS2003", "ABC", "ZiRanMa", "PinYin++", "ZiGuang", "XiaoHe",
84 };
85
86 #define UI_SHUANGPIN_SCHEMES_NUM 6
87
88 static const int ui_keysym_model[] =
89 {
90     XK_space, XK_Control_L, XK_Control_R, XK_Shift_L, XK_Shift_R
91 };
92
93 #define UI_KEYSYM_MODEL_NUM 5
94
95 #define INIT_KEY_SETTING(prefix)                                        \
96     do {                                                                \
97         if (hk.modifiers & ControlMask) {                               \
98             gtk_toggle_button_set_active(prefix##_ctrl_check, TRUE);    \
99         }                                                               \
100         if (hk.modifiers & ShiftMask)                                   \
101             gtk_toggle_button_set_active(prefix##_shift_check, TRUE);   \
102         int i;                                                          \
103         for (i = 0; i < UI_KEYSYM_MODEL_NUM; i++) {                     \
104             if (hk.keysym == ui_keysym_model[i]) {                      \
105                 gtk_combo_box_set_active(prefix##_combo, i);            \
106                 break;                                                  \
107             }                                                           \
108         }                                                               \
109     } while(0)
110
111 #define INIT_COLOR_SETTING(widget_name)                         \
112     do {                                                        \
113         GdkColor color;                                         \
114         gdk_color_parse(colorstr, &color);                      \
115         gtk_color_button_set_color((widget_name), &color);      \
116     } while(0)
117
118 static int
119 fill_skin_list(const char* dirpath, varchar names[], int idx)
120 {
121     DIR* dir = opendir(dirpath);
122     if (!dir) {
123         mkdir(dirpath, 0644);
124         return idx;
125     }
126
127     struct dirent* ent = NULL;
128     while ((ent = readdir(dir))) {
129         if (ent->d_name[0] == '.') continue;
130         if (strcmp(ent->d_name, "classic") == 0) continue;
131
132         strncpy(names[idx], ent->d_name, sizeof(varchar));
133         idx++;
134     }
135     closedir(dir);
136     return idx;
137 }
138
139 #define SYSTEM_SKIN_DIR SUNPINYIN_XIM_SETTING_DIR"/skins"
140 #define USER_SKIN_DIR "%s/.sunpinyin/xim_skins"
141 #define MAX_SKINS 256
142
143 static int
144 list_skins(const char* current_skin_name)
145 {
146     int idx_ret = 0;
147     int skin_count = 0;
148     varchar skins[MAX_SKINS];
149
150     GtkListStore* model = GTK_LIST_STORE(gtk_combo_box_get_model(skin_combo));
151     GtkTreeIter iter;
152     gtk_list_store_append(model, &iter);
153     gtk_list_store_set(model, &iter, 0, "classic", -1);
154
155     skin_count = fill_skin_list(SYSTEM_SKIN_DIR, skins, skin_count);
156     varchar dirpath;
157     snprintf(dirpath, sizeof(varchar), USER_SKIN_DIR, getenv("HOME"));
158     skin_count = fill_skin_list(dirpath, skins, skin_count);
159
160     /* sort and unique the names */
161     qsort(skins, skin_count, sizeof(varchar),
162           (int (*)(const void*, const void*)) strcmp);
163
164     int i, j, idx = 1;
165     for (i = 0; i < skin_count; i++) {
166         for (j = i + 1; j < skin_count; j++) {
167             if (strcmp(skins[i], skins[j]) == 0) {
168                 i = j;
169             } else {
170                 break;
171             }
172         }
173         if (strcmp(skins[i], current_skin_name) == 0) {
174             idx_ret = idx;
175         }
176         gtk_list_store_append(model, &iter);
177         gtk_list_store_set(model, &iter, 0, skins[i], -1);
178         idx++;
179     }
180
181     return idx_ret;
182 }
183
184 static void
185 init_settings(void)
186 {
187     settings_init();
188     settings_load();
189
190     hotkey_t hk;
191     settings_get(TRIGGER_KEY, &hk);
192     INIT_KEY_SETTING(trigger);
193
194     settings_get(ENG_KEY, &hk);
195     INIT_KEY_SETTING(english);
196
197     varchar colorstr;
198     settings_get(PREEDIT_COLOR, colorstr);
199     INIT_COLOR_SETTING(background_color_btn);
200
201     settings_get(PREEDIT_FONT_COLOR, colorstr);
202     INIT_COLOR_SETTING(font_color_btn);
203
204     varchar fontstr;
205     settings_get(PREEDIT_FONT, fontstr);
206     gtk_font_button_set_font_name(font_btn, fontstr);
207
208
209     gtk_adjustment_set_value(opacity_value,
210                              settings_get_double(PREEDIT_OPACITY));
211
212     gtk_adjustment_set_value(ncandidates, settings_get_int(CANDIDATES_SIZE));
213
214     gtk_toggle_button_set_active(minus_plus_check,
215                                  settings_get_int(PAGE_MINUS_PLUS));
216     gtk_toggle_button_set_active(comma_period_check,
217                                  settings_get_int(PAGE_COMMA_PERIOD));
218     gtk_toggle_button_set_active(paren_check,
219                                  settings_get_int(PAGE_PAREN));
220
221     gtk_toggle_button_set_active(fuzzy_seg_check,
222                                  settings_get_int(FUZZY_SEGMENTATION));
223     gtk_toggle_button_set_active(fuzzy_inner_seg_check,
224                                  settings_get_int(FUZZY_INNER_SEGMENTATION));
225
226     gtk_toggle_button_set_active(cancel_on_backspace_check,
227                                  settings_get_int(CANCEL_ON_BACKSPACE));
228
229     gtk_toggle_button_set_active(smart_punct_check,
230                                  settings_get_int(SMART_PUNCT));
231
232     gtk_toggle_button_set_active(shuangpin_check,
233                                  settings_get_int(SHUANGPIN));
234     varchar scheme;
235     int i;
236     settings_get(SHUANGPIN_SCHEME, scheme);
237     for (i = 0; i < UI_SHUANGPIN_SCHEMES_NUM; i++) {
238         if (strcmp(ui_shuangpin_schemes[i], scheme) == 0) {
239             gtk_combo_box_set_active(shuangpin_combo, i);
240             break;
241         }
242     }
243
244     /* skin */
245     varchar skin_name;
246     settings_get(SKIN_NAME, skin_name);
247     int idx = list_skins(skin_name);
248     gtk_combo_box_set_active(skin_combo, idx);
249
250     gtk_toggle_button_set_active(hide_icbar_check,
251                                  settings_get_int(HIDE_ICBAR));
252 }
253
254 static void
255 init(void)
256 {
257     GtkBuilder* builder = gtk_builder_new();
258     gtk_builder_add_from_file(builder, UI_FILE, NULL);
259     main_wnd = GTK_WIDGET(gtk_builder_get_object(builder, "settings_dialog"));
260
261     RETRIEVE(trigger_ctrl_check, GTK_TOGGLE_BUTTON);
262     RETRIEVE(english_ctrl_check, GTK_TOGGLE_BUTTON);
263     RETRIEVE(trigger_shift_check, GTK_TOGGLE_BUTTON);
264     RETRIEVE(english_shift_check, GTK_TOGGLE_BUTTON);
265     RETRIEVE(trigger_combo, GTK_COMBO_BOX);
266     RETRIEVE(english_combo, GTK_COMBO_BOX);
267     RETRIEVE(background_color_btn, GTK_COLOR_BUTTON);
268     RETRIEVE(font_btn, GTK_FONT_BUTTON);
269     RETRIEVE(font_color_btn, GTK_COLOR_BUTTON);
270     RETRIEVE(opacity_value, GTK_ADJUSTMENT);
271     RETRIEVE(ncandidates, GTK_ADJUSTMENT);
272     RETRIEVE(minus_plus_check, GTK_TOGGLE_BUTTON);
273     RETRIEVE(comma_period_check, GTK_TOGGLE_BUTTON);
274     RETRIEVE(paren_check, GTK_TOGGLE_BUTTON);
275     RETRIEVE(fuzzy_seg_check, GTK_TOGGLE_BUTTON);
276     RETRIEVE(fuzzy_inner_seg_check, GTK_TOGGLE_BUTTON);
277     RETRIEVE(cancel_on_backspace_check, GTK_TOGGLE_BUTTON);
278     RETRIEVE(smart_punct_check, GTK_TOGGLE_BUTTON);
279     RETRIEVE(shuangpin_check, GTK_TOGGLE_BUTTON);
280     RETRIEVE(shuangpin_combo, GTK_COMBO_BOX);
281     RETRIEVE(skin_combo, GTK_COMBO_BOX);
282     RETRIEVE(hide_icbar_check, GTK_TOGGLE_BUTTON);
283
284     init_settings();
285
286     gtk_builder_connect_signals(builder, NULL);
287
288     g_object_unref(builder);
289 }
290
291 #define REFRESH_KEY_SETTING(prefix)                                     \
292     do {                                                                \
293         hk.modifiers = hk.keysym = 0;                                   \
294         if (gtk_toggle_button_get_active(prefix##_ctrl_check)) {        \
295             hk.modifiers |= ControlMask;                                \
296         }                                                               \
297         if (gtk_toggle_button_get_active(prefix##_shift_check)) {       \
298             hk.modifiers |= ShiftMask;                                  \
299         }                                                               \
300         int idx = gtk_combo_box_get_active(prefix##_combo);             \
301         if (idx >= 0)                                                   \
302             hk.keysym = ui_keysym_model[idx];                           \
303     } while (0)
304
305 #define REFRESH_COLOR_SETTING(widget_name)                              \
306     do {                                                                \
307         GdkColor color;                                                 \
308         gtk_color_button_get_color((widget_name), &color);              \
309         snprintf(colorstr, sizeof(varchar), "#%.2X%.2X%.2X",            \
310                  color.red >> 8, color.green >> 8, color.blue >> 8);    \
311     } while(0)
312
313 static void
314 send_reload()
315 {
316     /* notify all running xsunpinyin with this user */
317     pid_t child_pid = fork();
318     if (child_pid == 0)
319     {
320         char cmd[256] = {0};
321         snprintf(cmd, 256, "%d", getuid());
322         execl("/usr/bin/pkill", "/usr/bin/pkill", "-10", "^xsunpinyin$", "-u", cmd, (char*)0);
323         exit(0);
324     }
325 }
326
327 void
328 state_changed()
329 {
330     hotkey_t hk;
331     REFRESH_KEY_SETTING(trigger);
332     settings_set(TRIGGER_KEY, &hk);
333
334     REFRESH_KEY_SETTING(english);
335     settings_set(ENG_KEY, &hk);
336
337     varchar colorstr;
338     REFRESH_COLOR_SETTING(background_color_btn);
339     settings_set(PREEDIT_COLOR, colorstr);
340
341     REFRESH_COLOR_SETTING(font_color_btn);
342     settings_set(PREEDIT_FONT_COLOR, colorstr);
343
344     /* font and size information */
345     settings_set(PREEDIT_FONT, (void*) gtk_font_button_get_font_name(font_btn));
346
347     /* font color information */
348     settings_set_double(PREEDIT_OPACITY,
349                         gtk_adjustment_get_value(opacity_value));
350
351     settings_set_int(CANDIDATES_SIZE,
352                      gtk_adjustment_get_value(ncandidates));
353
354     /* page up and down trigger */
355     settings_set_int(PAGE_MINUS_PLUS,
356                      gtk_toggle_button_get_active(minus_plus_check));
357     settings_set_int(PAGE_COMMA_PERIOD,
358                      gtk_toggle_button_get_active(comma_period_check));
359     settings_set_int(PAGE_PAREN,
360                      gtk_toggle_button_get_active(paren_check));
361
362     /* fuzzy segmentation */
363     settings_set_int(FUZZY_SEGMENTATION,
364                      gtk_toggle_button_get_active(fuzzy_seg_check));
365     settings_set_int(FUZZY_INNER_SEGMENTATION,
366                      gtk_toggle_button_get_active(fuzzy_inner_seg_check));
367
368     /* cancel on backspace */
369     settings_set_int(CANCEL_ON_BACKSPACE,
370                      gtk_toggle_button_get_active(cancel_on_backspace_check));
371
372     /* smart punctuation */
373     settings_set_int(SMART_PUNCT,
374                      gtk_toggle_button_get_active(smart_punct_check));
375
376     settings_set_int(SHUANGPIN, gtk_toggle_button_get_active(shuangpin_check));
377     int sche_idx = gtk_combo_box_get_active(shuangpin_combo);
378     if (sche_idx < UI_SHUANGPIN_SCHEMES_NUM)
379         settings_set_string(SHUANGPIN_SCHEME, ui_shuangpin_schemes[sche_idx]);
380
381     /* skins */
382     settings_set_string(SKIN_NAME, gtk_combo_box_get_active_text(skin_combo));
383
384     /* whether hide icbar */
385     settings_set_int(HIDE_ICBAR, gtk_toggle_button_get_active(hide_icbar_check));
386
387     settings_save();
388     send_reload();
389 }
390
391 int main(int argc, char *argv[])
392 {
393     init_display(&argc, &argv);
394     init();
395
396     gtk_widget_show(main_wnd);
397
398     gtk_main();
399     return 0;
400 }