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 CalendarManager.cpp
18 * @author Lukasz Marek (l.marek@samsung.com)
19 * @author Wojciech Bielawski (w.bielawski@samsung.com)
23 #include "CalendarManager.h"
25 #include <dpl/log/log.h>
26 #include <calendar-svc-provider.h>
27 #include <calendar-svc-errors.h>
29 #include <Commons/StringUtils.h>
32 const int ID_ALL_ACCOUNTS = 0;
33 const char* SERVICE_PROVIDER_NAME = "phone";
36 namespace WrtDeviceApis {
41 int CalendarManager::m_instanceCount = 0;
43 CalendarManager::CalendarManager()
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.
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");
61 CalendarManager::~CalendarManager()
64 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
66 if (m_instanceCount == 0) {
67 LogDebug("closing calendar DB");
68 if (CAL_SUCCESS != calendar_svc_close()) {
69 LogError("Calendar database not clearly closed.");
74 int CalendarManager::getAccountId()
76 return ID_ALL_ACCOUNTS;
79 const char *CalendarManager::getServiceProviderName()
81 return SERVICE_PROVIDER_NAME;
84 void CalendarManager::OnRequestReceived(const EventGetCalendarsPtr &event)
86 static std::vector<ICalendarPtr> calendars;
89 if (calendars.empty()) {
90 cal_iter *iter = NULL;
92 calendar_svc_get_all(getAccountId(), 0, CAL_STRUCT_CALENDAR,
94 event->setResult(false);
97 cal_struct *calendar = NULL;
98 while (CAL_SUCCESS == calendar_svc_iter_next(iter)) {
99 if (event->checkCancelled()) {
103 calendar_svc_iter_get_info(iter, &calendar)) {
104 const char* name = calendar_svc_struct_get_str(
107 const char* id = calendar_svc_struct_get_str(
109 CAL_TABLE_TXT_CALENDAR_ID);
110 if (name != NULL && id != NULL) {
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);
120 LogError("calendar contains invalid parameters");
123 LogError("cannot get calendar");
126 calendar_svc_iter_remove(&iter);
128 if (!event->checkCancelled()) {
129 std::vector<ICalendarPtr>::const_iterator it = calendars.begin();
130 for (; it != calendars.end(); ++it) {
131 event->addCalendar(*it);
133 event->setResult(true);
136 Catch(Commons::Exception)
138 LogError("error occuered during obtaining data");
139 event->setResult(false);
141 event->setCancelAllowed(true);