tizen 2.4 release
[apps/home/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 <dd-deviced-managed.h>
24 #include <dd-deviced.h>
25
26 #include <stdio.h>
27 #include <malloc.h>
28 #include <time.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sqlite3.h>
32 #include <eventsystem.h>
33
34 #include <vconf.h>
35 #include <vconf-keys.h>
36
37 #include <unicode/udat.h>
38 #include <unicode/ustring.h>
39 #include <unicode/uloc.h>
40 #include <unicode/ucal.h>
41 #include <unicode/utmscale.h>
42
43 #include <eventsystem.h>
44 #include <bundle_internal.h>
45 #include <system_settings.h>
46
47 #define ONE_MINUTE 60
48 #define PROGRESS_POPUP_SHOW 0
49 #define DEF_BUF_SIZE 256
50
51 /*  ICU utilities*/
52 static UChar *uastrcpy(const char *chars);
53 static void ICU_set_timezone(const char *timezone);
54 static void get_gmt_offset(char *str_buf, int size);
55
56 static char *get_timezone_str();
57 static char *get_city_name_result();
58 static bool get_city_name(char *tzname);
59 static char *get_timezone_displayname();
60
61 static char *s_result;
62 static int query_callback(void *NotUsed, int argc, char **argv, char **azColName);
63 static bool setting_update_timezone(SettingTimeUG *ad);
64 static void __setting_update_datefield_cb(void *cb);
65
66
67 static int setting_time_main_create(void *cb);
68 static int setting_time_main_destroy(void *cb);
69 static int setting_time_main_update(void *cb);
70 static int setting_time_main_cleanup(void *cb);
71
72 setting_view setting_view_time_main = {
73         .create = setting_time_main_create,
74         .destroy = setting_time_main_destroy,
75         .update = setting_time_main_update,
76         .cleanup = setting_time_main_cleanup,
77 };
78
79
80 bool __update_timezone_idler(SettingTimeUG *ad)
81 {
82         setting_update_timezone(ad);
83         ecore_idler_del(ad->update_timezone_idler);
84         ad->update_timezone_idler = NULL;
85         return 0;
86 }
87
88 void time_changed_callback(keynode_t *key, void *data)
89 {
90
91         SETTING_TRACE_DEBUG("time and timezone have been changed: TIME_CHANGED_HEYNOTY_CALLBACK");
92         SettingTimeUG *ad = (SettingTimeUG *)data;
93
94         int ret = setting_time_update_cb(ad);
95         if (ret != 0) {
96                 SETTING_TRACE("FAIL: setting_time_update_db(ad)\n");
97         }
98
99 #if 0
100         SETTING_TRACE("ad->is_datefield_selected:%d", ad->is_datefield_selected)
101         if (EINA_TRUE == ad->is_datefield_selected) {
102                 /* Timezone is not changed and a selected value of the datefield is already changed */
103                 SETTING_TRACE("Datefield is selected\n");
104                 return ;
105         }
106 #endif
107
108         tzset();
109         /* vconfset to update timezone */
110         char pTZPath[__MAX_PATH_SIZE];
111         ssize_t nLen = readlink(SETTING_TZONE_SYMLINK_PATH, pTZPath, sizeof(pTZPath) - 1);
112         if (nLen != -1) {
113                 pTZPath[nLen] = '\0';
114         } else {
115                 SETTING_TRACE("parse error for SETTING_TZONE_SYMLINK_PATH");
116                 return;
117         }
118
119         if (!__setting_set_city_tzone(&pTZPath[20])) {
120                 SETTING_TRACE("__setting_set_city_tzone ERROR");
121                 return;
122         }
123         char *timezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
124         setting_retm_if(timezone == NULL, "get vonf failed");
125         ICU_set_timezone(timezone);
126         FREE(timezone);
127
128         /* update timezone */
129         if (ad->update_timezone_idler) {
130                 ecore_idler_del(ad->update_timezone_idler);
131                 ad->update_timezone_idler = NULL;
132         }
133         ad->update_timezone_idler =
134             ecore_idler_add((Ecore_Task_Cb) __update_timezone_idler, ad);
135         /* update time */
136         if (ad->refresh_time_idler) {
137                 ecore_idler_del(ad->refresh_time_idler);
138                 ad->refresh_time_idler = NULL;
139         }
140         ad->refresh_time_idler = ecore_idler_add(setting_update_datefield_cb, ad);
141 }
142
143 void setting_time_update_time_date_format_string(SettingTimeUG *ad)
144 {
145         SETTING_TRACE_BEGIN;
146
147         struct tm *ts;
148         struct tm ts_ret;
149         time_t ctime;
150
151         int ret, value, err;
152
153         char time_str[MAX_ARGS];
154
155         ctime = time(NULL);
156         ts = localtime_r(&ctime, &ts_ret);
157         ret_if(!ts);
158
159         /* set time in particular format */
160         ret = setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224, &value, &err);
161         if (ret == SETTING_RETURN_FAIL)
162                 value = VCONFKEY_TIME_FORMAT_12;
163
164         switch (value) {
165                 case VCONFKEY_TIME_FORMAT_12: {
166                                 /* Get Time */
167                                 strftime(time_str, MAX_ARGS, "%l:%M %p ", &ts_ret);
168                         }
169                         break;
170
171                 case VCONFKEY_TIME_FORMAT_24: {
172                                 strftime(time_str, MAX_ARGS, "%H:%M", &ts_ret);
173                         }
174                         break;
175                 default:
176                         /* do nothing */
177                         break;
178         }
179
180         if (ad->data_time) {
181                 elm_object_item_data_set(ad->data_time->item, ad->data_time);
182                 elm_genlist_item_update(ad->data_time->item);
183         }
184 }
185
186 bool setting_update_timezone(SettingTimeUG *ad)
187 {
188         /* SETTING_TRACE_BEGIN; */
189         char time_zone_sub_str[CITY_BUF_SIZE + GMT_BUF_SIZE + 3] = { 0, };
190         char *displayTimezone = NULL;
191         char gmt_offset[GMT_BUF_SIZE] = {0, };
192         int ret = 0;
193
194         /*  get time zone */
195         displayTimezone = get_timezone_displayname();
196         get_gmt_offset(gmt_offset, GMT_BUF_SIZE);
197
198         if (displayTimezone != NULL) {
199                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT %s",
200                                displayTimezone, gmt_offset);
201                 retv_if(ret < 0, 0);
202         } else {
203                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "Korean Standard Time, GMT +9:00");
204                 retv_if(ret < 0, 0);
205         }
206         SETTING_TRACE("ad->data_tz:%p", ad->data_tz);
207
208         if (ad->data_tz) {
209                 ad->data_tz->sub_desc = (char *)g_strdup(time_zone_sub_str);
210                 elm_object_item_data_set(ad->data_tz->item, ad->data_tz);
211                 elm_genlist_item_update(ad->data_tz->item);
212         }
213
214         FREE(displayTimezone);
215
216         return 0;
217 }
218
219 static void setting_time_main_int_vconf_change_cb(keynode_t *key, void *data)
220 {
221         SETTING_TRACE_BEGIN;
222         ret_if(NULL == data);
223         SettingTimeUG *ad = data;
224
225         char *vconf_name = vconf_keynode_get_name(key);
226         /* SETTING_TRACE("vconf_name:%s", vconf_name); */
227
228         if (!safeStrCmp(vconf_name, VCONFKEY_REGIONFORMAT_TIME1224) ||
229             !safeStrCmp(vconf_name, VCONFKEY_SETAPPL_DATE_FORMAT_INT)) {
230                 char *time;
231                 static char time_arr[DEF_BUF_SIZE];
232                 char *pa_time_format = get_pa_time_format_str();
233                 setting_retm_if(NULL == pa_time_format, "pa_time_format is NULL");
234
235                 char* lang = NULL;
236                 int err = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &lang);
237                 char *date = __setting_phone_lang_get_by_pattern(lang, "MMM/d/yyyy");
238
239                 if (!strncmp(pa_time_format, "IDS_ST_BODY_12_HOURS", strlen("IDS_ST_BODY_12_HOURS")))
240                         time = __setting_phone_lang_get_by_pattern(lang, "hhmm");
241                 else
242                         time = __setting_phone_lang_get_by_pattern(lang, "Hm");
243
244                 strncpy(time_arr, time, DEF_BUF_SIZE);
245                 time_arr[DEF_BUF_SIZE - 1] = '\0';
246
247                 elm_object_text_set(ad->data_time->btn_right, time_arr);
248                 FREE(pa_time_format);
249         }
250 }
251
252 static int _alarmmgr_set_timezone_helper(char *tzdata)
253 {
254         int ret = alarmmgr_set_timezone(tzdata);
255         if (ret) {
256                 /* set event system */
257                 setting_set_event_system(SYS_EVENT_TIME_ZONE,
258                                          EVT_KEY_TIME_ZONE,
259                                          tzdata);
260         }
261
262         return ret;
263 }
264
265 static int _alarmmgr_set_systime_helper(time_t t_current)
266 {
267         int ret;
268         ret = alarmmgr_set_systime(t_current);
269         if (ret) {
270                 /* set event system */
271                 setting_set_event_system(SYS_EVENT_SCREEN_AUTOROTATE_STATE,
272                                          EVT_KEY_TIME_CHANGED,
273                                          t_current);
274         }
275
276         return ret;
277 }
278
279
280 static void __update_time_via_sim_card(void *data)
281 {
282         setting_retm_if(data == NULL, "Data parameter is NULL");
283         SettingTimeUG *ad = (SettingTimeUG *) data;
284         int ret = 0;
285         int t_nitz = 0;
286         int t_offset = 0;
287         ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_GMT, &t_nitz);
288         ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &t_offset);
289         char *tzpath = vconf_get_str(VCONFKEY_TELEPHONY_NITZ_ZONE);
290         if (ret != 0 || isEmptyStr(tzpath)) {
291                 return;
292         }
293         ret = 0;/*reset.. */
294
295         /* a.time zone */
296         SETTING_TRACE("tz_path:%s", tzpath);
297
298         char tz_path[MAX_COMMON_BUFFER_LEN / 4 + 1];
299         safeCopyStr(tz_path, SETTING_TIME_ZONEINFO_PATH, MAX_COMMON_BUFFER_LEN / 4);
300         g_strlcat(tz_path, tzpath, sizeof(tz_path));
301         SETTING_TRACE("full tz_path:%s", tz_path);
302         ret = _alarmmgr_set_timezone_helper(tz_path);
303         if (ret < 0) {
304                 SETTING_TRACE("tzpath is not valid.");
305                 return;
306         } else
307                 SETTING_TRACE("_alarmmgr_set_timezone_helper - successful : %s \n", tz_path);
308         if (!__setting_set_city_tzone(tzpath)) {
309                 SETTING_TRACE("__setting_set_city_tzone ERROR")
310                 return;
311         }
312         get_city_name(tzpath);
313         char *city = get_city_name_result();
314         SETTING_TRACE("city:%s", city);
315         if (city) {
316                 ret = vconf_set_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT, city);
317                 setting_retm_if(ret != 0, "set vconf[%s] failed", VCONFKEY_SETAPPL_CITYNAME_INDEX_INT);
318         }
319         setting_update_timezone(ad);
320
321         /* b.TIME AND DATE */
322         int t_uptime = 0;
323         FILE *fp = fopen("/proc/uptime", "r");
324         if (fp) {
325                 if (!fscanf(fp, "%d", &t_uptime)) {
326                         SETTING_TRACE_ERROR("fscanf error");
327                 }
328                 fclose(fp);
329                 fp = NULL;
330         } else {
331                 SETTING_TRACE_ERROR("fopen error");
332         }
333
334         SETTING_TRACE("t_nitz:%d, t_offset:%d, t_uptime:%d", t_nitz, t_offset, t_uptime);
335         time_t t_current = t_nitz + t_uptime - t_offset;
336         SETTING_TRACE("t_current:%d", t_current);
337         ret = _alarmmgr_set_systime_helper(t_current);
338         setting_retm_if(ret == -1, "_alarmmgr_set_systime_helper call failed");
339         if (ad->data_time) {
340                 struct tm ts_ret;
341                 struct tm *ts = localtime_r(&t_current, &ts_ret);
342                 ret_if(!ts);
343                 setting_enable_evas_object(ad->data_time->btn_left);
344                 setting_enable_evas_object(ad->data_time->btn_right);
345         }
346
347         /* time change - event */
348         static int t_event_val = -1;
349         vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED, t_event_val);
350
351         FREE(tzpath);
352 }
353
354
355 static void setting_time_main_bool_vconf_change_cb(keynode_t *key, void *data)
356 {
357         /* SETTING_TRACE_BEGIN; */
358         ret_if(NULL == data);
359         SettingTimeUG *ad = data;
360         int status = vconf_keynode_get_bool(key);
361         char *vconf_name = vconf_keynode_get_name(key);
362
363         if (!safeStrCmp(vconf_name, VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL)) {
364                 if (ad->data_auto) {
365                         ad->is_datefield_selected = EINA_FALSE;
366
367                         setting_update_gl_item_chk_status(ad->data_auto, status);
368
369                         if (!ad->data_auto->chk_status) {
370                                 if (ad->data_tz) {
371                                         setting_enable_genlist_item(ad->data_tz->item);
372                                 }
373
374                                 if (ad->data_time) {
375                                         ad->data_time->isItemDisableFlag = FALSE;
376                                         /*setting_ensable_evas_object(ad->data_time->eo_check); */
377                                         setting_enable_evas_object(ad->data_time->btn_left);
378                                         setting_enable_evas_object(ad->data_time->btn_right);
379                                         setting_enable_genlist_item(ad->data_time->item);
380                                 }
381
382
383                         } else {
384                                 if (ad->data_tz) {
385                                         setting_disable_genlist_item(ad->data_tz->item);
386                                 }
387
388                                 if (ad->data_time) {
389                                         ad->data_time->isItemDisableFlag = FALSE;
390                                         /*setting_disable_evas_object(ad->data_time->eo_check); */
391                                         setting_disable_evas_object(ad->data_time->btn_left);
392                                         setting_disable_evas_object(ad->data_time->btn_right);
393                                         setting_disable_genlist_item(ad->data_time->item);
394                                 }
395
396                                 __update_time_via_sim_card(ad);
397                         }
398                 }
399         }
400 }
401
402 static void __sim_time_info_chg_cb(keynode_t *key, void *data)
403 {
404         SETTING_TRACE_BEGIN;
405         ret_if(NULL == data);
406         SettingTimeUG *ad = data;
407         char *vconf_name = vconf_keynode_get_name(key);
408         /*SETTING_TRACE("vconf_name:%s", vconf_name);*/
409         if (!safeStrCmp(vconf_name, VCONFKEY_TELEPHONY_NITZ_GMT)
410             || !safeStrCmp(vconf_name, VCONFKEY_TELEPHONY_NITZ_EVENT_GMT)
411             || !safeStrCmp(vconf_name, VCONFKEY_TELEPHONY_NITZ_ZONE)) {
412                 int value = 0;
413                 int err = 0;
414                 setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE, &value,
415                                          &err);
416                 if (value) {
417                         int ret = 0;
418                         int t_nitz = 0;
419                         int t_offset = 0;
420                         ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_GMT, &t_nitz);
421                         ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &t_offset);
422                         char *tzpath = vconf_get_str(VCONFKEY_TELEPHONY_NITZ_ZONE);
423                         if (ret != 0 || isEmptyStr(tzpath)) {
424                                 if (ad->data_auto) {
425                                         setting_disable_genlist_item(ad->data_auto->item);
426                                 }
427                                 if (ad->data_tz) {
428                                         setting_enable_genlist_item(ad->data_tz->item);
429                                 }
430                                 if (ad->data_time) {
431                                         setting_enable_genlist_item(ad->data_time->item);
432                                 }
433                                 return;
434                         }
435                         FREE(tzpath);
436
437                         if (ad->data_auto) {
438                                 setting_enable_genlist_item(ad->data_auto->item);
439                         }
440                         if (ad->data_tz) {
441                                 setting_disable_genlist_item(ad->data_tz->item);
442                         }
443                         if (ad->data_time) {
444                                 setting_disable_genlist_item(ad->data_time->item);
445                         }
446
447                         __update_time_via_sim_card(ad);
448                 }
449         }
450 }
451
452 #if 0
453 static int __setting_time_check_enable_automatic_update()
454 {
455         /*to read TAPI vconfs */
456         int ret = 0;
457         int t_nitz = 0;
458         int t_offset = 0;
459         ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_GMT, &t_nitz);
460         ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &t_offset);
461         char *tzpath = vconf_get_str(VCONFKEY_TELEPHONY_NITZ_ZONE);
462         if (ret == 0 && !isEmptyStr(tzpath))
463                 return 1;
464         else
465                 return 0;
466 }
467 #endif
468
469 /* ***************************************************
470  *
471  *basic func
472  *
473  ***************************************************/
474 static void setting_time_main_datefield_set_cb(void *data, Evas_Object *object, void *event_info)
475 {
476         SETTING_TRACE_BEGIN;
477         SETTING_TRACE("SETTING_DATETIME");
478
479
480         /*Setting_GenGroupItem_Data *item_data = param->item_data; */
481         /* error check */
482         ret_if(data == NULL);
483         Setting_GenGroupItem_Data *list_item = (Setting_GenGroupItem_Data *) data;
484         SettingTimeUG *ad = list_item->userdata;
485
486         Evas_Object *obj = list_item->eo_check; /* elm_datetime_add */
487
488         if (obj) {
489                 elm_object_signal_emit(list_item->eo_check /* elm_datetime_xxx*/, "picker,action,value,set", "");
490         } else {
491                 SETTING_TRACE_ERROR("list_item->eo_check is NULL");
492         }
493
494         ad->is_datefield_selected = EINA_TRUE;
495
496 #ifdef USE_TIMER_UPDATE_TIME_IN_TIME_VIEW
497         if (ad->update_timer) {
498                 ecore_timer_reset(ad->update_timer);
499                 ecore_timer_freeze(ad->update_timer);
500         }
501 #endif
502
503         /* If auto update is on, don't anything while the datefield has been updated (i.e., time runs) */
504         if (1 == setting_time_check_automatic_time_update_state()) {
505                 SETTING_TRACE("AUTO_TIME ON: no action");
506                 SETTING_TRACE_END;
507 #ifdef USE_TIMER_UPDATE_TIME_IN_TIME_VIEW
508                 if (ad->update_timer) {
509                         ecore_timer_thaw(ad->update_timer);
510                 }
511 #endif
512                 return;
513         }
514
515         struct tm _tm;
516         elm_datetime_value_get(obj, &_tm);
517
518         SETTING_TRACE("year : %d, month : %d, day : %d, hour : %d, min : %d",
519                       _tm.tm_year, _tm.tm_mon, _tm.tm_mday, _tm.tm_hour, _tm.tm_min);
520
521         _tm.tm_isdst = -1;
522         _tm.tm_sec = 0;
523
524         /*  local time  -> gmtime */
525         time_t the_time = mktime(&_tm); /* represent local time */
526
527         /* invoke API to change time */
528         int ret = _alarmmgr_set_systime_helper(the_time);
529
530 #ifdef USE_TIMER_UPDATE_TIME_IN_TIME_VIEW
531         if (ad->update_timer) {
532                 ecore_timer_thaw(ad->update_timer);
533         }
534 #endif
535
536 #if 1
537         time_t cctime = time(NULL);
538         struct tm ts_ret;
539         struct tm *ts = localtime_r(&cctime, &ts_ret);
540         SETTING_TRACE("After _alarmmgr_set_systime_helper() ....year : %d, month : %d, day : %d, hour : %d, min : %d",
541                       ts_ret.tm_year, ts_ret.tm_mon, ts_ret.tm_mday, ts_ret.tm_hour, ts_ret.tm_min);
542 #endif
543         Evas_Object *popup = NULL;
544         popup = evas_object_data_get(obj, "popup");
545         if (popup) {
546                 evas_object_del(popup);
547                 popup = NULL;
548         }
549         setting_retm_if(ret == -1, "_alarmmgr_set_systime_helper call failed");
550 }
551
552 #ifdef USE_TIMER_UPDATE_TIME_IN_TIME_VIEW
553 Eina_Bool __refresh_date_timer(void *data)
554 {
555         SETTING_TRACE_BEGIN;
556         SettingTimeUG *ad = data;
557
558         __setting_update_datefield_cb(ad);
559         return 1;
560 }
561 #endif
562
563 void hour_format_event_handler(const char *event_name, bundle *data, void *user_data)
564 {
565         const char *hour_format_set = NULL;
566         SETTING_TRACE("hour format set event(%s) received", event_name);
567
568         hour_format_set = bundle_get_val(data, EVT_KEY_HOUR_FORMAT);
569         SETTING_TRACE("hour_format_set(%s)", hour_format_set);
570 }
571
572 int hour_format_event_reg_id;
573
574 static int setting_time_main_create(void *cb)
575 {
576         SETTING_TRACE_BEGIN;
577         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
578
579         SettingTimeUG *ad = (SettingTimeUG *) cb;
580
581         char *title = _(DATE_TIME_STR_DATE_AND_TIME);
582         char *caller = NULL;
583         ad->is_datefield_selected = EINA_FALSE;
584
585         app_control_h service = ad->bundle_data;
586
587         app_control_get_extra_data(service, "caller", &caller);
588         app_control_get_extra_data(service, "title", &title);
589
590         Evas_Object *scroller;  /*  scroller is a genlist */
591
592         ad->caller = caller;
593
594         title = DATE_TIME_STR_DATE_AND_TIME;
595         gchar *title_str = NULL;
596
597         if (title)
598                 title_str = title;
599
600         ad->ly_main =
601             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
602                                                    ad->win_get, title_str,
603                                                    NULL,
604                                                                                            NULL,
605                                                    setting_time_main_click_softkey_left_cb,
606                                                    NULL, ad, &scroller,
607                                                    &ad->navi_bar);
608
609         ad->scrl_main = scroller;
610         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);
611
612         int value;
613         int err;
614         int ret = 0;
615         Elm_Object_Item *item;
616
617         item = elm_genlist_item_append(scroller, &itc_seperator, NULL, NULL,
618                                        ELM_GENLIST_ITEM_NONE, NULL, NULL);
619         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
620         setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE, &value,
621                                  &err);
622
623         /*int enable_automatic = __setting_time_check_enable_automatic_update(); */
624         bool auto_flag = value;
625
626         if (auto_flag) { /*do special process only when auto mode */
627                 /*to read TAPI vconfs */
628                 int ret = 0;
629                 int t_nitz = 0;
630                 int t_offset = 0;
631                 ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_GMT, &t_nitz);
632                 ret += vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &t_offset);
633                 char *tzpath = vconf_get_str(VCONFKEY_TELEPHONY_NITZ_ZONE);
634                 if (ret == 0 && !isEmptyStr(tzpath)) {
635                         /*1.to update timezone */
636                         char tz_path[MAX_COMMON_BUFFER_LEN / 4 + 1];
637                         safeCopyStr(tz_path, SETTING_TIME_ZONEINFO_PATH, MAX_COMMON_BUFFER_LEN / 4);
638                         g_strlcat(tz_path, tzpath, sizeof(tz_path));
639                         SETTING_TRACE("full tz_path:%s", tz_path);
640                         ret = _alarmmgr_set_timezone_helper(tz_path);
641                         if (ret < 0) {
642                                 SETTING_TRACE("tzpath is not valid.");
643                         } else
644                                 SETTING_TRACE("_alarmmgr_set_timezone_helper - successful : %s \n", tz_path);
645                         if (!__setting_set_city_tzone(tzpath)) {
646                                 SETTING_TRACE("__setting_set_city_tzone ERROR")
647                         }
648                         get_city_name(tzpath);
649                         char *city = get_city_name_result();
650                         SETTING_TRACE("city:%s", city);
651                         ret = vconf_set_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT, city);
652
653                         /* 2.to set current time */
654                         int t_uptime = 0;
655                         FILE *fp = fopen("/proc/uptime", "r");
656                         if (fp) {
657                                 if (!fscanf(fp, "%d", &t_uptime)) {
658                                         SETTING_TRACE_ERROR("fscanf error");
659                                 }
660                                 fclose(fp);
661                                 fp = NULL;
662                         } else {
663                                 SETTING_TRACE_ERROR("fopen error");
664                         }
665
666                         SETTING_TRACE("t_nitz:%d, t_offset:%d, t_uptime:%d", t_nitz, t_offset, t_uptime);
667                         time_t t_current = t_nitz + t_uptime - t_offset;
668                         SETTING_TRACE("t_current:%d", t_current);
669                         ret = _alarmmgr_set_systime_helper(t_current);
670                 }
671                 FREE(tzpath);
672         }
673
674         /*  UI automatic */
675         /*if (!isEmulBin()) { */
676         ad->data_auto =
677             setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon),
678                                              setting_time_main_mouse_up_Gendial_list_cb,
679                                              ad, SWALLOW_Type_1ICON_1RADIO, NULL,
680                                              NULL, value,
681                                              DATE_TIME_STR_AUTOMATIC_UPDATE, NULL,
682                                              setting_time_main_chk_btn_cb);
683         if (ad->data_auto) {
684                 ad->data_auto->userdata = ad;
685         } else {
686                 SETTING_TRACE_ERROR("ad->data_auto is NULL");
687         }
688
689         /*ADD_GL_HELP(scroller,SETTING_TIME_AUTO_UPDATE_DESC); */
690         /*} */
691
692
693         /* create DATE_AND_TIME */
694         ad->data_time =
695             setting_create_Gendial_field_def(scroller, &(ad->itc_layout),
696                                              setting_time_main_mouse_up_Gendial_list_cb,
697                                              ad, SWALLOW_Type_LAYOUT_DATEFIELD,
698                                              NULL, NULL, 0,
699                                              DATE_TIME_STR_DATE_AND_TIME,
700                                              NULL,
701                                              NULL);
702         if (ad->data_time) {
703                 ad->data_time->userdata = ad;
704                 ad->data_time->nf = ad->navi_bar;
705                 ad->data_time->isItemDisableFlag = auto_flag;
706                 ad->data_time->activated_cb = setting_time_main_datefield_set_cb;
707                 elm_genlist_item_select_mode_set(ad->data_time->item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
708
709                 Elm_Object_Item *item = elm_genlist_item_append(scroller, &(itc_bottom_line), NULL, NULL,
710                                                                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
711                 elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
712
713         } else {
714                 SETTING_TRACE_ERROR("ad->data_time is NULL");
715         }
716
717         /*ADD_GL_SEPARATOR(scroller); */
718         setting_set_i18n("worldclock", SETTING_TIME_SHARE_LOCAL_PATH);
719         char time_zone_sub_str[CITY_BUF_SIZE + GMT_BUF_SIZE + 3] = { 0, };
720         char *displayTimezone = NULL;
721
722         displayTimezone = get_timezone_displayname();
723
724         if (displayTimezone != NULL) {
725
726                 /* don't use hard coded value */
727                 /* get value from current timezone */
728
729                 char str_buf[GMT_BUF_SIZE] = {0, };
730                 get_gmt_offset(str_buf, GMT_BUF_SIZE);
731
732                 char *timezone_str = get_timezone_str();
733
734                 SETTING_TRACE("timezone : %s, displayTimezone :%s, gmt_offset : %s",
735                               timezone_str, displayTimezone, _(str_buf));
736                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "%s, GMT %s",
737                                displayTimezone, _(str_buf));
738                 FREE(timezone_str);
739                 retv_if(ret < 0, 0);
740         } else {
741                 /* default code */
742                 ret = snprintf(time_zone_sub_str, CITY_BUF_SIZE + GMT_BUF_SIZE + 2, "Korean Standard Time, GMT +9:00");
743                 retv_if(ret < 0, 0);
744         }
745         FREE(displayTimezone);
746
747         /*  UI create time zone */
748         ad->data_tz =
749             setting_create_Gendial_field_def(scroller, &(ad->itc_2text_2),
750                                              setting_time_main_mouse_up_Gendial_list_cb,
751                                              ad, SWALLOW_Type_INVALID, NULL,
752                                              NULL, 0, DATE_TIME_STR_TIME_ZONE,
753                                              time_zone_sub_str, NULL);
754         if (ad->data_tz) {
755                 ad->data_tz->userdata = ad;
756         } else {
757                 SETTING_TRACE_ERROR("ad->data_tz is NULL");
758         }
759
760         /*  UI create "Time format" */
761         value = VCONFKEY_TIME_FORMAT_12;
762         setting_get_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224, &value, &err);
763         ad->data_time_fmt = setting_create_Gendial_field_def(scroller,
764                                                              &(ad->itc_1text_1icon),
765                                                              setting_time_main_mouse_up_Gendial_list_cb,
766                                                              ad,
767                                                              SWALLOW_Type_1ICON_1RADIO,
768                                                              NULL,
769                                                              NULL,
770                                                              (value == VCONFKEY_TIME_FORMAT_24),
771                                                              "IDS_ST_MBODY_24_HOUR_CLOCK",
772                                                              NULL,
773                                                              setting_time_main_chk_btn_cb);
774         if (ad->data_time_fmt) {
775                 __BACK_POINTER_SET(ad->data_time_fmt);
776                 ad->data_time_fmt->userdata = ad;
777         } else {
778                 SETTING_TRACE_ERROR("ad->data_time_fmt is NULL");
779         }
780         ADD_GL_HELP_NO_SEP(scroller, "IDS_ST_SBODY_SHOW_THE_TIME_IN_24_HOUR_FORMAT_INSTEAD_OF_12_HOUR_HAM_PM_FORMAT");
781
782 #if APPLIED_DATATIME_DATA_FORMAT
783         char *pa_date_format = get_pa_date_format_str();
784         ad->data_date_fmt = setting_create_Gendial_exp_parent_field(scroller,
785                                                                     &(ad->itc_2text_3_parent),
786                                                                     setting_time_main_mouse_up_Gendial_list_cb,
787                                                                     ad, SWALLOW_Type_INVALID,
788                                                                     DATE_TIME_STR_TIME_FORMAT,
789                                                                     pa_time_format);
790         if (ad->data_date_fmt) {
791                 ad->data_date_fmt->int_slp_setting_binded =
792                     INT_SLP_SETTING_DATE_FORMAT;
793         } else {
794                 SETTING_TRACE_ERROR("ad->data_date_fmt is NULL");
795         }
796         G_FREE(pa_date_format);
797 #endif
798
799         if (auto_flag) {
800                 if (ad->data_tz) {
801                         setting_disable_genlist_item(ad->data_tz->item);
802                 }
803                 if (ad->data_time) {
804                         setting_disable_genlist_item(ad->data_time->item);
805                 }
806         }
807
808         setting_view_time_main.is_create = 1;
809         vconf_notify_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED, time_changed_callback, (void *)ad);
810
811         /*  register vconf key to get event callback for converting 12/24H */
812         vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, setting_time_main_int_vconf_change_cb, ad);
813         /*  register vconf key to get event callback for date format */
814         vconf_notify_key_changed(VCONFKEY_SETAPPL_DATE_FORMAT_INT, setting_time_main_int_vconf_change_cb, ad);
815
816         vconf_notify_key_changed(VCONFKEY_TELEPHONY_NITZ_GMT, __sim_time_info_chg_cb, ad);
817         vconf_notify_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, __sim_time_info_chg_cb, ad);
818         vconf_notify_key_changed(VCONFKEY_TELEPHONY_NITZ_ZONE, __sim_time_info_chg_cb, ad);
819
820         vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
821                                  setting_time_main_bool_vconf_change_cb, ad);
822
823         /* to avoid update time-diff, every per 3s to update. */
824 #ifdef USE_TIMER_UPDATE_TIME_IN_TIME_VIEW
825         ad->update_timer = ecore_timer_add(3, (Ecore_Task_Cb) __refresh_date_timer, ad);
826 #endif
827
828         /* eventsystem */
829         if (ES_R_OK != eventsystem_register_event(SYS_EVENT_HOUR_FORMAT,
830                                                   &hour_format_event_reg_id,
831                                                   (eventsystem_handler)hour_format_event_handler, cb)) {
832                 SETTING_TRACE_ERROR("error");
833         }
834
835         return SETTING_RETURN_SUCCESS;
836 }
837
838 static int setting_time_main_destroy(void *cb)
839 {
840         SETTING_TRACE_BEGIN;
841         /* error check */
842         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
843
844         SettingTimeUG *ad = (SettingTimeUG *) cb;
845
846         /*      __finalize_resource(ad); */
847         if (ad->caller)
848                 FREE(ad->caller);
849
850 #ifdef USE_TIMER_UPDATE_TIME_IN_TIME_VIEW
851         if (ad->update_timer) {
852                 ecore_timer_del(ad->update_timer);
853                 ad->update_timer = NULL;
854         }
855 #endif
856
857 #if PROGRESS_POPUP_SHOW
858         if (ad->pop_progress) {
859                 evas_object_del(ad->pop_progress);
860                 ad->pop_progress = NULL;
861         }
862 #endif
863
864         if (ad->update_timezone_idler) {
865                 ecore_idler_del(ad->update_timezone_idler);
866                 ad->update_timezone_idler = NULL;
867         }
868
869         /* timer */
870         if (ad->refresh_time_idler) {
871                 ecore_idler_del(ad->refresh_time_idler);
872                 ad->refresh_time_idler = NULL;
873         }
874
875         vconf_ignore_key_changed(VCONFKEY_SYSTEM_TIME_CHANGED,
876                                  time_changed_callback);
877
878         vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224,
879                                  setting_time_main_int_vconf_change_cb);
880         vconf_ignore_key_changed(VCONFKEY_SETAPPL_DATE_FORMAT_INT,
881                                  setting_time_main_int_vconf_change_cb);
882
883         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_NITZ_GMT, __sim_time_info_chg_cb);
884         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, __sim_time_info_chg_cb);
885         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_NITZ_ZONE, __sim_time_info_chg_cb);
886
887         vconf_ignore_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
888                                  setting_time_main_bool_vconf_change_cb);
889
890         if (ad->ly_main != NULL) {
891                 evas_object_del(ad->ly_main);
892                 ad->ly_main = NULL;
893                 setting_view_time_main.is_create = 0;
894         }
895
896         if (ES_R_OK != eventsystem_unregister_event(hour_format_event_reg_id)) {
897                 SETTING_TRACE_ERROR("error");
898         }
899
900         return SETTING_RETURN_SUCCESS;
901 }
902
903 static int setting_time_main_update(void *cb)
904 {
905         SETTING_TRACE_BEGIN;
906         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
907
908         SettingTimeUG *ad = (SettingTimeUG *) cb;
909
910         if (ad->ly_main != NULL) {
911                 evas_object_show(ad->ly_main);
912                 /* update timezone */
913                 if (ad->update_timezone_idler) {
914                         ecore_idler_del(ad->update_timezone_idler);
915                         ad->update_timezone_idler = NULL;
916                 }
917                 ad->update_timezone_idler =
918                     ecore_idler_add((Ecore_Task_Cb) __update_timezone_idler, ad);
919                 /* update time */
920                 if (ad->refresh_time_idler) {
921                         ecore_idler_del(ad->refresh_time_idler);
922                         ad->refresh_time_idler = NULL;
923                 }
924                 ad->refresh_time_idler = ecore_idler_add(setting_update_datefield_cb, ad);
925         }
926         return SETTING_RETURN_SUCCESS;
927 }
928
929 static int setting_time_main_cleanup(void *cb)
930 {
931         SETTING_TRACE_BEGIN;
932         return SETTING_RETURN_SUCCESS;
933 }
934
935
936 /* ***************************************************
937  **
938  **general func
939  **
940  ****************************************************/
941
942 int setting_time_check_automatic_time_update_state()
943 {
944         SETTING_TRACE_BEGIN;
945         int bvalue = 0;
946         int err = 0;
947
948         setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
949                                  &bvalue, &err);
950
951         return bvalue;
952
953 }
954
955 static int __setting_set_city_tzone(const char *pTZPath)
956 {
957         /* let's get city & timezone string from pTZPath */
958         /* 1. city name */
959         char szTimezone[GMT_BUF_SIZE] = {0,};
960         char *pStr = strdup(pTZPath);
961
962         /* 2. timezone string +/-<n> ex. +9, -1 */
963         get_gmt_offset(szTimezone, GMT_BUF_SIZE);
964
965         /* set timezone_id */
966         /** @todo replace with vconf ID */
967         vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_ID, pStr);
968         FREE(pStr);
969
970         return TRUE;
971 }
972
973 static void
974 setting_time_main_launch_worldclock_result_ug_cb(ui_gadget_h ug,
975                                                  app_control_h result, void *priv)
976 {
977         SETTING_TRACE_BEGIN;
978         /* error check */
979         retm_if(priv == NULL, "Data parameter is NULL");
980
981         SettingTimeUG *ad = (SettingTimeUG *) priv;
982
983         char *city = NULL;
984         char *tzpath = NULL;
985         app_control_get_extra_data(result, "city", &city);
986         app_control_get_extra_data(result, "tzpath", &tzpath);
987
988         if (!tzpath) {
989                 SETTING_TRACE("tzpath from worldclock UG is null.");
990                 setting_update_timezone(ad);
991                 return;
992         } else {
993                 SETTING_TRACE("tzpath : %s", tzpath);
994         }
995         SETTING_TRACE("city : %s", city);
996
997         /* --------------------------------------------------------------------- */
998         char tz_path[MAX_COMMON_BUFFER_LEN / 4 + 1];
999         safeCopyStr(tz_path, SETTING_TIME_ZONEINFO_PATH, MAX_COMMON_BUFFER_LEN / 4);
1000         g_strlcat(tz_path, tzpath, sizeof(tz_path));
1001         SETTING_TRACE("full tz_path:%s", tz_path);
1002
1003         int ret = _alarmmgr_set_timezone_helper(tz_path);
1004         if (ret < 0) {
1005                 SETTING_TRACE("tzpath is not valid.");
1006                 if (tzpath)
1007                         FREE(tzpath);
1008                 if (city)
1009                         FREE(city);
1010                 return;
1011         } else
1012                 SETTING_TRACE("_alarmmgr_set_timezone_helper - successful : %s \n", tz_path);
1013
1014         ret = vconf_set_str(VCONFKEY_SETAPPL_CITYNAME_INDEX_INT, city);
1015         setting_retm_if(ret != 0, "set vconf failed");
1016
1017         /* parse city and GMT offset from tzpath and system time property */
1018         /* set the strings in vconf which will be used while updating display of timezone */
1019         if (!__setting_set_city_tzone(tzpath)) {
1020                 SETTING_TRACE("__setting_set_city_tzone ERROR");
1021                 if (tzpath)
1022                         FREE(tzpath);
1023                 if (city)
1024                         FREE(city);
1025                 return;
1026         }
1027
1028         /* update the display for timezone */
1029         setting_update_timezone(ad);
1030         static int t_event_val = -1;
1031         vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED, t_event_val);
1032         if (tzpath)
1033                 FREE(tzpath);
1034         if (city)
1035                 FREE(city);
1036         SETTING_TRACE_END;
1037 }
1038
1039 void setting_time_main_launch_worldclock_destroy_ug_cb(ui_gadget_h ug,
1040                                                        void *priv)
1041 {
1042         SETTING_TRACE_BEGIN;
1043         /* if(ug) ug_destroy(ug); */
1044         /* restore the '<-' button on the navigate bar */
1045         ret_if(!priv);
1046         SettingTimeUG *ad = (SettingTimeUG *) priv;
1047         if (ug) {
1048                 setting_ug_destroy(ug);
1049                 ad->ug_loading = NULL;
1050         }
1051         Elm_Object_Item *navi_it = elm_naviframe_top_item_get(ad->navi_bar);
1052         ret_if(!navi_it);
1053
1054         elm_object_tree_focus_allow_set(ad->ly_main, EINA_TRUE);
1055 }
1056
1057 void setting_time_main_launch_worldclock_layout_ug_cb(ui_gadget_h ug,
1058                                                       enum ug_mode mode,
1059                                                       void *priv)
1060 {
1061         SETTING_TRACE_BEGIN;
1062         /*SettingTimeUG *ad = (SettingTimeUG *) priv; */
1063         Evas_Object *base;
1064
1065         if (!priv)
1066                 return;
1067
1068         base = (Evas_Object *) ug_get_layout(ug);
1069         if (!base)
1070                 return;
1071
1072         switch (mode) {
1073                 case UG_MODE_FULLVIEW:
1074                         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
1075                                                          EVAS_HINT_EXPAND);
1076                         /*elm_win_resize_object_add(ad->win_get, base); */
1077                         evas_object_show(base);
1078                         break;
1079                 default:
1080                         break;
1081         }
1082         SETTING_TRACE_END;
1083 }
1084
1085
1086 void setting_time_main_launch_worldclock_sg(void *data)
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         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
1095         setting_retm_if(!cbs, "calloc failed");
1096         cbs->layout_cb = setting_time_main_launch_worldclock_layout_ug_cb;
1097         cbs->result_cb = setting_time_main_launch_worldclock_result_ug_cb;
1098         cbs->destroy_cb = setting_time_main_launch_worldclock_destroy_ug_cb;
1099         cbs->priv = (void *)ad;
1100
1101         elm_object_tree_focus_allow_set(ad->ly_main, EINA_FALSE);
1102         SETTING_TRACE("to load ug[%s]", "worldclock-efl");
1103         ad->ug_loading =
1104             setting_ug_create(ad->ug, "worldclock-efl", UG_MODE_FULLVIEW, NULL, cbs);
1105         if (NULL == ad->ug_loading) {   /* error handling */
1106         }
1107
1108         FREE(cbs);
1109         return;
1110 }
1111
1112 static void __setting_update_datefield_cb(void *cb)
1113 {
1114         SETTING_TRACE_BEGIN;
1115         retvm_if(cb == NULL, EINA_FALSE, "Data parameter is NULL");
1116         SettingTimeUG *ad = (SettingTimeUG *) cb;
1117
1118         if (ad->data_time) {
1119                 time_t cctime = time(NULL);
1120                 struct tm ts_ret;
1121                 struct tm *ts = localtime_r(&cctime, &ts_ret);
1122                 SETTING_TRACE("year : %d, month : %d, day : %d, hour : %d, min : %d",
1123                               ts_ret.tm_year, ts_ret.tm_mon, ts_ret.tm_mday, ts_ret.tm_hour, ts_ret.tm_min);
1124                 if (ts) {
1125
1126                         char* region = NULL;
1127                         int err = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &region );
1128                         char *date = __setting_phone_lang_get_by_pattern(region, "MMM/d/yyyy");
1129                         char *time;
1130                         static char date_arr[DEF_BUF_SIZE], time_arr[DEF_BUF_SIZE];
1131                         char *pa_time_format = get_pa_time_format_str();
1132                         setting_retvm_if(NULL == pa_time_format, FALSE, "pa_time_format is NULL");
1133                         if (!strncmp(pa_time_format, "IDS_ST_BODY_12_HOURS", strlen("IDS_ST_BODY_12_HOURS")))
1134                                 time = __setting_phone_lang_get_by_pattern(region, "hhmm");
1135                         else
1136                                 time = __setting_phone_lang_get_by_pattern(region, "Hm");
1137
1138                         strncpy(date_arr, date, DEF_BUF_SIZE);
1139                         strncpy(time_arr, time, DEF_BUF_SIZE);
1140
1141                         date_arr[DEF_BUF_SIZE - 1] = '\0';
1142                         time_arr[DEF_BUF_SIZE - 1] = '\0';
1143
1144                         if (ad->data_time->btn_left)
1145                                 elm_object_text_set(ad->data_time->btn_left, date_arr);
1146                         else {
1147                                 SETTING_TRACE_ERROR("ad->data_time->btn_left is NULL");
1148                         }
1149                         if (ad->data_time->btn_right)
1150                                 elm_object_text_set(ad->data_time->btn_right, time_arr);
1151                         else {
1152                                 SETTING_TRACE_ERROR("ad->data_time->btn_right is NULL");
1153                         }
1154                         SETTING_TRACE_BEGIN;
1155                         FREE(pa_time_format);
1156                 }
1157         }
1158 }
1159
1160 Eina_Bool setting_update_datefield_cb(void *data)
1161 {
1162         SETTING_TRACE_BEGIN;
1163         /* error check */
1164         retvm_if(data == NULL, FALSE, "Data parameter is NULL");
1165
1166         SettingTimeUG *ad = (SettingTimeUG *) data;
1167
1168         __setting_update_datefield_cb(ad);
1169
1170         ad->refresh_time_idler = NULL;
1171         return ECORE_CALLBACK_CANCEL;
1172 }
1173
1174 /* ***************************************************
1175  **
1176  **call back func
1177  **
1178  ****************************************************/
1179
1180 void
1181 setting_time_main_click_softkey_left_cb(void *data, Evas_Object *obj,
1182                                         void *event_info)
1183 {
1184         SETTING_TRACE_BEGIN;
1185         retm_if(data == NULL, "Data parameter is NULL");
1186
1187         SettingTimeUG *ad = (SettingTimeUG *) data;
1188
1189         /*__finalize_resource(ad); */
1190
1191         /* Send destroy request */
1192         ug_destroy_me(ad->ug);
1193 }
1194
1195 /* ***************************************************
1196  **
1197  **call back func
1198  **
1199  ****************************************************/
1200
1201 void
1202 setting_time_main_click_softkey_caller_exist_left_cb(void *data,
1203                                                      Evas_Object *obj,
1204                                                      void *event_info)
1205 {
1206         SETTING_TRACE_BEGIN;
1207         /* error check */
1208         retm_if(data == NULL, "Data parameter is NULL");
1209
1210         SettingTimeUG *ad = (SettingTimeUG *) data;
1211
1212         /*  Create Bundle and send message */
1213         app_control_h svc;
1214         if (app_control_create(&svc))
1215                 return;
1216
1217         app_control_add_extra_data(svc, "result", "lbutton_click");
1218         ug_send_result(ad->ug, svc);
1219
1220         app_control_destroy(svc);
1221
1222         /* Send destroy request */
1223         ug_destroy_me(ad->ug);
1224 }
1225
1226 void
1227 setting_time_main_click_softkey_caller_exist_right_cb(void *data,
1228                                                       Evas_Object *obj,
1229                                                       void *event_info)
1230 {
1231         SETTING_TRACE_BEGIN;
1232         /* error check */
1233         retm_if(data == NULL, "Data parameter is NULL");
1234
1235         SettingTimeUG *ad = (SettingTimeUG *) data;
1236
1237         /*  Create Bundle and send message */
1238         app_control_h svc;
1239         if (app_control_create(&svc))
1240                 return;
1241
1242         app_control_add_extra_data(svc, "result", "rbutton_click");
1243         ug_send_result(ad->ug, svc);
1244
1245         app_control_destroy(svc);
1246
1247         /* Send destroy request */
1248         ug_destroy_me(ad->ug);
1249 }
1250
1251
1252 static void __time_auto_update(void *data)
1253 {
1254         SETTING_TRACE_BEGIN;
1255         setting_retm_if(data == NULL, "Data parameter is NULL");
1256         SettingTimeUG *ad = (SettingTimeUG *) data;
1257         int err;
1258         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1259                                  SETTING_ON_OFF_BTN_ON, &err);
1260         if (ad->data_tz) {
1261                 setting_disable_genlist_item(ad->data_tz->item);
1262         }
1263
1264 #if 0
1265         elm_object_text_set(ad->data_time->btn_left, date);
1266         evas_object_resize(ad->data_time->btn_left, 300, -1);
1267 #endif
1268
1269 #if 1
1270         if (ad->data_time) {
1271                 ad->data_time->isItemDisableFlag = FALSE;
1272                 /*setting_disable_evas_object(ad->data_time->eo_check); */
1273                 setting_disable_evas_object(ad->data_time->btn_left);
1274                 setting_disable_evas_object(ad->data_time->btn_right);
1275                 setting_disable_genlist_item(ad->data_time->item);
1276         }
1277 #endif
1278         __update_time_via_sim_card(ad);
1279         SETTING_TRACE_END;
1280 }
1281
1282 static void
1283 setting_time_main_chk_btn_cb(void *data, Evas_Object *obj, void *event_info)
1284 {
1285         SETTING_TRACE_BEGIN;
1286         /* error check */
1287         retm_if(data == NULL, "Data parameter is NULL");
1288         SettingTimeUG *ad = NULL;
1289         Setting_GenGroupItem_Data *list_item =
1290             (Setting_GenGroupItem_Data *) data;
1291         ad = list_item->userdata;
1292         list_item->chk_status = elm_check_state_get(obj);       /*  for genlist update status */
1293
1294         int err;
1295         if (obj == ad->data_auto->eo_check) {
1296                 int bvalue;
1297                 setting_get_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1298                                          &bvalue, &err);
1299                 if (bvalue) {
1300                         (void) setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE, SETTING_ON_OFF_BTN_OFF, &err);
1301                         setting_update_gl_item_chk_status(ad->data_auto, 0);
1302
1303                         if (ad->data_tz) {
1304                                 setting_enable_genlist_item(ad->data_tz->item);
1305                         }
1306
1307                         if (ad->data_time) {
1308                                 ad->data_time->isItemDisableFlag = FALSE;
1309                                 /*setting_disable_evas_object(ad->data_time->eo_check); */
1310                                 setting_enable_evas_object(ad->data_time->btn_left);
1311                                 setting_enable_evas_object(ad->data_time->btn_right);
1312                                 setting_enable_genlist_item(ad->data_time->item);
1313                         }
1314                 } else {
1315                         __time_auto_update(ad);
1316                 }
1317         }
1318
1319
1320
1321 //#define FUNCTION_SYSTEM_SETTING 1
1322 #if FUNCTION_SYSTEM_SETTING
1323         /* Time format */
1324         if (ad->data_time_fmt == list_item) {
1325                 int err = 0;
1326                 int value = VCONFKEY_TIME_FORMAT_12;
1327                 if (list_item->chk_status) {
1328                         err = system_settings_set_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, true);
1329                 } else {
1330                         err = system_settings_set_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, false);
1331                 }
1332         }
1333 #else
1334         /* Time format */
1335         if (ad->data_time_fmt == list_item) {
1336                 int err = 0;
1337                 int value = VCONFKEY_TIME_FORMAT_12;
1338                 if (list_item->chk_status) {
1339                         value = VCONFKEY_TIME_FORMAT_24;
1340                         setting_set_event_system(SYS_EVENT_HOUR_FORMAT, EVT_KEY_HOUR_FORMAT, EVT_VAL_HOURFORMAT_24);
1341                 } else {
1342                         setting_set_event_system(SYS_EVENT_HOUR_FORMAT, EVT_KEY_HOUR_FORMAT, EVT_VAL_HOURFORMAT_12);
1343                 }
1344                 setting_set_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224, value, &err);
1345         }
1346 #endif
1347         SETTING_TRACE_END;
1348 }
1349
1350 static void setting_time_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj,
1351                                                        void *event_info)
1352 {
1353         SETTING_TRACE_BEGIN;
1354         /* error check */
1355         setting_retm_if(data == NULL, "Data parameter is NULL");
1356
1357         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
1358         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
1359         elm_genlist_item_selected_set(item, 0);
1360         Setting_GenGroupItem_Data *list_item =
1361             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
1362         setting_retm_if(NULL == list_item, "list_item is NULL");
1363
1364         SettingTimeUG *ad = (SettingTimeUG *) data;
1365         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
1366
1367         if (ad->data_tz == list_item) {
1368                 ad->is_datefield_selected = EINA_FALSE;
1369                 if (1 == setting_time_check_automatic_time_update_state()) {
1370                         setting_create_popup(ad, ad->win_get, NULL, _(AUTO_TIME_UPDATE_ON), NULL, 0, false, false, 0);
1371                 } else {
1372                         setting_time_main_launch_worldclock_sg(ad);
1373                 }
1374
1375         } else if (ad->data_time == list_item) {
1376                 ad->is_datefield_selected = EINA_TRUE;
1377                 if (1 == setting_time_check_automatic_time_update_state()) {
1378                         setting_create_popup(ad, ad->win_get, NULL, _(AUTO_TIME_UPDATE_ON), NULL, 0, false, false, 0);
1379                 } else {
1380                         /* setting_view_change(&setting_view_time_main, &setting_view_time_time, ad); */
1381                 }
1382         } else if (ad->data_auto == list_item) {
1383                 ad->is_datefield_selected = EINA_FALSE;
1384                 int old_status = list_item->chk_status;
1385                 /* new status */
1386                 setting_update_gl_item_chk_status(list_item, !old_status);
1387                 int err = 0;
1388                 if (!list_item->chk_status) {
1389                         (void)
1390                         setting_set_bool_slp_key
1391                         (BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1392                          SETTING_ON_OFF_BTN_OFF, &err);
1393
1394                         if (ad->data_tz) {
1395                                 setting_enable_genlist_item(ad->data_tz->item);
1396                         }
1397
1398                         if (ad->data_time) {
1399                                 ad->data_time->isItemDisableFlag = FALSE;
1400                                 setting_enable_evas_object(ad->data_time->eo_check);
1401                                 setting_enable_genlist_item(ad->data_time->item);
1402                         }
1403
1404                 } else {
1405                         __time_auto_update(ad);
1406                 }
1407         } else if (ad->data_time_fmt == list_item)      {
1408                 int err = 0;
1409                 setting_update_gl_item_chk_status(list_item, !list_item->chk_status);
1410                 int value = VCONFKEY_TIME_FORMAT_12;
1411                 if (list_item->chk_status)
1412                         value = VCONFKEY_TIME_FORMAT_24;
1413                 setting_set_int_slp_key(INT_SLP_SETTING_REGIONFORMAT_TIME1224, value, &err);
1414         }
1415 }
1416
1417
1418 void setting_time_main_btn_update_ok_cb(void *cb)
1419 {
1420         SETTING_TRACE_BEGIN;
1421         SettingTimeUG *ad = (SettingTimeUG *) cb;
1422         int err;                /* error check */
1423         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1424                                  SETTING_ON_OFF_BTN_ON, &err);
1425         setting_update_gl_item_chk_status(ad->data_auto, 1);
1426
1427         deviced_request_reboot();
1428
1429         /*  We'd better check return value from deviced_request_reboot() function. */
1430         /*  It will return 0 on success or -1 if failed. */
1431 }
1432
1433
1434 void setting_time_main_btn_update_cancel_cb(void *cb)
1435 {
1436         SETTING_TRACE_BEGIN;
1437         SettingTimeUG *ad = (SettingTimeUG *) cb;
1438
1439         int err;                /* error check */
1440
1441         retm_if(cb == NULL, "Data parameter is NULL");
1442         setting_set_bool_slp_key(BOOL_SLP_SETTING_AUTOMATIC_TIME_UPDATE,
1443                                  SETTING_ON_OFF_BTN_OFF, &err);
1444         setting_update_gl_item_chk_status(ad->data_auto, 0);
1445
1446 }
1447
1448
1449 static UChar *uastrcpy(const char *chars)
1450 {
1451         int len = 0;
1452         UChar *str = NULL;
1453         len = safeStrLen(chars);
1454         str = (UChar *) malloc(sizeof(UChar)*(len + 1));
1455         if (!str)
1456                 return NULL;
1457         u_uastrcpy(str, chars);
1458         return str;
1459 }
1460
1461 static void ICU_set_timezone(const char *timezone)
1462 {
1463         SETTING_TRACE("ICU_set_timezone = %s ", timezone);
1464         UErrorCode ec = U_ZERO_ERROR;
1465         UChar *str = uastrcpy(timezone);
1466
1467         if (str) {
1468                 ucal_setDefaultTimeZone(str, &ec);
1469                 if (U_SUCCESS(ec)) {
1470                         SETTING_TRACE("ucal_setDefaultTimeZone() SUCCESS ");
1471                 } else {
1472                         SETTING_TRACE("ucal_setDefaultTimeZone() FAILED : %s ",
1473                                         u_errorName(ec));
1474                 }
1475                 FREE(str);
1476         }
1477 }
1478
1479 /**
1480  * ex) timezone : Asia/Seoul --> returns "+09"
1481  *
1482  * char outstr2[4];
1483  */
1484 static void get_gmt_offset(char *str_buf, int size)
1485 {
1486         /* timezone string +/-<n> ex. +9, -1 */
1487         time_t t = time(0);     /* get unix time. sec. */
1488
1489         struct tm *data;
1490         data = localtime(&t);           /* save time as structure. */
1491
1492         setting_retm_if(!data, "data is NULL");
1493
1494         int gmtoffset_hour = (data->tm_gmtoff) / 3600;          /* result : hour. */
1495         int gmtoffset_min = ((data->tm_gmtoff) % 3600) / 60;    /* result : min. */
1496
1497 #if 0
1498         if (gmtoffset_min != 0) {
1499                 SETTING_TRACE("gmtoffset_min != 0 -------> %d", gmtoffset_min);
1500                 gmtoffset_min = 30;
1501         }
1502 #endif
1503
1504         /*snprintf(str_buf, size, "%+d:%02u", gmtoffset_hour, gmtoffset_min); */
1505         snprintf(str_buf, size, "%+d:%02d", gmtoffset_hour, abs(gmtoffset_min));
1506         SETTING_TRACE_DEBUG("szTimezone is of a valid format: GMT: %s", str_buf);
1507
1508         int ret = vconf_set_str(VCONFKEY_SETAPPL_TIMEZONE_INT, str_buf);
1509         if (ret == -1) {
1510                 SETTING_TRACE_ERROR("set vconf failed");
1511         }
1512
1513 }
1514
1515
1516 /* automatic */
1517 static char *get_timezone_str()
1518 {
1519         SETTING_TRACE_BEGIN;
1520
1521         enum { BUFFERSIZE = 1024 };
1522         char buf[BUFFERSIZE];
1523         ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf) - 1);
1524
1525         if (len != -1) {
1526                 buf[len] = '\0';
1527         } else {
1528                 /* handle error condition */
1529         }
1530         return g_strdup(buf + 20);              /*Asia/Seoul */
1531 }
1532
1533 static char *get_city_name_result()
1534 {
1535         return s_result;
1536 }
1537
1538
1539 /* in_str = "Asia/Seoul" */
1540 static bool get_city_name(char *tzname)
1541 {
1542         sqlite3 *pSQLite3 = NULL;
1543         char    *szErrMsg = NULL;
1544
1545         int rst = sqlite3_open("/opt/dbspace/.worldclock.db", &pSQLite3);
1546         if (rst) {
1547                 SETTING_TRACE("Can't open database: %s", sqlite3_errmsg(pSQLite3));
1548                 sqlite3_close(pSQLite3);
1549                 pSQLite3 = NULL;
1550                 return false;
1551         } else {
1552                 SETTING_TRACE("Database opened!!");
1553                 char query_str[DEF_BUF_SIZE];
1554                 snprintf(query_str, DEF_BUF_SIZE, "SELECT city FROM city_table where tz_path=\"%s\"", tzname);
1555                 SETTING_TRACE("%s \n", query_str);
1556                 rst = sqlite3_exec(pSQLite3, query_str, query_callback, 0, &szErrMsg);
1557         }
1558
1559         sqlite3_free(szErrMsg);
1560         sqlite3_close(pSQLite3);
1561         SETTING_TRACE("Database close!!\n");
1562         return true;
1563 }
1564
1565 static int query_callback(void *NotUsed, int argc, char **argv, char **azColName)
1566 {
1567         int i = 0;
1568         SETTING_TRACE("%s\n", argv[i] ? argv[i] : "NULL");
1569         s_result = g_strdup(argv[i] ? argv[i] : "NULL");
1570         return 0;
1571 };
1572
1573 static char *get_timezone_displayname()
1574 {
1575         SETTING_TRACE_BEGIN;
1576
1577         UErrorCode status = U_ZERO_ERROR;;
1578         UChar displayName[SETTING_STR_SLP_LEN] = {0, };
1579         char *timezone_str = NULL;
1580         const char *localeID = uloc_getDefault();
1581
1582         time_t t = time(0);
1583         struct tm *data = localtime(&t);
1584
1585         ICU_set_timezone(get_timezone_str());
1586
1587         UCalendar *cal = ucal_open(NULL, -1, localeID, UCAL_GREGORIAN, &status);
1588         if (U_FAILURE(status))
1589                 SETTING_TRACE_ERROR("ucal_open got an error : %d", status);
1590
1591         status = U_ZERO_ERROR;
1592
1593         /* get timezone display name (check dst) */
1594         if (cal) {
1595                 if (data) {
1596                         if (data->tm_isdst)
1597                                 ucal_getTimeZoneDisplayName(cal, UCAL_DST, localeID, displayName, SETTING_STR_SLP_LEN, &status);
1598                         else
1599                                 ucal_getTimeZoneDisplayName(cal, UCAL_STANDARD, localeID, displayName, SETTING_STR_SLP_LEN, &status);
1600                 }
1601
1602                 if (U_FAILURE(status)) {
1603                         SETTING_TRACE_ERROR("ucal_getTimeZoneDisplayName got an error : %s, %d", displayName, status);
1604                         status = U_ZERO_ERROR;
1605                 } else {
1606                         int len = 0;
1607                         int len_str = 0;
1608                         int len_utf8 = 0;
1609
1610                         len = u_strlen(displayName);
1611                         len_str = sizeof(char)*4*(len+1);
1612                         timezone_str = (char *)calloc(1, len_str);
1613                         if (!timezone_str) {
1614                                 SETTING_TRACE_ERROR("timezone string is null");
1615                                 return NULL;
1616                         }
1617                         u_strToUTF8(timezone_str, len_str, &len_utf8, displayName, len, &status);
1618                         SETTING_TRACE("*************************** timezone : %s, %s", displayName, timezone_str);
1619                 }
1620                 ucal_close(cal);
1621         }
1622
1623         SETTING_TRACE_END;
1624         return timezone_str;
1625
1626 }
1627