tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Task / TaskManager.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        TaskManager.cpp
18  * @author      Lukasz Marek (l.marek@samsung.com)
19  * @author      Wojciech Bielawski (w.bielawski@samsung.com)
20  * @version     0.1
21  */
22
23 #include "TaskManager.h"
24
25 #include <algorithm>
26 #include <dpl/log/log.h>
27 #include <calendar-svc-provider.h>
28 #include <calendar-svc-errors.h>
29
30 namespace {
31 const int ID_ALL_ACCOUNTS = 0;
32 const int TASK_ID = 1;
33 const char* SERVICE_PROVIDER_NAME = "phone";
34 const char* TASK_NAME = "test";
35 }
36
37 namespace WrtDeviceApis {
38 namespace Task {
39
40 using namespace Api;
41
42 int TaskManager::m_instanceCount = 0;
43
44 TaskManager::TaskManager()
45 {
46     LogDebug("entered");
47     /* wac  platform doesn't support calendar manager.
48      * This implementation have to implement it on its own.
49      * One calendar have to be created and stored for further use.
50      */
51     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
52     if (m_instanceCount == 0) {
53         LogDebug("opening calendar DB");
54         if (CAL_SUCCESS != calendar_svc_connect()) {
55             ThrowMsg(Commons::PlatformException,
56                      "Calendar DB initialization failed");
57         }
58     }
59     m_instanceCount++;
60 }
61
62 TaskManager::~TaskManager()
63 {
64     LogDebug("entered");
65     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
66     m_instanceCount--;
67     if (m_instanceCount == 0) {
68         LogDebug("closing calendar DB");
69         if (CAL_SUCCESS != calendar_svc_close()) {
70             LogError("Calendar database not clearly closed.");
71         }
72     }
73 }
74
75 int TaskManager::getAccountId()
76 {
77     return ID_ALL_ACCOUNTS;
78 }
79
80 const char *TaskManager::getServiceProviderName()
81 {
82     return SERVICE_PROVIDER_NAME;
83 }
84
85 std::list<ITaskPtr> TaskManager::getTasksInternal()
86 {
87     LogDebug("entered");
88     std::list<ITaskPtr> result;
89     ITaskPtr defaultTask(new Task());
90     defaultTask->setName(TASK_NAME);
91     defaultTask->setId(TASK_ID);
92     defaultTask->setAccountId(ID_ALL_ACCOUNTS);
93     defaultTask->setType(Task::DEVICE_TASK);
94     result.push_back(defaultTask);
95     return result;
96 }
97
98 void TaskManager::OnRequestReceived(const IEventGetTasksPtr &task)
99 {
100     Try
101     {
102         const std::list<ITaskPtr> &result = getTasksInternal();
103         task->tryCancelled();
104         task->setResult(true);
105         std::list<ITaskPtr>::const_iterator it = result.begin(), end =
106             result.end();
107         for (; it != end; ++it) {
108             task->addTask(*it);
109         }
110     }
111     Catch(Commons::EventCancelledException)
112     {
113         //revert changes if any
114     }
115     task->setCancelAllowed(true);
116 }
117 }
118 }