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 * @file ContactManager.cpp
19 * @author Lukasz Marek(l.marek@samsung.com)
24 #include <dpl/log/log.h>
25 #include <contacts-svc.h>
26 #include <Contact/IAddressBook.h>
27 #include <Contact/Contact.h>
28 #include "ContactManager.h"
29 #include "AddressBook.h"
32 const char *DEVICE_ADDRESSBOOK = "DEVICE AddressBook";
33 const char *SIM_ADDRESSBOOK = "SIM AddressBook";
36 namespace WrtDeviceApis {
41 //initialize static variables
42 int ContactManager::m_instanceCount = 0;
43 bool ContactManager::m_opened = false;
45 ContactManager::ContactManager() :
46 //create sim and device address books
47 m_deviceBook(new AddressBook(IAddressBookObject::PhoneBook)),
48 m_simBook(new AddressBook(IAddressBookObject::SIMBook))
51 //set address book names
52 m_deviceBook->setName(DEVICE_ADDRESSBOOK);
53 m_simBook->setName(SIM_ADDRESSBOOK);
54 //protect for pararel execution
55 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
56 if (m_instanceCount == 0) {
57 //when it is first instance then open connection to database
58 if (contacts_svc_connect() == CTS_SUCCESS) {
59 //contacts database is opened properly
60 LogDebug("contacts database is opened properly");
63 //error during opening contacts database
64 LogError("error during opening contacts database");
67 //increase counter of instances
71 ContactManager::~ContactManager()
74 //protect for pararel execution
75 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
76 //decrease counter of instances
78 if (m_instanceCount == 0) {
79 //when it is last instance then clse connection to database
80 if (contacts_svc_disconnect() != CTS_SUCCESS) {
81 //error during closing contacts database
82 LogError("error during closing contacts database");
88 void ContactManager::OnRequestReceived(const EventGetAddressBooksPtr &event)
92 //when database is opened then add addressbooks to result...
93 event->addAddressBook(m_deviceBook);
94 event->addAddressBook(m_simBook);
95 //... and set result as success
96 event->setResult(true);
98 //when database is not opened then set result as failed
99 event->setResult(false);
103 void ContactManager::OnRequestReceived(const Api::EventGetOwnerInfoPtr &event)
106 //TODO: platform doesn't support owner information, returning empty object now.
107 event->setOwnerInfo(ContactPtr(new Contact()));
108 event->setDataFound(false);
109 //set result as success anyway to inform receiver it's not execution error
110 event->setResult(true);