dd618f2510b6adbf0c1965dbaeedf7b945e8b311
[apps/core/preloaded/calendar.git] / src / noti-handler.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 <alarm.h>
20 #include <aul.h>
21 #include <Ecore_X.h>
22 #include <utilX.h>
23 #include <app.h>
24 #include <vconf.h>
25
26 #include <notification.h>
27 #include <appsvc.h>
28 #include <reminder.h>
29
30 #include "noti-handler.h"
31 #include "cld-images.h"
32
33 #define SERVICE_CALENDAR_NAME "operation"
34 #define SERVICE_CALENDAR_INDEX_NAME "index"
35
36 static calendar_record_h __cal_noti_handler_get_record_from_calendar_alarm(calendar_record_h calendar_alarm)
37 {
38         c_retv_if(!calendar_alarm, NULL);
39
40         int record_index = 0;
41
42         calendar_error_e error = CALENDAR_ERROR_NONE;
43
44         calendar_record_h record = NULL;
45
46         error = calendar_record_get_int(calendar_alarm, _calendar_alarm.event_id, &record_index);
47         if (error != CALENDAR_ERROR_NONE) {
48                 error = calendar_db_get_record(_calendar_event._uri, record_index, &record);
49         } else {
50                 error = calendar_record_get_int(calendar_alarm, _calendar_alarm.todo_id, &record_index);
51                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
52
53                 error = calendar_db_get_record(_calendar_todo._uri, record_index, &record);
54         }
55
56         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
57
58         return record;
59 }
60
61 static void __cal_noti_handler_add_noti(struct appdata* ad, calendar_record_h record)
62 {
63         CAL_FN_START;
64
65         c_ret_if(!ad);
66         c_ret_if(!record);
67
68         calendar_time_s start_time = {0};
69
70         _calendar_get_start_time(record, &start_time);
71
72         struct tm tm = {0};
73
74         if (_calendar_is_allday_record(record)) {
75                 tm.tm_year = start_time.time.date.year - 1900;
76                 tm.tm_mon = start_time.time.date.month - 1;
77                 tm.tm_mday = start_time.time.date.mday;
78         } else
79                 cal_util_convert_lli_to_tm(NULL, start_time.time.utime, &tm);
80
81         char *title = _calendar_get_summary(record);
82
83         notification_h notification = NULL;
84
85         notification_error_e error = NOTIFICATION_ERROR_NONE;
86
87         int notification_id = 0;
88
89         int ret = vconf_get_int(CAL_VCONFKEY_NOTIFICATION_ID, &notification_id);
90         c_warn_if(ret != 0,"vconf_get_int(CAL_VCONFKEY_NOTIFICATION_ID) is failed(%d)", ret);
91
92         Eina_Bool is_first_notification = EINA_FALSE;
93
94         notification = notification_load(CALENDAR_PACKAGE, notification_id);
95         if (!notification) {
96                 notification = notification_create(NOTIFICATION_TYPE_NOTI);
97
98                 if(!notification){
99                         free(title);
100                         c_retm_if(!notification, "notification_create() is failed.");
101                 }
102
103                 is_first_notification = EINA_TRUE;
104         }
105
106         if (is_first_notification) {
107                 error = notification_set_layout(notification, NOTIFICATION_LY_NOTI_EVENT_MULTIPLE);
108                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_layout() is failed(%x)", error);
109
110                 error = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_TITLE, S_("IDS_COM_BODY_S_PLANNER"), "IDS_COM_BODY_S_PLANNER", NOTIFICATION_VARIABLE_TYPE_NONE);
111                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_text() is failed(%x)", error);
112
113                 error = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_CONTENT, S_("IDS_COM_POP_MISSED_EVENT"), "IDS_COM_POP_MISSED_EVENT", NOTIFICATION_VARIABLE_TYPE_NONE);
114                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_text() is failed(%x)", error);
115
116                 error = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_INFO_1, title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
117                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_text() is failed(%x)", error);
118
119                 time_t current = time(NULL);
120
121                 error = notification_set_time_to_text(notification, NOTIFICATION_TEXT_TYPE_INFO_SUB_1, current);
122                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_time_to_text() is failed(%x)", error);
123         } else {
124
125                 int notification_count = 0;
126
127                 error = notification_get_count(NOTIFICATION_TYPE_NOTI, CALENDAR_PACKAGE, NOTIFICATION_GROUP_ID_NONE, notification_id, &notification_count);
128                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_get_count() is failed(%x)", error);
129
130                 if (notification_count == 1) {
131
132                         error = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_INFO_2, title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
133                         c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_text() is failed(%x)", error);
134
135                         time_t current = time(NULL);
136
137                         error = notification_set_time_to_text(notification, NOTIFICATION_TEXT_TYPE_INFO_SUB_2, current);
138                         c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_time_to_text() is failed(%x)", error);
139                 }
140
141                 char text[128] = {0};
142
143                 // TODO:i18n
144                 snprintf(text, sizeof(text), "%d %s", notification_count + 1, S_("IDS_COM_POP_MISSED_EVENTS"));
145
146                 error = notification_set_text(notification, NOTIFICATION_TEXT_TYPE_CONTENT, text, "IDS_COM_POP_MISSED_EVENTS", NOTIFICATION_VARIABLE_TYPE_NONE);
147                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_text() is failed(%x)", error);
148         }
149
150         error = notification_set_time(notification, mktime(&tm));
151         c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_time() is failed(%x)", error);
152
153         bundle *b = NULL;
154
155         b = bundle_create();
156         if (!b) {
157                 ERR("bundle_create() is failed");
158
159                 notification_free(notification);
160                 free(title);
161                 return;
162         }
163
164         ret = appsvc_set_operation(b, APPSVC_OPERATION_VIEW);
165         c_warn_if(ret, "appsvc_set_operation() is failed");
166
167         ret = appsvc_set_appid(b, CALENDAR_PACKAGE);
168         c_warn_if(ret, "appsvc_set_appid() is failed");
169
170         char item_id[8] = {0};
171
172         snprintf(item_id, sizeof(item_id), "%d", _calendar_get_record_index(record));
173
174         ret = bundle_add(b, CAL_APPSVC_PARAM_INDEX, item_id);
175         c_warn_if(ret, "bundle_add() is failed");
176
177         _calendar_record_type type = _calendar_get_record_type(record);
178
179         if (type == _CALENDAR_RECORD_TYPE_EVENT) {
180                 ret = bundle_add(b, CAL_APPSVC_PARAM_TYPE, "event");
181         } else if (type == _CALENDAR_RECORD_TYPE_TODO) {
182                 ret = bundle_add(b, CAL_APPSVC_PARAM_TYPE, "todo");
183         }
184
185         c_warn_if(ret, "bundle_add() is failed");
186
187         error = notification_set_execute_option(notification, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, b);
188         c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_execute_option() is failed(%x)", error);
189
190         error = notification_set_execute_option(notification, NOTIFICATION_EXECUTE_TYPE_MULTI_LAUNCH, NULL, NULL, b);
191         c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_execute_option() is failed(%x)", error);
192
193         error = notification_set_pkgname(notification, CALENDAR_PACKAGE);
194         c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_set_pkgname() is failed(%x)", error);
195
196         if (is_first_notification) {
197
198                 error = notification_insert(notification, &notification_id);
199                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_insert() is failed(%x)", error);
200
201                 ret = vconf_set_int(CAL_VCONFKEY_NOTIFICATION_ID, notification_id);
202                 c_warn_if(ret != 0,"vconf_set_int(CAL_VCONFKEY_NOTIFICATION_ID, %d) is failed(%d)", notification_id, ret);
203         } else {
204                 error = notification_update(notification);
205                 c_warn_if(error != NOTIFICATION_ERROR_NONE, "notification_update() is failed(%x)", error);
206         }
207
208         free(title);
209         notification_free(notification);
210         bundle_free(b);
211 }
212
213 static void __cal_noti_handler_add_noti_for_reminder(struct appdata* ad, calendar_record_h calendar_alarm)
214 {
215         c_ret_if(!ad);
216         c_ret_if(!calendar_alarm);
217
218         calendar_record_h record = __cal_noti_handler_get_record_from_calendar_alarm(calendar_alarm);
219         c_ret_if(!record);
220
221         __cal_noti_handler_add_noti(ad, record);
222
223         calendar_error_e error = CALENDAR_ERROR_NONE;
224
225         error = calendar_record_destroy(record, true);
226         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)");
227 }
228
229 int cal_noti_handler_show_for_reminder(int record_index, void *data)
230 {
231         CAL_FN_START;
232
233         c_retv_if(!data, -1);
234
235         struct appdata *ad = data;
236
237         calendar_error_e error = CALENDAR_ERROR_NONE;
238
239         calendar_query_h query = NULL;
240
241         error = calendar_query_create(_calendar_alarm._uri, &query);
242         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
243
244         calendar_filter_h filter = NULL;
245
246         error = calendar_filter_create(_calendar_alarm._uri, &filter);
247         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
248
249         error = calendar_filter_add_int(filter, _calendar_alarm.alarm_id, CALENDAR_MATCH_EQUAL, (int)record_index);
250         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
251
252         error = calendar_query_set_filter(query, filter);
253         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_add_filter() is failed(%x)", error);
254
255         calendar_list_h list = NULL;
256
257         error = calendar_db_get_records_with_query(query, 0, 0, &list);
258         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
259
260         int count = 0;
261
262         int i = 0;
263
264         error = calendar_list_get_count(list, &count);
265         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
266
267         Eina_Bool is_driving_mode = EINA_FALSE;
268         Eina_Bool is_driving_mode_schedule = EINA_FALSE;
269
270         int ret = vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_DRIVINGMODE, (int *)&is_driving_mode);
271         c_warn_if(ret != 0, "vconf_get_int(VCONFKEY_SETAPPL_DRIVINGMODE_DRIVINGMODE) is failed(%d)", ret);
272
273         if (is_driving_mode) {
274                 ret = vconf_get_bool(VCONFKEY_SETAPPL_DRIVINGMODE_SCHEDULE, (int *)&is_driving_mode_schedule);
275                 c_warn_if(ret != 0, "vconf_get_int(VCONFKEY_SETAPPL_DRIVINGMODE_SCHEDULE) is failed(%d)", ret);
276         }
277
278         error = calendar_list_first(list);
279         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
280
281         for (i = 0; i < count; i++) {
282                 calendar_record_h calendar_alarm = NULL;
283
284                 error = calendar_list_get_current_record_p(list, &calendar_alarm);
285                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
286
287                 __cal_noti_handler_add_noti_for_reminder(ad, calendar_alarm);
288
289                 calendar_list_next(list);
290         }
291
292         error = calendar_list_destroy(list, true);
293         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
294
295         error = calendar_filter_destroy(filter);
296         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
297
298         error = calendar_query_destroy(query);
299         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
300
301         return 0;
302 }
303
304 int cal_noti_handler_show_for_location_reminder(int record_index, void *data)
305 {
306         CAL_FN_START;
307
308         c_retv_if(!data, -1);
309
310         struct appdata *ad = data;
311
312         calendar_record_h record = _calendar_get_record_with_index(record_index);
313
314         __cal_noti_handler_add_noti(ad, record);
315         
316         return 0;
317
318 }