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_notesJSObjIsSet = false;
48 m_groupIdsJSObjIsSet = false;
51 m_groupIdsJSValue = NULL;
56 if (m_platformContactObjectList != NULL)
57 contacts_list_destroy(m_platformContactObjectList, true);
59 if(groupIdsJSObjIsSet()){
60 JSContextRef contextRef = getContext();
61 JSObjectRef tempJSObject = getGroupIdsJSObj();
62 JSValueUnprotect(contextRef, tempJSObject);
65 if(notesJSObjIsSet()){
66 JSContextRef contextRef = getContext();
67 JSObjectRef tempJSObject = getNotesJSObj();
68 JSValueUnprotect(contextRef, tempJSObject);
72 string Contact::convertToString(const string &format)
76 char *vcard_stream = NULL;
78 if(!format.empty() && format != "VCARD_30")
79 ThrowMsg(ConversionException, "format must be 'VCARD_30'");
81 ContactPtr thisObj = SharedFromThis();
82 ContactObjectA2PConverterPtr contactObjConverter(NULL);
84 contacts_record_h contacts_record = NULL;
86 contactObjConverter = ContactObjectA2PConverterPtr(new ContactObjectA2PConverter(thisObj, true) );
87 contacts_record = contactObjConverter->getPlatformContact();
89 ThrowMsg(PlatformException, "Fail to extract contact to platform object.");
91 errorCode = contacts_vcard_make_from_contact(contacts_record, &vcard_stream);
92 if(errorCode != CONTACTS_ERROR_NONE)
93 ThrowMsg(PlatformException, "Fail to convert to vCard.");
95 string result((char *)vcard_stream);
102 void Contact::setContactFromString(const std::string &vObjectStr)
106 const char *vcard_stream = vObjectStr.c_str();
108 contacts_list_h contacts_list = NULL;
109 errorCode = contacts_vcard_parse_to_contacts(vcard_stream, &contacts_list);
110 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
111 ThrowMsg(InvalidArgumentException, "Invalid vCard string.");
112 else if(errorCode != CONTACTS_ERROR_NONE)
113 ThrowMsg(PlatformException, "Fail to convert contact from vCard.");
115 unsigned int record_count = 0;
116 errorCode = contacts_list_get_count(contacts_list, &record_count);
117 if(errorCode != CONTACTS_ERROR_NONE || record_count == 0)
119 contacts_list_destroy(contacts_list, true);
120 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (2)");
123 contacts_record_h contacts_record;
124 contacts_list_first(contacts_list);
125 errorCode = contacts_list_get_current_record_p(contacts_list, &contacts_record);
126 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
128 contacts_list_destroy(contacts_list, true);
129 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (3)");
132 ContactObjectP2AConverterPtr contactObjConverter(NULL);
134 ContactPtr thisObj = SharedFromThis();
135 contactObjConverter = ContactObjectP2AConverterPtr(new ContactObjectP2AConverter(contacts_record, true, thisObj) );
136 contactObjConverter->getAbstractContact();
137 } Catch (Exception) {
138 ThrowMsg(PlatformException, "Fail to extract contact from platform object.");
141 m_platformContactObject = contacts_record;
142 m_platformContactObjectList = contacts_list;
145 void Contact::setNotesJSObj(bool value, JSObjectRef initObj)
147 m_notesJSObjIsSet = value;
148 m_notesObj = initObj;
151 bool Contact::notesJSObjIsSet() const
153 return m_notesJSObjIsSet;
156 JSObjectRef Contact::getNotesJSObj()
161 JSValueRef Contact::getNotesJSValue()
163 return m_notesJSValue;
166 void Contact::setGroupIdsJSObj(bool value, JSObjectRef initObj)
168 m_groupIdsJSObjIsSet = value;
169 m_groupIdsObj = initObj;
172 bool Contact::groupIdsJSObjIsSet() const
174 return m_groupIdsJSObjIsSet;
177 JSObjectRef Contact::getGroupIdsJSObj()
179 return m_groupIdsObj;
182 JSValueRef Contact::getGroupIdsJSValue()
184 return m_groupIdsJSValue;
187 void Contact::setContext(JSContextRef contextRef)
189 if(m_context == NULL)
190 m_context = contextRef;
193 JSContextRef Contact::getContext()