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.
18 * @author Shi Hezhang (hezhang.shi@samsung.com)
19 * @author Pan Rui (r.pan@samsung.com)
24 #include <dpl/scoped_ptr.h>
25 #include <dpl/log/log.h>
26 #include <calendar-svc-provider.h>
27 #include <calendar-svc-errors.h>
28 #include "TaskManager.h"
30 namespace WrtDeviceApis {
45 void Task::OnRequestReceived(const IEventAddTaskPtr &task)
50 if (!task->getTask()) {
51 ThrowMsg(Commons::NullPointerException, "task parameter is NULL");
53 if (task->getTask()->getId() != CalendarTask::UNDEFINED_TASK_ID) {
54 LogWarning("adding task that is already added");
55 task->getTask()->setId(CalendarTask::UNDEFINED_TASK_ID);
57 TaskWrapperPtr taskWrapper(new TaskWrapper(task->getTask()));
58 taskWrapper->convertAbstractTaskToPlatformTask();
59 if (task->checkCancelled()) {
60 task->setCancelAllowed(true);
61 task->setResult(true);
64 taskWrapper->saveTask();
65 task->setResult(true);
67 catch (const Commons::Exception &ex)
69 LogError("Error during adding task" << ex.DumpToString());
70 task->setResult(false);
72 task->setCancelAllowed(false);
75 void Task::OnRequestReceived(const IEventDeleteTaskPtr &task)
80 if (!task->getTask()) {
81 ThrowMsg(Commons::NullPointerException, "task parameter is NULL");
83 if (task->getTask()->getId() == CalendarTask::UNDEFINED_TASK_ID) {
84 ThrowMsg(Commons::InvalidArgumentException,
85 "Cannot delete non-existing task.");
87 DPL::ScopedPtr<TaskWrapper> taskWrapper(new TaskWrapper(task->getTask()));
88 taskWrapper->convertAbstractTaskToPlatformTask();
89 if (task->checkCancelled()) {
90 task->setCancelAllowed(true);
91 task->setResult(true);
94 taskWrapper->deleteTask();
95 task->setResult(true);
97 catch (const Commons::Exception &ex)
99 LogError("Error during deleting task " << ex.DumpToString());
100 task->setResult(false);
102 task->setCancelAllowed(false);
105 void Task::OnRequestReceived(const IEventUpdateTaskPtr &task)
110 if (!task->getTask()) {
111 ThrowMsg(Commons::NullPointerException, "task parameter is NULL");
113 if (task->getTask()->getId() == CalendarTask::UNDEFINED_TASK_ID) {
115 Commons::InvalidArgumentException,
116 "Cannot update non-existing task. Task needs adding or ID is wrong");
118 TaskWrapperPtr taskWrapper(new TaskWrapper(task->getTask()));
119 taskWrapper->convertAbstractTaskToPlatformTask();
120 if (task->checkCancelled()) {
121 task->setCancelAllowed(true);
122 task->setResult(true);
125 taskWrapper->saveTask();
126 task->setResult(true);
128 catch (const Commons::Exception &ex)
130 LogError("Error during updating task " << ex.DumpToString());
131 task->setResult(false);
133 task->setCancelAllowed(false);
136 void Task::OnRequestReceived(const IEventFindTasksPtr &task)
139 const TaskFilterPtr &filter = task->getFilter();
140 cal_struct *platformTask = NULL;
141 cal_iter *iter = NULL;
143 task->setResult(true);
147 calendar_svc_get_all(0, 0, CAL_STRUCT_TODO, &iter)) {
148 ThrowMsg(Commons::PlatformException, "Can't get all records");
150 //TODO: currently platform starts iteration with below function
151 // It's possible that current approach will change to be familiar with std iterators
153 while (CAL_SUCCESS == calendar_svc_iter_next(iter)) {
154 task->tryCancelled();
156 calendar_svc_iter_get_info(iter, &platformTask)) {
157 ThrowMsg(Commons::PlatformException, "Can't get task info.");
160 int eventId = calendar_svc_struct_get_int(platformTask,
161 CAL_VALUE_INT_INDEX);
162 if (CAL_SUCCESS != calendar_svc_struct_free(&platformTask)) {
163 LogError("Can't free calendar task struct.");
166 TaskWrapperPtr taskWrapper(new TaskWrapper());
167 taskWrapper->loadTask(eventId);
168 if (!filter || taskWrapper->matchFilters(filter)) {
169 if (foundCnt >= task->getFirstTask() &&
170 (task->getLastTask() == -1 || foundCnt <=
171 task->getLastTask())) {
172 task->addTask(taskWrapper->getAbstractTask());
178 catch (const Commons::Exception &ex)
180 LogError("Exception: " << ex.DumpToString());
181 task->setResult(false);
183 //According to example in calendar-svc-provider.h it's not needed to pass here
184 //iter set on first element
185 calendar_svc_iter_remove(&iter);
186 task->setCancelAllowed(true);
189 void Task::OnRequestReceived(const IEventCreateTaskPtr &task)
194 CalendarTaskPtr result(new CalendarTask());
195 task->setResult(result.Get() != NULL);
196 task->setTask(result);
198 catch (const Commons::Exception &ex)
200 LogError("Error during creating an task " << ex.DumpToString());
201 task->setResult(false);