Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnSettingsDeleteListForm.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    PhnSettingsDeleteListForm.cpp
19  * @brief   Settings Delete list form
20  */
21 #include <FApp.h>
22 #include "PhnAppUtility.h"
23 #include "PhnCustomItemBgElement.h"
24 #include "PhnSettingsConstants.h"
25 #include "PhnSettingsDeleteListForm.h"
26 #include "PhnSettingsPresentationModel.h"
27 #include "PhnSceneRegister.h"
28 #include "PhnTypes.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Ui::Controls;
36 using namespace Tizen::Ui::Scenes;
37
38 //constants
39 const int IDI_SELECT_ALL_ITEM_ELEMENT_ID = 20010;
40 //by default, list has 1 item
41 const int IDI_DEFAULT_LIST_COUNT = 1;
42 //Element Id for main text used in List view item
43 const int IDI_LIST_MAIN_TXT_ITEMID = 1000;
44 static const wchar_t* IDL_SETTINGS_DELETE_LIST_FORM = L"IDL_SETTINGS_DELETE_LIST_FORM";
45 static const wchar_t* IDC_LABEL_DELETE_SELECTED = L"IDC_LABEL_DELETE_SELECTED";
46
47 static const unsigned int COLOR_DELETE_LABEL_SELECTED = Color32<215, 225, 232, 255>::Value;
48
49 SettingsDeleteListForm::SettingsDeleteListForm(ListOfType listOfType)
50         : __listOfType(listOfType)
51         , __pNumbersList(null)
52         , __pMsgsMap(null)
53         , __pSettingsPresentor(null)
54         , __checkedItemCount(0)
55         , __pSelectedLabel(null)
56 {
57 }
58
59 SettingsDeleteListForm::~SettingsDeleteListForm(void)
60 {
61 }
62
63 void
64 SettingsDeleteListForm::Initialize(void)
65 {
66         Construct(IDL_SETTINGS_DELETE_LIST_FORM);
67 }
68
69 result
70 SettingsDeleteListForm::OnInitializing(void)
71 {
72         result r = E_SUCCESS;
73
74         __pSettingsPresentor = SettingsPresentationModel::GetInstance();
75
76         String title(L"");
77         switch (__listOfType)
78         {
79         case LISTOF_NUMBERS:
80         {
81                 title.Append(AppUtility::GetResourceString(IDS_DELETE_NUMBER_TITLE));
82         }
83         break;
84
85         case LISTOF_MESSAGES:
86         {
87                 title.Append(AppUtility::GetResourceString(IDS_DELETE_MSGS_TITLE));
88         }
89         break;
90
91         default:
92                 break;
93         }
94
95         //set Title
96         Header* pHeader = GetHeader();
97         if (pHeader != null)
98         {
99                 pHeader->SetTitleText(title);
100         }
101         //Initialize Footer
102         InitializeFooter();
103
104         //initialize list view
105         r = InitializeListView();
106         return r;
107 }
108
109 void
110 SettingsDeleteListForm::InitializeSelectLabel()
111 {
112         __pSelectedLabel = static_cast<Label*>(GetControl(IDC_LABEL_DELETE_SELECTED));
113         if(__pSelectedLabel != null)
114         {
115                 if(__listOfType == LISTOF_NUMBERS)
116                 {
117                         __pSelectedLabel->SetText(AppUtility::GetResourceString(IDS_SELECT_NUMBER));
118                 }
119                 else if(__listOfType == LISTOF_MESSAGES)
120                 {
121                         __pSelectedLabel->SetText(AppUtility::GetResourceString(IDS_SELECT_MESSAGE));
122                 }
123
124                 SetControlAlwaysOnTop(*__pSelectedLabel,true);
125                 __pSelectedLabel->SetBackgroundColor(COLOR_DELETE_LABEL_SELECTED);
126                 __pSelectedLabel->SetShowState(true);
127         }
128 }
129 void
130 SettingsDeleteListForm::InitializeFooter(void)
131 {
132         Footer* pFooter = GetFooter();
133         if (pFooter != null)
134         {
135                 pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON_TEXT);
136
137                 String footerItemName(L"");
138                 //Delete footer item
139                 footerItemName.Append(AppUtility::GetResourceString(IDS_DELETE_BTN_STRING));
140                 FooterItem delFooterItem;
141                 delFooterItem.Construct(IDA_DELETE_FOOTER_ITEMID);
142                 delFooterItem.SetText(footerItemName);
143                 pFooter->InsertItemAt(0, delFooterItem);
144                 pFooter->SetItemEnabled(0, false);
145
146                 //Cancel button
147                 Bitmap* pNormalBmp = AppUtility::GetBitmapFromResourcesN(IDB_CANCEL_FOOTER_ITEM, W_HDR_FTR_ICON, H_HDR_FTR_ICON);
148                 ButtonItem cancelButtonItem;
149                 cancelButtonItem.Construct(BUTTON_ITEM_STYLE_ICON, IDA_CANCEL_FOOTER_ITEMID);
150                 cancelButtonItem.SetIcon(BUTTON_ITEM_STATUS_NORMAL, pNormalBmp);
151                 cancelButtonItem.SetIcon(BUTTON_ITEM_STATUS_PRESSED, pNormalBmp);
152                 pFooter->SetButton(BUTTON_POSITION_RIGHT, cancelButtonItem);
153                 delete pNormalBmp;
154                 pNormalBmp = null;
155
156                 pFooter->AddActionEventListener(*this);
157         }
158 }
159
160 result
161 SettingsDeleteListForm::OnTerminating(void)
162 {
163         result r = E_SUCCESS;
164
165         if (__pNumbersList)
166         {
167                 delete __pNumbersList;
168                 __pNumbersList = null;
169         }
170
171         if (__pMsgsMap)
172         {
173                 delete __pMsgsMap;
174                 __pMsgsMap = null;
175         }
176         __pSettingsPresentor = null;
177         return r;
178 }
179
180 void
181 SettingsDeleteListForm::OnActionPerformed(const Control& source, int actionId)
182 {
183         SceneManager* pSceneManager = SceneManager::GetInstance();
184         AppAssert(pSceneManager != null);
185
186         switch (actionId)
187         {
188         case IDA_CANCEL_FOOTER_ITEMID:
189         {
190                 if (__listOfType == LISTOF_NUMBERS)
191                 {
192                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_CALL_REJECTLIST_MENU), null);
193                 }
194                 else
195                 {
196                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_CALL_REJECTMSGS_MENU), null);
197                 }
198         }
199         break;
200         case IDA_DELETE_FOOTER_ITEMID:
201         {
202                 if (__listOfType == LISTOF_NUMBERS)
203                 {
204                         DeleteRejectNumbersList();
205                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_CALL_REJECTLIST_MENU), null);
206                 }
207                 else
208                 {
209                         DeleteRejectMsgsList();
210                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_CALL_REJECTMSGS_MENU), null);
211                 }
212         }
213         break;
214
215         default:
216                 break;
217         }
218 }
219
220 void
221 SettingsDeleteListForm::DeleteRejectNumbersList(void)
222 {
223         ListView* pDelListView = static_cast<ListView*>(GetControl(IDC_LISTVIEW));
224         if (pDelListView != null)
225         {
226                 for (int index = 1; index < pDelListView->GetItemCount(); index++)
227                 {
228                         if (pDelListView->IsItemChecked(index) == true)
229                         {
230                                 CallRejectInfo rejectInfo;
231                                 __pNumbersList->GetAt(index-1, rejectInfo);
232                                 __pSettingsPresentor->RemoveCallRejectRow(rejectInfo.rowId);
233                         }
234                 }
235         }
236 }
237
238 void
239 SettingsDeleteListForm::DeleteRejectMsgsList(void)
240 {
241         IListT<int> *pMsgKeyList = __pMsgsMap->GetKeysN();
242         //list of deleted keys
243         ArrayListT<int> *pDelKeyList = new (std::nothrow) ArrayListT<int>();
244         pDelKeyList->Construct();
245         //fetch checked messages to be deleted
246         ListView* pDelListView = static_cast<ListView*>(GetControl(IDC_LISTVIEW));
247         if (pDelListView != null)
248         {
249                 for (int index = 1; index < pDelListView->GetItemCount(); index++)
250                 {
251                         if (pDelListView->IsItemChecked(index) == true)
252                         {
253                                 int key;
254                                 if(pMsgKeyList->GetAt(index-1, key) == E_SUCCESS)
255                                 {
256                                         pDelKeyList->Add(key);
257                                 }
258                         }
259                 }
260         }
261         delete pMsgKeyList;
262         //delete messages
263         if(pDelKeyList->GetCount() > 0)
264         {
265                 __pSettingsPresentor->RemoveRejectMessage(*pDelKeyList);
266         }
267         delete pDelKeyList;
268 }
269
270 void
271 SettingsDeleteListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
272 {
273         // Add your scene activate code here
274         switch (__listOfType)
275         {
276         case LISTOF_NUMBERS:
277         {
278                 //fetch "reject numbers" or "reject msg" list
279                 FetchNumbersRejectList();
280         }
281         break;
282
283         case LISTOF_MESSAGES:
284         {
285                 //fetch "reject msgs" list
286                 FetchRejectMsgsList();
287         }
288         break;
289
290         default:
291                 break;
292         }
293
294         ListView* pDelListView = static_cast<ListView*>(GetControl(IDC_LISTVIEW));
295         if (pDelListView != null)
296         {
297                 pDelListView->UpdateList();
298                 int itemCount = pDelListView->GetItemCount();
299                 for(int itemIndex = itemCount-1; itemIndex >=0 ; itemIndex--)
300                 {
301                         bool isSelected = pDelListView->IsItemChecked(itemIndex);
302                         if(isSelected == true)
303                         {
304                                 pDelListView->SetItemChecked(itemIndex,false);
305                         }
306                 }
307         }
308         __checkedItemCount = 0;
309         Footer* pFooter = GetFooter();
310         if(pFooter != null)
311         {
312                 pFooter->SetItemEnabled(0, false);
313                 pFooter->Invalidate(true);
314         }
315         //to show selected msg or numbers
316         InitializeSelectLabel();
317 }
318
319 void
320 SettingsDeleteListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
321 {
322 }
323
324 void
325 SettingsDeleteListForm::FetchNumbersRejectList(void)
326 {
327         if (__pNumbersList != null)
328         {
329                 delete __pNumbersList;
330                 __pNumbersList = null;
331         }
332         __pNumbersList = static_cast<ArrayListT<CallRejectInfo>*>(__pSettingsPresentor->GetCallRejectList());
333 }
334
335 void
336 SettingsDeleteListForm::FetchRejectMsgsList(void)
337 {
338         if (__pMsgsMap != null)
339         {
340                 delete __pMsgsMap;
341                 __pMsgsMap = null;
342         }
343         //item to be fetched from Db
344         __pMsgsMap = __pSettingsPresentor->GetRejectMessageListN();
345 }
346
347 result
348 SettingsDeleteListForm::InitializeListView(void)
349 {
350         result r = E_FAILURE;
351         ListView* pDelListView = static_cast<ListView*>(GetControl(IDC_LISTVIEW));
352         if (pDelListView != null)
353         {
354                 pDelListView->SetBackgroundColor(this->GetBackgroundColor());
355                 pDelListView->AddListViewItemEventListener(*this);
356                 r = pDelListView->SetItemProvider(*this);
357         }
358         return r;
359 }
360
361 void
362 SettingsDeleteListForm::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
363 {
364         String text;
365         String selStr;
366         switch (index)
367         {
368         case 0://"Select All"
369         {
370                 bool isSelect = (status == LIST_ITEM_STATUS_CHECKED);
371                 SelectAllItems(isSelect);
372         }
373         break;
374
375         default:
376         {
377                 ListView* pDelListView = static_cast<ListView*>(GetControl(IDC_LISTVIEW));
378                 bool isItemUnchecked = (status == LIST_ITEM_STATUS_UNCHECKED);
379                 if(isItemUnchecked == true)
380                 {
381                         __checkedItemCount--;
382                         //un-check "Select All", if it was checked earlier and any other item is unchecked.
383                         if(pDelListView->IsItemChecked(0) == true)
384                         {
385                                 pDelListView->SetItemChecked(0,false);
386                         }
387                 }
388                 else
389                 {
390                         __checkedItemCount++;
391                         if(pDelListView->GetItemCount() == (__checkedItemCount+1))
392                         {
393                                 pDelListView->SetItemChecked(0,true);
394                         }
395                 }
396                 if (__checkedItemCount == 0)
397                 {
398
399                         if(__listOfType == LISTOF_NUMBERS)
400                         {
401                                 selStr = AppUtility::GetResourceString(IDS_SELECT_NUMBER);
402                         }
403                         else if(__listOfType == LISTOF_MESSAGES)
404                         {
405                                 selStr = AppUtility::GetResourceString(IDS_SELECT_MESSAGE);
406                         }
407                         __pSelectedLabel->SetText(selStr);
408                 }
409                 else
410                 {
411                         if(__listOfType == LISTOF_NUMBERS)
412                         {
413                                 //selStr = AppUtility::GetResourceString(IDS_NUMBER_SELECTED);
414                                 if(__checkedItemCount > 1)
415                                 {
416                                         selStr = AppUtility::GetResourceString(IDS_NUMBERS_SELECTED);
417                                 }
418                                 else
419                                 {
420                                         selStr = AppUtility::GetResourceString(IDS_NUMBER_SELECTED);
421                                 }
422                         }
423                         else if(__listOfType == LISTOF_MESSAGES)
424                         {
425                                 if(__checkedItemCount > 1)
426                                 {
427                                         selStr = AppUtility::GetResourceString(IDS_MESSAGES_SELECTED);
428                                 }
429                                 else
430                                 {
431                                         selStr = AppUtility::GetResourceString(IDS_MESSAGE_SELECTED);
432                                 }
433
434                         }
435                         text.Format(100, selStr.GetPointer(), __checkedItemCount);
436                         __pSelectedLabel->SetText(text);
437                 }
438                 pDelListView->Invalidate(true);
439                 __pSelectedLabel->Invalidate(false);
440         }
441         break;
442         }
443
444         Footer* pFooter = GetFooter();
445         if(pFooter != null)
446         {
447                 if(__checkedItemCount > 0)
448                 {
449                         pFooter->SetItemEnabled(0, true);
450                 }
451                 else
452                 {
453                         pFooter->SetItemEnabled(0, false);
454                 }
455                 pFooter->Invalidate(true);
456         }
457 }
458
459 void
460 SettingsDeleteListForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
461 {
462 }
463 void
464 SettingsDeleteListForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
465 {
466 }
467
468 void
469 SettingsDeleteListForm::OnListViewItemLongPressed(ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
470 {
471 }
472
473 void
474 SettingsDeleteListForm::SelectAllItems(bool isSelect)
475 {
476         String text;
477         String selStr;
478         ListView* pDelListView = static_cast<ListView*>(GetControl(IDC_LISTVIEW));
479         int itemCount = pDelListView->GetItemCount();
480         for (int index = 1; index < itemCount; index++)
481         {
482                 pDelListView->SetItemChecked(index,isSelect);
483         }
484         pDelListView->RequestRedraw();
485         if(isSelect)
486         {
487                 __checkedItemCount = pDelListView->GetItemCount()-1;
488                 if(__listOfType == LISTOF_NUMBERS)
489                 {
490                         if(__checkedItemCount > 1)
491                         {
492                                 selStr = AppUtility::GetResourceString(IDS_NUMBERS_SELECTED);
493                         }
494                         else
495                         {
496                                 selStr = AppUtility::GetResourceString(IDS_NUMBER_SELECTED);
497                         }
498
499                 }
500                 else if(__listOfType == LISTOF_MESSAGES)
501                 {
502                         if(__checkedItemCount > 1)
503                         {
504                                 selStr = AppUtility::GetResourceString(IDS_MESSAGES_SELECTED);
505                         }
506                         else
507                         {
508                                 selStr = AppUtility::GetResourceString(IDS_MESSAGE_SELECTED);
509                         }
510                 }
511                 text.Format(100, selStr.GetPointer(), __checkedItemCount);
512                 __pSelectedLabel->SetText(text);
513         }
514         else
515         {
516                 __checkedItemCount = 0;
517                 if(__listOfType == LISTOF_NUMBERS)
518                 {
519                         selStr = AppUtility::GetResourceString(IDS_SELECT_NUMBER);
520                 }
521                 else if(__listOfType == LISTOF_MESSAGES)
522                 {
523                         selStr = AppUtility::GetResourceString(IDS_SELECT_MESSAGE);
524                 }
525                 __pSelectedLabel->SetText(selStr);
526
527         }
528
529         __pSelectedLabel->Invalidate(false);
530         Draw();
531 }
532
533 int
534 SettingsDeleteListForm::GetItemCount(void)
535 {
536         int itemCount = IDI_DEFAULT_LIST_COUNT;
537
538         switch (__listOfType)
539         {
540         case LISTOF_NUMBERS:
541         {
542                 if (__pNumbersList != null)
543                 {
544                         itemCount += __pNumbersList->GetCount();
545                 }
546         }
547         break;
548
549         case LISTOF_MESSAGES:
550         {
551                 if (__pMsgsMap != null)
552                 {
553                         itemCount += __pMsgsMap->GetCount();
554                 }
555         }
556         break;
557
558         default:
559                 break;
560         }
561         return itemCount;
562 }
563
564 ListItemBase*
565 SettingsDeleteListForm::CreateItem(int index, int itemWidth)
566 {
567         CustomItem* pItem = null;
568
569         if (index == 0)
570         {
571                 //Default Item - "SELECT ALL"
572                 pItem = new (std::nothrow) CustomItem();
573                 pItem->Construct(Dimension(itemWidth, H_LIST_NORMAL_MENU_ITEM), LIST_ANNEX_STYLE_MARK);
574
575                 String itemName = AppUtility::GetResourceString(IDS_SELECT_ALL_LIST_ITEM_STRING);
576                 pItem->AddElement(Rectangle(X_DEL_LIST_ELEMENT, Y_DEL_LIST_ELEMENT, W_DELETE_LIST_ITEM, H_LIST_NORMAL_MENU_ITEM), IDI_SELECT_ALL_ITEM_ELEMENT_ID, itemName, false);
577                 pItem->SetElementTextHorizontalAlignment(IDI_LIST_MAIN_TXT_ITEMID, ALIGNMENT_LEFT);
578                 pItem->SetElementTextVerticalAlignment(IDI_LIST_MAIN_TXT_ITEMID, ALIGNMENT_MIDDLE);
579                 pItem->SetBackgroundColor(LIST_ITEM_DRAWING_STATUS_NORMAL, COLOR_SELECTALL_ITEM_BG);
580         }
581         else
582         {
583                 switch (__listOfType)
584                 {
585                 case LISTOF_NUMBERS:
586                         pItem = CreateListItemForNumbers(index, itemWidth);
587                         break;
588
589                 case LISTOF_MESSAGES:
590                         pItem = CreateListItemForMsgs(index, itemWidth);
591                         break;
592
593                 default:
594                         break;
595
596                 }
597         }
598
599         return pItem;
600 }
601
602 CustomItem*
603 SettingsDeleteListForm::CreateListItemForNumbers(int index, int itemWidth)
604 {
605         CustomItem* pItem = null;
606         int listIndex = index - 1;
607
608         if (__pNumbersList != null && listIndex < __pNumbersList->GetCount())
609         {
610                 //fetch number
611                 String number;
612                 CallRejectInfo rejectInfo;
613                 __pNumbersList->GetAt(listIndex, rejectInfo);
614                 number.Append(rejectInfo.phoneNumber);
615                 //create list item
616                 pItem = new (std::nothrow) CustomItem();
617                 pItem->Construct(Dimension(itemWidth, H_LIST_NORMAL_MENU_ITEM), LIST_ANNEX_STYLE_MARK);
618
619                 pItem->AddElement(Rectangle(X_DEL_LIST_ELEMENT, Y_DEL_LIST_ELEMENT, W_DELETE_LIST_ITEM, H_LIST_NORMAL_MENU_ITEM), listIndex, number,
620                                                   FONT_SIZE_MAIN_TXT, COLOR_NORMAL_SUB_TXT, COLOR_PRESS_MAIN_TXT, COLOR_PRESS_MAIN_TXT, false);
621                 pItem->SetElementTextHorizontalAlignment(listIndex, ALIGNMENT_LEFT);
622                 pItem->SetElementTextVerticalAlignment(listIndex, ALIGNMENT_MIDDLE);
623                 pItem->SetBackgroundColor(LIST_ITEM_DRAWING_STATUS_NORMAL, COLOR_NORMAL_ITEM_BG);
624                 pItem->SetBackgroundColor(LIST_ITEM_DRAWING_STATUS_PRESSED, COLOR_PRESS_ITEM_BG);
625         }
626         return pItem;
627 }
628
629 CustomItem*
630 SettingsDeleteListForm::CreateListItemForMsgs(int index, int itemWidth)
631 {
632         CustomItem* pItem = null;
633         int listIndex = index - 1;
634
635         if (__pMsgsMap != null && listIndex < __pMsgsMap->GetCount())
636         {
637                 IListT<String> *pMsgList = __pMsgsMap->GetValuesN();
638                 if(pMsgList != null)
639                 {
640                         //fetch number
641                         String message;
642                         pMsgList->GetAt(listIndex, message);
643
644                         //create list item
645                         pItem = new (std::nothrow) CustomItem();
646                         pItem->Construct(Dimension(itemWidth, H_LIST_NORMAL_MENU_ITEM), LIST_ANNEX_STYLE_MARK);
647
648                         pItem->AddElement(Rectangle(X_DEL_LIST_ELEMENT, Y_DEL_LIST_ELEMENT, W_DELETE_LIST_ITEM, H_LIST_NORMAL_MENU_ITEM), listIndex, message,
649                                                           FONT_SIZE_MAIN_TXT, COLOR_NORMAL_SUB_TXT, COLOR_PRESS_MAIN_TXT, COLOR_PRESS_MAIN_TXT, false);
650                         pItem->SetElementTextHorizontalAlignment(listIndex, ALIGNMENT_LEFT);
651                         pItem->SetElementTextVerticalAlignment(listIndex, ALIGNMENT_MIDDLE);
652                         pItem->SetBackgroundColor(LIST_ITEM_DRAWING_STATUS_NORMAL, COLOR_NORMAL_ITEM_BG);
653                         pItem->SetBackgroundColor(LIST_ITEM_DRAWING_STATUS_PRESSED, COLOR_PRESS_ITEM_BG);
654                         delete pMsgList;
655                 }
656         }
657         return pItem;
658 }
659
660 bool
661 SettingsDeleteListForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
662 {
663         delete pItem;
664         pItem = null;
665         return true;
666 }