0789c1e20b281a06f96d1cea281a9d317e63d3d7
[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
335                 if(__pGroupListPresentationModel->IsContactInCategory(*__pGroupIdList, index) != listView.IsItemChecked(index))
336                 {
337                         modifiedItems++;
338                 }
339         }
340
341         if(modifiedItems > 0)
342         {
343                 pFooter->SetItemEnabled(0, true);
344         }
345         else
346         {
347                 pFooter->SetItemEnabled(0, false);
348         }
349         pFooter->Invalidate(true);
350         UpdateBottomLabel();
351 }
352
353 void
354 GroupSelectorForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction)
355 {
356 }
357
358 result
359 GroupSelectorForm::UpdateBottomLabel(void)
360 {
361         int itemCount = __pListView->GetItemCount();
362         int selectedCount = 0;
363         bool isChecked = false;
364
365         for (int index = 0; index < itemCount; index++)
366         {
367                 __pGroupIndexMap->GetValue(__pGroupListPresentationModel->GetCategoryId(index), isChecked);
368
369                 if (isChecked == true)
370                 {
371                         selectedCount++;
372                 }
373         }
374
375         String selected = ResourceManager::GetString(L"IDS_COM_POP_SELECTED");
376         selected.Append(L" (");
377         selected.Append(selectedCount);
378         selected.Append(L")");
379         __pBottomLabel->SetText(selected);
380
381
382         if (selectedCount > 0)
383         {
384                 __pBottomLabel->SetShowState(true);
385                 __pListView->SetSize(__pListView->GetSize().width, GetClientAreaBounds().height - __pBottomLabel->GetHeight());
386         }
387         else
388         {
389                 __pBottomLabel->SetShowState(false);
390                 __pListView->SetSize(__pListView->GetSize().width, GetClientAreaBounds().height);
391         }
392
393         __pBottomLabel->Invalidate(false);
394
395         return E_SUCCESS;
396 }
397
398 void
399 GroupSelectorForm::OnContactsChanged(void)
400 {
401 }
402
403 void
404 GroupSelectorForm::OnCategoriesChanged(void)
405 {
406         __pListView->UpdateList();
407
408         for (int index = 0; index < __pListView->GetItemCount(); index++)
409         {
410                 bool isChecked = false;
411
412                 __pGroupIndexMap->GetValue(__pGroupListPresentationModel->GetCategoryId(index), isChecked);
413
414                 if (isChecked == true)
415                 {
416                         result r = __pListView->SetItemChecked(index, true);
417                         TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item checked", GetErrorMessage(r));
418                 }
419                 else
420                 {
421                         result r = __pListView->SetItemChecked(index, false);
422                         TryReturnVoid(r == E_SUCCESS, "[%s] Unable to set item unchecked", GetErrorMessage(r));
423
424                         __pGroupIndexMap->Add(__pGroupListPresentationModel->GetCategoryId(index), false);
425                 }
426         }
427
428         __pListView->Invalidate(true);
429 }
430
431 void
432 GroupSelectorForm::OnForeground(void)
433 {
434         UpdateBottomLabel();
435 }
436
437 void
438 GroupSelectorForm::OnBackground(void)
439 {
440 }