Update change log and spec for wrt-plugins-tizen_0.4.49
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactManager.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        ContactManager.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "ContactManager.h"
26 #include <contacts.h>
27 #include <string>
28 #include <Commons/Regex.h>
29 #include "ContactFactory.h"
30 #include "IAddressBook.h"
31 #include "ContactsSvcWrapper.h"
32 #include "ContactsSvcObjectConverter.h"
33 #include "ContactUtility.h"
34 #include "PersonFilterValidator.h"
35 #include "PersonSearchEngine.h"
36 #include "Person.h"
37 #include "ContactQueue.h"
38 #include <Logger.h>
39
40 namespace DeviceAPI {
41 namespace Contact {
42
43 using namespace std;
44 using namespace WrtDeviceApis::Commons;
45 using namespace DeviceAPI::Tizen;
46
47 ContactManager::ContactManager() : m_eventMapAcc(0)
48 {
49         ContactsSvcWrapperSingleton::Instance();
50 }
51
52 ContactManager::~ContactManager()
53 {
54         if(m_contactManagerEmitters.size() != 0)
55         {
56                 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterPersonsChangeListener(this);
57         }
58 }
59
60 void ContactManager::OnRequestReceived(const EventContactManagerGetAddressBookPtr &event)
61 {
62         LoggerD("entered");
63
64         Try
65         {
66                 std::string addressBookId = event->getAddressBookId();
67                 if(addressBookId.empty())
68                 {
69                         //when database is not opened then set result as failed
70                         ThrowMsg(NotFoundException, "invalid Address book");
71                 }
72
73                 AddressBookPtr addressBook =
74                                 ContactsSvcWrapperSingleton::Instance().getAddressBook(addressBookId);
75
76                 event->setAddressBook(addressBook);
77                 event->setResult(true);
78                 event->setExceptionCode(ExceptionCodes::None);
79         }
80         Catch(PlatformException)
81         {
82                 //when database is not opened then set result as failed
83                 LoggerE("Contact DB not opened : " << _rethrown_exception.GetMessage());
84                 event->setResult(false);
85                 event->setExceptionCode(ExceptionCodes::PlatformException);
86                 return;
87         }
88         Catch(NotFoundException)
89         {
90                 //when database is not opened then set result as failed
91                 LoggerE("Not found : " << _rethrown_exception.GetMessage());
92                 event->setResult(false);
93                 event->setExceptionCode(ExceptionCodes::NotFoundException);
94         }
95         Catch(Exception)
96         {
97                 LoggerE("Unknown exception : " << _rethrown_exception.GetMessage());
98                 event->setResult(false);
99                 event->setExceptionCode(ExceptionCodes::UnknownException);
100                 return;
101         }
102 }
103
104 void ContactManager::OnRequestReceived(const EventContactManagerGetAddressBooksPtr &event)
105 {
106         LoggerD("entered");
107
108         Try
109         {
110                 AddressBookArrayPtr addressBookArray =
111                                 ContactsSvcWrapperSingleton::Instance().getAddressBooks();
112
113                 event->setAddressBooks(addressBookArray);
114                 event->setResult(true);
115                 event->setExceptionCode(ExceptionCodes::None);
116         }
117         Catch (PlatformException)
118         {
119                 //when database is not opened then set result as failed
120                 LoggerE("Contact DB is not opened.");
121                 event->setResult(false);
122                 event->setExceptionCode(ExceptionCodes::PlatformException);
123                 return;
124         }
125         Catch (Exception)
126         {
127                 LoggerE("Unknown exception : " << _rethrown_exception.GetMessage());
128                 event->setResult(false);
129                 event->setExceptionCode(ExceptionCodes::UnknownException);
130                 return;
131         }
132 }
133
134 void ContactManager::OnRequestReceived(const EventContactManagerGetDefaultAddressBookPtr &event)
135 {
136         LoggerD("entered");
137
138         Try
139         {
140                 AddressBookPtr defaultAddressBook =
141                                 ContactsSvcWrapperSingleton::Instance().getDefaultAddressBook();
142
143                 event->setDefaultAddressBook(defaultAddressBook);
144                 event->setResult(true);
145                 event->setExceptionCode(ExceptionCodes::None);
146         }
147         Catch (PlatformException)
148         {
149                 //when database is not opened then set result as failed
150                 LoggerE("Contact DB is not opened.");
151                 event->setResult(false);
152                 event->setExceptionCode(ExceptionCodes::PlatformException);
153                 return;
154         }
155         Catch (Exception)
156         {
157                 LoggerE("Unknown exception : " << _rethrown_exception.GetMessage());
158                 event->setResult(false);
159                 event->setExceptionCode(ExceptionCodes::UnknownException);
160                 return;
161         }
162 }
163
164 void ContactManager::OnRequestReceived(const EventContactManagerGetUnifiedAddressBookPtr &event)
165 {
166         LoggerD("entered");
167
168         Try
169         {
170                 AddressBookPtr unifiedAddressBook =
171                                 ContactsSvcWrapperSingleton::Instance().getUnifiedAddressBook();
172
173                 event->setUnifiedAddressBook(unifiedAddressBook);
174                 event->setResult(true);
175                 event->setExceptionCode(ExceptionCodes::None);
176         }
177         Catch (PlatformException)
178         {
179                 //when database is not opened then set result as failed
180                 LoggerE("Contact DB is not opened.");
181                 event->setResult(false);
182                 event->setExceptionCode(ExceptionCodes::PlatformException);
183                 return;
184         }
185         Catch (Exception)
186         {
187                 LoggerE("Unknown exception : " << _rethrown_exception.GetMessage());
188                 event->setResult(false);
189                 event->setExceptionCode(ExceptionCodes::UnknownException);
190                 return;
191         }
192 }
193
194 void ContactManager::OnRequestReceived(const EventContactManagerGetPtr &event)
195 {
196         LoggerD("entered");
197
198         int errorCode = 0;
199         contacts_record_h contacts_record = NULL;
200
201         Try     {
202                 if(!event->getIdIsSet())
203                         ThrowMsg(InvalidArgumentException, "Invalid person");
204
205                 string personId = event->getId();
206
207                 int personIdInt = ContactUtility::strToInt(personId);
208
209                 errorCode = contacts_db_get_record(_contacts_person._uri, personIdInt, &contacts_record);
210                 if(errorCode != CONTACTS_ERROR_NONE)
211                 {
212                         ThrowMsg(NotFoundException, "No person : " << errorCode);
213                 }
214                 PersonPtr person = PersonPtr(new Person());
215
216                 ContactsSvcObjectConverter::convertToAbstract(contacts_record, person);
217
218                 event->setPerson(person);
219                 event->setResult(true);
220
221         } Catch (NotFoundException) {
222                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
223                 event->setExceptionCode(ExceptionCodes::NotFoundException);
224                 event->setResult(false);
225
226         } Catch (PlatformException) {
227                 LoggerE("Error during getting person : " << _rethrown_exception.GetMessage());
228                 event->setExceptionCode(ExceptionCodes::PlatformException);
229                 event->setResult(false);
230
231         } Catch (InvalidArgumentException) {
232                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
233                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
234                 event->setResult(false);
235
236         } Catch(Exception) {
237                 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
238                 event->setExceptionCode(ExceptionCodes::UnknownException);
239                 event->setResult(false);
240         }
241
242         if(contacts_record != NULL)
243                 contacts_record_destroy(contacts_record, true);
244 }
245
246 void ContactManager::OnRequestReceived(const EventContactManagerUpdatePtr &event)
247 {
248         LoggerD("entered");
249
250         int errorCode = 0;
251
252         contacts_record_h contacts_record = NULL;
253         PersonPtr person(NULL);
254
255         Try {
256                 if(!event->getPersonIsSet())
257                         ThrowMsg(InvalidArgumentException, "Persons were not set.");
258
259                 person = event->getPerson();
260                 if(!person)
261                         ThrowMsg(InvalidArgumentException, "No persons.");
262
263                 if(!person->getIdIsSet())
264                         ThrowMsg(InvalidArgumentException, "Person object is wrong");
265
266         } Catch(InvalidArgumentException) {
267                 LoggerE("Invalid arguments for updating persons : " << _rethrown_exception.GetMessage());
268                 event->setResult(false);
269                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
270                 return;
271         }
272
273         Try
274         {
275                 int id = ContactUtility::strToInt(person->getId());
276                 errorCode = contacts_db_get_record(_contacts_person._uri, id, &contacts_record);
277                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
278                         ThrowMsg(NotFoundException, "Error during executing contacts_db_get_record()");
279                 else if (errorCode != CONTACTS_ERROR_NONE) {
280                         LoggerE("error code : " << errorCode);
281                         ThrowMsg(PlatformException, "Error during executing contacts_db_get_record()");
282                 }
283
284                 ContactsSvcObjectConverter::convertToPlatform(person, contacts_record);
285
286                 errorCode = contacts_db_update_record(contacts_record);
287                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
288                         ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
289                 else if (errorCode != CONTACTS_ERROR_NONE) {
290                         LoggerE("error code : " << errorCode);
291                         ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
292                 }
293
294                 event->setResult(true);
295                 event->setExceptionCode(ExceptionCodes::None);
296         }
297         Catch (NotFoundException)
298         {
299                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
300                 event->setResult(false);
301                 event->setExceptionCode(ExceptionCodes::NotFoundException);
302         }
303         Catch (PlatformException) {
304                 LoggerE("Error during updating person : " << _rethrown_exception.GetMessage());
305                 event->setResult(false);
306                 event->setExceptionCode(ExceptionCodes::PlatformException);
307         }
308         Catch (Exception) {
309                 LoggerE("Error during display_contact_id person : " << _rethrown_exception.GetMessage());
310                 event->setResult(false);
311                 event->setExceptionCode(ExceptionCodes::PlatformException);
312         }
313
314         if(contacts_record != NULL)
315                 contacts_record_destroy(contacts_record, true);
316
317         return;
318 }
319
320 void ContactManager::managerUpdateBatch(const EventContactManagerUpdateBatchPtr &event)
321 {
322         LoggerD("entered");
323         int errorCode = 0;
324         contacts_list_h contacts_list = NULL;
325         PersonArrayPtr persons(NULL);
326
327         if(!event->getPersonsIsSet())
328                 ThrowMsg(InvalidArgumentException, "Persons were not set.");
329
330         persons = event->getPersons();
331         if(!persons)
332                 ThrowMsg(InvalidArgumentException, "No persons.");
333
334         errorCode = contacts_list_create(&contacts_list);
335         if(errorCode != CONTACTS_ERROR_NONE){
336                 if(contacts_list != NULL)
337                         contacts_list_destroy(contacts_list, false);
338                 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
339         }
340
341         for(PersonArray::iterator i = persons->begin(); i != persons->end(); i++)
342         {
343                 contacts_record_h contacts_record = NULL;
344                 PersonPtr person = *i;
345
346                 Try
347                 {
348                         int personIdInt = ContactUtility::strToInt(person->getId());
349                         errorCode = contacts_db_get_record(_contacts_person._uri, personIdInt, &contacts_record);
350                         if(errorCode != CONTACTS_ERROR_NONE)
351                         {
352                                 ThrowMsg(NotFoundException, "No person");
353                         }
354
355                         ContactsSvcObjectConverter::convertToPlatform(person, contacts_record);
356
357                         errorCode = contacts_list_add(contacts_list, contacts_record);
358                         if(errorCode != CONTACTS_ERROR_NONE)
359                         {
360                                 ThrowMsg(PlatformException, "Error during add to list");
361                         }
362                 }
363                 Catch(NotFoundException)
364                 {
365                         if(contacts_list != NULL)
366                                 contacts_list_destroy(contacts_list, false);
367                         ThrowMsg(NotFoundException, "Error during converting contact object");
368 //                              LoggerE("Error during converting person object : " << _rethrown_exception.GetMessage());
369 //                              continue;
370                 }
371                 Catch(PlatformException)
372                 {
373                         if(contacts_list != NULL)
374                                 contacts_list_destroy(contacts_list, false);
375                         ThrowMsg(NotFoundException, "Error during converting contact object");
376 //                              LoggerE("Error during converting person object : " << _rethrown_exception.GetMessage());
377 //                              continue;
378                 }
379         }
380
381         KeySharePtrPair *keyPair = new KeySharePtrPair();
382         keyPair->key = m_eventMapAcc;
383         keyPair->contactManager = this;
384         errorCode = contacts_db_update_records_async(contacts_list, personsUpdateBatchResultCallback, (void*)keyPair);
385         if(errorCode != CONTACTS_ERROR_NONE)
386         {
387                 delete keyPair;
388                 if(contacts_list != NULL)
389                         contacts_list_destroy(contacts_list, false);
390                 ThrowMsg(PlatformException, "Error during contacts_db_update_records_async");
391         }
392
393         errorCode = contacts_list_destroy(contacts_list, true);
394         contacts_list = NULL;
395         if(errorCode != CONTACTS_ERROR_NONE)
396         {
397                 delete keyPair;
398                 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
399         }
400
401         pair<long, EventContactManagerUpdateBatchPtr> keyEventPair(m_eventMapAcc, event);
402         m_updateBatchEventMap.insert(keyEventPair);
403
404         m_eventMapAcc++;
405 }
406
407 void ContactManager::OnRequestReceived(const EventContactManagerUpdateBatchPtr &event)
408 {
409         LoggerD("entered");
410
411         Try
412         {
413                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
414                 if(isEmpty){
415                         managerUpdateBatch(event);
416                 }else{
417                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::UPATEBATCH, this, event);
418                 }
419                 ContactQueueManagerSingleton::Instance().increaseQueueList();
420                 event->switchToManualAnswer();
421         }
422         Catch (NotFoundException)
423         {
424                 LoggerE("NotFoundException during updating persons : " << _rethrown_exception.GetMessage());
425                 event->setResult(false);
426                 event->setExceptionCode(ExceptionCodes::NotFoundException);
427         }
428         Catch (PlatformException)
429         {
430                 LoggerE("PlatformException during updating persons : " << _rethrown_exception.GetMessage());
431                 event->setResult(false);
432                 event->setExceptionCode(ExceptionCodes::PlatformException);
433         }
434         Catch(InvalidArgumentException)
435         {
436                 LoggerE("InvalidArgumentException during updating persons : " << _rethrown_exception.GetMessage());
437                 event->setResult(false);
438                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
439         }
440         Catch (Exception)
441         {
442                 LoggerE("Error during updating persons : " << _rethrown_exception.GetMessage());
443                 event->setResult(false);
444                 event->setExceptionCode(ExceptionCodes::PlatformException);
445         }
446 }
447
448 void ContactManager::OnRequestReceived(const EventContactManagerRemovePtr &event)
449 {
450         LoggerD("entered");
451         int personId;
452
453         Try
454         {
455                 if(!event->getPersonIdIsSet())
456                         ThrowMsg(InvalidArgumentException, "Persons were not set.");
457
458                 string personIdStr = event->getPersonId();
459
460                 if(!validate("^[0-9]+$", personIdStr, VALIDATE_MATCH_CASELESS))
461                         ThrowMsg(InvalidArgumentException, "invalid" );
462
463                 try {
464                         istringstream iss(personIdStr);
465                         iss >> personId;
466                 } catch (...) {
467                         ThrowMsg(InvalidArgumentException, "wrong" );
468                 }
469
470                 int errorCode = contacts_db_delete_record(_contacts_person._uri, personId);
471                 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
472                         ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
473                 else if (errorCode != CONTACTS_ERROR_NONE)
474                         ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
475
476                 event->setResult(true);
477                 event->setExceptionCode(ExceptionCodes::None);
478         }
479         Catch (InvalidArgumentException)
480         {
481                 LoggerE("Invalid person : " << _rethrown_exception.GetMessage());
482                 event->setResult(false);
483                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
484                 return;
485         }
486         Catch (NotFoundException)
487         {
488                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
489                 event->setResult(false);
490                 event->setExceptionCode(ExceptionCodes::NotFoundException);
491                 return;
492         }
493         Catch (PlatformException) {
494                 LoggerE("Error during removing person : " << _rethrown_exception.GetMessage());
495                 event->setResult(false);
496                 event->setExceptionCode(ExceptionCodes::PlatformException);
497                 return;
498         }
499         Catch (Exception) {
500                 LoggerE("Error during removing person : " << _rethrown_exception.GetMessage());
501                 event->setResult(false);
502                 event->setExceptionCode(ExceptionCodes::PlatformException);
503                 return;
504         }
505 }
506
507 void ContactManager::managerRemoveBatch(const EventContactManagerRemoveBatchPtr &event)
508 {
509         LoggerD("entered");
510         int errorCode = 0;
511         StringArrayPtr personIds(NULL);
512
513         if(!event->getPersonIdsIsSet())
514                 ThrowMsg(InvalidArgumentException, "Person IDs were not set.");
515
516         personIds = event->getPersonIds();
517         if(!personIds)
518                 ThrowMsg(InvalidArgumentException, "No person.");
519
520         int *ids = new int[personIds->size()];
521         int count = 0;
522
523         if(errorCode != CONTACTS_ERROR_NONE)
524         {
525                 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
526         }
527
528         for(StringArray::iterator i = personIds->begin(); i != personIds->end(); i++)
529         {
530                 string personIdStr = *i;
531
532                 Try
533                 {
534                         int personId;
535
536                         if(!validate("^[0-9]+$", personIdStr, VALIDATE_MATCH_CASELESS))
537                                 ThrowMsg(InvalidArgumentException, "invalid" );
538
539                         try
540                         {
541                                 istringstream iss(personIdStr);
542                                 iss >> personId;
543                         }
544                         catch (...)
545                         {
546                                 ThrowMsg(InvalidArgumentException, "wrong" );
547                         }
548
549                         ids[count] = personId;
550                         count++;
551                 }
552                 Catch(InvalidArgumentException)
553                 {
554                         ThrowMsg(InvalidArgumentException, "Error during converting person object");
555 //                              LoggerE("Error during converting person object : " << _rethrown_exception.GetMessage());
556 //                              continue;
557                 }
558         }
559
560         KeySharePtrPair *keyPair = new KeySharePtrPair();
561         keyPair->key = m_eventMapAcc;
562         keyPair->contactManager = this;
563         errorCode = contacts_db_delete_records_async(_contacts_person._uri, ids, count, personsRemoveBatchResultCallback, (void*)keyPair);
564
565         if(ids != NULL)
566         {
567                 delete [] ids;
568         }
569
570         if(errorCode != CONTACTS_ERROR_NONE)
571         {
572                 delete keyPair;
573                 ThrowMsg(PlatformException, "Error during add to list");
574         }
575
576         pair<long, EventContactManagerRemoveBatchPtr> keyEventPair(m_eventMapAcc, event);
577         m_removeBatchEventMap.insert(keyEventPair);
578
579         m_eventMapAcc++;
580 }
581
582 void ContactManager::OnRequestReceived(const EventContactManagerRemoveBatchPtr &event)
583 {
584         LoggerD("entered");
585
586         Try
587         {
588                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
589                 if(isEmpty){
590                         managerRemoveBatch(event);
591                 }else{
592                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::DELETEBATCH, this, event);
593                 }
594                 ContactQueueManagerSingleton::Instance().increaseQueueList();
595                 event->switchToManualAnswer();
596         }
597         Catch (NotFoundException)
598         {
599                 LoggerE("NotFoundException during deleting persons : " << _rethrown_exception.GetMessage());
600                 event->setResult(false);
601                 event->setExceptionCode(ExceptionCodes::NotFoundException);
602         }
603         Catch (PlatformException)
604         {
605                 LoggerE("PlatformException during deleting persons : " << _rethrown_exception.GetMessage());
606                 event->setResult(false);
607                 event->setExceptionCode(ExceptionCodes::PlatformException);
608         }
609         Catch(InvalidArgumentException)
610         {
611                 LoggerE("InvalidArgumentException during deleting persons : " << _rethrown_exception.GetMessage());
612                 event->setResult(false);
613                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
614         }
615         Catch (Exception)
616         {
617                 LoggerE("Error during deleting persons : " << _rethrown_exception.GetMessage());
618                 event->setResult(false);
619                 event->setExceptionCode(ExceptionCodes::PlatformException);
620         }
621 }
622
623 void ContactManager::OnRequestReceived(const EventContactManagerFindPtr &event)
624 {
625         LoggerD("entered");
626
627         Try     {
628                 PersonSearchEnginePtr searchEngine(new PersonSearchEngine());
629
630                 if(event->getFilterIsSet())
631                 {
632                         FilterPtr filter = event->getFilter();
633
634                         FilterValidatorPtr validator = PersonFilterValidatorFactory::getPersonFilterValidator();
635                         bool success = filter->validate(validator);
636
637                         if(!success)
638                                 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
639
640                         searchEngine->setCondition(filter);
641                 }
642
643                 if (event->getSortModeIsSet())
644                         searchEngine->setSortMode(event->getSortMode());
645                 else
646                         searchEngine->setSortMode();
647
648                 event->setPersons(searchEngine->getResult());
649                 event->setResult(true);
650
651         } Catch (NotFoundException) {
652                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
653                 event->setExceptionCode(ExceptionCodes::NotFoundException);
654                 event->setResult(false);
655
656         } Catch (PlatformException) {
657                 LoggerE("Error during finding person : " << _rethrown_exception.GetMessage());
658                 event->setExceptionCode(ExceptionCodes::PlatformException);
659                 event->setResult(false);
660
661         } Catch (InvalidArgumentException) {
662                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
663                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
664                 event->setResult(false);
665
666         } Catch (Exception) {
667                 LoggerE("Error during finding person : " << _rethrown_exception.GetMessage());
668                 event->setExceptionCode(ExceptionCodes::PlatformException);
669                 event->setResult(false);
670         }
671 }
672
673 void ContactManager::OnRequestReceived(const EventContactManagerAddChangeListenerPtr &event)
674 {
675         LoggerD("entered");
676
677         Try     {
678                 if(!event->getEmitterIsSet())
679                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
680
681                 EventContactManagerChangeListenerEmitterPtr emitter = event->getEmitter();
682                 if(emitter == NULL)
683                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
684
685                 if(m_contactManagerEmitters.size() == 0)
686                 {
687                         LoggerD("Watch registered initially");
688
689                         ContactsSvcChangeListenerManagerSingleton::Instance().registerPersonsChangeListener(this);
690                 }
691
692                 m_contactManagerEmitters.attach(emitter);
693
694                 long id = ContactsSvcChangeListenerManagerSingleton::Instance().getWatchIdAndInc();
695                 m_watchIdMap[id] = emitter->getId();
696
697             event->setId(id);
698                 event->setResult(true);
699
700         } Catch (NotFoundException) {
701                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
702                 event->setExceptionCode(ExceptionCodes::NotFoundException);
703                 event->setResult(false);
704
705         } Catch (PlatformException) {
706                 LoggerE("Error during addListner person : " << _rethrown_exception.GetMessage());
707                 event->setExceptionCode(ExceptionCodes::PlatformException);
708                 event->setResult(false);
709
710         } Catch (InvalidArgumentException) {
711                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
712                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
713                 event->setResult(false);
714
715         } Catch (Exception) {
716                 LoggerE("Error during addListner person : " << _rethrown_exception.GetMessage());
717                 event->setExceptionCode(ExceptionCodes::PlatformException);
718                 event->setResult(false);
719         }
720 }
721
722 void ContactManager::OnRequestReceived(const EventContactManagerRemoveChangeListenerPtr &event)
723 {
724         LoggerD("entered");
725
726         Try     {
727                 if(!event->getIdIsSet())
728                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
729
730                 long id = event->getId();
731                 if(m_watchIdMap.find(id) == m_watchIdMap.end())
732                         ThrowMsg(NotFoundException, "No watchId 1 ");
733
734                 //DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
735                 bool success = m_contactManagerEmitters.detach(m_watchIdMap[id]);
736                 if(!success)
737                         ThrowMsg(NotFoundException, "No watchId 2 ");
738
739                 m_watchIdMap.erase(id);
740
741                 if(m_contactManagerEmitters.size() == 0)
742                 {
743                         LoggerD("No watcher is registered. unsubscribing from person service.");
744
745                         ContactsSvcChangeListenerManagerSingleton::Instance().unregisterPersonsChangeListener(this);
746                 }
747
748                 event->setResult(true);
749
750         } Catch (NotFoundException) {
751                 LoggerE("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
752                 event->setExceptionCode(ExceptionCodes::NotFoundException);
753                 event->setResult(false);
754
755         } Catch (PlatformException) {
756                 LoggerE("Error during removeListner person : " << _rethrown_exception.GetMessage());
757                 event->setExceptionCode(ExceptionCodes::PlatformException);
758                 event->setResult(false);
759
760         } Catch (InvalidArgumentException) {
761                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
762                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
763                 event->setResult(false);
764
765         } Catch (Exception) {
766                 LoggerE("Error during removeListner person : " << _rethrown_exception.GetMessage());
767                 event->setExceptionCode(ExceptionCodes::PlatformException);
768                 event->setResult(false);
769         }
770 }
771
772 void ContactManager::onContactsSvcPersonsAdded(PersonArrayPtr &persons)
773 {
774         EventInfoContactManagerChangeAddedPtr personsAdded(new EventInfoContactManagerChangeAdded());
775         personsAdded->setPersons(persons);
776         EventInfoContactManagerChangePtr event = DPL::StaticPointerCast<EventInfoContactManagerChange>(personsAdded);
777         EventContactManagerChangeListenerPtr listener(new EventContactManagerChangeListener(event));
778         m_contactManagerEmitters.emit(listener);
779 }
780
781 void ContactManager::onContactsSvcPersonsUpdated(PersonArrayPtr &persons)
782 {
783         EventInfoContactManagerChangeUpdatedPtr personsUpdated(new EventInfoContactManagerChangeUpdated());
784         personsUpdated->setPersons(persons);
785         EventInfoContactManagerChangePtr event = DPL::StaticPointerCast<EventInfoContactManagerChange>(personsUpdated);
786         EventContactManagerChangeListenerPtr listener(new EventContactManagerChangeListener(event));
787         m_contactManagerEmitters.emit(listener);
788 }
789
790 void ContactManager::onContactsSvcPersonsRemoved(StringArrayPtr &personIds)
791 {
792         EventInfoContactManagerChangeRemovedPtr personsRemoved(new EventInfoContactManagerChangeRemoved());
793         personsRemoved->setPersonIds(personIds);
794         EventInfoContactManagerChangePtr event = DPL::StaticPointerCast<EventInfoContactManagerChange>(personsRemoved);
795         EventContactManagerChangeListenerPtr listener(new EventContactManagerChangeListener(event));
796         m_contactManagerEmitters.emit(listener);
797 }
798
799 void ContactManager::personsUpdateBatchResultCallback(int error, void *user_data)
800 {
801         LoggerD("entered");
802
803         if(user_data == NULL)
804         {
805                 LoggerE("user_data is NULL");
806                 return;
807         }
808
809         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
810
811         long key = keyPair->key;
812         ContactManager *contactManager = keyPair->contactManager;
813
814         delete keyPair;
815
816         contactManager->personsUpdateBatchResultCallback(error, key);
817 }
818
819 void ContactManager::personsUpdateBatchResultCallback(int error, long key)
820 {
821         EventContactManagerUpdateBatchPtr event;
822
823         EventContactManagerUpdateBatchMap::iterator iter;
824         iter = m_updateBatchEventMap.find(key);
825         if(iter == m_updateBatchEventMap.end())
826         {
827                 LoggerE("No event for key : " << key);
828                 return;
829         }
830
831         event = iter->second;
832         m_updateBatchEventMap.erase(iter);
833
834         if(error != CONTACTS_ERROR_NONE)
835         {
836                 LoggerE("persons_db_result_cb gives error : " << error);
837                 event->setResult(false);
838                 event->setExceptionCode(ExceptionCodes::PlatformException);
839                 EventRequestReceiver<EventContactManagerUpdateBatch>::ManualAnswer(event);
840                 return;
841         }
842
843         event->setResult(true);
844         event->setExceptionCode(ExceptionCodes::None);
845         EventRequestReceiver<EventContactManagerUpdateBatch>::ManualAnswer(event);
846 }
847
848 void ContactManager::personsRemoveBatchResultCallback(int error, void *user_data)
849 {
850         LoggerD("entered");
851
852         if(user_data == NULL)
853         {
854                 LoggerE("user_data is NULL");
855                 return;
856         }
857
858         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
859
860         long key = keyPair->key;
861         ContactManager *contactManager = keyPair->contactManager;
862
863         delete keyPair;
864
865         contactManager->personsRemoveBatchResultCallback(error, key);
866 }
867
868 void ContactManager::personsRemoveBatchResultCallback(int error, long key)
869 {
870         EventContactManagerRemoveBatchPtr event;
871
872         EventContactManagerRemoveBatchMap::iterator iter;
873         iter = m_removeBatchEventMap.find(key);
874         if(iter == m_removeBatchEventMap.end())
875         {
876                 LoggerE("No event for key : " << key);
877                 return;
878         }
879
880         event = iter->second;
881         m_removeBatchEventMap.erase(iter);
882
883         if(error != CONTACTS_ERROR_NONE)
884         {
885                 LoggerE("persons_db_result_cb gives error : " << error);
886                 event->setResult(false);
887                 event->setExceptionCode(ExceptionCodes::PlatformException);
888                 EventRequestReceiver<EventContactManagerRemoveBatch>::ManualAnswer(event);
889                 return;
890         }
891
892         event->setResult(true);
893         event->setExceptionCode(ExceptionCodes::None);
894         EventRequestReceiver<EventContactManagerRemoveBatch>::ManualAnswer(event);
895 }
896
897 } // Contact
898 } // DeviceAPI