2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file CtGroupListPresentationModel.cpp
19 * @brief This is the implementation file for the GroupListPresentationModel class.
23 #include "CtGroupListPresentationModel.h"
24 #include "CtIContactEventListener.h"
25 #include "CtResourceManager.h"
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Media;
31 using namespace Tizen::Social;
33 GroupListPresentationModel* GroupListPresentationModel::__pInstance = null;
35 GroupListPresentationModel*
36 GroupListPresentationModel::GetInstance(void)
38 if (__pInstance == null)
47 GroupListPresentationModel::CreateInstance(void)
49 __pInstance = new (std::nothrow) GroupListPresentationModel();
50 result r = __pInstance->Construct();
51 TryCatch(r == E_SUCCESS, , "[%s] Construction Failed", GetErrorMessage(r));
53 std::atexit(DestroyInstance);
62 GroupListPresentationModel::DestroyInstance(void)
67 GroupListPresentationModel::GroupListPresentationModel(void)
68 : __pAddressbook(null)
69 , __pCategoryList(null)
71 , __pCategoryChangeListener(null)
75 GroupListPresentationModel::~GroupListPresentationModel(void)
77 delete __pAddressbook;
79 if (__pCategoryList != null)
81 __pCategoryList->RemoveAll(true);
82 delete __pCategoryList;
85 if (__pPersonList != null)
87 __pPersonList->RemoveAll(true);
91 if (__pCategoryChangeListener != null)
93 __pCategoryChangeListener->RemoveAll(false);
94 delete __pCategoryChangeListener;
99 GroupListPresentationModel::Construct(void)
101 result r = E_SUCCESS;
103 AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
105 __pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
106 if (__pAddressbook == null)
111 r = __pAddressbook->SetAddressbookChangeEventListener(this);
112 TryCatch(r == E_SUCCESS, , "[%s] Unable to set event listener", GetErrorMessage(r));
114 __pCategoryList = __pAddressbook->GetAllCategoriesN();
116 TryCatch(__pCategoryList && r == E_SUCCESS, , "[%s] Unable to get all categories", GetErrorMessage(r));
118 __pCategoryChangeListener = new (std::nothrow) ArrayList();
119 __pCategoryChangeListener->Construct();
124 delete __pAddressbook;
125 __pAddressbook = null;
127 if (__pCategoryList != null)
129 __pCategoryList->RemoveAll(true);
131 delete __pCategoryList;
132 __pCategoryList = null;
135 delete __pCategoryChangeListener;
136 __pCategoryChangeListener = null;
142 GroupListPresentationModel::RemoveCategory(int index)
144 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
145 if (pCategory == null)
150 __pAddressbook->RemoveCategory(*pCategory);
156 GroupListPresentationModel::GetCategoryName(int index)
160 Category* pCategory = static_cast<Category *>(__pCategoryList->GetAt(index));
161 if (pCategory == null)
166 returnName = pCategory->GetName();
171 Tizen::Base::Collection::IList*
172 GroupListPresentationModel::GetContactIdListInCategory(int index)
174 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
175 if (pCategory == null)
180 IList* pContactList = AddressbookManager::GetInstance()->GetContactsByCategoryN(pCategory->GetRecordId());
181 if (pContactList == null)
186 ArrayList* pList = new (std::nothrow) ArrayList();
189 for (int i = 0; i < pContactList->GetCount(); i++)
191 Contact* pContact = static_cast<Contact*>(pContactList->GetAt(i));
192 if (pContact != null)
194 pList->Add(*(new (std::nothrow) Integer(pContact->GetRecordId())));
204 GroupListPresentationModel::IsContactInCategory(const Tizen::Base::Collection::IList& categoryIdList, int categoryIndex)
206 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(categoryIndex));
207 if (pCategory == null)
212 IEnumerator* pEnum = categoryIdList.GetEnumeratorN();
214 while (pEnum->MoveNext() == E_SUCCESS)
216 String* pCategoryIdString = static_cast<String *>(pEnum->GetCurrent());
218 if (pCategoryIdString->Equals(Integer::ToString(pCategory->GetRecordId()), true))
232 GroupListPresentationModel::IsDefaultCategory(int index)
234 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
235 if (pCategory == null)
240 return pCategory->IsDefault();
244 GroupListPresentationModel::GetContactCountInCategoryN(int index, IList*& pContactList, AppControlReturnType returnType)
246 Category* pCategory = static_cast<Category *>(__pCategoryList->GetAt(index));
247 if (pCategory == null)
251 if (returnType != APP_CONTROL_RETURN_TYPE_PHONE &&
252 returnType != APP_CONTROL_RETURN_TYPE_EMAIL)
254 return pCategory->GetMemberCount();
259 AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
261 mainFilter.AppendInt(FI_CONJ_OP_NONE, PERSON_FI_PR_CATEGORY_ID, FI_CMP_OP_EQUAL, pCategory->GetRecordId());
262 if (returnType == APP_CONTROL_RETURN_TYPE_PHONE)
264 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, true);
266 else if (returnType == APP_CONTROL_RETURN_TYPE_EMAIL)
268 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_EMAIL, FI_CMP_OP_EQUAL, true);
271 pList = AddressbookManager::GetInstance()->SearchN(mainFilter);
278 returnCount = pList->GetCount();
280 pContactList = pList;
286 GroupListPresentationModel::GetContactCountInNotAssignedCategory(AppControlReturnType returnType)
290 AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
292 mainFilter.AppendInt(FI_CONJ_OP_NONE, PERSON_FI_PR_CATEGORY_ID, FI_CMP_OP_IS_NULL, INVALID_RECORD_ID);
293 if (returnType == APP_CONTROL_RETURN_TYPE_PHONE)
295 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, true);
297 else if (returnType == APP_CONTROL_RETURN_TYPE_EMAIL)
299 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_EMAIL, FI_CMP_OP_EQUAL, true);
302 pList = AddressbookManager::GetInstance()->SearchN(mainFilter);
309 returnCount = pList->GetCount();
311 pList->RemoveAll(true);
318 GroupListPresentationModel::GetCategoryCount(void)
320 return __pAddressbook->GetCategoryCount();
324 GroupListPresentationModel::GetContactCount(void)
326 return __pAddressbook->GetContactCount();
329 Tizen::Graphics::Bitmap*
330 GroupListPresentationModel::GetThumbnailN(int index, IList* pList)
333 Bitmap* pThumbnail = null;
334 IList* pPersonList = null;
335 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
336 TryReturn(pCategory != null, null, "Unable to get a category");
338 filePath = pCategory->GetThumbnailPath();
340 if (filePath.IsEmpty())
344 pPersonList = __pPersonList;
351 for (int i = 0; i < pPersonList->GetCount(); i++)
353 Person* pPerson = static_cast<Person*>(pPersonList->GetAt(i));
354 TryReturn(pPerson != null, null, "Unable to get a person");
355 filePath = pPerson->GetThumbnailPath();
357 if (filePath.IsEmpty() == false)
364 if (filePath.IsEmpty() == false)
366 ImageBuffer thumbnailImageBuffer;
367 result r = thumbnailImageBuffer.Construct(filePath);
370 pThumbnail = thumbnailImageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_NONE);
378 GroupListPresentationModel::UpdatePersonList(int categoryIndex)
380 RecordId categoryId = INVALID_RECORD_ID;
382 if (__pCategoryList != null)
384 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(categoryIndex));
385 if (pCategory != null)
387 categoryId = pCategory->GetRecordId();
390 if (__pPersonList != null)
392 delete __pPersonList;
394 __pPersonList = AddressbookManager::GetInstance()->GetPersonsByCategoryN(categoryId);
397 if (__pPersonList == null)
405 Tizen::Social::RecordId
406 GroupListPresentationModel::GetCategoryId(int index)
408 Category* pCategory = static_cast<Category *>(__pCategoryList->GetAt(index));
409 if (pCategory == null)
414 return pCategory->GetRecordId();
418 GroupListPresentationModel::UpdateCategoryList(void)
420 if (__pCategoryList != null)
422 __pCategoryList->RemoveAll(true);
423 delete __pCategoryList;
424 __pCategoryList = null;
427 __pCategoryList = __pAddressbook->GetAllCategoriesN();
428 if ((__pCategoryList == null) || IsFailed(GetLastResult()))
437 GroupListPresentationModel::AddContactToCategory(int index, Tizen::Base::Collection::IList* pList)
444 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
445 if (pCategory == null)
450 Integer* pContactId = null;
451 IEnumerator* pEnum = pList->GetEnumeratorN();
452 while (pEnum->MoveNext() == E_SUCCESS)
454 pContactId = static_cast<Integer*>(pEnum->GetCurrent());
455 if (pContactId != null)
457 pCategory->AddMember(pContactId->ToInt());
462 AddressbookManager::GetInstance()->UpdateCategory(*pCategory);
468 GroupListPresentationModel::AddGroupChangeListener(const IContactEventListener& listener)
470 __pCategoryChangeListener->Add(listener);
474 GroupListPresentationModel::RemoveGroupChangeListener(const IContactEventListener& listener)
476 __pCategoryChangeListener->Remove(listener, false);
480 GroupListPresentationModel::OnContactsChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
482 UpdateCategoryList();
484 IContactEventListener* pInterface = null;
485 IEnumerator* pEnum = __pCategoryChangeListener->GetEnumeratorN();
486 while (pEnum->MoveNext() == E_SUCCESS)
488 pInterface = static_cast<IContactEventListener *>(pEnum->GetCurrent());
489 if (pInterface == null)
494 pInterface->OnContactsChanged();
501 GroupListPresentationModel::OnCategoriesChanged(const Tizen::Base::Collection::IList& categoryChangeInfoList)
503 UpdateCategoryList();
505 IContactEventListener* pInterface = null;
506 IEnumerator* pEnum = __pCategoryChangeListener->GetEnumeratorN();
507 while (pEnum->MoveNext() == E_SUCCESS)
509 pInterface = static_cast<IContactEventListener *>(pEnum->GetCurrent());
510 if (pInterface == null)
515 pInterface->OnCategoriesChanged();