68dd15fa3d507b1da3756baac4332b76d74c1262
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Task / Task.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        Task.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 "Task.h"
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"
29
30 namespace WrtDeviceApis {
31 namespace Task {
32
33 using namespace Api;
34
35 Task::Task()
36 {
37     LogDebug("entered");
38 }
39
40 Task::~Task()
41 {
42     LogDebug("entered");
43 }
44
45 void Task::OnRequestReceived(const IEventAddTaskPtr &task)
46 {
47     LogDebug("entered");
48     Try
49     {
50         if (!task->getTask()) {
51             ThrowMsg(Commons::NullPointerException, "task parameter is NULL");
52         }
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);
56         }
57         TaskWrapperPtr taskWrapper(new TaskWrapper(task->getTask()));
58         taskWrapper->convertAbstractTaskToPlatformTask();
59         if (task->checkCancelled()) {
60             task->setCancelAllowed(true);
61             task->setResult(true);
62             return;
63         }
64         taskWrapper->saveTask();
65         task->setResult(true);
66     }
67     catch (const Commons::Exception &ex)
68     {
69         LogError("Error during adding task" << ex.DumpToString());
70         task->setResult(false);
71     }
72     task->setCancelAllowed(false);
73 }
74
75 void Task::OnRequestReceived(const IEventDeleteTaskPtr &task)
76 {
77     LogDebug("entered");
78     Try
79     {
80         if (!task->getTask()) {
81             ThrowMsg(Commons::NullPointerException, "task parameter is NULL");
82         }
83         if (task->getTask()->getId() == CalendarTask::UNDEFINED_TASK_ID) {
84             ThrowMsg(Commons::InvalidArgumentException,
85                      "Cannot delete non-existing task.");
86         }
87         DPL::ScopedPtr<TaskWrapper> taskWrapper(new TaskWrapper(task->getTask()));
88         taskWrapper->convertAbstractTaskToPlatformTask();
89         if (task->checkCancelled()) {
90             task->setCancelAllowed(true);
91             task->setResult(true);
92             return;
93         }
94         taskWrapper->deleteTask();
95         task->setResult(true);
96     }
97     catch (const Commons::Exception &ex)
98     {
99         LogError("Error during deleting task " << ex.DumpToString());
100         task->setResult(false);
101     }
102     task->setCancelAllowed(false);
103 }
104
105 void Task::OnRequestReceived(const IEventUpdateTaskPtr &task)
106 {
107     LogDebug("entered");
108     Try
109     {
110         if (!task->getTask()) {
111             ThrowMsg(Commons::NullPointerException, "task parameter is NULL");
112         }
113         if (task->getTask()->getId() == CalendarTask::UNDEFINED_TASK_ID) {
114             ThrowMsg(
115                 Commons::InvalidArgumentException,
116                 "Cannot update non-existing task. Task needs adding or ID is wrong");
117         }
118         TaskWrapperPtr taskWrapper(new TaskWrapper(task->getTask()));
119         taskWrapper->convertAbstractTaskToPlatformTask();
120         if (task->checkCancelled()) {
121             task->setCancelAllowed(true);
122             task->setResult(true);
123             return;
124         }
125         taskWrapper->saveTask();
126         task->setResult(true);
127     }
128     catch (const Commons::Exception &ex)
129     {
130         LogError("Error during updating task " << ex.DumpToString());
131         task->setResult(false);
132     }
133     task->setCancelAllowed(false);
134 }
135
136 void Task::OnRequestReceived(const IEventFindTasksPtr &task)
137 {
138     LogDebug("entered");
139     const TaskFilterPtr &filter = task->getFilter();
140     cal_struct *platformTask = NULL;
141     cal_iter *iter = NULL;
142     int foundCnt = 0;
143     task->setResult(true);
144
145     try {
146         if (CAL_SUCCESS !=
147             calendar_svc_get_all(0, 0, CAL_STRUCT_TODO, &iter)) {
148             ThrowMsg(Commons::PlatformException, "Can't get all records");
149         }
150         //TODO: currently platform starts iteration with below function
151         // It's possible that current approach will change to be familiar with std iterators
152
153         while (CAL_SUCCESS == calendar_svc_iter_next(iter)) {
154             task->tryCancelled();
155             if (CAL_SUCCESS !=
156                 calendar_svc_iter_get_info(iter, &platformTask)) {
157                 ThrowMsg(Commons::PlatformException, "Can't get task info.");
158             }
159             //getting ID
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.");
164             }
165
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());
173                 }
174                 foundCnt++;
175             }
176         }
177     }
178     catch (const Commons::Exception &ex)
179     {
180         LogError("Exception: " << ex.DumpToString());
181         task->setResult(false);
182     }
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);
187 }
188
189 void Task::OnRequestReceived(const IEventCreateTaskPtr &task)
190 {
191     LogDebug("entered");
192     Try
193     {
194         CalendarTaskPtr result(new CalendarTask());
195         task->setResult(result.Get() != NULL);
196         task->setTask(result);
197     }
198     catch (const Commons::Exception &ex)
199     {
200         LogError("Error during creating an task " << ex.DumpToString());
201         task->setResult(false);
202     }
203 }
204 }
205 }
206