tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Task / TaskWrapper.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file        TaskWrapper.cpp
18  * @author      Shi Hezhang (hezhang.shi@samsung.com)
19  * @author      Pan Rui (r.pan@samsung.com)
20  * @version     0.1
21  */
22
23 #include <pcrecpp.h>
24 #include <dpl/log/log.h>
25 #include <Commons/Exception.h>
26 #include <string.h>
27 #include "TaskWrapper.h"
28 #include "Task.h"
29
30 #define LOCAL_DEBUG_MODE 1
31
32 namespace WrtDeviceApis {
33 namespace Task {
34
35 using namespace Api;
36
37 TaskWrapper::TaskWrapper() :
38     m_platformTask(NULL),
39     m_abstractTask(NULL)
40 {
41     LogDebug("entered");
42     m_abstractTask = CalendarTaskPtr(new CalendarTask());
43     if (!m_abstractTask) {
44         ThrowMsg(Commons::UnknownException, "abstract object is not created");
45     }
46 }
47
48 TaskWrapper::TaskWrapper(const CalendarTaskPtr &task) :
49     m_platformTask(NULL),
50     m_abstractTask(task)
51 {
52     LogDebug("entered");
53 }
54
55 TaskWrapper::~TaskWrapper()
56 {
57     LogDebug("entered");
58     freePlatformTask();
59 }
60
61 int TaskWrapper::getIDFromPlatformTask() const
62 {
63     LogDebug("Entered");
64     if (m_platformTask == NULL) {
65         ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
66     }
67
68     return calendar_svc_struct_get_int(m_platformTask, CAL_VALUE_INT_INDEX);
69 }
70
71 int TaskWrapper::getStatusFromPlatformTask() const
72 {
73     LogDebug("Entered");
74     if (m_platformTask == NULL) {
75         ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
76     }
77
78     return calendar_svc_struct_get_int(m_platformTask,
79                                        CAL_VALUE_INT_TASK_STATUS);
80 }
81
82 int TaskWrapper::getPriorityFromPlatformTask() const
83 {
84     LogDebug("Entered");
85     if (m_platformTask == NULL) {
86         ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
87     }
88
89     return calendar_svc_struct_get_int(m_platformTask, CAL_VALUE_INT_PRIORITY);
90 }
91 void TaskWrapper::saveTask()
92 {
93     LogDebug("entered");
94     //Check if platform struct is set.
95     //It could be set here, but forcing user to do it manually can help to avoid problems.
96     if (m_platformTask == NULL) {
97         ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
98     }
99
100     int taskID = getIDFromPlatformTask();
101     LogDebug("taskID: " << taskID);
102     bool newEvent = (taskID == CalendarTask::UNDEFINED_TASK_ID);
103
104     displayPlatformTask();
105
106     //insert new record or update existing one
107     if (newEvent) { //insert new task
108         int returnValue = calendar_svc_insert(m_platformTask);
109         if (CAL_SUCCESS > returnValue) {
110             LogError("Can't insert new task, error code: " << returnValue);
111             ThrowMsg(Commons::PlatformException, "Can't insert new task.");
112         }
113         m_abstractTask->setId(returnValue);
114         LogInfo("New calendar task inserted");
115     } else { //update
116         if (CAL_SUCCESS != calendar_svc_update(m_platformTask)) {
117             ThrowMsg(Commons::PlatformException, "Can't update new task.");
118         }
119         LogDebug("Calendar task updated");
120     }
121 }
122
123 void TaskWrapper::loadTask(int id)
124 {
125     LogDebug("Entered. ID of task to load: " << id);
126     freePlatformTask();
127     int errorCode = calendar_svc_get(CAL_STRUCT_TODO, id, NULL, &m_platformTask);
128     if (CAL_SUCCESS > errorCode) {
129         LogError(
130             "Can't get task with ID = " << id << ", error code: " <<
131             errorCode);
132         ThrowMsg(
133             Commons::PlatformException,
134             "Can't get task with ID = " << id << ", error code: " <<
135             errorCode);
136     }
137     convertPlatformTaskToAbstractTask();
138     displayAbstractTask();
139 }
140
141 bool TaskWrapper::matchFilters(TaskFilterPtr filter)
142 {
143     LogDebug("Entered");
144     if (!filter) {
145         return true;
146     }
147     if (!m_abstractTask) {
148         ThrowMsg(Commons::NullPointerException,
149                  "abstractEvent object is not set");
150     }
151
152     LogDebug("---new task to match---");
153     m_abstractTask->display();
154
155     if (filter->getIdIsSet()) {
156         if (m_abstractTask->getId() != filter->getIdFilter()) {
157 #ifdef LOCAL_DEBUG_MODE
158             LogDebug("rejected by id " << filter->getIdFilter());
159 #endif
160             return false;
161         }
162     }
163     if (filter->getTaskIdIsSet()) {
164         if (m_abstractTask->getTaskId() != filter->getTaskIdFilter()) {
165 #ifdef LOCAL_DEBUG_MODE
166             LogDebug("rejected by calendar id " << filter->getTaskIdFilter());
167 #endif
168             return false;
169         }
170     }
171     if (filter->getDescriptionIsSet()) {
172         if (!pcrecpp::RE(filter->getDescriptionFilter()).FullMatch(
173                 m_abstractTask->getDescription())) {
174 #ifdef LOCAL_DEBUG_MODE
175             LogDebug("rejected by description " << filter->getDescriptionFilter());
176 #endif
177             return false;
178         }
179     }
180     if (filter->getSubjectIsSet()) {
181         if (!pcrecpp::RE(filter->getSubjectFilter()).FullMatch(m_abstractTask->
182                                                                    getSubject()))
183         {
184 #ifdef LOCAL_DEBUG_MODE
185             LogDebug("rejected by subject " << filter->getSubjectFilter());
186 #endif
187             return false;
188         }
189     }
190     if (filter->getDueDateIsSet()) {
191         if ((m_abstractTask->getDueDateIsSet() == false) ||
192             (filter->getDueDateMinFilter() != -1 &&
193              m_abstractTask->getDueDate() < filter->getDueDateMinFilter()) ||
194             (filter->getDueDateMaxFilter() != -1 &&
195              m_abstractTask->getDueDate() > filter->getDueDateMaxFilter())) {
196 #ifdef LOCAL_DEBUG_MODE
197             LogDebug(
198                 "rejected by start time min " << filter->getDueDateMinFilter());
199             LogDebug(
200                 "rejected by start time max " << filter->getDueDateMaxFilter());
201             LogDebug("due date " << m_abstractTask->getDueDate());
202 #endif
203             return false;
204         }
205     }
206
207     if (filter->getStatusIsSet()) {
208         StatusList statusList = *(filter->getStatusFilter());
209         StatusList::const_iterator it = statusList.begin(), end =
210             statusList.end();
211         for (; it != end; ++it) {
212             if (m_abstractTask->getStatus() == *it) {
213                 break;
214             }
215         }
216         if (it == end) {
217 #ifdef LOCAL_DEBUG_MODE
218             LogDebug("rejected by status ");
219 #endif
220             return false;
221         }
222     }
223
224     if (filter->getPriorityIsSet()) {
225         PriorityList priorityList = *(filter->getPriorityFilter());
226         PriorityList::const_iterator it = priorityList.begin(), end =
227             priorityList.end();
228         for (; it != end; ++it) {
229             if (m_abstractTask->getPriority() == *it) {
230                 break;
231             }
232         }
233         if (it == end) {
234 #ifdef LOCAL_DEBUG_MODE
235             LogDebug("rejected by priority ");
236 #endif
237             return false;
238         }
239     }
240     /*
241             if (filter->getPriorityIsSet()) {
242                     if (m_abstractTask->getPriority() != filter->getPriorityFilter()) {
243        #ifdef LOCAL_DEBUG_MODE
244                             LogDebug("rejected by priority " << filter->getPriorityFilter() );
245        #endif
246                             return false;
247                     }
248             }
249      */
250     return true;
251 }
252
253 void TaskWrapper::deleteTask()
254 {
255     LogDebug("entered");
256     if (m_platformTask == NULL) {
257         ThrowMsg(Commons::NullPointerException,
258                  "Failed to delete task in calendar (m_platformTask==NULL)");
259     }
260     int taskID = getIDFromPlatformTask();
261     LogDebug("taskID: " << taskID);
262     if (taskID == CalendarTask::UNDEFINED_TASK_ID) {
263         ThrowMsg(
264             Commons::InvalidArgumentException,
265             "Failed to delete task in calendar (task is not saved in calendar)");
266     }
267     if (CAL_SUCCESS != calendar_svc_delete(CAL_STRUCT_TODO, taskID)) {
268         ThrowMsg(Commons::PlatformException, "Can't delete calendar task.");
269     }
270
271     m_abstractTask->setId(CalendarTask::UNDEFINED_TASK_ID);
272     setIDToPlatformTask();
273 }
274
275 cal_struct *TaskWrapper::getPlatformTask() const
276 {
277     return m_platformTask;
278 }
279
280 CalendarTaskPtr TaskWrapper::getAbstractTask() const
281 {
282     return m_abstractTask;
283 }
284
285 void TaskWrapper::freePlatformTask()
286 {
287     LogDebug("entered");
288     if (m_platformTask != NULL) {
289         if (CAL_SUCCESS != calendar_svc_struct_free(&m_platformTask)) {
290             LogError("Can't free calendar task struct.");
291         }
292         m_platformTask = NULL;
293     }
294 }
295
296 cal_struct *TaskWrapper::convertAbstractTaskToPlatformTask()
297 {
298     LogDebug("entered");
299     freePlatformTask();
300     m_platformTask = calendar_svc_struct_new(CAL_STRUCT_TODO);
301     if (!m_platformTask) {
302         ThrowMsg(Commons::UnknownException, "cannot create platform task");
303     }
304     setDescriptionToPlatformTask();
305     setSubjectToPlatformTask();
306     setDueDateToPlatformTask();
307     setStatusToPlatformTask();
308     setPriorityToPlatformTask();
309     setIDToPlatformTask();
310     return getPlatformTask();
311 }
312
313 void TaskWrapper::setDescriptionToPlatformTask()
314 {
315     if (!m_platformTask) {
316         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
317     }
318     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformTask,
319                                                    CAL_VALUE_TXT_DESCRIPTION,
320                                                    m_abstractTask->
321                                                        getDescription().c_str()))
322     {
323         ThrowMsg(Commons::PlatformException, "Can't set task description.");
324     }
325 }
326
327 void TaskWrapper::setSubjectToPlatformTask()
328 {
329     if (!m_platformTask) {
330         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
331     }
332     if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformTask,
333                                                    CAL_VALUE_TXT_SUMMARY,
334                                                    m_abstractTask->getSubject()
335                                                        .c_str())) {
336         ThrowMsg(Commons::PlatformException, "Can't set task subject.");
337     }
338 }
339
340 void TaskWrapper::setDueDateToPlatformTask()
341 {
342     if (!m_platformTask) {
343         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
344     }
345
346     if (m_abstractTask->getDueDateIsSet() == true) {
347         time_t time = m_abstractTask->getDueDate();
348
349         if (CAL_SUCCESS != calendar_svc_struct_set_time(m_platformTask,
350                                                         CAL_VALUE_GMT_END_DATE_TIME,
351                                                         CAL_TZ_FLAG_GMT,
352                                                         time)) {
353             ThrowMsg(Commons::PlatformException, "Can't set task due time.");
354         }
355         if (CAL_SUCCESS != calendar_svc_struct_set_time(m_platformTask,
356                                                         CAL_VALUE_GMT_START_DATE_TIME,
357                                                         CAL_TZ_FLAG_GMT,
358                                                         time)) {
359             ThrowMsg(Commons::PlatformException, "Can't set task due time.");
360         }
361     }
362 }
363
364 void TaskWrapper::setStatusToPlatformTask()
365 {
366     if (!m_platformTask) {
367         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
368     }
369
370     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformTask,
371                                                    CAL_VALUE_INT_TASK_STATUS,
372                                                    m_abstractTask->getStatus()))
373     {
374         ThrowMsg(Commons::PlatformException, "Can't set task Status.");
375     }
376 }
377 void TaskWrapper::setPriorityToPlatformTask()
378 {
379     if (!m_platformTask) {
380         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
381     }
382
383     if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformTask,
384                                                    CAL_VALUE_INT_PRIORITY,
385                                                    m_abstractTask->getPriority()))
386     {
387         ThrowMsg(Commons::PlatformException, "Can't set task Priority.");
388     }
389 }
390
391 void TaskWrapper::setIDToPlatformTask()
392 {
393     if (m_abstractTask->getIdIsSet()) {
394         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformTask,
395                                                        CAL_VALUE_INT_INDEX,
396                                                        m_abstractTask->getId()))
397         {
398             ThrowMsg(Commons::PlatformException, "Can't set task ID.");
399         }
400     }
401 }
402
403 CalendarTaskPtr TaskWrapper::convertPlatformTaskToAbstractTask()
404 {
405     LogDebug("entered");
406     setDescriptionFromPlatformTask();
407     setSubjectFromPlatformTask();
408     setDueDateFromPlatformTask();
409     setStatusFromPlatformTask();
410     setPriorityFromPlatformTask();
411     setIDFromPlatformTask();
412     return getAbstractTask();
413 }
414
415 void TaskWrapper::setDescriptionFromPlatformTask()
416 {
417     if (!m_platformTask) {
418         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
419     }
420     const char *description = calendar_svc_struct_get_str(
421             m_platformTask,
422             CAL_VALUE_TXT_DESCRIPTION);
423     if (description) {
424         m_abstractTask->setDescription(description);
425     }
426 }
427
428 void TaskWrapper::setSubjectFromPlatformTask()
429 {
430     if (!m_platformTask) {
431         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
432     }
433     const char *summary = calendar_svc_struct_get_str(m_platformTask,
434                                                       CAL_VALUE_TXT_SUMMARY);
435     if (summary) {
436         m_abstractTask->setSubject(summary);
437     }
438 }
439
440 void TaskWrapper::setDueDateFromPlatformTask()
441 {
442     if (!m_platformTask) {
443         ThrowMsg(Commons::UnknownException, "Null platform pointer.");
444     }
445     time_t dueDate = calendar_svc_struct_get_time(m_platformTask,
446                                                   CAL_VALUE_GMT_END_DATE_TIME,
447                                                   CAL_TZ_FLAG_GMT);
448
449     struct tm *tminfo = localtime(&dueDate);
450     if (tminfo->tm_year != PLATFORM_UNSUPPORTED_DATE_YEAR) {
451         m_abstractTask->setDueDate(dueDate);
452     }
453 }
454
455 void TaskWrapper::setStatusFromPlatformTask()
456 {
457     m_abstractTask->setStatus(
458         (CalendarTask::TaskStatus)getStatusFromPlatformTask());
459 }
460 void TaskWrapper::setPriorityFromPlatformTask()
461 {
462     m_abstractTask->setPriority(
463         (CalendarTask::TaskPriority)getPriorityFromPlatformTask());
464 }
465
466 void TaskWrapper::setIDFromPlatformTask()
467 {
468     m_abstractTask->setId(getIDFromPlatformTask());
469 }
470
471 void TaskWrapper::displayAbstractTask()
472 {
473     m_abstractTask->display();
474 }
475
476 void TaskWrapper::displayPlatformTask()
477 {
478     LogDebug("task id: " <<
479              calendar_svc_struct_get_int(m_platformTask, CAL_VALUE_INT_INDEX));
480     LogDebug("task due time: " <<
481              calendar_svc_struct_get_time(m_platformTask,
482                                           CAL_VALUE_GMT_END_DATE_TIME,
483                                           CAL_TZ_FLAG_GMT));
484     LogDebug("task summary: " <<
485              calendar_svc_struct_get_str(m_platformTask, CAL_VALUE_TXT_SUMMARY));
486     LogDebug("task description: " <<
487              calendar_svc_struct_get_str(m_platformTask,
488                                          CAL_VALUE_TXT_DESCRIPTION));
489     LogDebug("task priority: " <<
490              calendar_svc_struct_get_str(m_platformTask, CAL_VALUE_INT_PRIORITY));
491     LogDebug("task status: " <<
492              calendar_svc_struct_get_str(m_platformTask,
493                                          CAL_VALUE_INT_TASK_STATUS));
494 }
495 }
496 }