tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / 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      Lukasz Marek(l.marek@samsung.com)
20  * @version     0.1
21  *
22  */
23
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"
30
31 namespace {
32 const char *DEVICE_ADDRESSBOOK = "DEVICE AddressBook";
33 const char *SIM_ADDRESSBOOK = "SIM AddressBook";
34 }
35
36 namespace WrtDeviceApis {
37 namespace Contact {
38
39 using namespace Api;
40
41 //initialize static variables
42 int ContactManager::m_instanceCount = 0;
43 bool ContactManager::m_opened = false;
44
45 ContactManager::ContactManager() :
46     //create sim and device address books
47     m_deviceBook(new AddressBook(IAddressBookObject::PhoneBook)),
48     m_simBook(new AddressBook(IAddressBookObject::SIMBook))
49 {
50     LogDebug("entered");
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");
61             m_opened = true;
62         } else {
63             //error during opening contacts database
64             LogError("error during opening contacts database");
65         }
66     }
67     //increase counter of instances
68     m_instanceCount++;
69 }
70
71 ContactManager::~ContactManager()
72 {
73     LogDebug("entered");
74     //protect for pararel execution
75     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
76     //decrease counter of instances
77     m_instanceCount--;
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");
83         }
84         m_opened = false;
85     }
86 }
87
88 void ContactManager::OnRequestReceived(const EventGetAddressBooksPtr &event)
89 {
90     LogDebug("entered");
91     if (m_opened) {
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);
97     } else {
98         //when database is not opened then set result as failed
99         event->setResult(false);
100     }
101 }
102
103 void ContactManager::OnRequestReceived(const Api::EventGetOwnerInfoPtr &event)
104 {
105     LogDebug("entered");
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);
111 }
112 }
113 }