NABI_SE Issue Fix 45285 45321 45446 45336
[apps/osp/Contacts.git] / src / CtContactPresentationModel.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        CtContactPresentationModel.cpp
19  * @brief       This is the implementation file for the ContactPresentationModel class.
20  */
21
22 #include <FGraphics.h>
23 #include <FLocales.h>
24 #include "CtContactPresentationModel.h"
25 #include "CtIContactEventListener.h"
26 #include "CtResourceManager.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Io;
32 using namespace Tizen::Locales;
33 using namespace Tizen::Social;
34
35 static const wchar_t* IDB_ACCOUNT_ICON_ICQ = L"C02_Icon_ICQ.png";
36 static const wchar_t* IDB_ACCOUNT_ICON_YAHOO = L"C02_Icon_yahoo.png";
37 static const wchar_t* IDB_ACCOUNT_ICON_MSN = L"C02_Icon_windows_live.png";
38 static const wchar_t* IDB_ACCOUNT_ICON_OTHERS = L"C02_Icon_others.png";
39
40 static const wchar_t* CHARACTER_HYPHEN = L"-";
41 static const wchar_t* CHARACTER_EMPTY = L"";
42
43 ContactPresentationModel::ContactPresentationModel(void)
44 : __pAssignedCategoryList(null)
45 , __pContactListenerList(null)
46 , __pContact(null)
47 , __pAddressbook(null)
48 , __defaultNumberIndex(0)
49 , __lastRemovedContactIndex(-1)
50 , __lastRemovedEmailIndex(-1)
51 , __newMode(false)
52 , __isEditing(false)
53 {
54 }
55
56 ContactPresentationModel::~ContactPresentationModel(void)
57 {
58         delete __pAddressbook;
59         delete __pContact;
60
61         if (__pContactListenerList != null)
62         {
63                 __pContactListenerList->RemoveAll(true);
64                 delete __pContactListenerList;
65         }
66
67         if (__pAssignedCategoryList != null)
68         {
69                 __pAssignedCategoryList->RemoveAll(true);
70                 delete __pAssignedCategoryList;
71         }
72 }
73
74 result
75 ContactPresentationModel::Construct(void)
76 {
77         result r = E_SUCCESS;
78
79         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
80
81         __pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
82         if (__pAddressbook == null)
83         {
84                 return E_FAILURE;
85         }
86
87         r = __pAddressbook->SetAddressbookChangeEventListener(this);
88         TryCatch(r == E_SUCCESS, , "[%s] Unable to set event listener", GetErrorMessage(r));
89
90         __pContactListenerList = new (std::nothrow) ArrayList();
91         __pContactListenerList->Construct();
92
93         __pAssignedCategoryList = new (std::nothrow) ArrayList();
94         __pAssignedCategoryList->Construct();
95
96         return r;
97
98 CATCH:
99         delete __pAddressbook;
100         __pAddressbook = null;
101
102         __pContactListenerList->RemoveAll();
103         delete __pContactListenerList;
104         __pContactListenerList = null;
105
106         __pAssignedCategoryList->RemoveAll();
107         delete __pAssignedCategoryList;
108         __pAssignedCategoryList = null;
109
110         return r;
111 }
112
113 result
114 ContactPresentationModel::PrepareNewContact(void)
115 {
116         __pContact = new (std::nothrow) Contact();
117
118         __newMode = true;
119
120         return E_SUCCESS;
121 }
122
123 result
124 ContactPresentationModel::SetContactId(const Tizen::Social::RecordId contactId, bool isEditing)
125 {
126         result r = E_SUCCESS;
127
128         delete __pContact;
129         __pContact = __pAddressbook->GetContactN(contactId);
130
131         r = GetLastResult();
132         TryReturn(__pContact != null && r == E_SUCCESS, r, "[%s] selected Contact is invalid", GetErrorMessage(r));
133
134         IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
135         if (pList == null)
136         {
137                 return r;
138         }
139
140         Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(__pContact->GetPersonId());
141         if (pPerson == null)
142         {
143                 pList->RemoveAll(true);
144                 delete pList;
145
146                 return r;
147         }
148
149         PhoneNumber defaultNumber = pPerson->GetPrimaryPhoneNumber();
150
151         for (int i = 0; i < pList->GetCount(); i++)
152         {
153                 PhoneNumber* pPhoneNumber = static_cast<PhoneNumber*>(pList->GetAt(i));
154                 if (pPhoneNumber != null && pPhoneNumber->Equals(defaultNumber))
155                 {
156                         __defaultNumberIndex = i;
157                         break;
158                 }
159         }
160
161         if (isEditing)
162         {
163                 __isEditing = isEditing;
164
165                 IList* pAssignedCategoriesList = __pAddressbook->GetCategoriesByContactN(__pContact->GetRecordId());
166                 if (pAssignedCategoriesList == null)
167                 {
168                         return E_FAILURE;
169                 }
170                 IEnumerator* pEnum = pAssignedCategoriesList->GetEnumeratorN();
171                 if (pEnum == null)
172                 {
173                         return E_FAILURE;
174                 }
175
176                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
177                 {
178                         Category* pCategory = static_cast<Category *>(pEnum->GetCurrent());
179
180                         if (pCategory != null)
181                         {
182                                 AddContactToCategory(pCategory->GetRecordId());
183                         }
184                 }
185
186                 pAssignedCategoriesList->RemoveAll(true);
187                 delete pAssignedCategoriesList;
188                 delete pEnum;
189         }
190
191         pList->RemoveAll(true);
192         delete pList;
193         delete pPerson;
194
195         return r;
196 }
197
198 result
199 ContactPresentationModel::SetVcfPath(const Tizen::Base::String& filePath, bool isNewContact)
200 {
201         __vcfPath = filePath;
202         __newMode = isNewContact;
203
204         IList* pPhoneNumberList = null;
205         IList* pList = AddressbookManager::GetInstance()->ParseContactsFromVcardN(filePath);
206         result r = GetLastResult();
207         TryCatch(r == E_SUCCESS && pList != null && pList->GetCount() > 0, r = E_FAILURE, "[%s] ParseContactsFromVcardN() failed", GetErrorMessage(r));
208
209         __pContact = static_cast<Contact*>(pList->GetAt(0));
210         TryCatch(__pContact != null, r = E_FAILURE, "[E_FAILURE] Unable to cast Contact");
211
212         pPhoneNumberList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
213
214         if (pPhoneNumberList != null)
215         {
216                 PhoneNumber* pPhoneNumber = static_cast<PhoneNumber*>(pPhoneNumberList->GetAt(0));
217                 if (pPhoneNumber != null)
218                 {
219                         Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(__pContact->GetPersonId());
220                         if (pPerson != null)
221                         {
222                                 pPerson->SetAsPrimaryPhoneNumber(*pPhoneNumber);
223                         }
224
225                         delete pPerson;
226                 }
227                 delete pPhoneNumberList;
228         }
229
230 CATCH:
231         delete pList;
232
233         return r;
234 }
235
236 Tizen::Base::String
237 ContactPresentationModel::GetVcfPath(void)
238 {
239         return __vcfPath;
240 }
241
242 RecordId
243 ContactPresentationModel::GetContactId(void)
244 {
245         return __pContact->GetRecordId();
246 }
247
248 result
249 ContactPresentationModel::AddContactToCategory(Tizen::Social::RecordId categoryId)
250 {
251         result r = E_SUCCESS;
252
253         if (__newMode || __isEditing)
254         {
255                 String* pCategoryStringId = new (std::nothrow) String();
256
257                 pCategoryStringId->Append(categoryId);
258
259                 r = __pAssignedCategoryList->Add(*pCategoryStringId);
260         }
261         else
262         {
263                 r = AddressbookManager::GetInstance()->AddMemberToCategory(categoryId, __pContact->GetRecordId());
264         }
265
266         return r;
267 }
268
269 result
270 ContactPresentationModel::RemoveContactFromAllCategories(void)
271 {
272         result r = E_SUCCESS;
273
274         if (__newMode || __isEditing)
275         {
276                 if (__pAssignedCategoryList != null)
277                 {
278                         __pAssignedCategoryList->RemoveAll(true);
279                 }
280         }
281         else
282         {
283                 TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
284
285                 IList* pCategoryList = __pAddressbook->GetCategoriesByContactN(__pContact->GetRecordId());
286                 TryReturn(pCategoryList != null, E_FAILURE, "Unable to get category list");
287
288                 IEnumerator* pEnum = pCategoryList->GetEnumeratorN();
289                 while (pEnum->MoveNext() == E_SUCCESS)
290                 {
291                         Category* pCategory = static_cast<Category*>(pEnum->GetCurrent());
292                         if (pCategory != null)
293                         {
294                                 AddressbookManager::GetInstance()->RemoveMemberFromCategory(pCategory->GetRecordId(), __pContact->GetRecordId());
295                         }
296                 }
297
298                 delete pEnum;
299                 pCategoryList->RemoveAll(true);
300                 delete pCategoryList;
301         }
302
303         return r;
304 }
305
306 result
307 ContactPresentationModel::RemoveContact(void)
308 {
309         return __pAddressbook->RemoveContact(__pContact->GetRecordId());
310 }
311
312 bool
313 ContactPresentationModel::IsContactRemoved(void)
314 {
315         return (__pContact ? false : true);
316 }
317
318 result
319 ContactPresentationModel::AddContact(void)
320 {
321         return __pAddressbook->AddContact(*__pContact);
322 }
323
324 result
325 ContactPresentationModel::UpdateContact(void)
326 {
327         return __pAddressbook->UpdateContact(*__pContact);
328 }
329
330 String
331 ContactPresentationModel::GetValue(DetailProperty id, int index)
332 {
333         String returnString;
334         IList* pList = null;
335
336         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
337
338         switch (id)
339         {
340         case DETAIL_PROPERTY_GROUP:
341                 returnString = GetCategory();
342                 break;
343         case DETAIL_PROPERTY_RINGTONE:
344                 returnString = GetRingtone();
345                 break;
346         case DETAIL_PROPERTY_DEFAULT_PHONE_NUMBER:
347                 {
348                         DetailPhoneNumberType type;
349                         returnString = GetDefaultNumber(type);
350                 }
351                 break;
352         case DETAIL_PROPERTY_PHONE_NUMBER:
353                 {
354                         DetailPhoneNumberType type;
355                         returnString = GetPhoneNumber(index, type);
356                 }
357                 break;
358         case DETAIL_PROPERTY_EMAIL:
359                 {
360                         DetailEmailType type;
361                         returnString = GetEmail(index, type);
362                 }
363                 break;
364         case DETAIL_PROPERTY_IM_ADDRESS:
365                 {
366                         DetailImAddressType type;
367                         returnString = GetImAddress(index, type);
368                 }
369                 break;
370         case DETAIL_PROPERTY_ADDRESS: //fall through
371         case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE: //fall through
372         case DETAIL_PROPERTY_ADDRESS_COUNTRY: //fall through
373         case DETAIL_PROPERTY_ADDRESS_PROVINCE: //fall through
374         case DETAIL_PROPERTY_ADDRESS_CITY: //fall through
375         case DETAIL_PROPERTY_ADDRESS_STREET:
376                 {
377                         DetailAddressType type;
378                         returnString = GetAddress(id, index, type);
379                 }
380                 break;
381         case DETAIL_PROPERTY_BIRTHDAY:
382                 {
383                         returnString = GetDateTime(id);
384                 }
385                 break;
386         case DETAIL_PROPERTY_URL:
387                 {
388                         DetailUrlType type;
389                         returnString = GetUrl(index, type);
390                 }
391                 break;
392         case DETAIL_PROPERTY_NOTE:
393                 {
394                         IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_NOTES);
395
396                         if (pList == null)
397                         {
398                                 break;
399                         }
400
401                         String* pNote = static_cast<String *>(pList->GetAt(0));
402                         if (pNote == null)
403                         {
404                                 break;
405                         }
406
407                         returnString = *pNote;
408                 }
409                 break;
410         case DETAIL_PROPERTY_DISPLAY_NAME:
411                 {
412                         __pContact->GetValue(CONTACT_PROPERTY_ID_DISPLAY_NAME, returnString);
413                 }
414                 break;
415         case DETAIL_PROPERTY_FIRST_NAME:
416                 {
417                         __pContact->GetValue(CONTACT_PROPERTY_ID_FIRST_NAME, returnString);
418                 }
419                 break;
420         case DETAIL_PROPERTY_LAST_NAME:
421                 {
422                         __pContact->GetValue(CONTACT_PROPERTY_ID_LAST_NAME, returnString);
423                 }
424                 break;
425         case DETAIL_PROPERTY_MIDDLE_NAME:
426                 {
427                         __pContact->GetValue(CONTACT_PROPERTY_ID_MIDDLE_NAME, returnString);
428                 }
429                 break;
430         case DETAIL_PROPERTY_NICK_NAME:
431                 {
432                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_NICKNAMES);
433
434                         if (pList == null)
435                         {
436                                 break;
437                         }
438
439                         String* pNickName = static_cast<String *>(pList->GetAt(0));
440                         if (pNickName == null)
441                         {
442                                 break;
443                         }
444
445                         returnString = *pNickName;
446                 }
447                 break;
448         case DETAIL_PROPERTY_NAME_SUFFIX:
449                 {
450                         __pContact->GetValue(CONTACT_PROPERTY_ID_NAME_SUFFIX, returnString);
451                 }
452                 break;
453         case DETAIL_PROPERTY_JOB_TITLE:
454                 {
455                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ORGANIZATIONS);
456
457                         if (pList == null)
458                         {
459                                 break;
460                         }
461
462                         Organization* pOrganization = static_cast<Organization *>(pList->GetAt(0));
463                         if (pOrganization == null)
464                         {
465                                 break;
466                         }
467
468                         returnString = pOrganization->GetJobTitle();
469                 }
470                 break;
471         case DETAIL_PROPERTY_DEPARTMENT:
472                 {
473                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ORGANIZATIONS);
474
475                         if (pList == null)
476                         {
477                                 break;
478                         }
479
480                         Organization* pOrganization = static_cast<Organization *>(pList->GetAt(0));
481                         if (pOrganization == null)
482                         {
483                                 break;
484                         }
485
486                         returnString = pOrganization->GetDepartment();
487                 }
488                 break;
489         case DETAIL_PROPERTY_COMPANY:
490                 {
491                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ORGANIZATIONS);
492
493                         if (pList == null)
494                         {
495                                 break;
496                         }
497
498                         Organization* pOrganization = static_cast<Organization *>(pList->GetAt(0));
499                         if (pOrganization == null)
500                         {
501                                 break;
502                         }
503
504                         returnString = pOrganization->GetName();
505                 }
506                 break;
507         case DETAIL_PROPERTY_THUMBNAIL:
508                 {
509                         returnString = __pContact->GetThumbnailPath();
510                 }
511                 break;
512         default:
513                 break;
514         }
515
516         if (pList != null)
517         {
518                 pList->RemoveAll(true);
519                 delete pList;
520         }
521
522         return returnString;
523 }
524
525 result
526 ContactPresentationModel::GetBirthday(Tizen::Base::DateTime& value)
527 {
528         result r = E_SUCCESS;
529
530         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
531
532         IList* pEventList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_EVENTS);
533         if (pEventList == null)
534         {
535                 return E_FAILURE;
536         }
537
538         ContactEvent* pBirthday = static_cast<ContactEvent *>(pEventList->GetAt(0));
539         if (pBirthday == null)
540         {
541                 return E_FAILURE;
542         }
543
544         value = pBirthday->GetDate();
545
546         pEventList->RemoveAll(true);
547         delete pEventList;
548
549         return r;
550 }
551
552 Tizen::Base::String
553 ContactPresentationModel::GetType(DetailProperty id, int index)
554 {
555         String returnString;
556         String value;
557
558         switch (id)
559         {
560         case DETAIL_PROPERTY_DEFAULT_PHONE_NUMBER:
561                 {
562                         DetailPhoneNumberType type;
563
564                         value = GetDefaultNumber(type);
565                         returnString = GetPhoneNumberTypeString(type);
566                 }
567                 break;
568         case DETAIL_PROPERTY_PHONE_NUMBER:
569                 {
570                         DetailPhoneNumberType type;
571
572                         value = GetPhoneNumber(index, type);
573                         returnString = GetPhoneNumberTypeString(type);
574                 }
575                 break;
576         case DETAIL_PROPERTY_EMAIL:
577                 {
578                         DetailEmailType type;
579
580                         value = GetEmail(index, type);
581                         returnString = GetEmailTypeString(type);
582                 }
583                 break;
584         case DETAIL_PROPERTY_IM_ADDRESS:
585                 {
586                         DetailImAddressType type;
587
588                         value = GetImAddress(index, type);
589                         returnString = GetImAddressTypeString(type);
590                 }
591                 break;
592         case DETAIL_PROPERTY_ADDRESS:
593                 {
594                         DetailAddressType type;
595
596                         value = GetAddress(id, index, type);
597                         returnString = GetAddressTypeString(type);
598                 }
599                 break;
600         case DETAIL_PROPERTY_URL:
601                 {
602                         DetailUrlType type;
603
604                         value = GetUrl(index, type);
605                         returnString = GetUrlTypeString(type);
606
607                 }
608                 break;
609         default:
610                 break;
611         }
612         return returnString;
613 }
614
615 Tizen::Graphics::Bitmap*
616 ContactPresentationModel::GetThumbnailN(void)
617 {
618         TryReturn(__pContact != null, null, "[E_FAILURE] The contact is invalid.");
619
620         return __pContact->GetThumbnailN();
621 }
622
623 Tizen::Graphics::Bitmap*
624 ContactPresentationModel::GetAccountIconN(const Tizen::Base::String& account)
625 {
626         if (account.Equals(IM_ADDRESS_YAHOO, true))
627         {
628                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_YAHOO);
629         }
630         else if (account.Equals(IM_ADDRESS_MSN, true))
631         {
632                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_MSN);
633         }
634         else if (account.Equals(IM_ADDRESS_ICQ, true))
635         {
636                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_ICQ);
637         }
638 //      else if (account.Equals(IM_ADDRESS_AIM, true))
639 //      {
640 //              //[TODO]
641 //      }
642 //      else if (account.Equals(IM_ADDRESS_SKYPE, true))
643 //      {
644 //              //[TODO]
645 //      }
646 //      else if (account.Equals(IM_ADDRESS_QQ, true))
647 //      {
648 //              //[TODO]
649 //      }
650 //      else if (account.Equals(IM_ADDRESS_GOOGLE_TALK, true))
651 //      {
652 //              //[TODO]
653 //      }
654 //      else if (account.Equals(IM_ADDRESS_JABBER, true))
655 //      {
656 //              //[TODO]
657 //      }
658         else
659         {
660                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_OTHERS);
661         }
662
663         return null;
664 }
665
666 int
667 ContactPresentationModel::GetMultiValuesCount(DetailProperty id)
668 {
669         int returnValue = 0;
670         IList* pList = null;
671
672         TryReturn(__pContact != null, returnValue, "[E_FAILURE] The contact is invalid.");
673
674         switch (id)
675         {
676         case DETAIL_PROPERTY_PHONE_NUMBER:
677                 {
678                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
679                         if (pList != null)
680                         {
681                                 returnValue = pList->GetCount();
682                         }
683                 }
684                 break;
685         case DETAIL_PROPERTY_EMAIL:
686                 {
687                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_EMAILS);
688                         if (pList != null)
689                         {
690                                 returnValue = pList->GetCount();
691                         }
692                 }
693                 break;
694         case DETAIL_PROPERTY_IM_ADDRESS:
695                 {
696                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_IMADDRESSES);
697                         if (pList != null)
698                         {
699                                 returnValue = pList->GetCount();
700                         }
701                 }
702                 break;
703         case DETAIL_PROPERTY_ADDRESS:
704                 {
705                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ADDRESSES);
706                         if (pList != null)
707                         {
708                                 returnValue = pList->GetCount();
709                         }
710                 }
711                 break;
712         case DETAIL_PROPERTY_URL:
713                 {
714                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_URLS);
715                         if (pList != null)
716                         {
717                                 returnValue = pList->GetCount();
718                         }
719                 }
720                 break;
721         default:
722                 break;
723         }
724
725         if (pList != null)
726         {
727                 pList->RemoveAll(true);
728                 delete pList;
729         }
730
731         return returnValue;
732 }
733
734 int
735 ContactPresentationModel::GetCategoryCount(void)
736 {
737         return __pAddressbook->GetCategoryCount();
738 }
739
740 Tizen::Base::Collection::IList*
741 ContactPresentationModel::GetAssignedCategoryIdListN(void)
742 {
743         IEnumerator* pEnum = null;
744         ArrayList* pCategoryList = new (std::nothrow) ArrayList();
745         pCategoryList->Construct();
746
747         if (__newMode || __isEditing)
748         {
749                 pEnum = __pAssignedCategoryList->GetEnumeratorN();
750
751                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
752                 {
753                         String* pCategoryStringId = static_cast<String *>(pEnum->GetCurrent());
754
755                         if (pCategoryStringId != null)
756                         {
757                                 String* pRetrunCategoryStringId = new (std::nothrow) String(*pCategoryStringId);
758
759                                 pCategoryList->Add(*pRetrunCategoryStringId);
760                         }
761                 }
762
763                 delete pEnum;
764
765                 return pCategoryList;
766         }
767         else
768         {
769                 IList* pList = __pAddressbook->GetCategoriesByContactN(__pContact->GetRecordId());
770
771                 if (pList != null)
772                 {
773                         pEnum = pList->GetEnumeratorN();
774
775                         for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
776                         {
777                                 Category* pCategory = static_cast<Category *>(pEnum->GetCurrent());
778
779                                 if (pCategory != null)
780                                 {
781                                         String* pRetrunCategoryStringId = new (std::nothrow) String();
782
783                                         pRetrunCategoryStringId->Append(pCategory->GetRecordId());
784                                         pCategoryList->Add(*pRetrunCategoryStringId);
785                                 }
786                         }
787
788                         pList->RemoveAll(true);
789                         delete pList;
790                         delete pEnum;
791                 }
792
793                 return pCategoryList;
794         }
795 }
796
797 Tizen::Base::String
798 ContactPresentationModel::GetCategory(void)
799 {
800         String returnString;
801
802         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
803
804         if (__newMode || __isEditing)
805         {
806                 IEnumerator* pEnum = __pAssignedCategoryList->GetEnumeratorN();
807                 Category* pCategory = null;
808                 String* pCategoryStringId = null;
809                 RecordId categoryId;
810
811                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
812                 {
813                         pCategoryStringId = static_cast<String*>(pEnum->GetCurrent());
814
815                         if (pCategoryStringId != null)
816                         {
817                                 Integer::Parse(*pCategoryStringId, categoryId);
818                                 pCategory = __pAddressbook->GetCategoryN(categoryId);
819
820                                 String categoryName;
821                                 if (pCategory != null)
822                                 {
823                                         categoryName = pCategory->GetName();
824                                         // Not supported - if (pCategory->IsDefault())
825                                         {
826                                                 GetDefaultCategoryName(categoryName);
827                                         }
828
829                                         if (index == 0)
830                                         {
831                                                 returnString = categoryName;
832                                         }
833                                         else if (index > 0)
834                                         {
835                                                 returnString += L", ";
836                                                 returnString += categoryName;
837                                         }
838                                 }
839                                 delete pCategory;
840                         }
841                 }
842                 delete pEnum;
843         }
844         else
845         {
846                 Category* pCategory = null;
847                 IList* pCategoryList = __pAddressbook->GetCategoriesByContactN(__pContact->GetRecordId());
848                 if (pCategoryList == null)
849                 {
850                         return returnString;
851                 }
852
853                 IEnumerator* pEnum = pCategoryList->GetEnumeratorN();
854                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
855                 {
856                         pCategory = static_cast<Category*>(pEnum->GetCurrent());
857
858                         String categoryName;
859                         if (pCategory != null)
860                         {
861                                 categoryName = pCategory->GetName();
862                                 // Not supported - if (pCategory->IsDefault())
863                                 {
864                                          GetDefaultCategoryName(categoryName);
865                                 }
866
867                                 if (index == 0)
868                                 {
869                                         returnString = categoryName;
870                                 }
871                                 else if (index > 0)
872                                 {
873                                         returnString += L", ";
874                                         returnString += categoryName;
875                                 }
876                         }
877                 }
878
879                 delete pEnum;
880                 pCategoryList->RemoveAll(true);
881                 delete pCategoryList;
882         }
883
884         return returnString;
885 }
886
887 result
888 ContactPresentationModel::GetDefaultCategoryName(Tizen::Base::String& categoryName)
889 {
890         if (categoryName.Equals(DEFAULT_GROUP_FAMILY, true))
891         {
892                 categoryName.Clear();
893                 categoryName = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT_GROUP_FAMILY");
894         }
895         else if (categoryName.Equals(DEFAULT_GROUP_FRIENDS, true))
896         {
897                 categoryName.Clear();
898                 categoryName = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT_GROUP_FRIENDS");
899         }
900         else if (categoryName.Equals(DEFAULT_GROUP_COWORKERS, true))
901         {
902                 categoryName.Clear();
903                 categoryName = ResourceManager::GetString(L"IDS_PB_BODY_CO_WORKERS");
904         }
905
906         return E_SUCCESS;
907 }
908
909 Tizen::Base::String
910 ContactPresentationModel::GetRingtone(void)
911 {
912         String returnString;
913         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
914
915         result r = __pContact->GetValue(CONTACT_PROPERTY_ID_RINGTONE, returnString);
916         TryReturn(r == E_SUCCESS, String(), "[E_FAILURE] Unable to get value");
917         
918         if (returnString.IsEmpty())
919         {
920                 returnString = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT");
921         }
922         else
923         {
924                 File file;
925                 bool isFileExisting;
926                 isFileExisting = file.IsFileExist(returnString);
927                 if(!isFileExisting)
928                 {
929                         SetValue(DETAIL_PROPERTY_RINGTONE, String());
930                         UpdateContact();
931                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT");
932                 }
933         }
934
935         return returnString;
936 }
937
938 Tizen::Base::String
939 ContactPresentationModel::GetDefaultNumber(DetailPhoneNumberType& type)
940 {
941         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
942
943         String returnString;
944
945         IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
946         TryReturn(pList != null, String(), "[E_FAILURE] The contact doesn't have a phone number at least.");
947
948         PhoneNumber* pPhoneNumber = static_cast<PhoneNumber*>(pList->GetAt(__defaultNumberIndex));
949         if (pPhoneNumber != null)
950         {
951                 returnString = pPhoneNumber->GetPhoneNumber();
952                 if (returnString.Contains(CHARACTER_HYPHEN))
953                 {
954                         returnString.Replace(CHARACTER_HYPHEN, CHARACTER_EMPTY);
955                 }
956                 type = (DetailPhoneNumberType)pPhoneNumber->GetType();
957         }
958
959         pList->RemoveAll(true);
960         delete pList;
961
962         return returnString;
963 }
964
965 int
966 ContactPresentationModel::GetDefaultNumberIndex(void)
967 {
968         return __defaultNumberIndex;
969 }
970
971 Tizen::Base::String
972 ContactPresentationModel::GetPhoneNumber(int index, DetailPhoneNumberType& type)
973 {
974         String returnString;
975
976         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
977
978         if (index == -1)
979         {
980                 return returnString;
981         }
982
983         IList* pPhoneNumberList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
984         AppAssert(pPhoneNumberList);
985
986         PhoneNumber* pNumber = static_cast<PhoneNumber *>(pPhoneNumberList->GetAt(index));
987         if (pNumber != null)
988         {
989                 returnString = pNumber->GetPhoneNumber();
990
991                 if (returnString.Contains(CHARACTER_HYPHEN))
992                 {
993                         returnString.Replace(CHARACTER_HYPHEN, CHARACTER_EMPTY);
994                 }
995                 type = (DetailPhoneNumberType)pNumber->GetType();
996         }
997
998         pPhoneNumberList->RemoveAll(true);
999         delete pPhoneNumberList;
1000
1001         return returnString;
1002 }
1003
1004 Tizen::Base::String
1005 ContactPresentationModel::GetEmail(int index, DetailEmailType& type)
1006 {
1007         String returnString;
1008
1009         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1010
1011         if (index == -1)
1012         {
1013                 return returnString;
1014         }
1015
1016         IList* pEmailList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_EMAILS);
1017         AppAssert(pEmailList);
1018
1019         Email* pEmail = static_cast<Email *>(pEmailList->GetAt(index));
1020         if (pEmail != null)
1021         {
1022                 returnString = pEmail->GetEmail();
1023                 type = (DetailEmailType)pEmail->GetType();
1024         }
1025
1026         pEmailList->RemoveAll(true);
1027         delete pEmailList;
1028
1029         return returnString;
1030 }
1031
1032 Tizen::Base::String
1033 ContactPresentationModel::GetImAddress(int index, DetailImAddressType& type)
1034 {
1035         String returnString;
1036
1037         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1038
1039         if (index == -1)
1040         {
1041                 return returnString;
1042         }
1043
1044         IList* pImAddressList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_IMADDRESSES);
1045         AppAssert(pImAddressList);
1046
1047         ImAddress* pImAddress =  static_cast<ImAddress *>(pImAddressList->GetAt(index));
1048         if (pImAddress != null)
1049         {
1050                 returnString = pImAddress->GetImAddress();
1051
1052                 String typeString = pImAddress->GetServiceProviderName();
1053
1054                 if (typeString.Equals(IM_ADDRESS_AIM, true))
1055                 {
1056                         type = DETAIL_IM_ADDRESS_TYPE_AIM;
1057                 }
1058                 else if (typeString.Equals(IM_ADDRESS_MSN, true))
1059                 {
1060                         type = DETAIL_IM_ADDRESS_TYPE_MSN;
1061                 }
1062                 else if (typeString.Equals(IM_ADDRESS_YAHOO, true))
1063                 {
1064                         type = DETAIL_IM_ADDRESS_TYPE_YAHOO;
1065                 }
1066                 else if (typeString.Equals(IM_ADDRESS_SKYPE, true))
1067                 {
1068                         type = DETAIL_IM_ADDRESS_TYPE_SKYPE;
1069                 }
1070                 else if (typeString.Equals(IM_ADDRESS_QQ, true))
1071                 {
1072                         type = DETAIL_IM_ADDRESS_TYPE_QQ;
1073                 }
1074                 else if (typeString.Equals(IM_ADDRESS_GOOGLE_TALK, true))
1075                 {
1076                         type = DETAIL_IM_ADDRESS_TYPE_GOOGLE_TALK;
1077                 }
1078                 else if (typeString.Equals(IM_ADDRESS_ICQ, true))
1079                 {
1080                         type = DETAIL_IM_ADDRESS_TYPE_ICQ;
1081                 }
1082                 else if (typeString.Equals(IM_ADDRESS_JABBER, true))
1083                 {
1084                         type = DETAIL_IM_ADDRESS_TYPE_JABBER;
1085                 }
1086                 else
1087                 {
1088                         type = DETAIL_IM_ADDRESS_TYPE_OTHER;
1089                 }
1090         }
1091
1092         pImAddressList->RemoveAll(true);
1093         delete pImAddressList;
1094
1095         return returnString;
1096 }
1097
1098 Tizen::Base::String
1099 ContactPresentationModel::GetAddress(DetailProperty id, int index, DetailAddressType& type)
1100 {
1101         String returnString;
1102
1103         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1104
1105         if (index == -1)
1106         {
1107                 return returnString;
1108         }
1109
1110         IList* pAddressList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ADDRESSES);
1111         AppAssert(pAddressList);
1112
1113         Address* pAddress = static_cast<Address *>(pAddressList->GetAt(index));
1114         if (pAddress != null)
1115         {
1116                 String street = pAddress->GetStreet();
1117                 String city = pAddress->GetCity();
1118                 String state = pAddress->GetState();
1119                 String country = pAddress->GetCountry();
1120                 String postalCode = pAddress->GetPostalCode();
1121
1122                 switch (id)
1123                 {
1124                 case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE:
1125                         returnString = postalCode;
1126                         break;
1127                 case DETAIL_PROPERTY_ADDRESS_COUNTRY:
1128                         returnString = country;
1129                         break;
1130                 case DETAIL_PROPERTY_ADDRESS_PROVINCE:
1131                         returnString = state;
1132                         break;
1133                 case DETAIL_PROPERTY_ADDRESS_CITY:
1134                         returnString = city;
1135                         break;
1136                 case DETAIL_PROPERTY_ADDRESS_STREET:
1137                         returnString = street;
1138                         break;
1139                 case DETAIL_PROPERTY_ADDRESS:
1140                         if (postalCode.IsEmpty() == false)
1141                         {
1142                                 returnString.Append(postalCode);
1143                                 returnString.Append(CHARACTER_SPACE);
1144                         }
1145                         if (country.IsEmpty() == false)
1146                         {
1147                                 returnString.Append(country);
1148                                 returnString.Append(CHARACTER_SPACE);
1149                         }
1150                         if (state.IsEmpty() == false)
1151                         {
1152                                 returnString.Append(state);
1153                                 returnString.Append(CHARACTER_SPACE);
1154                         }
1155                         if (city.IsEmpty() == false)
1156                         {
1157                                 returnString.Append(city);
1158                                 returnString.Append(CHARACTER_SPACE);
1159                         }
1160                         if (street.IsEmpty() == false)
1161                         {
1162                                 returnString.Append(street);
1163                         }
1164                         break;
1165                 default:
1166                         break;
1167                 }
1168
1169                 type = (DetailAddressType)pAddress->GetType();
1170         }
1171
1172         pAddressList->RemoveAll(true);
1173         delete pAddressList;
1174
1175         return returnString;
1176 }
1177
1178 Tizen::Base::String
1179 ContactPresentationModel::GetDateTime(DetailProperty id)
1180 {
1181         result r = E_SUCCESS;
1182         String returnString;
1183         int day = 0;
1184         int year = 0;
1185         String month;
1186         DateTimeSymbols dateTimeSymbols;
1187
1188         r = dateTimeSymbols.Construct(CALENDAR_GREGORIAN);
1189         TryReturn(r == E_SUCCESS, String(), "[E_FAILURE] Given locale is not supported.");
1190         const IList* pMonthList = dateTimeSymbols.GetShortMonths();
1191
1192         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1193
1194         DateTime* pDateTime = new (std::nothrow) DateTime();
1195
1196         if (id == DETAIL_PROPERTY_BIRTHDAY)
1197         {
1198                 const Object* pGettingMonth = null;
1199
1200                 r = GetBirthday(*pDateTime);
1201                 TryCatch(r == E_SUCCESS, , "[%s] Unable to get value", GetErrorMessage(r));
1202
1203                 day = pDateTime->GetDay();
1204                 year = pDateTime->GetYear();
1205
1206                 pGettingMonth = pMonthList->GetAt(pDateTime->GetMonth() - 1);
1207                 TryCatch(pGettingMonth != null, , "[E_FAILURE] Unable to get a month", GetErrorMessage(r));
1208
1209                 month.Append(*static_cast<String*>(const_cast<Object*>(pGettingMonth)));
1210
1211                 returnString.Append(day);
1212                 returnString.Append(CHARACTER_SPACE);
1213                 returnString.Append(month);
1214                 returnString.Append(CHARACTER_SPACE);
1215                 returnString.Append(year);
1216         }
1217
1218 CATCH:
1219         delete pDateTime;
1220
1221         return returnString;
1222 }
1223
1224 Tizen::Base::String
1225 ContactPresentationModel::GetUrl(int index, DetailUrlType& type)
1226 {
1227         String returnString;
1228
1229         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1230
1231         IList* pUrlList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_URLS);
1232         if (pUrlList == null)
1233         {
1234                 return returnString;
1235         }
1236
1237         Url* pUrl = static_cast<Url*>(pUrlList->GetAt(index));
1238         if (pUrl != null)
1239         {
1240                 returnString = pUrl->GetUrl();
1241                 type = (DetailUrlType)pUrl->GetType();
1242         }
1243
1244         pUrlList->RemoveAll(true);
1245         delete pUrlList;
1246
1247         return returnString;
1248 }
1249
1250 void
1251 ContactPresentationModel::AddContactChangeListener(const IContactEventListener& listener)
1252 {
1253         __pContactListenerList->Add(listener);
1254 }
1255
1256 void
1257 ContactPresentationModel::RemoveContactChangeListener(const IContactEventListener& listener)
1258 {
1259         __pContactListenerList->Remove(listener, false);
1260 }
1261
1262 void
1263 ContactPresentationModel::OnContactsChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
1264 {
1265         if (__newMode == false && __isEditing == false)
1266         {
1267                 if (__pContact != null)
1268                 {
1269                         SetContactId(__pContact->GetRecordId());
1270                 }
1271         }
1272         else if (__isEditing)
1273         {
1274                 IEnumerator* pEnum = contactChangeInfoList.GetEnumeratorN();
1275
1276                 if (pEnum != null && __pContact != null)
1277                 {
1278                         while (pEnum->MoveNext() == E_SUCCESS)
1279                         {
1280                                 const ContactChangeInfo* pChangeInfo = static_cast<const ContactChangeInfo *>(pEnum->GetCurrent());
1281
1282                                 if (pChangeInfo != null && pChangeInfo->GetChangeType() == RECORD_CHANGE_TYPE_REMOVED && __pContact->GetRecordId() == pChangeInfo->GetContactId())
1283                                 {
1284                                         SetContactId(INVALID_RECORD_ID);
1285                                         break;
1286                                 }
1287                         }
1288                         delete pEnum;
1289                 }
1290         }
1291
1292         IContactEventListener* pInterface = null;
1293         IEnumerator* pEnum = __pContactListenerList->GetEnumeratorN();
1294         while(pEnum->MoveNext() == E_SUCCESS)
1295         {
1296                 pInterface = static_cast<IContactEventListener*>(pEnum->GetCurrent());
1297                 if (pInterface == null)
1298                 {
1299                         delete pEnum;
1300                         return;
1301                 }
1302                 pInterface->OnContactsChanged();
1303         }
1304
1305         delete pEnum;
1306 }
1307
1308 void
1309 ContactPresentationModel::OnCategoriesChanged(const Tizen::Base::Collection::IList& categoryChangeInfoList)
1310 {
1311         IContactEventListener* pInterface = null;
1312         IEnumerator* pEnum = __pContactListenerList->GetEnumeratorN();
1313         while(pEnum->MoveNext() == E_SUCCESS)
1314         {
1315                 pInterface = static_cast<IContactEventListener*>(pEnum->GetCurrent());
1316                 if (pInterface == null)
1317                 {
1318                         delete pEnum;
1319                         return;
1320                 }
1321                 pInterface->OnCategoriesChanged();
1322         }
1323
1324         delete pEnum;
1325 }
1326
1327 result
1328 ContactPresentationModel::SetValue(DetailProperty id, const Tizen::Base::String& value, int index)
1329 {
1330         result r = E_SUCCESS;
1331
1332         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1333
1334         String trimmedString(value);
1335         trimmedString.Trim();
1336
1337         Email email = Email();
1338         ImAddress imAddress = ImAddress();
1339         Url url = Url();
1340         PhoneNumber phoneNumber = PhoneNumber();
1341
1342         switch (id)
1343         {
1344         case DETAIL_PROPERTY_RINGTONE:
1345                 {
1346                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_RINGTONE, trimmedString);
1347                 }
1348                 break;
1349         case DETAIL_PROPERTY_DEFAULT_PHONE_NUMBER:
1350                 {
1351                         __defaultNumberIndex = index;
1352                 }
1353                 break;
1354         case DETAIL_PROPERTY_PHONE_NUMBER:
1355                 {
1356                         r = SetPhoneNumber(DETAIL_PHONENUMBER_TYPE_MOBILE, trimmedString, index);
1357                 }
1358                 break;
1359         case DETAIL_PROPERTY_EMAIL:
1360                 {
1361                         r = SetEmail(DETAIL_EMAIL_TYPE_HOME, trimmedString, index);
1362                 }
1363                 break;
1364         case DETAIL_PROPERTY_IM_ADDRESS:
1365                 {
1366                         r = SetImAddress(DETAIL_IM_ADDRESS_TYPE_MSN, trimmedString, index);
1367                 }
1368                 break;
1369         case DETAIL_PROPERTY_ADDRESS: //fall through
1370         case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE: //fall through
1371         case DETAIL_PROPERTY_ADDRESS_COUNTRY: //fall through
1372         case DETAIL_PROPERTY_ADDRESS_PROVINCE: //fall through
1373         case DETAIL_PROPERTY_ADDRESS_CITY: //fall through
1374         case DETAIL_PROPERTY_ADDRESS_STREET:
1375                 {
1376                         SetAddress(id, trimmedString);
1377                         __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ADDRESSES, index);
1378                         r = __pContact->SetAddressAt(index, __address);
1379                         if (r != E_SUCCESS)
1380                         {
1381                                 r = __pContact->AddAddress(__address);
1382                         }
1383                 }
1384                 break;
1385         case DETAIL_PROPERTY_URL:
1386                 {
1387                         SetUrl(DETAIL_URL_TYPE_HOME, trimmedString, index);
1388                 }
1389                 break;
1390         case DETAIL_PROPERTY_NOTE:
1391                 {
1392                         if (value.IsEmpty() == true)
1393                         {
1394                                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_NOTES, index);
1395
1396                                 return r;
1397                         }
1398
1399                         r = __pContact->SetNoteAt(index, value);
1400                         if (r != E_SUCCESS)
1401                         {
1402                                 r = __pContact->AddNote(value);
1403                         }
1404                 }
1405                 break;
1406         case DETAIL_PROPERTY_DISPLAY_NAME:
1407                 {
1408                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_DISPLAY_NAME, trimmedString);
1409                 }
1410                 break;
1411         case DETAIL_PROPERTY_FIRST_NAME:
1412                 {
1413                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_FIRST_NAME, trimmedString);
1414                 }
1415                 break;
1416         case DETAIL_PROPERTY_LAST_NAME:
1417                 {
1418                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_LAST_NAME, trimmedString);
1419                 }
1420                 break;
1421         case DETAIL_PROPERTY_MIDDLE_NAME:
1422                 {
1423                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_MIDDLE_NAME, trimmedString);
1424                 }
1425                 break;
1426         case DETAIL_PROPERTY_NICK_NAME:
1427                 {
1428                         if (trimmedString.IsEmpty() == true)
1429                         {
1430                                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_NICKNAMES, index);
1431
1432                                 return r;
1433                         }
1434
1435                         r = __pContact->SetNicknameAt(index, trimmedString);
1436                         if (r != E_SUCCESS)
1437                         {
1438                                 r = __pContact->AddNickname(trimmedString);
1439                         }
1440                 }
1441                 break;
1442         case DETAIL_PROPERTY_NAME_SUFFIX:
1443                 {
1444                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_NAME_SUFFIX, trimmedString);
1445                 }
1446                 break;
1447         case DETAIL_PROPERTY_JOB_TITLE:
1448                 {
1449                         r = SetOrganization(DETAIL_PROPERTY_JOB_TITLE, trimmedString);
1450                 }
1451                 break;
1452         case DETAIL_PROPERTY_DEPARTMENT:
1453                 {
1454                         r = SetOrganization(DETAIL_PROPERTY_DEPARTMENT, trimmedString);
1455                 }
1456                 break;
1457         case DETAIL_PROPERTY_COMPANY:
1458                 {
1459                         r = SetOrganization(DETAIL_PROPERTY_COMPANY, trimmedString);
1460                 }
1461                 break;
1462         default:
1463                 break;
1464         }
1465
1466         return r;
1467 }
1468
1469 result
1470 ContactPresentationModel::AddAddressBook(void)
1471 {
1472         result r = E_SUCCESS;
1473
1474         if (__newMode == true)
1475         {
1476                 r = AddContact();
1477
1478                 RecordId newPersonId;
1479                 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
1480                 IList* pList = pAddressbookManager->GetContactsByPersonN(__pContact->GetPersonId());
1481
1482                 if (pList != null && pList->GetCount() > 1)
1483                 {
1484                         r = pAddressbookManager->UnlinkContact(__pContact->GetPersonId(), __pContact->GetRecordId(), newPersonId);
1485                 }
1486
1487                 delete pList;
1488         }
1489         else
1490         {
1491                 r = UpdateContact();
1492         }
1493
1494         if (r == E_SUCCESS)
1495         {
1496                 IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
1497                 TryReturn(pList != null, r, "[E_FAILURE] The contact doesn't have a phone number at least.");
1498
1499                 PhoneNumber* pPhoneNumber = static_cast<PhoneNumber*>(pList->GetAt(__defaultNumberIndex));
1500                 if (pPhoneNumber != null)
1501                 {
1502                         Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(__pContact->GetPersonId());
1503                         if (pPerson != null)
1504                         {
1505                                 pPerson->SetAsPrimaryPhoneNumber(*pPhoneNumber);
1506                         }
1507                         delete pPerson;
1508                 }
1509
1510                 pList->RemoveAll(true);
1511                 delete pList;
1512         }
1513
1514         if (__newMode || __isEditing)
1515         {
1516                 if (__isEditing == true)
1517                 {
1518                         __isEditing = false;
1519                         RemoveContactFromAllCategories();
1520                         __isEditing = true;
1521                 }
1522
1523                 IEnumerator* pEnum = __pAssignedCategoryList->GetEnumeratorN();
1524                 String* pCategoryStringId = null;
1525                 RecordId categoryId;
1526
1527                 while (pEnum->MoveNext() == E_SUCCESS)
1528                 {
1529                         pCategoryStringId = static_cast<String*>(pEnum->GetCurrent());
1530
1531                         if (pCategoryStringId != null)
1532                         {
1533                                 Integer::Parse(*pCategoryStringId, categoryId);
1534                                 r = __pAddressbook->AddMemberToCategory(categoryId, __pContact->GetRecordId());
1535                         }
1536                 }
1537                 delete pEnum;
1538         }
1539
1540         return r;
1541 }
1542
1543 result
1544 ContactPresentationModel::SetAddress(DetailProperty id, const Tizen::Base::String& value)
1545 {
1546         result r = E_SUCCESS;
1547
1548         switch (id)
1549         {
1550         case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE:
1551                 r = __address.SetPostalCode(value);
1552                 break;
1553         case DETAIL_PROPERTY_ADDRESS_COUNTRY:
1554                 r = __address.SetCountry(value);
1555                 break;
1556         case DETAIL_PROPERTY_ADDRESS_PROVINCE:
1557                 r = __address.SetState(value);
1558                 break;
1559         case DETAIL_PROPERTY_ADDRESS_CITY:
1560                 r = __address.SetCity(value);
1561                 break;
1562         case DETAIL_PROPERTY_ADDRESS_STREET:
1563                 r = __address.SetStreet(value);
1564                 break;
1565         default:
1566                 break;
1567         }
1568
1569         return r;
1570 }
1571
1572 result
1573 ContactPresentationModel::SetBirthday(const Tizen::Base::DateTime& value)
1574 {
1575         result r = E_SUCCESS;
1576
1577         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1578
1579         ContactEvent birthday;
1580         birthday.SetDate(value);
1581
1582         r = __pContact->SetEventAt(0, birthday);
1583         if (r != E_SUCCESS)
1584         {
1585                 r = __pContact->AddEvent(birthday);
1586         }
1587
1588         return r;
1589 }
1590
1591 result
1592 ContactPresentationModel::SetPhoneNumber(DetailPhoneNumberType type, const Tizen::Base::String& value, int index)
1593 {
1594         result r = E_SUCCESS;
1595
1596         if(index==0)
1597         {
1598                 __lastRemovedContactIndex=-1;
1599         }
1600
1601         if (value.IsEmpty() == true)
1602         {
1603                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_PHONE_NUMBERS, index);
1604                 __lastRemovedContactIndex=index;
1605
1606                 if(index == __defaultNumberIndex)
1607                 {
1608                         if(index>0)
1609                         {
1610                                 __defaultNumberIndex = __defaultNumberIndex-1;
1611                         }
1612                 }
1613
1614                 return r;
1615         }
1616
1617         PhoneNumber phoneNumber;
1618         phoneNumber.SetPhoneNumber(value);
1619         phoneNumber.SetType((PhoneNumberType)type);
1620
1621         if(__lastRemovedContactIndex!=-1 && index>__lastRemovedContactIndex)
1622         {
1623                 r = __pContact->SetPhoneNumberAt(index-1, phoneNumber);
1624         }
1625         else
1626         {
1627                 r = __pContact->SetPhoneNumberAt(index, phoneNumber);
1628         }
1629
1630         if (r != E_SUCCESS)
1631         {
1632                 r = __pContact->AddPhoneNumber(phoneNumber);
1633         }
1634
1635         return r;
1636 }
1637
1638 result
1639 ContactPresentationModel::SetEmail(DetailEmailType type, const Tizen::Base::String& value, int index)
1640 {
1641         result r = E_SUCCESS;
1642
1643         if (index==0)
1644         {
1645                 __lastRemovedEmailIndex = -1;
1646         }
1647
1648         if (value.IsEmpty() == true)
1649         {
1650                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_EMAILS, index);
1651                 __lastRemovedEmailIndex = index;
1652
1653                 return r;
1654         }
1655
1656         Email email;
1657         email.SetEmail(value);
1658         email.SetType((EmailType)type);
1659
1660
1661         if (__lastRemovedEmailIndex >= 0 && __lastRemovedEmailIndex < index)
1662         {
1663                 r = __pContact->SetEmailAt(index - 1, email);
1664         }
1665         else
1666         {
1667                 r = __pContact->SetEmailAt(index, email);
1668         }
1669
1670
1671         if (r != E_SUCCESS)
1672         {
1673                 r = __pContact->AddEmail(email);
1674         }
1675
1676         return r;
1677 }
1678
1679 result
1680 ContactPresentationModel::SetAddress(DetailAddressType type, const Tizen::Base::String& value, int index)
1681 {
1682         result r = E_SUCCESS;
1683
1684         if (value.IsEmpty() == true)
1685         {
1686                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ADDRESSES, index);
1687
1688                 return r;
1689         }
1690
1691         Address address;
1692         address.SetType((AddressType)type);
1693
1694         r = __pContact->SetAddressAt(index, address);
1695         if (r != E_SUCCESS)
1696         {
1697                 r = __pContact->AddAddress(__address);
1698         }
1699
1700         return r;
1701 }
1702
1703 result
1704 ContactPresentationModel::SetUrl(DetailUrlType type, const Tizen::Base::String& value, int index)
1705 {
1706         result r = E_SUCCESS;
1707
1708         if (value.IsEmpty() == true)
1709         {
1710                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_URLS, index);
1711
1712                 return r;
1713         }
1714
1715         Url url;
1716         url.SetType(UrlType(type));
1717         url.SetUrl(value);
1718
1719         r = __pContact->SetUrlAt(index, url);
1720         if (r != E_SUCCESS)
1721         {
1722                 r = __pContact->AddUrl(url);
1723         }
1724
1725         return r;
1726 }
1727
1728 result
1729 ContactPresentationModel::SetImAddress(DetailImAddressType type, const Tizen::Base::String& value, int index)
1730 {
1731         result r = E_SUCCESS;
1732
1733         if (value.IsEmpty() == true)
1734         {
1735                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_IMADDRESSES, index);
1736
1737                 return r;
1738         }
1739
1740         ImAddress imAddress;
1741         imAddress.SetImAddress(value);
1742         imAddress.SetServiceProviderName(GetImAddressTypeString(type));
1743         r = __pContact->SetImAddressAt(index, imAddress);
1744
1745         if (r != E_SUCCESS)
1746         {
1747                 r = __pContact->AddImAddress(imAddress);
1748         }
1749
1750         return r;
1751 }
1752
1753 result
1754 ContactPresentationModel::SetThumbnail(const Tizen::Base::String& filePath)
1755 {
1756         result r = E_SUCCESS;
1757
1758         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1759
1760         r = __pContact->SetThumbnail(filePath);
1761
1762         return r;
1763 }
1764
1765 result
1766 ContactPresentationModel::SetOrganization(DetailProperty id, const Tizen::Base::String& value)
1767 {
1768         TryReturn((id >= DETAIL_PROPERTY_JOB_TITLE && id <= DETAIL_PROPERTY_COMPANY), E_FAILURE, "[E_FAILURE] Unable to set the specific id");
1769
1770         result r = E_SUCCESS;
1771
1772         IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ORGANIZATIONS);
1773
1774         if (pList == null)
1775         {
1776                 return E_FAILURE;
1777         }
1778
1779         if (pList->GetCount() == 0)
1780         {
1781                 if (value.IsEmpty())
1782                 {
1783                         return E_SUCCESS;
1784                 }
1785
1786                 Organization organization;
1787
1788                 switch (id)
1789                 {
1790                 case DETAIL_PROPERTY_JOB_TITLE:
1791                         {
1792                                 organization.SetJobTitle(value);
1793                         }
1794                         break;
1795                 case DETAIL_PROPERTY_DEPARTMENT:
1796                         {
1797                                 organization.SetDepartment(value);
1798                         }
1799                         break;
1800                 case DETAIL_PROPERTY_COMPANY:
1801                         {
1802                                 organization.SetName(value);
1803                         }
1804                         break;
1805                 }
1806
1807                 r = __pContact->AddOrganization(organization);
1808         }
1809         else
1810         {
1811                 Organization* pOrganization = static_cast<Organization *>(pList->GetAt(0));
1812                 if (pOrganization == null)
1813                 {
1814                         return E_FAILURE;
1815                 }
1816
1817                 switch (id)
1818                 {
1819                 case DETAIL_PROPERTY_JOB_TITLE:
1820                         {
1821                                 pOrganization->SetJobTitle(value);
1822                         }
1823                         break;
1824                 case DETAIL_PROPERTY_DEPARTMENT:
1825                         {
1826                                 pOrganization->SetDepartment(value);
1827                         }
1828                         break;
1829                 case DETAIL_PROPERTY_COMPANY:
1830                         {
1831                                 pOrganization->SetName(value);
1832                         }
1833                         break;
1834                 }
1835
1836                 if (pOrganization->GetJobTitle().IsEmpty() && pOrganization->GetDepartment().IsEmpty() && pOrganization->GetName().IsEmpty())
1837                 {
1838                         r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ORGANIZATIONS, 0);
1839                 }
1840                 else
1841                 {
1842                         if (value.IsEmpty())
1843                         {
1844                                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ORGANIZATIONS, 0);
1845
1846                                 if (r == E_SUCCESS)
1847                                 {
1848                                         r = __pContact->AddOrganization(*pOrganization);
1849                                 }
1850                         }
1851                         else
1852                         {
1853                                 r = __pContact->SetOrganizationAt(0, *pOrganization);
1854                         }
1855                 }
1856         }
1857
1858         pList->RemoveAll(true);
1859         delete pList;
1860
1861         return r;
1862 }
1863
1864 Tizen::Base::String
1865 ContactPresentationModel::GetPhoneNumberTypeString(DetailPhoneNumberType type)
1866 {
1867         String returnString;
1868
1869         switch (type)
1870         {
1871         case DETAIL_PHONENUMBER_TYPE_HOME:
1872                 {
1873                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
1874                 }
1875                 break;
1876         case DETAIL_PHONENUMBER_TYPE_WORK:
1877                 {
1878                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
1879                 }
1880                 break;
1881         case DETAIL_PHONENUMBER_TYPE_MOBILE:
1882                 {
1883                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_MOBILE");
1884                 }
1885                 break;
1886         case DETAIL_PHONENUMBER_TYPE_HOME_FAX:
1887                 {
1888                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_FAX_HHOME");
1889                 }
1890                 break;
1891         case DETAIL_PHONENUMBER_TYPE_WORK_FAX:
1892                 {
1893                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_FAX_HWORK");
1894                 }
1895                 break;
1896         case DETAIL_PHONENUMBER_TYPE_PAGER:
1897                 {
1898                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_PAGER");
1899                 }
1900                 break;
1901         case DETAIL_PHONENUMBER_TYPE_OTHER:
1902                 {
1903                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
1904                 }
1905                 break;
1906         }
1907
1908         return returnString;
1909 }
1910
1911 Tizen::Base::String
1912 ContactPresentationModel::GetEmailTypeString(DetailEmailType type)
1913 {
1914         String returnString;
1915
1916         switch (type)
1917         {
1918         case DETAIL_EMAIL_TYPE_HOME:
1919                 {
1920                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
1921                 }
1922                 break;
1923         case DETAIL_EMAIL_TYPE_WORK:
1924                 {
1925                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
1926                 }
1927                 break;
1928         case DETAIL_EMAIL_TYPE_OTHER:
1929                 {
1930                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
1931                 }
1932                 break;
1933         }
1934
1935         return returnString;
1936 }
1937
1938 Tizen::Base::String
1939 ContactPresentationModel::GetAddressTypeString(DetailAddressType type)
1940 {
1941         String returnString;
1942
1943         switch (type)
1944         {
1945         case DETAIL_ADDRESS_TYPE_HOME:
1946                 {
1947                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
1948                 }
1949                 break;
1950         case DETAIL_ADDRESS_TYPE_WORK:
1951                 {
1952                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
1953                 }
1954                 break;
1955         case DETAIL_ADDRESS_TYPE_OTHER:
1956                 {
1957                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
1958                 }
1959                 break;
1960         }
1961
1962         return returnString;
1963 }
1964
1965 Tizen::Base::String
1966 ContactPresentationModel::GetUrlTypeString(DetailUrlType type)
1967 {
1968         String returnString;
1969
1970         switch (type)
1971         {
1972         case DETAIL_URL_TYPE_HOME:
1973                 {
1974                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
1975                 }
1976                 break;
1977         case DETAIL_URL_TYPE_WORK:
1978                 {
1979                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
1980                 }
1981                 break;
1982         case DETAIL_URL_TYPE_OTHER:
1983                 {
1984                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
1985                 }
1986                 break;
1987         }
1988
1989         return returnString;
1990 }
1991
1992 Tizen::Base::String
1993 ContactPresentationModel::GetImAddressTypeString(DetailImAddressType type)
1994 {
1995         String returnString;
1996
1997         switch (type)
1998         {
1999         case DETAIL_IM_ADDRESS_TYPE_AIM:
2000                 {
2001                         returnString = IM_ADDRESS_AIM;
2002                 }
2003                 break;
2004         case DETAIL_IM_ADDRESS_TYPE_MSN:
2005                 {
2006                         returnString = IM_ADDRESS_MSN;
2007                 }
2008                 break;
2009         case DETAIL_IM_ADDRESS_TYPE_YAHOO:
2010                 {
2011                         returnString = IM_ADDRESS_YAHOO;
2012                 }
2013                 break;
2014         case DETAIL_IM_ADDRESS_TYPE_SKYPE:
2015                 {
2016                         returnString = IM_ADDRESS_SKYPE;
2017                 }
2018                 break;
2019         case DETAIL_IM_ADDRESS_TYPE_QQ:
2020                 {
2021                         returnString = IM_ADDRESS_QQ;
2022                 }
2023                 break;
2024         case DETAIL_IM_ADDRESS_TYPE_GOOGLE_TALK:
2025                 {
2026                         returnString = IM_ADDRESS_GOOGLE_TALK;
2027                 }
2028                 break;
2029         case DETAIL_IM_ADDRESS_TYPE_ICQ:
2030                 {
2031                         returnString = IM_ADDRESS_ICQ;
2032                 }
2033                 break;
2034         case DETAIL_IM_ADDRESS_TYPE_JABBER:
2035                 {
2036                         returnString = IM_ADDRESS_JABBER;
2037                 }
2038                 break;
2039         case DETAIL_IM_ADDRESS_TYPE_OTHER:
2040                 {
2041                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
2042                 }
2043                 break;
2044         }
2045
2046         return returnString;
2047 }
2048
2049 DetailPhoneNumberType
2050 ContactPresentationModel::GetPhoneNumberType(int index)
2051 {
2052         DetailPhoneNumberType type;
2053         String value;
2054
2055         value = GetPhoneNumber(index, type);
2056
2057         return type;
2058 }
2059
2060 DetailEmailType
2061 ContactPresentationModel::GetEmailType(int index)
2062 {
2063         DetailEmailType type;
2064         String value;
2065
2066         value = GetEmail(index, type);
2067
2068         return type;
2069 }
2070
2071 DetailAddressType
2072 ContactPresentationModel::GetAddressType(DetailProperty id, int index)
2073 {
2074         DetailAddressType type;
2075         String value;
2076
2077         value = GetAddress(id, index, type);
2078
2079         return type;
2080 }
2081
2082 DetailUrlType
2083 ContactPresentationModel::GetUrlType(int index)
2084 {
2085         DetailUrlType type;
2086         String value;
2087
2088         value = GetUrl(index, type);
2089
2090         return type;
2091 }
2092
2093 DetailImAddressType
2094 ContactPresentationModel::GetImAddressType(int index)
2095 {
2096         DetailImAddressType type;
2097         String value;
2098
2099         value = GetImAddress(index, type);
2100
2101         return type;
2102 }
2103
2104 void
2105 ContactPresentationModel::SetAsFavorite(bool isFavorite)
2106 {
2107         AddressbookManager::GetInstance()->SetPersonAsFavorite(__pContact->GetPersonId(), isFavorite);
2108 }
2109
2110 bool
2111 ContactPresentationModel::IsFavorite(void)
2112 {
2113         bool isFavorite = false;
2114         PersonId personId = __pContact->GetPersonId();
2115
2116         Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(personId);
2117         if (pPerson == null)
2118         {
2119                 return false;
2120         }
2121
2122         isFavorite = pPerson->IsFavorite();
2123
2124         delete pPerson;
2125
2126         return isFavorite;
2127 }