wrt-plugins-tizen_0.4.23
[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 #include <dpl/exception.h>
27 #include <Commons/Exception.h>
28 #include <Commons/Regex.h>
29 #include "ContactObjectA2PConverter.h"
30 #include "ContactObjectP2AConverter.h"
31 #include <Logger.h>
32
33 namespace DeviceAPI {
34 namespace Contact {
35
36 using namespace WrtDeviceApis::Commons;
37 using namespace std;
38
39 Contact::Contact()
40         : IContact()
41         , m_platformContactObject(NULL)
42         , m_platformContactObjectList(NULL)
43 {
44 //      LoggerD("entered");
45
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;
54
55         m_numbersObj = NULL;
56         m_numbersJSValue = NULL;
57
58         m_emailsObj = NULL;
59         m_emailsJSValue = NULL;
60
61         m_anniversariesObj = NULL;
62         m_anniversariesJSValue = NULL;
63
64         m_urlsObj = NULL;
65         m_urlsJSValue = NULL;
66
67         m_addressesObj = NULL;
68         m_addressesJSValue = NULL;
69
70         m_organizationsObj = NULL;
71         m_organizationsJSValue = NULL;
72
73         m_groupIdsObj = NULL;
74         m_groupIdsJSValue = NULL;
75         m_notesObj = NULL;
76         m_notesJSValue = NULL;
77
78         m_context = NULL;
79
80 }
81
82 Contact::~Contact()
83 {
84         if (m_platformContactObjectList != NULL)
85                 contacts_list_destroy(m_platformContactObjectList, true);
86
87         if(numbersJSObjIsSet()){
88                 JSContextRef contextRef = getContext();
89                 JSObjectRef tempJSObject = getNumbersJSObj();
90                 JSValueUnprotect(contextRef, tempJSObject);
91         }
92
93         if(EmailsJSObjIsSet()){
94                 JSContextRef contextRef = getContext();
95                 JSObjectRef tempJSObject = getEmailsJSObj();
96                 JSValueUnprotect(contextRef, tempJSObject);
97         }
98
99         if(anniversariesJSObjIsSet()){
100                 JSContextRef contextRef = getContext();
101                 JSObjectRef tempJSObject = getAnniversariesJSObj();
102                 JSValueUnprotect(contextRef, tempJSObject);
103         }
104
105         if(urlsJSObjIsSet()){
106                 JSContextRef contextRef = getContext();
107                 JSObjectRef tempJSObject = getUrlsJSObj();
108                 JSValueUnprotect(contextRef, tempJSObject);
109         }
110
111         if(addressesJSObjIsSet()){
112                 JSContextRef contextRef = getContext();
113                 JSObjectRef tempJSObject = getAddressesJSObj();
114                 JSValueUnprotect(contextRef, tempJSObject);
115         }
116
117         if(organizationsJSObjIsSet()){
118                 JSContextRef contextRef = getContext();
119                 JSObjectRef tempJSObject = getOrganizationsJSObj();
120                 JSValueUnprotect(contextRef, tempJSObject);
121         }
122
123         if(groupIdsJSObjIsSet()){
124                 JSContextRef contextRef = getContext();
125                 JSObjectRef tempJSObject = getGroupIdsJSObj();
126                 JSValueUnprotect(contextRef, tempJSObject);
127         }
128
129         if(notesJSObjIsSet()){
130                 JSContextRef contextRef = getContext();
131                 JSObjectRef tempJSObject = getNotesJSObj();
132                 JSValueUnprotect(contextRef, tempJSObject);
133         }
134 }
135
136 string Contact::convertToString(const string &format)
137 {
138         LoggerD("entered");
139         int errorCode = 0;
140         char *vcard_stream = NULL;
141
142         if(!format.empty() && format != "VCARD_30")
143                 ThrowMsg(ConversionException, "format must be 'VCARD_30'");
144
145         ContactPtr thisObj = SharedFromThis();
146         ContactObjectA2PConverterPtr contactObjConverter(NULL);
147
148         contacts_record_h contacts_record = NULL;
149         Try {
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.");
154         }
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.");
158
159         string result((char *)vcard_stream);
160
161         free(vcard_stream);
162
163         return result;
164 }
165
166 void Contact::setContactFromString(const std::string &vObjectStr)
167 {
168         LoggerD("entered");
169         int errorCode = 0;
170         const char *vcard_stream = vObjectStr.c_str();
171
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.");
178
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)
182         {
183                 contacts_list_destroy(contacts_list, true);
184                 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (2)");
185         }
186
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)
191         {
192                 contacts_list_destroy(contacts_list, true);
193                 ThrowMsg(InvalidArgumentException, "Invalid vCard string. (3)");
194         }
195
196         ContactObjectP2AConverterPtr contactObjConverter(NULL);
197         Try {
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.");
203         }
204
205         m_platformContactObject = contacts_record;
206         m_platformContactObjectList = contacts_list;
207 }
208
209 void Contact::setNumbersJSObj(bool value, JSObjectRef initObj)
210 {
211         m_numbersJSObjIsSet = value;
212         m_numbersObj = initObj;
213 }
214
215 bool Contact::numbersJSObjIsSet() const
216 {
217         return m_numbersJSObjIsSet;
218 }
219
220 JSObjectRef Contact::getNumbersJSObj()
221 {
222         return m_numbersObj;
223 }
224
225 JSValueRef Contact::getNumbersJSValue()
226 {
227         return m_numbersJSValue;
228 }
229
230 void Contact::resetNumbersJSObj()
231 {
232         if(numbersJSObjIsSet()){
233                 JSContextRef contextRef = getContext();
234                 JSObjectRef tempJSObject = getNumbersJSObj();
235                 JSValueUnprotect(contextRef, tempJSObject);
236         }
237         m_numbersJSObjIsSet = false;
238         m_numbersObj = NULL;
239         m_numbersJSValue = NULL;
240 }
241
242 void Contact::setEmailsJSObj(bool value, JSObjectRef initObj)
243 {
244         m_emailsJSObjIsSet = value;
245         m_emailsObj = initObj;
246 }
247
248 bool Contact::EmailsJSObjIsSet() const
249 {
250         return m_emailsJSObjIsSet;
251 }
252
253 JSObjectRef Contact::getEmailsJSObj()
254 {
255         return m_emailsObj;
256 }
257
258 JSValueRef Contact::getEmailsJSValue()
259 {
260         return m_emailsJSValue;
261 }
262
263 void Contact::resetEmailsJSObj()
264 {
265         if(EmailsJSObjIsSet()){
266                 JSContextRef contextRef = getContext();
267                 JSObjectRef tempJSObject = getEmailsJSObj();
268                 JSValueUnprotect(contextRef, tempJSObject);
269         }
270         m_emailsJSObjIsSet = false;
271         m_emailsObj = NULL;
272         m_emailsJSValue = NULL;
273 }
274
275 void Contact::setAnniversariesJSObj(bool value, JSObjectRef initObj)
276 {
277         m_anniversariesJSObjIsSet = value;
278         m_anniversariesObj = initObj;
279 }
280
281 bool Contact::anniversariesJSObjIsSet() const
282 {
283         return m_anniversariesJSObjIsSet;
284 }
285
286 JSObjectRef Contact::getAnniversariesJSObj()
287 {
288         return m_anniversariesObj;
289 }
290
291 JSValueRef Contact::getAnniversariesJSValue()
292 {
293         return m_anniversariesJSValue;
294 }
295
296 void Contact::resetAnniversariesJSObj()
297 {
298         if(anniversariesJSObjIsSet()){
299                 JSContextRef contextRef = getContext();
300                 JSObjectRef tempJSObject = getAnniversariesJSObj();
301                 JSValueUnprotect(contextRef, tempJSObject);
302         }
303         m_anniversariesJSObjIsSet = false;
304         m_anniversariesObj = NULL;
305         m_anniversariesJSValue = NULL;
306 }
307
308 void Contact::setUrlsJSObj(bool value, JSObjectRef initObj)
309 {
310         m_urlsJSObjIsSet = value;
311         m_urlsObj = initObj;
312 }
313
314 bool Contact::urlsJSObjIsSet() const
315 {
316         return m_urlsJSObjIsSet;
317 }
318
319 JSObjectRef Contact::getUrlsJSObj()
320 {
321         return m_urlsObj;
322 }
323
324 JSValueRef Contact::getUrlsJSValue()
325 {
326         return m_urlsJSValue;
327 }
328
329 void Contact::resetUrlsJSObj()
330 {
331         if(urlsJSObjIsSet()){
332                 JSContextRef contextRef = getContext();
333                 JSObjectRef tempJSObject = getUrlsJSObj();
334                 JSValueUnprotect(contextRef, tempJSObject);
335         }
336         m_urlsJSObjIsSet = false;
337         m_urlsObj = NULL;
338         m_urlsJSValue = NULL;
339 }
340
341 void Contact::setAddressesJSObj(bool value, JSObjectRef initObj)
342 {
343         m_addressesJSObjIsSet = value;
344         m_addressesObj = initObj;
345 }
346
347 bool Contact::addressesJSObjIsSet() const
348 {
349         return m_addressesJSObjIsSet;
350 }
351
352 JSObjectRef Contact::getAddressesJSObj()
353 {
354         return m_addressesObj;
355 }
356
357 JSValueRef Contact::getAddressesJSValue()
358 {
359         return m_addressesJSValue;
360 }
361
362 void Contact::resetAddressesJSObj()
363 {
364         if(addressesJSObjIsSet()){
365                 JSContextRef contextRef = getContext();
366                 JSObjectRef tempJSObject = getAddressesJSObj();
367                 JSValueUnprotect(contextRef, tempJSObject);
368         }
369         m_addressesJSObjIsSet = false;
370         m_addressesObj = NULL;
371         m_addressesJSValue = NULL;
372 }
373
374 void Contact::setOrganizationsJSObj(bool value, JSObjectRef initObj)
375 {
376         m_organizationsJSObjIsSet = value;
377         m_organizationsObj = initObj;
378 }
379
380 bool Contact::organizationsJSObjIsSet() const
381 {
382         return m_organizationsJSObjIsSet;
383 }
384
385 JSObjectRef Contact::getOrganizationsJSObj()
386 {
387         return m_organizationsObj;
388 }
389
390 JSValueRef Contact::getOrganizationsJSValue()
391 {
392         return m_organizationsJSValue;
393 }
394
395 void Contact::resetOrganizationsJSObj()
396 {
397         if(organizationsJSObjIsSet()){
398                 JSContextRef contextRef = getContext();
399                 JSObjectRef tempJSObject = getOrganizationsJSObj();
400                 JSValueUnprotect(contextRef, tempJSObject);
401         }
402         m_organizationsJSObjIsSet = false;
403         m_organizationsObj = NULL;
404         m_organizationsJSValue = NULL;
405 }
406
407 void Contact::setNotesJSObj(bool value, JSObjectRef initObj)
408 {
409         m_notesJSObjIsSet = value;
410         m_notesObj = initObj;
411 }
412
413 bool Contact::notesJSObjIsSet() const
414 {
415         return m_notesJSObjIsSet;
416 }
417
418 JSObjectRef Contact::getNotesJSObj()
419 {
420         return m_notesObj;
421 }
422
423 JSValueRef Contact::getNotesJSValue()
424 {
425         return m_notesJSValue;
426 }
427
428 void Contact::resetNotesJSObj()
429 {
430         if(notesJSObjIsSet()){
431                 JSContextRef contextRef = getContext();
432                 JSObjectRef tempJSObject = getNotesJSObj();
433                 JSValueUnprotect(contextRef, tempJSObject);
434         }
435         m_notesJSObjIsSet = false;
436         m_notesObj = NULL;
437         m_notesJSValue = NULL;
438 }
439
440 void Contact::setGroupIdsJSObj(bool value, JSObjectRef initObj)
441 {
442         m_groupIdsJSObjIsSet = value;
443         m_groupIdsObj = initObj;
444 }
445
446 bool Contact::groupIdsJSObjIsSet() const
447 {
448         return m_groupIdsJSObjIsSet;
449 }
450
451 JSObjectRef Contact::getGroupIdsJSObj()
452 {
453         return m_groupIdsObj;
454 }
455
456 JSValueRef Contact::getGroupIdsJSValue()
457 {
458         return m_groupIdsJSValue;
459 }
460
461 void Contact::resetGroupIdsJSObj()
462 {
463         if(groupIdsJSObjIsSet()){
464                 JSContextRef contextRef = getContext();
465                 JSObjectRef tempJSObject = getGroupIdsJSObj();
466                 JSValueUnprotect(contextRef, tempJSObject);
467         }
468         m_groupIdsJSObjIsSet = false;
469         m_groupIdsObj = NULL;
470         m_groupIdsJSValue = NULL;
471 }
472
473 void Contact::setContext(JSContextRef contextRef)
474 {
475         if(m_context == NULL)
476                 m_context = contextRef;
477 }
478
479 JSContextRef Contact::getContext()
480 {
481         return m_context;
482 }
483
484 } // Contact
485 } // DeviceAPI