[Bug] setting > date and time: modify function get_gmt_offset to format the time .
[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         ad->data_auto =
551             setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon),
552                                              setting_time_main_mouse_up_Gendial_list_cb,
553                                              ad, SWALLOW_Type_1TOGGLE, NULL,
554                                              NULL, value,
555                                              "IDS_ST_BODY_AUTOMATIC_UPDATE", NULL,
556                                              setting_time_main_chk_btn_cb);
557         if (ad->data_auto) {
558                 ad->data_auto->userdata = ad;
559         } else {
560                 SETTING_TRACE_ERROR("ad->data_auto is NULL");
561         }
562
563         setting_create_Gendial_field_def(scroller, &itc_multiline_text, NULL,
564                                          ad, SWALLOW_Type_LAYOUT_SPECIALIZTION,
565                                          NULL, NULL, 0, SETTING_TIME_AUTO_UPDATE_DESC, NULL,
566                                          NULL);
567
568         appcore_set_i18n("worldclock", SETTING_TIME_SHARE_LOCAL_PATH);
569
570         char time_zone_sub_str[CITY_BUF_SIZE + GMT_BUF_SIZE + 3] = { 0, };
571         char *pa_timezone = NULL;
572         char *pa_city = NULL;
573
574         pa_city = vconf_get_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT);   // IDS_WCL_BODY_CITYNAME_SEOUL
575         pa_timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT);             // "Asia/Seoul"
576
577         if ((pa_timezone != NULL) && (_(pa_city) != NULL)) {
578
579                 // don't use hard coded value
580                 // get value from current timezone
581
582                 char str_buf[GMT_BUF_SIZE] = {0, };
583                 get_gmt_offset(str_buf, GMT_BUF_SIZE);
584
585                 char* timezone_str = get_timezone_str();
586
587                 bool ret = get_city_name(timezone_str);
588                 if (ret) {
589                         SETTING_TRACE(">>>>>> RESULT : %s", get_city_name_result());
590                 }
591                 char* cityname_id = get_city_name_result();
592
593                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT %s", _(cityname_id), _(str_buf));
594                 FREE(pa_city);
595                 FREE(pa_timezone);
596                 retv_if(ret < 0, 0);
597         } else {
598                 // default code
599                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT +09", _("IDS_WCL_BODY_CITYNAME_SEOUL"));
600                 retv_if(ret < 0, 0);
601         }
602
603         /*  UI create time zone */
604         ad->data_tz =
605             setting_create_Gendial_field_def(scroller, &(ad->itc_2text_2),
606                                              setting_time_main_mouse_up_Gendial_list_cb,
607                                              ad, SWALLOW_Type_INVALID, NULL,
608                                              NULL, 0, "IDS_ST_BODY_TIME_ZONE",
609                                              time_zone_sub_str, NULL);
610         if (ad->data_tz) {
611                 ad->data_tz->userdata = ad;
612         } else {
613                 SETTING_TRACE_ERROR("ad->data_tz is NULL");
614         }
615
616         /* create DATE_AND_TIME */
617         /* DATE_AND_TIME */
618         ret =
619             setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224,
620                                     &value, &err);
621         if (ret == SETTING_RETURN_FAIL) {
622                 value = VCONFKEY_TIME_FORMAT_12;
623         }
624         bool is_12hours = (value == VCONFKEY_TIME_FORMAT_12 ? TRUE : FALSE);
625         char *date_format_str[] = { "%d %b %Y", "%b %d %Y", "%Y %b %d", "%Y %d %b" };
626         ret =
627             setting_get_int_slp_key(INT_SLP_SETTING_DATE_FORMAT, &value, &err);
628         if (ret == SETTING_RETURN_FAIL)
629                 value = SETTING_DATE_FORMAT_DD_MM_YYYY;
630
631         /*  UI format string */
632         ad->data_time =
633             setting_create_Gendial_field_def(scroller, &(ad->itc_layout),
634                                              setting_time_main_mouse_up_Gendial_list_cb,
635                                              ad, SWALLOW_Type_LAYOUT_DATEFIELD,
636                                              NULL, NULL, is_12hours,
637                                              "IDS_ST_BODY_DATE_AND_TIME",
638                                              date_format_str[value],
639                                              setting_time_main_datefield_change_cb);
640         if (ad->data_time) {
641                 ad->data_time->userdata = ad;
642                 ad->data_time->isItemDisableFlag = auto_flag;
643         } else {
644                 SETTING_TRACE_ERROR("ad->data_time is NULL");
645         }
646         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL,
647                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
648         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
649
650         /*  UI create "Time format" */
651         setting_enable_expandable_genlist(scroller, ad,
652                                           __setting_time_main_exp_cb, NULL);
653
654         char* pa_time_format = get_pa_time_format_str();
655         ad->data_time_fmt = setting_create_Gendial_exp_parent_field(scroller,
656                                                     &(ad->itc_2text_3_parent),
657                                                     setting_time_main_mouse_up_Gendial_list_cb,
658                                                     ad, SWALLOW_Type_INVALID,
659                                                     "IDS_ST_BODY_TIME_FORMAT",
660                                                     pa_time_format);
661         if (ad->data_time_fmt) {
662                 ad->data_time_fmt->int_slp_setting_binded =
663                         INT_SLP_SETTING_REGIONFORMAT_TIME1224;
664         } else {
665                 SETTING_TRACE_ERROR("ad->data_time_fmt is NULL");
666         }
667         G_FREE(pa_time_format);
668
669 #if APPLIED_DATATIME_DATA_FORMAT
670         char* pa_date_format = get_pa_date_format_str();
671         ad->data_date_fmt = setting_create_Gendial_exp_parent_field(scroller,
672                                                     &(ad->itc_2text_3_parent),
673                                                     setting_time_main_mouse_up_Gendial_list_cb,
674                                                     ad, SWALLOW_Type_INVALID,
675                                                     "IDS_ST_BODY_DATE_FORMAT",
676                                                     pa_time_format);
677         if (ad->data_date_fmt) {
678                 ad->data_date_fmt->int_slp_setting_binded =
679                         INT_SLP_SETTING_DATE_FORMAT;
680         } else {
681                 SETTING_TRACE_ERROR("ad->data_date_fmt is NULL");
682         }
683         G_FREE(pa_date_format);
684 #endif
685
686 #if APPLIED_DATATIME_FIRSTDAY_WEEK
687     char* pa_week = get_pa_week_format_str();
688         ad->data_firstday_week_fmt =
689             setting_create_Gendial_exp_parent_field(scroller,
690                                                     &(ad->itc_2text_3_parent),
691                                                     setting_time_main_mouse_up_Gendial_list_cb,
692                                                     ad, SWALLOW_Type_INVALID,
693                                                     "IDS_CLD_BODY_FIRST_DAY_OF_WEEK",
694                                                     pa_week);
695         if (ad->data_firstday_week_fmt) {
696                 ad->data_firstday_week_fmt->int_slp_setting_binded =
697                         INT_SLP_SETTING_WEEK_FORMAT;
698         } else {
699                 SETTING_TRACE_ERROR("ad->data_firstday_week_fmt is NULL");
700         }
701     G_FREE(pa_week);
702 #endif
703
704         item = elm_genlist_item_append(scroller, &itc_bottom_seperator, NULL, NULL,
705                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
706         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
707
708         if (auto_flag)
709         {
710                 if (ad->data_tz) {
711                         setting_disable_genlist_item(ad->data_tz->item);
712                 }
713                 if (ad->data_time) {
714                         setting_disable_genlist_item(ad->data_time->item);
715                 }
716         }
717
718         setting_view_time_main.is_create = 1;
719
720 #if TIME_CHANGED_HEYNOTY_CALLBACK
721         /* ---------------------------------------------------------------- */
722         /*  heynoti registration */
723         /* ---------------------------------------------------------------- */
724         ad->noti_fd = heynoti_init();
725
726         if (ad->noti_fd == -1) {
727                 SETTING_TRACE("heynoti_init FAIL");
728                 return SETTING_RETURN_FAIL;
729         } else {
730                 SETTING_TRACE("heynoti_init OK");
731         }
732
733         if (-1 == heynoti_subscribe(ad->noti_fd, SETTING_TIME_CHANGED, time_changed_callback, (void *)ad)) {
734                 SETTING_TRACE("heynoti_subscribe FAIL");
735         } else {
736                 SETTING_TRACE("heynoti_subscribe OK");
737         }
738
739         if (-1 == heynoti_attach_handler(ad->noti_fd)) {
740                 SETTING_TRACE("heynoti_attach_handler FAIL");
741         } else {
742                 SETTING_TRACE("heynoti_attach_handler OK");
743         }
744 #endif
745
746         /*  register vconf key to get event callback for converting 12/24H */
747         ret = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
748                                      setting_time_main_int_vconf_change_cb, ad);
749         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
750                          (char *)VCONFKEY_REGIONFORMAT_TIME1224, ret);
751
752         /*  register vconf key to get event callback for date format */
753         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DATE_FORMAT_INT,
754                                      setting_time_main_int_vconf_change_cb, ad);
755         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
756                          (char *)VCONFKEY_SETAPPL_DATE_FORMAT_INT, ret);
757
758         /* to avoid update time-diff, every per 3s to update. */
759         ad->update_timer = ecore_timer_add(3, (Ecore_Task_Cb) __refresh_date_timer, ad);
760
761         return SETTING_RETURN_SUCCESS;
762 }
763
764 static int setting_time_main_destroy(void *cb)
765 {
766         SETTING_TRACE_BEGIN;
767         /* error check */
768         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
769
770         SettingTimeUG *ad = (SettingTimeUG *) cb;
771
772 #if TIME_CHANGED_HEYNOTY_CALLBACK
773         /* Closing heynoti */
774         if (ad->noti_fd != -1)//first need to check the fd validation
775         {
776                 heynoti_unsubscribe(ad->noti_fd, SETTING_TIME_CHANGED, time_changed_callback);
777                 //heynoti_detach_handler() will be done in heynoti_close().
778                 heynoti_close(ad->noti_fd);
779                 ad->noti_fd = -1;
780         }
781 #endif
782         if (ad->update_timer) {
783                 ecore_timer_del(ad->update_timer);
784                 ad->update_timer = NULL;
785         }
786
787 #if PROGRESS_POPUP_SHOW
788         if (ad->pop_progress) {
789                 evas_object_del(ad->pop_progress);
790                 ad->pop_progress = NULL;
791         }
792 #endif
793
794         if (ad->update_timezone_idler) {
795                 ecore_idler_del(ad->update_timezone_idler);
796                 ad->update_timezone_idler = NULL;
797         }
798         if (ad->refresh_time_idler) {
799                 ecore_idler_del(ad->refresh_time_idler);
800                 ad->refresh_time_idler = NULL;
801         }
802         vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
803                                  setting_time_main_int_vconf_change_cb);
804         vconf_ignore_key_changed(VCONFKEY_SETAPPL_DATE_FORMAT_INT,
805                                  setting_time_main_int_vconf_change_cb);
806
807         if (ad->ly_main != NULL) {
808                 evas_object_del(ad->ly_main);
809                 setting_view_time_main.is_create = 0;
810         }
811
812         return SETTING_RETURN_SUCCESS;
813 }
814
815 static int setting_time_main_update(void *cb)
816 {
817         SETTING_TRACE_BEGIN;
818         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
819
820         SettingTimeUG *ad = (SettingTimeUG *) cb;
821
822         if (ad->ly_main != NULL) {
823                 evas_object_show(ad->ly_main);
824                 // update timezone
825                 if (ad->update_timezone_idler) {
826                         ecore_idler_del(ad->update_timezone_idler);
827                         ad->update_timezone_idler = NULL;
828                 }
829                 ad->update_timezone_idler =
830                     ecore_idler_add((Ecore_Task_Cb) __update_timezone_idler, ad);
831                 // update time
832                 if (ad->refresh_time_idler) {
833                         ecore_idler_del(ad->refresh_time_idler);
834                         ad->refresh_time_idler = NULL;
835                 }
836                 ad->refresh_time_idler = ecore_idler_add(setting_update_datefield, ad);
837         }
838         return SETTING_RETURN_SUCCESS;
839 }
840
841 static int setting_time_main_cleanup(void *cb)
842 {
843         SETTING_TRACE_BEGIN;
844         return SETTING_RETURN_SUCCESS;
845 }
846
847
848 /* ***************************************************
849  **
850  **general func
851  **
852  ****************************************************/
853
854 int setting_time_check_automatic_time_update_state()
855 {
856         SETTING_TRACE_BEGIN;
857         int bvalue = 0;
858         int ret = 0;
859         int err = 0;
860
861         ret =
862             setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
863                                      &bvalue, &err);
864
865         return bvalue;
866
867 }
868
869 static int __setting_set_city_tzone(const char* pTZPath)
870 {
871         // let's get city & timezone string from pTZPath
872         // 1. city name
873         char szTimezone[GMT_BUF_SIZE] = {0,};
874         char* pStr = strdup(pTZPath);
875
876         // 2. timezone string +/-<n> ex. +9, -1
877         get_gmt_offset(szTimezone, GMT_BUF_SIZE);
878
879         int ret = vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_INT, szTimezone);
880         if (ret == -1) {
881                 FREE(pStr);
882                 SETTING_TRACE("set vconf failed");
883                 return FALSE;
884         }
885
886         // set timezone_id
887         /** @todo replace with vconf ID */
888         ret = vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_ID, pStr);
889         FREE(pStr);
890
891         return TRUE;
892 }
893
894 static void
895 setting_time_main_launch_worldclock_result_ug_cb(ui_gadget_h ug,
896                                                  service_h result, void *priv)
897 {
898         SETTING_TRACE_BEGIN;
899         /* error check */
900         retm_if(priv == NULL, "Data parameter is NULL");
901
902         SettingTimeUG *ad = (SettingTimeUG *) priv;
903
904         char *city = NULL;
905         char *tzpath = NULL;
906         service_get_extra_data(result, "city", &city);
907         service_get_extra_data(result, "tzpath", &tzpath);
908
909         if (!tzpath) {
910                 SETTING_TRACE("tzpath from worldclock UG is null.");
911                 return;
912         } else {
913                 SETTING_TRACE("tzpath : %s", tzpath );
914         }
915
916         /* --------------------------------------------------------------------- */
917         char tz_path[MAX_COMMON_BUFFER_LEN / 4 + 1];
918         safeCopyStr(tz_path, SETTING_TIME_ZONEINFO_PATH, MAX_COMMON_BUFFER_LEN / 4);
919         g_strlcat(tz_path, tzpath, sizeof(tz_path));
920         SETTING_TRACE("full tz_path:%s", tz_path);
921
922         int ret = sysman_set_timezone(tz_path);
923         if (ret < 0) {
924                 SETTING_TRACE("tzpath is not valid.");
925                 return;
926         } else
927                 SETTING_TRACE("sysman_set_timezone - successful : %s \n", tz_path);
928
929         ret = vconf_set_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT, city);
930         setting_retm_if(ret != 0, "set vconf failed");
931
932         // parse city and GMT offset from tzpath and system time property
933         // set the strings in vconf which will be used while updating display of timezone
934         if (!__setting_set_city_tzone(tzpath)) {
935                 SETTING_TRACE("__setting_set_city_tzone ERROR")
936                 return;
937         }
938
939         // update the display for timezone
940         setting_update_timezone(ad);
941         heynoti_publish(SETTING_TIME_CHANGED);
942         SETTING_TRACE_END;
943 }
944
945 void setting_time_main_launch_worldclock_destroy_ug_cb(ui_gadget_h ug,
946                                                        void *priv)
947 {
948         SETTING_TRACE_BEGIN;
949         /* if(ug) ug_destroy(ug); */
950         /* restore the '<-' button on the navigate bar */
951         ret_if(!priv);
952         SettingTimeUG *ad = (SettingTimeUG *) priv;
953         if (ug) {
954                 ug_destroy(ug);
955                 ad->ug_loading = NULL;
956         }
957         Elm_Object_Item *navi_it = elm_naviframe_top_item_get(ad->navi_bar);
958         ret_if(!navi_it);
959         Evas_Object *back_btn = elm_object_item_part_content_get(navi_it, "prev_btn");
960
961         if (back_btn != NULL) {
962                 elm_object_style_set(back_btn, NAVI_BACK_BUTTON_STYLE); /* take into effect */
963         }
964
965 }
966
967 void setting_time_main_launch_worldclock_layout_ug_cb(ui_gadget_h ug,
968                                                       enum ug_mode mode,
969                                                       void *priv)
970 {
971         SETTING_TRACE_BEGIN;
972         SettingTimeUG *ad = (SettingTimeUG *) priv;
973         Evas_Object *base;
974
975         if (!priv)
976                 return;
977
978         base = (Evas_Object *) ug_get_layout(ug);
979         if (!base)
980                 return;
981
982         switch (mode) {
983         case UG_MODE_FULLVIEW:
984                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
985                                                  EVAS_HINT_EXPAND);
986                 elm_win_resize_object_add(ad->win_get, base);
987                 evas_object_show(base);
988                 break;
989         default:
990                 break;
991         }
992         SETTING_TRACE_END;
993 }
994
995
996 void setting_time_main_launch_worldclock_sg(void *data)
997 {
998         SETTING_TRACE_BEGIN;
999         /* error check */
1000         retm_if(data == NULL, "Data parameter is NULL");
1001
1002         SettingTimeUG *ad = (SettingTimeUG *) data;
1003
1004         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
1005         setting_retm_if(!cbs, "calloc failed");
1006         cbs->layout_cb = setting_time_main_launch_worldclock_layout_ug_cb;
1007         cbs->result_cb = setting_time_main_launch_worldclock_result_ug_cb;
1008         cbs->destroy_cb = setting_time_main_launch_worldclock_destroy_ug_cb;
1009         cbs->priv = (void *)ad;
1010
1011         SETTING_TRACE("to load ug[%s]", "worldclock-efl");
1012         ad->ug_loading =
1013             ug_create(ad->ug, "worldclock-efl", UG_MODE_FULLVIEW, NULL, cbs);
1014         if (NULL == ad->ug_loading) {   /* error handling */
1015         }
1016
1017         FREE(cbs);
1018         return;
1019 }
1020
1021
1022 Eina_Bool setting_update_datefield(void *cb)
1023 {
1024         SETTING_TRACE_BEGIN;
1025         retvm_if(cb == NULL, EINA_FALSE, "Data parameter is NULL");
1026         SettingTimeUG *ad = (SettingTimeUG *) cb;
1027
1028         __refresh_date_timer(ad);
1029         ad->refresh_time_idler = NULL;
1030         return 0;
1031 }
1032
1033 /* ***************************************************
1034  **
1035  **call back func
1036  **
1037  ****************************************************/
1038
1039 void
1040 setting_time_main_click_softkey_left_cb(void *data, Evas_Object *obj,
1041                                         void *event_info)
1042 {
1043         SETTING_TRACE_BEGIN;
1044         retm_if(data == NULL, "Data parameter is NULL");
1045
1046         SettingTimeUG *ad = (SettingTimeUG *) data;
1047
1048         /* Send destroy request */
1049         ug_destroy_me(ad->ug);
1050 }
1051
1052 /* ***************************************************
1053  **
1054  **call back func
1055  **
1056  ****************************************************/
1057
1058 void
1059 setting_time_main_click_softkey_caller_exist_left_cb(void *data,
1060                                                      Evas_Object *obj,
1061                                                      void *event_info)
1062 {
1063         SETTING_TRACE_BEGIN;
1064         /* error check */
1065         retm_if(data == NULL, "Data parameter is NULL");
1066
1067         SettingTimeUG *ad = (SettingTimeUG *) data;
1068
1069         /*  Create Bundle and send message */
1070         service_h svc;
1071         if (service_create(&svc))
1072                 return;
1073
1074         service_add_extra_data(svc, "result", "lbutton_click");
1075         ug_send_result(ad->ug, svc);
1076
1077         service_destroy(svc);
1078
1079         /* Send destroy request */
1080         ug_destroy_me(ad->ug);
1081 }
1082
1083 void
1084 setting_time_main_click_softkey_caller_exist_right_cb(void *data,
1085                                                       Evas_Object *obj,
1086                                                       void *event_info)
1087 {
1088         SETTING_TRACE_BEGIN;
1089         /* error check */
1090         retm_if(data == NULL, "Data parameter is NULL");
1091
1092         SettingTimeUG *ad = (SettingTimeUG *) data;
1093
1094         /*  Create Bundle and send message */
1095         service_h svc;
1096         if (service_create(&svc))
1097                 return;
1098
1099         service_add_extra_data(svc, "result", "rbutton_click");
1100         ug_send_result(ad->ug, svc);
1101
1102         service_destroy(svc);
1103
1104         /* Send destroy request */
1105         ug_destroy_me(ad->ug);
1106 }
1107
1108 static void setting_time_reboot_resp_cb(void *data, Evas_Object *obj,
1109                                         void *event_info)
1110 {
1111         SETTING_TRACE_BEGIN;
1112         int response_type = btn_type(obj);
1113         if (POPUP_RESPONSE_OK == response_type) {
1114                 setting_time_main_btn_update_ok_cb(data);
1115         } else if (POPUP_RESPONSE_CANCEL == response_type) {
1116                 setting_time_main_btn_update_cancel_cb(data);
1117         }
1118
1119         SettingTimeUG *ad = data;
1120         if (ad->pop)
1121         {
1122                 evas_object_del(ad->pop);
1123                 ad->pop = NULL;
1124         }
1125 }
1126
1127 static void
1128 setting_time_main_chk_btn_cb(void *data, Evas_Object *obj, void *event_info)
1129 {
1130         SETTING_TRACE_BEGIN;
1131         /* error check */
1132         retm_if(data == NULL, "Data parameter is NULL");
1133         SettingTimeUG *ad = NULL;
1134         Setting_GenGroupItem_Data *list_item =
1135             (Setting_GenGroupItem_Data *) data;
1136         ad = list_item->userdata;
1137         list_item->chk_status = elm_check_state_get(obj);       /*  for genlist update status */
1138         if (ad->data_auto)
1139                 ad->chk_auto = ad->data_auto->eo_check;
1140         if (ad->data_dst)
1141                 ad->chk_dst = ad->data_dst->eo_check;
1142
1143         if (ad->data_time) {
1144                 elm_object_focus_set(ad->data_time->eo_check, EINA_FALSE);
1145         }
1146
1147         int err;
1148         if (obj == ad->chk_auto) {
1149                 int bvalue;
1150                 setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1151                                          &bvalue, &err);
1152                 if (bvalue) {
1153                         (void) setting_set_bool_slp_key (BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE, SETTING_ON_OFF_BTN_OFF, &err);
1154                         elm_check_state_set(ad->chk_auto, 0);
1155
1156                         if (ad->data_tz) {
1157                                 setting_enable_genlist_item(ad->data_tz->item);
1158                         }
1159
1160                         if (ad->data_time) {
1161                                 ad->data_time->isItemDisableFlag = FALSE;
1162                                 setting_enable_evas_object(ad->data_time->eo_check);
1163                                 setting_enable_genlist_item(ad->data_time->item);
1164                         }
1165
1166                 } else {
1167                         ad->pop =
1168                             setting_create_popup_with_btn(ad, ad->win_get, NULL,
1169                                                  _("IDS_ST_POP_RESTART_PHONE_TO_USE_AUTOMATIC_UPDATE_Q"),
1170                                                  setting_time_reboot_resp_cb,
1171                                                  0, 2,
1172                                                  dgettext("sys_string",
1173                                                        "IDS_COM_SK_OK"),
1174                                                  _("IDS_COM_SK_CANCEL"));
1175                 }
1176         }
1177 }
1178
1179
1180 static void setting_time_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj,
1181                                            void *event_info)
1182 {
1183         SETTING_TRACE_BEGIN;
1184         /* error check */
1185         setting_retm_if(data == NULL, "Data parameter is NULL");
1186
1187         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
1188         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
1189         elm_genlist_item_selected_set(item, 0);
1190         Setting_GenGroupItem_Data *list_item =
1191             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
1192
1193         SettingTimeUG *ad = (SettingTimeUG *) data;
1194         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
1195
1196         if (ad->data_time && 0 != safeStrCmp("IDS_ST_BODY_DATE_AND_TIME", list_item->keyStr)) {
1197                 elm_object_focus_set(ad->data_time->eo_check, EINA_FALSE);
1198         }
1199
1200         if (!safeStrCmp("IDS_ST_BODY_TIME_ZONE", list_item->keyStr)) {
1201                 ad->is_datefield_selected = EINA_FALSE;
1202                 if (1 == setting_time_check_automatic_time_update_state()) {
1203                         setting_create_simple_popup(ad, ad->win_get,
1204                                                     NULL, _(AUTO_TIME_UPDATE_ON));
1205                 } else {
1206                         setting_time_main_launch_worldclock_sg(ad);
1207                 }
1208
1209         } else if (!safeStrCmp("IDS_ST_BODY_DATE_AND_TIME", list_item->keyStr)) {
1210                 ad->is_datefield_selected = EINA_TRUE;
1211                 if (1 == setting_time_check_automatic_time_update_state()) {
1212                         setting_create_simple_popup(ad, ad->win_get,
1213                                                     NULL, _(AUTO_TIME_UPDATE_ON));
1214                 } else {
1215                         /* setting_view_change(&setting_view_time_main, &setting_view_time_time, ad); */
1216                 }
1217         } else if (!safeStrCmp("IDS_ST_BODY_AUTOMATIC_UPDATE", list_item->keyStr)) {
1218                 ad->is_datefield_selected = EINA_FALSE;
1219                 int old_status = elm_check_state_get(list_item->eo_check);
1220                 /* new status */
1221                 list_item->chk_status = !old_status;
1222                 elm_check_state_set(list_item->eo_check, list_item->chk_status);
1223                 int err = 0;
1224                 if (!list_item->chk_status) {
1225                         (void)
1226                             setting_set_bool_slp_key
1227                             (BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1228                              SETTING_ON_OFF_BTN_OFF, &err);
1229
1230                         if (ad->data_tz) {
1231                                 setting_enable_genlist_item(ad->data_tz->item);
1232                         }
1233
1234                         if (ad->data_time) {
1235                                 ad->data_time->isItemDisableFlag = FALSE;
1236                                 setting_enable_evas_object(ad->data_time->eo_check);
1237                                 setting_enable_genlist_item(ad->data_time->item);
1238                         }
1239
1240                 } else {
1241                         ad->pop =
1242                             setting_create_popup_with_btn(ad, ad->win_get, NULL,
1243                                                  _("IDS_ST_POP_RESTART_PHONE_TO_USE_AUTOMATIC_UPDATE_Q"),
1244                                                  setting_time_reboot_resp_cb,
1245                                                  0, 2,
1246                                                  dgettext("sys_string", "IDS_COM_SK_OK"),
1247                                                  _("IDS_COM_SK_CANCEL"));
1248                 }
1249
1250         } else if (!safeStrCmp("IDS_ST_BODY_TIME_FORMAT", list_item->keyStr)
1251                    || !safeStrCmp("IDS_ST_BODY_DATE_FORMAT", list_item->keyStr)
1252                    || !safeStrCmp("IDS_CLD_BODY_FIRST_DAY_OF_WEEK", list_item->keyStr)) {
1253                 ad->is_datefield_selected = EINA_FALSE;
1254                 bool status = !elm_genlist_item_expanded_get(item);
1255                 elm_genlist_item_expanded_set(item, status);
1256         }
1257 }
1258
1259
1260 void setting_time_main_btn_update_ok_cb(void *cb)
1261 {
1262         SETTING_TRACE_BEGIN;
1263         SettingTimeUG *ad = (SettingTimeUG *) cb;
1264         int err;                /* error check */
1265         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1266                                  SETTING_ON_OFF_BTN_ON, &err);
1267         elm_check_state_set(ad->data_auto->eo_check, 1);
1268         ad->data_auto->chk_status = 1;
1269
1270         sysman_request_reboot();
1271
1272         /*  We'd better check return value from sysman_request_reboot() function. */
1273         /*  It will return 0 on success or -1 if failed. */
1274 }
1275
1276
1277 void setting_time_main_btn_update_cancel_cb(void *cb)
1278 {
1279         SETTING_TRACE_BEGIN;
1280         SettingTimeUG *ad = (SettingTimeUG *) cb;
1281
1282         int err;                /* error check */
1283
1284         retm_if(cb == NULL, "Data parameter is NULL");
1285         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1286                                  SETTING_ON_OFF_BTN_OFF, &err);
1287         elm_check_state_set(ad->data_auto->eo_check, 0);
1288         ad->data_auto->chk_status = 0;
1289
1290 }
1291
1292
1293 static UChar *uastrcpy(const char *chars)
1294 {
1295         int len = 0;
1296         UChar *str = NULL;
1297         len = safeStrLen(chars);
1298         str = (UChar *) malloc(sizeof(UChar) *(len + 1));
1299         if (!str)
1300                 return NULL;
1301         u_uastrcpy(str, chars);
1302         return str;
1303 }
1304
1305
1306 static int get_timezone_isdst(int* isdst)
1307 {
1308         SETTING_TRACE_BEGIN;
1309     struct tm *ts;
1310     time_t ctime;
1311
1312     enum { BUFFERSIZE = 1024 };
1313     char buf[BUFFERSIZE];
1314     ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
1315
1316     if (len != -1) {
1317         buf[len] = '\0';
1318     }
1319     else {
1320         /* handle error condition */
1321     }
1322
1323     ctime = time(NULL);
1324     ts = localtime(&ctime);
1325         if ( ! ts ) {
1326                 return SETTING_GENERAL_ERR_NULL_DATA_PARAMETER;
1327         }
1328
1329     SETTING_TRACE(" == tz_path = %s", buf);
1330     SETTING_TRACE(" == isdst = %d\n", ts->tm_isdst);
1331         *isdst = ts->tm_isdst;
1332
1333         return SETTING_RETURN_SUCCESS;
1334 }
1335
1336
1337 static void ICU_set_timezone(const char *timezone)
1338 {
1339         SETTING_TRACE("ICU_set_timezone = %s ", timezone);
1340         UErrorCode ec = U_ZERO_ERROR;
1341         UChar *str = uastrcpy(timezone);
1342
1343         ucal_setDefaultTimeZone(str, &ec);
1344         if (U_SUCCESS(ec)) {
1345                 SETTING_TRACE("ucal_setDefaultTimeZone() SUCCESS ");
1346         } else {
1347                 SETTING_TRACE("ucal_setDefaultTimeZone() FAILED : %s ",
1348                               u_errorName(ec));
1349         }
1350         FREE(str);
1351 }
1352
1353
1354 static void setting_time_main_controlbar_lbtn_label_set(
1355                                                         Evas_Object *controlbar,
1356                                                         char *label)
1357 {
1358         SETTING_TRACE_BEGIN;
1359         setting_retm_if(controlbar == NULL, "controlbar parameter is NULL");
1360         setting_retm_if(label == NULL, "label parameter is NULL");
1361
1362         Evas_Object *eo_lbtn = NULL;
1363         Elm_Object_Item *first_item = NULL;
1364
1365         first_item = elm_toolbar_first_item_get(controlbar);
1366         setting_retm_if(first_item == NULL, "get first_item failed");
1367
1368         eo_lbtn = elm_object_item_part_content_get(first_item, "object");
1369         setting_retm_if(eo_lbtn == NULL, "get eo_lbtn failed");
1370
1371         elm_object_text_set(eo_lbtn, label);
1372 }
1373
1374
1375 /**
1376  * ex) timezone : Asia/Seoul --> returns "+09"
1377  *
1378  * char outstr2[4];
1379  */
1380 static void get_gmt_offset(char *str_buf, int size)
1381 {
1382         // timezone string +/-<n> ex. +9, -1
1383         time_t t = time(0);     // get unix time. sec.
1384
1385         struct tm* data;
1386         data = localtime(&t);           // save time as structure.
1387         retv_if(!data, FALSE);
1388         data->tm_isdst = 0;                     // summer time, not applied.
1389         time_t a = mktime(data);
1390
1391         data = gmtime(&a);
1392         data->tm_isdst = 0;                     // summer time, not applied.
1393         time_t b = mktime(data);
1394
1395         int gmtoffset_hour = (a-b)/3600;        // result : hour.
1396         int gmtoffset_min = ((a-b)%3600)/60;    // result : min.
1397         if(gmtoffset_min != 0) {
1398                 gmtoffset_min = 30;
1399         }
1400         
1401         snprintf(str_buf, size, "%+d:%02u", gmtoffset_hour, gmtoffset_min);
1402         SETTING_TRACE("szTimezone is of a valid format: GMT: %s", str_buf);
1403 }
1404
1405
1406 // automatic
1407 static char* get_timezone_str()
1408 {
1409     SETTING_TRACE_BEGIN;
1410
1411     enum { BUFFERSIZE = 1024 };
1412     char buf[BUFFERSIZE];
1413     ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
1414
1415     if (len != -1) {
1416         buf[len] = '\0';
1417     }
1418     else {
1419         /* handle error condition */
1420     }
1421     return g_strdup(buf+20);            //Asia/Seoul
1422 }
1423
1424 static char* get_city_name_result()
1425 {
1426         return s_result;
1427 }
1428
1429
1430 // in_str = "Asia/Seoul"
1431 static bool get_city_name(char* tzname)
1432 {
1433         sqlite3 *pSQLite3 = NULL;
1434         char    *szErrMsg = NULL;
1435
1436         int rst = sqlite3_open( "/opt/dbspace/.worldclock.db", &pSQLite3);
1437         if ( rst )
1438         {
1439                 printf( "Can't open database: %s\n", sqlite3_errmsg( pSQLite3 ));
1440                 sqlite3_close( pSQLite3 );
1441                 pSQLite3 = NULL;
1442                 return false;
1443         }
1444         else
1445         {
1446                 printf("Database opened!!\n");
1447                 char query_str[256];
1448                 snprintf(query_str, 256, "SELECT city FROM city_table where tz_path=\"%s\"", tzname);
1449                 printf("%s \n", query_str);
1450                 rst = sqlite3_exec(pSQLite3, query_str, query_callback, 0, &szErrMsg);
1451         }
1452
1453         //객체해제
1454         sqlite3_free( szErrMsg );
1455         sqlite3_close( pSQLite3 );
1456         printf("Database close!!\n");
1457         return true;
1458 }
1459
1460 static int query_callback(void *NotUsed, int argc, char **argv, char **azColName)
1461 {
1462     int i = 0;
1463     printf("%s\n", argv[i] ? argv[i] : "NULL");
1464         s_result = g_strdup(argv[i] ? argv[i] : "NULL");
1465     return 0;
1466 };
1467
1468
1469 #if 0                   /* example */
1470 int main(int argc, char* argv[])
1471 {
1472         g_type_init ();
1473         int ret;
1474         // exporting - current status
1475         if ( (argc == 2) && (0 == strcmp(argv[1], "export"))) {
1476                         setting_export();
1477         }
1478         else if ( (argc == 2) && (0 == strcmp(argv[1], "timezone_init"))) {
1479
1480                 //////////////////////////////////////////////////////////////////////////
1481                 char* tz_path_zone_city = get_timezone_str();
1482                 bool ret = get_city_name(tz_path_zone_city);
1483                 if (ret) {
1484                         printf(">>>>>> RESULT : %s \n", get_city_name_result());
1485                 }
1486                 //////////////////////////////////////////////////////////////////////////
1487         }
1488         else {
1489                 // cfg create
1490                 // TRUE or FALSE
1491                 ret = setting_cfg_create();
1492         }
1493         return 0;
1494 }
1495 #endif
1496
1497
1498