a469d16af04ce1a9903d3ef94ae59dc73e90b50f
[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 void __cal_device_orientation(void *data, Evas_Object *obj, void *event)
433 {
434         CAL_FN_START;
435
436         c_retm_if(!data, "user_data is null.");
437
438         struct appdata *ad = data;
439         Eina_Bool is_landscape_mode = EINA_FALSE;
440
441         int rotation = elm_win_rotation_get(obj);
442         c_ret_if(rotation != CAL_WINDOW_ROTATION_0
443                 && rotation != CAL_WINDOW_ROTATION_90
444                 && rotation != CAL_WINDOW_ROTATION_180
445                 && rotation != CAL_WINDOW_ROTATION_270);
446
447         if (ad->rotation == (cal_window_rotation)rotation)
448                 return;
449
450         switch (rotation){
451         case CAL_WINDOW_ROTATION_0:
452                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
453                 break;
454         case CAL_WINDOW_ROTATION_90:
455                 is_landscape_mode = EINA_TRUE;
456                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
457                 break;
458         case CAL_WINDOW_ROTATION_180:
459                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
460                 break;
461         case CAL_WINDOW_ROTATION_270:
462                 is_landscape_mode = EINA_TRUE;
463                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
464                 break;
465         default:
466                 break;
467         }
468
469         ad->rotation = (cal_window_rotation)rotation;
470         ad->is_landscape_mode = is_landscape_mode;
471
472         int width = 0;
473         int height = 0;
474
475         ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
476
477         if (ad->is_landscape_mode) {
478                 ad->win_w = height;
479                 ad->win_h = width;
480         } else {
481                 ad->win_w = width;
482                 ad->win_h = height;
483         }
484
485         cal_main_rotation(ad);
486 }
487
488 static bool cal_create(void *data)
489 {
490         CAL_FN_START;
491
492         c_retvm_if(!data, false, "data is null");
493
494         struct appdata *ad = data;
495
496         Evas_Object *window = cal_util_add_window(CALENDAR, &ad->win_w, &ad->win_h);
497         c_retv_if(!window, false);
498
499         evas_object_smart_callback_add(window, "profile,changed", __cal_main_win_profile_changed_callback, ad);
500         evas_object_smart_callback_add(window, "wm,rotation,changed", __cal_device_orientation, ad);
501
502         Evas_Object *bg = cal_util_add_bg(window, EINA_TRUE);
503         c_retv_if(!bg, false);
504
505         Evas_Object *conformant = elm_conformant_add(window);
506         c_retv_if(!conformant, false);
507
508         evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
509         elm_win_resize_object_add(window, conformant);
510         evas_object_show(conformant);
511
512         Evas_Object *layout = __cal_main_add_base_layout(conformant);
513         c_retv_if(!layout, false);
514
515         ad->win = window;
516         ad->bg = bg;
517         ad->conform = conformant;
518         ad->base = layout;
519
520         int ret = __cal_main_initialize(ad);
521         c_retvm_if(ret, false, "__cal_main_initialize is failed(%d)", ret);
522
523         ret = __cal_main_add_naviframe(ad);
524         c_retvm_if(ret, false, "__cal_main_add_naviframe is failed(%d)", ret);
525
526         CAL_FN_END;
527
528         return true;
529 }
530
531 static void cal_terminate(void *data)
532 {
533         CAL_FN_START;
534
535         c_retm_if(!data, "data is null");
536
537         struct appdata *ad = data;
538
539         ug_destroy_all();
540
541         __cal_main_finish(ad);
542
543         if (ad->win)
544                 evas_object_del(ad->win);
545
546         CAL_FN_END;
547 }
548
549 static void __cal_main_raise_main_view(struct appdata *ad)
550 {
551         CAL_FN_START;
552
553         c_retm_if(!ad, "ad is null");
554
555         Elm_Object_Item* bottom = elm_naviframe_bottom_item_get(ad->naviframe);
556         c_ret_if(!bottom);
557
558         Elm_Object_Item* top = elm_naviframe_top_item_get(ad->naviframe);
559         c_ret_if(!top);
560
561         if (bottom != top)
562                 elm_naviframe_item_pop_to(bottom);
563
564         ad->current_view = ad->request_view;
565
566         CAL_FN_END;
567 }
568
569 static Eina_Bool __cal_main_live_calendar_destroy_callback(void *data)
570 {
571         struct appdata *ad = data;
572
573         if (ad->win) {
574                 elm_win_lower(ad->win);
575         }
576
577         if (ad->current_view == CV_DAY) {
578                 Evas_Object *content = elm_object_part_content_get(ad->main, "cont");
579                 cal_day_change_content_display_status(content);
580         }
581
582         return EINA_FALSE;
583 }
584
585 static Eina_Bool __cal_main_ug_destroy_callback(void *data)
586 {
587         c_retv_if(!data, EINA_FALSE);
588
589         struct appdata *ad = data;
590
591         if (!ad->main)
592                 elm_exit();
593         else
594                 elm_win_lower(ad->win);
595
596         return EINA_FALSE;
597 }
598
599 static inline void __cal_main_add_naviframe_content(struct appdata *ad)
600 {
601         CAL_FN_START;
602
603         switch (ad->request_view) {
604
605                 case CV_YEAR:
606                 case CV_MONTH:
607                 case CV_WEEK:
608                 case CV_DAY:
609                 case CV_LIST:
610                 case CV_TODO:
611                         __cal_main_raise_main_view(ad);
612
613                         if (!ad->main)
614                                 cal_main_create_view(ad);
615                         else
616                                 cal_main_change_view(ad->main, ad->request_view);
617                         if (ad->request_view == CV_TODO) {
618                                 ad->item_type = ITEM_TYPE_TODO;
619                         } else {
620                                 ad->item_type = ITEM_TYPE_EVENT;
621                         }
622                         break;
623
624                 case CV_DETAIL:
625                 {
626                         calendar_record_h record = NULL;
627
628                         record = _calendar_get_record_with_index(ad->cid);
629                         c_ret_if(!record);
630
631                         cal_detail_create_view(ad, record, __cal_main_ug_destroy_callback, ad);
632
633                         calendar_error_e error = CALENDAR_ERROR_NONE;
634
635                         error = calendar_record_destroy(record, NULL);
636                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
637                 }
638                         break;
639
640                 case CV_EDIT:
641                 {
642                         calendar_record_h record = NULL;
643                         calendar_error_e error = CALENDAR_ERROR_NONE;
644
645                         if (ad->item_type == ITEM_TYPE_TODO) {
646                                 error = calendar_record_create(_calendar_todo._uri, &record);
647                         } else {
648                                 error = calendar_record_create(_calendar_event._uri, &record);
649                         }
650                         c_retm_if(error != CALENDAR_ERROR_NONE, "calendar_record_create() is failed(%x)", error);
651
652                         cal_edit_create_view(ad, record, __cal_main_live_calendar_destroy_callback, ad);
653
654                         if (ad->current_view == CV_DAY) {
655                                 Evas_Object *content = elm_object_part_content_get(ad->main, "cont");
656                                 cal_day_change_content_display_status(content);
657                         }
658
659                         if(ad->main)
660                                 elm_win_lower(ad->win);
661                 }
662                 break;
663
664                 default:
665                         /* TODO: add more */
666                         break;
667         }
668
669         CAL_FN_END;
670 }
671
672 static void cal_resume(void *data)
673 {
674         CAL_FN_START;
675
676         c_retm_if(!data, "data is null");
677
678         struct appdata* ad = data;
679
680         ug_resume();
681
682         ad->is_pause = EINA_FALSE;
683
684         if (ad->is_update_view)
685                 __cal_main_changed_callback(ad);
686
687         CAL_FN_END;
688 }
689
690 static void __cal_main_select_view(service_h service, struct appdata* ad)
691 {
692         CAL_FN_START;
693
694         c_ret_if(!service);
695         c_ret_if(!ad);
696
697         char *operation = NULL;
698         char *caller = NULL;
699         char *count = NULL;
700         char *type = NULL;
701         char *index = NULL;
702
703         ad->request_view = CV_MONTH;
704
705         int r = service_get_operation(service, &operation);
706         c_warn_if(SERVICE_ERROR_NONE != r, "service_get_opertaion is failed(%d).", r);
707
708         if (!CAL_STRCMP(operation, SERVICE_OPERATION_DEFAULT)) {
709
710                 service_get_extra_data(service, CAL_APPSVC_PARAM_REMINDER_ID, &index);
711
712                 if (!CAL_STRLEN(index))
713                         service_get_extra_data(service, CAL_APPSVC_PARAM_LOCATION_REMINDER, &index);
714
715                 if (CAL_STRLEN(index) && (r = atoi(index))) {
716
717                         cal_noti_handler_show_for_location_reminder(r, ad);
718
719                         ad->request_view = CV_NOTI;
720                 } else {
721
722                         r = service_get_extra_data(service, CAL_APPSVC_PARAM_CALLER, &caller);
723                         c_warn_if(SERVICE_ERROR_NONE != r, "service_get_extra_data is failed(%d).", r);
724
725                         r = service_get_extra_data(service, CAL_APPSVC_PARAM_TYPE, &type);
726                         c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
727
728                         if (!CAL_STRCMP(caller, "live")) {
729
730                                 r = service_get_extra_data(service, CAL_APPSVC_PARAM_COUNT, &count);
731                                 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
732
733                                 if (CAL_STRLEN(count) && atoi(count))
734                                         ad->request_view = CV_LIST;
735                                 else {
736                                         ad->request_view = CV_EDIT;
737                                         ad->cid = 0;
738
739                                         //base_time
740                                         char *time = NULL;
741                                         struct tm base_time = {0};
742                                         r = service_get_extra_data(service, "base_time", &time);
743                                         long long int lli_time = strtoll(time, NULL, 10);
744                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
745                                         CAL_FREE(time);
746
747                                         ad->base_tm = base_time;
748
749                                         r = service_get_extra_data(service, "itemType", &type);
750                                         c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
751
752                                         if (!CAL_STRCMP(type, "todo")) {
753                                                 ad->item_type = ITEM_TYPE_TODO;
754                                         } else  {
755                                                 ad->item_type = ITEM_TYPE_EVENT;
756                                         }
757
758                                 }
759
760                                 if (!CAL_STRCMP(type, "week")) {
761                                         ad->request_view = CV_WEEK;
762                                         ad->cid = 0;
763
764                                         //base_time
765                                         char *time = NULL;
766                                         struct tm base_time = {0};
767                                         r = service_get_extra_data(service, "base_time", &time);
768                                         long long int lli_time = strtoll(time, NULL, 10);
769                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
770                                         CAL_FREE(time);
771
772                                         ad->base_tm = base_time;
773                                 } else if (!CAL_STRCMP(type, "day")) {
774                                         ad->request_view = CV_DAY;
775                                         ad->cid = 0;
776
777                                         //base_time
778                                         char *time = NULL;
779                                         struct tm base_time = {0};
780                                         r = service_get_extra_data(service, "base_time", &time);
781                                         long long int lli_time = strtoll(time, NULL, 10);
782                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
783                                         CAL_FREE(time);
784
785                                         ad->base_tm = base_time;
786                                 } else if (!CAL_STRCMP(type, "month")) {
787                                         ad->request_view = CV_MONTH;
788                                         ad->cid = 0;
789
790                                         //base_time
791                                         char *time = NULL;
792                                         struct tm base_time = {0};
793                                         r = service_get_extra_data(service, "base_time", &time);
794                                         long long int lli_time = strtoll(time, NULL, 10);
795                                         cal_util_convert_lli_to_tm(NULL, lli_time, &base_time);
796                                         CAL_FREE(time);
797
798                                         ad->base_tm = base_time;
799                                 } else if (!CAL_STRCMP(type, "task")) {
800                                         ad->request_view = CV_TODO;
801                                         ad->cid = 0;
802                                 }
803                         }
804                 }
805         }else if (!CAL_STRCMP(operation, SERVICE_OPERATION_VIEW)) {
806                 r = service_get_extra_data(service, CAL_APPSVC_PARAM_TYPE, &type);
807                 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
808
809                 if (!CAL_STRCMP(type, "event")) {
810                         ad->item_type = ITEM_TYPE_EVENT;
811                 } else if (!CAL_STRCMP(type, "todo")) {
812                         ad->item_type = ITEM_TYPE_TODO;
813                 }
814
815                 r = service_get_extra_data(service, CAL_APPSVC_PARAM_INDEX, &index);
816                 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
817
818                 if (CAL_STRLEN(index) && (r = atoi(index))) {
819                         ad->cid = r;
820
821                         ad->request_view = CV_DETAIL;
822                 }
823         }
824
825         CAL_FREE(operation);
826         CAL_FREE(caller);
827         CAL_FREE(count);
828         CAL_FREE(type);
829         CAL_FREE(index);
830
831         CAL_FN_END;
832 }
833
834 static void cal_pause(void *data)
835 {
836         CAL_FN_START;
837
838         c_ret_if(!data);
839
840         struct appdata* ad = data;
841         c_ret_if(!ad->naviframe);
842
843         ug_pause();
844
845         if (ad->is_aul) {
846                 ad->is_aul = EINA_FALSE;                                                                        //unset flag
847
848                 if (ad->content && elm_naviframe_top_item_get(ad->naviframe) != elm_naviframe_bottom_item_get(ad->naviframe))
849                         elm_naviframe_item_pop_to(ad->content); //pop to calendar app's content
850                 else
851                         elm_exit();                                                                                     //if there is no calendar app's contents, exit.
852
853                 ad->request_view = CV_UNKNOWN;
854         }
855
856         ad->is_pause = EINA_TRUE;
857
858         CAL_FN_END;
859 }
860
861 static void cal_service(service_h service, void *user_data)
862 {
863         CAL_FN_START;
864
865         c_ret_if(!service);
866         c_ret_if(!user_data);
867
868         struct appdata *ad = user_data;
869
870         __cal_main_select_view(service, ad);
871
872         if (ad->request_view == CV_NOTI) {
873                 if (!ad->main)
874                         elm_exit();
875                 else
876                         return;
877         }
878
879         if (ad->ug) {
880                 int ret = ug_destroy(ad->ug);
881                 c_warn_if(ret, "ug_destroy() is failed.");
882
883                 ad->ug = NULL;
884         }
885
886         __cal_main_add_naviframe_content(ad);
887
888         if (ad->win)
889                 elm_win_activate(ad->win);
890
891         evas_object_show(ad->win);
892
893         CAL_FN_END;
894 }
895
896 static void __cal_language_changed(void *user_data)
897 {
898         CAL_FN_START;
899
900         ug_send_event(UG_EVENT_LANG_CHANGE);
901
902         cal_lang_manager_apply();
903 }
904
905 static inline void __cal_check_first_day_of_week(int *first_day_of_week)
906 {
907         c_ret_if(!first_day_of_week);
908
909         int ret = vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, first_day_of_week);
910         c_warn_if(ret != 0, "vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK) is failed.");
911
912         if (ret != 0 || *first_day_of_week == -1)
913                 *first_day_of_week = cal_util_get_default_first_day_of_week();
914 }
915
916 static void __cal_region_format_changed(void *user_data)
917 {
918         CAL_FN_START;
919
920         struct appdata *ad = user_data;
921         c_ret_if(!ad);
922
923         __cal_check_first_day_of_week(&ad->wday_start);
924
925         ug_send_event(UG_EVENT_REGION_CHANGE);
926
927         cal_lang_manager_apply();
928 }
929
930 int main(int argc, char *argv[])
931 {
932         struct appdata ad;
933
934         app_event_callback_s event_callback;
935
936         event_callback.create = cal_create;
937         event_callback.terminate = cal_terminate;
938         event_callback.pause = cal_pause;
939         event_callback.resume = cal_resume;
940         event_callback.service = cal_service;
941         event_callback.low_memory = NULL;
942         event_callback.low_battery = NULL;
943         event_callback.device_orientation = NULL;
944         event_callback.language_changed = __cal_language_changed;
945         event_callback.region_format_changed = __cal_region_format_changed;
946
947         memset(&ad, 0x0, sizeof(struct appdata));
948
949         return app_efl_main(&argc, &argv, &event_callback, &ad);
950 }