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 Kisub Song (kisubs.song@samsung.com)
24 #include <dpl/log/log.h>
25 #include <contacts-svc.h>
26 #include <API/Contact/ContactFactory.h>
27 #include <API/Contact/IAddressBook.h>
28 #include <API/Contact/IContact.h>
29 #include "ContactManager.h"
30 #include "AddressBook.h"
31 #include "DownloadManager.h"
33 #define DEVICE_ADDRESSBOOK "DEVICE AddressBook"
34 #define SIM_ADDRESSBOOK "SIM AddressBook"
40 using namespace WrtDeviceApis::Commons;
41 using namespace TizenApis::Api::Contact;
42 using namespace TizenApis::Api::Tizen;
44 //initialize static variables
45 int ContactManager::m_instanceCount = 0;
46 bool ContactManager::m_opened = false;
48 ContactManager::ContactManager()
49 : m_addressBookStorage(new AddressBookStorage())
51 // Currently only supports PhoneBook
52 AddressBookPtr addressBook(new AddressBook(IAddressBook::PhoneBook));
53 addressBook->setName(DEVICE_ADDRESSBOOK);
54 addressBook->setReadOnly(false);
56 m_addressBookStorage->insert(addressBook, true);
58 //protect for pararel execution
59 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
60 if (m_instanceCount == 0)
62 //when it is first instance then open connection to database
63 if (contacts_svc_connect() == CTS_SUCCESS)
65 //contacts database is opened properly
66 LogDebug("contacts database is opened properly");
71 //error during opening contacts database
72 LogError("error during opening contacts database");
76 //increase counter of instances
79 //initialize download manager
80 DownloadManagerPtr downloadManager = DownloadManager::getInstance();
83 ContactManager::~ContactManager()
85 // LogDebug("entered");
86 //protect for pararel execution
87 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
88 //decrease counter of instances
90 if (m_instanceCount == 0)
92 //when it is last instance then clse connection to database
93 if (contacts_svc_disconnect() != CTS_SUCCESS)
95 //error during closing contacts database
96 LogError("error during closing contacts database");
102 AddressBookPtr ContactManager::getDefaultAddressBook()
106 //when database is not opened then set result as failed
107 LogError("Contact DB is not opened.");
108 ThrowMsg(PlatformException, "Contact DB is not opened.");
111 return m_addressBookStorage->getDefaultAddressBook();
114 AddressBookPtr ContactManager::getAddressBook(const std::string &addressBookId)
118 //when database is not opened then set result as failed
119 LogError("Contact DB is not opened.");
120 ThrowMsg(PlatformException, "Contact DB is not opened.");
123 if(addressBookId == "")
125 //when database is not opened then set result as failed
126 LogError("Wrong address book id");
127 ThrowMsg(InvalidArgumentException, "Wrong address book id.");
130 AddressBookPtr addressBook;
133 addressBook = m_addressBookStorage->getAddressBook(addressBookId);
136 //when database is not opened then set result as failed
137 LogError("Not found : " << _rethrown_exception.GetMessage());
138 ThrowMsg(NotFoundException, "No address book");
144 void ContactManager::OnRequestReceived(const EventContactManagerGetAddressBooksPtr &event)
148 //when database is not opened then set result as failed
149 LogError("Contact DB is not opened.");
150 event->setResult(false);
151 event->setExceptionCode(ExceptionCodes::PlatformException);
157 AddressBookArrayPtr addressBookArray = m_addressBookStorage->getAddressBooks();
159 if(addressBookArray->size() == 0)
160 ThrowMsg(NotFoundException, "No address book");
162 event->setAddressBooks(addressBookArray);
163 event->setResult(true);
164 event->setExceptionCode(ExceptionCodes::None);
166 Catch (UnknownException)
168 LogError("Unknown exception : " << _rethrown_exception.GetMessage());
169 event->setResult(false);
170 event->setExceptionCode(ExceptionCodes::UnknownException);