58aa4cce6fa8aa468d2eae5093d8ae18d4fc1798
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Calendar / CalendarManager.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        CalendarManager.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 "CalendarManager.h"
24 #include <algorithm>
25 #include <dpl/log/log.h>
26 #include <calendar-svc-provider.h>
27 #include <calendar-svc-errors.h>
28 #include <vector>
29 #include <Commons/StringUtils.h>
30
31 namespace {
32 const int ID_ALL_ACCOUNTS = 0;
33 const char* SERVICE_PROVIDER_NAME = "phone";
34 }
35
36 namespace WrtDeviceApis {
37 namespace Calendar {
38
39 using namespace Api;
40
41 int CalendarManager::m_instanceCount = 0;
42
43 CalendarManager::CalendarManager()
44 {
45     LogDebug("entered");
46     /* wac platform doesn't support calendar manager.
47      * This implementation have to implement it on its own.
48      * One calendar have to be created and stored for further use.
49      */
50     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
51     if (m_instanceCount == 0) {
52         LogDebug("opening calendar DB");
53         if (CAL_SUCCESS != calendar_svc_connect()) {
54             ThrowMsg(Commons::PlatformException,
55                      "Calendar DB initialization failed");
56         }
57     }
58     m_instanceCount++;
59 }
60
61 CalendarManager::~CalendarManager()
62 {
63     LogDebug("entered");
64     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
65     m_instanceCount--;
66     if (m_instanceCount == 0) {
67         LogDebug("closing calendar DB");
68         if (CAL_SUCCESS != calendar_svc_close()) {
69             LogError("Calendar database not clearly closed.");
70         }
71     }
72 }
73
74 int CalendarManager::getAccountId()
75 {
76     return ID_ALL_ACCOUNTS;
77 }
78
79 const char *CalendarManager::getServiceProviderName()
80 {
81     return SERVICE_PROVIDER_NAME;
82 }
83
84 void CalendarManager::OnRequestReceived(const EventGetCalendarsPtr &event)
85 {
86     static std::vector<ICalendarPtr> calendars;
87     Try
88     {
89         if (calendars.empty()) {
90             cal_iter *iter = NULL;
91             if (CAL_SUCCESS !=
92                 calendar_svc_get_all(getAccountId(), 0, CAL_STRUCT_CALENDAR,
93                                      &iter)) {
94                 event->setResult(false);
95                 return;
96             }
97             cal_struct *calendar = NULL;
98             while (CAL_SUCCESS == calendar_svc_iter_next(iter)) {
99                 if (event->checkCancelled()) {
100                     break;
101                 }
102                 if (CAL_SUCCESS ==
103                     calendar_svc_iter_get_info(iter, &calendar)) {
104                     const char* name = calendar_svc_struct_get_str(
105                             calendar,
106                             CAL_TABLE_TXT_NAME);
107                     const char* id = calendar_svc_struct_get_str(
108                             calendar,
109                             CAL_TABLE_TXT_CALENDAR_ID);
110                     if (name != NULL && id != NULL) {
111                         LogDebug(
112                             "got calendar, id=" << id << ", name: " << name);
113                         ICalendarPtr newCalendar(new Calendar());
114                         newCalendar->setName(name);
115                         newCalendar->setId(Commons::String::toInt(id));
116                         newCalendar->setAccountId(getAccountId());
117                         newCalendar->setType(Calendar::DEVICE_CALENDAR);
118                         calendars.push_back(newCalendar);
119                     } else {
120                         LogError("calendar contains invalid parameters");
121                     }
122                 } else {
123                     LogError("cannot get calendar");
124                 }
125             }
126             calendar_svc_iter_remove(&iter);
127         }
128         if (!event->checkCancelled()) {
129             std::vector<ICalendarPtr>::const_iterator it = calendars.begin();
130             for (; it != calendars.end(); ++it) {
131                 event->addCalendar(*it);
132             }
133             event->setResult(true);
134         }
135     }
136     Catch(Commons::Exception)
137     {
138         LogError("error occuered during obtaining data");
139         event->setResult(false);
140     }
141     event->setCancelAllowed(true);
142 }
143 }
144 }