2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file TaskWrapper.cpp
18 * @author Shi Hezhang (hezhang.shi@samsung.com)
19 * @author Pan Rui (r.pan@samsung.com)
24 #include <dpl/log/log.h>
25 #include <Commons/Exception.h>
27 #include "TaskWrapper.h"
30 #define LOCAL_DEBUG_MODE 1
32 namespace WrtDeviceApis {
37 TaskWrapper::TaskWrapper() :
42 m_abstractTask = CalendarTaskPtr(new CalendarTask());
43 if (!m_abstractTask) {
44 ThrowMsg(Commons::UnknownException, "abstract object is not created");
48 TaskWrapper::TaskWrapper(const CalendarTaskPtr &task) :
55 TaskWrapper::~TaskWrapper()
61 int TaskWrapper::getIDFromPlatformTask() const
64 if (m_platformTask == NULL) {
65 ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
68 return calendar_svc_struct_get_int(m_platformTask, CAL_VALUE_INT_INDEX);
71 int TaskWrapper::getStatusFromPlatformTask() const
74 if (m_platformTask == NULL) {
75 ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
78 return calendar_svc_struct_get_int(m_platformTask,
79 CAL_VALUE_INT_TASK_STATUS);
82 int TaskWrapper::getPriorityFromPlatformTask() const
85 if (m_platformTask == NULL) {
86 ThrowMsg(Commons::NullPointerException, "m_platformTask is not set");
89 return calendar_svc_struct_get_int(m_platformTask, CAL_VALUE_INT_PRIORITY);
91 void TaskWrapper::saveTask()
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");
100 int taskID = getIDFromPlatformTask();
101 LogDebug("taskID: " << taskID);
102 bool newEvent = (taskID == CalendarTask::UNDEFINED_TASK_ID);
104 displayPlatformTask();
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.");
113 m_abstractTask->setId(returnValue);
114 LogInfo("New calendar task inserted");
116 if (CAL_SUCCESS != calendar_svc_update(m_platformTask)) {
117 ThrowMsg(Commons::PlatformException, "Can't update new task.");
119 LogDebug("Calendar task updated");
123 void TaskWrapper::loadTask(int id)
125 LogDebug("Entered. ID of task to load: " << id);
127 int errorCode = calendar_svc_get(CAL_STRUCT_TODO, id, NULL, &m_platformTask);
128 if (CAL_SUCCESS > errorCode) {
130 "Can't get task with ID = " << id << ", error code: " <<
133 Commons::PlatformException,
134 "Can't get task with ID = " << id << ", error code: " <<
137 convertPlatformTaskToAbstractTask();
138 displayAbstractTask();
141 bool TaskWrapper::matchFilters(TaskFilterPtr filter)
147 if (!m_abstractTask) {
148 ThrowMsg(Commons::NullPointerException,
149 "abstractEvent object is not set");
152 LogDebug("---new task to match---");
153 m_abstractTask->display();
155 if (filter->getIdIsSet()) {
156 if (m_abstractTask->getId() != filter->getIdFilter()) {
157 #ifdef LOCAL_DEBUG_MODE
158 LogDebug("rejected by id " << filter->getIdFilter());
163 if (filter->getTaskIdIsSet()) {
164 if (m_abstractTask->getTaskId() != filter->getTaskIdFilter()) {
165 #ifdef LOCAL_DEBUG_MODE
166 LogDebug("rejected by calendar id " << filter->getTaskIdFilter());
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());
180 if (filter->getSubjectIsSet()) {
181 if (!pcrecpp::RE(filter->getSubjectFilter()).FullMatch(m_abstractTask->
184 #ifdef LOCAL_DEBUG_MODE
185 LogDebug("rejected by subject " << filter->getSubjectFilter());
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
198 "rejected by start time min " << filter->getDueDateMinFilter());
200 "rejected by start time max " << filter->getDueDateMaxFilter());
201 LogDebug("due date " << m_abstractTask->getDueDate());
207 if (filter->getStatusIsSet()) {
208 StatusList statusList = *(filter->getStatusFilter());
209 StatusList::const_iterator it = statusList.begin(), end =
211 for (; it != end; ++it) {
212 if (m_abstractTask->getStatus() == *it) {
217 #ifdef LOCAL_DEBUG_MODE
218 LogDebug("rejected by status ");
224 if (filter->getPriorityIsSet()) {
225 PriorityList priorityList = *(filter->getPriorityFilter());
226 PriorityList::const_iterator it = priorityList.begin(), end =
228 for (; it != end; ++it) {
229 if (m_abstractTask->getPriority() == *it) {
234 #ifdef LOCAL_DEBUG_MODE
235 LogDebug("rejected by priority ");
241 if (filter->getPriorityIsSet()) {
242 if (m_abstractTask->getPriority() != filter->getPriorityFilter()) {
243 #ifdef LOCAL_DEBUG_MODE
244 LogDebug("rejected by priority " << filter->getPriorityFilter() );
253 void TaskWrapper::deleteTask()
256 if (m_platformTask == NULL) {
257 ThrowMsg(Commons::NullPointerException,
258 "Failed to delete task in calendar (m_platformTask==NULL)");
260 int taskID = getIDFromPlatformTask();
261 LogDebug("taskID: " << taskID);
262 if (taskID == CalendarTask::UNDEFINED_TASK_ID) {
264 Commons::InvalidArgumentException,
265 "Failed to delete task in calendar (task is not saved in calendar)");
267 if (CAL_SUCCESS != calendar_svc_delete(CAL_STRUCT_TODO, taskID)) {
268 ThrowMsg(Commons::PlatformException, "Can't delete calendar task.");
271 m_abstractTask->setId(CalendarTask::UNDEFINED_TASK_ID);
272 setIDToPlatformTask();
275 cal_struct *TaskWrapper::getPlatformTask() const
277 return m_platformTask;
280 CalendarTaskPtr TaskWrapper::getAbstractTask() const
282 return m_abstractTask;
285 void TaskWrapper::freePlatformTask()
288 if (m_platformTask != NULL) {
289 if (CAL_SUCCESS != calendar_svc_struct_free(&m_platformTask)) {
290 LogError("Can't free calendar task struct.");
292 m_platformTask = NULL;
296 cal_struct *TaskWrapper::convertAbstractTaskToPlatformTask()
300 m_platformTask = calendar_svc_struct_new(CAL_STRUCT_TODO);
301 if (!m_platformTask) {
302 ThrowMsg(Commons::UnknownException, "cannot create platform task");
304 setDescriptionToPlatformTask();
305 setSubjectToPlatformTask();
306 setDueDateToPlatformTask();
307 setStatusToPlatformTask();
308 setPriorityToPlatformTask();
309 setIDToPlatformTask();
310 return getPlatformTask();
313 void TaskWrapper::setDescriptionToPlatformTask()
315 if (!m_platformTask) {
316 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
318 if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformTask,
319 CAL_VALUE_TXT_DESCRIPTION,
321 getDescription().c_str()))
323 ThrowMsg(Commons::PlatformException, "Can't set task description.");
327 void TaskWrapper::setSubjectToPlatformTask()
329 if (!m_platformTask) {
330 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
332 if (CAL_SUCCESS != calendar_svc_struct_set_str(m_platformTask,
333 CAL_VALUE_TXT_SUMMARY,
334 m_abstractTask->getSubject()
336 ThrowMsg(Commons::PlatformException, "Can't set task subject.");
340 void TaskWrapper::setDueDateToPlatformTask()
342 if (!m_platformTask) {
343 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
346 if (m_abstractTask->getDueDateIsSet() == true) {
347 time_t time = m_abstractTask->getDueDate();
349 if (CAL_SUCCESS != calendar_svc_struct_set_time(m_platformTask,
350 CAL_VALUE_GMT_END_DATE_TIME,
353 ThrowMsg(Commons::PlatformException, "Can't set task due time.");
355 if (CAL_SUCCESS != calendar_svc_struct_set_time(m_platformTask,
356 CAL_VALUE_GMT_START_DATE_TIME,
359 ThrowMsg(Commons::PlatformException, "Can't set task due time.");
364 void TaskWrapper::setStatusToPlatformTask()
366 if (!m_platformTask) {
367 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
370 if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformTask,
371 CAL_VALUE_INT_TASK_STATUS,
372 m_abstractTask->getStatus()))
374 ThrowMsg(Commons::PlatformException, "Can't set task Status.");
377 void TaskWrapper::setPriorityToPlatformTask()
379 if (!m_platformTask) {
380 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
383 if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformTask,
384 CAL_VALUE_INT_PRIORITY,
385 m_abstractTask->getPriority()))
387 ThrowMsg(Commons::PlatformException, "Can't set task Priority.");
391 void TaskWrapper::setIDToPlatformTask()
393 if (m_abstractTask->getIdIsSet()) {
394 if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformTask,
396 m_abstractTask->getId()))
398 ThrowMsg(Commons::PlatformException, "Can't set task ID.");
403 CalendarTaskPtr TaskWrapper::convertPlatformTaskToAbstractTask()
406 setDescriptionFromPlatformTask();
407 setSubjectFromPlatformTask();
408 setDueDateFromPlatformTask();
409 setStatusFromPlatformTask();
410 setPriorityFromPlatformTask();
411 setIDFromPlatformTask();
412 return getAbstractTask();
415 void TaskWrapper::setDescriptionFromPlatformTask()
417 if (!m_platformTask) {
418 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
420 const char *description = calendar_svc_struct_get_str(
422 CAL_VALUE_TXT_DESCRIPTION);
424 m_abstractTask->setDescription(description);
428 void TaskWrapper::setSubjectFromPlatformTask()
430 if (!m_platformTask) {
431 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
433 const char *summary = calendar_svc_struct_get_str(m_platformTask,
434 CAL_VALUE_TXT_SUMMARY);
436 m_abstractTask->setSubject(summary);
440 void TaskWrapper::setDueDateFromPlatformTask()
442 if (!m_platformTask) {
443 ThrowMsg(Commons::UnknownException, "Null platform pointer.");
445 time_t dueDate = calendar_svc_struct_get_time(m_platformTask,
446 CAL_VALUE_GMT_END_DATE_TIME,
449 struct tm *tminfo = localtime(&dueDate);
450 if (tminfo->tm_year != PLATFORM_UNSUPPORTED_DATE_YEAR) {
451 m_abstractTask->setDueDate(dueDate);
455 void TaskWrapper::setStatusFromPlatformTask()
457 m_abstractTask->setStatus(
458 (CalendarTask::TaskStatus)getStatusFromPlatformTask());
460 void TaskWrapper::setPriorityFromPlatformTask()
462 m_abstractTask->setPriority(
463 (CalendarTask::TaskPriority)getPriorityFromPlatformTask());
466 void TaskWrapper::setIDFromPlatformTask()
468 m_abstractTask->setId(getIDFromPlatformTask());
471 void TaskWrapper::displayAbstractTask()
473 m_abstractTask->display();
476 void TaskWrapper::displayPlatformTask()
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,
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));