[Contact] Refactoring API structures
[framework/web/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / AddressBook.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file        AddressBook.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief
22  */
23
24 #include <ctime>
25 #include <cstdlib>
26 #include <cstddef>
27 #include <fstream>
28 #include <sstream>
29 #include <pcrecpp.h>
30 #include <dpl/log/log.h>
31 #include <dpl/exception.h>
32 #include <Commons/Exception.h>
33 #include <Commons/Regex.h>
34 #include <API/Contact/IAddressBook.h>
35 #include <API/Contact/IContact.h>
36 #include "AddressBook.h"
37 #include "Contact.h"
38 #include "ContactObjectA2PConverter.h"
39 #include "ContactObjectP2AConverter.h"
40 #include "ContactSearchEngine.h"
41 #include "ContactFilterValidator.h"
42 #include "ContactDownloadManager.h"
43 #include "ContactUtility.h"
44
45 namespace TizenApis {
46 namespace Platform {
47 namespace Contact {
48
49 using namespace TizenApis::Api::Contact;
50 using namespace TizenApis::Api::Tizen;
51 using namespace WrtDeviceApis::Commons;
52 using namespace std;
53
54 AddressBook::AddressBook() :
55                 IAddressBook(),
56                 m_name("TEST_ADDRESS_BOOK")
57 {
58     LogDebug("entered");
59 }
60
61 AddressBook::~AddressBook()
62 {
63         if(m_addressBookEmitters.size() != 0)
64         {
65                 ContactListenerManagerSingleton::Instance().unregisterAppListChangedCallbacks(this);
66         }
67 }
68
69 std::string AddressBook::getId() const
70 {
71         std::stringstream oss;
72         oss << m_id;
73         return oss.str();
74 }
75
76 void AddressBook::setId(const std::string &value)
77 {
78         std::stringstream oss(value);
79         oss >> m_id;
80 }
81
82 std::string AddressBook::getAccountId() const
83 {
84         std::stringstream oss;
85         oss << m_accountId;
86         return oss.str();
87 }
88
89 void AddressBook::setAccountId(const std::string &value)
90 {
91         std::stringstream oss(value);
92         oss >> m_accountId;
93 }
94
95 void AddressBook::OnRequestReceived(const EventAddressBookGetPtr &event)
96 {
97         LogDebug("entered");
98
99         CTSstruct *ctsStruct = NULL;
100         Try     {
101                 if(!event->getIdIsSet())
102                         ThrowMsg(InvalidArgumentException, "Invalid contact id");
103
104                 string contactId = event->getId();
105
106                 int contactIdInt = ContactUtility::strToInt(contactId);
107
108                 int errorCode = contacts_svc_get_contact(contactIdInt, &ctsStruct);
109                 if(errorCode != CTS_SUCCESS)
110                 {
111                         ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ")");
112                 }
113                 ContactObjectP2AConverterPtr contactObjConverter(
114                                 new ContactObjectP2AConverter(ctsStruct, false));
115                 ContactPtr contact = contactObjConverter->getAbstractContact();
116
117                 event->setContact(contact);
118                 event->setResult(true);
119
120         } Catch (NotFoundException) {
121                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
122                 event->setExceptionCode(ExceptionCodes::NotFoundException);
123                 event->setResult(false);
124
125         } Catch (PlatformException) {
126                 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
127                 event->setExceptionCode(ExceptionCodes::PlatformException);
128                 event->setResult(false);
129
130         } Catch (InvalidArgumentException) {
131                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
132                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
133                 event->setResult(false);
134
135         } Catch(Exception) {
136                 LogError("Error on platform : " << _rethrown_exception.GetMessage());
137                 event->setExceptionCode(ExceptionCodes::UnknownException);
138                 event->setResult(false);
139         }
140
141         if(ctsStruct != NULL)
142                 contacts_svc_struct_free(ctsStruct);
143 }
144
145 void AddressBook::OnRequestReceived(const EventAddressBookAddPtr &event)
146 {
147         LogDebug("entered");
148
149         ContactPtr contact(NULL);
150
151         Try {
152                 if(!event->getContactIsSet())
153                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
154
155                 contact = event->getContact();
156                 if(!contact)
157                         ThrowMsg(InvalidArgumentException, "No contacts.");
158
159         } Catch(InvalidArgumentException) {
160                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
161                 event->setResult(false);
162                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
163                 return;
164         }
165
166         CTSstruct *ctsStruct = NULL;
167
168         Try
169         {
170                 internalAddContact(contact);
171
172                 string id = contact->getId();
173                 if(id != "")
174                 {
175                         int idInt = ContactUtility::strToInt(id);
176                         int errorCode = contacts_svc_get_contact(idInt, &ctsStruct);
177                         if(errorCode != CTS_SUCCESS)
178                         {
179                                 ThrowMsg(PlatformException, "No contact just inserted  (id:" << id << ")");
180                         }
181                         ContactObjectP2AConverterPtr contactObjConverter(
182                                         new ContactObjectP2AConverter(ctsStruct, false));
183                         ContactPtr insertedContact = contactObjConverter->getAbstractContact();
184                         contact->copy(insertedContact);
185                 }
186
187                 event->setResult(true);
188                 event->setExceptionCode(ExceptionCodes::None);
189         }
190         Catch (PlatformException) {
191                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
192                 event->setResult(false);
193                 event->setExceptionCode(ExceptionCodes::PlatformException);
194         }
195         Catch (Exception) {
196                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
197                 event->setResult(false);
198                 event->setExceptionCode(ExceptionCodes::UnknownException);
199         }
200
201         if(ctsStruct != NULL)
202                 contacts_svc_struct_free(ctsStruct);
203 }
204
205 void AddressBook::OnRequestReceived(const EventAddressBookAddBatchPtr &event)
206 {
207         LogDebug("entered");
208         ContactArrayPtr contacts(NULL);
209
210         Try {
211                 if(!event->getContactsIsSet())
212                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
213
214                 contacts = event->getContacts();
215                 if(!contacts)
216                         ThrowMsg(InvalidArgumentException, "No contacts.");
217
218         } Catch(InvalidArgumentException) {
219                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
220                 event->setResult(false);
221                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
222                 return;
223         }
224
225         CTSstruct *ctsStruct = NULL;
226
227         Try
228         {
229                 int ret = contacts_svc_begin_trans();
230                 if (ret < 0) {
231                         LogError("error code " << ret);
232                         ThrowMsg(PlatformException, "Error during executing contacts_svc_begin_trans()");
233                 }
234
235                 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
236                 {
237                         ContactPtr contact = *i;
238
239                         internalAddContact(contact);
240                 }
241                 ret = contacts_svc_end_trans(true);
242                 if (ret < 0) {
243                         LogError("error code " << ret);
244                         ThrowMsg(PlatformException, "Error during executing contacts_svc_end_trans()");
245                 }
246
247                 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
248                 {
249                         ContactPtr contact = *i;
250
251                         string id = contact->getId();
252                         if(id != "")
253                         {
254                                 int idInt = ContactUtility::strToInt(id);
255                                 int errorCode = contacts_svc_get_contact(idInt, &ctsStruct);
256                                 if(errorCode != CTS_SUCCESS)
257                                 {
258                                         ThrowMsg(PlatformException, "No contact just inserted  (id:" << id << ")");
259                                 }
260                                 ContactObjectP2AConverterPtr contactObjConverter(
261                                                 new ContactObjectP2AConverter(ctsStruct, false));
262                                 ContactPtr insertedContact = contactObjConverter->getAbstractContact();
263                                 contact->copy(insertedContact);
264                         }
265                 }
266
267                 event->setResult(true);
268                 event->setExceptionCode(ExceptionCodes::None);
269         }
270         Catch (NotFoundException)
271         {
272                 contacts_svc_end_trans(false);
273                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
274                 event->setResult(false);
275                 event->setExceptionCode(ExceptionCodes::NotFoundException);
276         }
277         Catch (PlatformException) {
278                 contacts_svc_end_trans(false);
279                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
280                 event->setResult(false);
281                 event->setExceptionCode(ExceptionCodes::PlatformException);
282         }
283         Catch (Exception) {
284                 contacts_svc_end_trans(false);
285                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
286                 event->setResult(false);
287                 event->setExceptionCode(ExceptionCodes::PlatformException);
288         }
289
290         if(ctsStruct != NULL)
291                 contacts_svc_struct_free(ctsStruct);
292 }
293
294 void AddressBook::OnRequestReceived(const EventAddressBookUpdatePtr &event)
295 {
296         ContactPtr contact(NULL);
297
298         Try {
299                 if(!event->getContactIsSet())
300                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
301
302                 contact = event->getContact();
303                 if(!contact)
304                         ThrowMsg(InvalidArgumentException, "No contacts.");
305
306         } Catch(InvalidArgumentException) {
307                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
308                 event->setResult(false);
309                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
310                 return;
311         }
312
313         Try
314         {
315                 internalAddContact(contact);
316         }
317         Catch (NotFoundException)
318         {
319                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
320                 event->setResult(false);
321                 event->setExceptionCode(ExceptionCodes::NotFoundException);
322                 return;
323         }
324         Catch (PlatformException) {
325                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
326                 event->setResult(false);
327                 event->setExceptionCode(ExceptionCodes::PlatformException);
328                 return;
329         }
330         Catch (Exception) {
331                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
332                 event->setResult(false);
333                 event->setExceptionCode(ExceptionCodes::PlatformException);
334                 return;
335         }
336         //m_latestVersion = get_contact_version();
337
338         event->setResult(true);
339         event->setExceptionCode(ExceptionCodes::None);
340 }
341
342 void AddressBook::OnRequestReceived(const EventAddressBookUpdateBatchPtr &event)
343 {
344         LogDebug("entered");
345         ContactArrayPtr contacts(NULL);
346
347         Try {
348                 if(!event->getContactsIsSet())
349                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
350
351                 contacts = event->getContacts();
352                 if(!contacts)
353                         ThrowMsg(InvalidArgumentException, "No contacts.");
354
355         } Catch(InvalidArgumentException) {
356                 LogError("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
357                 event->setResult(false);
358                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
359                 return;
360         }
361
362         Try
363         {
364                 int ret = contacts_svc_begin_trans();
365                 if (ret < 0) {
366                         LogError("error code " << ret);
367                         ThrowMsg(PlatformException, "Error during executing contacts_svc_begin_trans()");
368                 }
369
370                 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
371                 {
372                         ContactPtr contact = *i;
373
374                         internalAddContact(contact);
375                 }
376                 contacts_svc_end_trans(true);
377                 if (ret < 0) {
378                         LogError("error code " << ret);
379                         ThrowMsg(PlatformException, "Error during executing contacts_svc_end_trans()");
380                 }
381         }
382         Catch (NotFoundException)
383         {
384                 contacts_svc_end_trans(false);
385                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
386                 event->setResult(false);
387                 event->setExceptionCode(ExceptionCodes::NotFoundException);
388                 return;
389         }
390         Catch (PlatformException) {
391                 contacts_svc_end_trans(false);
392                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
393                 event->setResult(false);
394                 event->setExceptionCode(ExceptionCodes::PlatformException);
395                 return;
396         }
397         Catch (Exception) {
398                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
399                 event->setResult(false);
400                 event->setExceptionCode(ExceptionCodes::PlatformException);
401                 return;
402         }
403         //m_latestVersion = get_contact_version();
404
405         event->setResult(true);
406         event->setExceptionCode(ExceptionCodes::None);
407 }
408
409 void AddressBook::OnRequestReceived(const EventAddressBookRemovePtr &event)
410 {
411         LogDebug("entered");
412         int contactId;
413
414         Try
415         {
416                 if(!event->getContactIdIsSet())
417                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
418
419                 string contactIdStr = event->getContactId();
420
421                 if(!validate("^[0-9]+$", contactIdStr, VALIDATE_MATCH_CASELESS))
422                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
423
424                 try {
425                         istringstream iss(contactIdStr);
426                         iss >> contactId;
427                 } catch (...) {
428                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
429                 }
430
431                 internalDeleteContactById(contactId);
432         }
433         Catch (InvalidArgumentException)
434         {
435                 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
436                 event->setResult(false);
437                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
438                 return;
439         }
440         Catch (NotFoundException)
441         {
442                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
443                 event->setResult(false);
444                 event->setExceptionCode(ExceptionCodes::NotFoundException);
445                 return;
446         }
447         Catch (PlatformException) {
448                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
449                 event->setResult(false);
450                 event->setExceptionCode(ExceptionCodes::PlatformException);
451                 return;
452         }
453         Catch (Exception) {
454                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
455                 event->setResult(false);
456                 event->setExceptionCode(ExceptionCodes::PlatformException);
457                 return;
458         }
459         //m_latestVersion = get_contact_version();
460
461         event->setResult(true);
462         event->setExceptionCode(ExceptionCodes::None);
463 }
464
465 void AddressBook::OnRequestReceived(const EventAddressBookRemoveBatchPtr &event)
466 {
467         LogDebug("entered");
468         StringArrayPtr contactIds(NULL);
469
470         Try {
471                 if(!event->getContactIdsIsSet())
472                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
473
474                 contactIds = event->getContactIds();
475                 if(!contactIds)
476                         ThrowMsg(InvalidArgumentException, "No contacts.");
477
478         } Catch(InvalidArgumentException) {
479                 LogError("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
480                 event->setResult(false);
481                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
482                 return;
483         }
484
485         Try
486         {
487                 int ret = contacts_svc_begin_trans();
488                 if (ret < 0) {
489                         LogError("error code " << ret);
490                         ThrowMsg(PlatformException, "Error during executing contacts_svc_begin_trans()");
491                 }
492                 for(StringArray::iterator i = contactIds->begin(); i != contactIds->end(); i++)
493                 {
494                         string contactIdStr = *i;
495
496                         int contactId;
497
498                         if(!validate("^[0-9]+$", contactIdStr, VALIDATE_MATCH_CASELESS))
499                                 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
500
501                         try {
502                                 istringstream iss(contactIdStr);
503                                 iss >> contactId;
504                         } catch (...) {
505                                 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
506                         }
507
508                         internalDeleteContactById(contactId);
509                 }
510                 contacts_svc_end_trans(true);
511                 if (ret < 0) {
512                         LogError("error code " << ret);
513                         ThrowMsg(PlatformException, "Error during executing contacts_svc_end_trans()");
514                 }
515         }
516         Catch (InvalidArgumentException)
517         {
518                 contacts_svc_end_trans(false);
519                 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
520                 event->setResult(false);
521                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
522                 return;
523         }
524         Catch (NotFoundException)
525         {
526                 contacts_svc_end_trans(false);
527                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
528                 event->setResult(false);
529                 event->setExceptionCode(ExceptionCodes::NotFoundException);
530                 return;
531         }
532         Catch (PlatformException) {
533                 contacts_svc_end_trans(false);
534                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
535                 event->setResult(false);
536                 event->setExceptionCode(ExceptionCodes::PlatformException);
537                 return;
538         }
539         Catch (Exception) {
540                 contacts_svc_end_trans(false);
541                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
542                 event->setResult(false);
543                 event->setExceptionCode(ExceptionCodes::PlatformException);
544                 return;
545         }
546         //m_latestVersion = get_contact_version();
547
548         event->setResult(true);
549         event->setExceptionCode(ExceptionCodes::None);
550 }
551
552 void AddressBook::OnRequestReceived(const EventAddressBookFindPtr &event)
553 {
554         LogDebug("entered");
555         Try     {
556                 ContactSearchEnginePtr searchEngine(new ContactSearchEngine(ContactSearchEngine::CONTACT_QUERY));
557
558                 if(event->getFilterIsSet())
559                 {
560                         FilterPtr filter = event->getFilter();
561
562                         FilterValidatorPtr validator = ContactFilterValidatorFactory::getContactFilterValidator();
563                         bool success = filter->validate(validator);
564
565                         if(!success)
566                                 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
567
568                         searchEngine->setCondition(filter);
569                 }
570
571                 // Currently not support attributeOfInterests
572                 //if (event->getAttributesOfInterestIsSet())
573                 //      searchEngine->setAttributeOfInterest(event->getAttributesOfInterest());
574                 //else
575                 //      searchEngine->setAttributeOfInterest();
576
577                 searchEngine->setAttributeOfInterest();
578
579                 if (event->getSortModeIsSet())
580                 {
581                         searchEngine->setSortMode(event->getSortMode());
582                 }
583                 else
584                         searchEngine->setSortMode();
585
586                 event->setContacts(searchEngine->getContactSearchResult());
587                 event->setResult(true);
588
589         } Catch (NotFoundException) {
590                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
591                 event->setExceptionCode(ExceptionCodes::NotFoundException);
592                 event->setResult(false);
593
594         } Catch (PlatformException) {
595                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
596                 event->setExceptionCode(ExceptionCodes::PlatformException);
597                 event->setResult(false);
598
599         } Catch (InvalidArgumentException) {
600                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
601                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
602                 event->setResult(false);
603
604         } Catch (Exception) {
605                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
606                 event->setExceptionCode(ExceptionCodes::PlatformException);
607                 event->setResult(false);
608         }
609 }
610
611 void AddressBook::OnRequestReceived(const EventAddressBookGetCategoriesPtr &event)
612 {
613         LogDebug("entered");
614
615         Try     {
616                 StringArrayPtr categories = StringArrayPtr(new StringArray());
617
618                 CTSiter *iter = NULL;
619                 if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_GROUP, &iter))
620                         ThrowMsg(PlatformException, "Fail to get categories list.");
621
622                 while(CTS_SUCCESS == contacts_svc_iter_next(iter))
623                 {
624                         CTSvalue *group = contacts_svc_iter_get_info(iter);
625                         if(group == NULL)
626                                 break;
627
628                         const char *groupCStr = contacts_svc_value_get_str(group, CTS_LIST_GROUP_NAME_STR);
629                         if(groupCStr)
630                                 categories->push_back(string(groupCStr));
631
632                         contacts_svc_value_free(group);
633                 }
634                 contacts_svc_iter_remove(iter);
635
636                 event->setCategories(categories);
637                 event->setResult(true);
638
639         } Catch (NotFoundException) {
640                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
641                 event->setExceptionCode(ExceptionCodes::NotFoundException);
642                 event->setResult(false);
643
644         } Catch (PlatformException) {
645                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
646                 event->setExceptionCode(ExceptionCodes::PlatformException);
647                 event->setResult(false);
648
649         } Catch (InvalidArgumentException) {
650                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
651                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
652                 event->setResult(false);
653
654         } Catch(Exception) {
655                 LogError("Error on platform : " << _rethrown_exception.GetMessage());
656                 event->setExceptionCode(ExceptionCodes::UnknownException);
657                 event->setResult(false);
658         }
659 }
660
661 void AddressBook::OnRequestReceived(const EventAddressBookAddChangeListenerPtr &event)
662 {
663         LogDebug("entered");
664
665         Try     {
666                 if(!event->getEmitterIsSet())
667                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
668
669                 EventAddressBookChangeListenerEmitterPtr emitter = event->getEmitter();
670                 if(emitter == NULL)
671                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
672
673                 //DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
674
675                 if(m_addressBookEmitters.size() == 0)
676                 {
677                         LogDebug("Watch registered initially");
678
679                         ContactListenerManagerSingleton::Instance().registerAppListChangedCallbacks(this, m_id);
680                 }
681
682                 m_addressBookEmitters.attach(emitter);
683
684                 long id = ContactListenerManagerSingleton::Instance().getWatchIdAndInc();
685                 m_watchIdMap[id] = emitter->getId();
686
687             event->setId(id);
688                 event->setResult(true);
689
690         } Catch (NotFoundException) {
691                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
692                 event->setExceptionCode(ExceptionCodes::NotFoundException);
693                 event->setResult(false);
694
695         } Catch (PlatformException) {
696                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
697                 event->setExceptionCode(ExceptionCodes::PlatformException);
698                 event->setResult(false);
699
700         } Catch (InvalidArgumentException) {
701                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
702                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
703                 event->setResult(false);
704
705         } Catch (Exception) {
706                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
707                 event->setExceptionCode(ExceptionCodes::PlatformException);
708                 event->setResult(false);
709         }
710 }
711
712 void AddressBook::OnRequestReceived(const EventAddressBookRemoveChangeListenerPtr &event)
713 {
714         LogDebug("entered");
715
716         Try     {
717                 if(!event->getIdIsSet())
718                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
719
720                 long id = event->getId();
721                 if(m_watchIdMap.find(id) == m_watchIdMap.end())
722                         ThrowMsg(NotFoundException, "No watchId : " << id);
723
724                 //DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
725                 bool success = m_addressBookEmitters.detach(m_watchIdMap[id]);
726                 if(!success)
727                         ThrowMsg(NotFoundException, "No watchId : " << id);
728
729                 m_watchIdMap.erase(id);
730
731                 if(m_addressBookEmitters.size() == 0)
732                 {
733                         LogDebug("No watcher is registered. unsubscribing from contact service.");
734
735                         ContactListenerManagerSingleton::Instance().unregisterAppListChangedCallbacks(this);
736                 }
737
738                 event->setResult(true);
739
740         } Catch (NotFoundException) {
741                 LogError("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
742                 event->setExceptionCode(ExceptionCodes::NotFoundException);
743                 event->setResult(false);
744
745         } Catch (PlatformException) {
746                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
747                 event->setExceptionCode(ExceptionCodes::PlatformException);
748                 event->setResult(false);
749
750         } Catch (InvalidArgumentException) {
751                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
752                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
753                 event->setResult(false);
754
755         } Catch (Exception) {
756                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
757                 event->setExceptionCode(ExceptionCodes::PlatformException);
758                 event->setResult(false);
759         }
760 }
761
762 void AddressBook::onContactEventAdded(TizenApis::Api::Contact::ContactArrayPtr &contacts)
763 {
764         EventInfoAddressBookChangeAddedPtr contactsAdded(new EventInfoAddressBookChangeAdded());
765         contactsAdded->setContacts(contacts);
766         EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsAdded);
767         EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
768         m_addressBookEmitters.emit(listener);
769 }
770
771 void AddressBook::onContactEventUpdated(TizenApis::Api::Contact::ContactArrayPtr &contacts)
772 {
773         EventInfoAddressBookChangeUpdatedPtr contactsUpdated(new EventInfoAddressBookChangeUpdated());
774         contactsUpdated->setContacts(contacts);
775         EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsUpdated);
776         EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
777         m_addressBookEmitters.emit(listener);
778 }
779
780 void AddressBook::onContactEventRemoved(TizenApis::Api::Contact::StringArrayPtr &contactIds)
781 {
782         EventInfoAddressBookChangeRemovedPtr contactsRemoved(new EventInfoAddressBookChangeRemoved());
783         contactsRemoved->setContactIds(contactIds);
784         EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsRemoved);
785         EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
786         m_addressBookEmitters.emit(listener);
787 }
788
789 void AddressBook::internalAddContact(const ContactPtr &newContact)
790 {
791         LogDebug("entered");
792
793         DPL::Mutex::ScopedLock lock(&m_contactEditingMutex);
794
795         //translate provided path to real filesystem path
796         if (newContact->getPhotoURIIsSet())
797         {
798                 string realPath = ContactDownloadManager::getInstance()->getRealPath(newContact->getPhotoURI());
799                 if(realPath.length() <= MAXPATHLEN)
800                         newContact->setPhotoURI(realPath);
801         }
802
803         if (newContact->getRingtoneURIIsSet())
804         {
805                 string realPath = ContactDownloadManager::getInstance()->getRealPath(newContact->getRingtoneURI());
806                 if(realPath.length() <= MAXPATHLEN)
807                         newContact->setRingtoneURI(realPath);
808         }
809
810         DPL::SharedPtr<TizenApis::Platform::Contact::Contact> newContactT =
811                         DPL::StaticPointerCast<TizenApis::Platform::Contact::Contact>(newContact);
812
813         ContactObjectA2PConverterPtr contactObjConverter(NULL);
814
815         CTSstruct* contact = newContactT->getCTSStruct();
816         if(contact == NULL)
817         {
818                 contactObjConverter = ContactObjectA2PConverterPtr(
819                                 new ContactObjectA2PConverter(newContact, false) );
820                 contact = contactObjConverter->getPlatformContact();
821         }
822         else
823         {
824                 contactObjConverter = ContactObjectA2PConverterPtr(
825                                 new ContactObjectA2PConverter(newContact, false, contact) );
826                 contact = contactObjConverter->getPlatformContact();
827         }
828
829         if (!contact)
830         {
831                 LogError("Error during converting contact object");
832                 ThrowMsg(PlatformException, "Error during converting contact object");
833         }
834
835         if ( newContact->getIdIsSet() )
836         {
837                 int ret = contacts_svc_update_contact(contact);
838                 if (ret == CTS_ERR_DB_RECORD_NOT_FOUND)
839                         ThrowMsg(NotFoundException, "Error during executing contacts_svc_update_contact()");
840                 else if (ret < 0) {
841                         LogError("error code " << ret);
842                         ThrowMsg(PlatformException, "Error during executing contacts_svc_update_contact()");
843                 }
844         }
845         else
846         {
847                 string addressBookIdStr = newContact->getAddressBookId();
848                 int addressBookId = ContactUtility::strToInt(addressBookIdStr);
849
850                 int id = contacts_svc_insert_contact(addressBookId, contact);
851                 if (id < 0) {
852                         LogError("error code " << id);
853                         ThrowMsg(PlatformException, "Error during executing contacts_svc_insert_contact()");
854                 }
855                 newContact->setId(id);
856         }
857 }
858
859 void AddressBook::internalDeleteContactById(int id)
860 {
861         LogDebug("entered with id " << id);
862
863         if (id == -1)
864                 return;
865
866         DPL::Mutex::ScopedLock lock(&m_contactEditingMutex);
867
868         int res = contacts_svc_delete_contact(id);
869         if (res == CTS_ERR_DB_RECORD_NOT_FOUND)
870                 ThrowMsg(NotFoundException, "Error during executing contacts_svc_delete_contact()");
871         else if (res != CTS_SUCCESS)
872                 ThrowMsg(PlatformException, "Error during executing contacts_svc_delete_contact()");
873 }
874
875 } // Contact
876 } // Platform
877 } // TizenApis