Change select method of language in setup wizard
[apps/core/preloaded/settings.git] / setting-phone / src / setting-phone-display-language.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-phone-display-language.h>
22
23
24 static int setting_phone_display_language_create(void *cb);
25 static int setting_phone_display_language_destroy(void *cb);
26 static int setting_phone_display_language_cleanup(void *cb);
27
28
29 setting_view setting_view_phone_display_language = {
30         .create = setting_phone_display_language_create,
31         .destroy = setting_phone_display_language_destroy,
32         .update = NULL,
33         .cleanup = setting_phone_display_language_cleanup,
34 };
35
36 /*
37  * exit and save
38  */
39 static void setting_phone_display_language_caller_exist_right_cb(void *data,
40                                                        Evas_Object *obj,
41                                                        void *event_info)
42 {
43         SETTING_TRACE_BEGIN;
44         setting_retm_if(data == NULL, "Data parameter is NULL");
45
46         SettingPhoneUG *ad = (SettingPhoneUG *) data;
47 #if 0
48         int ret, err;
49
50         int cur_chk_lang = (int)elm_radio_value_get(ad->chk_lang);
51
52         setting_phone_lang_change_lang_value(cur_chk_lang);
53
54         //ret = setting_get_int_slp_key(INT_SLP_SETTING_LANG, &chr_chk_lang, &err);
55         SETTING_TRACE("---> cur_chk_lang : %d", cur_chk_lang);
56         ret = setting_set_int_slp_key(INT_SLP_SETTING_LANG, cur_chk_lang, &err);
57
58         if (0 == cur_chk_lang) {
59                 (void)vconf_set_str(VCONFKEY_LANGSET, ad->sim_lang);
60         }
61
62         /* Just filter out Setting App, other Apps stil need to be terminated
63        until they implement updating language or region automatically */
64
65         const char *pa_lang = vconf_get_str(VCONFKEY_LANGSET);
66         if (pa_lang) {
67                 char *q = strchr(pa_lang, '.');
68                 if (q) {
69                         *q = '\0';
70                 }
71                 SETTING_TRACE("pa_lang:%s", pa_lang);
72                 int err = -1;
73                 uloc_setDefault(pa_lang, &err);
74                 free((void *)pa_lang);
75                 pa_lang = NULL;
76         } else {
77                 SETTING_TRACE_ERROR("get vconf failed");
78         }
79 #endif
80
81         /*  Create Bundle and send message */
82         service_h svc;
83         if(service_create(&svc))
84                 return;
85
86         service_add_extra_data(svc, "result", "rbutton_click");
87         ug_send_result(ad->ug, svc);
88
89         service_destroy(svc);
90
91         ug_destroy_me(ad->ug);
92 }
93
94
95 /* ***************************************************
96  *
97  *basic func
98  *
99  ***************************************************/
100
101 static int setting_phone_display_language_create(void *cb)
102 {
103         SETTING_TRACE_BEGIN;
104         /* error check */
105         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
106
107         SettingPhoneUG *ad = (SettingPhoneUG *) cb;
108         char *title = _("IDS_ST_HEADER_DISPLAY_LANGUAGE");
109
110         Evas_Object *scroller = NULL;
111         setting_call_back_func gl_sel_cb = NULL;
112
113         if (&setting_view_phone_display_language == ad->view_to_load) {
114                 char *caller = NULL;
115
116                 service_h service = ad->bundle_data;
117
118                 service_get_extra_data(service, "caller", &caller);
119
120                 if(caller)
121                 {
122                         ad->ly_language = setting_create_layout_navi_bar_genlist(ad->win_main_layout,
123                                         ad->win_get,
124                                         title,
125                                         _("IDS_COM_BODY_BACK"), NULL,
126                                         setting_phone_display_language_caller_exist_right_cb, NULL,
127                                         ad, &scroller, &ad->navi_bar);
128                         gl_sel_cb = setting_phone_display_language_mouse_up_Gendial_list_radio_cb;
129                 }
130                 else
131                 {
132                         SETTING_TRACE_ERROR("[ERROR] caller is empty");
133                         return SETTING_RETURN_FAIL;
134                 }
135         } else {
136                 /* add basic layout */
137                 setting_push_layout_navi_bar_genlist(ad->win_main_layout, ad->win_get,
138                                              title, _("IDS_COM_BODY_BACK"),
139                                              NULL,
140                                              setting_phone_display_language_click_softkey_cancel_cb,
141                                              NULL, ad, &scroller, ad->navi_bar);
142                 gl_sel_cb = setting_phone_display_language_mouse_up_Gendial_list_radio_cb;
143         }
144
145         ad->gl_lang = scroller;
146
147         Elm_Object_Item *item = NULL;
148         item =
149             elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL,
150                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
151         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
152
153         /* scroller is a genlist */
154         ad->chk_lang = elm_radio_add(scroller);
155         elm_radio_state_value_set(ad->chk_lang, -1);
156
157
158
159         // [UI] Automatic
160         Setting_GenGroupItem_Data *item_data = NULL;
161         item_data =
162             setting_create_Gendial_field_1radio(scroller,
163                                                 &(ad->itc_1text_1icon_2),
164                                                 gl_sel_cb,
165                                                 ad->chk_lang,
166                                                 SWALLOW_Type_1RADIO,
167                                                 ad->chk_lang, 0,
168                                                 "IDS_COM_BODY_AUTOMATIC", NULL);
169         if (item_data) {
170                 item_data->userdata = ad;
171         } else {
172                 SETTING_TRACE_ERROR("item_data is NULL");
173         }
174
175         // create loop operation here
176     Eina_List* elist = NULL;
177         setting_lang_entry* pnode = NULL;
178     int idx = SETTING_LANG_KOREA;               // 2
179
180         // load language table from XML file
181     Eina_List* langlist = setting_get_language_list();
182
183     EINA_LIST_FOREACH( langlist, elist, pnode)
184     {
185                 item_data =
186                         setting_create_Gendial_field_1radio(scroller,
187                                                         &(ad->itc_1text_1icon_2),
188                                                         gl_sel_cb,
189                                                         ad->chk_lang,
190                                                         SWALLOW_Type_1RADIO,
191                                                         ad->chk_lang,
192                                                         //idx,                  // <<< WARNING
193                                                         pnode->number,
194                                                         pnode->title, NULL);
195                 if (item_data) {
196                         item_data->userdata = ad;
197                 } else {
198                         SETTING_TRACE_ERROR("item_data is NULL");
199                 }
200         idx++;
201     }
202
203         item = elm_genlist_item_append(scroller, &itc_bottom_seperator, NULL, NULL,
204                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
205         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
206
207         // set radio button to the selected state.
208         int index = -1;
209         int ret, err;
210         ret = setting_get_int_slp_key(INT_SLP_SETTING_LANG, &index, &err);
211         elm_radio_value_set(ad->chk_lang, index);
212
213         ad->prev_lang = (int)elm_radio_value_get(ad->chk_lang);
214         setting_view_phone_display_language.is_create = 1;
215
216         return SETTING_RETURN_SUCCESS;
217 }
218
219 static int setting_phone_display_language_destroy(void *cb)
220 {
221         SETTING_TRACE_BEGIN;
222         /* error check */
223         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
224         SettingPhoneUG *ad = (SettingPhoneUG *) cb;
225
226         /*  FIXED : destroy only if it was created. */
227         if (setting_view_phone_display_language.is_create) {
228                 if (ad->popup) {
229                         evas_object_del(ad->popup);
230                         ad->popup = NULL;
231                 }
232
233                 if (ad->pop_progress) {
234                         evas_object_del(ad->pop_progress);
235                         ad->pop_progress = NULL;
236                 }
237                 elm_naviframe_item_pop(ad->navi_bar);
238                 setting_view_phone_display_language.is_create = 0;
239
240                 setting_get_language_list_destroy();
241         }
242
243         return SETTING_RETURN_SUCCESS;
244 }
245
246 static int setting_phone_display_language_cleanup(void *cb)
247 {
248         SETTING_TRACE_BEGIN;
249         /* error check */
250         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
251         setting_phone_display_language_destroy(cb);
252
253         return SETTING_RETURN_SUCCESS;
254 }
255
256 static void
257 setting_phone_display_language_click_softkey_cancel_cb(void *data,
258                                                        Evas_Object *obj,
259                                                        void *event_info)
260 {
261         SETTING_TRACE_BEGIN;
262         /* error check */
263         retm_if(data == NULL, "Data parameter is NULL");
264         SettingPhoneUG *ad = (SettingPhoneUG *) data;
265         setting_view_change(&setting_view_phone_display_language,
266                             &setting_view_phone_language_region, ad);
267 }
268
269 static void
270 setting_phone_display_language_mouse_up_Gendial_list_radio_cb(void *data,
271                                                               Evas_Object *obj,
272                                                               void *event_info)
273 {
274         SETTING_TRACE_BEGIN;
275         /* error check */
276         setting_retm_if(data == NULL, "Data parameter is NULL");
277         setting_retm_if(event_info == NULL,
278                         "Invalid argument: event info is NULL");
279
280         Evas_Object *radio = (Evas_Object *) data;
281         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
282         elm_genlist_item_selected_set(item, 0);
283         Setting_GenGroupItem_Data *list_item =
284             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
285         SettingPhoneUG *ad = (SettingPhoneUG *) list_item->userdata;
286
287         if (ad->popup) {
288                 return;
289         }
290         //SETTING_TRACE("---------------------------------------------");
291         //SETTING_TRACE("ad->prev_lang = %d ", ad->prev_lang);
292         //SETTING_TRACE("chk_status = %d ", list_item->chk_status);
293         //SETTING_TRACE("---------------------------------------------");
294         /*  if not change, return */
295         if (ad->prev_lang == list_item->chk_status) {
296                 SETTING_TRACE("NOT CHANGED ");
297                 return;
298         }
299
300         elm_radio_value_set(radio, list_item->chk_status);
301         setting_phone_display_language_close_popup_ex(ad);
302 }
303
304 static void setting_phone_display_language_close_popup_ex(void *data)
305 {
306         SETTING_TRACE_BEGIN;
307         setting_retm_if(data == NULL, "Data parameter is NULL");
308         SettingPhoneUG *ad = (SettingPhoneUG *) data;
309
310         ad->prev_lang = elm_radio_value_get(ad->chk_lang);
311         SETTING_TRACE("cur_chk_lang = %d", ad->prev_lang);
312
313         // 0. change VCONFKEY_LANGSET
314         setting_phone_lang_change_lang_value(ad->prev_lang);
315
316         /*  if set automatic */
317         if (0 == ad->prev_lang) {
318                 vconf_set_str(VCONFKEY_LANGSET, ad->sim_lang);
319         }
320
321         // 2. GET SELECTED LANG
322         const char *pa_lang = vconf_get_str(VCONFKEY_LANGSET);
323         if (pa_lang) {  /* remove the */
324                 char *q = strchr(pa_lang, '.');
325                 if (q)
326                         *q = '\0';
327         }
328         SETTING_TRACE("pa_lang:%s", pa_lang);
329         int err = -1;
330         // 3. SET DEFAULT
331         // default UI language
332         uloc_setDefault(pa_lang, &err);
333         if (U_FAILURE(err)) {
334                 SETTING_TRACE("FAILED  : uloc_setDefault error code : %d", err);
335         } else {
336                 SETTING_TRACE("SUCCESS : uloc_setDefault error code : %d", err);
337         }
338         FREE(pa_lang);
339
340         char *caller = NULL;
341         service_h service = ad->bundle_data;
342
343         service_get_extra_data(service, "caller", &caller);
344
345         if(caller) {
346                 /*  Create Bundle and send message */
347                 service_h svc;
348                 if(service_create(&svc))
349                         return;
350
351                 service_add_extra_data(svc, "result", "rbutton_click");
352                 ug_send_result(ad->ug, svc);
353
354                 service_destroy(svc);
355
356                 ug_destroy_me(ad->ug);
357
358         } else {
359                 /* after lang-change, go back to the previous page*/
360                 elm_naviframe_item_pop(ad->navi_bar);
361                 setting_view_phone_display_language.is_create = 0;
362         }
363 }