3 * Copyright 2012 Samsung Electronics Co., Ltd
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
9 * http://www.tizenopensource.org/license
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.
19 #include <appcore-efl.h>
22 #include <ui-gadget.h>
25 #include <vconf-keys.h>
31 #include "noti-handler.h"
33 #define CAL_APPSVC_PARAM_NAME AUL_K_SERVICE_NAME
34 #define CAL_APPSVC_PARAM_CALLER "caller"
35 #define CAL_APPSVC_PARAM_COUNT "count"
36 #define CAL_APPSVC_PARAM_INDEX "index"
38 #define NOTI_TIME_CHANGED "setting_time_changed"
40 static void __cal_main_setup_data(struct appdata *ad);
41 static Eina_Bool __cal_main_update_today(void *data);
43 static Evas_Object* __cal_main_add_base_layout(Evas_Object *win)
46 ly = cal_util_add_layout(win, NULL);
48 elm_win_resize_object_add(win, ly);
53 static void __cal_main_changed_callback(void *data)
59 struct appdata *ad = data;
61 __cal_main_setup_data(ad);
64 ad->is_update_view = EINA_TRUE;
68 cal_main_update_view(ad);
73 static void __cal_main_set_today_tm(struct appdata *ad)
77 time_t now = time(NULL);
78 localtime_r(&now, &tm);
79 tm.tm_min = 0; // min, sec reset for mktime()
85 time_t t = mktime(&tm);
87 if (t == (time_t)-1) {
88 ERR("mktime() is failed");
93 t = t + (24 * 60 * 60) - time(NULL);
94 ad->today_tmr = ecore_timer_add((double)t, __cal_main_update_today, ad);
97 static Eina_Bool __cal_main_update_today(void *data)
99 c_retv_if(!data, ECORE_CALLBACK_CANCEL);
101 struct appdata *ad = data;
103 __cal_main_set_today_tm(ad);
106 edje_object_signal_emit(ad->today_edj, "update_today", "prog");
108 return ECORE_CALLBACK_CANCEL;
111 static void __cal_main_setup_data(struct appdata *ad)
115 __cal_main_set_today_tm(ad);
117 if (ad->base_tm.tm_year == 0)
118 ad->base_tm = ad->today_tm;
122 int r = vconf_get_int(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, &val);
126 ad->wday_start = val;
128 r = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &val);
131 else if( val == 1 || val == 2)
132 ad->is_hour24 = val -1;
134 ERR("vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &val); error");
136 ad->is_update_view = EINA_TRUE;
138 ad->acct_id = ALL_VISIBILITY_ACCOUNT;
143 static void __cal_main_time_changed_callback(void *data)
147 struct appdata *ad = data;
149 __cal_main_update_today(ad);
152 static int __cal_main_initialize_notification(struct appdata *ad)
154 int fd = heynoti_init();
155 c_retvm_if(fd < 0, -1, "heynoti_init() is failed(%d)", fd);
157 int r = heynoti_subscribe(fd, NOTI_TIME_CHANGED, __cal_main_time_changed_callback, ad);
159 ERR("heynoti_subscribe(%d, NOTI_TIME_CHANGED) is failed(%d)", fd, r);
164 r = heynoti_attach_handler(fd);
166 ERR("heynoti_attach_handler(%d) is failed(%d)", fd, r);
174 static void __cal_main_timezone_vconf_changed_callback(keynode_t *node, void *user_data)
179 c_ret_if(!user_data);
181 Eina_Bool is_lock_timezone = vconf_keynode_get_int(node);
185 if (is_lock_timezone)
186 text = vconf_get_str(CAL_VCONFKEY_LOCK_TIMEZONE_PATH);
188 text = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
190 if (CAL_STRLEN(text)) {
192 cal_util_set_timezone(text);
196 cal_main_update_view(user_data);
199 ERR("vconf_get_str is failed.");
204 static void __cal_main_week_of_day_format_vconf_changed_callback(keynode_t *node, void *user_data)
208 c_retm_if(!node, "node is null");
209 c_retm_if(!user_data, "user_data is null");
211 struct appdata* ad = user_data;
212 int r = vconf_keynode_get_int(node);
213 c_retm_if(r < 0, "vconf_keynode_get_int is failed");
217 cal_main_update_view(ad);
222 static void __cal_main_view_of_month_format_vconf_changed_callback(keynode_t *node, void *user_data)
226 c_retm_if(!node, "node is null");
227 c_retm_if(!user_data, "user_data is null");
229 struct appdata* ad = user_data;
230 int r = vconf_keynode_get_int(node);
231 c_retm_if(r < 0, "vconf_keynode_get_int is failed");
235 cal_main_update_view(ad);
240 static void __cal_main_view_of_display_compelete_todo_vconf_changed_callback(keynode_t *node, void *user_data)
244 c_retm_if(!node, "node is null");
245 c_retm_if(!user_data, "user_data is null");
247 struct appdata* ad = user_data;
249 if (CV_TODO != ad->current_view)
252 int r = vconf_keynode_get_int(node);
253 c_retm_if(r < 0, "vconf_keynode_get_int is failed");
255 ad->is_display_complete_todo = r;
257 cal_main_update_view(ad);
262 static void __cal_main_view_of_display_week_number_vconf_changed_callback(keynode_t *node, void *user_data)
266 c_retm_if(!node, "node is null");
267 c_retm_if(!user_data, "user_data is null");
269 struct appdata* ad = user_data;
271 if (CV_MONTH != ad->current_view && !ad->is_weekly)
274 int r = vconf_keynode_get_int(node);
275 c_retm_if(r < 0, "vconf_keynode_get_int is failed");
277 ad->is_display_weeknumber = r;
279 cal_main_update_view(ad);
284 static void __cal_main_time_format_vconf_changed_callback(keynode_t *node, void *user_data)
288 c_retm_if(!node, "node is null");
289 c_retm_if(!user_data, "user_data is null");
291 struct appdata* ad = user_data;
293 int r = vconf_keynode_get_int(node);
294 c_retm_if(r < 0, "vconf_keynode_get_int is failed");
296 ad->is_hour24 = r -1;
298 cal_main_update_view(ad);
303 static int __cal_main_initialize(struct appdata *ad)
307 c_retvm_if(!ad, -1, "ad is null");
309 __cal_main_setup_data(ad);
311 int r = CALENDAR_SVC_CONNECT();
312 c_retvm_if(r != CAL_SUCCESS, -1, "CALENDAR_SVC_CONNECT is failed(%d)", r);
314 cal_util_connect_pattern_generator();
316 cal_util_initialize_timezone();
318 elm_theme_extension_add(NULL, "/opt/apps/org.tizen.calendar/res/edje/theme.edj");
320 r = vconf_notify_key_changed(CAL_VCONFKEY_FOCUS_VIEW,__cal_main_view_of_month_format_vconf_changed_callback, ad);
321 c_warn_if (r, "vconf_notify_key_changed failed");
323 r = vconf_notify_key_changed(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, __cal_main_view_of_display_week_number_vconf_changed_callback, ad);
324 c_warn_if (r, "vconf_notify_key_changed failed");
326 r = vconf_notify_key_changed(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, __cal_main_view_of_display_compelete_todo_vconf_changed_callback, ad);
327 c_warn_if (r, "vconf_notify_key_changed failed");
329 r = vconf_notify_key_changed(CAL_VCONFKEY_FIRST_DAY_OF_WEEK , __cal_main_week_of_day_format_vconf_changed_callback, ad);
330 c_warn_if (r, "vconf_notify_key_changed failed");
332 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224 , __cal_main_time_format_vconf_changed_callback, ad);
333 c_warn_if (r, "vconf_notify_key_changed failed");
335 r = vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT , __cal_main_timezone_vconf_changed_callback, ad);
336 c_warn_if (r, "vconf_notify_key_changed failed");
338 r = vconf_notify_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, __cal_main_timezone_vconf_changed_callback, ad);
339 c_warn_if (r, "vconf_notify_key_changed failed");
341 r = vconf_notify_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_PATH, __cal_main_timezone_vconf_changed_callback, ad);
342 c_warn_if (r, "vconf_notify_key_changed failed");
344 ad->notifd = __cal_main_initialize_notification(ad);
345 c_warn_if(ad->notifd < 0, "__cal_main_initialize_notification is failed(%d)", ad->notifd);
347 r = account_connect();
348 c_warn_if(r != ACCOUNT_ERROR_NONE, "account_connect is failed(%x)", r);
350 r = contacts_svc_connect();
351 c_warn_if(r!=CTS_SUCCESS, "contacts_svc_connect() is failed(%d)", r);
353 UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_ENABLE);
360 static void __cal_main_finish(struct appdata *ad)
365 heynoti_close(ad->notifd);
367 cal_util_disconnect_pattern_generator();
369 int r = contacts_svc_disconnect();
370 c_warn_if(r!=CTS_SUCCESS, "contacts_svc_disconnect() is failed(%d)", r);
372 r = account_disconnect();
373 c_warn_if(r != ACCOUNT_ERROR_NONE, "account_connect is failed(%x)", r);
375 r = vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_PATH, __cal_main_timezone_vconf_changed_callback);
376 c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_PATH) failed");
378 r = vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF, __cal_main_timezone_vconf_changed_callback);
379 c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_LOCK_TIMEZONE_ON_OFF) failed");
381 r = vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT, __cal_main_timezone_vconf_changed_callback);
382 c_warn_if(r, "vconf_ignore_key_changed(VCONFKEY_SETAPPL_TIMEZONE_INT) failed");
384 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __cal_main_time_format_vconf_changed_callback);
385 c_warn_if(r, "vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224) failed");
387 r = vconf_ignore_key_changed(CAL_VCONFKEY_FIRST_DAY_OF_WEEK, __cal_main_week_of_day_format_vconf_changed_callback);
388 c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_FIRST_DAY_OF_WEEK) failed");
390 r = vconf_ignore_key_changed(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF, __cal_main_view_of_display_compelete_todo_vconf_changed_callback);
391 c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_COMPLETE_TODO_ON_OFF) failed");
393 r = vconf_ignore_key_changed(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF, __cal_main_view_of_display_week_number_vconf_changed_callback);
394 c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_WEEK_NMUBER_ON_OFF) failed");
396 r = vconf_ignore_key_changed(CAL_VCONFKEY_FOCUS_VIEW, __cal_main_view_of_month_format_vconf_changed_callback);
397 c_warn_if(r, "vconf_ignore_key_changed(CAL_VCONFKEY_FOCUS_VIEW) failed");
399 CALENDAR_SVC_DISCONNECT();
402 static int __cal_main_add_naviframe(struct appdata *ad)
404 c_retvm_if(!ad, -1, "ad is null");
408 ad->nv = elm_naviframe_add(ad->base);
409 c_retvm_if(!ad->nv, -1, "elm_naviframe_add returned null");
411 elm_object_part_content_set(ad->base, "elm.swallow.content", ad->nv);
418 static void __cal_main_win_profile_changed_callback(void *data, Evas_Object *obj, void *event)
423 struct appdata *ad = data;
426 Evas_Object *base = ad->base;
428 const char *profile = elm_config_profile_get();
431 if (!CAL_STRCMP(profile, "desktop"))
432 elm_layout_theme_set(base, "layout", "application", "noindicator");
434 elm_layout_theme_set(base, "layout", "application", "default");
436 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
438 elm_win_resize_object_add(obj, base);
440 evas_object_show(base);
443 static bool cal_create(void *data)
447 c_retvm_if(!data, false, "data is null");
449 struct appdata *ad = data;
451 // Add code by Graphics part
452 elm_config_preferred_engine_set("opengl_x11");
454 ad->win = cal_util_add_window(CALENDAR_PACKAGE, &ad->win_w, &ad->win_h);
455 c_retvm_if(!ad->win, false, "ad->win is null");
457 evas_object_smart_callback_add(ad->win, "profile,changed", __cal_main_win_profile_changed_callback, ad);
459 ad->bg = cal_util_add_bg(ad->win, EINA_TRUE);
460 c_retvm_if(!ad->bg, false, "ad->bg is null");
462 ad->base = __cal_main_add_base_layout(ad->win);
463 c_retvm_if(!ad->base, false, "ad->base is null");
465 int r = __cal_main_initialize(ad);
466 c_retvm_if(r, false, "__cal_main_initialize is failed(%d)", r);
468 r = __cal_main_add_naviframe(ad);
469 c_retvm_if(r, false, "__cal_main_add_naviframe is failed(%d)", r);
471 ad->calendar_facebook_id = cal_account_svc_get_facebook_calendar_id();
478 static void cal_terminate(void *data)
482 c_retm_if(!data, "data is null");
484 struct appdata *ad = data;
488 __cal_main_finish(ad);
491 evas_object_del(ad->win);
496 static void __cal_main_raise_main_view(struct appdata *ad)
500 c_retm_if(!ad, "ad is null");
502 Elm_Object_Item* bottom = elm_naviframe_bottom_item_get(ad->nv);
505 elm_naviframe_item_pop_to(bottom);
507 if (ad->main_navi_item == bottom)
508 elm_naviframe_item_pop(ad->nv);
513 static inline void __cal_main_add_naviframe_content(struct appdata *ad)
517 switch (ad->request_view) {
524 __cal_main_raise_main_view(ad);
525 cal_main_create_view(ad);
529 cal_detail_create_view(ad,ad->base);
533 cal_edit_create_view(ad,ad->base);
541 Evas_Object *back_button = elm_object_item_part_content_get(elm_naviframe_bottom_item_get(ad->nv), "prev_btn");
542 c_ret_if(!back_button);
544 elm_object_focus_allow_set(back_button, EINA_FALSE);
549 static void cal_resume(void *data)
553 c_retm_if(!data, "data is null");
555 struct appdata* ad = data;
559 ad->is_pause = EINA_FALSE;
561 if (ad->is_update_view)
562 __cal_main_changed_callback(ad);
567 static void __cal_main_select_view(service_h service, struct appdata* ad)
574 char *operation = NULL;
575 char *alarm_id = NULL;
579 ad->request_view = CV_MONTH;
581 int r = service_get_operation(service, &operation);
582 c_warn_if(SERVICE_ERROR_NONE != r, "service_get_opertaion is failed(%d).", r);
584 if (!CAL_STRCMP(operation, SERVICE_OPERATION_DEFAULT)) {
586 r = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &alarm_id);
587 c_warn_if(SERVICE_ERROR_NONE != r, "service_get_extra_data is failed(%d).", r);
589 if (CAL_STRLEN(alarm_id) && (r = atoi(alarm_id))) {
591 cal_noti_handler_callback(r, ad);
593 ad->request_view = CV_NOTI;
596 r = service_get_extra_data(service, CAL_APPSVC_PARAM_CALLER, &caller);
597 c_warn_if(SERVICE_ERROR_NONE != r, "service_get_extra_data is failed(%d).", r);
599 if (!CAL_STRCMP(caller, "live")) {
601 r = service_get_extra_data(service, CAL_APPSVC_PARAM_COUNT, &count);
602 c_warn_if(SERVICE_ERROR_NONE != r,"service_get_extra_data is failed(%d).", r);
604 if (CAL_STRLEN(count) && atoi(count))
605 ad->request_view = CV_LIST;
607 ad->request_view = CV_EDIT;
627 static void cal_pause(void *data)
633 struct appdata* ad = data;
640 ad->is_aul = EINA_FALSE; //unset flag
642 if (ad->content && elm_naviframe_top_item_get(ad->nv) != elm_naviframe_bottom_item_get(ad->nv))
643 elm_naviframe_item_pop_to(ad->content); //pop to calendar app's content
645 elm_exit(); //if there is no calendar app's contents, exit.
647 ad->request_view = CV_UNKNOWN;
650 ad->is_pause = EINA_TRUE;
655 static void cal_service(service_h service, void *user_data)
660 c_ret_if(!user_data);
662 struct appdata *ad = user_data;
666 __cal_main_select_view(service, ad);
668 if (ad->request_view == CV_NOTI)
672 elm_win_activate(ad->win);
674 evas_object_show(ad->win);
676 __cal_main_add_naviframe_content(ad);
678 if (ad->is_landscape_mode)
679 cal_main_update_view(ad);
684 static void cal_device_orientation(app_device_orientation_e orientation, void *user_data)
686 c_retm_if(!user_data, "user_data is null.");
688 struct appdata *ad = user_data;
690 Eina_Bool is_landscape_mode = EINA_FALSE;
694 ecore_x_window_size_get(ecore_x_window_root_first_get(), &width, &height);
696 switch (orientation){
697 case APP_DEVICE_ORIENTATION_0:
700 case APP_DEVICE_ORIENTATION_90:
702 is_landscape_mode = EINA_TRUE;
704 case APP_DEVICE_ORIENTATION_180:
707 case APP_DEVICE_ORIENTATION_270:
709 is_landscape_mode = EINA_TRUE;
715 ad->is_landscape_mode = is_landscape_mode;
717 if (ad->is_landscape_mode) {
725 elm_win_rotation_set(ad->win, rotate_val);
726 cal_main_update_view(ad);
731 int main(int argc, char *argv[])
735 app_event_callback_s event_callback;
737 event_callback.create = cal_create;
738 event_callback.terminate = cal_terminate;
739 event_callback.pause = cal_pause;
740 event_callback.resume = cal_resume;
741 event_callback.service = cal_service;
742 event_callback.low_memory = NULL;
743 event_callback.low_battery = NULL;
744 event_callback.device_orientation = cal_device_orientation;
745 event_callback.language_changed = NULL;
746 event_callback.region_format_changed = NULL;
748 memset(&ad, 0x0, sizeof(struct appdata));
750 return app_efl_main(&argc, &argv, &event_callback, &ad);