wrt-plugins-tizen_0.4.23
[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, "Address book id is empty");
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 id");
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 (id:" << personIdInt << ") : " << 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                 LoggerD("Load platform object id : " << id);
277                 errorCode = contacts_db_get_record(_contacts_person._uri, id, &contacts_record);
278                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
279                         ThrowMsg(NotFoundException, "Error during executing contacts_db_get_record()");
280                 else if (errorCode != CONTACTS_ERROR_NONE) {
281                         LoggerE("error code : " << errorCode);
282                         ThrowMsg(PlatformException, "Error during executing contacts_db_get_record()");
283                 }
284
285                 ContactsSvcObjectConverter::convertToPlatform(person, contacts_record);
286
287                 errorCode = contacts_db_update_record(contacts_record);
288                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
289                         ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
290                 else if (errorCode != CONTACTS_ERROR_NONE) {
291                         LoggerE("error code : " << errorCode);
292                         ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
293                 }
294
295                 event->setResult(true);
296                 event->setExceptionCode(ExceptionCodes::None);
297         }
298         Catch (NotFoundException)
299         {
300                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
301                 event->setResult(false);
302                 event->setExceptionCode(ExceptionCodes::NotFoundException);
303         }
304         Catch (PlatformException) {
305                 LoggerE("Error during updating person : " << _rethrown_exception.GetMessage());
306                 event->setResult(false);
307                 event->setExceptionCode(ExceptionCodes::PlatformException);
308         }
309         Catch (Exception) {
310                 LoggerE("Error during display_contact_id person : " << _rethrown_exception.GetMessage());
311                 event->setResult(false);
312                 event->setExceptionCode(ExceptionCodes::PlatformException);
313         }
314
315         if(contacts_record != NULL)
316                 contacts_record_destroy(contacts_record, true);
317
318         return;
319 }
320
321 void ContactManager::managerUpdateBatch(const EventContactManagerUpdateBatchPtr &event)
322 {
323         LoggerD("entered");
324         int errorCode = 0;
325         contacts_list_h contacts_list = NULL;
326         PersonArrayPtr persons(NULL);
327
328         Try
329         {
330                 if(!event->getPersonsIsSet())
331                         ThrowMsg(InvalidArgumentException, "Persons were not set.");
332
333                 persons = event->getPersons();
334                 if(!persons)
335                         ThrowMsg(InvalidArgumentException, "No persons.");
336
337         }
338         Catch(InvalidArgumentException)
339         {
340                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
341                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
342                 if(!isEmpty){
343                         ContactQueueManagerSingleton::Instance().pop();
344                 }
345                 LoggerE("Invalid arguments for updating persons : " << _rethrown_exception.GetMessage());
346                 event->setResult(false);
347                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
348                 return;
349         }
350
351         Try
352         {
353                 errorCode = contacts_list_create(&contacts_list);
354                 if(errorCode != CONTACTS_ERROR_NONE)
355                 {
356                         ThrowMsg(PlatformException, "Fail to create contacts_list_h");
357                 }
358
359                 for(PersonArray::iterator i = persons->begin(); i != persons->end(); i++)
360                 {
361                         contacts_record_h contacts_record = NULL;
362                         PersonPtr person = *i;
363
364                         Try
365                         {
366                                 LoggerD("id : " << person->getId());
367                                 int personIdInt = ContactUtility::strToInt(person->getId());
368                                 errorCode = contacts_db_get_record(_contacts_person._uri, personIdInt, &contacts_record);
369                                 if(errorCode != CONTACTS_ERROR_NONE)
370                                 {
371                                         ThrowMsg(NotFoundException, "No person (id:" << personIdInt << ")");
372                                 }
373
374                                 ContactsSvcObjectConverter::convertToPlatform(person, contacts_record);
375
376                                 errorCode = contacts_list_add(contacts_list, contacts_record);
377                                 if(errorCode != CONTACTS_ERROR_NONE)
378                                 {
379                                         ThrowMsg(PlatformException, "Error during add to list");
380                                 }
381                         }
382                         Catch(Exception)
383                         {
384                                 ThrowMsg(NotFoundException, "Error during converting contact object");
385 //                              LoggerE("Error during converting person object : " << _rethrown_exception.GetMessage());
386 //                              continue;
387                         }
388                 }
389
390                 KeySharePtrPair *keyPair = new KeySharePtrPair();
391                 keyPair->key = m_eventMapAcc;
392                 keyPair->contactManager = this;
393                 errorCode = contacts_db_update_records_async(contacts_list, personsUpdateBatchResultCallback, (void*)keyPair);
394                 if(errorCode != CONTACTS_ERROR_NONE)
395                 {
396                         delete keyPair;
397                         ThrowMsg(PlatformException, "Error during contacts_db_update_records_async");
398                 }
399
400                 errorCode = contacts_list_destroy(contacts_list, true);
401                 contacts_list = NULL;
402                 if(errorCode != CONTACTS_ERROR_NONE)
403                 {
404                         delete keyPair;
405                         ThrowMsg(PlatformException, "Error during contacts_list_destroy");
406                 }
407
408                 pair<long, EventContactManagerUpdateBatchPtr> keyEventPair(m_eventMapAcc, event);
409                 m_updateBatchEventMap.insert(keyEventPair);
410
411                 m_eventMapAcc++;
412
413 //              event->switchToManualAnswer();
414         }
415         Catch (NotFoundException)
416         {
417                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
418                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
419                 if(!isEmpty){
420                         ContactQueueManagerSingleton::Instance().pop();
421                 }
422                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
423                 event->setResult(false);
424                 event->setExceptionCode(ExceptionCodes::NotFoundException);
425         }
426         Catch (PlatformException)
427         {
428                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
429                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
430                 if(!isEmpty){
431                         ContactQueueManagerSingleton::Instance().pop();
432                 }
433                 LoggerE("Error during updating persons : " << _rethrown_exception.GetMessage());
434                 event->setResult(false);
435                 event->setExceptionCode(ExceptionCodes::PlatformException);
436         }
437         Catch (Exception)
438         {
439                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
440                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
441                 if(!isEmpty){
442                         ContactQueueManagerSingleton::Instance().pop();
443                 }
444                 LoggerE("Error during updating persons : " << _rethrown_exception.GetMessage());
445                 event->setResult(false);
446                 event->setExceptionCode(ExceptionCodes::PlatformException);
447         }
448
449         if(contacts_list != NULL)
450                 contacts_list_destroy(contacts_list, false);
451 }
452
453 void ContactManager::OnRequestReceived(const EventContactManagerUpdateBatchPtr &event)
454 {
455         LoggerD("entered");
456
457         Try
458         {
459                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
460                 LoggerD("isEmpty : " << isEmpty);
461                 if(isEmpty){
462                         managerUpdateBatch(event);
463                 }else{
464                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::UPATEBATCH, this, event);
465                 }
466                 ContactQueueManagerSingleton::Instance().increaseQueueList();
467                 event->switchToManualAnswer();
468         }
469         Catch (Exception)
470         {
471                 LoggerE("Error during updating persons : " << _rethrown_exception.GetMessage());
472                 event->setResult(false);
473                 event->setExceptionCode(ExceptionCodes::PlatformException);
474         }
475 }
476
477 void ContactManager::OnRequestReceived(const EventContactManagerRemovePtr &event)
478 {
479         LoggerD("entered");
480         int personId;
481
482         Try
483         {
484                 if(!event->getPersonIdIsSet())
485                         ThrowMsg(InvalidArgumentException, "Persons were not set.");
486
487                 string personIdStr = event->getPersonId();
488
489                 if(!validate("^[0-9]+$", personIdStr, VALIDATE_MATCH_CASELESS))
490                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
491
492                 try {
493                         istringstream iss(personIdStr);
494                         iss >> personId;
495                 } catch (...) {
496                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
497                 }
498
499                 int errorCode = contacts_db_delete_record(_contacts_person._uri, personId);
500                 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
501                         ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
502                 else if (errorCode != CONTACTS_ERROR_NONE)
503                         ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
504
505                 event->setResult(true);
506                 event->setExceptionCode(ExceptionCodes::None);
507         }
508         Catch (InvalidArgumentException)
509         {
510                 LoggerE("Invalid person id : " << _rethrown_exception.GetMessage());
511                 event->setResult(false);
512                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
513                 return;
514         }
515         Catch (NotFoundException)
516         {
517                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
518                 event->setResult(false);
519                 event->setExceptionCode(ExceptionCodes::NotFoundException);
520                 return;
521         }
522         Catch (PlatformException) {
523                 LoggerE("Error during removing person : " << _rethrown_exception.GetMessage());
524                 event->setResult(false);
525                 event->setExceptionCode(ExceptionCodes::PlatformException);
526                 return;
527         }
528         Catch (Exception) {
529                 LoggerE("Error during removing person : " << _rethrown_exception.GetMessage());
530                 event->setResult(false);
531                 event->setExceptionCode(ExceptionCodes::PlatformException);
532                 return;
533         }
534 }
535
536 void ContactManager::managerRemoveBatch(const EventContactManagerRemoveBatchPtr &event)
537 {
538         LoggerD("entered");
539         int errorCode = 0;
540         StringArrayPtr personIds(NULL);
541
542         Try
543         {
544                 if(!event->getPersonIdsIsSet())
545                         ThrowMsg(InvalidArgumentException, "Person IDs were not set.");
546
547                 personIds = event->getPersonIds();
548                 if(!personIds)
549                         ThrowMsg(InvalidArgumentException, "No person IDs.");
550
551         }
552         Catch(InvalidArgumentException)
553         {
554                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
555                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
556                 if(!isEmpty){
557                         ContactQueueManagerSingleton::Instance().pop();
558                 }
559                 LoggerE("Invalid arguments for removing persons : " << _rethrown_exception.GetMessage());
560                 event->setResult(false);
561                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
562                 return;
563         }
564
565         Try
566         {
567                 int *ids = new int[personIds->size()];
568                 int count = 0;
569
570                 if(errorCode != CONTACTS_ERROR_NONE)
571                 {
572                         ThrowMsg(PlatformException, "Fail to create contacts_list_h");
573                 }
574
575                 for(StringArray::iterator i = personIds->begin(); i != personIds->end(); i++)
576                 {
577                         string personIdStr = *i;
578
579                         Try
580                         {
581                                 int personId;
582
583                                 if(!validate("^[0-9]+$", personIdStr, VALIDATE_MATCH_CASELESS))
584                                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
585
586                                 try
587                                 {
588                                         istringstream iss(personIdStr);
589                                         iss >> personId;
590                                 }
591                                 catch (...)
592                                 {
593                                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
594                                 }
595
596                                 ids[count] = personId;
597                                 count++;
598                         }
599                         Catch(Exception)
600                         {
601                                 ThrowMsg(NotFoundException, "Error during converting person object");
602 //                              LoggerE("Error during converting person object : " << _rethrown_exception.GetMessage());
603 //                              continue;
604                         }
605                 }
606
607                 KeySharePtrPair *keyPair = new KeySharePtrPair();
608                 keyPair->key = m_eventMapAcc;
609                 keyPair->contactManager = this;
610                 errorCode = contacts_db_delete_records_async(_contacts_person._uri, ids, count, personsRemoveBatchResultCallback, (void*)keyPair);
611
612                 if(ids != NULL)
613                 {
614                         delete [] ids;
615                 }
616
617                 if(errorCode != CONTACTS_ERROR_NONE)
618                 {
619                         delete keyPair;
620                         ThrowMsg(PlatformException, "Error during add to list");
621                 }
622
623                 pair<long, EventContactManagerRemoveBatchPtr> keyEventPair(m_eventMapAcc, event);
624                 m_removeBatchEventMap.insert(keyEventPair);
625
626                 m_eventMapAcc++;
627
628 //              event->switchToManualAnswer();
629         }
630         Catch (InvalidArgumentException)
631         {
632                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
633                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
634                 if(!isEmpty){
635                         ContactQueueManagerSingleton::Instance().pop();
636                 }
637                 LoggerE("Invalid person id : " << _rethrown_exception.GetMessage());
638                 event->setResult(false);
639                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
640                 return;
641         }
642         Catch (NotFoundException)
643         {
644                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
645                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
646                 if(!isEmpty){
647                         ContactQueueManagerSingleton::Instance().pop();
648                 }
649                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
650                 event->setResult(false);
651                 event->setExceptionCode(ExceptionCodes::NotFoundException);
652                 return;
653         }
654         Catch (PlatformException)
655         {
656                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
657                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
658                 if(!isEmpty){
659                         ContactQueueManagerSingleton::Instance().pop();
660                 }
661                 LoggerE("Error during removing persons : " << _rethrown_exception.GetMessage());
662                 event->setResult(false);
663                 event->setExceptionCode(ExceptionCodes::PlatformException);
664                 return;
665         }
666         Catch (Exception)
667         {
668                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
669                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
670                 if(!isEmpty){
671                         ContactQueueManagerSingleton::Instance().pop();
672                 }
673                 LoggerE("Error during removing persons : " << _rethrown_exception.GetMessage());
674                 event->setResult(false);
675                 event->setExceptionCode(ExceptionCodes::PlatformException);
676                 return;
677         }
678 }
679
680 void ContactManager::OnRequestReceived(const EventContactManagerRemoveBatchPtr &event)
681 {
682         LoggerD("entered");
683
684         Try
685         {
686                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
687                 LoggerD("isEmpty : " << isEmpty);
688                 if(isEmpty){
689                         managerRemoveBatch(event);
690                 }else{
691                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::DELETEBATCH, this, event);
692                 }
693                 ContactQueueManagerSingleton::Instance().increaseQueueList();
694                 event->switchToManualAnswer();
695         }
696         Catch (Exception)
697         {
698                 LoggerE("Error during deleting contacts : " << _rethrown_exception.GetMessage());
699                 event->setResult(false);
700                 event->setExceptionCode(ExceptionCodes::PlatformException);
701         }
702 }
703
704 void ContactManager::OnRequestReceived(const EventContactManagerFindPtr &event)
705 {
706         LoggerD("entered");
707
708         Try     {
709                 PersonSearchEnginePtr searchEngine(new PersonSearchEngine());
710
711                 if(event->getFilterIsSet())
712                 {
713                         FilterPtr filter = event->getFilter();
714
715                         FilterValidatorPtr validator = PersonFilterValidatorFactory::getPersonFilterValidator();
716                         bool success = filter->validate(validator);
717
718                         if(!success)
719                                 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
720
721                         searchEngine->setCondition(filter);
722                 }
723
724                 if (event->getSortModeIsSet())
725                         searchEngine->setSortMode(event->getSortMode());
726                 else
727                         searchEngine->setSortMode();
728
729                 event->setPersons(searchEngine->getResult());
730                 event->setResult(true);
731
732         } Catch (NotFoundException) {
733                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
734                 event->setExceptionCode(ExceptionCodes::NotFoundException);
735                 event->setResult(false);
736
737         } Catch (PlatformException) {
738                 LoggerE("Error during finding person : " << _rethrown_exception.GetMessage());
739                 event->setExceptionCode(ExceptionCodes::PlatformException);
740                 event->setResult(false);
741
742         } Catch (InvalidArgumentException) {
743                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
744                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
745                 event->setResult(false);
746
747         } Catch (Exception) {
748                 LoggerE("Error during finding person : " << _rethrown_exception.GetMessage());
749                 event->setExceptionCode(ExceptionCodes::PlatformException);
750                 event->setResult(false);
751         }
752 }
753
754 void ContactManager::OnRequestReceived(const EventContactManagerAddChangeListenerPtr &event)
755 {
756         LoggerD("entered");
757
758         Try     {
759                 if(!event->getEmitterIsSet())
760                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
761
762                 EventContactManagerChangeListenerEmitterPtr emitter = event->getEmitter();
763                 if(emitter == NULL)
764                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
765
766                 if(m_contactManagerEmitters.size() == 0)
767                 {
768                         LoggerD("Watch registered initially");
769
770                         ContactsSvcChangeListenerManagerSingleton::Instance().registerPersonsChangeListener(this);
771                 }
772
773                 m_contactManagerEmitters.attach(emitter);
774
775                 long id = ContactsSvcChangeListenerManagerSingleton::Instance().getWatchIdAndInc();
776                 m_watchIdMap[id] = emitter->getId();
777
778             event->setId(id);
779                 event->setResult(true);
780
781         } Catch (NotFoundException) {
782                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
783                 event->setExceptionCode(ExceptionCodes::NotFoundException);
784                 event->setResult(false);
785
786         } Catch (PlatformException) {
787                 LoggerE("Error during addListner person : " << _rethrown_exception.GetMessage());
788                 event->setExceptionCode(ExceptionCodes::PlatformException);
789                 event->setResult(false);
790
791         } Catch (InvalidArgumentException) {
792                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
793                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
794                 event->setResult(false);
795
796         } Catch (Exception) {
797                 LoggerE("Error during addListner person : " << _rethrown_exception.GetMessage());
798                 event->setExceptionCode(ExceptionCodes::PlatformException);
799                 event->setResult(false);
800         }
801 }
802
803 void ContactManager::OnRequestReceived(const EventContactManagerRemoveChangeListenerPtr &event)
804 {
805         LoggerD("entered");
806
807         Try     {
808                 if(!event->getIdIsSet())
809                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
810
811                 long id = event->getId();
812                 if(m_watchIdMap.find(id) == m_watchIdMap.end())
813                         ThrowMsg(NotFoundException, "No watchId : " << id);
814
815                 //DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
816                 bool success = m_contactManagerEmitters.detach(m_watchIdMap[id]);
817                 if(!success)
818                         ThrowMsg(NotFoundException, "No watchId : " << id);
819
820                 m_watchIdMap.erase(id);
821
822                 if(m_contactManagerEmitters.size() == 0)
823                 {
824                         LoggerD("No watcher is registered. unsubscribing from person service.");
825
826                         ContactsSvcChangeListenerManagerSingleton::Instance().unregisterPersonsChangeListener(this);
827                 }
828
829                 event->setResult(true);
830
831         } Catch (NotFoundException) {
832                 LoggerE("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
833                 event->setExceptionCode(ExceptionCodes::NotFoundException);
834                 event->setResult(false);
835
836         } Catch (PlatformException) {
837                 LoggerE("Error during removeListner person : " << _rethrown_exception.GetMessage());
838                 event->setExceptionCode(ExceptionCodes::PlatformException);
839                 event->setResult(false);
840
841         } Catch (InvalidArgumentException) {
842                 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
843                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
844                 event->setResult(false);
845
846         } Catch (Exception) {
847                 LoggerE("Error during removeListner person : " << _rethrown_exception.GetMessage());
848                 event->setExceptionCode(ExceptionCodes::PlatformException);
849                 event->setResult(false);
850         }
851 }
852
853 void ContactManager::onContactsSvcPersonsAdded(PersonArrayPtr &persons)
854 {
855         EventInfoContactManagerChangeAddedPtr personsAdded(new EventInfoContactManagerChangeAdded());
856         personsAdded->setPersons(persons);
857         EventInfoContactManagerChangePtr event = DPL::StaticPointerCast<EventInfoContactManagerChange>(personsAdded);
858         EventContactManagerChangeListenerPtr listener(new EventContactManagerChangeListener(event));
859         m_contactManagerEmitters.emit(listener);
860 }
861
862 void ContactManager::onContactsSvcPersonsUpdated(PersonArrayPtr &persons)
863 {
864         EventInfoContactManagerChangeUpdatedPtr personsUpdated(new EventInfoContactManagerChangeUpdated());
865         personsUpdated->setPersons(persons);
866         EventInfoContactManagerChangePtr event = DPL::StaticPointerCast<EventInfoContactManagerChange>(personsUpdated);
867         EventContactManagerChangeListenerPtr listener(new EventContactManagerChangeListener(event));
868         m_contactManagerEmitters.emit(listener);
869 }
870
871 void ContactManager::onContactsSvcPersonsRemoved(StringArrayPtr &personIds)
872 {
873         EventInfoContactManagerChangeRemovedPtr personsRemoved(new EventInfoContactManagerChangeRemoved());
874         personsRemoved->setPersonIds(personIds);
875         EventInfoContactManagerChangePtr event = DPL::StaticPointerCast<EventInfoContactManagerChange>(personsRemoved);
876         EventContactManagerChangeListenerPtr listener(new EventContactManagerChangeListener(event));
877         m_contactManagerEmitters.emit(listener);
878 }
879
880 void ContactManager::personsUpdateBatchResultCallback(int error, void *user_data)
881 {
882         LoggerD("entered");
883
884         if(user_data == NULL)
885         {
886                 LoggerE("user_data is NULL");
887                 return;
888         }
889
890         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
891
892         long key = keyPair->key;
893         ContactManager *contactManager = keyPair->contactManager;
894
895         delete keyPair;
896
897         contactManager->personsUpdateBatchResultCallback(error, key);
898 }
899
900 void ContactManager::personsUpdateBatchResultCallback(int error, long key)
901 {
902         EventContactManagerUpdateBatchPtr event;
903
904         EventContactManagerUpdateBatchMap::iterator iter;
905         iter = m_updateBatchEventMap.find(key);
906         if(iter == m_updateBatchEventMap.end())
907         {
908                 LoggerE("No event for key : " << key);
909                 return;
910         }
911
912         event = iter->second;
913         m_updateBatchEventMap.erase(iter);
914
915         if(error != CONTACTS_ERROR_NONE)
916         {
917                 LoggerE("persons_db_result_cb gives error : " << error);
918                 event->setResult(false);
919                 event->setExceptionCode(ExceptionCodes::PlatformException);
920                 EventRequestReceiver<EventContactManagerUpdateBatch>::ManualAnswer(event);
921                 return;
922         }
923
924         event->setResult(true);
925         event->setExceptionCode(ExceptionCodes::None);
926         EventRequestReceiver<EventContactManagerUpdateBatch>::ManualAnswer(event);
927 }
928
929 void ContactManager::personsRemoveBatchResultCallback(int error, void *user_data)
930 {
931         LoggerD("entered");
932
933         if(user_data == NULL)
934         {
935                 LoggerE("user_data is NULL");
936                 return;
937         }
938
939         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
940
941         long key = keyPair->key;
942         ContactManager *contactManager = keyPair->contactManager;
943
944         delete keyPair;
945
946         contactManager->personsRemoveBatchResultCallback(error, key);
947 }
948
949 void ContactManager::personsRemoveBatchResultCallback(int error, long key)
950 {
951         EventContactManagerRemoveBatchPtr event;
952
953         EventContactManagerRemoveBatchMap::iterator iter;
954         iter = m_removeBatchEventMap.find(key);
955         if(iter == m_removeBatchEventMap.end())
956         {
957                 LoggerE("No event for key : " << key);
958                 return;
959         }
960
961         event = iter->second;
962         m_removeBatchEventMap.erase(iter);
963
964         if(error != CONTACTS_ERROR_NONE)
965         {
966                 LoggerE("persons_db_result_cb gives error : " << error);
967                 event->setResult(false);
968                 event->setExceptionCode(ExceptionCodes::PlatformException);
969                 EventRequestReceiver<EventContactManagerRemoveBatch>::ManualAnswer(event);
970                 return;
971         }
972
973         event->setResult(true);
974         event->setExceptionCode(ExceptionCodes::None);
975         EventRequestReceiver<EventContactManagerRemoveBatch>::ManualAnswer(event);
976 }
977
978 } // Contact
979 } // DeviceAPI