afcb9f06385859ce1a034c4f47fe5ddc4c4e0ad2
[profile/ivi/isf.git] / ism / extras / efl_setting / isf_setting_wizard.cpp
1 /*
2  * ISF(Input Service Framework)
3  *
4  * ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable.
5  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6  *
7  * Contact: Shuo Liu <shuo0805.liu@samsung.com>, Hengliang Luo <hl.luo@samsung.com>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the
11  * Free Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
15  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation, Inc., 51
21  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24
25 #ifndef UG_WIZARD_MODULE_API
26 #define UG_WIZARD_MODULE_API __attribute__ ((visibility("default")))
27 #endif
28
29 #define Uses_SCIM_CONFIG_PATH
30 #define Uses_SCIM_HELPER_MODULE
31
32 #include <stdio.h>
33 #include <Elementary.h>
34 #include <Ecore_IMF.h>
35 #include "isf_control.h"
36 #include <Ecore_X.h>
37 #include <glib.h>
38 #include <glib-object.h>
39 #include <vconf-keys.h>
40 #include <vconf.h>
41 #include <ui-gadget-module.h>
42 #include <ui-gadget.h>
43 #include <dlog.h>
44 #include "scim.h"
45 #include "scim_stl_map.h"
46 #include "isf_setting_wizard.h"
47 #include "../efl_panel/isf_panel_utility.h"
48
49
50 using namespace scim;
51
52
53 #define WIZARD_PACKAGE                             "keyboard-setting-wizard-efl"
54 #define WIZARD_LOCALEDIR                           "/opt/ug/res/locale"
55 #define T_(s)                                      dgettext(WIZARD_PACKAGE, s)
56 #define LOG_TAG                                    "isfsettingwizard"
57
58 static Elm_Genlist_Item_Class itc1,itcSeparator;
59 static int mark = 0;
60
61
62 static Ecore_IMF_Context *imf_context = NULL;
63
64 static Evas_Object *sw_radio_grp   = NULL;      //sw view raido group
65 static std::vector<String> sw_iselist;
66
67 static ConfigPointer _config;
68
69 static char ise_bak[256] = {'\0'};
70 static char _active_ise_name[256] = {'\0'};
71
72
73 extern std::vector <String>  _names;
74 extern std::vector <String>  _uuids;
75 extern std::vector <String>  _module_names;
76 extern std::vector <String>  _langs;
77
78 static String uuid_to_name(String uuid)
79 {
80     String tmpName("");
81     for(unsigned int i = 0;i<_uuids.size();i++)
82     {
83         if (strcmp(uuid.c_str(),_uuids[i].c_str())== 0) {
84             tmpName = _names[i];
85             break;
86         }
87     }
88     return tmpName;
89 }
90
91 static Evas_Object *_create_bg(Evas_Object *win)
92 {
93     Evas_Object *bg = elm_bg_add(win);
94     evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
95     evas_object_show(bg);
96     return bg;
97 }
98
99 static Evas_Object *create_fullview (Evas_Object *parent, struct ug_data *ugd)
100 {
101     Evas_Object *bg = _create_bg(parent);
102     Evas_Object *layout_main = NULL;
103
104     layout_main = elm_layout_add (parent);
105
106     if (layout_main == NULL)
107         return NULL;
108
109     elm_layout_theme_set (layout_main, "layout","application","default");
110     elm_object_style_set(bg, "group_list");
111     elm_object_part_content_set (layout_main, "elm.swallow.bg", bg);
112
113     return layout_main;
114 }
115
116 static Evas_Object *create_frameview (Evas_Object *parent, struct ug_data *ugd)
117 {
118     Evas_Object *bg = _create_bg(parent);
119     Evas_Object *layout_main = elm_layout_add (parent);
120     if (layout_main == NULL)
121         return NULL;
122     elm_layout_theme_set (layout_main, "layout", "application", "default");
123     elm_object_style_set(bg, "group_list");
124     elm_object_part_content_set (layout_main, "elm.swallow.bg", bg);
125
126     return layout_main;
127 }
128
129 static void back_cb (void *data, Evas_Object *obj, void *event_info)
130 {
131     if (data == NULL)
132         return;
133
134     struct ug_data *ugd = (struct ug_data *)data;
135     ug_destroy_me (ugd->ug);
136 }
137
138 static Evas_Object* _create_naviframe_layout (Evas_Object* parent)
139 {
140     Evas_Object *naviframe = elm_naviframe_add (parent);
141     elm_object_part_content_set (parent, "elm.swallow.content", naviframe);
142     evas_object_show (naviframe);
143
144     return naviframe;
145 }
146
147 static bool in_exit = false;
148 static void sw_keyboard_selection_view_back_cb (void *data, Evas_Object *obj, void *event_info)
149 {
150     if (in_exit) {
151         LOGD("do nothing ,exit!\n");
152         return;
153     }
154     in_exit = true;
155
156     if (data == NULL)
157         return;
158
159     struct ug_data *ugd = NULL;
160     ugd = (ug_data *)data;
161
162     if (strcmp (ise_bak, _active_ise_name) != 0) {
163     //if _active_ise_name is changed , active _active_ise_name.
164     //find the uuid of the active
165         String uuid;
166         for (unsigned int i = 0; i < _names.size (); i++) {
167             if (strcmp (_names[i].c_str (), _active_ise_name) == 0)
168                 uuid = _uuids[i];
169         }
170         isf_control_set_active_ise_by_uuid ( uuid.c_str ());
171
172         snprintf (ise_bak, sizeof (ise_bak), "%s", _active_ise_name);
173     }
174
175     bundle *b = NULL;
176     b = bundle_create();
177     bundle_add(b, "name", "keyboard-setting-wizard-efl");
178     bundle_add(b, "description", "previous clicked");
179     ug_send_result(ugd->ug, b);
180     bundle_free(b);
181
182     back_cb(data,obj,event_info);
183
184     LOGD("End of %s", __func__);
185 }
186
187 static void sw_keyboard_selection_view_set_cb (void *data, Evas_Object *obj, void *event_info)
188 {
189     if (in_exit)
190         return;
191     in_exit = true;
192
193     if (data == NULL)
194         return;
195
196     struct ug_data *ugd = (struct ug_data *)data;
197
198     if (strcmp (ise_bak, _active_ise_name) != 0) {
199         //if _active_ise_name is changed , active _active_ise_name.
200         //find the uuid of the active
201         String uuid;
202         for (unsigned int i = 0; i < _names.size (); i++) {
203             if (strcmp (_names[i].c_str (), _active_ise_name) == 0)
204                 uuid = _uuids[i];
205         }
206         isf_control_set_active_ise_by_uuid ( uuid.c_str ());
207
208         snprintf (ise_bak, sizeof (ise_bak), "%s", _active_ise_name);
209         LOGD("Set active ISE : %s", ise_bak);
210     }
211
212     bundle *b = NULL;
213     b = bundle_create();
214     bundle_add(b, "name", "keyboard-setting-wizard-efl");
215     bundle_add(b, "description", "next clicked");
216     ug_send_result(ugd->ug, b);
217     bundle_free(b);
218
219     back_cb(data,obj,event_info);
220
221     LOGD("End of %s", __func__);
222 //   call next ug
223 }
224
225 static void _gl_sel(void *data, Evas_Object *obj, void *event_info)
226 {
227     Elm_Object_Item *item = (Elm_Object_Item *)event_info;
228     elm_genlist_item_selected_set(item, 0);
229     mark = (int) (data);
230     snprintf (_active_ise_name, sizeof (_active_ise_name), "%s", sw_iselist[mark].c_str ());
231     elm_genlist_item_update(item);
232     return;
233 }
234
235 static void _sw_radio_cb (void *data, Evas_Object *obj, void *event_info)
236 {
237     int index = GPOINTER_TO_INT(data);
238     elm_radio_value_set (sw_radio_grp, index);
239     snprintf (_active_ise_name, sizeof (_active_ise_name), "%s", sw_iselist[index].c_str ());
240 }
241
242 static char *_gl_label_get(void *data, Evas_Object *obj, const char *part)
243 {
244     int index = (int)(data);
245     if (!strcmp(part, "elm.text")) {
246         return strdup(sw_iselist[index].c_str());
247     }
248     return NULL;
249 }
250
251 static Evas_Object *_gl_icon_get(void *data, Evas_Object *obj, const char *part)
252 {
253     if (!strcmp(part, "elm.icon")) {
254
255         int index = (int)(data);
256         Evas_Object *radio  = elm_radio_add (obj);
257         elm_radio_state_value_set (radio, index);
258         if (sw_radio_grp == NULL)
259             sw_radio_grp = elm_radio_add(obj);
260         elm_radio_group_add (radio, sw_radio_grp);
261         evas_object_show (radio);
262         evas_object_smart_callback_add (radio, "changed", _sw_radio_cb, (void *) (index));
263         if (mark == index) {
264             elm_radio_value_set (sw_radio_grp, mark);
265         }
266         return radio;
267     }
268     return NULL;
269 }
270
271 static Eina_Bool _gl_state_get(void *data, Evas_Object *obj, const char *part)
272 {
273     return EINA_FALSE;
274 }
275
276 static void _gl_del(void *data, Evas_Object *obj)
277 {
278     return;
279 }
280
281 static Evas_Object *isf_setting_main_view_tizen(ug_data * ugd)
282 {
283    String tmpStr = _config->read(SCIM_CONFIG_DEFAULT_HELPER_ISE,String("b70bf6cc-ff77-47dc-a137-60acc32d1e0c"));
284    snprintf (_active_ise_name, sizeof (_active_ise_name), "%s", (uuid_to_name(tmpStr)).c_str());
285    snprintf (ise_bak, sizeof (ise_bak), "%s", _active_ise_name);
286    LOGD("Default ISE Name : %s", ise_bak);
287
288     ugd->naviframe = _create_naviframe_layout (ugd->layout_main);
289     const char *navi_btn_l_lable = bundle_get_val(ugd->data, "navi_btn_left");
290     const char *navi_btn_r_lable = bundle_get_val(ugd->data, "navi_btn_right");
291
292     if (sw_radio_grp != NULL)
293     {
294         evas_object_del(sw_radio_grp);
295         sw_radio_grp = NULL;
296     }
297
298     Evas_Object *genlist = elm_genlist_add(ugd->naviframe);
299     evas_object_show(genlist);
300
301     Elm_Object_Item *nf_it;
302     Evas_Object *cbar = elm_toolbar_add (ugd->naviframe);
303     elm_toolbar_shrink_mode_set(cbar, ELM_TOOLBAR_SHRINK_EXPAND);
304     if (cbar == NULL) return NULL;
305
306     if (navi_btn_l_lable!= NULL) {
307         elm_toolbar_item_append(cbar, NULL, navi_btn_l_lable, sw_keyboard_selection_view_back_cb, ugd);
308         elm_toolbar_item_append(cbar, NULL, navi_btn_r_lable, sw_keyboard_selection_view_set_cb, ugd);
309
310         nf_it = elm_naviframe_item_push(ugd->naviframe, T_("Keyboard"), NULL, NULL, genlist, NULL);
311         elm_object_item_part_content_set(nf_it, "controlbar", cbar);
312     }
313     else {
314         elm_toolbar_item_append(cbar, NULL, navi_btn_r_lable, sw_keyboard_selection_view_set_cb, ugd);
315         nf_it = elm_naviframe_item_push(ugd->naviframe, T_("Keyboard"), NULL, NULL, genlist, NULL);
316         elm_object_item_part_content_set(nf_it, "controlbar", cbar);
317     }
318
319     unsigned int i = 0;
320
321     sw_iselist.clear();
322     std::vector<String> selected_langs, all_langs;
323
324     isf_get_all_languages (all_langs);
325     isf_get_helper_names_in_languages (all_langs, sw_iselist);
326     std::sort (sw_iselist.begin (), sw_iselist.end ());
327
328     if (sw_iselist.size () > 0) {
329         // Set item class for dialogue group seperator
330         itcSeparator.item_style = "dialogue/separator/21/with_line";
331         itcSeparator.func.text_get = NULL;
332         itcSeparator.func.content_get = NULL;
333         itcSeparator.func.state_get = NULL;
334         itcSeparator.func.del = NULL;
335
336         //separator
337         Elm_Object_Item *item;
338         item = elm_genlist_item_append(
339                     genlist,                // genlist object
340                     &itcSeparator,          // item class
341                     NULL,                   // data
342                     NULL,
343                     ELM_GENLIST_ITEM_NONE,
344                     NULL,
345                     NULL);
346         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
347     }
348
349     for (i = 0; i < sw_iselist.size (); i++) {
350         if (strcmp (_active_ise_name, sw_iselist[i].c_str ()) == 0) {
351             mark = i;
352             break;
353         }
354     }
355
356     //set item class for 1text.1icon(text+radiobutton)
357     itc1.item_style = "dialogue/1text.1icon.2";
358     itc1.func.text_get = _gl_label_get;
359     itc1.func.content_get = _gl_icon_get;
360     itc1.func.state_get = _gl_state_get;
361     itc1.func.del = _gl_del;
362     for (i = 0;i < sw_iselist.size();i++) {
363         elm_genlist_item_append(genlist, &itc1,
364                     (void *)(i), NULL, ELM_GENLIST_ITEM_NONE, _gl_sel,
365                     (void *)(i));
366     }
367
368     return ugd->naviframe;
369 }
370
371 ConfigPointer isf_imf_context_get_config(void);
372 static void *on_create (struct ui_gadget *ug, enum ug_mode mode, bundle *data, void *priv)
373 {
374     Evas_Object *parent = NULL;
375     Evas_Object *content = NULL;
376
377     if ( ug == NULL || priv == NULL)
378         return NULL;
379
380     bindtextdomain (WIZARD_PACKAGE, WIZARD_LOCALEDIR);
381
382     struct ug_data *ugd = (struct ug_data *)priv;
383     ugd->ug = ug;
384     ugd->data = data;
385     parent = (Evas_Object *) ug_get_parent_layout (ug);
386     if (parent == NULL)
387         return NULL;
388     //-------------------------- ise infomation ----------------------------
389
390     const char *ctx_id = ecore_imf_context_default_id_get ();
391     if (ctx_id != NULL) {
392         imf_context = ecore_imf_context_add (ctx_id);
393     }
394
395     _config = isf_imf_context_get_config ();
396     if (_config.null ()) {
397         std::cerr << "Create dummy config!!!\n";
398         _config = new DummyConfig ();
399     }
400
401     if (_config.null ()) {
402         std::cerr << "Can not create Config Object!\n";
403     }
404
405     //only helper ISEs will be needed in isfsetting according to phone requirement.
406     isf_load_ise_information (HELPER_ONLY, _config);
407     // Request ISF to update ISE list, below codes are very important, dont remove
408     char **iselist = NULL;
409     int count = isf_control_get_ise_list (&iselist);
410     for (unsigned int i = 0; i < (unsigned int)count; i++) {
411         SCIM_DEBUG_MAIN (3) << " [" << i << " : " << iselist[i] << "] \n";
412         if (iselist[i] != NULL)
413             delete []  (iselist[i]);
414     }
415
416     if (iselist!=NULL)
417         free(iselist);
418     //-------------------------- ise infomation ----------------------------
419
420     //construct the UI part of the isfsetting module
421     if (mode == UG_MODE_FULLVIEW)
422         ugd->layout_main = create_fullview (parent, ugd);
423     else
424         ugd->layout_main = create_frameview (parent, ugd);
425
426     if (ugd->layout_main != NULL) {
427         content = isf_setting_main_view_tizen(ugd);
428         elm_object_part_content_set (ugd->layout_main, "elm.swallow.content", content);
429     }
430     return (void *)ugd->layout_main;
431 }
432
433 static void on_start (struct ui_gadget *ug, bundle *data, void *priv)
434 {
435 }
436
437 static void on_pause (struct ui_gadget *ug, bundle *data, void *priv)
438 {
439
440 }
441
442 static void on_resume (struct ui_gadget *ug, bundle *data, void *priv)
443 {
444
445 }
446
447 static void on_destroy (struct ui_gadget *ug, bundle *data, void *priv)
448 {
449     if ( ug == NULL|| priv == NULL)
450         return;
451
452     if (imf_context != NULL) {
453         ecore_imf_context_del(imf_context);
454         imf_context = NULL;
455     }
456
457     struct ug_data *ugd = (struct ug_data *) priv;
458
459     if (ugd->naviframe != NULL) {
460         evas_object_del (ugd->naviframe);
461         ugd->naviframe = NULL;
462     }
463
464     if (ugd->layout_main != NULL) {
465         evas_object_del (ugd->layout_main);
466         ugd->layout_main = NULL;
467     }
468
469     if (!_config.null ()) {
470         _config->flush ();
471         _config.reset ();
472     }
473 }
474
475 static void on_message (struct ui_gadget *ug, bundle *msg, bundle *data, void *priv)
476 {
477 }
478
479 static void on_event (struct ui_gadget *ug, enum ug_event event, bundle *data, void *priv)
480 {
481     switch (event) {
482     case UG_EVENT_LOW_MEMORY:
483         break;
484     case UG_EVENT_LOW_BATTERY:
485         break;
486     case UG_EVENT_LANG_CHANGE:
487         break;
488     case UG_EVENT_ROTATE_PORTRAIT:
489         break;
490     case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
491         break;
492     case UG_EVENT_ROTATE_LANDSCAPE:
493         break;
494     case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
495         break;
496     default:
497         break;
498     }
499 }
500
501 static void on_key_event(struct ui_gadget *ug, enum ug_key_event event, bundle *data, void *priv)
502 {
503     if (ug == NULL)
504         return;
505
506     switch (event) {
507         case UG_KEY_EVENT_END:
508             ug_destroy_me(ug);
509             break;
510         default:
511             break;
512     }
513 }
514
515 #ifdef __cplusplus
516 extern "C"
517 {
518 #endif
519     UG_WIZARD_MODULE_API int UG_MODULE_INIT (struct ug_module_ops *ops) {
520         if (ops == NULL)
521             return -1;
522
523         struct ug_data *ugd = (ug_data*)calloc (1, sizeof (struct ug_data));
524         if (ugd == NULL)
525             return -1;
526
527         ops->create  = on_create;
528         ops->start   = on_start;
529         ops->pause   = on_pause;
530         ops->resume  = on_resume;
531         ops->destroy = on_destroy;
532         ops->message = on_message;
533         ops->event   = on_event;
534         ops->key_event = on_key_event;
535         ops->priv    = ugd;
536         ops->opt     = UG_OPT_INDICATOR_PORTRAIT_ONLY;
537
538         return 0;
539     }
540
541     UG_WIZARD_MODULE_API void UG_MODULE_EXIT (struct ug_module_ops *ops) {
542         if (ops == NULL)
543             return;
544
545         struct ug_data *ugd = (struct ug_data *)(ops->priv);
546         if (ugd != NULL)
547             free (ugd);
548     }
549
550 #ifdef __cplusplus
551 }
552 #endif
553 /*
554 vi:ts=4:ai:nowrap:expandtab
555 */
556