Initialize Tizen 2.3
[apps/osp/Contacts.git] / src / CtGroupSelectorForm.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        CtGroupSelectorForm.cpp
19  * @brief       This is the implementation file for the GroupSelectorForm class.
20  */
21
22 #include <FApp.h>
23 #include <FBase.h>
24 #include "CtContactsApp.h"
25 #include "CtGroupListPresentationModel.h"
26 #include "CtGroupSelectorForm.h"
27 #include "CtResourceManager.h"
28 #include "CtSceneRegister.h"
29 #include "CtTypes.h"
30
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Controls;
37 using namespace Tizen::Ui::Scenes;
38 using namespace Tizen::Social;
39
40 static const int IDA_FOOTER_DONE = 11;
41 static const int IDA_FOOTER_CANCEL = 12;
42 static const int IDA_MORE_CREATE_GROUP = 13;
43
44 static const int X_LIST_ITEM_TEXT_MARGIN = 36;
45
46 GroupSelectorForm::GroupSelectorForm(void)
47  :__isDonePressed(false)
48  ,__pGroupListPresentationModel(null)
49  , __pListView(null)
50  , __pBottomLabel(null)
51  , __pOptionMenu(null)
52  , __pGroupIndexMap(null)
53  , __pGroupIdList(null)
54 {
55 }
56
57 GroupSelectorForm::~GroupSelectorForm(void)
58 {
59 }
60
61 void
62 GroupSelectorForm::Initialize(void)
63 {
64         Construct(L"IDL_GROUP_SELECTOR_FORM");
65 }
66
67 result
68 GroupSelectorForm::OnInitializing(void)
69 {
70         Footer* pFooter = GetFooter();
71         pFooter->SetItemEnabled(1, false);
72         pFooter->AddActionEventListener(*this);
73         SetFormBackEventListener(this);
74
75         __pGroupListPresentationModel = GroupListPresentationModel::GetInstance();
76         __pGroupListPresentationModel->AddGroupChangeListener(*this);
77
78         __pListView = static_cast<ListView*>(GetControl(L"IDC_LISTVIEW", false));
79         __pListView->SetItemProvider(*this);
80         __pListView->AddListViewItemEventListener(*this);
81
82         __pBottomLabel = static_cast<Label*>(GetControl(L"IDC_LABEL", false));
83
84         __pGroupIndexMap = new (std::nothrow) HashMapT<int, bool>();
85         __pGroupIndexMap->Construct(__pGroupListPresentationModel->GetCategoryCount());
86
87         __pOptionMenu = new (std::nothrow) OptionMenu();
88         __pOptionMenu->Construct();
89         __pOptionMenu->AddItem(ResourceManager::GetString(L"IDS_COM_BODY_CREATE"), IDA_MORE_CREATE_GROUP);
90         __pOptionMenu->AddActionEventListener(*this);
91         __pOptionMenu->SetShowState(false);
92
93         return E_SUCCESS;
94 }
95
96 result
97 GroupSelectorForm::OnTerminating(void)
98 {
99         result r = E_SUCCESS;
100
101         if (__pGroupIndexMap)
102         {
103                 __pGroupIndexMap->RemoveAll();
104                 delete __pGroupIndexMap;
105         }
106
107         if (__pGroupIdList)
108         {
109                 __pGroupIdList->RemoveAll(true);
110                 delete __pGroupIdList;
111         }
112
113         if (__pGroupListPresentationModel != null)
114         {
115                 __pGroupListPresentationModel->RemoveGroupChangeListener(*this);
116         }
117
118         delete __pOptionMenu;
119
120         return r;
121 }
122
123 void
124 GroupSelectorForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
125 {
126         result r = E_SUCCESS;
127
128         SceneManager* pSceneManager = SceneManager::GetInstance();
129         ArrayList* pList = null;
130
131         switch (actionId)
132         {
133         case IDA_MORE_CREATE_GROUP:
134                 {
135                         r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_GROUP_EDITOR, SCENE_TRANSITION_ANIMATION_TYPE_LEFT));
136                         TryCatch(r == E_SUCCESS, , "[%s] Unable to go forward", GetErrorMessage(r));
137                 }
138                 break;
139         case IDA_FOOTER_DONE:
140                 {
141                         __isDonePressed = true;
142                         pList = new (std::nothrow) ArrayList();
143                         pList->Construct(__pGroupIndexMap->GetCount());
144
145                         for (int i = 0; i < __pGroupListPresentationModel->GetCategoryCount(); i++)
146                         {
147                                 bool categoryValue = false;
148                                 RecordId categoryId = __pGroupListPresentationModel->GetCategoryId(i);
149
150                                 __pGroupIndexMap->GetValue(categoryId, categoryValue);
151
152                                 if (categoryValue)
153                                 {
154                                         pList->Add(*(new (std::nothrow) Integer(categoryId)));
155                                 }
156                         }
157
158                         r = pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), pList);
159                         TryCatch(r == E_SUCCESS, , "[%s] Unable to go backward", GetErrorMessage(r));
160                 }
161                 break;
162         case IDA_FOOTER_CANCEL:
163                 {
164                         r = pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
165                         TryCatch(r == E_SUCCESS, ,"[%s] Unable to go backward", GetErrorMessage(r));
166                 }
167                 break;
168         default:
169                 break;
170         }
171
172         return;
173
174 CATCH:
175         if (pList != null)
176         {
177                 pList->RemoveAll(true);
178                 delete pList;
179         }
180
181         return;
182 }
183
184 void
185 GroupSelectorForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
186 {
187         ContactsApp* pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
188         pContactsApp->AddContactsAppChangeEventListener(*this);
189
190         SetFormMenuEventListener(this);
191
192         if (pArgs == null)
193         {
194                 return;
195         }
196
197         if (__pGroupIdList != null)
198         {
199                 __pGroupIdList->RemoveAll(true);
200                 delete __pGroupIdList;
201         }
202
203         __pGroupIdList = pArgs;
204
205         __pGroupIndexMap->RemoveAll();
206
207         int categoryCount = __pGroupListPresentationModel->GetCategoryCount();
208         for (int index = 0; index < categoryCount; index++)
209         {
210                 if (__pGroupListPresentationModel->IsContactInCategory(*__pGroupIdList, index) == true)
211                 {
212                         __pGroupIndexMap->Add(__pGroupListPresentationModel->GetCategoryId(index), true);
213                 }
214                 else
215                 {
216                         __pGroupIndexMap->Add(__pGroupListPresentationModel->GetCategoryId(index), false);
217                 }
218         }
219
220         __pListView->UpdateList();
221
222         for (int index = 0; index < __pListView->GetItemCount(); index++)
223         {
224                 bool isChecked = false;
225
226                 __pGroupIndexMap->GetValue(__pGroupListPresentationModel->GetCategoryId(index), isChecked);
227
228                 if (isChecked == true)
229                 {
230                         result r = __pListView->SetItemChecked(index, true);
231                         TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item checked", GetErrorMessage(r));
232                 }
233         }
234
235         UpdateBottomLabel();
236 }
237
238 void
239 GroupSelectorForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
240 {
241         ContactsApp* pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
242         pContactsApp->RemoveContactsAppChangeEventListener(*this);
243         SetFormMenuEventListener(null);
244 }
245
246 void
247 GroupSelectorForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
248 {
249         SceneManager *pSceneManager = SceneManager::GetInstance();
250         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
251 }
252
253 void
254 GroupSelectorForm::OnFormMenuRequested(Tizen::Ui::Controls::Form& source)
255 {
256         __pOptionMenu->SetShowState(true);
257         __pOptionMenu->Show();
258 }
259
260 Tizen::Ui::Controls::ListItemBase*
261 GroupSelectorForm::CreateItem(int index, int itemWidth)
262 {
263         result r = E_SUCCESS;
264         String name;
265         Color textColor = Color::GetColor(COLOR_ID_BLACK);
266
267         ContactsApp* pContactsApp = static_cast<ContactsApp*>(ContactsApp::GetInstance());
268
269         CustomItem* pItem = new (std::nothrow) CustomItem();
270         pItem->Construct(Dimension(itemWidth, pContactsApp->GetListItemSingleLineHeight()),  LIST_ANNEX_STYLE_MARK);
271
272         name = __pGroupListPresentationModel->GetCategoryName(index);
273
274         r = pItem->AddElement(Rectangle(X_LIST_ITEM_TEXT_MARGIN/2, 0, itemWidth - (ListItemBase::GetAnnexWidth(LIST_ANNEX_STYLE_MARK) + X_LIST_ITEM_TEXT_MARGIN), pContactsApp->GetListItemSingleLineHeight()), index, name, pContactsApp->GetFontSize(), textColor, textColor, textColor);
275         TryCatch(r == E_SUCCESS, , "[%s] Unable to add element", GetErrorMessage(r));
276
277         return pItem;
278
279 CATCH:
280         delete pItem;
281         pItem = null;
282
283         return null;
284 }
285
286 bool
287 GroupSelectorForm::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
288 {
289         delete pItem;
290
291         return true;
292 }
293
294 int
295 GroupSelectorForm::GetItemCount(void)
296 {
297         return __pGroupListPresentationModel->GetCategoryCount();
298 }
299
300 void
301 GroupSelectorForm::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
302 {
303 }
304
305 void
306 GroupSelectorForm::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
307 {
308         result r = E_SUCCESS;
309
310         if (status == LIST_ITEM_STATUS_CHECKED)
311         {
312                 r = listView.SetItemChecked(index, true);
313                 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item checked", GetErrorMessage(r));
314
315                 r = __pGroupIndexMap->SetValue(__pGroupListPresentationModel->GetCategoryId(index), true);
316                 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set value", GetErrorMessage(r));
317         }
318         else if (status == LIST_ITEM_STATUS_UNCHECKED)
319         {
320                 r = listView.SetItemChecked(index, false);
321                 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item checked", GetErrorMessage(r));
322
323                 r = __pGroupIndexMap->SetValue(__pGroupListPresentationModel->GetCategoryId(index), false);
324                 TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set value", GetErrorMessage(r));
325         }
326
327         Footer* pFooter = GetFooter();
328
329         int modifiedItems = 0;
330         int itemCount = listView.GetItemCount();
331         for(int count = 0; count < itemCount; count++)
332         {
333                 if(__pGroupListPresentationModel->IsContactInCategory(*__pGroupIdList, count) != listView.IsItemChecked(count))
334                 {
335                         modifiedItems++;
336                 }
337         }
338
339         if(modifiedItems > 0)
340         {
341                 pFooter->SetItemEnabled(1, true);
342         }
343         else
344         {
345                 pFooter->SetItemEnabled(1, false);
346         }
347         pFooter->Invalidate(true);
348         UpdateBottomLabel();
349 }
350
351 void
352 GroupSelectorForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction)
353 {
354 }
355
356 result
357 GroupSelectorForm::UpdateBottomLabel(void)
358 {
359         int itemCount = __pListView->GetItemCount();
360         int selectedCount = 0;
361         bool isChecked = false;
362
363         for (int index = 0; index < itemCount; index++)
364         {
365                 __pGroupIndexMap->GetValue(__pGroupListPresentationModel->GetCategoryId(index), isChecked);
366
367                 if (isChecked == true)
368                 {
369                         selectedCount++;
370                 }
371         }
372
373         String selected = ResourceManager::GetString(L"IDS_COM_POP_SELECTED");
374         selected.Append(L" (");
375         selected.Append(selectedCount);
376         selected.Append(L")");
377         __pBottomLabel->SetText(selected);
378
379         if (selectedCount > 0)
380         {
381                 __pBottomLabel->SetShowState(true);
382                 __pListView->SetSize(__pListView->GetSize().width, GetClientAreaBounds().height - __pBottomLabel->GetHeight());
383         }
384         else
385         {
386                 __pBottomLabel->SetShowState(false);
387                 __pListView->SetSize(__pListView->GetSize().width, GetClientAreaBounds().height);
388         }
389
390         __pBottomLabel->Invalidate(false);
391
392         return E_SUCCESS;
393 }
394
395 void
396 GroupSelectorForm::OnContactsChanged(void)
397 {
398         if (__isDonePressed == false)
399         {
400                 SceneManager *pSceneManager = SceneManager::GetInstance();
401                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
402         }
403
404         __isDonePressed = false;
405
406 }
407
408 void
409 GroupSelectorForm::OnCategoriesChanged(void)
410 {
411         __pListView->UpdateList();
412
413         for (int index = 0; index < __pListView->GetItemCount(); index++)
414         {
415                 bool isChecked = false;
416
417                 __pGroupIndexMap->GetValue(__pGroupListPresentationModel->GetCategoryId(index), isChecked);
418
419                 if (isChecked == true)
420                 {
421                         result r = __pListView->SetItemChecked(index, true);
422                         TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item checked", GetErrorMessage(r));
423                 }
424                 else
425                 {
426                         result r = __pListView->SetItemChecked(index, false);
427                         TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item unchecked", GetErrorMessage(r));
428
429                         __pGroupIndexMap->Add(__pGroupListPresentationModel->GetCategoryId(index), false);
430                 }
431         }
432
433         Footer* pFooter = GetFooter();
434
435         int modifiedItems = 0;
436         int itemCount = __pListView->GetItemCount();
437
438         for(int count = 0; count < itemCount; count++)
439         {
440                 if(__pGroupListPresentationModel->IsContactInCategory(*__pGroupIdList, count) != __pListView->IsItemChecked(count))
441                 {
442                         modifiedItems++;
443                 }
444         }
445
446         if (pFooter != null)
447         {
448                 if(modifiedItems > 0)
449                 {
450                         pFooter->SetItemEnabled(1, true);
451                 }
452                 else
453                 {
454                         pFooter->SetItemEnabled(1, false);
455                 }
456
457                 pFooter->Invalidate(true);
458         }
459
460         __pListView->Invalidate(true);
461         UpdateBottomLabel();
462 }
463
464 void
465 GroupSelectorForm::OnForeground(void)
466 {
467         UpdateBottomLabel();
468 }
469
470 void
471 GroupSelectorForm::OnBackground(void)
472 {
473 }