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