2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file ContactAddress.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
26 #include "ContactAddress.h"
31 ContactAddress::ContactAddress() :
32 m_countryIsSet(false),
35 m_streetAddressIsSet(false),
36 m_additionalInformationIsSet(false),
37 m_postalCodeIsSet(false),
40 m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
41 is_typesSetJSArray = false;
43 m_typesJsValue = NULL;
47 ContactAddress::~ContactAddress()
49 if(IsTypesSetJSArray()){
50 JSContextRef contextRef = getContext();
51 JSObjectRef tempJSObject = getTypesJSObj();
52 JSValueUnprotect(contextRef, tempJSObject);
55 std::string ContactAddress::getCountry() const
60 void ContactAddress::setCountry(const std::string &value)
63 m_countryIsSet = true;
66 void ContactAddress::unsetCountry()
69 m_countryIsSet = false;
72 bool ContactAddress::getCountryIsSet() const
74 return m_countryIsSet;
77 std::string ContactAddress::getRegion() const
82 void ContactAddress::setRegion(const std::string &value)
88 void ContactAddress::unsetRegion()
91 m_regionIsSet = false;
94 bool ContactAddress::getRegionIsSet() const
99 std::string ContactAddress::getCity() const
104 void ContactAddress::setCity(const std::string &value)
110 void ContactAddress::unsetCity()
116 bool ContactAddress::getCityIsSet() const
121 std::string ContactAddress::getStreetAddress() const
123 return m_streetAddress;
126 void ContactAddress::setStreetAddress(const std::string &value)
128 m_streetAddress = value;
129 m_streetAddressIsSet = true;
132 void ContactAddress::unsetStreetAddress()
134 m_streetAddress = "";
135 m_streetAddressIsSet = false;
138 bool ContactAddress::getStreetAddressIsSet() const
140 return m_streetAddressIsSet;
143 std::string ContactAddress::getAdditionalInformation() const
145 return m_additionalInformation;
148 void ContactAddress::setAdditionalInformation(const std::string &value)
150 m_additionalInformation = value;
151 m_additionalInformationIsSet = true;
154 void ContactAddress::unsetAdditionalInformation()
156 m_additionalInformation = "";
157 m_additionalInformationIsSet = false;
160 bool ContactAddress::getAdditionalInformationIsSet() const
162 return m_additionalInformationIsSet;
165 std::string ContactAddress::getPostalCode() const
170 void ContactAddress::setPostalCode(const std::string &value)
172 m_postalCode = value;
173 m_postalCodeIsSet = true;
176 void ContactAddress::unsetPostalCode()
179 m_postalCodeIsSet = false;
182 bool ContactAddress::getPostalCodeIsSet() const
184 return m_postalCodeIsSet;
187 bool ContactAddress::getIsDefault() const
192 void ContactAddress::setIsDefault(const bool &value)
197 ContactAddressTypeArrayPtr ContactAddress::getTypes() const
202 void ContactAddress::setTypes(const ContactAddressTypeArrayPtr &value)
207 void ContactAddress::addType(const ContactAddressType &value)
209 m_types->push_back(value);
212 bool ContactAddress::isTypeOf(const ContactAddressType &value) const
214 return std::find(m_types->begin(), m_types->end(), value) != m_types->end();
217 int ContactAddress::getTypesNum() const
219 return m_types->size();
223 bool ContactAddress::compareTo(const ContactAddressPtr &address,
225 bool includeTypes) const
227 //compare basic fields
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
239 //if types have different sizes then addresses are different
240 if (m_types->size() != address->getTypes()->size()) {
244 for (size_t i = 0; i < address->getTypes()->size(); i++) {
245 if (!isTypeOf(address->getTypes()->at(i))) {
254 std::string ContactAddress::getAsSingleString() const
256 std::string fullAddress;
257 //add street and street number when not empty
258 if (!m_streetAddress.empty()) {
259 fullAddress = m_streetAddress;
261 //add city and postal code when not empty
262 if (!m_city.empty()) {
263 if (!fullAddress.empty()) {
266 if (!m_postalCode.empty()) {
267 fullAddress += m_postalCode + " " + m_city;
269 fullAddress += m_city;
272 //add country when not empty
273 if (!m_country.empty()) {
274 if (!fullAddress.empty()) {
275 fullAddress += " " + m_country;
277 fullAddress = m_country;
280 if (!fullAddress.empty()) {
283 //when no data on detailed fields, then return free form field
287 void ContactAddress::clear()
290 m_countryIsSet = false;
293 m_regionIsSet = false;
298 m_streetAddress = "";
299 m_streetAddressIsSet = false;
301 m_additionalInformation = "";
302 m_additionalInformationIsSet = false;
305 m_postalCodeIsSet = false;
309 m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
312 ContactAddressPtr ContactAddress::clone() const
314 ContactAddressPtr result(new ContactAddress());
316 result->m_country = m_country;
317 result->m_countryIsSet = m_countryIsSet;
319 result->m_region = m_region;
320 result->m_regionIsSet = m_regionIsSet;
322 result->m_city = m_city;
323 result->m_cityIsSet = m_cityIsSet;
325 result->m_streetAddress = m_streetAddress;
326 result->m_streetAddressIsSet = m_streetAddressIsSet;
328 result->m_additionalInformation = m_additionalInformation;
329 result->m_additionalInformationIsSet = m_additionalInformationIsSet;
331 result->m_postalCode = m_postalCode;
332 result->m_postalCodeIsSet = m_postalCodeIsSet;
334 result->m_isDefault = m_isDefault;
336 result->m_types = ContactAddressTypeArrayPtr(new ContactAddressTypeArray());
337 ContactAddressTypeArray::iterator typeIter;
338 for(typeIter = m_types->begin(); typeIter != m_types->end(); typeIter++)
340 ContactAddressType addressType = *typeIter;
341 result->m_types->push_back(addressType);
347 void ContactAddress::setTypesJSArray(bool value, JSObjectRef initValue)
349 is_typesSetJSArray = value;
350 m_typesObj = initValue;
353 JSValueRef ContactAddress::getTypesJSArray()
355 return m_typesJsValue;
358 JSObjectRef ContactAddress::getTypesJSObj()
363 bool ContactAddress::IsTypesSetJSArray() const
365 return is_typesSetJSArray;
369 void ContactAddress::setContext(JSContextRef contextRef)
371 if(m_context == NULL)
372 m_context = contextRef;
375 JSContextRef ContactAddress::getContext()