b5c13af033002e130ed9dda920a00b6d50da7cb4
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Contact / ContactAddress.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        ContactAddress.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include <algorithm>
25 #include "ContactAddress.h"
26
27 namespace TizenApis {
28 namespace Api {
29 namespace Contact {
30
31 ContactAddress::ContactAddress() :
32         m_countryIsSet(false),
33         m_regionIsSet(false),
34         m_cityIsSet(false),
35         m_streetAddressIsSet(false),
36         m_additionalInformationIsSet(false),
37         m_postalCodeIsSet(false)
38 {
39         m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
40 }
41
42 ContactAddress::~ContactAddress()
43 {
44         //nothing to do
45 }
46 std::string ContactAddress::getCountry() const
47 {
48         return m_country;
49 }
50
51 void ContactAddress::setCountry(const std::string &value)
52 {
53         m_country = value;
54         m_countryIsSet = true;
55 }
56
57 bool ContactAddress::getCountryIsSet() const
58 {
59         return m_countryIsSet;
60 }
61
62 std::string ContactAddress::getRegion() const
63 {
64         return m_region;
65 }
66
67 void ContactAddress::setRegion(const std::string &value)
68 {
69         m_region = value;
70         m_regionIsSet = true;
71 }
72
73 bool ContactAddress::getRegionIsSet() const
74 {
75         return m_regionIsSet;
76 }
77
78 std::string ContactAddress::getCity() const
79 {
80         return m_city;
81 }
82
83 void ContactAddress::setCity(const std::string &value)
84 {
85         m_city = value;
86         m_cityIsSet = true;
87 }
88
89 bool ContactAddress::getCityIsSet() const
90 {
91         return m_cityIsSet;
92 }
93
94 std::string ContactAddress::getStreetAddress() const
95 {
96         return m_streetAddress;
97 }
98
99 void ContactAddress::setStreetAddress(const std::string &value)
100 {
101         m_streetAddress = value;
102         m_streetAddressIsSet = true;
103 }
104
105 bool ContactAddress::getStreetAddressIsSet() const
106 {
107         return m_streetAddressIsSet;
108 }
109
110 std::string ContactAddress::getAdditionalInformation() const
111 {
112         return m_additionalInformation;
113 }
114
115 void ContactAddress::setAdditionalInformation(const std::string &value)
116 {
117         m_additionalInformation = value;
118         m_additionalInformationIsSet = true;
119 }
120
121 bool ContactAddress::getAdditionalInformationIsSet() const
122 {
123         return m_additionalInformationIsSet;
124 }
125
126 std::string ContactAddress::getPostalCode() const
127 {
128         return m_postalCode;
129 }
130
131 void ContactAddress::setPostalCode(const std::string &value)
132 {
133         m_postalCode = value;
134         m_postalCodeIsSet = true;
135 }
136
137 bool ContactAddress::getPostalCodeIsSet() const
138 {
139         return m_postalCodeIsSet;
140 }
141
142 ContactAddressTypeArrayPtr ContactAddress::getTypes() const
143 {
144         return m_types;
145 }
146
147 void ContactAddress::setTypes(const ContactAddressTypeArrayPtr &value)
148 {
149         m_types = value;
150 }
151
152 void ContactAddress::addType(const ContactAddressType &value)
153 {
154         m_types->push_back(value);
155 }
156
157 bool ContactAddress::isTypeOf(const ContactAddressType &value) const
158 {
159         return std::find(m_types->begin(), m_types->end(), value) != m_types->end();
160 }
161
162 int ContactAddress::getTypesNum() const
163 {
164         return m_types->size();
165 }
166
167
168 bool ContactAddress::compareTo(const ContactAddressPtr &address,
169                 bool includeId,
170                 bool includeTypes) const
171 {
172         //compare basic fields
173         if ((!includeId) &&
174                 m_country == address->getCountry() &&
175                 m_region == address->getRegion() &&
176                 m_city == address->getCity() &&
177                 m_streetAddress == address->getStreetAddress() &&
178                 m_additionalInformation == address->getAdditionalInformation() &&
179                 m_postalCode == address->getPostalCode()) {
180                 //if not include fields then addresses are equal
181                 if (!includeTypes) {
182                         return true;
183                 }
184                 //if types have different sizes then addresses are different
185                 if (m_types->size() != address->getTypes()->size()) {
186                         return false;
187                 }
188                 //compare each type
189                 for (size_t i = 0; i < address->getTypes()->size(); i++) {
190                         if (!isTypeOf(address->getTypes()->at(i))) {
191                                 return false;
192                         }
193                 }
194                 return true;
195         }
196         return false;
197 }
198
199 std::string ContactAddress::getAsSingleString() const
200 {
201         std::string fullAddress;
202         //add street and street number when not empty
203         if (!m_streetAddress.empty()) {
204                 fullAddress = m_streetAddress;
205         }
206         //add city and postal code when not empty
207         if (!m_city.empty()) {
208                 if (!fullAddress.empty()) {
209                         fullAddress += " ";
210                 }
211                 if (!m_postalCode.empty()) {
212                         fullAddress += m_postalCode + " " + m_city;
213                 } else {
214                         fullAddress += m_city;
215                 }
216         }
217         //add country when not empty
218         if (!m_country.empty()) {
219                 if (!fullAddress.empty()) {
220                         fullAddress += " " + m_country;
221                 } else {
222                         fullAddress = m_country;
223                 }
224         }
225         if (!fullAddress.empty()) {
226                 return fullAddress;
227         }
228         //when no data on detailed fields, then return free form field
229         return "";
230 }
231
232 void ContactAddress::clear()
233 {
234         m_country = "";
235         m_region = "";
236         m_city = "";
237         m_streetAddress = "";
238         m_additionalInformation = "";
239         m_postalCode = "";
240         m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
241
242         m_countryIsSet = false;
243         m_regionIsSet = false;
244         m_cityIsSet = false;
245         m_streetAddressIsSet = false;
246         m_additionalInformationIsSet = false;
247         m_postalCodeIsSet = false;
248 }
249
250 ContactAddressPtr ContactAddress::clone() const
251 {
252         ContactAddressPtr result(new ContactAddress());
253
254         result->m_country = m_country;
255         result->m_region = m_region;
256         result->m_city = m_city;
257         result->m_streetAddress = m_streetAddress;
258         result->m_additionalInformation = m_additionalInformation;
259         result->m_postalCode = m_postalCode;
260         result->m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
261         ContactAddressTypeArray::iterator typeIter;
262         for(typeIter = m_types->begin(); typeIter != m_types->end(); typeIter++)
263         {
264                 ContactAddressType addressType = *typeIter;
265                 result->m_types->push_back(addressType);
266         }
267
268         result->m_countryIsSet = m_countryIsSet;
269         result->m_regionIsSet = m_regionIsSet;
270         result->m_cityIsSet = m_cityIsSet;
271         result->m_streetAddressIsSet = m_streetAddressIsSet;
272         result->m_additionalInformationIsSet = m_additionalInformationIsSet;
273         result->m_postalCodeIsSet = m_postalCodeIsSet;
274
275         return result;
276 }
277
278 } // Contact
279 } // Api
280 } // TizenApis