upload codes for TIZEN 2.0
[apps/home/clock.git] / worldclock / src / worldclock_main_view.c
1 /*
2 *
3 * Copyright 2012  Samsung Electronics Co., Ltd
4 *
5 * Licensed under the Flora License, Version 1.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://www.tizenopensource.org/license
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <stdio.h>
20 #include <appcore-efl.h>
21 #include <Elementary.h>
22 #include <Ecore_X.h>
23 #include <vconf.h>
24 #include <glib-object.h>
25
26 #include "worldclock.h"
27 #include "worldclock_data.h"
28 #include "worldclock_dlog.h"
29 #include "worldclock_gmt.h"
30 #include "worldclock_util.h"
31 #include "worldclock_fwk_icu.h"
32 #include "worldclock_add_view.h"
33 #include "worldclock_edit_view.h"
34 #include "worldclock_main_view.h"
35 #include "worldclock_detail_view.h"
36 #include "worldclock_ug_add_view.h"
37
38 #define USING_WORLDCLOCK_UG 1
39
40 /**********************************************************************
41 ******************define, struct ,typedef, union, enum, global val *************************************
42 ***********************************************************************/
43 //#define SECOND_UPDATE
44 typedef struct {
45         int x;
46         int y;
47         int changed;
48 } mouse_pos_t;
49
50 typedef struct {
51         Evas_Object *eo_parent; //parent
52         Evas_Object *eo_navibar;        //navi_bar
53         Elm_Object_Item *ei_naviframe;
54         Evas_Object *eo_controlbar;     //controlbar
55         Evas_Object *eo_btn_lft, *eo_btn_rht;   //button
56         Elm_Object_Item *eci_btn_lft, *eci_btn_rht, *eci_btn_mid ;
57         Evas_Object *eo_btn_back;       //button back
58         Evas_Object *eo_layout; //layout
59         Evas_Object *eo_content;        //content
60         Evas_Object *ctxpopup;
61         //
62         Evas_Object *eo_genlist;        //genlist
63         /*int total_number;     //total number */
64         Ecore_Timer *timer;
65         struct appdata *ad;
66         mouse_pos_t mouse_pos;
67         Wcl_Return_Cb return_cb;
68         Elm_Object_Item *egi_edit;      //For store the main selected item;
69         Eina_Bool bIsReorderView;
70 } view_data;
71
72 /**********************************************************************
73 ******************Local function declear, extern function declear*************************************
74 ***********************************************************************/
75 static void __mainview_quit_cb(void *data, Evas_Object * obj, void *event_info);
76 static void __mainview_buttons_state_update(view_data * view);
77 static void __mainview_return_cb(Eina_Bool isReload);
78 static void __mainview_addview_return_cb(void *data, Eina_Bool isReload);
79 static void __mainview_show_addview_cb(void *data, Evas_Object * obj,
80                                        void *event_info);
81 static void __mainview_back_button_cb(void *data, Evas_Object * obj,
82                                       void *event_info);
83 static void __mainview_editview_return_cb(void *data, Eina_Bool isReload);
84 static void __mainview_show_editview_cb(void *data, Evas_Object * obj,
85                                         void *event_info);
86 static void __mainview_detailview_return_cb(void *data, Eina_Bool isReload);
87 static void __mainview_show_detailview_cb(void *data, Evas_Object * obj,
88                                           void *event_info);
89 static int __mainview_genlist_update_clock(void *data);
90 static char *__mainview_genlist_label_get(void *data, Evas_Object * obj,
91                                           const char *part);
92 static Evas_Object *__mainview_genlist_icon_get(void *data, Evas_Object * obj,
93                                                 const char *part);
94 static void __mainview_genlist_item_sel(void *data, Evas_Object * obj,
95                                         void *event_info);
96 static Evas_Object *__mainview_genlist_add(Evas_Object * parent, void *data);
97 static Eina_Bool __mainview_timer_cb(void *data);
98 static int __mainview_genlist_create(view_data * view);
99 static int __mainview_genlist_update(view_data * view);
100
101 /**********************************************************************
102 ******************Global val , static global val*************************************
103 ***********************************************************************/
104
105 static view_data *g_view_data = NULL;
106 static Elm_Genlist_Item_Class g_ts = {
107         .item_style = "worldclock_mainview_list",
108         .func.text_get = __mainview_genlist_label_get,
109         .func.content_get = __mainview_genlist_icon_get,
110         .func.state_get = NULL,
111         .func.del = NULL,
112 };
113
114 /**********************************************************************
115 ******************Local function ref*************************************
116 ***********************************************************************/
117 /**
118  *
119  *
120  * @param[in]  data
121  * @param[in]  obj
122  * @param[in]  event_info
123  * @return
124  */
125 static void __mainview_quit_cb(void *data, Evas_Object * obj, void *event_info)
126 {
127         ret_if(!g_view_data);
128         view_data *view = g_view_data;
129         struct appdata *ad = view->ad;
130         ret_if(!ad);
131
132         // reset pointer to callback function
133         if (view->return_cb) {
134                 view->return_cb(data, EINA_FALSE);
135                 view->return_cb = NULL;
136         }
137         // remove default list used to store all appended cities
138         if (ad->eina_list) {
139                 worldclock_util_glist_remove_all(ad->eina_list, EINA_TRUE);
140                 ad->eina_list = NULL;
141         }
142         ECORE_TIMER_DELIF(view->timer);
143         EVAS_OBJECT_DELIF(view->eo_layout);
144         FREEIF(g_view_data);
145 }
146
147 /**
148  * Update the status about edit button according to the number of items in genlist
149  * If the number is zero, disable edit button
150  * If the number is at least one, enable edit button
151  *
152  * @return
153  */
154 static void __mainview_buttons_state_update(view_data * view)
155 {
156         ret_if(!view || !view->eo_genlist);
157         // get the count number of genlist
158         CLK_FUN_BEG();
159         int count = elm_genlist_items_count(view->eo_genlist);
160         Eina_Bool bVisible =
161             count >= WORLDCLOCK_MAX_CITY_COUNT ? EINA_FALSE : EINA_TRUE;
162         elm_object_item_disabled_set(view->eci_btn_rht, !bVisible);
163         bVisible = count >= 1 ? EINA_TRUE : EINA_FALSE;
164         elm_object_item_disabled_set(view->eci_btn_mid, !bVisible);
165         CLK_FUN_END();
166 }
167
168 /**
169  * Callback function used for relaunch mainview when exit from the next view
170  *
171  * @param[in]  data      Data used in this function
172  *
173  * @return
174  */
175 static void __mainview_return_cb(Eina_Bool isReload)
176 {
177         ret_if(!g_view_data);
178         view_data *view = g_view_data;
179         struct appdata *ad = view->ad;
180         ret_if(!ad);
181
182         if (isReload) {
183                 __mainview_genlist_create(view);
184         } else {
185                 __mainview_genlist_update(view);
186         }
187         // set controlbar visible
188         if (ad->controlbar_cb) {
189                 ad->controlbar_cb(EINA_TRUE);
190         }
191         // update the status of edit button
192         if (!view->bIsReorderView) {
193                 __mainview_buttons_state_update(view);
194         }
195         // capture idle image
196         if (ad->idle_capture_cb) {
197                 ad->idle_capture_cb();
198         }
199 }
200
201 /**
202  * Callback function used for append new city from add view
203  *
204  * @param[in]  data      Data used in this function
205  *
206  * @return
207  */
208 static void __mainview_addview_return_cb(void *data, Eina_Bool isReload)
209 {
210         ret_if(!g_view_data);
211         view_data *view = g_view_data;
212         struct appdata *ad = view->ad;
213         ret_if(!ad);
214
215         ad->page = WCL_PAGE_MAIN;
216         if (!data) {
217                 return __mainview_return_cb(isReload);
218         }
219         int ret = 0;
220         // Get data of new city
221         Wcl_CitySet *cs = (Wcl_CitySet *) data;
222         // prepend selected city into city list
223         ret = worldclock_insert_city_to_list(&(ad->eina_list), cs, 0);
224         // if append not success, free data return NULL
225         if (ret <= 0) {
226                 free(cs);
227                 cs = NULL;
228                 return;
229         }
230         // write this record to database
231         worldclock_data_update_db_record(cs);
232         // append such record to genlist
233         elm_genlist_item_prepend(view->eo_genlist, &g_ts, cs,
234                                  NULL, ELM_GENLIST_ITEM_NONE,
235                                  __mainview_genlist_item_sel, cs);
236
237         // do others which need to process when exit from next view
238         __mainview_return_cb(isReload);
239 }
240
241 /**
242  * Show add view in main view
243  *
244  * @param[in]  data        Data used in this function
245  * @param[in]  obj         Evas_Object which emit this event
246  * @param[in]  event_info  Information about this event
247  *
248  * @return
249  */
250 static void __mainview_show_addview_cb(void *data, Evas_Object * obj,
251                                        void *event_info)
252 {
253         ret_if(!data);
254         view_data *view = (view_data *) data;
255         struct appdata *ad = view->ad;
256         int ret = 0;
257
258         // show add view
259 #if USING_WORLDCLOCK_UG
260         ret = worldclock_ug_addview_add(view->eo_parent, ad, __mainview_addview_return_cb);
261 #else
262         ret = worldclock_addview_add(view->eo_parent, ad, __mainview_addview_return_cb);
263
264         if (ad->controlbar_cb) {
265                 // hide controlbar
266                 ad->controlbar_cb(EINA_FALSE);
267         }
268 #endif
269
270         ret_if(SUCCESS != ret);
271         // reset timer
272         ECORE_TIMER_DELIF(view->timer);
273 }
274
275
276 //
277 static void __mainview_back_button_cb(void *data, Evas_Object * obj,
278                                       void *event_info)
279 {
280         ret_if(!data);
281         Evas_Object *win = (Evas_Object *) data;
282         elm_win_lower(win);
283 }
284
285 /**
286  * Callback function used for append new city from add view
287  *
288  * @param[in]  data      Data used in this function
289  *
290  * @return
291  */
292 static void __mainview_editview_return_cb(void *data, Eina_Bool isReload)
293 {
294         ret_if(!g_view_data);
295         view_data *view = g_view_data;
296         struct appdata *ad = view->ad;
297         ret_if(!ad);
298
299         Elm_Object_Item *gli = NULL;
300         Elm_Object_Item *gli_next = NULL;
301         Wcl_CitySet *cs = NULL;
302         // get the first item in genlist
303         gli = elm_genlist_first_item_get(g_view_data->eo_genlist);
304         while (gli) {
305                 // reset now_time
306                 cs = (Wcl_CitySet *) elm_object_item_data_get(gli);
307                 ret_if(!cs);
308                 worldclock_data_get_city_status_from_db(cs);
309
310                 // get the next genlist item
311                 gli_next = elm_genlist_item_next_get(gli);
312
313                 // if item unselected, remove it
314                 if (!cs->selected) {
315                         // remove genlist item
316                         elm_object_item_del(gli);
317                         // remove eina list item
318                         ad->eina_list = eina_list_remove(ad->eina_list, cs);
319                 }
320                 // go to next genlist item
321                 gli = gli_next;
322         }
323         worldclock_data_update_db_record(cs);
324         // do others which need to process when exit from next view
325         ad->page = WCL_PAGE_MAIN;
326         __mainview_return_cb(isReload);
327 }
328
329 static void __mainview_show_delview_cb(void *data, Evas_Object * obj,
330                                        void *event_info)
331 {
332         ret_if(!data);
333         view_data *view = (view_data *) data;
334         struct appdata *ad = view->ad;
335         if (view->ctxpopup != NULL) {
336                 evas_object_del(view->ctxpopup);
337                 view->ctxpopup = NULL;
338         }
339         int ret = 0;
340         if (ad->controlbar_cb) {
341                 // hide controlbar
342                 ad->controlbar_cb(EINA_FALSE);
343         }
344         // show delete view
345         ret =
346             worldclock_editview_add(view->eo_parent, ad,
347                                     __mainview_editview_return_cb);
348         ret_if(SUCCESS != ret);
349         // reset timer
350         ECORE_TIMER_DELIF(view->timer);
351 }
352
353 static void __mainview_dismiss_cb(void *data, Evas_Object * obj,
354                                   void *event_info)
355 {
356         if (obj != NULL) {
357                 evas_object_del(obj);
358                 obj = NULL;
359         }
360 }
361
362 /**
363  * Callback function used for append new city from add view
364  *
365  * @param[in]  data      Data used in this function
366  *
367  * @return
368  */
369 static void __mainview_detailview_return_cb(void *data, Eina_Bool isReload)
370 {
371         ret_if(!g_view_data);
372         view_data *view = g_view_data;
373         if (data && view->egi_edit) {
374                 // Get data of new city
375                 Wcl_CitySet *cs = (Wcl_CitySet *) data;
376
377                 // replace
378                 Wcl_CitySet *cs2update =
379                     (Wcl_CitySet *) elm_object_item_data_get(view->egi_edit);
380                 // copy new data to it
381                 worldclock_data_cityset_copy(cs2update, cs);
382                 free(cs);
383
384                 elm_genlist_item_update(view->egi_edit);
385                 worldclock_data_update_db_record(cs2update);
386                 view->egi_edit = NULL;
387         }
388         __mainview_return_cb(isReload);
389 }
390
391 /**
392  * Show edit view in main view
393  *
394  * @param[in]  data         Data used in this function
395  * @param[in]  obj          Evas_Object who emit this event
396  * @param[in]  event_info   Information about such event
397  *
398  * @return
399  */
400 static void __mainview_show_detailview_cb(void *data, Evas_Object * obj,
401                                           void *event_info)
402 {
403         ret_if(!data || !g_view_data);
404         view_data *view = g_view_data;
405         struct appdata *ad = view->ad;
406         ret_if(!ad);
407
408         int ret = 0;
409         if (ad->controlbar_cb) {
410                 // hide controlbar
411                 ad->controlbar_cb(EINA_FALSE);
412         }
413         // show edit view
414         Wcl_CitySet *cs = (Wcl_CitySet *) calloc(1, sizeof(Wcl_CitySet));
415         worldclock_data_cityset_copy(cs, (Wcl_CitySet *) data);
416         ret =
417             worldclock_detailview_add(view->eo_parent, ad,
418                                       __mainview_detailview_return_cb, cs);
419         ret_if(SUCCESS != ret);
420         // reset timer
421         ECORE_TIMER_DELIF(view->timer);
422 }
423
424 /*
425 *  update clock timer callback
426 */
427 /**
428  * Update status of clock
429  *
430  * @param[in]  data
431  * @return
432  */
433 static int __mainview_genlist_update_clock(void *data)
434 {
435         retv_if(!g_view_data, FAILED);
436         view_data *view = g_view_data;
437         struct appdata *ad = view->ad;
438         retv_if(!ad, FAILED);
439         Elm_Object_Item *gli = NULL;
440         Wcl_CitySet *cs = NULL;
441
442         retv_if(!view->eo_genlist, FAILED);
443         // get the first item in genlist
444         gli = elm_genlist_first_item_get(view->eo_genlist);
445         // get homezone
446         FREEIF(ad->home_cs);
447         ad->home_cs = worldclock_data_get_local_city();
448         while (gli) {
449                 // reset now_time
450                 cs = (Wcl_CitySet *) elm_object_item_data_get(gli);
451                 retv_if(!cs, FAILED);
452                 cs->now_time = 0;
453                 cs->dst = -1;
454                 elm_genlist_item_update(gli);
455                 gli = elm_genlist_item_next_get(gli);
456         }
457         return SUCCESS;
458 }
459
460 //////////////////////////// genlist related /////////////////////////////////////////////////
461 /**
462  * Get label of given part in genlist item
463  *
464  * @param[in]  data   Data used in this function
465  * @param[in]  obj    Evas_Object who emit this event
466  * @param[in]  part   Name of the part in genlist item
467  *
468  * @return     NULL if meet error
469  *             Pointer to string in label
470  */
471 static char *__mainview_genlist_label_get(void *data, Evas_Object * obj,
472                                           const char *part)
473 {
474         retv_if(!data || !part || !g_view_data, NULL);
475         Wcl_CitySet *cs = (Wcl_CitySet *) data;
476         CLK_FUN_DEBUG_BEG();
477         char buf[BUF_SIZE] = { 0 };
478         time_t ntime = worldclock_genlist_time_get(cs, g_view_data->ad);
479         if (IS_STR_EQUAL(part, "elm.text.time")) {
480                 snprintf(buf, BUF_SIZE,
481                          worldclock_icu_get_time_str(ntime, cs->tz_path));
482         } else if (IS_STR_EQUAL(part, "elm.text.date")) {
483                 snprintf(buf, BUF_SIZE,
484                          worldclock_icu_get_date_str(ntime, cs->tz_path));
485         } else if (IS_STR_EQUAL(part, "elm.text.city")) {
486                 snprintf(buf, BUF_SIZE, _(cs->city));
487                 /*BEG: limit max length 12 added by x536.gong in Wed Nov 30 23:09:34 PST 2011 */
488                 int length = strlen(_(cs->city));
489                 if (length > 12) {
490                         snprintf(&buf[12], BUF_SIZE - 12, "...");
491                 }
492                 /*END: limit max length 12 added by x536.gong in Wed Nov 30 23:09:34 PST 2011 */
493         }
494         CLK_FUN_DEBUG_END();
495         return strdup(buf);
496 }
497
498 /**
499  * Get icon of given part in genlist item
500  *
501  * @param[in]  data   Data used in this function
502  * @param[in]  obj    Evas_Object who emit this event
503  * @param[in]  part   Name of the part in genlist item
504  *
505  * @return     NULL if meet error
506  *             Pointer to new icon
507  */
508 static Evas_Object *__mainview_genlist_icon_get(void *data, Evas_Object * obj,
509                                                 const char *part)
510 {
511         retv_if(!data || !part || !g_view_data, NULL);
512         Wcl_CitySet *cs = (Wcl_CitySet *) data;
513         CLK_FUN_DEBUG_BEG();
514         char buf[BUF_SIZE] = { 0, };
515         time_t ntime = worldclock_genlist_time_get(cs, g_view_data->ad);
516         //get current time
517         worldclock_time_s *pWorldclock_time;
518         pWorldclock_time = worldclock_icu_get_time_int(ntime, cs->tz_path);
519         retv_if(!pWorldclock_time, NULL);
520
521         Eina_Bool bDay = EINA_FALSE;
522         if ((pWorldclock_time->hour < WCL_NIGHT_TIME)
523             && (pWorldclock_time->hour >= WCL_DAY_TIME)) {
524                 bDay = EINA_TRUE;
525         }
526         Evas_Object *ly = NULL;
527         if (IS_STR_EQUAL(part, "elm.swallow.dst")) {
528                 Evas_Object *ly = NULL;
529                 if (cs->dst > 0) {
530                         // add layout for dst
531                         ly = elm_layout_add(obj);
532                         retv_if(ly == NULL, NULL);
533                         // set edje
534                         elm_layout_file_set(ly, WCL_EDJ_NAME, "icon/dst");
535                         evas_object_size_hint_weight_set(ly, 1.0, 0.0);
536                         evas_object_size_hint_align_set(ly, 0.0, 0.5);
537                         // show layout
538                         evas_object_show(ly);
539                 }
540                 CLK_FUN_DEBUG_END();
541                 return ly;
542         } else if (IS_STR_EQUAL(part, "elm.swallow.bg")) {
543                 char *image_path =
544                     (EINA_TRUE ==
545                      bDay) ? WCL_IMAGE_PATH "/P03_worldclock_bg_day.png" :
546                     WCL_IMAGE_PATH "/P03_worldclock_bg_night.png";
547                 snprintf(buf, BUF_SIZE, image_path);
548                 ly = elm_icon_add(obj);
549                 Eina_Bool icon_set_ret_value = 0;
550                 icon_set_ret_value = elm_icon_file_set(ly, buf, NULL);
551                 evas_object_show(ly);
552                 CLK_FUN_DEBUG_END();
553                 return ly;
554         } else if (IS_STR_EQUAL(part, "elm.swallow.hour")) {
555                 char *image_path_pre =
556                     (EINA_TRUE ==
557                      bDay) ? WCL_IMAGE_PATH
558                     "/Day/hour/P03_worldclock_bg_day_hour_" : WCL_IMAGE_PATH
559                     "/Night/hour/P03_worldclock_bg_night_hour_";
560                 int hour = pWorldclock_time->hour % 12;
561                 int min = pWorldclock_time->min;
562                 snprintf(buf, BUF_SIZE, "%s%02d.png", image_path_pre,
563                          hour * 5 + min / 12);
564                 ly = elm_icon_add(obj);
565                 elm_icon_file_set(ly, buf, NULL);
566                 evas_object_show(ly);
567                 CLK_FUN_DEBUG_END();
568                 return ly;
569         } else if (IS_STR_EQUAL(part, "elm.swallow.min")) {
570                 char *image_path_pre =
571                     (EINA_TRUE ==
572                      bDay) ? WCL_IMAGE_PATH
573                     "/Day/minute/P03_worldclock_bg_day_minute_" : WCL_IMAGE_PATH
574                     "/Night/minute/P03_worldclock_bg_night_minute_";
575                 int min = pWorldclock_time->min;
576                 snprintf(buf, BUF_SIZE, "%s%02d.png", image_path_pre, min);
577                 ly = elm_icon_add(obj);
578                 elm_icon_file_set(ly, buf, NULL);
579                 evas_object_show(ly);
580                 CLK_FUN_DEBUG_END();
581                 return ly;
582         } else if (IS_STR_EQUAL(part, "elm.swallow.sec")) {
583 #ifndef SECOND_UPDATE
584                 return NULL;
585 #else
586                 char *image_path_pre =
587                     (EINA_TRUE ==
588                      bDay) ? WCL_IMAGE_PATH
589                     "/Day/second/P03_worldclock_bg_day_second_" : WCL_IMAGE_PATH
590                     "/Night/second/P03_worldclock_bg_night_second_";
591                 int sec = pWorldclock_time->sec;
592                 snprintf(buf, BUF_SIZE, "%s%02d.png", image_path_pre, sec);
593                 ly = elm_icon_add(obj);
594                 elm_icon_file_set(ly, buf, NULL);
595                 evas_object_show(ly);
596                 CLK_FUN_DEBUG_END();
597                 return ly;
598 #endif
599         } else if (IS_STR_EQUAL(part, "elm.swallow.center")) {
600                 char *image_path =
601                     WCL_IMAGE_PATH "/P03_worldclock_bg_center_point.png";
602                 snprintf(buf, BUF_SIZE, image_path);
603                 ly = elm_icon_add(obj);
604                 elm_icon_file_set(ly, buf, NULL);
605                 evas_object_show(ly);
606                 CLK_FUN_DEBUG_END();
607                 return ly;
608         }
609         CLK_FUN_DEBUG_END();
610         return NULL;
611 }
612
613 /**
614  * Callback function which used for genlist selection event processing
615  *
616  * @param[in]  data         data which used in this function
617  * @param[in]  obj          the object which emit the selection event
618  * @param[in]  event_info   information about selection event
619  *
620  * @return
621  */
622 static void __mainview_genlist_item_sel(void *data, Evas_Object * obj,
623                                         void *event_info)
624 {
625         retm_if(NULL_CHECK(data), "data error");
626         Elm_Object_Item *gli = (Elm_Object_Item *) (event_info);
627         elm_genlist_item_selected_set(gli, 0);
628         Evas_Event_Mouse_Up *ev = (Evas_Event_Mouse_Up *) event_info;
629         ret_if(ev && (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD));
630
631         g_view_data->egi_edit = gli;
632         __mainview_show_detailview_cb(data, NULL, NULL);
633 }
634
635 /**
636  * Create genlist to display all selected cities
637  *
638  * @param[in]  parent   Parent of new genlist
639  * @param[in]  data     Data used in this function
640  *
641  * @return     NULL if meet error
642  *             Pointer to new genlist
643  */
644 static Evas_Object *__mainview_genlist_add(Evas_Object * parent, void *data)
645 {
646         retv_if(!data, NULL);
647         struct appdata *ad = (struct appdata *)data;
648         Evas_Object *genlist = NULL;
649         // add genlist
650         CLK_FUN_BEG();
651         genlist = elm_genlist_add(parent);
652         evas_object_resize(genlist, ad->root_w,
653                            ad->root_h - INDICATOR_H * ad->root_h / YRES);
654         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND,
655                                          EVAS_HINT_EXPAND);
656         evas_object_size_hint_align_set(genlist, -1.0, -1.0);
657
658         // swallow
659         edje_object_part_swallow(_EDJ(parent), "main/genlist", genlist);
660         // show genlist
661         evas_object_show(genlist);
662         CLK_FUN_END();
663         return genlist;
664 }
665
666 /////////////////////////////////// genlist related finish /////////////////////////////////////////
667
668 /**
669  * Callback function used for update clock status
670  *
671  * @param[in]  data      Data used in this function
672  *
673  * @return     EINA_FALSE if invoke failed
674  *             EINA_TRUE if successfully
675  */
676 static Eina_Bool __mainview_timer_cb(void *data)
677 {
678         CLK_FUN_BEG();
679         retv_if(!data, EINA_FALSE);
680         struct appdata *ad = (struct appdata *)data;
681         // every minute, set a new timer
682         ECORE_TIMER_DELIF(g_view_data->timer);
683         // update content of clock in genlist item
684         __mainview_genlist_update_clock(ad);
685 #ifndef SECOND_UPDATE
686         struct tm local_time;
687         time_t ctime = 0;
688         // get current time
689         ctime = time(NULL);
690         // get local time
691         localtime_r(&ctime, &local_time);
692         // add new timer
693         g_view_data->timer =
694             ecore_timer_add(60 - local_time.tm_sec, __mainview_timer_cb, ad);
695 #else
696         g_view_data->timer = ecore_timer_add(1, __mainview_timer_cb, ad);
697 #endif
698         CLK_FUN_END();
699         return EINA_TRUE;
700 }
701
702 /**
703  * Update main view
704  *
705  * @param[in]  data      data used in this function
706  * @param[in]  isReload  Flag to define whether reload genlist data
707  *
708  * @return
709  */
710 static int __mainview_genlist_create(view_data * view)
711 {
712         retv_if(!view || !view->eo_genlist || !view->ad, FAILED);
713         struct appdata *ad = view->ad;
714         Eina_List *el = NULL;
715         Wcl_CitySet *cs = NULL;
716         //clear and get
717         CLK_FUN_BEG();
718
719         worldclock_util_glist_remove_all(ad->eina_list, EINA_TRUE);
720         ad->eina_list = worldclock_data_get_all_added_city();
721         if(!ad->eina_list) {
722             // clear genlsit
723             elm_genlist_clear(view->eo_genlist);
724         return FAILED;
725     }
726
727         // clear genlsit
728         elm_genlist_clear(view->eo_genlist);
729
730         // append genlsit
731         for (el = ad->eina_list; el; el = el->next) {
732                 cs = el->data;
733                 if (!cs) {
734                         continue;
735                 }
736                 elm_genlist_item_append(view->eo_genlist, &g_ts, cs,
737                                         NULL, ELM_GENLIST_ITEM_NONE,
738                                         __mainview_genlist_item_sel, cs);
739         }
740         //reset timer
741         ECORE_TIMER_DELIF(view->timer);
742         // update time status
743 #ifndef SECOND_UPDATE
744         struct tm local_time;
745         time_t ctime = 0;
746         double interval_time = 0.0;
747         // get current time
748         ctime = time(NULL);
749         // get local time
750         localtime_r(&ctime, &local_time);
751         // update on the next miniute
752         interval_time = 60 - local_time.tm_sec;
753         // add timer to plan update
754         view->timer = ecore_timer_add(interval_time, __mainview_timer_cb, ad);
755 #else
756         view->timer = ecore_timer_add(1, __mainview_timer_cb, ad);
757 #endif
758         CLK_FUN_END();
759
760         return SUCCESS;
761 }
762
763 /**
764  * Update main view
765  *
766  * @param[in]  data      data used in this function
767  * @param[in]  isReload  Flag to define whether reload genlist data
768  *
769  * @return
770  */
771 static int __mainview_genlist_update(view_data * view)
772 {
773         retv_if(!view || !view->eo_genlist || !view->ad, FAILED);
774         struct appdata *ad = view->ad;
775         Eina_List *el = NULL;
776         Wcl_CitySet *cs = NULL;
777         if (NULL == ad->eina_list) {
778                 // get all selected cities
779                 ad->eina_list = worldclock_data_get_all_added_city();
780         } else {
781                 // if eina_list exist, resort it
782                 ad->eina_list =
783                     eina_list_sort(ad->eina_list, eina_list_count(ad->eina_list)
784                                    , worldclock_sequence_compare_cb);
785         }
786         retv_if(!ad->eina_list, FAILED);
787         for (el = ad->eina_list; el; el = el->next) {
788                 cs = el->data;
789                 cs->dst = -1;
790         }
791         //reset timer
792         ECORE_TIMER_DELIF(view->timer);
793         // update time status
794 #ifndef SECOND_UPDATE
795         struct tm local_time;
796         time_t ctime = 0;
797         double interval_time = 0.0;
798         // get current time
799         ctime = time(NULL);
800         // get local time
801         localtime_r(&ctime, &local_time);
802         // if not reload genlist, update immediately
803         interval_time = 0.5;
804         // add timer to plan update
805         view->timer = ecore_timer_add(interval_time, __mainview_timer_cb, ad);
806 #else
807         view->timer = ecore_timer_add(1, __mainview_timer_cb, ad);
808 #endif
809         return SUCCESS;
810 }
811
812 /**********************************************************************
813 ******************Global function  ref*************************************
814 ***********************************************************************/
815 /**
816  * Update main view
817  *
818  * @param[in]  data      data used in this function
819  *
820  * @return
821  */
822 void worldclock_mainview_update(void *data)
823 {
824         // update content of clock in genlist item
825         __mainview_genlist_update_clock(data);
826 }
827
828 /**
829  * Create main view
830  *
831  * @param[in]  parent   Parent view to main view
832  * @param[in]  data     Data used in this function
833  * @param[in]  func     Callback function register by previous view, which will be invoked when exit main view
834  *
835  * @return     FAILED if meet error
836  *             SUCCESS if create successfully
837  */
838 int worldclock_mainview_add(Evas_Object * parent, void *data,
839                             Wcl_Return_Cb func)
840 {
841         retv_if(!data, FAILED);
842         struct appdata *ad = (struct appdata *)data;
843         if (!g_view_data)
844     {
845             view_data *view = CALLOC(1, view_data);
846             retv_if(!view, FAILED);
847             CLK_FUN_BEG();
848             memset(view, 0, sizeof(view_data));
849             g_view_data = view;
850
851             view->eo_parent = parent;
852             view->eo_navibar = ad->navi_bar;
853             view->ad = ad;
854             view->return_cb = func;
855             view->bIsReorderView = EINA_FALSE;
856             ad->page = WCL_PAGE_MAIN;
857
858             const char *group_name = NULL;
859             // select call_flag & group name by caller
860             switch (ad->caller) {
861             case WCL_CALLER_IS_APP_IT_SELF:
862                 group_name = GRP_MAIN;
863                 break;
864             case WCL_CALLER_IS_SHARED_LIBRARY:
865                 group_name = GRP_MAIN2;
866                 break;
867             default:
868                 return FAILED;
869             }
870             // add samsung window layout
871             view->eo_layout = widget_create_layout(view->eo_parent);
872             /* load edje */
873             view->eo_content = load_edj(view->eo_layout, WCL_EDJ_NAME, group_name);
874             elm_object_part_content_set(view->eo_layout, "elm.swallow.content",
875                                     view->eo_content);
876             /* add genlist */
877             view->eo_genlist = __mainview_genlist_add(view->eo_content, ad);
878             // init data list
879             __mainview_genlist_create(view);
880     }
881
882         /* create controlbar */
883         g_view_data->eo_controlbar = widget_create_controlbar(g_view_data->eo_navibar, NULL);
884
885         Elm_Object_Item *eci;
886
887         g_view_data->eci_btn_lft =
888             elm_toolbar_item_append( g_view_data->eo_controlbar, WCL_IMAGE_ADD, NULL,
889                                                     __mainview_show_addview_cb,
890                                                     g_view_data);
891
892         eci = elm_toolbar_item_append(g_view_data->eo_controlbar, NULL,
893                                       NULL, NULL, NULL);
894         elm_object_item_disabled_set(eci, EINA_TRUE);
895
896         g_view_data->eci_btn_mid =
897             elm_toolbar_item_append(g_view_data->eo_controlbar, NULL,
898                                     S_("IDS_COM_SK_EDIT"),
899                                     __mainview_show_delview_cb, g_view_data );
900
901         eci = elm_toolbar_item_append(g_view_data->eo_controlbar, NULL,
902                                       NULL, NULL, NULL);
903         elm_object_item_disabled_set(eci, EINA_TRUE);
904
905         g_view_data->eo_btn_back =
906             widget_create_button(g_view_data->eo_controlbar,
907                                  "naviframe/end_btn/default", NULL, NULL,
908                                  __mainview_back_button_cb, ad->win);
909
910         // push mainview to navigationbar
911         g_view_data->ei_naviframe =
912             widget_naviframe_push(g_view_data->eo_navibar,
913                                   S_("IDS_COM_BODY_WORLD_CLOCK"),
914                                   g_view_data->eo_btn_back, NULL, g_view_data->eo_layout,
915                                   "tabbar", g_view_data->eo_controlbar);
916         // update state on main view
917         __mainview_buttons_state_update(g_view_data);
918         ad->controlbar_cb(EINA_TRUE);
919         CLK_FUN_END();
920         return SUCCESS;
921 }
922
923 /**
924  * Free data used in main view
925  *
926  * @param[in]  data      Data used in this function
927  *
928  * @return
929  */
930 void worldclock_mainview_free(void *data)
931 {
932         ret_if(!data);
933         // free add view
934 #if USING_WORLDCLOCK_UG
935         worldclock_ug_addview_free(data);
936 #else
937         worldclock_addview_free(data);
938 #endif
939         // free edit view
940         worldclock_editview_free(data);
941         __mainview_quit_cb(data, NULL, NULL);
942 }