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