1167a3b0a9bb6894abb0bae992289953bd603751
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / ContactManager.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 /**
18  * @file        ContactManager.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
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"
32
33 #define DEVICE_ADDRESSBOOK      "DEVICE AddressBook"
34 #define SIM_ADDRESSBOOK         "SIM AddressBook"
35
36 namespace TizenApis {
37 namespace Platform {
38 namespace Contact {
39
40 using namespace WrtDeviceApis::Commons;
41 using namespace TizenApis::Api::Contact;
42 using namespace TizenApis::Api::Tizen;
43
44 //initialize static variables
45 int ContactManager::m_instanceCount = 0;
46 bool ContactManager::m_opened = false;
47
48 ContactManager::ContactManager()
49         : m_addressBookStorage(new AddressBookStorage())
50 {
51         // Currently only supports PhoneBook
52         AddressBookPtr addressBook(new AddressBook(IAddressBook::PhoneBook));
53         addressBook->setName(DEVICE_ADDRESSBOOK);
54         addressBook->setReadOnly(false);
55
56         m_addressBookStorage->insert(addressBook, true);
57
58         //protect for pararel execution
59         DPL::Mutex::ScopedLock mx(&m_constructorMutex);
60         if (m_instanceCount == 0)
61         {
62                 //when it is first instance then open connection to database
63                 if (contacts_svc_connect() == CTS_SUCCESS)
64                 {
65                         //contacts database is opened properly
66                         LogDebug("contacts database is opened properly");
67                         m_opened = true;
68                 }
69                 else
70                 {
71                         //error during opening contacts database
72                         LogError("error during opening contacts database");
73                 }
74         }
75
76         //increase counter of instances
77         m_instanceCount++;
78
79         //initialize download manager
80         DownloadManagerPtr downloadManager = DownloadManager::getInstance();
81 }
82
83 ContactManager::~ContactManager()
84 {
85 //      LogDebug("entered");
86         //protect for pararel execution
87         DPL::Mutex::ScopedLock mx(&m_constructorMutex);
88         //decrease counter of instances
89         m_instanceCount--;
90         if (m_instanceCount == 0)
91         {
92                 //when it is last instance then clse connection to database
93                 if (contacts_svc_disconnect() != CTS_SUCCESS)
94                 {
95                         //error during closing contacts database
96                         LogError("error during closing contacts database");
97                 }
98                 m_opened = false;
99         }
100 }
101
102 AddressBookPtr ContactManager::getDefaultAddressBook()
103 {
104         if(!m_opened)
105         {
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.");
109         }
110
111         return m_addressBookStorage->getDefaultAddressBook();
112 }
113
114 AddressBookPtr ContactManager::getAddressBook(const std::string &addressBookId)
115 {
116         if(!m_opened)
117         {
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.");
121         }
122
123         if(addressBookId == "")
124         {
125                 //when database is not opened then set result as failed
126                 LogError("Wrong address book id");
127                 ThrowMsg(InvalidArgumentException, "Wrong address book id.");
128         }
129
130         AddressBookPtr addressBook;
131
132         Try {
133                 addressBook = m_addressBookStorage->getAddressBook(addressBookId);
134
135         } Catch(Exception) {
136                 //when database is not opened then set result as failed
137                 LogError("Not found : " << _rethrown_exception.GetMessage());
138                 ThrowMsg(NotFoundException, "No address book");
139         }
140
141         return addressBook;
142 }
143
144 void ContactManager::OnRequestReceived(const EventContactManagerGetAddressBooksPtr &event)
145 {
146         if(!m_opened)
147         {
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);
152                 return;
153         }
154
155         Try
156         {
157                 AddressBookArrayPtr addressBookArray = m_addressBookStorage->getAddressBooks();
158
159                 if(addressBookArray->size() == 0)
160                         ThrowMsg(NotFoundException, "No address book");
161
162                 event->setAddressBooks(addressBookArray);
163                 event->setResult(true);
164                 event->setExceptionCode(ExceptionCodes::None);
165         }
166         Catch (UnknownException)
167         {
168                 LogError("Unknown exception : " << _rethrown_exception.GetMessage());
169                 event->setResult(false);
170                 event->setExceptionCode(ExceptionCodes::UnknownException);
171                 return;
172         }
173 }
174
175 } // Contact
176 } // Platform
177 } // TizenApis