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)
26 #include <dpl/exception.h>
27 #include <Commons/Exception.h>
28 #include <Commons/Regex.h>
29 #include "ContactObjectA2PConverter.h"
30 #include "ContactObjectP2AConverter.h"
36 using namespace WrtDeviceApis::Commons;
41 , m_platformContactObject(NULL)
42 , m_platformContactObjectList(NULL)
44 // LoggerD("entered");
46 m_numbersJSObjIsSet = false;
47 m_emailsJSObjIsSet = false;
48 m_anniversariesJSObjIsSet = false;
49 m_urlsJSObjIsSet = false;
50 m_addressesJSObjIsSet = false;
51 m_organizationsJSObjIsSet = false;
52 m_notesJSObjIsSet = false;
53 m_groupIdsJSObjIsSet = false;
56 m_numbersJSValue = NULL;
59 m_emailsJSValue = NULL;
61 m_anniversariesObj = NULL;
62 m_anniversariesJSValue = NULL;
67 m_addressesObj = NULL;
68 m_addressesJSValue = NULL;
70 m_organizationsObj = NULL;
71 m_organizationsJSValue = NULL;
74 m_groupIdsJSValue = NULL;
76 m_notesJSValue = NULL;
84 if (m_platformContactObjectList != NULL)
85 contacts_list_destroy(m_platformContactObjectList, true);
87 if(numbersJSObjIsSet()){
88 JSContextRef contextRef = getContext();
89 JSObjectRef tempJSObject = getNumbersJSObj();
90 JSValueUnprotect(contextRef, tempJSObject);
93 if(EmailsJSObjIsSet()){
94 JSContextRef contextRef = getContext();
95 JSObjectRef tempJSObject = getEmailsJSObj();
96 JSValueUnprotect(contextRef, tempJSObject);
99 if(anniversariesJSObjIsSet()){
100 JSContextRef contextRef = getContext();
101 JSObjectRef tempJSObject = getAnniversariesJSObj();
102 JSValueUnprotect(contextRef, tempJSObject);
105 if(urlsJSObjIsSet()){
106 JSContextRef contextRef = getContext();
107 JSObjectRef tempJSObject = getUrlsJSObj();
108 JSValueUnprotect(contextRef, tempJSObject);
111 if(addressesJSObjIsSet()){
112 JSContextRef contextRef = getContext();
113 JSObjectRef tempJSObject = getAddressesJSObj();
114 JSValueUnprotect(contextRef, tempJSObject);
117 if(organizationsJSObjIsSet()){
118 JSContextRef contextRef = getContext();
119 JSObjectRef tempJSObject = getOrganizationsJSObj();
120 JSValueUnprotect(contextRef, tempJSObject);
123 if(groupIdsJSObjIsSet()){
124 JSContextRef contextRef = getContext();
125 JSObjectRef tempJSObject = getGroupIdsJSObj();
126 JSValueUnprotect(contextRef, tempJSObject);
129 if(notesJSObjIsSet()){
130 JSContextRef contextRef = getContext();
131 JSObjectRef tempJSObject = getNotesJSObj();
132 JSValueUnprotect(contextRef, tempJSObject);
136 string Contact::convertToString(const string &format)
140 char *vcard_stream = NULL;
142 if(!format.empty() && format != "VCARD_30")
143 ThrowMsg(ConversionException, "format must be 'VCARD_30'");
145 ContactPtr thisObj = SharedFromThis();
146 ContactObjectA2PConverterPtr contactObjConverter(NULL);
148 contacts_record_h contacts_record = NULL;
150 contactObjConverter = ContactObjectA2PConverterPtr(new ContactObjectA2PConverter(thisObj, true) );
151 contacts_record = contactObjConverter->getPlatformContact();
152 } Catch (Exception) {
153 ThrowMsg(PlatformException, "Fail to extract contact to platform object.");
155 errorCode = contacts_vcard_make_from_contact(contacts_record, &vcard_stream);
156 if(errorCode != CONTACTS_ERROR_NONE)
157 ThrowMsg(PlatformException, "Fail to convert to vCard.");
159 string result((char *)vcard_stream);
166 void Contact::setContactFromString(const std::string &vObjectStr)
170 const char *vcard_stream = vObjectStr.c_str();
172 contacts_list_h contacts_list = NULL;
173 errorCode = contacts_vcard_parse_to_contacts(vcard_stream, &contacts_list);
174 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
175 ThrowMsg(InvalidArgumentException, "Invalid vCard string.");
176 else if(errorCode != CONTACTS_ERROR_NONE)
177 ThrowMsg(PlatformException, "Fail to convert contact from vCard.");
179 unsigned int record_count = 0;
180 errorCode = contacts_list_get_count(contacts_list, &record_count);
181 if(errorCode != CONTACTS_ERROR_NONE || record_count == 0)
183 contacts_list_destroy(contacts_list, true);
184 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (2)");
187 contacts_record_h contacts_record;
188 contacts_list_first(contacts_list);
189 errorCode = contacts_list_get_current_record_p(contacts_list, &contacts_record);
190 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
192 contacts_list_destroy(contacts_list, true);
193 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (3)");
196 ContactObjectP2AConverterPtr contactObjConverter(NULL);
198 ContactPtr thisObj = SharedFromThis();
199 contactObjConverter = ContactObjectP2AConverterPtr(new ContactObjectP2AConverter(contacts_record, true, thisObj) );
200 contactObjConverter->getAbstractContact();
201 } Catch (Exception) {
202 ThrowMsg(PlatformException, "Fail to extract contact from platform object.");
205 m_platformContactObject = contacts_record;
206 m_platformContactObjectList = contacts_list;
209 void Contact::setNumbersJSObj(bool value, JSObjectRef initObj)
211 m_numbersJSObjIsSet = value;
212 m_numbersObj = initObj;
215 bool Contact::numbersJSObjIsSet() const
217 return m_numbersJSObjIsSet;
220 JSObjectRef Contact::getNumbersJSObj()
225 JSValueRef Contact::getNumbersJSValue()
227 return m_numbersJSValue;
230 void Contact::resetNumbersJSObj()
232 if(numbersJSObjIsSet()){
233 JSContextRef contextRef = getContext();
234 JSObjectRef tempJSObject = getNumbersJSObj();
235 JSValueUnprotect(contextRef, tempJSObject);
237 m_numbersJSObjIsSet = false;
239 m_numbersJSValue = NULL;
242 void Contact::setEmailsJSObj(bool value, JSObjectRef initObj)
244 m_emailsJSObjIsSet = value;
245 m_emailsObj = initObj;
248 bool Contact::EmailsJSObjIsSet() const
250 return m_emailsJSObjIsSet;
253 JSObjectRef Contact::getEmailsJSObj()
258 JSValueRef Contact::getEmailsJSValue()
260 return m_emailsJSValue;
263 void Contact::resetEmailsJSObj()
265 if(EmailsJSObjIsSet()){
266 JSContextRef contextRef = getContext();
267 JSObjectRef tempJSObject = getEmailsJSObj();
268 JSValueUnprotect(contextRef, tempJSObject);
270 m_emailsJSObjIsSet = false;
272 m_emailsJSValue = NULL;
275 void Contact::setAnniversariesJSObj(bool value, JSObjectRef initObj)
277 m_anniversariesJSObjIsSet = value;
278 m_anniversariesObj = initObj;
281 bool Contact::anniversariesJSObjIsSet() const
283 return m_anniversariesJSObjIsSet;
286 JSObjectRef Contact::getAnniversariesJSObj()
288 return m_anniversariesObj;
291 JSValueRef Contact::getAnniversariesJSValue()
293 return m_anniversariesJSValue;
296 void Contact::resetAnniversariesJSObj()
298 if(anniversariesJSObjIsSet()){
299 JSContextRef contextRef = getContext();
300 JSObjectRef tempJSObject = getAnniversariesJSObj();
301 JSValueUnprotect(contextRef, tempJSObject);
303 m_anniversariesJSObjIsSet = false;
304 m_anniversariesObj = NULL;
305 m_anniversariesJSValue = NULL;
308 void Contact::setUrlsJSObj(bool value, JSObjectRef initObj)
310 m_urlsJSObjIsSet = value;
314 bool Contact::urlsJSObjIsSet() const
316 return m_urlsJSObjIsSet;
319 JSObjectRef Contact::getUrlsJSObj()
324 JSValueRef Contact::getUrlsJSValue()
326 return m_urlsJSValue;
329 void Contact::resetUrlsJSObj()
331 if(urlsJSObjIsSet()){
332 JSContextRef contextRef = getContext();
333 JSObjectRef tempJSObject = getUrlsJSObj();
334 JSValueUnprotect(contextRef, tempJSObject);
336 m_urlsJSObjIsSet = false;
338 m_urlsJSValue = NULL;
341 void Contact::setAddressesJSObj(bool value, JSObjectRef initObj)
343 m_addressesJSObjIsSet = value;
344 m_addressesObj = initObj;
347 bool Contact::addressesJSObjIsSet() const
349 return m_addressesJSObjIsSet;
352 JSObjectRef Contact::getAddressesJSObj()
354 return m_addressesObj;
357 JSValueRef Contact::getAddressesJSValue()
359 return m_addressesJSValue;
362 void Contact::resetAddressesJSObj()
364 if(addressesJSObjIsSet()){
365 JSContextRef contextRef = getContext();
366 JSObjectRef tempJSObject = getAddressesJSObj();
367 JSValueUnprotect(contextRef, tempJSObject);
369 m_addressesJSObjIsSet = false;
370 m_addressesObj = NULL;
371 m_addressesJSValue = NULL;
374 void Contact::setOrganizationsJSObj(bool value, JSObjectRef initObj)
376 m_organizationsJSObjIsSet = value;
377 m_organizationsObj = initObj;
380 bool Contact::organizationsJSObjIsSet() const
382 return m_organizationsJSObjIsSet;
385 JSObjectRef Contact::getOrganizationsJSObj()
387 return m_organizationsObj;
390 JSValueRef Contact::getOrganizationsJSValue()
392 return m_organizationsJSValue;
395 void Contact::resetOrganizationsJSObj()
397 if(organizationsJSObjIsSet()){
398 JSContextRef contextRef = getContext();
399 JSObjectRef tempJSObject = getOrganizationsJSObj();
400 JSValueUnprotect(contextRef, tempJSObject);
402 m_organizationsJSObjIsSet = false;
403 m_organizationsObj = NULL;
404 m_organizationsJSValue = NULL;
407 void Contact::setNotesJSObj(bool value, JSObjectRef initObj)
409 m_notesJSObjIsSet = value;
410 m_notesObj = initObj;
413 bool Contact::notesJSObjIsSet() const
415 return m_notesJSObjIsSet;
418 JSObjectRef Contact::getNotesJSObj()
423 JSValueRef Contact::getNotesJSValue()
425 return m_notesJSValue;
428 void Contact::resetNotesJSObj()
430 if(notesJSObjIsSet()){
431 JSContextRef contextRef = getContext();
432 JSObjectRef tempJSObject = getNotesJSObj();
433 JSValueUnprotect(contextRef, tempJSObject);
435 m_notesJSObjIsSet = false;
437 m_notesJSValue = NULL;
440 void Contact::setGroupIdsJSObj(bool value, JSObjectRef initObj)
442 m_groupIdsJSObjIsSet = value;
443 m_groupIdsObj = initObj;
446 bool Contact::groupIdsJSObjIsSet() const
448 return m_groupIdsJSObjIsSet;
451 JSObjectRef Contact::getGroupIdsJSObj()
453 return m_groupIdsObj;
456 JSValueRef Contact::getGroupIdsJSValue()
458 return m_groupIdsJSValue;
461 void Contact::resetGroupIdsJSObj()
463 if(groupIdsJSObjIsSet()){
464 JSContextRef contextRef = getContext();
465 JSObjectRef tempJSObject = getGroupIdsJSObj();
466 JSValueUnprotect(contextRef, tempJSObject);
468 m_groupIdsJSObjIsSet = false;
469 m_groupIdsObj = NULL;
470 m_groupIdsJSValue = NULL;
473 void Contact::setContext(JSContextRef contextRef)
475 if(m_context == NULL)
476 m_context = contextRef;
479 JSContextRef Contact::getContext()