merge with master
[apps/core/preloaded/calendar.git] / src / main.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://floralicense.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 <utilX.h>
20 #include <Ecore_X.h>
21 #include <ui-gadget.h>
22 #include <vconf.h>
23 #include <aul.h>
24 #include <app.h>
25
26 #include "view.h"
27 #include "edit-view.h"
28 #include "detail-view.h"
29 #include "noti-handler.h"
30 #include "lang-manager.h"
31
32 static void __cal_main_setup_data(struct appdata *ad);
33 static Eina_Bool __cal_main_update_today(void *data);
34
35 static void __cal_main_changed_callback(void *data)
36 {
37         CAL_FN_START;
38
39         c_ret_if(!data);
40
41         struct appdata *ad = data;
42
43         __cal_main_setup_data(ad);
44
45         if (ad->is_pause) {
46                 ad->is_update_view = EINA_TRUE;
47                 return;
48         }
49
50         cal_main_update_view(ad);
51
52         return;
53 }
54
55 static void __cal_main_set_today_tm(struct appdata *ad)
56 {
57         struct tm tm;
58
59         time_t now = time(NULL);
60         localtime_r(&now, &tm);
61
62         tm.tm_min = 0; // min, sec reset for mktime()
63         tm.tm_sec = 0;
64
65         ad->today_tm = tm;
66
67         tm.tm_hour = 0;
68         time_t t = mktime(&tm);
69
70         if (t == (time_t)-1) {
71                 ERR("mktime() is failed");
72                 ad->today_tmr = NULL;
73                 return;
74         }
75
76         t = t + (24 * 60 * 60) - time(NULL);
77         ad->today_tmr = ecore_timer_add((double)t, __cal_main_update_today, ad);
78 }
79
80 static Eina_Bool __cal_main_update_today(void *data)
81 {
82         c_retv_if(!data, ECORE_CALLBACK_CANCEL);
83
84         struct appdata *ad = data;
85
86         __cal_main_set_today_tm(ad);
87
88         if (ad->today_edj)
89                 edje_object_signal_emit(ad->today_edj, "update_today", "prog");
90
91         return ECORE_CALLBACK_CANCEL;
92 }
93
94 static void __cal_main_setup_data(struct appdata *ad)
95 {
96         CAL_FN_START;
97
98         __cal_main_set_today_tm(ad);
99
100         if (ad->base_tm.tm_year == 0)
101                 ad->base_tm = ad->today_tm;
102
103         int val;
104
105         int r = vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, &val);
106         if (r < 0 || val < 0)
107                 ad->wday_start = cal_util_get_default_first_day_of_week();
108         else
109                 ad->wday_start = val;
110
111         r = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &val);
112         if (r < 0)
113                 is_hour24 = 0;
114         else if( val == 1 || val == 2)
115                 is_hour24 = val -1;
116         else
117                 ERR("vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &val); error");
118
119         vconf_get_int(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, (int *)&ad->is_display_complete_todo);
120
121         ad->is_update_view = EINA_TRUE;
122
123         ad->acct_id = _CALENDAR_ALL_ACCOUNT_ID;
124
125         CAL_FN_END;
126 }
127
128 static void __cal_main_timezone_vconf_changed_callback(keynode_t *node, void *user_data)
129 {
130         CAL_FN_START;
131
132         c_ret_if(!node);
133         c_ret_if(!user_data);
134
135         Eina_Bool is_lock_timezone = vconf_keynode_get_int(node);
136
137         char *text = NULL;
138
139         if (is_lock_timezone)
140                 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
141         else
142                 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
143
144         if (CAL_STRLEN(text)) {
145
146                 cal_util_set_timezone(text);
147
148                 free(text);
149
150                 cal_main_update_view(user_data);
151
152         } else
153                 ERR("vconf_get_str is failed.");
154
155         CAL_FN_END;
156 }
157
158 static void __cal_main_week_of_day_format_vconf_changed_callback(keynode_t *node, void *user_data)
159 {
160         CAL_FN_START;
161
162         c_retm_if(!node, "node is null");
163         c_retm_if(!user_data, "user_data is null");
164
165         struct appdata* ad = user_data;
166         int r = vconf_keynode_get_int(node);
167         if (r < 0)
168                 ad->wday_start = cal_util_get_default_first_day_of_week();
169         else
170                 ad->wday_start = r;
171
172         cal_main_update_view(ad);
173
174         CAL_FN_END;
175 }
176
177 static void __cal_main_view_of_month_format_vconf_changed_callback(keynode_t *node, void *user_data)
178 {
179         CAL_FN_START;
180
181         c_retm_if(!node, "node is null");
182         c_retm_if(!user_data, "user_data is null");
183
184         struct appdata* ad = user_data;
185         int r = vconf_keynode_get_int(node);
186         c_retm_if(r < 0, "vconf_keynode_get_int is failed");
187
188         ad->daily_events_mode = r;
189
190         cal_main_update_view(ad);
191
192         CAL_FN_END;
193 }
194
195 static void __cal_main_view_of_display_compelete_todo_vconf_changed_callback(keynode_t *node, void *user_data)
196 {
197         CAL_FN_START;
198
199         c_retm_if(!node, "node is null");
200         c_retm_if(!user_data, "user_data is null");
201
202         struct appdata* ad = user_data;
203
204         if (CV_YEAR == ad->current_view)
205                 return;
206
207         int r = vconf_keynode_get_int(node);
208         c_retm_if(r < 0, "vconf_keynode_get_int is failed");
209
210         ad->is_display_complete_todo = r;
211
212         cal_main_update_view(ad);
213
214         CAL_FN_END;
215 }
216
217 static void __cal_main_view_of_display_week_number_vconf_changed_callback(keynode_t *node, void *user_data)
218 {
219         CAL_FN_START;
220
221         c_retm_if(!node, "node is null");
222         c_retm_if(!user_data, "user_data is null");
223
224         struct appdata* ad = user_data;
225
226         if (CV_MONTH != ad->current_view && !ad->daily_events_mode)
227                 return;
228
229         int r = vconf_keynode_get_int(node);
230         c_retm_if(r < 0, "vconf_keynode_get_int is failed");
231
232         ad->is_display_weeknumber = r;
233
234         cal_main_update_view(ad);
235
236         CAL_FN_END;
237 }
238
239 static void __cal_main_time_format_vconf_changed_callback(keynode_t *node, void *user_data)
240 {
241         CAL_FN_START;
242
243         c_retm_if(!node, "node is null");
244         c_retm_if(!user_data, "user_data is null");
245
246         struct appdata* ad = user_data;
247
248         int r = vconf_keynode_get_int(node);
249         c_retm_if(r < 0, "vconf_keynode_get_int is failed");
250
251         is_hour24 = r -1;
252
253         cal_main_update_view(ad);
254
255         CAL_FN_END;
256 }
257
258 static void __cal_main_time_changed_callback(keynode_t *node, void *user_data)
259 {
260         CAL_FN_START;
261
262         c_ret_if(!user_data);
263
264         struct appdata* ad = user_data;
265
266         __cal_main_set_today_tm(ad);
267
268         cal_main_update_view(ad);
269 }
270
271 static int __cal_main_initialize(struct appdata *ad)
272 {
273         CAL_FN_START;
274
275         c_retvm_if(!ad, -1, "ad is null");
276
277         __cal_main_setup_data(ad);
278
279         calendar_error_e err = calendar_connect();
280         c_warn_if(err != CALENDAR_ERROR_NONE, "calendar_connect is failed(%d)", err);
281
282         _calendar_init_hash();
283
284         cal_util_connect_pattern_generator();
285
286         cal_util_initialize_timezone();
287
288         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme.edj");
289         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme2.edj");
290         elm_theme_extension_add(NULL, "/usr/apps/"CALENDAR_PACKAGE"/res/edje/theme3.edj");
291
292         int r = vconf_notify_key_changed(CAL_VCONFKEY_FOCUS_VIEW,__cal_main_view_of_month_format_vconf_changed_callback, ad);
293         c_warn_if (r, "vconf_notify_key_changed failed");
294
295         r = vconf_notify_key_changed(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, __cal_main_view_of_display_week_number_vconf_changed_callback, ad);
296         c_warn_if (r, "vconf_notify_key_changed failed");
297
298         r = vconf_notify_key_changed(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, __cal_main_view_of_display_compelete_todo_vconf_changed_callback, ad);
299         c_warn_if (r, "vconf_notify_key_changed failed");
300
301         r = vconf_notify_key_changed(CAL_VCONFKEY_FIRST_DAY_OF_WEEK , __cal_main_week_of_day_format_vconf_changed_callback, ad);
302         c_warn_if (r, "vconf_notify_key_changed failed");
303
304         r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224 , __cal_main_time_format_vconf_changed_callback, ad);
305         c_warn_if (r, "vconf_notify_key_changed failed");
306
307         r = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT , __cal_main_timezone_vconf_changed_callback, ad);
308         c_warn_if (r, "vconf_notify_key_changed failed");
309
310         r = vconf_notify_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, __cal_main_timezone_vconf_changed_callback, ad);
311         c_warn_if (r, "vconf_notify_key_changed failed");
312
313         r = vconf_notify_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_PATH, __cal_main_timezone_vconf_changed_callback, ad);
314         c_warn_if (r, "vconf_notify_key_changed failed");
315
316         r = vconf_notify_key_changed(VCONFKEY_SYSTEM_TIMECHANGE, __cal_main_time_changed_callback, ad);
317         c_warn_if (r, "vconf_notify_key_changed failed");
318
319         r = contacts_connect2();
320         c_warn_if(r != CONTACTS_ERROR_NONE, "contacts_connect2() is failed(%x)", r);
321
322         UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_ENABLE);
323
324         CAL_FN_END;
325
326         return 0;
327 }
328
329 static void __cal_main_finish(struct appdata *ad)
330 {
331
332         cal_lang_manager_remove_all_callback();
333
334         ug_destroy_all();
335
336         cal_util_disconnect_pattern_generator();
337
338         int r = contacts_disconnect2();
339         c_warn_if(r!=CONTACTS_ERROR_NONE, "contacts_disconnect2() is failed(%x)", r);
340
341         r = vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_PATH, __cal_main_timezone_vconf_changed_callback);
342         c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_PATH) failed");
343
344         r = vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, __cal_main_timezone_vconf_changed_callback);
345         c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF) failed");
346
347         r = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT, __cal_main_timezone_vconf_changed_callback);
348         c_warn_if(r, "vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT) failed");
349
350         r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __cal_main_time_format_vconf_changed_callback);
351         c_warn_if(r, "vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224) failed");
352
353         r = vconf_ignore_key_changed(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, __cal_main_week_of_day_format_vconf_changed_callback);
354         c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_FIRST_DAY_OF_WEEK) failed");
355
356         r = vconf_ignore_key_changed(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, __cal_main_view_of_display_compelete_todo_vconf_changed_callback);
357         c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF) failed");
358
359         r = vconf_ignore_key_changed(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, __cal_main_view_of_display_week_number_vconf_changed_callback);
360         c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF) failed");
361
362         r = vconf_ignore_key_changed(CAL_VCONFKEY_FOCUS_VIEW, __cal_main_view_of_month_format_vconf_changed_callback);
363         c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_FOCUS_VIEW) failed");
364
365         calendar_error_e err = calendar_disconnect();
366         c_warn_if(err != CALENDAR_ERROR_NONE, "calendar_disconnect() is failed(%d)", err);
367 }
368
369 static int __cal_main_add_naviframe(struct appdata *ad)
370 {
371         c_retvm_if(!ad, -1, "ad is null");
372
373         CAL_FN_START;
374
375         ad->naviframe = elm_naviframe_add(ad->base);
376         c_retvm_if(!ad->naviframe, -1, "elm_naviframe_add returned null");
377
378         elm_object_part_content_set(ad->base, "elm.swallow.content", ad->naviframe);
379
380         CAL_FN_END;
381
382         return 0;
383 }
384
385 static void __cal_main_win_profile_changed_callback(void *data, Evas_Object *obj, void *event)
386 {
387         c_ret_if(!obj);
388         c_ret_if(!data);
389
390         struct appdata *ad = data;
391         c_ret_if(!ad->base);
392
393         Evas_Object *base = ad->base;
394
395         const char *profile = elm_config_profile_get();
396         c_ret_if(!profile);
397
398         if (!CAL_STRCMP(profile, "desktop"))
399                 elm_layout_theme_set(base, "layout", "application", "noindicator");
400         else
401                 elm_layout_theme_set(base, "layout", "application", "default");
402
403         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
404
405         elm_win_resize_object_add(obj, base);
406         elm_object_content_set(ad->conform, base);
407
408         evas_object_show(base);
409 }
410
411 static Evas_Object * __cal_main_add_base_layout(Evas_Object *conformant)
412 {
413         CAL_FN_START;
414
415         c_retv_if(!conformant, NULL);
416
417         Evas_Object *layout = elm_layout_add(conformant);
418         c_retv_if(!layout, false);
419
420         Eina_Bool r = elm_layout_theme_set(layout, "layout", "application", "default");
421         c_warn_if(!r, "elm_layout_theme_set is failed");
422
423         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
424
425         elm_object_content_set(conformant, layout);
426
427         CAL_FN_END;
428
429         return layout;
430 }
431
432 static bool cal_create(void *data)
433 {
434         CAL_FN_START;
435
436         c_retvm_if(!data, false, "data is null");
437
438         struct appdata *ad = data;
439
440         Evas_Object *window = cal_util_add_window(CALENDAR, &ad->win_w, &ad->win_h);
441         c_retv_if(!window, false);
442
443         evas_object_smart_callback_add(window, "profile,changed", __cal_main_win_profile_changed_callback, ad);
444
445         Evas_Object *bg = cal_util_add_bg(window, EINA_TRUE);
446         c_retv_if(!bg, false);
447
448         Evas_Object *conformant = elm_conformant_add(window);
449         c_retv_if(!conformant, false);
450
451         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
452         elm_win_resize_object_add(window, conformant);
453         evas_object_show(conformant);
454
455         Evas_Object *layout = __cal_main_add_base_layout(conformant);
456         c_retv_if(!layout, false);
457
458         evas_object_show(window);
459
460         ad->win = window;
461         ad->bg = bg;
462         ad->conform = conformant;
463         ad->base = layout;
464
465         int ret = __cal_main_initialize(ad);
466         c_retvm_if(ret, false, "__cal_main_initialize is failed(%d)", ret);
467
468         ret = __cal_main_add_naviframe(ad);
469         c_retvm_if(ret, false, "__cal_main_add_naviframe is failed(%d)", ret);
470
471         CAL_FN_END;
472
473         return true;
474 }
475
476 static void cal_terminate(void *data)
477 {
478         CAL_FN_START;
479
480         c_retm_if(!data, "data is null");
481
482         struct appdata *ad = data;
483
484         ug_destroy_all();
485
486         __cal_main_finish(ad);
487
488         if (ad->win)
489                 evas_object_del(ad->win);
490
491         CAL_FN_END;
492 }
493
494 static void __cal_main_raise_main_view(struct appdata *ad)
495 {
496         CAL_FN_START;
497
498         c_retm_if(!ad, "ad is null");
499
500         Elm_Object_Item* bottom = elm_naviframe_bottom_item_get(ad->naviframe);
501         c_ret_if(!bottom);
502
503         Elm_Object_Item* top = elm_naviframe_top_item_get(ad->naviframe);
504         c_ret_if(!top);
505
506         if (bottom != top)
507                 elm_naviframe_item_pop_to(bottom);
508
509         ad->current_view = ad->request_view;
510
511         CAL_FN_END;
512 }
513
514 static Eina_Bool __cal_main_live_calendar_destroy_callback(void *data)
515 {
516         struct appdata *ad = data;
517
518         if (ad->win) {
519                 elm_win_lower(ad->win);
520         }
521
522         if (ad->current_view == CV_DAY) {
523                 Evas_Object *content = elm_object_part_content_get(ad->main, "cont");
524                 cal_day_change_content_display_status(content);
525         }
526
527         return EINA_FALSE;
528 }
529
530 static Eina_Bool __cal_main_ug_destroy_callback(void *data)
531 {
532         c_retv_if(!data, EINA_FALSE);
533
534         struct appdata *ad = data;
535
536         if (!ad->main)
537                 elm_exit();
538         else
539                 elm_win_lower(ad->win);
540
541         return EINA_FALSE;
542 }
543
544 static inline void __cal_main_add_naviframe_content(struct appdata *ad)
545 {
546         CAL_FN_START;
547
548         switch (ad->request_view) {
549
550                 case CV_YEAR:
551                 case CV_MONTH:
552                 case CV_WEEK:
553                 case CV_DAY:
554                 case CV_LIST:
555                 case CV_TODO:
556                         __cal_main_raise_main_view(ad);
557
558                         if (!ad->main)
559                                 cal_main_create_view(ad);
560                         else
561                                 cal_main_change_view(ad->main, ad->request_view);
562                         if (ad->request_view == CV_TODO) {
563                                 ad->item_type = ITEM_TYPE_TODO;
564                         } else {
565                                 ad->item_type = ITEM_TYPE_EVENT;
566                         }
567                         break;
568
569                 case CV_DETAIL:
570                 {
571                         calendar_record_h record = NULL;
572
573                         record = _calendar_get_record_with_index(ad->cid);
574                         c_ret_if(!record);
575
576                         cal_detail_create_view(ad, record, __cal_main_ug_destroy_callback, ad);
577
578                         calendar_error_e error = CALENDAR_ERROR_NONE;
579
580                         error = calendar_record_destroy(record, NULL);
581                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
582                 }
583                         break;
584
585                 case CV_EDIT:
586                 {
587                         calendar_record_h record = NULL;
588                         calendar_error_e error = CALENDAR_ERROR_NONE;
589
590                         if (ad->item_type == ITEM_TYPE_TODO) {
591                                 error = calendar_record_create(_calendar_todo._uri, &record);
592                         } else {
593                                 error = calendar_record_create(_calendar_event._uri, &record);
594                         }
595                         c_retm_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
596
597                         cal_edit_create_view(ad, record, __cal_main_live_calendar_destroy_callback, ad);
598
599                         if (ad->current_view == CV_DAY) {
600                                 Evas_Object *content = elm_object_part_content_get(ad->main, "cont");
601                                 cal_day_change_content_display_status(content);
602                         }
603
604                         if(ad->main)
605                                 elm_win_lower(ad->win);
606                 }
607                 break;
608
609                 default:
610                         /* TODO: add more */
611                         break;
612         }
613
614         CAL_FN_END;
615 }
616
617 static void cal_resume(void *data)
618 {
619         CAL_FN_START;
620
621         c_retm_if(!data, "data is null");
622
623         struct appdata* ad = data;
624
625         ug_resume();
626
627         ad->is_pause = EINA_FALSE;
628
629         if (ad->is_update_view)
630                 __cal_main_changed_callback(ad);
631
632         CAL_FN_END;
633 }
634
635 static void __cal_main_select_view(service_h service, struct appdata* ad)
636 {
637         CAL_FN_START;
638
639         c_ret_if(!service);
640         c_ret_if(!ad);
641
642         char *operation = NULL;
643         char *caller = NULL;
644         char *count = NULL;
645         char *type = NULL;
646         char *index = NULL;
647
648         ad->request_view = CV_MONTH;
649
650         int r = service_get_operation(service, &operation);
651         c_warn_if(SERVICE_ERROR_NONE != r, "service_get_opertaion is failed(%d).", r);
652
653         if (!CAL_STRCMP(operation, SERVICE_OPERATION_DEFAULT)) {
654
655                 service_get_extra_data(service, CAL_APPSVC_PARAM_REMINDER_ID, &index);
656
657                 if (!CAL_STRLEN(index))
658                         service_get_extra_data(service, CAL_APPSVC_PARAM_LOCATION_REMINDER, &index);
659
660                 if (CAL_STRLEN(index) && (r = atoi(index))) {
661
662                         cal_noti_handler_show_for_location_reminder(r, ad);
663
664                         ad->request_view = CV_NOTI;
665                 } else {
666
667                         r = service_get_extra_data(service, CAL_APPSVC_PARAM_CALLER, &caller);
668                         c_warn_if(SERVICE_ERROR_NONE != r, "service_get_extra_data is failed(%d).", r);
669
670                         r = service_get_extra_data(service, CAL_APPSVC_PARAM_TYPE, &type);
671                         c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
672
673                         if (!CAL_STRCMP(caller, "live")) {
674
675                                 r = service_get_extra_data(service, CAL_APPSVC_PARAM_COUNT, &count);
676                                 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
677
678                                 if (CAL_STRLEN(count) && atoi(count))
679                                         ad->request_view = CV_LIST;
680                                 else {
681                                         ad->request_view = CV_EDIT;
682                                         ad->cid = 0;
683
684                                         //base_time
685                                         char *time = NULL;
686                                         struct tm base_time = {0};
687                                         r = service_get_extra_data(service, "base_time", &time);
688                                         long long int lli_time = strtoll(time, NULL, 10);
689                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
690                                         CAL_FREE(time);
691
692                                         ad->base_tm = base_time;
693
694                                         r = service_get_extra_data(service, CAL_APPCONTROL_ITEM_TYPE, &type);
695                                         c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
696
697                                         if (!CAL_STRCMP(type, "todo")) {
698                                                 ad->item_type = ITEM_TYPE_TODO;
699                                         } else  {
700                                                 ad->item_type = ITEM_TYPE_EVENT;
701                                         }
702
703                                 }
704
705                                 if (!CAL_STRCMP(type, "week")) {
706                                         ad->request_view = CV_WEEK;
707                                         ad->cid = 0;
708
709                                         //base_time
710                                         char *time = NULL;
711                                         struct tm base_time = {0};
712                                         r = service_get_extra_data(service, "base_time", &time);
713                                         long long int lli_time = strtoll(time, NULL, 10);
714                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
715                                         CAL_FREE(time);
716
717                                         ad->base_tm = base_time;
718                                 } else if (!CAL_STRCMP(type, "day")) {
719                                         ad->request_view = CV_DAY;
720                                         ad->cid = 0;
721
722                                         //base_time
723                                         char *time = NULL;
724                                         struct tm base_time = {0};
725                                         r = service_get_extra_data(service, "base_time", &time);
726                                         long long int lli_time = strtoll(time, NULL, 10);
727                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
728                                         CAL_FREE(time);
729
730                                         ad->base_tm = base_time;
731                                 } else if (!CAL_STRCMP(type, "month")) {
732                                         ad->request_view = CV_MONTH;
733                                         ad->cid = 0;
734
735                                         //base_time
736                                         char *time = NULL;
737                                         struct tm base_time = {0};
738                                         r = service_get_extra_data(service, "base_time", &time);
739                                         long long int lli_time = strtoll(time, NULL, 10);
740                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
741                                         CAL_FREE(time);
742
743                                         ad->base_tm = base_time;
744                                 } else if (!CAL_STRCMP(type, "task")) {
745                                         ad->request_view = CV_TODO;
746                                         ad->cid = 0;
747                                 }
748                         }
749                 }
750         }else if (!CAL_STRCMP(operation, SERVICE_OPERATION_VIEW)) {
751                 r = service_get_extra_data(service, CAL_APPSVC_PARAM_TYPE, &type);
752                 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
753
754                 if (!CAL_STRCMP(type, "event")) {
755                         ad->item_type = ITEM_TYPE_EVENT;
756                 } else if (!CAL_STRCMP(type, "todo")) {
757                         ad->item_type = ITEM_TYPE_TODO;
758                 }
759
760                 r = service_get_extra_data(service, CAL_APPSVC_PARAM_INDEX, &index);
761                 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
762
763                 if (CAL_STRLEN(index) && (r = atoi(index))) {
764                         ad->cid = r;
765
766                         ad->request_view = CV_DETAIL;
767                 }
768         }
769
770         CAL_FREE(operation);
771         CAL_FREE(caller);
772         CAL_FREE(count);
773         CAL_FREE(type);
774         CAL_FREE(index);
775
776         CAL_FN_END;
777 }
778
779 static void cal_pause(void *data)
780 {
781         CAL_FN_START;
782
783         c_ret_if(!data);
784
785         struct appdata* ad = data;
786         c_ret_if(!ad->naviframe);
787
788         ug_pause();
789
790         if (ad->is_aul) {
791                 ad->is_aul = EINA_FALSE;                                                                        //unset flag
792
793                 if (ad->content && elm_naviframe_top_item_get(ad->naviframe) != elm_naviframe_bottom_item_get(ad->naviframe))
794                         elm_naviframe_item_pop_to(ad->content); //pop to calendar app's content
795                 else
796                         elm_exit();                                                                                     //if there is no calendar app's contents, exit.
797
798                 ad->request_view = CV_UNKNOWN;
799         }
800
801         ad->is_pause = EINA_TRUE;
802
803         CAL_FN_END;
804 }
805
806 static void cal_service(service_h service, void *user_data)
807 {
808         CAL_FN_START;
809
810         c_ret_if(!service);
811         c_ret_if(!user_data);
812
813         struct appdata *ad = user_data;
814
815         __cal_main_select_view(service, ad);
816
817         if (ad->request_view == CV_NOTI) {
818                 if (!ad->main)
819                         elm_exit();
820                 else
821                         return;
822         }
823
824         if (ad->ug) {
825                 int ret = ug_destroy(ad->ug);
826                 c_warn_if(ret, "ug_destroy() is failed.");
827
828                 ad->ug = NULL;
829         }
830
831         __cal_main_add_naviframe_content(ad);
832
833         if (ad->win)
834                 elm_win_activate(ad->win);
835
836         evas_object_show(ad->win);
837
838         CAL_FN_END;
839 }
840
841 static void cal_device_orientation(app_device_orientation_e orientation, void *user_data)
842 {
843         c_retm_if(!user_data, "user_data is null.");
844
845         struct appdata *ad = user_data;
846         int rotate_val = 0;
847         Eina_Bool is_landscape_mode = EINA_FALSE;
848         int width = 0;
849         int height = 0;
850
851         ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
852
853         switch (orientation){
854         case APP_DEVICE_ORIENTATION_0:
855                 rotate_val = 0;
856                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
857                 break;
858         case APP_DEVICE_ORIENTATION_90:
859                 rotate_val = 90;
860                 is_landscape_mode = EINA_TRUE;
861                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
862                 break;
863         case APP_DEVICE_ORIENTATION_180:
864                 rotate_val = 180;
865                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
866                 break;
867         case APP_DEVICE_ORIENTATION_270:
868                 rotate_val = 270;
869                 is_landscape_mode = EINA_TRUE;
870                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
871                 break;
872         default:
873                 break;
874         }
875
876         if (ad->orientation == orientation)
877                 return;
878
879         ad->orientation = orientation;
880
881         ad->is_landscape_mode = is_landscape_mode;
882
883         if (ad->is_landscape_mode) {
884                 ad->win_w = height;
885                 ad->win_h = width;
886         } else {
887                 ad->win_w = width;
888                 ad->win_h = height;
889         }
890
891         elm_win_rotation_with_resize_set(ad->win, rotate_val);
892
893         cal_main_rotation(ad);
894
895 }
896
897 static void __cal_language_changed(void *user_data)
898 {
899         CAL_FN_START;
900
901         ug_send_event(UG_EVENT_LANG_CHANGE);
902
903         cal_lang_manager_apply();
904 }
905
906 static inline void __cal_check_first_day_of_week(int *first_day_of_week)
907 {
908         c_ret_if(!first_day_of_week);
909
910         int ret = vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, first_day_of_week);
911         c_warn_if(ret != 0, "vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK) is failed.");
912
913         if (ret != 0 || *first_day_of_week == -1)
914                 *first_day_of_week = cal_util_get_default_first_day_of_week();
915 }
916
917 static void __cal_region_format_changed(void *user_data)
918 {
919         CAL_FN_START;
920
921         struct appdata *ad = user_data;
922         c_ret_if(!ad);
923
924         __cal_check_first_day_of_week(&ad->wday_start);
925
926         ug_send_event(UG_EVENT_REGION_CHANGE);
927
928         cal_lang_manager_apply();
929 }
930
931 int main(int argc, char *argv[])
932 {
933         struct appdata ad;
934
935         app_event_callback_s event_callback;
936
937         event_callback.create = cal_create;
938         event_callback.terminate = cal_terminate;
939         event_callback.pause = cal_pause;
940         event_callback.resume = cal_resume;
941         event_callback.service = cal_service;
942         event_callback.low_memory = NULL;
943         event_callback.low_battery = NULL;
944         event_callback.device_orientation = cal_device_orientation;
945         event_callback.language_changed = __cal_language_changed;
946         event_callback.region_format_changed = __cal_region_format_changed;
947
948         memset(&ad, 0x0, sizeof(struct appdata));
949
950         return app_efl_main(&argc, &argv, &event_callback, &ad);
951 }