263bc1bfdc2090a11f364bc4f7632b7124408639
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / AddressBookStorage.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        AddressBookStorage.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include <sstream>
25 #include <dpl/exception.h>
26 #include "AddressBookStorage.h"
27
28 namespace TizenApis {
29 namespace Platform {
30 namespace Contact {
31
32 using namespace WrtDeviceApis::Commons;
33 using namespace TizenApis::Api::Contact;
34 //using namespace TizenApis::Api::Tizen;
35
36 AddressBookStorage::AddressBookStorage() :
37         m_addressBooks(new AddressBookArray()),
38         m_defaultAddressBookIndex(-1)
39 {
40 }
41
42 AddressBookStorage::~AddressBookStorage()
43 {
44 }
45
46 void AddressBookStorage::insert(AddressBookPtr addressBook, bool setAsDefault)
47 {
48         int index;
49         std::string id;
50
51         std::stringstream ss;
52
53         index = m_addressBooks->size();
54
55         // just use index as id (very simple)
56         ss << index;
57         id = ss.str();
58         m_idMap[id] = index;
59
60         addressBook->setId(id);
61
62         m_addressBooks->push_back(addressBook);
63
64         if(m_defaultAddressBookIndex == -1 || setAsDefault)
65                 m_defaultAddressBookIndex = index;
66 }
67
68 AddressBookPtr AddressBookStorage::getAddressBook(const std::string &id)
69 {
70         if(m_idMap.find(id) == m_idMap.end())
71                 ThrowMsg(NotFoundException, "No AddressBook id:" << id);
72
73         int index = m_idMap[id];
74
75         if(static_cast<int>(m_addressBooks->size()) <= index)
76                 ThrowMsg(NotFoundException, "No AddressBook id:" << id);
77
78         return m_addressBooks->at(index);
79 }
80
81 AddressBookPtr AddressBookStorage::getDefaultAddressBook() const
82 {
83         if(m_defaultAddressBookIndex == -1 || m_addressBooks->size() == 0)
84                 ThrowMsg(PlatformException, "Invalid request");
85
86         return m_addressBooks->at(m_defaultAddressBookIndex);
87 }
88
89 AddressBookArrayPtr AddressBookStorage::getAddressBooks() const
90 {
91         AddressBookArrayPtr newArray(new AddressBookArray());
92
93         for(AddressBookArray::iterator i = m_addressBooks->begin(); i != m_addressBooks->end(); i++)
94                 newArray->push_back(*i);
95
96         return newArray;
97 }
98
99 } // Contact
100 } // Platform
101 } // TizenApis