Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnSettingsRejectListForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (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    PhnSettingsRejectListForm.cpp
19  * @brief   Setting Reject list form
20  */
21 #include <FApp.h>
22 #include "PhnAppUtility.h"
23 #include "PhnSettingsConstants.h"
24 #include "PhnSettingsPresentationModel.h"
25 #include "PhnSettingsRejectListForm.h"
26 #include "PhnSceneRegister.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Graphics;
32 using namespace Tizen::Ui;
33 using namespace Tizen::Ui::Controls;
34 using namespace Tizen::Ui::Scenes;
35
36 //constants
37 const int IDI_TOTAL_GROUP_COUNT = 1;
38 const int IDI_DEFAULT_LIST_ITEMID = 500;
39 //by default, list has 1 item
40 const int IDI_DEFAULT_LIST_COUNT = 1;
41 const int IDI_ADD_FOOTER_INDEX = 0;
42 const int IDI_DEL_FOOTER_INDEX = 1;
43 static const wchar_t* IDL_SETTINGS_REJECTLIST_FORM = L"IDL_SETTINGS_REJECTLIST_FORM";
44
45 SettingsRejectListForm::SettingsRejectListForm(void)
46         : __pNumbersList(null)
47         , __pSettingsPresentor(null)
48 {
49 }
50
51 SettingsRejectListForm::~SettingsRejectListForm(void)
52 {
53 }
54
55 bool
56 SettingsRejectListForm::Initialize(void)
57 {
58         Construct(IDL_SETTINGS_REJECTLIST_FORM);
59         return true;
60 }
61
62 result
63 SettingsRejectListForm::OnInitializing(void)
64 {
65         result r = E_SUCCESS;
66         __pSettingsPresentor = SettingsPresentationModel::GetInstance();
67
68         // Setup back event listener
69         SetFormBackEventListener(this);
70         //Initialize Footer
71         InitializeFooter();
72         //initialize list view
73         r = InitializeGroupedTableView();
74         return r;
75 }
76
77 void
78 SettingsRejectListForm::InitializeFooter(void)
79 {
80         Footer* pFooter = GetFooter();
81         if (pFooter != null)
82         {
83                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON_TEXT);
84
85                 //Add footer item
86                 String footerItemName(L"");
87                 footerItemName.Append(AppUtility::GetResourceString(IDS_ADD_BTN_STRING));
88                 FooterItem addFooterItem;
89                 addFooterItem.Construct(IDA_ADD_FOOTER_ITEMID);
90                 addFooterItem.SetText(footerItemName);
91                 pFooter->InsertItemAt(IDI_ADD_FOOTER_INDEX,addFooterItem);
92
93                 //Delete footer item
94                 footerItemName.Clear();
95                 footerItemName.Append(AppUtility::GetResourceString(IDS_DELETE_BTN_STRING));
96                 FooterItem delFooterItem;
97                 delFooterItem.Construct(IDA_DELETE_FOOTER_ITEMID);
98                 delFooterItem.SetText(footerItemName);
99                 pFooter->InsertItemAt(IDI_DEL_FOOTER_INDEX,delFooterItem);
100                 //back button
101                 pFooter->SetBackButton();
102
103                 pFooter->AddActionEventListener(*this);
104         }
105 }
106
107 result
108 SettingsRejectListForm::OnTerminating(void)
109 {
110         result r = E_SUCCESS;
111
112         if (__pNumbersList)
113         {
114                 delete __pNumbersList;
115                 __pNumbersList = null;
116         }
117         __pSettingsPresentor = null;
118         return r;
119 }
120
121 void
122 SettingsRejectListForm::OnActionPerformed(const Control& source, int actionId)
123 {
124         SceneManager* pSceneManager = SceneManager::GetInstance();
125         AppAssert(pSceneManager != null);
126
127         switch (actionId)
128         {
129         case IDA_ADD_FOOTER_ITEMID:
130         {
131                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_ADD_REJECT_NUMBER), null);
132         }
133         break;
134
135         case IDA_DELETE_FOOTER_ITEMID:
136         {
137                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_DEL_NUMBER), null);
138         }
139         break;
140
141         default:
142                 break;
143         }
144 }
145
146 void
147 SettingsRejectListForm::OnFormBackRequested(Form& source)
148 {
149         SceneManager* pSceneManager = SceneManager::GetInstance();
150         AppAssert(pSceneManager != null);
151         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_MAIN_SETTING_MENU), null);
152 }
153
154 void
155 SettingsRejectListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
156 {
157         // Add your scene activate code here
158         FetchNumbersRejectList();
159         GroupedTableView* pMenuTableView = static_cast<GroupedTableView*>(GetControl(IDC_GROUPEDTABLEVIEW));
160         if (pMenuTableView != null)
161         {
162                 //fetch latest added numbers
163                 pMenuTableView->UpdateTableView();
164
165                 //set item's check status
166                 for (int itemIndex = 0; itemIndex < GetItemCount(0); itemIndex++)
167                 {
168                         bool isItemActivated = false;
169                         if(itemIndex == 0)
170                         {
171                                 isItemActivated = __pSettingsPresentor->GetUnknownRejectStatus();
172                         }
173                         else
174                         {
175                                 int listIndex = itemIndex - 1;
176                                 if (__pNumbersList != null && listIndex < __pNumbersList->GetCount())
177                                 {
178                                         //numbers from fetched list
179                                         CallRejectInfo rejectInfo;
180                                         __pNumbersList->GetAt(listIndex, rejectInfo);
181                                         isItemActivated = rejectInfo.isActivated;
182                                 }
183                         }
184                         pMenuTableView->SetItemChecked(0, itemIndex, isItemActivated);
185                 }
186                 pMenuTableView->Invalidate(true);
187         }
188
189         //Enable or Disable delete footer button
190         bool enableItem = true;
191         if (__pNumbersList == null || __pNumbersList->GetCount() <= 0)
192         {
193                 enableItem = false;
194         }
195         Footer* pFooter = GetFooter();
196         if (pFooter != null)
197         {
198                 pFooter->SetItemEnabled(IDI_DEL_FOOTER_INDEX, enableItem);
199         }
200
201 }
202
203 void
204 SettingsRejectListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
205 {
206 }
207
208 void
209 SettingsRejectListForm::FetchNumbersRejectList(void)
210 {
211         if (__pNumbersList != null)
212         {
213                 delete __pNumbersList;
214                 __pNumbersList = null;
215         }
216         __pNumbersList = static_cast<ArrayListT<CallRejectInfo>*>(__pSettingsPresentor->GetCallRejectList());
217 }
218
219 result
220 SettingsRejectListForm::InitializeGroupedTableView(void)
221 {
222         result r = E_FAILURE;
223         GroupedTableView* pMenuTableView = static_cast<GroupedTableView*>(GetControl(IDC_GROUPEDTABLEVIEW));
224         if (pMenuTableView != null)
225         {
226                 pMenuTableView->SetGroupedLookEnabled(true);
227                 pMenuTableView->SetItemProvider(this);
228                 r = pMenuTableView->AddGroupedTableViewItemEventListener(*this);
229                 pMenuTableView->UpdateTableView();
230         }
231         return r;
232 }
233
234 void
235 SettingsRejectListForm::OnGroupedTableViewItemStateChanged(GroupedTableView& tableView, int groupIndex, int itemIndex, TableViewItem* pItem, TableViewItemStatus status)
236 {
237         if(status == TABLE_VIEW_ITEM_STATUS_SELECTED)
238         {
239                 if(itemIndex == 0)
240                 {
241                         if(tableView.IsItemChecked(groupIndex,itemIndex) == true)
242                         {
243                                 tableView.SetItemChecked(groupIndex,itemIndex,false);
244                                 __pSettingsPresentor->SetUnknownRejectStatus(false);
245                         }
246                         else
247                         {
248                                 tableView.SetItemChecked(groupIndex,itemIndex,true);
249                                 __pSettingsPresentor->SetUnknownRejectStatus(true);
250                         }
251                 }
252                 else
253                 {
254                         CallRejectInfo rejectInfo;
255                         __pNumbersList->GetAt((itemIndex -1), rejectInfo);
256                         // "unknown number" element status changed
257                         if (tableView.IsItemChecked(groupIndex,itemIndex) == true)
258                         {
259                                 tableView.SetItemChecked(groupIndex,itemIndex,false);
260                                 __pSettingsPresentor->UpdateCallRejectRow(rejectInfo.rowId,rejectInfo.phoneNumber,false,rejectInfo.rejectCondition);
261                         }
262                         else
263                         {
264                                 tableView.SetItemChecked(groupIndex,itemIndex,true);
265                                 __pSettingsPresentor->UpdateCallRejectRow(rejectInfo.rowId,rejectInfo.phoneNumber,true,rejectInfo.rejectCondition);
266                         }
267
268                 }
269
270         }
271         if (itemIndex == 0)
272         {
273                 // "unknown number" element status changed
274                 if (status == TABLE_VIEW_ITEM_STATUS_CHECKED)
275                 {
276                         __pSettingsPresentor->SetUnknownRejectStatus(true);
277                 }
278                 else if(status == TABLE_VIEW_ITEM_STATUS_UNCHECKED)
279                 {
280                         __pSettingsPresentor->SetUnknownRejectStatus(false);
281                 }
282         }
283         else
284         {
285                 CallRejectInfo rejectInfo;
286                 __pNumbersList->GetAt((itemIndex -1), rejectInfo);
287                 // "unknown number" element status changed
288                 if (status == TABLE_VIEW_ITEM_STATUS_CHECKED)
289                 {
290                         __pSettingsPresentor->UpdateCallRejectRow(rejectInfo.rowId,rejectInfo.phoneNumber,true,rejectInfo.rejectCondition);
291                 }
292                 else if(status == TABLE_VIEW_ITEM_STATUS_UNCHECKED)
293                 {
294                         __pSettingsPresentor->UpdateCallRejectRow(rejectInfo.rowId,rejectInfo.phoneNumber,false,rejectInfo.rejectCondition);
295                 }
296         }
297 }
298
299 int
300 SettingsRejectListForm::GetGroupCount(void)
301 {
302         return IDI_TOTAL_GROUP_COUNT;
303 }
304
305 int
306 SettingsRejectListForm::GetItemCount(int groupIndex)
307 {
308         int itemCount = IDI_DEFAULT_LIST_COUNT;
309         if (__pNumbersList != null)
310         {
311                 itemCount += __pNumbersList->GetCount();
312         }
313         return itemCount;
314 }
315
316 TableViewGroupItem*
317 SettingsRejectListForm::CreateGroupItem(int groupIndex, int itemWidth)
318 {
319         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
320         pItem->Construct(Dimension(itemWidth, H_LIST_HIDDENGROUP_ITEM));
321         pItem->SetBackgroundColor(COLOR_GROUP_ITEM_BG);
322         return pItem;
323 }
324
325 bool
326 SettingsRejectListForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
327 {
328         delete pItem;
329         pItem = null;
330         return true;
331 }
332
333 void
334 SettingsRejectListForm::UpdateGroupItem(int groupIndex, TableViewGroupItem* pItem)
335 {
336         return;
337 }
338
339 TableViewItem*
340 SettingsRejectListForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
341 {
342         //sub text rectangle
343         Rectangle criteriaRect(X_LIST_MENU_TEXT_ITEM, H_LIST_NORMAL_TXT_ITEM,
344                                                                           (itemWidth - X_LIST_MENU_TEXT_ITEM), H_LIST_SUB_TXT_ITEM);
345         String criteria(L"");
346         //text item rectangle
347         Rectangle itemNameRect(X_LIST_MENU_TEXT_ITEM, 0, (itemWidth - X_LIST_MENU_TEXT_ITEM - W_SLIDER_ICON), H_LIST_NORMAL_MENU_ITEM);
348         String itemName(L"");
349
350         switch (itemIndex)
351         {
352         case 0:
353         {
354                 //default "unknown" item
355                 itemName.Append(AppUtility::GetResourceString(IDS_DEFAULT_REJECT_ITEM_STRING));
356         }
357         break;
358
359         default:
360         {
361                 int listIndex = itemIndex - 1;
362                 if (__pNumbersList != null && listIndex < __pNumbersList->GetCount())
363                 {
364                         //numbers from fetched list
365                         CallRejectInfo rejectInfo;
366                         __pNumbersList->GetAt(listIndex, rejectInfo);
367                         itemName.Append(rejectInfo.phoneNumber);
368                         //update item name rectangle
369                         itemNameRect.y = Y_LIST_MENU_TEXT_ITEM;
370                         itemNameRect.height = H_LIST_NORMAL_TXT_ITEM;
371                         //criteria string
372                         switch (rejectInfo.rejectCondition)
373                         {
374                         case CALL_REJECT_MATCH_EXACTLY:
375                         {
376                                 criteria.Append(AppUtility::GetResourceString(IDS_MATCHCRITERIA_EXACT));
377                         }
378                         break;
379
380                         case CALL_REJECT_MATCH_START:
381                         {
382                                 criteria.Append(AppUtility::GetResourceString(IDS_MATCHCRITERIA_START));
383                         }
384                         break;
385
386                         case CALL_REJECT_MATCH_END:
387                         {
388                                 criteria.Append(AppUtility::GetResourceString(IDS_MATCHCRITERIA_END));
389                         }
390                         break;
391
392                         case CALL_REJECT_MATCH_INCLUDE:
393                         {
394                                 criteria.Append(AppUtility::GetResourceString(IDS_MATCHCRITERIA_INCLUDE));
395                         }
396                         break;
397                         }
398                 }
399         }
400         break;
401         }
402
403         TableViewItem* pItem = new (std::nothrow) TableViewItem();
404         pItem->Construct(Dimension(itemWidth, H_LIST_NORMAL_MENU_ITEM), TABLE_VIEW_ANNEX_STYLE_ONOFF_SLIDING);
405         //item name
406         Label* pItemLbl = new (std::nothrow) Label();
407         pItemLbl->Construct(itemNameRect, itemName);
408         pItemLbl->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
409         pItemLbl->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
410         pItemLbl->SetTextConfig(FONT_SIZE_MAIN_TXT, LABEL_TEXT_STYLE_NORMAL);
411         pItemLbl->SetTextColor(COLOR_MAIN_TXT);
412         pItem->AddControl(*pItemLbl);
413         //criteria
414         if(criteria.IsEmpty() == false)
415         {
416                 Label* pCriteriaLbl = new (std::nothrow) Label();
417                 pCriteriaLbl->Construct(criteriaRect, criteria);
418                 pCriteriaLbl->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
419                 pCriteriaLbl->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
420                 pCriteriaLbl->SetTextConfig(FONT_SIZE_SUB_TXT, LABEL_TEXT_STYLE_NORMAL);
421                 pCriteriaLbl->SetTextColor(COLOR_NORMAL_SUB_TXT);
422                 pItem->AddControl(*pCriteriaLbl);
423         }
424         pItem->SetBackgroundColor(COLOR_LIST_MENU_ITEM, TABLE_VIEW_ITEM_DRAWING_STATUS_NORMAL);
425         return pItem;
426 }
427
428 bool
429 SettingsRejectListForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
430 {
431         delete pItem;
432         pItem = null;
433         return true;
434 }
435
436 void
437 SettingsRejectListForm::UpdateItem(int groupIndex, int itemIndex, TableViewItem* pItem)
438 {
439         return;
440 }
441
442 int
443 SettingsRejectListForm::GetDefaultGroupItemHeight(void)
444 {
445         return H_LIST_HIDDENGROUP_ITEM;
446 }
447
448 int
449 SettingsRejectListForm::GetDefaultItemHeight(void)
450 {
451         return H_LIST_NORMAL_MENU_ITEM;
452 }