Initialize Tizen 2.3
[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::GetDisplayName(Tizen::Base::String& name)
527 {
528         result r = E_SUCCESS;
529         name.Clear();
530
531         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
532
533         if (__vcfPath.IsEmpty() == true)
534         {
535                 Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(__pContact->GetPersonId());
536                 TryReturn(pPerson != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
537
538                 name = pPerson->GetDisplayName();
539         }
540         else
541         {
542                 String firstName = GetValue(DETAIL_PROPERTY_FIRST_NAME);
543                 String middleName = GetValue(DETAIL_PROPERTY_MIDDLE_NAME);
544                 String lastName = GetValue(DETAIL_PROPERTY_LAST_NAME);
545                 String suffix = GetValue(DETAIL_PROPERTY_NAME_SUFFIX);
546
547                 if (firstName.IsEmpty() == false)
548                 {
549                         name = (firstName);
550                 }
551                 if (middleName.IsEmpty() == false)
552                 {
553                         if (name.IsEmpty() == false)
554                         {
555                                 name += CHARACTER_SPACE;
556                         }
557                         name += (middleName);
558                 }
559                 if (lastName.IsEmpty() == false)
560                 {
561                         if (name.IsEmpty() == false)
562                         {
563                                 name += CHARACTER_SPACE;
564                         }
565                         name += (lastName);
566                 }
567                 if (suffix.IsEmpty() == false)
568                 {
569                         if (name.IsEmpty() == false)
570                         {
571                                 name += DELIMITER_COMMA;
572                                 name += CHARACTER_SPACE;
573                         }
574                         name += suffix;
575                 }
576         }
577
578         return r;
579 }
580
581 result
582 ContactPresentationModel::GetBirthday(Tizen::Base::DateTime& value)
583 {
584         result r = E_SUCCESS;
585
586         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
587
588         IList* pEventList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_EVENTS);
589         if (pEventList == null)
590         {
591                 return E_FAILURE;
592         }
593
594         ContactEvent* pBirthday = static_cast<ContactEvent *>(pEventList->GetAt(0));
595         if (pBirthday == null)
596         {
597                 return E_FAILURE;
598         }
599
600         value = pBirthday->GetDate();
601
602         pEventList->RemoveAll(true);
603         delete pEventList;
604
605         return r;
606 }
607
608 Tizen::Base::String
609 ContactPresentationModel::GetType(DetailProperty id, int index)
610 {
611         String returnString;
612         String value;
613
614         switch (id)
615         {
616         case DETAIL_PROPERTY_DEFAULT_PHONE_NUMBER:
617                 {
618                         DetailPhoneNumberType type;
619
620                         value = GetDefaultNumber(type);
621                         returnString = GetPhoneNumberTypeString(type);
622                 }
623                 break;
624         case DETAIL_PROPERTY_PHONE_NUMBER:
625                 {
626                         DetailPhoneNumberType type;
627
628                         value = GetPhoneNumber(index, type);
629                         returnString = GetPhoneNumberTypeString(type);
630                 }
631                 break;
632         case DETAIL_PROPERTY_EMAIL:
633                 {
634                         DetailEmailType type;
635
636                         value = GetEmail(index, type);
637                         returnString = GetEmailTypeString(type);
638                 }
639                 break;
640         case DETAIL_PROPERTY_IM_ADDRESS:
641                 {
642                         DetailImAddressType type;
643
644                         value = GetImAddress(index, type);
645                         returnString = GetImAddressTypeString(type);
646                 }
647                 break;
648         case DETAIL_PROPERTY_ADDRESS:
649                 {
650                         DetailAddressType type;
651
652                         value = GetAddress(id, index, type);
653                         returnString = GetAddressTypeString(type);
654                 }
655                 break;
656         case DETAIL_PROPERTY_URL:
657                 {
658                         DetailUrlType type;
659
660                         value = GetUrl(index, type);
661                         returnString = GetUrlTypeString(type);
662
663                 }
664                 break;
665         default:
666                 break;
667         }
668         return returnString;
669 }
670
671 Tizen::Graphics::Bitmap*
672 ContactPresentationModel::GetThumbnailN(void)
673 {
674         TryReturn(__pContact != null, null, "[E_FAILURE] The contact is invalid.");
675
676         return __pContact->GetThumbnailN();
677 }
678
679 Tizen::Graphics::Bitmap*
680 ContactPresentationModel::GetAccountIconN(const Tizen::Base::String& account)
681 {
682         if (account.Equals(IM_ADDRESS_YAHOO, true))
683         {
684                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_YAHOO);
685         }
686         else if (account.Equals(IM_ADDRESS_MSN, true))
687         {
688                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_MSN);
689         }
690         else if (account.Equals(IM_ADDRESS_ICQ, true))
691         {
692                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_ICQ);
693         }
694 //      else if (account.Equals(IM_ADDRESS_AIM, true))
695 //      {
696 //              //[TODO]
697 //      }
698 //      else if (account.Equals(IM_ADDRESS_SKYPE, true))
699 //      {
700 //              //[TODO]
701 //      }
702 //      else if (account.Equals(IM_ADDRESS_QQ, true))
703 //      {
704 //              //[TODO]
705 //      }
706 //      else if (account.Equals(IM_ADDRESS_GOOGLE_TALK, true))
707 //      {
708 //              //[TODO]
709 //      }
710 //      else if (account.Equals(IM_ADDRESS_JABBER, true))
711 //      {
712 //              //[TODO]
713 //      }
714         else
715         {
716                 return ResourceManager::GetBitmapN(IDB_ACCOUNT_ICON_OTHERS);
717         }
718
719         return null;
720 }
721
722 int
723 ContactPresentationModel::GetMultiValuesCount(DetailProperty id)
724 {
725         int returnValue = 0;
726         IList* pList = null;
727
728         TryReturn(__pContact != null, returnValue, "[E_FAILURE] The contact is invalid.");
729
730         switch (id)
731         {
732         case DETAIL_PROPERTY_PHONE_NUMBER:
733                 {
734                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
735                         if (pList != null)
736                         {
737                                 returnValue = pList->GetCount();
738                         }
739                 }
740                 break;
741         case DETAIL_PROPERTY_EMAIL:
742                 {
743                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_EMAILS);
744                         if (pList != null)
745                         {
746                                 returnValue = pList->GetCount();
747                         }
748                 }
749                 break;
750         case DETAIL_PROPERTY_IM_ADDRESS:
751                 {
752                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_IMADDRESSES);
753                         if (pList != null)
754                         {
755                                 returnValue = pList->GetCount();
756                         }
757                 }
758                 break;
759         case DETAIL_PROPERTY_ADDRESS:
760                 {
761                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ADDRESSES);
762                         if (pList != null)
763                         {
764                                 returnValue = pList->GetCount();
765                         }
766                 }
767                 break;
768         case DETAIL_PROPERTY_URL:
769                 {
770                         pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_URLS);
771                         if (pList != null)
772                         {
773                                 returnValue = pList->GetCount();
774                         }
775                 }
776                 break;
777         default:
778                 break;
779         }
780
781         if (pList != null)
782         {
783                 pList->RemoveAll(true);
784                 delete pList;
785         }
786
787         return returnValue;
788 }
789
790 int
791 ContactPresentationModel::GetCategoryCount(void)
792 {
793         return __pAddressbook->GetCategoryCount();
794 }
795
796 Tizen::Base::Collection::IList*
797 ContactPresentationModel::GetAssignedCategoryIdListN(void)
798 {
799         IEnumerator* pEnum = null;
800         ArrayList* pCategoryList = new (std::nothrow) ArrayList();
801         pCategoryList->Construct();
802
803         if (__newMode || __isEditing)
804         {
805                 pEnum = __pAssignedCategoryList->GetEnumeratorN();
806
807                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
808                 {
809                         String* pCategoryStringId = static_cast<String *>(pEnum->GetCurrent());
810
811                         if (pCategoryStringId != null)
812                         {
813                                 String* pRetrunCategoryStringId = new (std::nothrow) String(*pCategoryStringId);
814
815                                 pCategoryList->Add(*pRetrunCategoryStringId);
816                         }
817                 }
818
819                 delete pEnum;
820
821                 return pCategoryList;
822         }
823         else
824         {
825                 IList* pList = __pAddressbook->GetCategoriesByContactN(__pContact->GetRecordId());
826
827                 if (pList != null)
828                 {
829                         pEnum = pList->GetEnumeratorN();
830
831                         for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
832                         {
833                                 Category* pCategory = static_cast<Category *>(pEnum->GetCurrent());
834
835                                 if (pCategory != null)
836                                 {
837                                         String* pRetrunCategoryStringId = new (std::nothrow) String();
838
839                                         pRetrunCategoryStringId->Append(pCategory->GetRecordId());
840                                         pCategoryList->Add(*pRetrunCategoryStringId);
841                                 }
842                         }
843
844                         pList->RemoveAll(true);
845                         delete pList;
846                         delete pEnum;
847                 }
848
849                 return pCategoryList;
850         }
851 }
852
853 Tizen::Base::String
854 ContactPresentationModel::GetCategory(void)
855 {
856         String returnString;
857
858         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
859
860         if (__newMode || __isEditing)
861         {
862                 IEnumerator* pEnum = __pAssignedCategoryList->GetEnumeratorN();
863                 Category* pCategory = null;
864                 String* pCategoryStringId = null;
865                 RecordId categoryId;
866
867                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
868                 {
869                         pCategoryStringId = static_cast<String*>(pEnum->GetCurrent());
870
871                         if (pCategoryStringId != null)
872                         {
873                                 Integer::Parse(*pCategoryStringId, categoryId);
874                                 pCategory = __pAddressbook->GetCategoryN(categoryId);
875
876                                 String categoryName;
877                                 if (pCategory != null)
878                                 {
879                                         categoryName = pCategory->GetName();
880
881                                         if (index == 0)
882                                         {
883                                                 returnString = categoryName;
884                                         }
885                                         else if (index > 0)
886                                         {
887                                                 returnString += L", ";
888                                                 returnString += categoryName;
889                                         }
890                                 }
891                                 delete pCategory;
892                         }
893                 }
894                 delete pEnum;
895         }
896         else
897         {
898                 Category* pCategory = null;
899                 IList* pCategoryList = __pAddressbook->GetCategoriesByContactN(__pContact->GetRecordId());
900                 if (pCategoryList == null)
901                 {
902                         return returnString;
903                 }
904
905                 IEnumerator* pEnum = pCategoryList->GetEnumeratorN();
906                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
907                 {
908                         pCategory = static_cast<Category*>(pEnum->GetCurrent());
909
910                         String categoryName;
911                         if (pCategory != null)
912                         {
913                                 categoryName = pCategory->GetName();
914
915                                 if (index == 0)
916                                 {
917                                         returnString = categoryName;
918                                 }
919                                 else if (index > 0)
920                                 {
921                                         returnString += L", ";
922                                         returnString += categoryName;
923                                 }
924                         }
925                 }
926
927                 delete pEnum;
928                 pCategoryList->RemoveAll(true);
929                 delete pCategoryList;
930         }
931
932         return returnString;
933 }
934
935 result
936 ContactPresentationModel::GetDefaultCategoryName(Tizen::Base::String& categoryName)
937 {
938         if (categoryName.Equals(DEFAULT_GROUP_FAMILY, true))
939         {
940                 categoryName.Clear();
941                 categoryName = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT_GROUP_FAMILY");
942         }
943         else if (categoryName.Equals(DEFAULT_GROUP_FRIENDS, true))
944         {
945                 categoryName.Clear();
946                 categoryName = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT_GROUP_FRIENDS");
947         }
948         else if (categoryName.Equals(DEFAULT_GROUP_COWORKERS, true))
949         {
950                 categoryName.Clear();
951                 categoryName = ResourceManager::GetString(L"IDS_PB_BODY_CO_WORKERS");
952         }
953
954         return E_SUCCESS;
955 }
956
957 Tizen::Base::String
958 ContactPresentationModel::GetRingtone(void)
959 {
960         String returnString;
961         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
962
963         result r = __pContact->GetValue(CONTACT_PROPERTY_ID_RINGTONE, returnString);
964         TryReturn(r == E_SUCCESS, String(), "[E_FAILURE] Unable to get value");
965         
966         if (returnString.IsEmpty())
967         {
968                 returnString = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT");
969         }
970         else
971         {
972                 File file;
973                 bool isFileExisting;
974                 isFileExisting = file.IsFileExist(returnString);
975                 if(!isFileExisting)
976                 {
977                         SetValue(DETAIL_PROPERTY_RINGTONE, String());
978                         UpdateContact();
979                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_DEFAULT");
980                 }
981         }
982
983         return returnString;
984 }
985
986 Tizen::Base::String
987 ContactPresentationModel::GetDefaultNumber(DetailPhoneNumberType& type)
988 {
989         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
990
991         String returnString;
992
993         IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
994         TryReturn(pList != null, String(), "[E_FAILURE] The contact doesn't have a phone number at least.");
995
996         PhoneNumber* pPhoneNumber = static_cast<PhoneNumber*>(pList->GetAt(__defaultNumberIndex));
997         if (pPhoneNumber != null)
998         {
999                 returnString = pPhoneNumber->GetPhoneNumber();
1000                 if (returnString.Contains(CHARACTER_HYPHEN))
1001                 {
1002                         returnString.Replace(CHARACTER_HYPHEN, CHARACTER_EMPTY);
1003                 }
1004                 type = (DetailPhoneNumberType)pPhoneNumber->GetType();
1005         }
1006
1007         pList->RemoveAll(true);
1008         delete pList;
1009
1010         return returnString;
1011 }
1012
1013 int
1014 ContactPresentationModel::GetDefaultNumberIndex(void)
1015 {
1016         return __defaultNumberIndex;
1017 }
1018
1019 Tizen::Base::String
1020 ContactPresentationModel::GetPhoneNumber(int index, DetailPhoneNumberType& type)
1021 {
1022         String returnString;
1023
1024         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1025
1026         if (index == -1)
1027         {
1028                 return returnString;
1029         }
1030
1031         IList* pPhoneNumberList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
1032         AppAssert(pPhoneNumberList);
1033
1034         PhoneNumber* pNumber = static_cast<PhoneNumber *>(pPhoneNumberList->GetAt(index));
1035         if (pNumber != null)
1036         {
1037                 returnString = pNumber->GetPhoneNumber();
1038
1039                 if (returnString.Contains(CHARACTER_HYPHEN))
1040                 {
1041                         returnString.Replace(CHARACTER_HYPHEN, CHARACTER_EMPTY);
1042                 }
1043                 type = (DetailPhoneNumberType)pNumber->GetType();
1044         }
1045
1046         pPhoneNumberList->RemoveAll(true);
1047         delete pPhoneNumberList;
1048
1049         return returnString;
1050 }
1051
1052 Tizen::Base::String
1053 ContactPresentationModel::GetEmail(int index, DetailEmailType& type)
1054 {
1055         String returnString;
1056
1057         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1058
1059         if (index == -1)
1060         {
1061                 return returnString;
1062         }
1063
1064         IList* pEmailList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_EMAILS);
1065         AppAssert(pEmailList);
1066
1067         Email* pEmail = static_cast<Email *>(pEmailList->GetAt(index));
1068         if (pEmail != null)
1069         {
1070                 returnString = pEmail->GetEmail();
1071                 type = (DetailEmailType)pEmail->GetType();
1072         }
1073
1074         pEmailList->RemoveAll(true);
1075         delete pEmailList;
1076
1077         return returnString;
1078 }
1079
1080 Tizen::Base::String
1081 ContactPresentationModel::GetImAddress(int index, DetailImAddressType& type)
1082 {
1083         String returnString;
1084
1085         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1086
1087         if (index == -1)
1088         {
1089                 return returnString;
1090         }
1091
1092         IList* pImAddressList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_IMADDRESSES);
1093         AppAssert(pImAddressList);
1094
1095         ImAddress* pImAddress =  static_cast<ImAddress *>(pImAddressList->GetAt(index));
1096         if (pImAddress != null)
1097         {
1098                 returnString = pImAddress->GetImAddress();
1099
1100                 String typeString = pImAddress->GetServiceProviderName();
1101
1102                 if (typeString.Equals(IM_ADDRESS_AIM, true))
1103                 {
1104                         type = DETAIL_IM_ADDRESS_TYPE_AIM;
1105                 }
1106                 else if (typeString.Equals(IM_ADDRESS_MSN, true))
1107                 {
1108                         type = DETAIL_IM_ADDRESS_TYPE_MSN;
1109                 }
1110                 else if (typeString.Equals(IM_ADDRESS_YAHOO, true))
1111                 {
1112                         type = DETAIL_IM_ADDRESS_TYPE_YAHOO;
1113                 }
1114                 else if (typeString.Equals(IM_ADDRESS_SKYPE, true))
1115                 {
1116                         type = DETAIL_IM_ADDRESS_TYPE_SKYPE;
1117                 }
1118                 else if (typeString.Equals(IM_ADDRESS_QQ, true))
1119                 {
1120                         type = DETAIL_IM_ADDRESS_TYPE_QQ;
1121                 }
1122                 else if (typeString.Equals(IM_ADDRESS_GOOGLE_TALK, true))
1123                 {
1124                         type = DETAIL_IM_ADDRESS_TYPE_GOOGLE_TALK;
1125                 }
1126                 else if (typeString.Equals(IM_ADDRESS_ICQ, true))
1127                 {
1128                         type = DETAIL_IM_ADDRESS_TYPE_ICQ;
1129                 }
1130                 else if (typeString.Equals(IM_ADDRESS_JABBER, true))
1131                 {
1132                         type = DETAIL_IM_ADDRESS_TYPE_JABBER;
1133                 }
1134                 else
1135                 {
1136                         type = DETAIL_IM_ADDRESS_TYPE_OTHER;
1137                 }
1138         }
1139
1140         pImAddressList->RemoveAll(true);
1141         delete pImAddressList;
1142
1143         return returnString;
1144 }
1145
1146 Tizen::Base::String
1147 ContactPresentationModel::GetAddress(DetailProperty id, int index, DetailAddressType& type)
1148 {
1149         String returnString;
1150
1151         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1152
1153         if (index == -1)
1154         {
1155                 return returnString;
1156         }
1157
1158         IList* pAddressList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ADDRESSES);
1159         AppAssert(pAddressList);
1160
1161         Address* pAddress = static_cast<Address *>(pAddressList->GetAt(index));
1162         if (pAddress != null)
1163         {
1164                 String street = pAddress->GetStreet();
1165                 String city = pAddress->GetCity();
1166                 String state = pAddress->GetState();
1167                 String country = pAddress->GetCountry();
1168                 String postalCode = pAddress->GetPostalCode();
1169
1170                 switch (id)
1171                 {
1172                 case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE:
1173                         returnString = postalCode;
1174                         break;
1175                 case DETAIL_PROPERTY_ADDRESS_COUNTRY:
1176                         returnString = country;
1177                         break;
1178                 case DETAIL_PROPERTY_ADDRESS_PROVINCE:
1179                         returnString = state;
1180                         break;
1181                 case DETAIL_PROPERTY_ADDRESS_CITY:
1182                         returnString = city;
1183                         break;
1184                 case DETAIL_PROPERTY_ADDRESS_STREET:
1185                         returnString = street;
1186                         break;
1187                 case DETAIL_PROPERTY_ADDRESS:
1188                         if (postalCode.IsEmpty() == false)
1189                         {
1190                                 returnString.Append(postalCode);
1191                                 returnString.Append(CHARACTER_SPACE);
1192                         }
1193                         if (country.IsEmpty() == false)
1194                         {
1195                                 returnString.Append(country);
1196                                 returnString.Append(CHARACTER_SPACE);
1197                         }
1198                         if (state.IsEmpty() == false)
1199                         {
1200                                 returnString.Append(state);
1201                                 returnString.Append(CHARACTER_SPACE);
1202                         }
1203                         if (city.IsEmpty() == false)
1204                         {
1205                                 returnString.Append(city);
1206                                 returnString.Append(CHARACTER_SPACE);
1207                         }
1208                         if (street.IsEmpty() == false)
1209                         {
1210                                 returnString.Append(street);
1211                         }
1212                         break;
1213                 default:
1214                         break;
1215                 }
1216
1217                 type = (DetailAddressType)pAddress->GetType();
1218         }
1219
1220         pAddressList->RemoveAll(true);
1221         delete pAddressList;
1222
1223         return returnString;
1224 }
1225
1226 Tizen::Base::String
1227 ContactPresentationModel::GetDateTime(DetailProperty id)
1228 {
1229         result r = E_SUCCESS;
1230         String returnString;
1231         int day = 0;
1232         int year = 0;
1233         String month;
1234         DateTimeSymbols dateTimeSymbols;
1235
1236         r = dateTimeSymbols.Construct(CALENDAR_GREGORIAN);
1237         TryReturn(r == E_SUCCESS, String(), "[E_FAILURE] Given locale is not supported.");
1238         const IList* pMonthList = dateTimeSymbols.GetShortMonths();
1239
1240         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1241
1242         DateTime* pDateTime = new (std::nothrow) DateTime();
1243
1244         if (id == DETAIL_PROPERTY_BIRTHDAY)
1245         {
1246                 const Object* pGettingMonth = null;
1247
1248                 r = GetBirthday(*pDateTime);
1249                 TryCatch(r == E_SUCCESS, , "[%s] Unable to get value", GetErrorMessage(r));
1250
1251                 day = pDateTime->GetDay();
1252                 year = pDateTime->GetYear();
1253
1254                 pGettingMonth = pMonthList->GetAt(pDateTime->GetMonth() - 1);
1255                 TryCatch(pGettingMonth != null, , "[E_FAILURE] Unable to get a month", GetErrorMessage(r));
1256
1257                 month.Append(*static_cast<String*>(const_cast<Object*>(pGettingMonth)));
1258
1259                 returnString.Append(day);
1260                 returnString.Append(CHARACTER_SPACE);
1261                 returnString.Append(month);
1262                 returnString.Append(CHARACTER_SPACE);
1263                 returnString.Append(year);
1264         }
1265
1266 CATCH:
1267         delete pDateTime;
1268
1269         return returnString;
1270 }
1271
1272 Tizen::Base::String
1273 ContactPresentationModel::GetUrl(int index, DetailUrlType& type)
1274 {
1275         String returnString;
1276
1277         TryReturn(__pContact != null, String(), "[E_FAILURE] The contact is invalid.");
1278
1279         IList* pUrlList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_URLS);
1280         if (pUrlList == null)
1281         {
1282                 return returnString;
1283         }
1284
1285         Url* pUrl = static_cast<Url*>(pUrlList->GetAt(index));
1286         if (pUrl != null)
1287         {
1288                 returnString = pUrl->GetUrl();
1289                 type = (DetailUrlType)pUrl->GetType();
1290         }
1291
1292         pUrlList->RemoveAll(true);
1293         delete pUrlList;
1294
1295         return returnString;
1296 }
1297
1298 void
1299 ContactPresentationModel::AddContactChangeListener(const IContactEventListener& listener)
1300 {
1301         __pContactListenerList->Add(listener);
1302 }
1303
1304 void
1305 ContactPresentationModel::RemoveContactChangeListener(const IContactEventListener& listener)
1306 {
1307         __pContactListenerList->Remove(listener, false);
1308 }
1309
1310 void
1311 ContactPresentationModel::OnContactsChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
1312 {
1313         if (__newMode == false && __isEditing == false)
1314         {
1315                 if (__pContact != null)
1316                 {
1317                         SetContactId(__pContact->GetRecordId());
1318                 }
1319         }
1320         else if (__isEditing)
1321         {
1322                 IEnumerator* pEnum = contactChangeInfoList.GetEnumeratorN();
1323
1324                 if (pEnum != null && __pContact != null)
1325                 {
1326                         while (pEnum->MoveNext() == E_SUCCESS)
1327                         {
1328                                 const ContactChangeInfo* pChangeInfo = static_cast<const ContactChangeInfo *>(pEnum->GetCurrent());
1329
1330                                 if (pChangeInfo != null && pChangeInfo->GetChangeType() == RECORD_CHANGE_TYPE_REMOVED && __pContact->GetRecordId() == pChangeInfo->GetContactId())
1331                                 {
1332                                         SetContactId(INVALID_RECORD_ID);
1333                                         break;
1334                                 }
1335                         }
1336                         delete pEnum;
1337                 }
1338         }
1339
1340         IContactEventListener* pInterface = null;
1341         IEnumerator* pEnum = __pContactListenerList->GetEnumeratorN();
1342         while(pEnum->MoveNext() == E_SUCCESS)
1343         {
1344                 pInterface = static_cast<IContactEventListener*>(pEnum->GetCurrent());
1345                 if (pInterface == null)
1346                 {
1347                         delete pEnum;
1348                         return;
1349                 }
1350                 pInterface->OnContactsChanged();
1351         }
1352
1353         delete pEnum;
1354 }
1355
1356 void
1357 ContactPresentationModel::OnCategoriesChanged(const Tizen::Base::Collection::IList& categoryChangeInfoList)
1358 {
1359         IEnumerator* pEnumCategory = categoryChangeInfoList.GetEnumeratorN();
1360
1361         if (pEnumCategory != null && __pContact != null && __pAssignedCategoryList != null)
1362         {
1363                 while (pEnumCategory->MoveNext() == E_SUCCESS)
1364                 {
1365                         const CategoryChangeInfo* pChangeInfo = static_cast<const CategoryChangeInfo *>(pEnumCategory->GetCurrent());
1366
1367                         if (pChangeInfo != null && pChangeInfo->GetChangeType() == RECORD_CHANGE_TYPE_REMOVED)
1368                         {
1369                                 RecordId id = pChangeInfo->GetCategoryId();
1370
1371                                 String* pcurCategoryStringId = new (std::nothrow) String();
1372
1373                                 pcurCategoryStringId->Append(id);
1374
1375                                 IEnumerator* pEnum = __pAssignedCategoryList->GetEnumeratorN();
1376
1377                                 for (int index = 0; pEnum->MoveNext() == E_SUCCESS; index++)
1378                                 {
1379                                         String* pCategoryStringId = static_cast<String*>(pEnum->GetCurrent());
1380
1381                                         if (pCategoryStringId != null)
1382                                         {
1383                                                 if (pcurCategoryStringId->Equals(*pCategoryStringId, false) == true)
1384                                                 {
1385                                                         __pAssignedCategoryList->Remove(*pCategoryStringId, true);
1386                                                 }
1387                                         }
1388                                 }
1389
1390                                 delete pEnum;
1391                         }
1392                 }
1393
1394                 delete pEnumCategory;
1395         }
1396
1397         IContactEventListener* pInterface = null;
1398         IEnumerator* pEnum = __pContactListenerList->GetEnumeratorN();
1399         while(pEnum->MoveNext() == E_SUCCESS)
1400         {
1401                 pInterface = static_cast<IContactEventListener*>(pEnum->GetCurrent());
1402                 if (pInterface == null)
1403                 {
1404                         delete pEnum;
1405                         return;
1406                 }
1407                 pInterface->OnCategoriesChanged();
1408         }
1409
1410         delete pEnum;
1411 }
1412
1413 result
1414 ContactPresentationModel::SetValue(DetailProperty id, const Tizen::Base::String& value, int index)
1415 {
1416         result r = E_SUCCESS;
1417
1418         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1419
1420         String trimmedString(value);
1421         trimmedString.Trim();
1422
1423         Email email = Email();
1424         ImAddress imAddress = ImAddress();
1425         Url url = Url();
1426         PhoneNumber phoneNumber = PhoneNumber();
1427
1428         switch (id)
1429         {
1430         case DETAIL_PROPERTY_RINGTONE:
1431                 {
1432                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_RINGTONE, trimmedString);
1433                 }
1434                 break;
1435         case DETAIL_PROPERTY_DEFAULT_PHONE_NUMBER:
1436                 {
1437                         __defaultNumberIndex = index;
1438                 }
1439                 break;
1440         case DETAIL_PROPERTY_PHONE_NUMBER:
1441                 {
1442                         r = SetPhoneNumber(DETAIL_PHONENUMBER_TYPE_MOBILE, trimmedString, index);
1443                 }
1444                 break;
1445         case DETAIL_PROPERTY_EMAIL:
1446                 {
1447                         r = SetEmail(DETAIL_EMAIL_TYPE_HOME, trimmedString, index);
1448                 }
1449                 break;
1450         case DETAIL_PROPERTY_IM_ADDRESS:
1451                 {
1452                         r = SetImAddress(DETAIL_IM_ADDRESS_TYPE_MSN, trimmedString, index);
1453                 }
1454                 break;
1455         case DETAIL_PROPERTY_ADDRESS: //fall through
1456         case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE: //fall through
1457         case DETAIL_PROPERTY_ADDRESS_COUNTRY: //fall through
1458         case DETAIL_PROPERTY_ADDRESS_PROVINCE: //fall through
1459         case DETAIL_PROPERTY_ADDRESS_CITY: //fall through
1460         case DETAIL_PROPERTY_ADDRESS_STREET:
1461                 {
1462                         SetAddress(id, trimmedString);
1463                         __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ADDRESSES, index);
1464                         r = __pContact->SetAddressAt(index, __address);
1465                         if (r != E_SUCCESS)
1466                         {
1467                                 r = __pContact->AddAddress(__address);
1468                         }
1469                 }
1470                 break;
1471         case DETAIL_PROPERTY_URL:
1472                 {
1473                         SetUrl(DETAIL_URL_TYPE_HOME, trimmedString, index);
1474                 }
1475                 break;
1476         case DETAIL_PROPERTY_NOTE:
1477                 {
1478                         if (value.IsEmpty() == true)
1479                         {
1480                                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_NOTES, index);
1481
1482                                 return r;
1483                         }
1484
1485                         r = __pContact->SetNoteAt(index, value);
1486                         if (r != E_SUCCESS)
1487                         {
1488                                 r = __pContact->AddNote(value);
1489                         }
1490                 }
1491                 break;
1492         case DETAIL_PROPERTY_DISPLAY_NAME:
1493                 {
1494                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_DISPLAY_NAME, trimmedString);
1495                 }
1496                 break;
1497         case DETAIL_PROPERTY_FIRST_NAME:
1498                 {
1499                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_FIRST_NAME, trimmedString);
1500                 }
1501                 break;
1502         case DETAIL_PROPERTY_LAST_NAME:
1503                 {
1504                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_LAST_NAME, trimmedString);
1505                 }
1506                 break;
1507         case DETAIL_PROPERTY_MIDDLE_NAME:
1508                 {
1509                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_MIDDLE_NAME, trimmedString);
1510                 }
1511                 break;
1512         case DETAIL_PROPERTY_NICK_NAME:
1513                 {
1514                         if (trimmedString.IsEmpty() == true)
1515                         {
1516                                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_NICKNAMES, index);
1517
1518                                 return r;
1519                         }
1520
1521                         r = __pContact->SetNicknameAt(index, trimmedString);
1522                         if (r != E_SUCCESS)
1523                         {
1524                                 r = __pContact->AddNickname(trimmedString);
1525                         }
1526                 }
1527                 break;
1528         case DETAIL_PROPERTY_NAME_SUFFIX:
1529                 {
1530                         r = __pContact->SetValue(CONTACT_PROPERTY_ID_NAME_SUFFIX, trimmedString);
1531                 }
1532                 break;
1533         case DETAIL_PROPERTY_JOB_TITLE:
1534                 {
1535                         r = SetOrganization(DETAIL_PROPERTY_JOB_TITLE, trimmedString);
1536                 }
1537                 break;
1538         case DETAIL_PROPERTY_DEPARTMENT:
1539                 {
1540                         r = SetOrganization(DETAIL_PROPERTY_DEPARTMENT, trimmedString);
1541                 }
1542                 break;
1543         case DETAIL_PROPERTY_COMPANY:
1544                 {
1545                         r = SetOrganization(DETAIL_PROPERTY_COMPANY, trimmedString);
1546                 }
1547                 break;
1548         default:
1549                 break;
1550         }
1551
1552         return r;
1553 }
1554
1555 result
1556 ContactPresentationModel::AddAddressBook(void)
1557 {
1558         result r = E_SUCCESS;
1559
1560         if (__newMode == true)
1561         {
1562                 r = AddContact();
1563
1564                 RecordId newPersonId;
1565                 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
1566                 IList* pList = pAddressbookManager->GetContactsByPersonN(__pContact->GetPersonId());
1567
1568                 if (pList != null && pList->GetCount() > 1)
1569                 {
1570                         r = pAddressbookManager->UnlinkContact(__pContact->GetPersonId(), __pContact->GetRecordId(), newPersonId);
1571                 }
1572
1573                 delete pList;
1574         }
1575         else
1576         {
1577                 r = UpdateContact();
1578         }
1579
1580         if (r == E_SUCCESS)
1581         {
1582                 IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
1583                 TryReturn(pList != null, r, "[E_FAILURE] The contact doesn't have a phone number at least.");
1584
1585                 PhoneNumber* pPhoneNumber = static_cast<PhoneNumber*>(pList->GetAt(__defaultNumberIndex));
1586                 if (pPhoneNumber != null)
1587                 {
1588                         Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(__pContact->GetPersonId());
1589                         if (pPerson != null)
1590                         {
1591                                 pPerson->SetAsPrimaryPhoneNumber(*pPhoneNumber);
1592                                 UpdateContact();
1593                         }
1594                         delete pPerson;
1595                 }
1596
1597                 pList->RemoveAll(true);
1598                 delete pList;
1599         }
1600
1601         if (__newMode || __isEditing)
1602         {
1603                 if (__isEditing == true)
1604                 {
1605                         __isEditing = false;
1606                         RemoveContactFromAllCategories();
1607                         __isEditing = true;
1608                 }
1609
1610                 IEnumerator* pEnum = __pAssignedCategoryList->GetEnumeratorN();
1611                 String* pCategoryStringId = null;
1612                 RecordId categoryId;
1613
1614                 while (pEnum->MoveNext() == E_SUCCESS)
1615                 {
1616                         pCategoryStringId = static_cast<String*>(pEnum->GetCurrent());
1617
1618                         if (pCategoryStringId != null)
1619                         {
1620                                 Integer::Parse(*pCategoryStringId, categoryId);
1621                                 r = __pAddressbook->AddMemberToCategory(categoryId, __pContact->GetRecordId());
1622                         }
1623                 }
1624                 delete pEnum;
1625         }
1626
1627         return r;
1628 }
1629
1630 result
1631 ContactPresentationModel::SetAddress(DetailProperty id, const Tizen::Base::String& value)
1632 {
1633         result r = E_SUCCESS;
1634
1635         switch (id)
1636         {
1637         case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE:
1638                 r = __address.SetPostalCode(value);
1639                 break;
1640         case DETAIL_PROPERTY_ADDRESS_COUNTRY:
1641                 r = __address.SetCountry(value);
1642                 break;
1643         case DETAIL_PROPERTY_ADDRESS_PROVINCE:
1644                 r = __address.SetState(value);
1645                 break;
1646         case DETAIL_PROPERTY_ADDRESS_CITY:
1647                 r = __address.SetCity(value);
1648                 break;
1649         case DETAIL_PROPERTY_ADDRESS_STREET:
1650                 r = __address.SetStreet(value);
1651                 break;
1652         default:
1653                 break;
1654         }
1655
1656         return r;
1657 }
1658
1659 result
1660 ContactPresentationModel::SetBirthday(const Tizen::Base::DateTime& value)
1661 {
1662         result r = E_SUCCESS;
1663
1664         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1665
1666         ContactEvent birthday;
1667         birthday.SetDate(value);
1668
1669         r = __pContact->SetEventAt(0, birthday);
1670         if (r != E_SUCCESS)
1671         {
1672                 r = __pContact->AddEvent(birthday);
1673         }
1674
1675         return r;
1676 }
1677
1678 result
1679 ContactPresentationModel::RemoveBirthday(void)
1680 {
1681         result r = E_SUCCESS;
1682
1683         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1684         r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_EVENTS, 0);
1685
1686         return r;
1687 }
1688
1689 result
1690 ContactPresentationModel::SetPhoneNumber(DetailPhoneNumberType type, const Tizen::Base::String& value, int index)
1691 {
1692         result r = E_SUCCESS;
1693
1694         if(index==0)
1695         {
1696                 __lastRemovedContactIndex=-1;
1697         }
1698
1699         if (value.IsEmpty() == true)
1700         {
1701                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_PHONE_NUMBERS, index);
1702                 __lastRemovedContactIndex=index;
1703
1704                 return r;
1705         }
1706
1707         PhoneNumber phoneNumber;
1708         phoneNumber.SetPhoneNumber(value);
1709         phoneNumber.SetType((PhoneNumberType)type);
1710
1711         if(__lastRemovedContactIndex!=-1 && index>__lastRemovedContactIndex)
1712         {
1713                 r = __pContact->SetPhoneNumberAt(index-1, phoneNumber);
1714         }
1715         else
1716         {
1717                 r = __pContact->SetPhoneNumberAt(index, phoneNumber);
1718         }
1719
1720         if (r != E_SUCCESS)
1721         {
1722                 r = __pContact->AddPhoneNumber(phoneNumber);
1723         }
1724
1725         return r;
1726 }
1727
1728 result
1729 ContactPresentationModel::SetEmail(DetailEmailType type, const Tizen::Base::String& value, int index)
1730 {
1731         result r = E_SUCCESS;
1732
1733         if (index==0)
1734         {
1735                 __lastRemovedEmailIndex = -1;
1736         }
1737
1738         if (value.IsEmpty() == true)
1739         {
1740                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_EMAILS, index);
1741                 __lastRemovedEmailIndex = index;
1742
1743                 return r;
1744         }
1745
1746         Email email;
1747         email.SetEmail(value);
1748         email.SetType((EmailType)type);
1749
1750
1751         if (__lastRemovedEmailIndex >= 0 && __lastRemovedEmailIndex < index)
1752         {
1753                 r = __pContact->SetEmailAt(index - 1, email);
1754         }
1755         else
1756         {
1757                 r = __pContact->SetEmailAt(index, email);
1758         }
1759
1760
1761         if (r != E_SUCCESS)
1762         {
1763                 r = __pContact->AddEmail(email);
1764         }
1765
1766         return r;
1767 }
1768
1769 result
1770 ContactPresentationModel::SetAddress(DetailAddressType type, const Tizen::Base::String& value, int index, DetailProperty property)
1771 {
1772         result r = E_SUCCESS;
1773
1774         if (value.IsEmpty() == true)
1775         {
1776                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ADDRESSES, index);
1777
1778                 return r;
1779         }
1780
1781         Address address;
1782         address.SetType((AddressType)type);
1783
1784         switch (property)
1785         {
1786         case DETAIL_PROPERTY_ADDRESS_POSTAL_CODE:
1787                 address.SetPostalCode(value);
1788                 break;
1789         case DETAIL_PROPERTY_ADDRESS_COUNTRY:
1790                 address.SetCountry(value);
1791                 break;
1792         case DETAIL_PROPERTY_ADDRESS_PROVINCE:
1793                 address.SetState(value);
1794                 break;
1795         case DETAIL_PROPERTY_ADDRESS_CITY:
1796                 address.SetCity(value);
1797                 break;
1798         case DETAIL_PROPERTY_ADDRESS_STREET:
1799                 address.SetStreet(value);
1800                 break;
1801         default:
1802                 break;
1803         }
1804
1805
1806         r = __pContact->SetAddressAt(index, address);
1807         if (r != E_SUCCESS)
1808         {
1809                 r = __pContact->AddAddress(address);
1810         }
1811
1812         return r;
1813 }
1814
1815 result
1816 ContactPresentationModel::SetUrl(DetailUrlType type, const Tizen::Base::String& value, int index)
1817 {
1818         result r = E_SUCCESS;
1819
1820         if (value.IsEmpty() == true)
1821         {
1822                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_URLS, index);
1823
1824                 return r;
1825         }
1826
1827         Url url;
1828         url.SetType(UrlType(type));
1829         url.SetUrl(value);
1830
1831         r = __pContact->SetUrlAt(index, url);
1832         if (r != E_SUCCESS)
1833         {
1834                 r = __pContact->AddUrl(url);
1835         }
1836
1837         return r;
1838 }
1839
1840 result
1841 ContactPresentationModel::SetImAddress(DetailImAddressType type, const Tizen::Base::String& value, int index)
1842 {
1843         result r = E_SUCCESS;
1844
1845         if (value.IsEmpty() == true)
1846         {
1847                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_IMADDRESSES, index);
1848
1849                 return r;
1850         }
1851
1852         ImAddress imAddress;
1853         imAddress.SetImAddress(value);
1854         imAddress.SetServiceProviderName(GetImAddressTypeString(type));
1855         r = __pContact->SetImAddressAt(index, imAddress);
1856
1857         if (r != E_SUCCESS)
1858         {
1859                 r = __pContact->AddImAddress(imAddress);
1860         }
1861
1862         return r;
1863 }
1864
1865 result
1866 ContactPresentationModel::SetThumbnail(const Tizen::Base::String& filePath)
1867 {
1868         result r = E_SUCCESS;
1869
1870         TryReturn(__pContact != null, E_FAILURE, "[E_FAILURE] The contact is invalid.");
1871
1872         r = __pContact->SetThumbnail(filePath);
1873
1874         return r;
1875 }
1876
1877 result
1878 ContactPresentationModel::SetOrganization(DetailProperty id, const Tizen::Base::String& value)
1879 {
1880         TryReturn((id >= DETAIL_PROPERTY_JOB_TITLE && id <= DETAIL_PROPERTY_COMPANY), E_FAILURE, "[E_FAILURE] Unable to set the specific id");
1881
1882         result r = E_SUCCESS;
1883
1884         IList* pList = __pContact->GetValuesN(CONTACT_MPROPERTY_ID_ORGANIZATIONS);
1885
1886         if (pList == null)
1887         {
1888                 return E_FAILURE;
1889         }
1890
1891         if (pList->GetCount() == 0)
1892         {
1893                 if (value.IsEmpty())
1894                 {
1895                         return E_SUCCESS;
1896                 }
1897
1898                 Organization organization;
1899
1900                 switch (id)
1901                 {
1902                 case DETAIL_PROPERTY_JOB_TITLE:
1903                         {
1904                                 organization.SetJobTitle(value);
1905                         }
1906                         break;
1907                 case DETAIL_PROPERTY_DEPARTMENT:
1908                         {
1909                                 organization.SetDepartment(value);
1910                         }
1911                         break;
1912                 case DETAIL_PROPERTY_COMPANY:
1913                         {
1914                                 organization.SetName(value);
1915                         }
1916                         break;
1917                 }
1918
1919                 r = __pContact->AddOrganization(organization);
1920         }
1921         else
1922         {
1923                 Organization* pOrganization = static_cast<Organization *>(pList->GetAt(0));
1924                 if (pOrganization == null)
1925                 {
1926                         return E_FAILURE;
1927                 }
1928
1929                 switch (id)
1930                 {
1931                 case DETAIL_PROPERTY_JOB_TITLE:
1932                         {
1933                                 pOrganization->SetJobTitle(value);
1934                         }
1935                         break;
1936                 case DETAIL_PROPERTY_DEPARTMENT:
1937                         {
1938                                 pOrganization->SetDepartment(value);
1939                         }
1940                         break;
1941                 case DETAIL_PROPERTY_COMPANY:
1942                         {
1943                                 pOrganization->SetName(value);
1944                         }
1945                         break;
1946                 }
1947
1948                 if (pOrganization->GetJobTitle().IsEmpty() && pOrganization->GetDepartment().IsEmpty() && pOrganization->GetName().IsEmpty())
1949                 {
1950                         r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ORGANIZATIONS, 0);
1951                 }
1952                 else
1953                 {
1954                         if (value.IsEmpty())
1955                         {
1956                                 r = __pContact->RemoveAt(CONTACT_MPROPERTY_ID_ORGANIZATIONS, 0);
1957
1958                                 if (r == E_SUCCESS)
1959                                 {
1960                                         r = __pContact->AddOrganization(*pOrganization);
1961                                 }
1962                         }
1963                         else
1964                         {
1965                                 r = __pContact->SetOrganizationAt(0, *pOrganization);
1966                         }
1967                 }
1968         }
1969
1970         pList->RemoveAll(true);
1971         delete pList;
1972
1973         return r;
1974 }
1975
1976 Tizen::Base::String
1977 ContactPresentationModel::GetPhoneNumberTypeString(DetailPhoneNumberType type)
1978 {
1979         String returnString;
1980
1981         switch (type)
1982         {
1983         case DETAIL_PHONENUMBER_TYPE_HOME:
1984                 {
1985                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
1986                 }
1987                 break;
1988         case DETAIL_PHONENUMBER_TYPE_WORK:
1989                 {
1990                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
1991                 }
1992                 break;
1993         case DETAIL_PHONENUMBER_TYPE_MOBILE:
1994                 {
1995                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_MOBILE");
1996                 }
1997                 break;
1998         case DETAIL_PHONENUMBER_TYPE_HOME_FAX:
1999                 {
2000                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_FAX_HHOME");
2001                 }
2002                 break;
2003         case DETAIL_PHONENUMBER_TYPE_WORK_FAX:
2004                 {
2005                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_FAX_HWORK");
2006                 }
2007                 break;
2008         case DETAIL_PHONENUMBER_TYPE_PAGER:
2009                 {
2010                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_PAGER");
2011                 }
2012                 break;
2013         case DETAIL_PHONENUMBER_TYPE_OTHER:
2014                 {
2015                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
2016                 }
2017                 break;
2018         }
2019
2020         return returnString;
2021 }
2022
2023 Tizen::Base::String
2024 ContactPresentationModel::GetEmailTypeString(DetailEmailType type)
2025 {
2026         String returnString;
2027
2028         switch (type)
2029         {
2030         case DETAIL_EMAIL_TYPE_HOME:
2031                 {
2032                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
2033                 }
2034                 break;
2035         case DETAIL_EMAIL_TYPE_WORK:
2036                 {
2037                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
2038                 }
2039                 break;
2040         case DETAIL_EMAIL_TYPE_OTHER:
2041                 {
2042                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
2043                 }
2044                 break;
2045         }
2046
2047         return returnString;
2048 }
2049
2050 Tizen::Base::String
2051 ContactPresentationModel::GetAddressTypeString(DetailAddressType type)
2052 {
2053         String returnString;
2054
2055         switch (type)
2056         {
2057         case DETAIL_ADDRESS_TYPE_HOME:
2058                 {
2059                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
2060                 }
2061                 break;
2062         case DETAIL_ADDRESS_TYPE_WORK:
2063                 {
2064                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
2065                 }
2066                 break;
2067         case DETAIL_ADDRESS_TYPE_OTHER:
2068                 {
2069                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
2070                 }
2071                 break;
2072         }
2073
2074         return returnString;
2075 }
2076
2077 Tizen::Base::String
2078 ContactPresentationModel::GetUrlTypeString(DetailUrlType type)
2079 {
2080         String returnString;
2081
2082         switch (type)
2083         {
2084         case DETAIL_URL_TYPE_HOME:
2085                 {
2086                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_HOME");
2087                 }
2088                 break;
2089         case DETAIL_URL_TYPE_WORK:
2090                 {
2091                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_WORK");
2092                 }
2093                 break;
2094         case DETAIL_URL_TYPE_OTHER:
2095                 {
2096                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
2097                 }
2098                 break;
2099         }
2100
2101         return returnString;
2102 }
2103
2104 Tizen::Base::String
2105 ContactPresentationModel::GetImAddressTypeString(DetailImAddressType type)
2106 {
2107         String returnString;
2108
2109         switch (type)
2110         {
2111         case DETAIL_IM_ADDRESS_TYPE_AIM:
2112                 {
2113                         returnString = IM_ADDRESS_AIM;
2114                 }
2115                 break;
2116         case DETAIL_IM_ADDRESS_TYPE_MSN:
2117                 {
2118                         returnString = IM_ADDRESS_MSN;
2119                 }
2120                 break;
2121         case DETAIL_IM_ADDRESS_TYPE_YAHOO:
2122                 {
2123                         returnString = IM_ADDRESS_YAHOO;
2124                 }
2125                 break;
2126         case DETAIL_IM_ADDRESS_TYPE_SKYPE:
2127                 {
2128                         returnString = IM_ADDRESS_SKYPE;
2129                 }
2130                 break;
2131         case DETAIL_IM_ADDRESS_TYPE_QQ:
2132                 {
2133                         returnString = IM_ADDRESS_QQ;
2134                 }
2135                 break;
2136         case DETAIL_IM_ADDRESS_TYPE_GOOGLE_TALK:
2137                 {
2138                         returnString = IM_ADDRESS_GOOGLE_TALK;
2139                 }
2140                 break;
2141         case DETAIL_IM_ADDRESS_TYPE_ICQ:
2142                 {
2143                         returnString = IM_ADDRESS_ICQ;
2144                 }
2145                 break;
2146         case DETAIL_IM_ADDRESS_TYPE_JABBER:
2147                 {
2148                         returnString = IM_ADDRESS_JABBER;
2149                 }
2150                 break;
2151         case DETAIL_IM_ADDRESS_TYPE_OTHER:
2152                 {
2153                         returnString = ResourceManager::GetString(L"IDS_PB_BODY_OTHER");
2154                 }
2155                 break;
2156         }
2157
2158         return returnString;
2159 }
2160
2161 DetailPhoneNumberType
2162 ContactPresentationModel::GetPhoneNumberType(int index)
2163 {
2164         DetailPhoneNumberType type;
2165         String value;
2166
2167         value = GetPhoneNumber(index, type);
2168
2169         return type;
2170 }
2171
2172 DetailEmailType
2173 ContactPresentationModel::GetEmailType(int index)
2174 {
2175         DetailEmailType type;
2176         String value;
2177
2178         value = GetEmail(index, type);
2179
2180         return type;
2181 }
2182
2183 DetailAddressType
2184 ContactPresentationModel::GetAddressType(DetailProperty id, int index)
2185 {
2186         DetailAddressType type;
2187         String value;
2188
2189         value = GetAddress(id, index, type);
2190
2191         return type;
2192 }
2193
2194 DetailUrlType
2195 ContactPresentationModel::GetUrlType(int index)
2196 {
2197         DetailUrlType type;
2198         String value;
2199
2200         value = GetUrl(index, type);
2201
2202         return type;
2203 }
2204
2205 DetailImAddressType
2206 ContactPresentationModel::GetImAddressType(int index)
2207 {
2208         DetailImAddressType type;
2209         String value;
2210
2211         value = GetImAddress(index, type);
2212
2213         return type;
2214 }
2215
2216 void
2217 ContactPresentationModel::SetAsFavorite(bool isFavorite)
2218 {
2219         AddressbookManager::GetInstance()->SetPersonAsFavorite(__pContact->GetPersonId(), isFavorite);
2220 }
2221
2222 bool
2223 ContactPresentationModel::IsFavorite(void)
2224 {
2225         bool isFavorite = false;
2226         PersonId personId = __pContact->GetPersonId();
2227
2228         Person* pPerson = AddressbookManager::GetInstance()->GetPersonN(personId);
2229         if (pPerson == null)
2230         {
2231                 return false;
2232         }
2233
2234         isFavorite = pPerson->IsFavorite();
2235
2236         delete pPerson;
2237
2238         return isFavorite;
2239 }