Initialize Tizen 2.3
[apps/osp/Contacts.git] / src / CtContactListPresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        CtContactListPresentationModel.cpp
19  * @brief       This is the implementation file for the ContactListPresentationModel class.
20  */
21
22 #include <FApp.h>
23 #include <FGraphics.h>
24 #include <FMedia.h>
25 #include <FSocial.h>
26 #include "CtContactListPresentationModel.h"
27 #include "CtContactsApp.h"
28 #include "CtIContactEventListener.h"
29 #include "CtResourceManager.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Media;
36 using namespace Tizen::Social;
37 using namespace Tizen::System;
38
39 static const int VCARD_STRING_LENGTH = 26;
40
41 static const wchar_t* VCARD_PATH_FORMAT = L"data/Ct%04d%02d%02d%02d%02d%02d.vcf";
42 static const wchar_t* KEY_SHARP = L"#";
43
44 ContactListPresentationModel* ContactListPresentationModel::__pInstance = null;
45
46 ContactListPresentationModel*
47 ContactListPresentationModel::GetInstance(void)
48 {
49         if (__pInstance == null)
50         {
51                 CreateInstance();
52         }
53
54         return __pInstance;
55 }
56
57 void
58 ContactListPresentationModel::CreateInstance(void)
59 {
60         __pInstance = new (std::nothrow) ContactListPresentationModel();
61         result r = __pInstance->Construct();
62         TryCatch(r == E_SUCCESS, , "[%s] Construction failed", GetErrorMessage(r));
63
64         std::atexit(DestroyInstance);
65         return;
66
67 CATCH:
68         delete __pInstance;
69         __pInstance = null;
70 }
71
72 void
73 ContactListPresentationModel::DestroyInstance(void)
74 {
75         delete __pInstance;
76 }
77
78 ContactListPresentationModel::ContactListPresentationModel(void)
79         : _pContactList(null)
80         , _pContactChangeListenerList(null)
81         , _pGroupIndexList(null)
82         , _pAddressbook(null)
83         , __pSearchedContactList(null)
84         , __pExcludedContactIdList(null)
85         , __searchedText()
86         , __isContactChanged(true)
87 {
88 }
89
90 ContactListPresentationModel::~ContactListPresentationModel(void)
91 {
92         if (_pContactList != null)
93         {
94                 _pContactList->RemoveAll(true);
95                 delete _pContactList;
96         }
97
98         if (__pSearchedContactList != null)
99         {
100                 __pSearchedContactList->RemoveAll(true);
101                 delete __pSearchedContactList;
102         }
103
104         if (__pExcludedContactIdList != null)
105         {
106                 __pExcludedContactIdList->RemoveAll(true);
107                 delete __pExcludedContactIdList;
108         }
109
110         if (_pContactChangeListenerList != null)
111         {
112                 _pContactChangeListenerList->RemoveAll(false);
113                 delete _pContactChangeListenerList;
114         }
115
116         delete _pAddressbook;
117         delete _pGroupIndexList;
118 }
119
120 result
121 ContactListPresentationModel::Construct(void)
122 {
123         if (_pAddressbook != null)
124         {
125                 AppLogDebug("ContactListPresentationModel is already constructed.");
126                 return E_SUCCESS;
127         }
128
129         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
130
131         _pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
132         TryReturn(_pAddressbook != null, E_FAILURE, "No address book")
133
134         _pAddressbook->SetAddressbookChangeEventListener(this);
135
136         _pContactChangeListenerList = new (std::nothrow) ArrayList();
137         _pContactChangeListenerList->Construct();
138
139         return E_SUCCESS;
140 }
141
142 result
143 ContactListPresentationModel::InitializeContactList(Tizen::Base::Collection::IList* pContactIdList)
144 {
145         if (__pExcludedContactIdList != null)
146         {
147                 delete __pExcludedContactIdList;
148         }
149         __pExcludedContactIdList = pContactIdList;
150
151         return CreateContactList();
152 }
153
154 result
155 ContactListPresentationModel::CreateContactList(void)
156 {
157         result r = E_SUCCESS;
158
159         if (_pContactList == null)
160         {
161                 _pContactList = new (std::nothrow) MultiHashMap();
162                 _pContactList->Construct();
163         }
164         else
165         {
166                 _pContactList->RemoveAll(true);
167         }
168
169         if (_pGroupIndexList == null)
170         {
171                 _pGroupIndexList = new (std::nothrow) ArrayList();
172                 _pGroupIndexList->Construct();
173         }
174         else
175         {
176                 _pGroupIndexList->RemoveAll(false);
177         }
178
179         IList* pContactList = AddressbookManager::GetInstance()->GetAllPersonsN();
180         TryReturn(pContactList != null, E_FAILURE, "[E_FAILURE] Unable to get persons.");
181
182         if (__pExcludedContactIdList != null)
183         {
184                 IBidirectionalEnumerator* pIdEnum = __pExcludedContactIdList->GetBidirectionalEnumeratorN();
185                 pIdEnum->ResetLast();
186                 while (pIdEnum->MovePrevious() == E_SUCCESS)
187                 {
188                         Integer* pPersonId = static_cast<Integer*>(pIdEnum->GetCurrent());
189
190                         for (int index = pContactList->GetCount() - 1; index >= 0; index--)
191                         {
192                                 Person* pPerson = static_cast<Person *>(pContactList->GetAt(index));
193                                 if (pPerson != null && pPersonId != null &&
194                                         pPerson->GetId() == pPersonId->ToInt())
195                                 {
196                                         pContactList->RemoveAt(index, true);
197                                         break;
198                                 }
199                         }
200                 }
201
202                 delete pIdEnum;
203         }
204
205         ContactsApp* pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
206         if (pContactsApp->GetOperationId() == OPERATION_ID_PICK)
207         {
208                 TrimListForAppControl(pContactList);
209         }
210
211         IEnumerator* pEnum = pContactList->GetEnumeratorN();
212         TryReturn(pEnum, E_FAILURE, "No contact");
213
214         bool isNotUsedSharpKey = true;
215         String* pKey = null;
216         String* pSharpKey = new (std::nothrow) String(KEY_SHARP);
217
218         while (pEnum->MoveNext() == E_SUCCESS)
219         {
220                 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
221                 if (pPerson == null)
222                 {
223                         r = E_FAILURE;
224                         break;
225                 }
226
227                 String displayName = pPerson->GetDisplayName();
228                 String firstCharacter;
229
230                 displayName.SubString(0, 1, firstCharacter);
231                 firstCharacter.ToUpper();
232
233                 if (pKey == null || firstCharacter.CompareTo(*pKey) != 0)
234                 {
235                         if (firstCharacter[0] >= L'A' && firstCharacter[0] <= L'Z')
236                         {
237                                 if (_pGroupIndexList->Contains(firstCharacter))
238                                 {
239                                         int index;
240
241                                         _pGroupIndexList->IndexOf(firstCharacter, index);
242                                         pKey = static_cast<String *>(_pGroupIndexList->GetAt(index));
243                                         if (pKey == null)
244                                         {
245                                                 break;
246                                         }
247                                 }
248                                 else
249                                 {
250                                         pKey = new (std::nothrow) String(firstCharacter);
251                                         _pGroupIndexList->Add(*pKey);
252                                 }
253                         }
254                         else
255                         {
256                                 isNotUsedSharpKey = false;
257                                 pKey = pSharpKey;
258
259                                 if (_pGroupIndexList->Contains(*pKey) == false)
260                                 {
261                                         _pGroupIndexList->Add(*pKey);
262                                 }
263                         }
264                 }
265                 _pContactList->Add(*pKey, *pPerson);
266         }
267
268         _pGroupIndexList->Sort(StringComparer());
269
270         delete pEnum;
271         delete pContactList;
272
273         if (isNotUsedSharpKey)
274         {
275                 delete pSharpKey;
276         }
277
278         return r;
279 }
280
281 result
282 ContactListPresentationModel::TrimListForAppControl(Tizen::Base::Collection::IList*& pContactList)
283 {
284         TryReturn(pContactList != null, E_INVALID_ARG, "Invalid argument");
285
286         IList* pSelectedList = null;
287         ContactsApp* pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
288         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
289         AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
290
291         if (pContactsApp->GetReturnType() == APP_CONTROL_RETURN_TYPE_PHONE)
292         {
293                 mainFilter.AppendBool(FI_CONJ_OP_NONE, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, true);
294
295                 pSelectedList = pAddressbookManager->SearchN(mainFilter);
296                 TryReturn(pSelectedList != null, E_FAILURE, "Unable to get the list contained phone number");
297
298                 pContactList->RemoveAll(true);
299                 pContactList->AddItems(*pSelectedList);
300         }
301         else if (pContactsApp->GetReturnType() == APP_CONTROL_RETURN_TYPE_EMAIL)
302         {
303                 mainFilter.AppendBool(FI_CONJ_OP_NONE, PERSON_FI_PR_HAS_EMAIL, FI_CMP_OP_EQUAL, true);
304
305                 pSelectedList = pAddressbookManager->SearchN(mainFilter);
306                 TryReturn(pSelectedList != null, E_FAILURE, "Unable to get the list contained email");
307
308                 pContactList->RemoveAll(true);
309                 pContactList->AddItems(*pSelectedList);
310         }
311
312         return E_SUCCESS;
313 }
314
315 Tizen::Base::String
316 ContactListPresentationModel::GetKey(int groupIndex)
317 {
318         String* key = static_cast<String *>(_pGroupIndexList->GetAt(groupIndex));
319
320         if (key == null)
321         {
322                 return String();
323         }
324
325         return *key;
326 }
327
328 Tizen::Base::String
329 ContactListPresentationModel::GetAllKeys(void)
330 {
331         String allKeys;
332         IEnumerator* pEnum = _pGroupIndexList->GetEnumeratorN();
333
334         if (pEnum == null)
335         {
336                 return String();
337         }
338
339         while (pEnum->MoveNext() == E_SUCCESS)
340         {
341                 String* pKey = static_cast<String *>(pEnum->GetCurrent());
342
343                 allKeys.Append(*pKey);
344         }
345
346         delete pEnum;
347
348         return allKeys;
349 }
350
351 int
352 ContactListPresentationModel::GetKeyCount(void)
353 {
354         return _pGroupIndexList->GetCount();
355 }
356
357 int
358 ContactListPresentationModel::GetKeyIndex(const Tizen::Base::String& key)
359 {
360         int index;
361
362         _pGroupIndexList->IndexOf(key, index);
363
364         return index;
365 }
366
367 Tizen::Social::Person*
368 ContactListPresentationModel::GetPersonAt(int groupIndex, int itemIndex)
369 {
370         int count = 0;
371         Person* pPerson = null;
372         IEnumerator* pEnum = _pContactList->GetValuesN(GetKey(groupIndex));
373
374         if (pEnum == null)
375         {
376                 return null;
377         }
378
379         while (pEnum->MoveNext() == E_SUCCESS)
380         {
381                 if (itemIndex == count++)
382                 {
383                         pPerson = static_cast<Person *>(pEnum->GetCurrent());
384                         break;
385                 }
386         }
387
388         delete pEnum;
389
390         return pPerson;
391 }
392
393 result
394 ContactListPresentationModel::GetContactItemInfoN(int groupIndex, int itemIndex, Tizen::Base::String& name, Tizen::Graphics::Bitmap*& pThumbnail)
395 {
396         Person* pPerson = GetPersonAt(groupIndex, itemIndex);
397
398         if (pPerson == null)
399         {
400                 return E_FAILURE;
401         }
402
403         name = pPerson->GetDisplayName();
404         if (name.IsEmpty())
405         {
406                 Email email = pPerson->GetPrimaryEmail();
407                 name.Append(email.GetEmail());
408         }
409         String thumbnailPath = pPerson->GetThumbnailPath();
410
411         if (thumbnailPath.IsEmpty() == false)
412         {
413                 ImageBuffer thumbnailImageBuffer;
414                 result r = thumbnailImageBuffer.Construct(thumbnailPath);
415                 if (r == E_SUCCESS)
416                 {
417                         pThumbnail = thumbnailImageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_NONE);
418                 }
419         }
420
421         return E_SUCCESS;
422 }
423
424 int
425 ContactListPresentationModel::GetContactCount(void)
426 {
427         return _pContactList->GetCount();
428 }
429
430 int
431 ContactListPresentationModel::GetContactCount(int groupIndex)
432 {
433         int count = 0;
434
435         _pContactList->GetCount(GetKey(groupIndex), count);
436
437         return count;
438 }
439
440 result
441 ContactListPresentationModel::RemoveContact(int groupIndex, int itemIndex, bool isUpdated)
442 {
443         __isContactChanged = isUpdated;
444
445         Person* pPerson = GetPersonAt(groupIndex, itemIndex);
446
447         if (pPerson == null)
448         {
449                 return E_FAILURE;
450         }
451
452         return AddressbookManager::GetInstance()->RemovePerson(pPerson->GetId());
453 }
454
455 Tizen::Social::RecordId
456 ContactListPresentationModel::GetContactId(int groupIndex, int itemIndex)
457 {
458         Person* pPerson = GetPersonAt(groupIndex, itemIndex);
459
460         if (pPerson == null)
461         {
462                 return INVALID_RECORD_ID;
463         }
464
465         IList* pContactList = AddressbookManager::GetInstance()->GetContactsByPersonN(pPerson->GetId());
466         if (pContactList == null)
467         {
468                 return INVALID_RECORD_ID;
469         }
470
471         Contact* pContact = static_cast<Contact *>(pContactList->GetAt(0));
472         if (pContact == null)
473         {
474                 return INVALID_RECORD_ID;
475         }
476
477         RecordId contactId = pContact->GetRecordId();
478
479         pContactList->RemoveAll(true);
480         delete pContactList;
481         pContactList = null;
482
483         return contactId;
484 }
485
486 Tizen::Base::String
487 ContactListPresentationModel::GetName(int groupIndex, int itemIndex)
488 {
489         Person* pPerson = GetPersonAt(groupIndex, itemIndex);
490
491         if (pPerson == null)
492         {
493                 return null;
494         }
495
496         return pPerson->GetDisplayName();
497 }
498
499 Tizen::Base::String
500 ContactListPresentationModel::GetDefaultPhoneNumber(int groupIndex, int itemIndex)
501 {
502         Person* pPerson = GetPersonAt(groupIndex, itemIndex);
503         if (pPerson == null)
504         {
505                 return String();
506         }
507
508         PhoneNumber phoneNumber = pPerson->GetPrimaryPhoneNumber();
509
510         return phoneNumber.GetPhoneNumber();
511 }
512
513 Tizen::Base::String
514 ContactListPresentationModel::GetDefaultEmail(int groupIndex, int itemIndex)
515 {
516         String emailString;
517
518         Contact* pContact = _pAddressbook->GetContactN(GetContactId(groupIndex, itemIndex));
519         if (pContact == null)
520         {
521                 return emailString;
522         }
523
524         //[TODO] should change to default email or rename this function
525         IList* pEmailList = pContact->GetValuesN(CONTACT_MPROPERTY_ID_EMAILS);
526         if (pEmailList == null)
527         {
528                 delete pContact;
529
530                 return emailString;
531         }
532
533         Email* pEmail = static_cast<Email*>(pEmailList->GetAt(0));
534         if (pEmail == null)
535         {
536                 pEmailList->RemoveAll(true);
537                 delete pEmailList;
538                 delete pContact;
539
540                 return emailString;
541         }
542
543         emailString = pEmail->GetEmail();
544
545         pEmailList->RemoveAll(true);
546         delete pEmailList;
547         delete pContact;
548
549         return emailString;
550 }
551
552 Tizen::Base::Collection::IList*
553 ContactListPresentationModel::GetAllPhoneNumbersN(int groupIndex, int itemIndex)
554 {
555         Contact* pContact = _pAddressbook->GetContactN(GetContactId(groupIndex, itemIndex));
556         if (pContact == null)
557         {
558                 return null;
559         }
560         IList* pPhoneNumberList = pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
561         if (pPhoneNumberList == null)
562         {
563                 delete pContact;
564                 return null;
565         }
566
567         ArrayList* pPhoneNumberInfoList = new (std::nothrow) ArrayList();
568         pPhoneNumberInfoList->Construct();
569         PhoneNumber* pPhoneNumber = null;
570
571         IEnumerator* pEnum = pPhoneNumberList->GetEnumeratorN();
572         while (pEnum->MoveNext() == E_SUCCESS)
573         {
574                 pPhoneNumber = static_cast<PhoneNumber *>(pEnum->GetCurrent());
575                 if (pPhoneNumber != null)
576                 {
577                         String phoneNumbersInfo(L"");
578
579                         switch (pPhoneNumber->GetType())
580                         {
581                         case PHONENUMBER_TYPE_HOME:
582                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_HOME"));
583                                 break;
584                         case PHONENUMBER_TYPE_WORK:
585                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_WORK"));
586                                 break;
587                         case PHONENUMBER_TYPE_MOBILE:
588                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_MOBILE"));
589                                 break;
590                         case PHONENUMBER_TYPE_HOME_FAX:
591                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_FAX_HHOME"));
592                                 break;
593                         case PHONENUMBER_TYPE_WORK_FAX:
594                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_FAX_HWORK"));
595                                 break;
596                         case PHONENUMBER_TYPE_PAGER:
597                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_PAGER"));
598                                 break;
599                         case PHONENUMBER_TYPE_OTHER:
600                                 phoneNumbersInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_OTHER"));
601                                 break;
602                         default:
603                                 break;
604                         }
605                         phoneNumbersInfo.Append(CHARACTER_SPACE);
606                         phoneNumbersInfo.Append(pPhoneNumber->GetPhoneNumber());
607
608                         pPhoneNumberInfoList->Add(*(new (std::nothrow) String(phoneNumbersInfo)));
609                 }
610         }
611
612         delete pEnum;
613         delete pContact;
614         pPhoneNumberList->RemoveAll(true);
615         delete pPhoneNumberList;
616
617         return pPhoneNumberInfoList;
618 }
619
620 Tizen::Base::Collection::IList*
621 ContactListPresentationModel::GetAllEmailsN(int groupIndex, int itemIndex)
622 {
623         Contact* pContact = _pAddressbook->GetContactN(GetContactId(groupIndex, itemIndex));
624         if (pContact == null)
625         {
626                 return null;
627         }
628         IList* pEmailList = pContact->GetValuesN(CONTACT_MPROPERTY_ID_EMAILS);
629         if (pEmailList == null)
630         {
631                 delete pContact;
632                 return null;
633         }
634
635         ArrayList* pEmailInfoList = new (std::nothrow) ArrayList();
636         pEmailInfoList->Construct();
637         Email* pEmail = null;
638
639         IEnumerator* pEnum = pEmailList->GetEnumeratorN();
640         while (pEnum->MoveNext() == E_SUCCESS)
641         {
642                 pEmail = static_cast<Email *>(pEnum->GetCurrent());
643                 if (pEmail != null)
644                 {
645                         String EmailsInfo(L"");
646
647                         switch (pEmail->GetType())
648                         {
649                         case EMAIL_TYPE_PERSONAL:
650                                 EmailsInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_HOME"));
651                                 break;
652                         case EMAIL_TYPE_WORK:
653                                 EmailsInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_WORK"));
654                                 break;
655                         case EMAIL_TYPE_OTHER:
656                                 EmailsInfo.Append(ResourceManager::GetString(L"IDS_PB_BODY_OTHER"));
657                                 break;
658                         default:
659                                 break;
660                         }
661                         EmailsInfo.Append(CHARACTER_SPACE);
662                         EmailsInfo.Append(pEmail->GetEmail());
663
664                         pEmailInfoList->Add(*(new (std::nothrow) String(EmailsInfo)));
665                 }
666         }
667
668         delete pEnum;
669         delete pContact;
670         pEmailList->RemoveAll(true);
671         delete pEmailList;
672
673         return pEmailInfoList;
674 }
675
676 result
677 ContactListPresentationModel::SetSearchText(const Tizen::Base::String& searchText, SearchType searchType)
678 {
679         result r = E_SUCCESS;
680
681         if (__pSearchedContactList != null)
682         {
683                 __pSearchedContactList->RemoveAll(true);
684                 delete __pSearchedContactList;
685                 __pSearchedContactList = null;
686         }
687
688         __pSearchedContactList = GetSearchedContactListN(searchText, searchType);
689
690         if (__pSearchedContactList != null)
691         {
692                 r = searchText.ToLowerCase(__searchedText);
693         }
694
695         return r;
696 }
697
698 result
699 ContactListPresentationModel::GetSearchedContactItemInfo(int index, Tizen::Base::String& name, Tizen::Graphics::Bitmap*& pThumbnail)
700 {
701         if (__pSearchedContactList == null)
702         {
703                 return E_FAILURE;
704         }
705         Person* pPerson = static_cast<Person *>(__pSearchedContactList->GetAt(index));
706         if (pPerson == null)
707         {
708                 return E_FAILURE;
709         }
710
711         name = pPerson->GetDisplayName();
712         String thumbnailPath = pPerson->GetThumbnailPath();
713
714         if (thumbnailPath.IsEmpty() == false)
715         {
716                 ImageBuffer thumbnailImageBuffer;
717                 result r = thumbnailImageBuffer.Construct(pPerson->GetThumbnailPath());
718                 if (r == E_SUCCESS)
719                 {
720                         pThumbnail = thumbnailImageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_NONE);
721                 }
722         }
723
724         return E_SUCCESS;
725 }
726
727 Tizen::Base::Collection::IList*
728 ContactListPresentationModel::GetSearchedContactListN(const Tizen::Base::String& searchText, SearchType searchType)
729 {
730         if (searchText.GetLength() == 0)
731         {
732                 return null;
733         }
734
735         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
736         int phoneNumber;
737         IList* pSearchedList = null;
738
739         if (searchType != SEARCH_TYPE_HAS_EMAIL_ONLY)
740         {
741                 IList* pSearchingPhoneNumberList = null;
742                 AddressbookFilter phoneNumberFilter(AB_FI_TYPE_PHONE_CONTACT);
743                 phoneNumberFilter.AppendString(FI_CONJ_OP_NONE, PHONE_CONTACT_FI_PR_PHONE, FI_STR_OP_CONTAIN, searchText);
744                 phoneNumberFilter.AppendString(FI_CONJ_OP_OR, PHONE_CONTACT_FI_PR_DISPLAY_NAME, FI_STR_OP_CONTAIN, searchText);
745
746                 pSearchingPhoneNumberList = pAddressbookManager->SearchN(phoneNumberFilter);
747                 if (pSearchingPhoneNumberList == null)
748                 {
749                         return null;
750                 }
751
752                 IEnumerator* pEnum = pSearchingPhoneNumberList->GetEnumeratorN();
753                 if (pEnum == null)
754                 {
755                         pSearchingPhoneNumberList->RemoveAll(true);
756                         delete pSearchingPhoneNumberList;
757
758                         return null;
759                 }
760
761                 ArrayList* pSearchedPersonsList = new (std::nothrow) ArrayList();
762                 pSearchedPersonsList->Construct();
763
764                 while (pEnum->MoveNext() == E_SUCCESS)
765                 {
766                         PhoneNumberContact* pPhoneNumberContact = static_cast<PhoneNumberContact *>(pEnum->GetCurrent());
767                         Person* pPerson = pAddressbookManager->GetPersonN(pPhoneNumberContact->GetPersonId());
768
769                         if (pPerson != null && pSearchedPersonsList->Contains(*pPerson) == false)
770                         {
771                                 pSearchedPersonsList->Add(*pPerson);
772                         }
773                 }
774
775                 pSearchingPhoneNumberList->RemoveAll(true);
776                 delete pSearchingPhoneNumberList;
777                 delete pEnum;
778
779                 pSearchedList = pSearchedPersonsList;
780
781                 if (searchType != SEARCH_TYPE_HAS_PHONE_ONLY)
782                 {
783                         IList* pDisplayNameSearchedList = null;
784                         AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
785                         mainFilter.AppendString(FI_CONJ_OP_NONE, PERSON_FI_PR_DISPLAY_NAME, FI_STR_OP_CONTAIN, searchText);
786                         mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, false);
787
788                         pDisplayNameSearchedList = pAddressbookManager->SearchN(mainFilter);
789                         pSearchedList->InsertItemsFrom(*pDisplayNameSearchedList, 0);
790                 }
791         }
792         else
793         {
794                 AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
795                 mainFilter.AppendString(FI_CONJ_OP_NONE, PERSON_FI_PR_DISPLAY_NAME, FI_STR_OP_CONTAIN, searchText);
796
797                 if (searchType == SEARCH_TYPE_HAS_PHONE_ONLY)
798                 {
799                         mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, true);
800                 }
801                 else if (searchType == SEARCH_TYPE_HAS_EMAIL_ONLY)
802                 {
803                         mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_EMAIL, FI_CMP_OP_EQUAL, true);
804                 }
805
806                 pSearchedList = pAddressbookManager->SearchN(mainFilter);
807         }
808
809         for (int index = pSearchedList->GetCount() - 1; index >= 0; index--)
810         {
811                 Person* pPerson = static_cast<Person *>(pSearchedList->GetAt(index));
812                 if (__pExcludedContactIdList != null && pPerson != null)
813                 {
814                         for (int idIndex = __pExcludedContactIdList->GetCount() - 1; idIndex >= 0; idIndex--)
815                         {
816                                 Integer* pPersonId = static_cast<Integer*>(__pExcludedContactIdList->GetAt(idIndex));
817                                 if (pPersonId != null && pPersonId->ToInt() == pPerson->GetId())
818                                 {
819                                         pSearchedList->RemoveAt(index, true);
820                                         break;
821                                 }
822                         }
823                 }
824         }
825
826         return pSearchedList;
827 }
828
829 Tizen::Base::String
830 ContactListPresentationModel::GetSearchText(void)
831 {
832         return __searchedText;
833 }
834
835 void
836 ContactListPresentationModel::ResetSearchedContactList(void)
837 {
838         if (__pSearchedContactList != null)
839         {
840                 __pSearchedContactList->RemoveAll(true);
841                 delete __pSearchedContactList;
842                 __pSearchedContactList = null;
843         }
844 }
845
846 int
847 ContactListPresentationModel::GetSearchedContactCount(void)
848 {
849         if (__pSearchedContactList != null)
850         {
851                 return __pSearchedContactList->GetCount();
852         }
853
854         return 0;
855 }
856
857 result
858 ContactListPresentationModel::GetItemMainIndex(int searchedItemIndex, int& groupIndex, int& itemIndex)
859 {
860         Person* pSearchedPerson = static_cast<Person *>(__pSearchedContactList->GetAt(searchedItemIndex));
861
862         if (pSearchedPerson == null)
863         {
864                 return E_FAILURE;
865         }
866
867         int count = 0;
868         String firstCharacter;
869         String displayName = pSearchedPerson->GetDisplayName();
870         displayName.ToUpper();
871
872         if (displayName[0] >= L'A' && displayName[0] <= L'Z')
873         {
874                 displayName.SubString(0, 1, firstCharacter);
875         }
876         else
877         {
878                 firstCharacter.Append(KEY_SHARP);
879         }
880
881         IEnumerator* pEnum = _pContactList->GetValuesN(firstCharacter);
882         if (pEnum == null)
883         {
884                 return E_FAILURE;
885         }
886
887         while (pEnum->MoveNext() == E_SUCCESS)
888         {
889                 Person* pPerson = static_cast<Person *>(pEnum->GetCurrent());
890                 if (pPerson == null)
891                 {
892                         return E_FAILURE;
893                 }
894
895                 if (pSearchedPerson->GetId() == pPerson->GetId())
896                 {
897                         _pGroupIndexList->IndexOf(firstCharacter, groupIndex);
898                         itemIndex = count;
899                         break;
900                 }
901
902                 count++;
903         }
904
905         delete pEnum;
906
907         return E_SUCCESS;
908 }
909
910 int
911 ContactListPresentationModel::GetAddressbookCount(void)
912 {
913         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
914
915         IList* pAddressbookList = pAddressbookManager->GetAllAddressbooksN();
916         if (pAddressbookList == null)
917         {
918                 return -1;
919         }
920
921         int count = pAddressbookList->GetCount();
922         pAddressbookList->RemoveAll(true);
923         delete pAddressbookList;
924
925         return count;
926 }
927
928 Tizen::Base::String
929 ContactListPresentationModel::GetAddressbookName(int index)
930 {
931         String returnString;
932
933         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
934
935         IList* pAddressbookList = pAddressbookManager->GetAllAddressbooksN();
936         if (pAddressbookList == null)
937         {
938                 return returnString;
939         }
940
941         Addressbook* pAddressbook = static_cast<Addressbook *>(pAddressbookList->GetAt(index));
942         TryCatch(pAddressbook != null, , "Unable to get addressbook form addressbook manager");
943
944         //Todo : Return AccountId temporarily.
945         if (pAddressbook->GetId() == DEFAULT_ADDRESSBOOK_ID)
946         {
947                 returnString.Append(ResourceManager::GetString(L"IDS_PB_BODY_PHONE"));
948         }
949         else
950         {
951                 returnString.Append(pAddressbook->GetId());
952         }
953
954 CATCH:
955         pAddressbookList->RemoveAll(true);
956         delete pAddressbookList;
957
958         return returnString;
959 }
960
961 Tizen::Base::String
962 ContactListPresentationModel::GetCurrentAddressbook(void)
963 {
964         String addressbookName;
965
966         if (_pAddressbook->GetId() == DEFAULT_ADDRESSBOOK_ID)
967         {
968                 addressbookName.Append(ResourceManager::GetString(L"IDS_PB_BODY_PHONE"));
969         }
970         else
971         {
972                 addressbookName.Append(_pAddressbook->GetId());
973         }
974
975         return addressbookName;
976 }
977
978 bool
979 ContactListPresentationModel::IsContactInCategory(int groupIndex, int itemIndex, Tizen::Social::RecordId categoryId)
980 {
981         bool isContactInCategory = false;
982         Person* pPerson = GetPersonAt(groupIndex, itemIndex);
983
984         if (pPerson == null)
985         {
986                 return false;
987         }
988
989         AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
990         mainFilter.AppendInt(FI_CONJ_OP_NONE, PERSON_FI_PR_PERSON_ID, FI_CMP_OP_EQUAL, pPerson->GetId());
991         mainFilter.AppendInt(FI_CONJ_OP_AND, PERSON_FI_PR_CATEGORY_ID, FI_CMP_OP_EQUAL, categoryId);
992
993         IList* pList = AddressbookManager::GetInstance()->SearchN(mainFilter);
994
995         if (pList == null)
996         {
997                 return false;
998         }
999
1000         if (pList->GetCount() > 0)
1001         {
1002                 isContactInCategory = true;
1003         }
1004         else
1005         {
1006                 isContactInCategory = false;
1007         }
1008
1009         pList->RemoveAll(true);
1010         delete pList;
1011
1012         return isContactInCategory;
1013 }
1014
1015 bool
1016 ContactListPresentationModel::IsContactInCategory(int groupIndex, Tizen::Social::RecordId categoryId)
1017 {
1018         String key = GetKey(groupIndex);
1019         int count;
1020
1021         _pContactList->GetCount(key, count);
1022
1023         for (int i = 0; i < count; i++)
1024         {
1025                 if (IsContactInCategory(groupIndex, i, categoryId) == false)
1026                 {
1027                         return false;
1028                 }
1029         }
1030
1031         return true;
1032 }
1033
1034 bool
1035 ContactListPresentationModel::IsSearchedContactInCategory(int index, Tizen::Social::RecordId categoryId)
1036 {
1037         bool isContactInCategory = false;
1038         Person* pPerson = static_cast<Person *>(__pSearchedContactList->GetAt(index));
1039
1040         if (pPerson == null)
1041         {
1042                 return false;
1043         }
1044
1045         AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
1046         mainFilter.AppendInt(FI_CONJ_OP_NONE, PERSON_FI_PR_PERSON_ID, FI_CMP_OP_EQUAL, pPerson->GetId());
1047         mainFilter.AppendInt(FI_CONJ_OP_AND, PERSON_FI_PR_CATEGORY_ID, FI_CMP_OP_EQUAL, categoryId);
1048
1049         IList* pList = AddressbookManager::GetInstance()->SearchN(mainFilter);
1050
1051         if (pList == null)
1052         {
1053                 return false;
1054         }
1055
1056         if (pList->GetCount() > 0)
1057         {
1058                 isContactInCategory = true;
1059         }
1060         else
1061         {
1062                 isContactInCategory = false;
1063         }
1064
1065         pList->RemoveAll(true);
1066         delete pList;
1067
1068         return isContactInCategory;
1069 }
1070
1071 Tizen::Base::String
1072 ContactListPresentationModel::ExportToVcard(int groupIndex, int itemIndex)
1073 {
1074         String vCardPath;
1075         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
1076         Contact* pContact = pAddressbookManager->GetContactN(GetContactId(groupIndex, itemIndex));
1077         TryReturn(pContact != null, vCardPath, "[E_FAILURE] Unable to get the specific contact.");
1078
1079         DateTime currentDateTime;
1080         SystemTime::GetCurrentTime(TIME_MODE_WALL, currentDateTime);
1081
1082         vCardPath.Format(VCARD_STRING_LENGTH, VCARD_PATH_FORMAT, currentDateTime.GetYear(), currentDateTime.GetMonth(), currentDateTime.GetDay(),
1083                         currentDateTime.GetHour(), currentDateTime.GetMinute(), currentDateTime.GetSecond());
1084
1085         vCardPath = Application::GetInstance()->GetAppSharedPath() + vCardPath;
1086
1087         result r = pAddressbookManager->ExportContactToVcard(*pContact, vCardPath);
1088
1089         if (r != E_SUCCESS)
1090         {
1091                 vCardPath.Clear();
1092         }
1093
1094         delete pContact;
1095
1096         return vCardPath;
1097 }
1098
1099 void
1100 ContactListPresentationModel::AddContactChangeListener(const IContactEventListener& listener)
1101 {
1102         _pContactChangeListenerList->Add(listener);
1103 }
1104
1105 void
1106 ContactListPresentationModel::RemoveContactChangeListener(const IContactEventListener& listener)
1107 {
1108         _pContactChangeListenerList->Remove(listener, false);
1109 }
1110
1111 void
1112 ContactListPresentationModel::OnContactsChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
1113 {
1114         if (__isContactChanged == false)
1115         {
1116                 __isContactChanged = true;
1117
1118                 return;
1119         }
1120
1121         IContactEventListener* pInterface = null;
1122         IEnumerator* pEnum = _pContactChangeListenerList->GetEnumeratorN();
1123         while (pEnum->MoveNext() == E_SUCCESS)
1124         {
1125                 pInterface = static_cast<IContactEventListener *>(pEnum->GetCurrent());
1126                 if (pInterface == null)
1127                 {
1128                         delete pEnum;
1129                         return;
1130                 }
1131                 pInterface->OnContactsChanged();
1132         }
1133
1134         delete pEnum;
1135 }
1136
1137 void
1138 ContactListPresentationModel::OnCategoriesChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
1139 {
1140 }