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