2b395d28af1c9c151cf692664ca61d969bc47d8b
[apps/core/preloaded/settings.git] / setting-time / src / setting-time-main.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
22 #include <setting-time-main.h>
23 #include <sysman.h>
24
25 #include <stdio.h>
26 #include <malloc.h>
27 #include <time.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sqlite3.h>
31
32 #include <unicode/udat.h>
33 #include <unicode/ustring.h>
34 #include <unicode/uloc.h>
35 #include <unicode/ucal.h>
36 #include <unicode/utmscale.h>
37
38 #define ONE_MINUTE 60
39 #define PROGRESS_POPUP_SHOW 0
40
41 /*  ICU utilities*/
42 static UChar *uastrcpy(const char *chars);
43 static void ICU_set_timezone(const char *timezone);
44 static int get_timezone_isdst(int* isdst);
45 static void get_gmt_offset(char *str_buf, int size);
46
47 static char* get_timezone_str();
48 static char* get_city_name_result();
49 static bool get_city_name(char* tzname);
50
51 static char* s_result;
52 static int query_callback(void *NotUsed, int argc, char **argv, char **azColName);
53
54
55 static int setting_time_main_create(void *cb);
56 static int setting_time_main_destroy(void *cb);
57 static int setting_time_main_update(void *cb);
58 static int setting_time_main_cleanup(void *cb);
59
60 setting_view setting_view_time_main = {
61         .create = setting_time_main_create,
62         .destroy = setting_time_main_destroy,
63         .update = setting_time_main_update,
64         .cleanup = setting_time_main_cleanup,
65 };
66
67 static void setting_time_reboot_resp_cb(void *data, Evas_Object *obj,
68                                         void *event_info);
69
70 #if TIME_CHANGED_HEYNOTY_CALLBACK
71 void time_changed_callback(void *data)
72 {
73         SETTING_TRACE_DEBUG("time and timezone have been changed: TIME_CHANGED_HEYNOTY_CALLBACK");
74         SettingTimeUG *ad = (SettingTimeUG *)data;
75
76         int ret = setting_time_update_cb(ad);
77         if (ret != 0) {
78                 SETTING_TRACE("FAIL: setting_time_update_db(ad)\n");
79         }
80
81         if(EINA_TRUE == ad->is_datefield_selected) {
82                 /* Timezone is not changed and a selected value of the datefield is already changed */
83                 SETTING_TRACE("Datefield is selected\n");
84                 return ;
85         }
86
87         tzset();
88
89         // vconfset to update timezone
90         char pTZPath[__MAX_PATH_SIZE];
91         ssize_t nLen = readlink(SETTING_TZONE_SYMLINK_PATH, pTZPath, sizeof(pTZPath)-1);
92
93         if (nLen != -1) {
94                 pTZPath[nLen] = '\0';
95         } else {
96                 SETTING_TRACE("parse error for SETTING_TZONE_SYMLINK_PATH");
97                 return;
98         }
99
100         if (!__setting_set_city_tzone(&pTZPath[20])) {
101                 SETTING_TRACE("__setting_set_city_tzone ERROR");
102                 return;
103         }
104
105         setting_time_main_update(data);
106
107         char *timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
108         setting_retm_if(timezone == NULL, "get vonf failed");
109
110         ICU_set_timezone(timezone);
111         FREE(timezone);
112 }
113 #endif
114
115 void setting_time_update_time_date_format_string(SettingTimeUG *ad)
116 {
117         SETTING_TRACE_BEGIN;
118
119         struct tm *ts;
120         struct tm ts_ret;
121         time_t ctime;
122
123         int ret, value, err;
124
125         char time_str[MAX_ARGS];
126
127         ctime = time(NULL);
128         ts = localtime_r(&ctime, &ts_ret);
129         ret_if(!ts);
130
131         /* set time in particular format */
132         ret = setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224, &value, &err);
133         if (ret == SETTING_RETURN_FAIL)
134                 value = VCONFKEY_TIME_FORMAT_12;
135
136         switch (value) {
137         case VCONFKEY_TIME_FORMAT_12:
138                 {
139                         /* Get Time */
140                         strftime(time_str, MAX_ARGS, "%l:%M %p ", &ts_ret);
141                 }
142                 break;
143
144         case VCONFKEY_TIME_FORMAT_24:
145                 {
146                         strftime(time_str, MAX_ARGS, "%H:%M", &ts_ret);
147                 }
148                 break;
149         default:
150                 /* do nothing */
151                 break;
152         }
153
154         if (ad->data_time) {
155                 elm_object_item_data_set(ad->data_time->item, ad->data_time);
156                 elm_genlist_item_update(ad->data_time->item);
157         }
158 }
159
160 bool setting_update_timezone(SettingTimeUG *ad)
161 {
162         /* SETTING_TRACE_BEGIN; */
163         char time_zone_sub_str[CITY_BUF_SIZE + GMT_BUF_SIZE + 3] = { 0, };
164         char *pa_timezone = NULL;
165         char *pa_city = NULL;
166         int ret = 0;
167
168         /*  get time zone */
169         pa_city = vconf_get_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT);
170         pa_timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT);
171
172         //if ((pa_timezone != NULL) && (_(pa_city) != NULL && safeStrCmp(_(pa_city), "NULL"))) {
173         if ((pa_timezone != NULL) && (_(pa_city) != NULL )) {
174                 /* SETTING_TRACE_DEBUG("*** timezone: %s *** ", timezone); */
175                 /* SETTING_TRACE_DEBUG("*** city: %s *** ", _(city)); */
176                 /* SETTING_TRACE_DEBUG("*** ret: %d, dst: %d *** ", ret, dst); */
177
178                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT %s", _(pa_city), pa_timezone);
179                 //ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT %s", pa_timezone, _(pa_city));
180                 FREE(pa_city);
181                 retv_if(ret < 0, 0);
182         } else {
183                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT +09", _("IDS_WCL_BODY_CITYNAME_SEOUL"));
184                 retv_if(ret < 0, 0);
185         }
186         SETTING_TRACE("ad->data_tz:%p", ad->data_tz);
187
188         if (ad->data_tz) {
189                 ad->data_tz->sub_desc = (char *)g_strdup(time_zone_sub_str);
190                 elm_object_item_data_set(ad->data_tz->item, ad->data_tz);
191                 elm_genlist_item_update(ad->data_tz->item);
192         }
193         return 0;
194 }
195
196 bool __update_timezone_idler(SettingTimeUG *ad)
197 {
198         setting_update_timezone(ad);
199         ad->update_timezone_idler = NULL;
200         return 0;
201 }
202
203 static void setting_time_main_int_vconf_change_cb(keynode_t *key, void *data)
204 {
205         /* SETTING_TRACE_BEGIN; */
206         ret_if(NULL == data);
207         SettingTimeUG *ad = data;
208         int status = vconf_keynode_get_int(key);
209         char *vconf_name = vconf_keynode_get_name(key);
210         /* SETTING_TRACE("vconf_name:%s", vconf_name); */
211
212         if (!safeStrCmp(vconf_name, VCONFKEY_REGIONFORMAT_TIME1224)) {
213
214                 if (ad->data_time) {
215                         /* VCONFKEY_REGIONFORMAT_TIME1224 1:12H 2:24H */
216                         if (status <= 2 && status > 0)  /*  1, 2 */
217                                 --status;       /*  set to 0 or 1 */
218                         else
219                                 status = 0;     /*  set 12H       */
220
221                         /*  TODO assert 1 or 0 */
222                         ad->data_time->chk_status = !status;
223
224                         char datefield_format[MAX_DATETIME_FORMAT_LEN + 1] = {0, };
225
226                         const char *time_format = NULL;
227                         if (ad->data_time->chk_status)//12 hours
228                         {
229                                 time_format = "%I : %M %p";
230                         } else { //24 hours
231                                 time_format = "%H : %M";
232                         }
233
234 //get date format from datefiled itself, if elm_datetime_add() supports set date-format via ICU.
235 #ifdef SUPPORT_DATEFIELD_DATE_FORMAT_AUTOSET
236                         const char *old_format = elm_datetime_format_get(ad->data_time->eo_check);
237                         snprintf(datefield_format, MAX_DATE_FORMAT_LEN + 1, //add one space " "
238                                  "%s", old_format);
239                         safeStrNCat(datefield_format, time_format, MAX_DATETIME_FORMAT_LEN);
240                         SETTING_TRACE("datefield_format:%s", datefield_format);
241 #else
242                         int value = SETTING_DATE_FORMAT_DD_MM_YYYY;
243                         int err = -1;
244
245                         char *date_format_str[] = { "%d %b %Y", "%b %d %Y", "%Y %b %d", "%Y %d %b" };
246                         int ret = setting_get_int_slp_key(INT_SLP_SETTING_DATE_FORMAT, &value, &err);
247                         if (ret == SETTING_RETURN_FAIL)
248                                 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
249
250                         snprintf(datefield_format, MAX_DATETIME_FORMAT_LEN,
251                                  "%s %s", date_format_str[value], time_format);
252 #endif
253
254                         elm_datetime_format_set(ad->data_time->eo_check, datefield_format);
255                 }
256         }
257 }
258
259 /* ***************************************************
260  *
261  *basic func
262  *
263  ***************************************************/
264 static void setting_time_main_datefield_change_cb(void *data, Evas_Object *obj,
265                                       void *event_info)
266 {
267         SETTING_TRACE_BEGIN;
268         /* error check */
269         ret_if(data == NULL);
270         Setting_GenGroupItem_Data *list_item = (Setting_GenGroupItem_Data *) data;
271         SettingTimeUG *ad = list_item->userdata;
272
273         if (EINA_FALSE == ad->is_datefield_selected) {
274                 SETTING_TRACE("Datefield is not selected\n");
275                 return;
276         }
277
278         // If auto update is on, don't anything while the datefield has been updated (i.e., time runs)
279         if (1 == setting_time_check_automatic_time_update_state()) {
280                 SETTING_TRACE("AUTO_TIME ON: no action");
281                 SETTING_TRACE_END;
282                 return;
283         } else {
284                 // otherwise, we apply a new date which has been changed by a user
285         }
286
287         struct tm _tm;
288         elm_datetime_value_get(obj, &_tm);
289
290         SETTING_TRACE("year : %d, month : %d, day : %d, hour : %d, min : %d",
291                       _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min);
292         if (2037 < _tm.tm_year) {
293                 setting_create_simple_popup(ad, ad->win_get,
294                                             NULL, _(ERR_BIGGER_THAN_2037));
295                 return;
296         }
297 #if SUPPORT_SCREEN_PROTECTED    /* if not lock the current state, after set the time, the screen will become dimmed */
298         pm_lock_state(LCD_NORMAL, STAY_CUR_STATE, 0);
299 #endif
300         _tm.tm_sec = 0;
301
302         int isdst;
303         if (get_timezone_isdst(&isdst) == SETTING_RETURN_SUCCESS) {
304                 _tm.tm_isdst = isdst;
305         } else {
306                 // error
307                 SETTING_TRACE_ERROR("get_timezone_isdst() failed");
308                 return;
309         }
310
311         /*  local time  -> gmtime */
312         time_t the_time = mktime(&_tm); /* represent local time */
313         ad->changed_time = the_time;
314
315         if ( (time(NULL)/ONE_MINUTE) == (the_time/ONE_MINUTE) )
316         {
317                 SETTING_TRACE("Time is not changed\n");
318                 //current time didn't change
319                 return;
320         }
321
322 #if PROGRESS_POPUP_SHOW
323         // remove popup if created
324         if (ad->pop_progress) {
325                 evas_object_del(ad->pop_progress);
326                 ad->pop_progress = NULL;
327         }
328
329         // create new popup
330         ad->pop_progress =
331                 setting_create_popup_with_progressbar(ad, ad->win_get, "pending_list",
332                                                       NULL, NULL, NULL, 0, TRUE, TRUE);
333 #endif
334
335         // invoke API to change time
336         int ret = sysman_set_datetime(the_time);
337         setting_retm_if(ret == -1, "sysman_set_datetime call failed");
338
339         SETTING_TRACE_END;
340 }
341
342 Eina_Bool __refresh_date_timer(void *data)
343 {
344         SETTING_TRACE_BEGIN;
345         SettingTimeUG *ad = data;
346         if (ad->data_time) {
347                 time_t ctime = time(NULL);
348                 struct tm ts_ret;
349                 struct tm *ts = localtime_r(&ctime, &ts_ret);
350                 retv_if(!ts, 1);
351
352                 elm_datetime_value_set(ad->data_time->eo_check, &ts_ret);
353         }
354         return 1;
355 }
356
357 static void __setting_time_main_exp_cb(void *data, Evas_Object *obj,
358                                        void *event_info)
359 {
360         ret_if(NULL == data || NULL == event_info);
361         SETTING_TRACE_BEGIN;
362         SettingTimeUG *ad = (SettingTimeUG *) data;
363         Elm_Object_Item *parentItem = event_info;       /* parent item */
364         Setting_GenGroupItem_Data *data_parentItem = elm_object_item_data_get(parentItem);      /* parent data */
365         Evas_Object *scroller = elm_object_item_widget_get(parentItem);
366
367         Evas_Object *rgd;
368         if (data_parentItem == ad->data_time_fmt) {
369
370                 rgd = elm_radio_add(scroller);
371                 elm_radio_value_set(rgd, -1);
372                 setting_create_Gendial_exp_sub_field(scroller,
373                                                      &(ad->itc_1icon_1text_sub),
374                                                      NULL, NULL, parentItem,
375                                                      SWALLOW_Type_1RADIO, rgd,
376                                                      VCONFKEY_TIME_FORMAT_12,
377                                                      "IDS_COM_BODY_12_HOURS",
378                                                      NULL);
379
380                 setting_create_Gendial_exp_sub_field(scroller,
381                                                      &(ad->itc_1icon_1text_sub),
382                                                      NULL, NULL, parentItem,
383                                                      SWALLOW_Type_1RADIO, rgd,
384                                                      VCONFKEY_TIME_FORMAT_24,
385                                                      "IDS_ST_BODY_24_HOURS",
386                                                      NULL);
387
388                 setting_update_chk_status(rgd,
389                                           data_parentItem->int_slp_setting_binded);
390
391         } else if (data_parentItem == ad->data_date_fmt) {
392                 rgd = elm_radio_add(scroller);
393                 elm_radio_value_set(rgd, -1);
394                 setting_create_Gendial_exp_sub_field(scroller,
395                                                      &(ad->itc_1icon_1text_sub),
396                                                      NULL, NULL, parentItem,
397                                                      SWALLOW_Type_1RADIO, rgd,
398                                                      SETTING_DATE_FORMAT_DD_MM_YYYY,
399                                                      "IDS_ST_BODY_DDMMYYYY_DOT",
400                                                      NULL);
401
402                 setting_create_Gendial_exp_sub_field(scroller,
403                                                      &(ad->itc_1icon_1text_sub),
404                                                      NULL, NULL, parentItem,
405                                                      SWALLOW_Type_1RADIO, rgd,
406                                                      SETTING_DATE_FORMAT_MM_DD_YYYY,
407                                                      "IDS_ST_BODY_MM_DD_YYYY_DOT",
408                                                      NULL);
409
410                 setting_create_Gendial_exp_sub_field(scroller,
411                                                      &(ad->itc_1icon_1text_sub),
412                                                      NULL, NULL, parentItem,
413                                                      SWALLOW_Type_1RADIO, rgd,
414                                                      SETTING_DATE_FORMAT_YYYY_MM_DD,
415                                                      "IDS_COM_BODY_YYYYMMDD",
416                                                      NULL);
417
418                 setting_create_Gendial_exp_sub_field(scroller,
419                                                      &(ad->itc_1icon_1text_sub),
420                                                      NULL, NULL, parentItem,
421                                                      SWALLOW_Type_1RADIO, rgd,
422                                                      SETTING_DATE_FORMAT_YYYY_DD_MM,
423                                                      "IDS_ST_BODY_YYYY_DD_MM_DOT",
424                                                      NULL);
425
426                 setting_update_chk_status(rgd,
427                                           data_parentItem->int_slp_setting_binded);
428
429         } else if (data_parentItem == ad->data_firstday_week_fmt) {
430                 rgd = elm_radio_add(scroller);
431                 elm_radio_value_set(rgd, -1);
432                 setting_create_Gendial_exp_sub_field(scroller,
433                                                      &(ad->itc_1icon_1text_sub),
434                                                      NULL, rgd, parentItem,
435                                                      SWALLOW_Type_1RADIO, rgd,
436                                                      SETTING_WEEKOFDAY_FORMAT_SUNDAY,
437                                                      "IDS_ST_BODY_SUNDAY", NULL);
438
439                 setting_create_Gendial_exp_sub_field(scroller,
440                                                      &(ad->itc_1icon_1text_sub),
441                                                      NULL, rgd, parentItem,
442                                                      SWALLOW_Type_1RADIO, rgd,
443                                                      SETTING_WEEKOFDAY_FORMAT_MONDAY,
444                                                      "IDS_ST_BODY_MONDAY", NULL);
445
446                 setting_update_chk_status(rgd,
447                                           data_parentItem->int_slp_setting_binded);
448         }
449 }
450
451 static int setting_time_main_create(void *cb)
452 {
453         SETTING_TRACE_BEGIN;
454         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
455
456         SettingTimeUG *ad = (SettingTimeUG *) cb;
457
458         char *l_btn = NULL;
459         char *r_btn = NULL;
460         char *title = _("IDS_ST_BODY_DATE_AND_TIME");
461         char *caller = NULL;
462         ad->is_datefield_selected = EINA_FALSE;
463
464         setting_call_back_func l_button_cb = NULL;
465         setting_call_back_func r_button_cb = NULL;
466
467         service_h service = ad->bundle_data;
468
469         service_get_extra_data(service, "caller", &caller);
470         service_get_extra_data(service, "lbutton", &l_btn);
471         if(l_btn)
472                 l_button_cb = setting_time_main_click_softkey_caller_exist_left_cb;
473         service_get_extra_data(service, "title", &title);
474         service_get_extra_data(service, "rbutton", &r_btn);
475         if(r_btn)
476                 r_button_cb = setting_time_main_click_softkey_caller_exist_right_cb;
477
478         Evas_Object *scroller;  /*  scroller is a genlist */
479
480         ad->caller = caller;
481         if (ad->caller) {
482                 char *lbtn_str = (char *)g_strdup(l_btn);
483                 bool same_flag = FALSE;
484                 if (0 == safeStrCmp(l_btn, _("IDS_COM_BODY_BACK"))) {
485                         l_btn = (char *)dgettext("sys_string", "IDS_COM_BODY_BACK");
486                         same_flag = TRUE;
487                 }
488
489                 Evas_Object *controllbar = NULL;
490
491                 scroller = elm_genlist_add(ad->win_main_layout);
492                 elm_object_style_set(scroller, "dialogue");
493                 if(scroller == NULL) {
494                         SETTING_TRACE_ERROR("Cannot set scroller object  as contento of layout");
495                         // free allocated data before return.
496                         FREE(lbtn_str);
497                         return SETTING_DRAW_ERR_FAIL_SCROLLER;
498                 }
499
500                 elm_genlist_clear(scroller);
501                 ad->ly_main =
502                             setting_create_layout_navi_bar(ad->win_main_layout,
503                                                            ad->win_get,
504                                                            title,
505                                                            NULL, NULL, NULL,
506                                                            NULL, NULL, NULL,
507                                                            ad, scroller,
508                                                            &ad->navi_bar,
509                                                            &controllbar);
510
511                 elm_toolbar_item_append(controllbar, IMG_PREVIOUS, l_btn, l_button_cb, ad);
512                 elm_object_item_disabled_set(elm_toolbar_item_append(controllbar, NULL, "", NULL, NULL), EINA_TRUE);
513                 elm_object_item_disabled_set(elm_toolbar_item_append(controllbar, NULL, "", NULL, NULL), EINA_TRUE);
514                 elm_toolbar_item_append(controllbar, IMG_NEXT, r_btn, r_button_cb, ad);
515
516                 if (same_flag) {
517                         setting_time_main_controlbar_lbtn_label_set(controllbar,
518                                                                     lbtn_str);
519                 }
520                 if (lbtn_str) {
521                         G_FREE(lbtn_str);
522                 }
523
524         } else {
525                 l_btn = _("IDS_COM_BODY_BACK");
526                 title = _("IDS_ST_BODY_DATE_AND_TIME");
527                 ad->ly_main =
528                     setting_create_layout_navi_bar_genlist(ad->win_main_layout,
529                                                            ad->win_get, title,
530                                                            l_btn, NULL,
531                                                            setting_time_main_click_softkey_left_cb,
532                                                            NULL, ad, &scroller,
533                                                            &ad->navi_bar);
534         }
535         ad->scrl_main = scroller;
536
537         int value;
538         int err;
539         int ret = 0;
540         Elm_Object_Item *item;
541
542         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL,
543                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
544         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
545         setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE, &value,
546                                  &err);
547         bool auto_flag = value;
548
549         /*  UI automatic */
550         if (!isEmulBin()) {
551                 ad->data_auto =
552                     setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon),
553                                                      setting_time_main_mouse_up_Gendial_list_cb,
554                                                      ad, SWALLOW_Type_1TOGGLE, NULL,
555                                                      NULL, value,
556                                                      "IDS_ST_BODY_AUTOMATIC_UPDATE", NULL,
557                                                      setting_time_main_chk_btn_cb);
558                 if (ad->data_auto) {
559                         ad->data_auto->userdata = ad;
560                 } else {
561                         SETTING_TRACE_ERROR("ad->data_auto is NULL");
562                 }
563
564                 setting_create_Gendial_field_def(scroller, &itc_multiline_text, NULL,
565                                                  ad, SWALLOW_Type_LAYOUT_SPECIALIZTION,
566                                                  NULL, NULL, 0, SETTING_TIME_AUTO_UPDATE_DESC, NULL,
567                                                  NULL);
568         }
569
570         appcore_set_i18n("worldclock", SETTING_TIME_SHARE_LOCAL_PATH);
571
572         char time_zone_sub_str[CITY_BUF_SIZE + GMT_BUF_SIZE + 3] = { 0, };
573         char *pa_timezone = NULL;
574         char *pa_city = NULL;
575
576         pa_city = vconf_get_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT);   // IDS_WCL_BODY_CITYNAME_SEOUL
577         pa_timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT);             // "Asia/Seoul"
578
579         if ((pa_timezone != NULL) && (_(pa_city) != NULL)) {
580
581                 // don't use hard coded value
582                 // get value from current timezone
583
584                 char str_buf[GMT_BUF_SIZE] = {0, };
585                 get_gmt_offset(str_buf, GMT_BUF_SIZE);
586
587                 char* timezone_str = get_timezone_str();
588
589                 bool ret = get_city_name(timezone_str);
590                 if (ret) {
591                         SETTING_TRACE(">>>>>> RESULT : %s", get_city_name_result());
592                 }
593                 char* cityname_id = get_city_name_result();
594
595                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT %s", _(cityname_id), _(str_buf));
596                 FREE(pa_city);
597                 FREE(pa_timezone);
598                 retv_if(ret < 0, 0);
599         } else {
600                 // default code
601                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT +09", _("IDS_WCL_BODY_CITYNAME_SEOUL"));
602                 retv_if(ret < 0, 0);
603         }
604
605         /*  UI create time zone */
606         ad->data_tz =
607             setting_create_Gendial_field_def(scroller, &(ad->itc_2text_2),
608                                              setting_time_main_mouse_up_Gendial_list_cb,
609                                              ad, SWALLOW_Type_INVALID, NULL,
610                                              NULL, 0, "IDS_ST_BODY_TIME_ZONE",
611                                              time_zone_sub_str, NULL);
612         if (ad->data_tz) {
613                 ad->data_tz->userdata = ad;
614         } else {
615                 SETTING_TRACE_ERROR("ad->data_tz is NULL");
616         }
617
618         /* create DATE_AND_TIME */
619         /* DATE_AND_TIME */
620         ret =
621             setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224,
622                                     &value, &err);
623         if (ret == SETTING_RETURN_FAIL) {
624                 value = VCONFKEY_TIME_FORMAT_12;
625         }
626         bool is_12hours = (value == VCONFKEY_TIME_FORMAT_12 ? TRUE : FALSE);
627         char *date_format_str[] = { "%d %b %Y", "%b %d %Y", "%Y %b %d", "%Y %d %b" };
628         ret =
629             setting_get_int_slp_key(INT_SLP_SETTING_DATE_FORMAT, &value, &err);
630         if (ret == SETTING_RETURN_FAIL)
631                 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
632
633         /*  UI format string */
634         ad->data_time =
635             setting_create_Gendial_field_def(scroller, &(ad->itc_layout),
636                                              setting_time_main_mouse_up_Gendial_list_cb,
637                                              ad, SWALLOW_Type_LAYOUT_DATEFIELD,
638                                              NULL, NULL, is_12hours,
639                                              "IDS_ST_BODY_DATE_AND_TIME",
640                                              date_format_str[value],
641                                              setting_time_main_datefield_change_cb);
642         if (ad->data_time) {
643                 ad->data_time->userdata = ad;
644                 ad->data_time->isItemDisableFlag = auto_flag;
645         } else {
646                 SETTING_TRACE_ERROR("ad->data_time is NULL");
647         }
648         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL,
649                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
650         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
651
652         /*  UI create "Time format" */
653         setting_enable_expandable_genlist(scroller, ad,
654                                           __setting_time_main_exp_cb, NULL);
655
656         char* pa_time_format = get_pa_time_format_str();
657         ad->data_time_fmt = setting_create_Gendial_exp_parent_field(scroller,
658                                                     &(ad->itc_2text_3_parent),
659                                                     setting_time_main_mouse_up_Gendial_list_cb,
660                                                     ad, SWALLOW_Type_INVALID,
661                                                     "IDS_ST_BODY_TIME_FORMAT",
662                                                     pa_time_format);
663         if (ad->data_time_fmt) {
664                 ad->data_time_fmt->int_slp_setting_binded =
665                         INT_SLP_SETTING_REGIONFORMAT_TIME1224;
666         } else {
667                 SETTING_TRACE_ERROR("ad->data_time_fmt is NULL");
668         }
669         G_FREE(pa_time_format);
670
671 #if APPLIED_DATATIME_DATA_FORMAT
672         char* pa_date_format = get_pa_date_format_str();
673         ad->data_date_fmt = setting_create_Gendial_exp_parent_field(scroller,
674                                                     &(ad->itc_2text_3_parent),
675                                                     setting_time_main_mouse_up_Gendial_list_cb,
676                                                     ad, SWALLOW_Type_INVALID,
677                                                     "IDS_ST_BODY_DATE_FORMAT",
678                                                     pa_time_format);
679         if (ad->data_date_fmt) {
680                 ad->data_date_fmt->int_slp_setting_binded =
681                         INT_SLP_SETTING_DATE_FORMAT;
682         } else {
683                 SETTING_TRACE_ERROR("ad->data_date_fmt is NULL");
684         }
685         G_FREE(pa_date_format);
686 #endif
687
688 #if APPLIED_DATATIME_FIRSTDAY_WEEK
689     char* pa_week = get_pa_week_format_str();
690         ad->data_firstday_week_fmt =
691             setting_create_Gendial_exp_parent_field(scroller,
692                                                     &(ad->itc_2text_3_parent),
693                                                     setting_time_main_mouse_up_Gendial_list_cb,
694                                                     ad, SWALLOW_Type_INVALID,
695                                                     "IDS_CLD_BODY_FIRST_DAY_OF_WEEK",
696                                                     pa_week);
697         if (ad->data_firstday_week_fmt) {
698                 ad->data_firstday_week_fmt->int_slp_setting_binded =
699                         INT_SLP_SETTING_WEEK_FORMAT;
700         } else {
701                 SETTING_TRACE_ERROR("ad->data_firstday_week_fmt is NULL");
702         }
703     G_FREE(pa_week);
704 #endif
705
706         item = elm_genlist_item_append(scroller, &itc_bottom_seperator, NULL, NULL,
707                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
708         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
709
710         if (auto_flag)
711         {
712                 if (ad->data_tz) {
713                         setting_disable_genlist_item(ad->data_tz->item);
714                 }
715                 if (ad->data_time) {
716                         setting_disable_genlist_item(ad->data_time->item);
717                 }
718         }
719
720         setting_view_time_main.is_create = 1;
721
722 #if TIME_CHANGED_HEYNOTY_CALLBACK
723         /* ---------------------------------------------------------------- */
724         /*  heynoti registration */
725         /* ---------------------------------------------------------------- */
726         ad->noti_fd = heynoti_init();
727
728         if (ad->noti_fd == -1) {
729                 SETTING_TRACE("heynoti_init FAIL");
730                 return SETTING_RETURN_FAIL;
731         } else {
732                 SETTING_TRACE("heynoti_init OK");
733         }
734
735         if (-1 == heynoti_subscribe(ad->noti_fd, SETTING_TIME_CHANGED, time_changed_callback, (void *)ad)) {
736                 SETTING_TRACE("heynoti_subscribe FAIL");
737         } else {
738                 SETTING_TRACE("heynoti_subscribe OK");
739         }
740
741         if (-1 == heynoti_attach_handler(ad->noti_fd)) {
742                 SETTING_TRACE("heynoti_attach_handler FAIL");
743         } else {
744                 SETTING_TRACE("heynoti_attach_handler OK");
745         }
746 #endif
747
748         /*  register vconf key to get event callback for converting 12/24H */
749         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
750                                      setting_time_main_int_vconf_change_cb, ad);
751         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
752                          (char *)VCONFKEY_REGIONFORMAT_TIME1224, ret);
753
754         /*  register vconf key to get event callback for date format */
755         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DATE_FORMAT_INT,
756                                      setting_time_main_int_vconf_change_cb, ad);
757         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
758                          (char *)VCONFKEY_SETAPPL_DATE_FORMAT_INT, ret);
759
760         /* to avoid update time-diff, every per 3s to update. */
761         ad->update_timer = ecore_timer_add(3, (Ecore_Task_Cb) __refresh_date_timer, ad);
762
763         return SETTING_RETURN_SUCCESS;
764 }
765
766 static int setting_time_main_destroy(void *cb)
767 {
768         SETTING_TRACE_BEGIN;
769         /* error check */
770         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
771
772         SettingTimeUG *ad = (SettingTimeUG *) cb;
773
774 #if TIME_CHANGED_HEYNOTY_CALLBACK
775         /* Closing heynoti */
776         if (ad->noti_fd != -1)//first need to check the fd validation
777         {
778                 heynoti_unsubscribe(ad->noti_fd, SETTING_TIME_CHANGED, time_changed_callback);
779                 //heynoti_detach_handler() will be done in heynoti_close().
780                 heynoti_close(ad->noti_fd);
781                 ad->noti_fd = -1;
782         }
783 #endif
784         if (ad->update_timer) {
785                 ecore_timer_del(ad->update_timer);
786                 ad->update_timer = NULL;
787         }
788
789 #if PROGRESS_POPUP_SHOW
790         if (ad->pop_progress) {
791                 evas_object_del(ad->pop_progress);
792                 ad->pop_progress = NULL;
793         }
794 #endif
795
796         if (ad->update_timezone_idler) {
797                 ecore_idler_del(ad->update_timezone_idler);
798                 ad->update_timezone_idler = NULL;
799         }
800         if (ad->refresh_time_idler) {
801                 ecore_idler_del(ad->refresh_time_idler);
802                 ad->refresh_time_idler = NULL;
803         }
804         vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
805                                  setting_time_main_int_vconf_change_cb);
806         vconf_ignore_key_changed(VCONFKEY_SETAPPL_DATE_FORMAT_INT,
807                                  setting_time_main_int_vconf_change_cb);
808
809         if (ad->ly_main != NULL) {
810                 evas_object_del(ad->ly_main);
811                 setting_view_time_main.is_create = 0;
812         }
813
814         return SETTING_RETURN_SUCCESS;
815 }
816
817 static int setting_time_main_update(void *cb)
818 {
819         SETTING_TRACE_BEGIN;
820         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
821
822         SettingTimeUG *ad = (SettingTimeUG *) cb;
823
824         if (ad->ly_main != NULL) {
825                 evas_object_show(ad->ly_main);
826                 // update timezone
827                 if (ad->update_timezone_idler) {
828                         ecore_idler_del(ad->update_timezone_idler);
829                         ad->update_timezone_idler = NULL;
830                 }
831                 ad->update_timezone_idler =
832                     ecore_idler_add((Ecore_Task_Cb) __update_timezone_idler, ad);
833                 // update time
834                 if (ad->refresh_time_idler) {
835                         ecore_idler_del(ad->refresh_time_idler);
836                         ad->refresh_time_idler = NULL;
837                 }
838                 ad->refresh_time_idler = ecore_idler_add(setting_update_datefield, ad);
839         }
840         return SETTING_RETURN_SUCCESS;
841 }
842
843 static int setting_time_main_cleanup(void *cb)
844 {
845         SETTING_TRACE_BEGIN;
846         return SETTING_RETURN_SUCCESS;
847 }
848
849
850 /* ***************************************************
851  **
852  **general func
853  **
854  ****************************************************/
855
856 int setting_time_check_automatic_time_update_state()
857 {
858         SETTING_TRACE_BEGIN;
859         int bvalue = 0;
860         int ret = 0;
861         int err = 0;
862
863         ret =
864             setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
865                                      &bvalue, &err);
866
867         return bvalue;
868
869 }
870
871 static int __setting_set_city_tzone(const char* pTZPath)
872 {
873         // let's get city & timezone string from pTZPath
874         // 1. city name
875         char szTimezone[GMT_BUF_SIZE] = {0,};
876         char* pStr = strdup(pTZPath);
877
878         // 2. timezone string +/-<n> ex. +9, -1
879         get_gmt_offset(szTimezone, GMT_BUF_SIZE);
880
881         int ret = vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_INT, szTimezone);
882         if (ret == -1) {
883                 FREE(pStr);
884                 SETTING_TRACE("set vconf failed");
885                 return FALSE;
886         }
887
888         // set timezone_id
889         /** @todo replace with vconf ID */
890         ret = vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_ID, pStr);
891         FREE(pStr);
892
893         return TRUE;
894 }
895
896 static void
897 setting_time_main_launch_worldclock_result_ug_cb(ui_gadget_h ug,
898                                                  service_h result, void *priv)
899 {
900         SETTING_TRACE_BEGIN;
901         /* error check */
902         retm_if(priv == NULL, "Data parameter is NULL");
903
904         SettingTimeUG *ad = (SettingTimeUG *) priv;
905
906         char *city = NULL;
907         char *tzpath = NULL;
908         service_get_extra_data(result, "city", &city);
909         service_get_extra_data(result, "tzpath", &tzpath);
910
911         if (!tzpath) {
912                 SETTING_TRACE("tzpath from worldclock UG is null.");
913                 return;
914         } else {
915                 SETTING_TRACE("tzpath : %s", tzpath );
916         }
917
918         /* --------------------------------------------------------------------- */
919         char tz_path[MAX_COMMON_BUFFER_LEN / 4 + 1];
920         safeCopyStr(tz_path, SETTING_TIME_ZONEINFO_PATH, MAX_COMMON_BUFFER_LEN / 4);
921         g_strlcat(tz_path, tzpath, sizeof(tz_path));
922         SETTING_TRACE("full tz_path:%s", tz_path);
923
924         int ret = sysman_set_timezone(tz_path);
925         if (ret < 0) {
926                 SETTING_TRACE("tzpath is not valid.");
927                 return;
928         } else
929                 SETTING_TRACE("sysman_set_timezone - successful : %s \n", tz_path);
930
931         ret = vconf_set_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT, city);
932         setting_retm_if(ret != 0, "set vconf failed");
933
934         // parse city and GMT offset from tzpath and system time property
935         // set the strings in vconf which will be used while updating display of timezone
936         if (!__setting_set_city_tzone(tzpath)) {
937                 SETTING_TRACE("__setting_set_city_tzone ERROR")
938                 return;
939         }
940
941         // update the display for timezone
942         setting_update_timezone(ad);
943         heynoti_publish(SETTING_TIME_CHANGED);
944         SETTING_TRACE_END;
945 }
946
947 void setting_time_main_launch_worldclock_destroy_ug_cb(ui_gadget_h ug,
948                                                        void *priv)
949 {
950         SETTING_TRACE_BEGIN;
951         /* if(ug) ug_destroy(ug); */
952         /* restore the '<-' button on the navigate bar */
953         ret_if(!priv);
954         SettingTimeUG *ad = (SettingTimeUG *) priv;
955         if (ug) {
956                 ug_destroy(ug);
957                 ad->ug_loading = NULL;
958         }
959         Elm_Object_Item *navi_it = elm_naviframe_top_item_get(ad->navi_bar);
960         ret_if(!navi_it);
961         Evas_Object *back_btn = elm_object_item_part_content_get(navi_it, "prev_btn");
962
963         if (back_btn != NULL) {
964                 elm_object_style_set(back_btn, NAVI_BACK_BUTTON_STYLE); /* take into effect */
965         }
966
967 }
968
969 void setting_time_main_launch_worldclock_layout_ug_cb(ui_gadget_h ug,
970                                                       enum ug_mode mode,
971                                                       void *priv)
972 {
973         SETTING_TRACE_BEGIN;
974         SettingTimeUG *ad = (SettingTimeUG *) priv;
975         Evas_Object *base;
976
977         if (!priv)
978                 return;
979
980         base = (Evas_Object *) ug_get_layout(ug);
981         if (!base)
982                 return;
983
984         switch (mode) {
985         case UG_MODE_FULLVIEW:
986                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
987                                                  EVAS_HINT_EXPAND);
988                 elm_win_resize_object_add(ad->win_get, base);
989                 evas_object_show(base);
990                 break;
991         default:
992                 break;
993         }
994         SETTING_TRACE_END;
995 }
996
997
998 void setting_time_main_launch_worldclock_sg(void *data)
999 {
1000         SETTING_TRACE_BEGIN;
1001         /* error check */
1002         retm_if(data == NULL, "Data parameter is NULL");
1003
1004         SettingTimeUG *ad = (SettingTimeUG *) data;
1005
1006         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
1007         setting_retm_if(!cbs, "calloc failed");
1008         cbs->layout_cb = setting_time_main_launch_worldclock_layout_ug_cb;
1009         cbs->result_cb = setting_time_main_launch_worldclock_result_ug_cb;
1010         cbs->destroy_cb = setting_time_main_launch_worldclock_destroy_ug_cb;
1011         cbs->priv = (void *)ad;
1012
1013         SETTING_TRACE("to load ug[%s]", "worldclock-efl");
1014         ad->ug_loading =
1015             ug_create(ad->ug, "worldclock-efl", UG_MODE_FULLVIEW, NULL, cbs);
1016         if (NULL == ad->ug_loading) {   /* error handling */
1017         }
1018
1019         FREE(cbs);
1020         return;
1021 }
1022
1023
1024 Eina_Bool setting_update_datefield(void *cb)
1025 {
1026         SETTING_TRACE_BEGIN;
1027         retvm_if(cb == NULL, EINA_FALSE, "Data parameter is NULL");
1028         SettingTimeUG *ad = (SettingTimeUG *) cb;
1029
1030         __refresh_date_timer(ad);
1031         ad->refresh_time_idler = NULL;
1032         return 0;
1033 }
1034
1035 /* ***************************************************
1036  **
1037  **call back func
1038  **
1039  ****************************************************/
1040
1041 void
1042 setting_time_main_click_softkey_left_cb(void *data, Evas_Object *obj,
1043                                         void *event_info)
1044 {
1045         SETTING_TRACE_BEGIN;
1046         retm_if(data == NULL, "Data parameter is NULL");
1047
1048         SettingTimeUG *ad = (SettingTimeUG *) data;
1049
1050         /* Send destroy request */
1051         ug_destroy_me(ad->ug);
1052 }
1053
1054 /* ***************************************************
1055  **
1056  **call back func
1057  **
1058  ****************************************************/
1059
1060 void
1061 setting_time_main_click_softkey_caller_exist_left_cb(void *data,
1062                                                      Evas_Object *obj,
1063                                                      void *event_info)
1064 {
1065         SETTING_TRACE_BEGIN;
1066         /* error check */
1067         retm_if(data == NULL, "Data parameter is NULL");
1068
1069         SettingTimeUG *ad = (SettingTimeUG *) data;
1070
1071         /*  Create Bundle and send message */
1072         service_h svc;
1073         if (service_create(&svc))
1074                 return;
1075
1076         service_add_extra_data(svc, "result", "lbutton_click");
1077         ug_send_result(ad->ug, svc);
1078
1079         service_destroy(svc);
1080
1081         /* Send destroy request */
1082         ug_destroy_me(ad->ug);
1083 }
1084
1085 void
1086 setting_time_main_click_softkey_caller_exist_right_cb(void *data,
1087                                                       Evas_Object *obj,
1088                                                       void *event_info)
1089 {
1090         SETTING_TRACE_BEGIN;
1091         /* error check */
1092         retm_if(data == NULL, "Data parameter is NULL");
1093
1094         SettingTimeUG *ad = (SettingTimeUG *) data;
1095
1096         /*  Create Bundle and send message */
1097         service_h svc;
1098         if (service_create(&svc))
1099                 return;
1100
1101         service_add_extra_data(svc, "result", "rbutton_click");
1102         ug_send_result(ad->ug, svc);
1103
1104         service_destroy(svc);
1105
1106         /* Send destroy request */
1107         ug_destroy_me(ad->ug);
1108 }
1109
1110 static void setting_time_reboot_resp_cb(void *data, Evas_Object *obj,
1111                                         void *event_info)
1112 {
1113         SETTING_TRACE_BEGIN;
1114         int response_type = btn_type(obj);
1115         if (POPUP_RESPONSE_OK == response_type) {
1116                 setting_time_main_btn_update_ok_cb(data);
1117         } else if (POPUP_RESPONSE_CANCEL == response_type) {
1118                 setting_time_main_btn_update_cancel_cb(data);
1119         }
1120
1121         SettingTimeUG *ad = data;
1122         if (ad->pop)
1123         {
1124                 evas_object_del(ad->pop);
1125                 ad->pop = NULL;
1126         }
1127 }
1128
1129 static void
1130 setting_time_main_chk_btn_cb(void *data, Evas_Object *obj, void *event_info)
1131 {
1132         SETTING_TRACE_BEGIN;
1133         /* error check */
1134         retm_if(data == NULL, "Data parameter is NULL");
1135         SettingTimeUG *ad = NULL;
1136         Setting_GenGroupItem_Data *list_item =
1137             (Setting_GenGroupItem_Data *) data;
1138         ad = list_item->userdata;
1139         list_item->chk_status = elm_check_state_get(obj);       /*  for genlist update status */
1140         if (ad->data_auto)
1141                 ad->chk_auto = ad->data_auto->eo_check;
1142         if (ad->data_dst)
1143                 ad->chk_dst = ad->data_dst->eo_check;
1144
1145         if (ad->data_time) {
1146                 elm_object_focus_set(ad->data_time->eo_check, EINA_FALSE);
1147         }
1148
1149         int err;
1150         if (obj == ad->chk_auto) {
1151                 int bvalue;
1152                 setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1153                                          &bvalue, &err);
1154                 if (bvalue) {
1155                         (void) setting_set_bool_slp_key (BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE, SETTING_ON_OFF_BTN_OFF, &err);
1156                         elm_check_state_set(ad->chk_auto, 0);
1157
1158                         if (ad->data_tz) {
1159                                 setting_enable_genlist_item(ad->data_tz->item);
1160                         }
1161
1162                         if (ad->data_time) {
1163                                 ad->data_time->isItemDisableFlag = FALSE;
1164                                 setting_enable_evas_object(ad->data_time->eo_check);
1165                                 setting_enable_genlist_item(ad->data_time->item);
1166                         }
1167
1168                 } else {
1169                         ad->pop =
1170                             setting_create_popup_with_btn(ad, ad->win_get, NULL,
1171                                                  _("IDS_ST_POP_RESTART_PHONE_TO_USE_AUTOMATIC_UPDATE_Q"),
1172                                                  setting_time_reboot_resp_cb,
1173                                                  0, 2,
1174                                                  dgettext("sys_string",
1175                                                        "IDS_COM_SK_OK"),
1176                                                  _("IDS_COM_SK_CANCEL"));
1177                 }
1178         }
1179 }
1180
1181
1182 static void setting_time_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj,
1183                                            void *event_info)
1184 {
1185         SETTING_TRACE_BEGIN;
1186         /* error check */
1187         setting_retm_if(data == NULL, "Data parameter is NULL");
1188
1189         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
1190         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
1191         elm_genlist_item_selected_set(item, 0);
1192         Setting_GenGroupItem_Data *list_item =
1193             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
1194
1195         SettingTimeUG *ad = (SettingTimeUG *) data;
1196         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
1197
1198         if (ad->data_time && 0 != safeStrCmp("IDS_ST_BODY_DATE_AND_TIME", list_item->keyStr)) {
1199                 elm_object_focus_set(ad->data_time->eo_check, EINA_FALSE);
1200         }
1201
1202         if (!safeStrCmp("IDS_ST_BODY_TIME_ZONE", list_item->keyStr)) {
1203                 ad->is_datefield_selected = EINA_FALSE;
1204                 if (1 == setting_time_check_automatic_time_update_state()) {
1205                         setting_create_simple_popup(ad, ad->win_get,
1206                                                     NULL, _(AUTO_TIME_UPDATE_ON));
1207                 } else {
1208                         setting_time_main_launch_worldclock_sg(ad);
1209                 }
1210
1211         } else if (!safeStrCmp("IDS_ST_BODY_DATE_AND_TIME", list_item->keyStr)) {
1212                 ad->is_datefield_selected = EINA_TRUE;
1213                 if (1 == setting_time_check_automatic_time_update_state()) {
1214                         setting_create_simple_popup(ad, ad->win_get,
1215                                                     NULL, _(AUTO_TIME_UPDATE_ON));
1216                 } else {
1217                         /* setting_view_change(&setting_view_time_main, &setting_view_time_time, ad); */
1218                 }
1219         } else if (!safeStrCmp("IDS_ST_BODY_AUTOMATIC_UPDATE", list_item->keyStr)) {
1220                 ad->is_datefield_selected = EINA_FALSE;
1221                 int old_status = elm_check_state_get(list_item->eo_check);
1222                 /* new status */
1223                 list_item->chk_status = !old_status;
1224                 elm_check_state_set(list_item->eo_check, list_item->chk_status);
1225                 int err = 0;
1226                 if (!list_item->chk_status) {
1227                         (void)
1228                             setting_set_bool_slp_key
1229                             (BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1230                              SETTING_ON_OFF_BTN_OFF, &err);
1231
1232                         if (ad->data_tz) {
1233                                 setting_enable_genlist_item(ad->data_tz->item);
1234                         }
1235
1236                         if (ad->data_time) {
1237                                 ad->data_time->isItemDisableFlag = FALSE;
1238                                 setting_enable_evas_object(ad->data_time->eo_check);
1239                                 setting_enable_genlist_item(ad->data_time->item);
1240                         }
1241
1242                 } else {
1243                         ad->pop =
1244                             setting_create_popup_with_btn(ad, ad->win_get, NULL,
1245                                                  _("IDS_ST_POP_RESTART_PHONE_TO_USE_AUTOMATIC_UPDATE_Q"),
1246                                                  setting_time_reboot_resp_cb,
1247                                                  0, 2,
1248                                                  dgettext("sys_string", "IDS_COM_SK_OK"),
1249                                                  _("IDS_COM_SK_CANCEL"));
1250                 }
1251
1252         } else if (!safeStrCmp("IDS_ST_BODY_TIME_FORMAT", list_item->keyStr)
1253                    || !safeStrCmp("IDS_ST_BODY_DATE_FORMAT", list_item->keyStr)
1254                    || !safeStrCmp("IDS_CLD_BODY_FIRST_DAY_OF_WEEK", list_item->keyStr)) {
1255                 ad->is_datefield_selected = EINA_FALSE;
1256                 bool status = !elm_genlist_item_expanded_get(item);
1257                 elm_genlist_item_expanded_set(item, status);
1258         }
1259 }
1260
1261
1262 void setting_time_main_btn_update_ok_cb(void *cb)
1263 {
1264         SETTING_TRACE_BEGIN;
1265         SettingTimeUG *ad = (SettingTimeUG *) cb;
1266         int err;                /* error check */
1267         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1268                                  SETTING_ON_OFF_BTN_ON, &err);
1269         elm_check_state_set(ad->data_auto->eo_check, 1);
1270         ad->data_auto->chk_status = 1;
1271
1272         sysman_request_reboot();
1273
1274         /*  We'd better check return value from sysman_request_reboot() function. */
1275         /*  It will return 0 on success or -1 if failed. */
1276 }
1277
1278
1279 void setting_time_main_btn_update_cancel_cb(void *cb)
1280 {
1281         SETTING_TRACE_BEGIN;
1282         SettingTimeUG *ad = (SettingTimeUG *) cb;
1283
1284         int err;                /* error check */
1285
1286         retm_if(cb == NULL, "Data parameter is NULL");
1287         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1288                                  SETTING_ON_OFF_BTN_OFF, &err);
1289         elm_check_state_set(ad->data_auto->eo_check, 0);
1290         ad->data_auto->chk_status = 0;
1291
1292 }
1293
1294
1295 static UChar *uastrcpy(const char *chars)
1296 {
1297         int len = 0;
1298         UChar *str = NULL;
1299         len = safeStrLen(chars);
1300         str = (UChar *) malloc(sizeof(UChar) *(len + 1));
1301         if (!str)
1302                 return NULL;
1303         u_uastrcpy(str, chars);
1304         return str;
1305 }
1306
1307
1308 static int get_timezone_isdst(int* isdst)
1309 {
1310         SETTING_TRACE_BEGIN;
1311     struct tm *ts;
1312     time_t ctime;
1313
1314     enum { BUFFERSIZE = 1024 };
1315     char buf[BUFFERSIZE];
1316     ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
1317
1318     if (len != -1) {
1319         buf[len] = '\0';
1320     }
1321     else {
1322         /* handle error condition */
1323     }
1324
1325     ctime = time(NULL);
1326     ts = localtime(&ctime);
1327         if ( ! ts ) {
1328                 return SETTING_GENERAL_ERR_NULL_DATA_PARAMETER;
1329         }
1330
1331     SETTING_TRACE(" == tz_path = %s", buf);
1332     SETTING_TRACE(" == isdst = %d\n", ts->tm_isdst);
1333         *isdst = ts->tm_isdst;
1334
1335         return SETTING_RETURN_SUCCESS;
1336 }
1337
1338
1339 static void ICU_set_timezone(const char *timezone)
1340 {
1341         SETTING_TRACE("ICU_set_timezone = %s ", timezone);
1342         UErrorCode ec = U_ZERO_ERROR;
1343         UChar *str = uastrcpy(timezone);
1344
1345         ucal_setDefaultTimeZone(str, &ec);
1346         if (U_SUCCESS(ec)) {
1347                 SETTING_TRACE("ucal_setDefaultTimeZone() SUCCESS ");
1348         } else {
1349                 SETTING_TRACE("ucal_setDefaultTimeZone() FAILED : %s ",
1350                               u_errorName(ec));
1351         }
1352         FREE(str);
1353 }
1354
1355
1356 static void setting_time_main_controlbar_lbtn_label_set(
1357                                                         Evas_Object *controlbar,
1358                                                         char *label)
1359 {
1360         SETTING_TRACE_BEGIN;
1361         setting_retm_if(controlbar == NULL, "controlbar parameter is NULL");
1362         setting_retm_if(label == NULL, "label parameter is NULL");
1363
1364         Evas_Object *eo_lbtn = NULL;
1365         Elm_Object_Item *first_item = NULL;
1366
1367         first_item = elm_toolbar_first_item_get(controlbar);
1368         setting_retm_if(first_item == NULL, "get first_item failed");
1369
1370         eo_lbtn = elm_object_item_part_content_get(first_item, "object");
1371         setting_retm_if(eo_lbtn == NULL, "get eo_lbtn failed");
1372
1373         elm_object_text_set(eo_lbtn, label);
1374 }
1375
1376
1377 /**
1378  * ex) timezone : Asia/Seoul --> returns "+09"
1379  *
1380  * char outstr2[4];
1381  */
1382 static void get_gmt_offset(char *str_buf, int size)
1383 {
1384         SETTING_TRACE(" before GMT processing : %s", str_buf);
1385         // timezone string +/-<n> ex. +9, -1
1386         time_t t = time(0);     // get unix time. sec.
1387
1388         struct tm* data;
1389         data = localtime(&t);           // save time as structure.
1390         setting_retm_if(!data, "data is NULL");
1391         data->tm_isdst = 0;                     // summer time, not applied.
1392         time_t a = mktime(data);
1393
1394         data = gmtime(&a);
1395         data->tm_isdst = 0;                     // summer time, not applied.
1396         time_t b = mktime(data);
1397
1398         int gmtoffset_hour = (a-b)/3600;        // result : hour.
1399         int gmtoffset_min = ((a-b)%3600)/60;    // result : min.
1400         if(gmtoffset_min != 0) {
1401                 gmtoffset_min = 30;
1402         }
1403         
1404         snprintf(str_buf, size, "%+d:%02u", gmtoffset_hour, gmtoffset_min);
1405         SETTING_TRACE("szTimezone is of a valid format: GMT: %s", str_buf);
1406 }
1407
1408
1409 // automatic
1410 static char* get_timezone_str()
1411 {
1412     SETTING_TRACE_BEGIN;
1413
1414     enum { BUFFERSIZE = 1024 };
1415     char buf[BUFFERSIZE];
1416     ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
1417
1418     if (len != -1) {
1419         buf[len] = '\0';
1420     }
1421     else {
1422         /* handle error condition */
1423     }
1424     return g_strdup(buf+20);            //Asia/Seoul
1425 }
1426
1427 static char* get_city_name_result()
1428 {
1429         return s_result;
1430 }
1431
1432
1433 // in_str = "Asia/Seoul"
1434 static bool get_city_name(char* tzname)
1435 {
1436         sqlite3 *pSQLite3 = NULL;
1437         char    *szErrMsg = NULL;
1438
1439         int rst = sqlite3_open( "/opt/dbspace/.worldclock.db", &pSQLite3);
1440         if ( rst )
1441         {
1442                 printf( "Can't open database: %s\n", sqlite3_errmsg( pSQLite3 ));
1443                 sqlite3_close( pSQLite3 );
1444                 pSQLite3 = NULL;
1445                 return false;
1446         }
1447         else
1448         {
1449                 printf("Database opened!!\n");
1450                 char query_str[256];
1451                 snprintf(query_str, 256, "SELECT city FROM city_table where tz_path=\"%s\"", tzname);
1452                 printf("%s \n", query_str);
1453                 rst = sqlite3_exec(pSQLite3, query_str, query_callback, 0, &szErrMsg);
1454         }
1455
1456         //객체해제
1457         sqlite3_free( szErrMsg );
1458         sqlite3_close( pSQLite3 );
1459         printf("Database close!!\n");
1460         return true;
1461 }
1462
1463 static int query_callback(void *NotUsed, int argc, char **argv, char **azColName)
1464 {
1465     int i = 0;
1466     printf("%s\n", argv[i] ? argv[i] : "NULL");
1467         s_result = g_strdup(argv[i] ? argv[i] : "NULL");
1468     return 0;
1469 };
1470
1471
1472 #if 0                   /* example */
1473 int main(int argc, char* argv[])
1474 {
1475         g_type_init ();
1476         int ret;
1477         // exporting - current status
1478         if ( (argc == 2) && (0 == strcmp(argv[1], "export"))) {
1479                         setting_export();
1480         }
1481         else if ( (argc == 2) && (0 == strcmp(argv[1], "timezone_init"))) {
1482
1483                 //////////////////////////////////////////////////////////////////////////
1484                 char* tz_path_zone_city = get_timezone_str();
1485                 bool ret = get_city_name(tz_path_zone_city);
1486                 if (ret) {
1487                         printf(">>>>>> RESULT : %s \n", get_city_name_result());
1488                 }
1489                 //////////////////////////////////////////////////////////////////////////
1490         }
1491         else {
1492                 // cfg create
1493                 // TRUE or FALSE
1494                 ret = setting_cfg_create();
1495         }
1496         return 0;
1497 }
1498 #endif
1499
1500
1501