Update change log and spec for wrt-plugins-tizen_0.4.11
[framework/web/wrt-plugins-tizen.git] / src / Contact / Contact.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        Contact.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "Contact.h"
26
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"
33
34 namespace DeviceAPI {
35 namespace Contact {
36
37 using namespace WrtDeviceApis::Commons;
38 using namespace std;
39
40 Contact::Contact()
41         : IContact()
42         , m_platformContactObject(NULL)
43         , m_platformContactObjectList(NULL)
44 {
45 //      LogDebug("entered");
46
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;
55
56         m_numbersObj = NULL;
57         m_numbersJSValue = NULL;
58
59         m_emailsObj = NULL;
60         m_emailsJSValue = NULL;
61
62         m_anniversariesObj = NULL;
63         m_anniversariesJSValue = NULL;
64
65         m_urlsObj = NULL;
66         m_urlsJSValue = NULL;
67
68         m_addressesObj = NULL;
69         m_addressesJSValue = NULL;
70
71         m_organizationsObj = NULL;
72         m_organizationsJSValue = NULL;
73
74         m_groupIdsObj = NULL;
75         m_groupIdsJSValue = NULL;
76         m_notesObj = NULL;
77         m_notesJSValue = NULL;
78
79         m_context = NULL;
80
81 }
82
83 Contact::~Contact()
84 {
85         if (m_platformContactObjectList != NULL)
86                 contacts_list_destroy(m_platformContactObjectList, true);
87
88         if(numbersJSObjIsSet()){
89                 JSContextRef contextRef = getContext();
90                 JSObjectRef tempJSObject = getNumbersJSObj();
91                 JSValueUnprotect(contextRef, tempJSObject);
92         }
93
94         if(EmailsJSObjIsSet()){
95                 JSContextRef contextRef = getContext();
96                 JSObjectRef tempJSObject = getEmailsJSObj();
97                 JSValueUnprotect(contextRef, tempJSObject);
98         }
99
100         if(anniversariesJSObjIsSet()){
101                 JSContextRef contextRef = getContext();
102                 JSObjectRef tempJSObject = getAnniversariesJSObj();
103                 JSValueUnprotect(contextRef, tempJSObject);
104         }
105
106         if(urlsJSObjIsSet()){
107                 JSContextRef contextRef = getContext();
108                 JSObjectRef tempJSObject = getUrlsJSObj();
109                 JSValueUnprotect(contextRef, tempJSObject);
110         }
111
112         if(addressesJSObjIsSet()){
113                 JSContextRef contextRef = getContext();
114                 JSObjectRef tempJSObject = getAddressesJSObj();
115                 JSValueUnprotect(contextRef, tempJSObject);
116         }
117
118         if(organizationsJSObjIsSet()){
119                 JSContextRef contextRef = getContext();
120                 JSObjectRef tempJSObject = getOrganizationsJSObj();
121                 JSValueUnprotect(contextRef, tempJSObject);
122         }
123
124         if(groupIdsJSObjIsSet()){
125                 JSContextRef contextRef = getContext();
126                 JSObjectRef tempJSObject = getGroupIdsJSObj();
127                 JSValueUnprotect(contextRef, tempJSObject);
128         }
129
130         if(notesJSObjIsSet()){
131                 JSContextRef contextRef = getContext();
132                 JSObjectRef tempJSObject = getNotesJSObj();
133                 JSValueUnprotect(contextRef, tempJSObject);
134         }
135 }
136
137 string Contact::convertToString(const string &format)
138 {
139         LogDebug("entered");
140         int errorCode = 0;
141         char *vcard_stream = NULL;
142
143         if(!format.empty() && format != "VCARD_30")
144                 ThrowMsg(ConversionException, "format must be 'VCARD_30'");
145
146         ContactPtr thisObj = SharedFromThis();
147         ContactObjectA2PConverterPtr contactObjConverter(NULL);
148
149         contacts_record_h contacts_record = NULL;
150         Try {
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.");
155         }
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.");
159
160         string result((char *)vcard_stream);
161
162         free(vcard_stream);
163
164         return result;
165 }
166
167 void Contact::setContactFromString(const std::string &vObjectStr)
168 {
169         LogDebug("entered");
170         int errorCode = 0;
171         const char *vcard_stream = vObjectStr.c_str();
172
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.");
179
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)
183         {
184                 contacts_list_destroy(contacts_list, true);
185                 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (2)");
186         }
187
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)
192         {
193                 contacts_list_destroy(contacts_list, true);
194                 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (3)");
195         }
196
197         ContactObjectP2AConverterPtr contactObjConverter(NULL);
198         Try {
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.");
204         }
205
206         m_platformContactObject = contacts_record;
207         m_platformContactObjectList = contacts_list;
208 }
209
210 void Contact::setNumbersJSObj(bool value, JSObjectRef initObj)
211 {
212         m_numbersJSObjIsSet = value;
213         m_numbersObj = initObj;
214 }
215
216 bool Contact::numbersJSObjIsSet() const
217 {
218         return m_numbersJSObjIsSet;
219 }
220
221 JSObjectRef Contact::getNumbersJSObj()
222 {
223         return m_numbersObj;
224 }
225
226 JSValueRef Contact::getNumbersJSValue()
227 {
228         return m_numbersJSValue;
229 }
230
231 void Contact::setEmailsJSObj(bool value, JSObjectRef initObj)
232 {
233         m_emailsJSObjIsSet = value;
234         m_emailsObj = initObj;
235 }
236
237 bool Contact::EmailsJSObjIsSet() const
238 {
239         return m_emailsJSObjIsSet;
240 }
241
242 JSObjectRef Contact::getEmailsJSObj()
243 {
244         return m_emailsObj;
245 }
246
247 JSValueRef Contact::getEmailsJSValue()
248 {
249         return m_emailsJSValue;
250 }
251
252 void Contact::setAnniversariesJSObj(bool value, JSObjectRef initObj)
253 {
254         m_anniversariesJSObjIsSet = value;
255         m_anniversariesObj = initObj;
256 }
257
258 bool Contact::anniversariesJSObjIsSet() const
259 {
260         return m_anniversariesJSObjIsSet;
261 }
262
263 JSObjectRef Contact::getAnniversariesJSObj()
264 {
265         return m_anniversariesObj;
266 }
267
268 JSValueRef Contact::getAnniversariesJSValue()
269 {
270         return m_anniversariesJSValue;
271 }
272
273 void Contact::setUrlsJSObj(bool value, JSObjectRef initObj)
274 {
275         m_urlsJSObjIsSet = value;
276         m_urlsObj = initObj;
277 }
278
279 bool Contact::urlsJSObjIsSet() const
280 {
281         return m_urlsJSObjIsSet;
282 }
283
284 JSObjectRef Contact::getUrlsJSObj()
285 {
286         return m_urlsObj;
287 }
288
289 JSValueRef Contact::getUrlsJSValue()
290 {
291         return m_urlsJSValue;
292 }
293
294 void Contact::setAddressesJSObj(bool value, JSObjectRef initObj)
295 {
296         m_addressesJSObjIsSet = value;
297         m_addressesObj = initObj;
298 }
299
300 bool Contact::addressesJSObjIsSet() const
301 {
302         return m_addressesJSObjIsSet;
303 }
304
305 JSObjectRef Contact::getAddressesJSObj()
306 {
307         return m_addressesObj;
308 }
309
310 JSValueRef Contact::getAddressesJSValue()
311 {
312         return m_addressesJSValue;
313 }
314
315 void Contact::setOrganizationsJSObj(bool value, JSObjectRef initObj)
316 {
317         m_organizationsJSObjIsSet = value;
318         m_organizationsObj = initObj;
319 }
320
321 bool Contact::organizationsJSObjIsSet() const
322 {
323         return m_organizationsJSObjIsSet;
324 }
325
326 JSObjectRef Contact::getOrganizationsJSObj()
327 {
328         return m_organizationsObj;
329 }
330
331 JSValueRef Contact::getOrganizationsJSValue()
332 {
333         return m_organizationsJSValue;
334 }
335
336 void Contact::setNotesJSObj(bool value, JSObjectRef initObj)
337 {
338         m_notesJSObjIsSet = value;
339         m_notesObj = initObj;
340 }
341
342 bool Contact::notesJSObjIsSet() const
343 {
344         return m_notesJSObjIsSet;
345 }
346
347 JSObjectRef Contact::getNotesJSObj()
348 {
349         return m_notesObj;
350 }
351
352 JSValueRef Contact::getNotesJSValue()
353 {
354         return m_notesJSValue;
355 }
356
357 void Contact::setGroupIdsJSObj(bool value, JSObjectRef initObj)
358 {
359         m_groupIdsJSObjIsSet = value;
360         m_groupIdsObj = initObj;
361 }
362
363 bool Contact::groupIdsJSObjIsSet() const
364 {
365         return m_groupIdsJSObjIsSet;
366 }
367
368 JSObjectRef Contact::getGroupIdsJSObj()
369 {
370         return m_groupIdsObj;
371 }
372
373 JSValueRef Contact::getGroupIdsJSValue()
374 {
375         return m_groupIdsJSValue;
376 }
377
378 void Contact::setContext(JSContextRef contextRef)
379 {
380         if(m_context == NULL)
381                 m_context = contextRef;
382 }
383
384 JSContextRef Contact::getContext()
385 {
386         return m_context;
387 }
388
389 } // Contact
390 } // DeviceAPI