Initialize Tizen 2.3
[apps/osp/Contacts.git] / src / CtGroupListPresentationModel.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        CtGroupListPresentationModel.cpp
19  * @brief       This is the implementation file for the GroupListPresentationModel class.
20  */
21
22 #include <FMedia.h>
23 #include "CtGroupListPresentationModel.h"
24 #include "CtIContactEventListener.h"
25 #include "CtResourceManager.h"
26
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;
32
33 GroupListPresentationModel* GroupListPresentationModel::__pInstance = null;
34
35 GroupListPresentationModel*
36 GroupListPresentationModel::GetInstance(void)
37 {
38         if (__pInstance == null)
39         {
40                 CreateInstance();
41         }
42
43         return __pInstance;
44 }
45
46 void
47 GroupListPresentationModel::CreateInstance(void)
48 {
49         __pInstance = new (std::nothrow) GroupListPresentationModel();
50         result r = __pInstance->Construct();
51         TryCatch(r == E_SUCCESS, , "[%s] Construction Failed", GetErrorMessage(r));
52
53         std::atexit(DestroyInstance);
54         return;
55
56 CATCH:
57         delete __pInstance;
58         __pInstance = null;
59 }
60
61 void
62 GroupListPresentationModel::DestroyInstance(void)
63 {
64         delete __pInstance;
65 }
66
67 GroupListPresentationModel::GroupListPresentationModel(void)
68 : __pAddressbook(null)
69 , __pCategoryList(null)
70 , __pPersonList(null)
71 , __pCategoryChangeListener(null)
72 {
73 }
74
75 GroupListPresentationModel::~GroupListPresentationModel(void)
76 {
77         delete __pAddressbook;
78
79         if (__pCategoryList != null)
80         {
81                 __pCategoryList->RemoveAll(true);
82                 delete __pCategoryList;
83         }
84
85         if (__pPersonList != null)
86         {
87                 __pPersonList->RemoveAll(true);
88                 delete __pPersonList;
89         }
90
91         if (__pCategoryChangeListener != null)
92         {
93                 __pCategoryChangeListener->RemoveAll(false);
94                 delete __pCategoryChangeListener;
95         }
96 }
97
98 result
99 GroupListPresentationModel::Construct(void)
100 {
101         result r = E_SUCCESS;
102
103         AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
104
105         __pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);
106         if (__pAddressbook == null)
107         {
108                 return E_FAILURE;
109         }
110
111         r = __pAddressbook->SetAddressbookChangeEventListener(this);
112         TryCatch(r == E_SUCCESS, , "[%s] Unable to set event listener", GetErrorMessage(r));
113
114         __pCategoryList = __pAddressbook->GetAllCategoriesN();
115         r = GetLastResult();
116         TryCatch(__pCategoryList && r == E_SUCCESS, , "[%s] Unable to get all categories", GetErrorMessage(r));
117
118         __pCategoryChangeListener = new (std::nothrow) ArrayList();
119         __pCategoryChangeListener->Construct();
120
121         return r;
122
123 CATCH:
124         delete __pAddressbook;
125         __pAddressbook = null;
126
127         if (__pCategoryList != null)
128         {
129                 __pCategoryList->RemoveAll(true);
130
131                 delete __pCategoryList;
132                 __pCategoryList = null;
133         }
134
135         delete __pCategoryChangeListener;
136         __pCategoryChangeListener = null;
137
138         return r;
139 }
140
141 result
142 GroupListPresentationModel::RemoveCategory(int index)
143 {
144         Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
145         if (pCategory == null)
146         {
147                 return E_FAILURE;
148         }
149
150         __pAddressbook->RemoveCategory(*pCategory);
151
152         return E_SUCCESS;
153 }
154
155 Tizen::Base::String
156 GroupListPresentationModel::GetCategoryName(int index)
157 {
158         String returnName;
159
160         Category* pCategory = static_cast<Category *>(__pCategoryList->GetAt(index));
161         if (pCategory == null)
162         {
163                 return returnName;
164         }
165
166         returnName = pCategory->GetName();
167
168         return returnName;
169 }
170
171 Tizen::Base::Collection::IList*
172 GroupListPresentationModel::GetContactIdListInCategory(int index)
173 {
174         Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
175         if (pCategory == null)
176         {
177                 return null;
178         }
179
180         IList* pContactList = AddressbookManager::GetInstance()->GetContactsByCategoryN(pCategory->GetRecordId());
181         if (pContactList == null)
182         {
183                 return null;
184         }
185
186         ArrayList* pList = new (std::nothrow) ArrayList();
187         pList->Construct();
188
189         for (int i = 0; i < pContactList->GetCount(); i++)
190         {
191                 Contact* pContact = static_cast<Contact*>(pContactList->GetAt(i));
192                 if (pContact != null)
193                 {
194                         pList->Add(*(new (std::nothrow) Integer(pContact->GetRecordId())));
195                 }
196         }
197
198         delete pContactList;
199
200         return pList;
201 }
202
203 bool
204 GroupListPresentationModel::IsContactInCategory(const Tizen::Base::Collection::IList& categoryIdList, int categoryIndex)
205 {
206         Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(categoryIndex));
207         if (pCategory == null)
208         {
209                 return false;
210         }
211
212         IEnumerator* pEnum = categoryIdList.GetEnumeratorN();
213
214         while (pEnum->MoveNext() == E_SUCCESS)
215         {
216                 String* pCategoryIdString = static_cast<String *>(pEnum->GetCurrent());
217
218                 if (pCategoryIdString->Equals(Integer::ToString(pCategory->GetRecordId()), true))
219                 {
220                         delete pEnum;
221
222                         return true;
223                 }
224         }
225
226         delete pEnum;
227
228         return false;
229 }
230
231 bool
232 GroupListPresentationModel::IsDefaultCategory(int index)
233 {
234         Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
235         if (pCategory == null)
236         {
237                 return false;
238         }
239
240         return pCategory->IsDefault();
241 }
242
243 int
244 GroupListPresentationModel::GetContactCountInCategoryN(int index, IList*& pContactList, AppControlReturnType returnType)
245 {
246         Category* pCategory = static_cast<Category *>(__pCategoryList->GetAt(index));
247         if (pCategory == null)
248         {
249                 return -1;
250         }
251         if (returnType != APP_CONTROL_RETURN_TYPE_PHONE &&
252                 returnType != APP_CONTROL_RETURN_TYPE_EMAIL)
253         {
254                 return pCategory->GetMemberCount();
255         }
256
257         int returnCount = 0;
258         IList* pList = null;
259         AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
260
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)
263         {
264                 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, true);
265         }
266         else if (returnType == APP_CONTROL_RETURN_TYPE_EMAIL)
267         {
268                 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_EMAIL, FI_CMP_OP_EQUAL, true);
269         }
270
271         pList = AddressbookManager::GetInstance()->SearchN(mainFilter);
272
273         if (pList == null)
274         {
275                 return returnCount;
276         }
277
278         returnCount = pList->GetCount();
279
280         pContactList = pList;
281
282         return returnCount;
283 }
284
285 int
286 GroupListPresentationModel::GetContactCountInNotAssignedCategory(AppControlReturnType returnType)
287 {
288         int returnCount = 0;
289         IList* pList = null;
290         AddressbookFilter mainFilter(AB_FI_TYPE_PERSON);
291
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)
294         {
295                 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_PHONE, FI_CMP_OP_EQUAL, true);
296         }
297         else if (returnType == APP_CONTROL_RETURN_TYPE_EMAIL)
298         {
299                 mainFilter.AppendBool(FI_CONJ_OP_AND, PERSON_FI_PR_HAS_EMAIL, FI_CMP_OP_EQUAL, true);
300         }
301
302         pList = AddressbookManager::GetInstance()->SearchN(mainFilter);
303
304         if (pList == null)
305         {
306                 return returnCount;
307         }
308
309         returnCount = pList->GetCount();
310
311         pList->RemoveAll(true);
312         delete pList;
313
314         return returnCount;
315 }
316
317 int
318 GroupListPresentationModel::GetCategoryCount(void)
319 {
320         return __pAddressbook->GetCategoryCount();
321 }
322
323 int
324 GroupListPresentationModel::GetContactCount(void)
325 {
326         return __pAddressbook->GetContactCount();
327 }
328
329 Tizen::Graphics::Bitmap*
330 GroupListPresentationModel::GetThumbnailN(int index, IList* pList)
331 {
332         String filePath;
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");
337
338         filePath = pCategory->GetThumbnailPath();
339
340         if (filePath.IsEmpty())
341         {
342                 if (pList == null)
343                 {
344                         pPersonList = __pPersonList;
345                 }
346                 else
347                 {
348                         pPersonList = pList;
349                 }
350
351                 for (int i = 0; i < pPersonList->GetCount(); i++)
352                 {
353                         Person* pPerson = static_cast<Person*>(pPersonList->GetAt(i));
354                         TryReturn(pPerson != null, null, "Unable to get a person");
355                         filePath = pPerson->GetThumbnailPath();
356
357                         if (filePath.IsEmpty() == false)
358                         {
359                                 break;
360                         }
361                 }
362         }
363
364         if (filePath.IsEmpty() == false)
365         {
366                 ImageBuffer thumbnailImageBuffer;
367                 result r = thumbnailImageBuffer.Construct(filePath);
368                 if (r == E_SUCCESS)
369                 {
370                         pThumbnail = thumbnailImageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_RGB565, BUFFER_SCALING_NONE);
371                 }
372         }
373
374         return pThumbnail;
375 }
376
377 result
378 GroupListPresentationModel::UpdatePersonList(int categoryIndex)
379 {
380         RecordId categoryId = INVALID_RECORD_ID;
381
382         if (__pCategoryList != null)
383         {
384                 Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(categoryIndex));
385                 if (pCategory != null)
386                 {
387                         categoryId = pCategory->GetRecordId();
388                 }
389
390                 if (__pPersonList != null)
391                 {
392                         delete __pPersonList;
393                 }
394                 __pPersonList = AddressbookManager::GetInstance()->GetPersonsByCategoryN(categoryId);
395         }
396
397         if (__pPersonList == null)
398         {
399                 return E_FAILURE;
400         }
401
402         return E_SUCCESS;
403 }
404
405 Tizen::Social::RecordId
406 GroupListPresentationModel::GetCategoryId(int index)
407 {
408         Category* pCategory = static_cast<Category *>(__pCategoryList->GetAt(index));
409         if (pCategory == null)
410         {
411                 return -1;
412         }
413
414         return pCategory->GetRecordId();
415 }
416
417 result
418 GroupListPresentationModel::UpdateCategoryList(void)
419 {
420         if (__pCategoryList != null)
421         {
422                 __pCategoryList->RemoveAll(true);
423                 delete __pCategoryList;
424                 __pCategoryList = null;
425         }
426
427         __pCategoryList = __pAddressbook->GetAllCategoriesN();
428         if ((__pCategoryList == null) || IsFailed(GetLastResult()))
429         {
430                 return E_FAILURE;
431         }
432
433         return E_SUCCESS;
434 }
435
436 result
437 GroupListPresentationModel::AddContactToCategory(int index, Tizen::Base::Collection::IList* pList)
438 {
439         if (pList == null)
440         {
441                 return E_FAILURE;
442         }
443
444         Category* pCategory = static_cast<Category*>(__pCategoryList->GetAt(index));
445         if (pCategory == null)
446         {
447                 return E_FAILURE;
448         }
449
450         Integer* pContactId = null;
451         IEnumerator* pEnum = pList->GetEnumeratorN();
452         while (pEnum->MoveNext() == E_SUCCESS)
453         {
454                 pContactId = static_cast<Integer*>(pEnum->GetCurrent());
455                 if (pContactId != null)
456                 {
457                         pCategory->AddMember(pContactId->ToInt());
458                 }
459         }
460         delete pEnum;
461
462         AddressbookManager::GetInstance()->UpdateCategory(*pCategory);
463
464         return E_SUCCESS;
465 }
466
467 void
468 GroupListPresentationModel::AddGroupChangeListener(const IContactEventListener& listener)
469 {
470         __pCategoryChangeListener->Add(listener);
471 }
472
473 void
474 GroupListPresentationModel::RemoveGroupChangeListener(const IContactEventListener& listener)
475 {
476         __pCategoryChangeListener->Remove(listener, false);
477 }
478
479 void
480 GroupListPresentationModel::OnContactsChanged(const Tizen::Base::Collection::IList& contactChangeInfoList)
481 {
482         UpdateCategoryList();
483
484         IContactEventListener* pInterface = null;
485         IEnumerator* pEnum = __pCategoryChangeListener->GetEnumeratorN();
486         while (pEnum->MoveNext() == E_SUCCESS)
487         {
488                 pInterface = static_cast<IContactEventListener *>(pEnum->GetCurrent());
489                 if (pInterface == null)
490                 {
491                         delete pEnum;
492                         return;
493                 }
494                 pInterface->OnContactsChanged();
495         }
496
497         delete pEnum;
498 }
499
500 void
501 GroupListPresentationModel::OnCategoriesChanged(const Tizen::Base::Collection::IList& categoryChangeInfoList)
502 {
503         UpdateCategoryList();
504
505         IContactEventListener* pInterface = null;
506         IEnumerator* pEnum = __pCategoryChangeListener->GetEnumeratorN();
507         while (pEnum->MoveNext() == E_SUCCESS)
508         {
509                 pInterface = static_cast<IContactEventListener *>(pEnum->GetCurrent());
510                 if (pInterface == null)
511                 {
512                         delete pEnum;
513                         return;
514                 }
515                 pInterface->OnCategoriesChanged();
516         }
517
518         delete pEnum;
519 }