Update change log and spec for wrt-plugins-tizen_0.4.49
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactAddress.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        ContactAddress.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include <algorithm>
26 #include "ContactAddress.h"
27
28 namespace DeviceAPI {
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         m_isDefault(false)
39 {
40         m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
41         is_typesSetJSArray = false;
42         m_context = NULL;
43         m_typesJsValue = NULL;
44         m_typesObj = NULL;
45 }
46
47 ContactAddress::~ContactAddress()
48 {
49         if(IsTypesSetJSArray()){
50                 JSContextRef contextRef = getContext();
51                 JSObjectRef tempJSObject = getTypesJSObj();
52                 JSValueUnprotect(contextRef, tempJSObject);
53         }
54 }
55 std::string ContactAddress::getCountry() const
56 {
57         return m_country;
58 }
59
60 void ContactAddress::setCountry(const std::string &value)
61 {
62         m_country = value;
63         m_countryIsSet = true;
64 }
65
66 void ContactAddress::unsetCountry()
67 {
68         m_country = "";
69         m_countryIsSet = false;
70 }
71
72 bool ContactAddress::getCountryIsSet() const
73 {
74         return m_countryIsSet;
75 }
76
77 std::string ContactAddress::getRegion() const
78 {
79         return m_region;
80 }
81
82 void ContactAddress::setRegion(const std::string &value)
83 {
84         m_region = value;
85         m_regionIsSet = true;
86 }
87
88 void ContactAddress::unsetRegion()
89 {
90         m_region = "";
91         m_regionIsSet = false;
92 }
93
94 bool ContactAddress::getRegionIsSet() const
95 {
96         return m_regionIsSet;
97 }
98
99 std::string ContactAddress::getCity() const
100 {
101         return m_city;
102 }
103
104 void ContactAddress::setCity(const std::string &value)
105 {
106         m_city = value;
107         m_cityIsSet = true;
108 }
109
110 void ContactAddress::unsetCity()
111 {
112         m_city = "";
113         m_cityIsSet = false;
114 }
115
116 bool ContactAddress::getCityIsSet() const
117 {
118         return m_cityIsSet;
119 }
120
121 std::string ContactAddress::getStreetAddress() const
122 {
123         return m_streetAddress;
124 }
125
126 void ContactAddress::setStreetAddress(const std::string &value)
127 {
128         m_streetAddress = value;
129         m_streetAddressIsSet = true;
130 }
131
132 void ContactAddress::unsetStreetAddress()
133 {
134         m_streetAddress = "";
135         m_streetAddressIsSet = false;
136 }
137
138 bool ContactAddress::getStreetAddressIsSet() const
139 {
140         return m_streetAddressIsSet;
141 }
142
143 std::string ContactAddress::getAdditionalInformation() const
144 {
145         return m_additionalInformation;
146 }
147
148 void ContactAddress::setAdditionalInformation(const std::string &value)
149 {
150         m_additionalInformation = value;
151         m_additionalInformationIsSet = true;
152 }
153
154 void ContactAddress::unsetAdditionalInformation()
155 {
156         m_additionalInformation = "";
157         m_additionalInformationIsSet = false;
158 }
159
160 bool ContactAddress::getAdditionalInformationIsSet() const
161 {
162         return m_additionalInformationIsSet;
163 }
164
165 std::string ContactAddress::getPostalCode() const
166 {
167         return m_postalCode;
168 }
169
170 void ContactAddress::setPostalCode(const std::string &value)
171 {
172         m_postalCode = value;
173         m_postalCodeIsSet = true;
174 }
175
176 void ContactAddress::unsetPostalCode()
177 {
178         m_postalCode = "";
179         m_postalCodeIsSet = false;
180 }
181
182 bool ContactAddress::getPostalCodeIsSet() const
183 {
184         return m_postalCodeIsSet;
185 }
186
187 bool ContactAddress::getIsDefault() const
188 {
189         return m_isDefault;
190 }
191
192 void ContactAddress::setIsDefault(const bool &value)
193 {
194         m_isDefault = value;
195 }
196
197 ContactAddressTypeArrayPtr ContactAddress::getTypes() const
198 {
199         return m_types;
200 }
201
202 void ContactAddress::setTypes(const ContactAddressTypeArrayPtr &value)
203 {
204         m_types = value;
205 }
206
207 void ContactAddress::addType(const ContactAddressType &value)
208 {
209         m_types->push_back(value);
210 }
211
212 bool ContactAddress::isTypeOf(const ContactAddressType &value) const
213 {
214         return std::find(m_types->begin(), m_types->end(), value) != m_types->end();
215 }
216
217 int ContactAddress::getTypesNum() const
218 {
219         return m_types->size();
220 }
221
222
223 bool ContactAddress::compareTo(const ContactAddressPtr &address,
224                 bool includeId,
225                 bool includeTypes) const
226 {
227         //compare basic fields
228         if ((!includeId) &&
229                 m_country == address->getCountry() &&
230                 m_region == address->getRegion() &&
231                 m_city == address->getCity() &&
232                 m_streetAddress == address->getStreetAddress() &&
233                 m_additionalInformation == address->getAdditionalInformation() &&
234                 m_postalCode == address->getPostalCode()) {
235                 //if not include fields then addresses are equal
236                 if (!includeTypes) {
237                         return true;
238                 }
239                 //if types have different sizes then addresses are different
240                 if (m_types->size() != address->getTypes()->size()) {
241                         return false;
242                 }
243                 //compare each type
244                 for (size_t i = 0; i < address->getTypes()->size(); i++) {
245                         if (!isTypeOf(address->getTypes()->at(i))) {
246                                 return false;
247                         }
248                 }
249                 return true;
250         }
251         return false;
252 }
253
254 std::string ContactAddress::getAsSingleString() const
255 {
256         std::string fullAddress;
257         //add street and street number when not empty
258         if (!m_streetAddress.empty()) {
259                 fullAddress = m_streetAddress;
260         }
261         //add city and postal code when not empty
262         if (!m_city.empty()) {
263                 if (!fullAddress.empty()) {
264                         fullAddress += " ";
265                 }
266                 if (!m_postalCode.empty()) {
267                         fullAddress += m_postalCode + " " + m_city;
268                 } else {
269                         fullAddress += m_city;
270                 }
271         }
272         //add country when not empty
273         if (!m_country.empty()) {
274                 if (!fullAddress.empty()) {
275                         fullAddress += " " + m_country;
276                 } else {
277                         fullAddress = m_country;
278                 }
279         }
280         if (!fullAddress.empty()) {
281                 return fullAddress;
282         }
283         //when no data on detailed fields, then return free form field
284         return "";
285 }
286
287 void ContactAddress::clear()
288 {
289         m_country = "";
290         m_countryIsSet = false;
291
292         m_region = "";
293         m_regionIsSet = false;
294
295         m_city = "";
296         m_cityIsSet = false;
297
298         m_streetAddress = "";
299         m_streetAddressIsSet = false;
300
301         m_additionalInformation = "";
302         m_additionalInformationIsSet = false;
303
304         m_postalCode = "";
305         m_postalCodeIsSet = false;
306
307         m_label = "";
308         m_labelIsSet = false;
309
310         m_isDefault = false;
311
312         m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
313 }
314
315 ContactAddressPtr ContactAddress::clone() const
316 {
317         ContactAddressPtr result(new ContactAddress());
318
319         result->m_country = m_country;
320         result->m_countryIsSet = m_countryIsSet;
321
322         result->m_region = m_region;
323         result->m_regionIsSet = m_regionIsSet;
324
325         result->m_city = m_city;
326         result->m_cityIsSet = m_cityIsSet;
327
328         result->m_streetAddress = m_streetAddress;
329         result->m_streetAddressIsSet = m_streetAddressIsSet;
330
331         result->m_additionalInformation = m_additionalInformation;
332         result->m_additionalInformationIsSet = m_additionalInformationIsSet;
333
334         result->m_postalCode = m_postalCode;
335         result->m_postalCodeIsSet = m_postalCodeIsSet;
336
337         result->m_isDefault = m_isDefault;
338
339         result->m_label = m_label;
340         result->m_labelIsSet = m_labelIsSet;
341
342         result->m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
343         ContactAddressTypeArray::iterator typeIter;
344         for(typeIter = m_types->begin(); typeIter != m_types->end(); typeIter++)
345         {
346                 ContactAddressType addressType = *typeIter;
347                 result->m_types->push_back(addressType);
348         }
349
350         return result;
351 }
352
353 std::string ContactAddress::getLabel() const
354 {
355         return m_label;
356 }
357
358 void ContactAddress::setLabel(const std::string &value)
359 {
360         m_label = value;
361         m_labelIsSet = true;
362 }
363
364 bool ContactAddress::getLabelIsSet() const
365 {
366         return m_labelIsSet;
367 }
368
369 void ContactAddress::setTypesJSArray(bool value, JSObjectRef initValue)
370 {
371         is_typesSetJSArray = value;
372         m_typesObj = initValue;
373 }
374
375 JSValueRef ContactAddress::getTypesJSArray()
376 {
377         return m_typesJsValue;
378 }
379
380 JSObjectRef ContactAddress::getTypesJSObj()
381 {
382         return m_typesObj;
383 }
384
385 bool ContactAddress::IsTypesSetJSArray() const
386 {
387         return is_typesSetJSArray;
388 }
389
390 void ContactAddress::resetTypesJSObj()
391 {
392         if(IsTypesSetJSArray()){
393                 JSContextRef contextRef = getContext();
394                 JSObjectRef tempJSObject = getTypesJSObj();
395                 JSValueUnprotect(contextRef, tempJSObject);
396         }
397         is_typesSetJSArray = false;
398         m_typesObj = NULL;
399         m_typesJsValue = NULL;
400 }
401
402 void ContactAddress::setContext(JSContextRef contextRef)
403 {
404         if(m_context == NULL)
405                 m_context = contextRef;
406 }
407
408 JSContextRef ContactAddress::getContext()
409 {
410         return m_context;
411 }
412
413 } // Contact
414 } // DeviceAPI