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.
20 * @author Kisub Song (kisubs.song@samsung.com)
27 #include <dpl/log/log.h>
28 #include <dpl/exception.h>
29 #include <Commons/Exception.h>
30 #include <Commons/Regex.h>
31 #include "ContactObjectA2PConverter.h"
32 #include "ContactObjectP2AConverter.h"
37 using namespace WrtDeviceApis::Commons;
42 , m_platformContactObject(NULL)
43 , m_platformContactObjectList(NULL)
45 // LogDebug("entered");
47 m_numbersJSObjIsSet = false;
48 m_emailsJSObjIsSet = false;
49 m_anniversariesJSObjIsSet = false;
50 m_urlsJSObjIsSet = false;
51 m_addressesJSObjIsSet = false;
52 m_organizationsJSObjIsSet = false;
53 m_notesJSObjIsSet = false;
54 m_groupIdsJSObjIsSet = false;
57 m_numbersJSValue = NULL;
60 m_emailsJSValue = NULL;
62 m_anniversariesObj = NULL;
63 m_anniversariesJSValue = NULL;
68 m_addressesObj = NULL;
69 m_addressesJSValue = NULL;
71 m_organizationsObj = NULL;
72 m_organizationsJSValue = NULL;
75 m_groupIdsJSValue = NULL;
77 m_notesJSValue = NULL;
85 if (m_platformContactObjectList != NULL)
86 contacts_list_destroy(m_platformContactObjectList, true);
88 if(numbersJSObjIsSet()){
89 JSContextRef contextRef = getContext();
90 JSObjectRef tempJSObject = getNumbersJSObj();
91 JSValueUnprotect(contextRef, tempJSObject);
94 if(EmailsJSObjIsSet()){
95 JSContextRef contextRef = getContext();
96 JSObjectRef tempJSObject = getEmailsJSObj();
97 JSValueUnprotect(contextRef, tempJSObject);
100 if(anniversariesJSObjIsSet()){
101 JSContextRef contextRef = getContext();
102 JSObjectRef tempJSObject = getAnniversariesJSObj();
103 JSValueUnprotect(contextRef, tempJSObject);
106 if(urlsJSObjIsSet()){
107 JSContextRef contextRef = getContext();
108 JSObjectRef tempJSObject = getUrlsJSObj();
109 JSValueUnprotect(contextRef, tempJSObject);
112 if(addressesJSObjIsSet()){
113 JSContextRef contextRef = getContext();
114 JSObjectRef tempJSObject = getAddressesJSObj();
115 JSValueUnprotect(contextRef, tempJSObject);
118 if(organizationsJSObjIsSet()){
119 JSContextRef contextRef = getContext();
120 JSObjectRef tempJSObject = getOrganizationsJSObj();
121 JSValueUnprotect(contextRef, tempJSObject);
124 if(groupIdsJSObjIsSet()){
125 JSContextRef contextRef = getContext();
126 JSObjectRef tempJSObject = getGroupIdsJSObj();
127 JSValueUnprotect(contextRef, tempJSObject);
130 if(notesJSObjIsSet()){
131 JSContextRef contextRef = getContext();
132 JSObjectRef tempJSObject = getNotesJSObj();
133 JSValueUnprotect(contextRef, tempJSObject);
137 string Contact::convertToString(const string &format)
141 char *vcard_stream = NULL;
143 if(!format.empty() && format != "VCARD_30")
144 ThrowMsg(ConversionException, "format must be 'VCARD_30'");
146 ContactPtr thisObj = SharedFromThis();
147 ContactObjectA2PConverterPtr contactObjConverter(NULL);
149 contacts_record_h contacts_record = NULL;
151 contactObjConverter = ContactObjectA2PConverterPtr(new ContactObjectA2PConverter(thisObj, true) );
152 contacts_record = contactObjConverter->getPlatformContact();
153 } Catch (Exception) {
154 ThrowMsg(PlatformException, "Fail to extract contact to platform object.");
156 errorCode = contacts_vcard_make_from_contact(contacts_record, &vcard_stream);
157 if(errorCode != CONTACTS_ERROR_NONE)
158 ThrowMsg(PlatformException, "Fail to convert to vCard.");
160 string result((char *)vcard_stream);
167 void Contact::setContactFromString(const std::string &vObjectStr)
171 const char *vcard_stream = vObjectStr.c_str();
173 contacts_list_h contacts_list = NULL;
174 errorCode = contacts_vcard_parse_to_contacts(vcard_stream, &contacts_list);
175 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
176 ThrowMsg(InvalidArgumentException, "Invalid vCard string.");
177 else if(errorCode != CONTACTS_ERROR_NONE)
178 ThrowMsg(PlatformException, "Fail to convert contact from vCard.");
180 unsigned int record_count = 0;
181 errorCode = contacts_list_get_count(contacts_list, &record_count);
182 if(errorCode != CONTACTS_ERROR_NONE || record_count == 0)
184 contacts_list_destroy(contacts_list, true);
185 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (2)");
188 contacts_record_h contacts_record;
189 contacts_list_first(contacts_list);
190 errorCode = contacts_list_get_current_record_p(contacts_list, &contacts_record);
191 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
193 contacts_list_destroy(contacts_list, true);
194 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (3)");
197 ContactObjectP2AConverterPtr contactObjConverter(NULL);
199 ContactPtr thisObj = SharedFromThis();
200 contactObjConverter = ContactObjectP2AConverterPtr(new ContactObjectP2AConverter(contacts_record, true, thisObj) );
201 contactObjConverter->getAbstractContact();
202 } Catch (Exception) {
203 ThrowMsg(PlatformException, "Fail to extract contact from platform object.");
206 m_platformContactObject = contacts_record;
207 m_platformContactObjectList = contacts_list;
210 void Contact::setNumbersJSObj(bool value, JSObjectRef initObj)
212 m_numbersJSObjIsSet = value;
213 m_numbersObj = initObj;
216 bool Contact::numbersJSObjIsSet() const
218 return m_numbersJSObjIsSet;
221 JSObjectRef Contact::getNumbersJSObj()
226 JSValueRef Contact::getNumbersJSValue()
228 return m_numbersJSValue;
231 void Contact::setEmailsJSObj(bool value, JSObjectRef initObj)
233 m_emailsJSObjIsSet = value;
234 m_emailsObj = initObj;
237 bool Contact::EmailsJSObjIsSet() const
239 return m_emailsJSObjIsSet;
242 JSObjectRef Contact::getEmailsJSObj()
247 JSValueRef Contact::getEmailsJSValue()
249 return m_emailsJSValue;
252 void Contact::setAnniversariesJSObj(bool value, JSObjectRef initObj)
254 m_anniversariesJSObjIsSet = value;
255 m_anniversariesObj = initObj;
258 bool Contact::anniversariesJSObjIsSet() const
260 return m_anniversariesJSObjIsSet;
263 JSObjectRef Contact::getAnniversariesJSObj()
265 return m_anniversariesObj;
268 JSValueRef Contact::getAnniversariesJSValue()
270 return m_anniversariesJSValue;
273 void Contact::setUrlsJSObj(bool value, JSObjectRef initObj)
275 m_urlsJSObjIsSet = value;
279 bool Contact::urlsJSObjIsSet() const
281 return m_urlsJSObjIsSet;
284 JSObjectRef Contact::getUrlsJSObj()
289 JSValueRef Contact::getUrlsJSValue()
291 return m_urlsJSValue;
294 void Contact::setAddressesJSObj(bool value, JSObjectRef initObj)
296 m_addressesJSObjIsSet = value;
297 m_addressesObj = initObj;
300 bool Contact::addressesJSObjIsSet() const
302 return m_addressesJSObjIsSet;
305 JSObjectRef Contact::getAddressesJSObj()
307 return m_addressesObj;
310 JSValueRef Contact::getAddressesJSValue()
312 return m_addressesJSValue;
315 void Contact::setOrganizationsJSObj(bool value, JSObjectRef initObj)
317 m_organizationsJSObjIsSet = value;
318 m_organizationsObj = initObj;
321 bool Contact::organizationsJSObjIsSet() const
323 return m_organizationsJSObjIsSet;
326 JSObjectRef Contact::getOrganizationsJSObj()
328 return m_organizationsObj;
331 JSValueRef Contact::getOrganizationsJSValue()
333 return m_organizationsJSValue;
336 void Contact::setNotesJSObj(bool value, JSObjectRef initObj)
338 m_notesJSObjIsSet = value;
339 m_notesObj = initObj;
342 bool Contact::notesJSObjIsSet() const
344 return m_notesJSObjIsSet;
347 JSObjectRef Contact::getNotesJSObj()
352 JSValueRef Contact::getNotesJSValue()
354 return m_notesJSValue;
357 void Contact::setGroupIdsJSObj(bool value, JSObjectRef initObj)
359 m_groupIdsJSObjIsSet = value;
360 m_groupIdsObj = initObj;
363 bool Contact::groupIdsJSObjIsSet() const
365 return m_groupIdsJSObjIsSet;
368 JSObjectRef Contact::getGroupIdsJSObj()
370 return m_groupIdsObj;
373 JSValueRef Contact::getGroupIdsJSValue()
375 return m_groupIdsJSValue;
378 void Contact::setContext(JSContextRef contextRef)
380 if(m_context == NULL)
381 m_context = contextRef;
384 JSContextRef Contact::getContext()