Fix for N_SE-49387
[apps/osp/Calendar.git] / src / ClEventDeleterForm.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        ClEventDeleterForm.cpp
19  * @brief       This is the implementation file for the EventDeleterForm class.
20  */
21
22 #include <FApp.h>
23 #include "ClEventDeleterForm.h"
24 #include "ClMainFrame.h"
25 #include "ClResourceManager.h"
26 #include "ClEventListPresentationModel.h"
27 #include "ClTypes.h"
28 #include "ClEventItem.h"
29 #include "ClTwoButtonPopup.h"
30
31 using namespace Tizen;
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Graphics;
37 using namespace Tizen::Social;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Controls;
40 using namespace Tizen::Ui::Scenes;
41 using namespace Tizen::Locales;
42
43 static const int IDA_EVENT_DELETER_FORM_FOOTER_DELETE = 75001;
44 static const int IDA_EVENT_DELETER_FORM_DELETE_COMPLETE = 75009;
45 static const int IDA_SELECT_ALL_CHECKED = 75010;
46 static const int IDA_SELECT_ALL_UNCHECKED = 75011;
47 static const int IDA_BEFORE_TODAY_CHECKED = 75020;
48 static const int IDA_BEFORE_TODAY_UNCHECKED = 75021;
49
50 static const int ID_LIST_ITEM_BEFORE_AFTER = 100;
51
52 static const int H_ITEM = 112;
53 static const int H_GROUP_ITEM = 76;
54 static const int H_COUNT_LABEL = 48;
55 static const int LEFT_MARGIN = 16;
56 static const int TOTAL_MARGIN = 32;
57 static const int FONT_SIZE_EVENT_SUB_LIST = 32;
58 static const int TOP_BOTTOM_GROUP_ITEM_COUNT = 2;
59 static const int TOP_BOTTOM_ITEM_COUNT = 1;
60 static const int H_GROUP_ITEM_MINIMUM = 0;
61 static const int PREVIOUS_DATE = -1;
62 static const int NEXT_DATE = 1;
63
64 static const unsigned int COLOR_GROUP_ITEM_BACKGROUND = Color32<248, 246, 239>::Value;
65 static const unsigned int COLOR_GROUP_ITEM_TODAY_BACKGROUND = Color32<239, 236, 224>::Value;
66 static const unsigned int COLOR_GROUP_ITEM_TEXT = Color32<128, 128, 128>::Value;
67 static const unsigned int COLOR_GROUP_ITEM_TODAY_TEXT = Color32<59, 115, 182>::Value;
68 static const unsigned int COLOR_TOP_BOTTOM_ITEM_TEXT = Color32<42, 137, 194>::Value;
69 static const unsigned int COLOR_ITEM_TODAY_BACKGROUND = Color32<239, 236, 224>::Value;
70 static const unsigned int COLOR_ITEM_TEXT_PRESSED = Color32<255, 255, 255>::Value;
71
72
73 class AsyncDeleter
74         : public Thread
75 {
76 public:
77         result Initialize(EventListPresentationModel* pPm, IList* pList, Control* pTarget);
78
79         virtual Object* Run(void);
80 private:
81         EventListPresentationModel* __pPm;
82         IList* __pList;
83         Control* __pTarget;
84 };
85
86 result
87 AsyncDeleter::Initialize(EventListPresentationModel* pPm, IList* pList, Control* pTarget)
88 {
89         __pPm = pPm;
90         __pList = pList;
91         __pTarget = pTarget;
92         return Construct(DEFAULT_STACK_SIZE, THREAD_PRIORITY_LOW);
93 }
94
95 Object*
96 AsyncDeleter::Run(void)
97 {
98         __pPm->RemoveEvents(*__pList);
99         delete __pList;
100
101         if (__pTarget != null)
102         {
103                 __pTarget->SendUserEvent(IDA_EVENT_DELETER_FORM_DELETE_COMPLETE, null);
104         }
105         return null;
106 }
107
108 EventDeleterForm::EventDeleterForm(void)
109         : __pPm(null)
110         , __pList(null)
111         , __pGroupedListViewDeleteList(null)
112         , __pSelectedEvent(null)
113         , __pCalendarbook(null)
114         , __pDateFormatter(null)
115         , __pTimeFormatter(null)
116         , __pSelectAll(null)
117         , __pSelectBeforeToday(null)
118         , __pPanel(null)
119         , __pLabel(null)
120         , __pTwoButtonPopup(null)
121         , __pThread(null)
122         , __pProgressPopup(null)
123         , __topBottomItemEnabled(false)
124         , __fontSize(44.f)
125         , __itemHeight(112.f)
126 {
127 }
128
129 EventDeleterForm::~EventDeleterForm(void)
130 {
131
132 }
133
134 result
135 EventDeleterForm::Initialize(void)
136 {
137         return Construct(L"IDL_EVENT_DELETER_FORM");
138 }
139
140 result
141 EventDeleterForm::OnInitializing(void)
142 {
143         String fontSizeString;
144         Tizen::System::SettingInfo::GetValue(L"http://tizen.org/setting/font.size", fontSizeString);
145         AppLogDebug("fontSize: %ls", fontSizeString.GetPointer());
146         if (fontSizeString == L"small")
147         {
148                 __fontSize = 36.f;
149                 __itemHeight = 120.f;
150         }
151         else if (fontSizeString == L"medium")
152         {
153                 __fontSize = 44.f;
154                 __itemHeight = 140.f;
155         }
156         else if (fontSizeString == L"large")
157         {
158                 __fontSize = 64.f;
159                 __itemHeight = 157.f;
160         }
161         else if (fontSizeString == L"huge")
162         {
163                 __fontSize = 81.f;
164                 __itemHeight = 176.f;
165         }
166         else if (fontSizeString == L"giant")
167         {
168                 __fontSize = 106.f;
169                 __itemHeight = 205.f;
170         }
171
172         SetFormBackEventListener(this);
173         Tizen::System::SettingInfo::AddSettingEventListener(*this);
174         Footer* pFooter = GetFooter();
175         pFooter->AddActionEventListener(*this);
176
177         __pPanel = dynamic_cast<Panel*>(GetControl(L"IDC_PANEL"));
178         AppAssertf(__pPanel != null, "[E_FAILURE] Unable to get Panel.");
179
180         __pSelectAll = dynamic_cast<CheckButton*>(GetControl(L"IDC_SELECTALL_CHECKBUTTON", true));
181         AppAssertf(__pSelectAll != null, "[E_FAILURE] Unable to get CheckButton.");
182         __pSelectAll->SetActionId(IDA_SELECT_ALL_CHECKED, IDA_SELECT_ALL_UNCHECKED);
183         __pSelectAll->SetEnabled(false);
184         __pSelectAll->AddActionEventListener(*this);
185
186         __pSelectBeforeToday = dynamic_cast<CheckButton*>(GetControl(L"IDC_BEFORETODAY_CHECKBUTTON", true));
187         AppAssertf(__pSelectBeforeToday != null, "[E_FAILURE] Unable to get CheckButton.");
188         __pSelectBeforeToday->SetActionId(IDA_BEFORE_TODAY_CHECKED, IDA_BEFORE_TODAY_UNCHECKED);
189         __pSelectBeforeToday->AddActionEventListener(*this);
190         __pSelectBeforeToday->SetEnabled(false);
191         __pSelectBeforeToday->SetShowState(false);
192         __pPanel->SetSize(Dimension(GetClientAreaBounds().width, H_ITEM));
193
194         __pGroupedListViewDeleteList = dynamic_cast<GroupedListView*>(GetControl(L"IDC_GROUPEDLISTVIEW"));
195         AppAssertf(__pGroupedListViewDeleteList != null, "[E_FAILURE] Unable to get GroupedListView.");
196         __pGroupedListViewDeleteList->SetItemProvider(*this);
197         __pGroupedListViewDeleteList->SetTextOfEmptyList(ResourceManager::GetString(IDS_CLD_BODY_NO_EVENTS));
198         __pGroupedListViewDeleteList->SetBitmapOfEmptyList(ResourceManager::GetBitmapN(IDB_LIST_VIEW_NO_CONTENTS_TEXT));
199         __pGroupedListViewDeleteList->AddGroupedListViewItemEventListener(*this);
200
201         __pLabel = dynamic_cast<Label*>(GetControl(L"IDC_LABEL"));
202         AppAssertf(__pLabel != null, "[E_FAILURE] Unable to get Label.");
203
204         __pPm = EventListPresentationModel::GetInstance();
205
206         __pDateFormatter = ResourceManager::CreateDateFormatterN(DATE_TIME_STYLE_SHORT);
207
208         __pTwoButtonPopup = new (std::nothrow) TwoButtonPopup();
209         __pTwoButtonPopup->Initialize();
210
211         __pSelectedEvent = new (std::nothrow) ArrayList();
212         __pSelectedEvent->Construct();
213
214         return E_SUCCESS;
215 }
216
217 result
218 EventDeleterForm::OnTerminating(void)
219 {
220         if (__pThread)
221         {
222                 __pThread->Join();
223                 delete __pThread;
224         }
225
226         delete __pCalendarbook;
227         delete __pDateFormatter;
228         delete __pTimeFormatter;
229         if (__pTwoButtonPopup)
230         {
231                 __pTwoButtonPopup->Destroy();
232         }
233         if (__pProgressPopup)
234         {
235                 __pProgressPopup->Destroy();
236         }
237         __pPm->RemoveCalendarEventChangedEventListener(*this);
238
239         if (__pSelectedEvent)
240         {
241                 __pSelectedEvent->RemoveAll(true);
242             delete __pSelectedEvent;
243         }
244
245         Tizen::System::SettingInfo::RemoveSettingEventListener(*this);
246         return E_SUCCESS;
247 }
248
249 void
250 EventDeleterForm::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
251 {
252         switch (requestId)
253         {
254         case IDA_EVENT_POPUP_DELETE:
255         {
256                 if (__pThread)
257                 {
258                         __pThread->Join();
259                         delete __pThread;
260                 }
261
262                 if (!__pProgressPopup)
263                 {
264                         __pProgressPopup = new (std::nothrow) ProgressPopup();
265                         __pProgressPopup->Construct(false, false);
266                 }
267                 __pProgressPopup->SetShowState(true);
268                 __pProgressPopup->Show();
269
270                 LinkedList* pDeleteEvents = new (std::nothrow) LinkedList(SingleObjectDeleter);
271                 for (int i = __pGroupedListViewDeleteList->GetGroupCount() - 1; i >= 0; i--)
272                 {
273                         for (int j = __pGroupedListViewDeleteList->GetItemCountAt(i) - 1; j >= 0 ; j--)
274                         {
275                                 if (__pGroupedListViewDeleteList->IsItemChecked(i, j) == true)
276                                 {
277                                         int actualGroupIndex = i;
278                                         if (__topBottomItemEnabled == true)
279                                         {
280                                                 actualGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
281                                         }
282                                         pDeleteEvents->Add(new (std::nothrow) CalEventInstance(*__pPm->GetEventWithWholeIndex(actualGroupIndex, j)));
283                                 }
284                         }
285                 }
286
287                 AsyncDeleter* pAsyncDeleter = new (std::nothrow) AsyncDeleter();
288                 pAsyncDeleter->Initialize(__pPm, pDeleteEvents, this);
289                 pAsyncDeleter->Start();
290                 __pThread = pAsyncDeleter;
291                 break;
292         }
293         case IDA_EVENT_DELETER_FORM_DELETE_COMPLETE:
294                 __pThread->Join();
295                 delete __pThread;
296                 __pThread = null;
297                 __pProgressPopup->SetShowState(false);
298
299                 __pGroupedListViewDeleteList->UpdateList();
300
301                 for (int i = __pGroupedListViewDeleteList->GetGroupCount() - 1; i >= 0; i--)
302                 {
303                         for (int j = __pGroupedListViewDeleteList->GetItemCountAt(i) - 1; j >= 0 ; j--)
304                         {
305                                 __pGroupedListViewDeleteList->SetItemChecked(i, j, false);
306                         }
307                 }
308
309                 __pSelectAll->SetSelected(IsSelectedAllEvent());
310                 __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
311                 UpdateSelectedLabel();
312                 break;
313         default:
314                 break;
315         }
316
317         if (pArgs)
318         {
319                 pArgs->RemoveAll(true);
320                 delete pArgs;
321         }
322 }
323
324 void
325 EventDeleterForm::OnFormBackRequested(Form& source)
326 {
327         SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
328 }
329
330 int
331 EventDeleterForm::GetGroupCount(void)
332 {
333         int groupCount = __pPm->GetWholeDayCount();
334
335         if (groupCount == 1 && __pPm->GetEventWithWholeIndex(0, 0) == null)
336         {
337                 groupCount = 0;
338
339         }
340
341         if (groupCount != 0 && __topBottomItemEnabled == true)
342         {
343                 groupCount += TOP_BOTTOM_GROUP_ITEM_COUNT;
344         }
345         if (groupCount == 0 && __pTwoButtonPopup != NULL)
346         {
347                 __pTwoButtonPopup->SetShowState(false);
348         }
349         return groupCount;
350 }
351
352 int
353 EventDeleterForm::GetItemCount(int groupIndex)
354 {
355         int itemCount = 0;
356         int actualGroupIndex = groupIndex;
357
358         if (__topBottomItemEnabled == true)
359         {
360                 if (groupIndex == 0 || groupIndex == GetGroupCount() - 1)
361                 {
362                         return TOP_BOTTOM_ITEM_COUNT;
363                 }
364                 else
365                 {
366                         actualGroupIndex = groupIndex - (TOP_BOTTOM_GROUP_ITEM_COUNT / 2);
367                 }
368         }
369
370         itemCount = __pPm->GetWholeEventCount(actualGroupIndex);
371
372         return itemCount;
373 }
374
375 GroupItem*
376 EventDeleterForm::CreateGroupItem(int groupIndex, int itemWidth)
377 {
378         GroupItem* pItem = new (std::nothrow) GroupItem();
379
380         DateTime date;
381
382         if (__topBottomItemEnabled == true && (groupIndex == 0 || groupIndex == GetGroupCount() - 1))
383         {
384                 pItem->Construct(Dimension(itemWidth, H_GROUP_ITEM_MINIMUM));
385                 return pItem;
386         }
387
388         String dateString;
389         int actualGroupIndex = groupIndex;
390         int itemCount = 0;
391
392         if (__topBottomItemEnabled == true)
393         {
394                 actualGroupIndex -= 1;
395         }
396
397         date = __pPm->GetDateTimeFromGroupIndex(actualGroupIndex);
398         dateString = __pPm->GetDateString(date);
399         itemCount = __pPm->GetWholeEventCount(actualGroupIndex);
400
401         if (itemCount == 0)//This is Today Index.(event is empty)
402         {
403                 pItem->Construct(Dimension(itemWidth, 0));
404                 return pItem;
405         }
406         else
407         {
408                 pItem->Construct(Dimension(itemWidth, H_GROUP_ITEM));
409         }
410
411         pItem->SetElement(dateString);
412
413         if (EventListPresentationModel::IsToday(date) == true)
414         {
415                 pItem->SetTextColor(Color(COLOR_GROUP_ITEM_TODAY_TEXT));
416         }
417         else
418         {
419                 pItem->SetTextColor(Color(COLOR_GROUP_ITEM_TEXT));
420         }
421
422         return pItem;
423 }
424
425 ListItemBase*
426 EventDeleterForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
427 {
428         if (__topBottomItemEnabled == true)
429         {
430                 if (groupIndex == 0 || groupIndex == GetGroupCount() - 1)
431                 {
432                         CustomItem* pItem = new (std::nothrow) CustomItem();
433
434                         String string;
435                         String dateString;
436                         if (groupIndex == 0)
437                         {
438                                 __pDateFormatter->Format(__pPm->GetViewMinRange(), dateString);
439                                 string = ResourceManager::GetString(IDS_LIST_VIEW_TAP_VIEW_BEFORE);
440                         }
441                         else
442                         {
443                                 __pDateFormatter->Format(__pPm->GetViewMaxRange(), dateString);
444                                 string = ResourceManager::GetString(IDS_LIST_VIEW_TAP_VIEW_AFTER);
445                         }
446
447                         string.Format(string.GetLength() + dateString.GetLength(), string.GetPointer(), dateString.GetPointer());
448
449                         pItem->Construct(Dimension(itemWidth, H_ITEM), LIST_ANNEX_STYLE_NORMAL);
450                         pItem->AddElement(Rectangle(LEFT_MARGIN, 0, itemWidth - TOTAL_MARGIN, H_ITEM)
451                                                                                         , ID_LIST_ITEM_BEFORE_AFTER, string, FONT_SIZE_EVENT_SUB_LIST
452                                                                                         , Color(COLOR_TOP_BOTTOM_ITEM_TEXT), Color(COLOR_ITEM_TEXT_PRESSED), Color(COLOR_ITEM_TEXT_PRESSED));
453
454                         if (groupIndex == 0)
455                         {
456                                 DateTime minDateTime = Calendarbook::GetMinDateTime();
457                                 minDateTime.AddYears(1);
458                                 __pGroupedListViewDeleteList->SetItemEnabled(groupIndex, itemIndex, __pPm->GetViewMinRange() > minDateTime);
459                         }
460                         else
461                         {
462                                 DateTime maxDateTime = Calendarbook::GetMaxDateTime();
463                                 maxDateTime.AddYears(-1);
464                                 __pGroupedListViewDeleteList->SetItemEnabled(groupIndex, itemIndex, __pPm->GetViewMaxRange() < maxDateTime);
465                         }
466                         return pItem;
467                 }
468                 else
469                 {
470                         groupIndex -= TOP_BOTTOM_ITEM_COUNT;
471                 }
472         }
473         __pGroupedListViewDeleteList->SetItemEnabled(groupIndex, itemIndex, true);
474
475         const CalEventInstance* pEvent = __pPm->GetEventWithWholeIndex(groupIndex, itemIndex);
476         TryReturn((pEvent != null), null, "[E_SYSTEM] Unable to get event instance.");
477
478         EventItem* pItem = new (std::nothrow) EventItem();
479         pItem->Initialize(EVENT_ITEM_STYLE_SELECTION, itemWidth, __itemHeight);
480         if (EventListPresentationModel::IsToday(__pPm->GetDateTimeFromGroupIndex(groupIndex)) == true)
481         {
482         }
483         pItem->SetTitle(pEvent->GetSubject());
484         pItem->SetFontSize(__fontSize);
485         pItem->SetCalendarColor(__pPm->GetCalendarColor(pEvent->GetCalendarId()));
486         if (pEvent->IsAllDayEvent() == true)
487         {
488                 pItem->SetAllDayEvent();
489         }
490         else
491         {
492                 DateTime startTime = ResourceManager::ConvertUtcTimeToWallTime(pEvent->GetStartTime());
493                 DateTime endTime = ResourceManager::ConvertUtcTimeToWallTime(pEvent->GetEndTime());
494                 pItem->SetDateRange(startTime, endTime, __pPm->GetDateTimeFromGroupIndex(groupIndex), __pPm->GetTimeFormatter());
495         }
496
497         pItem->SetReminder(pEvent->HasReminder());
498         pItem->SetRepeat(pEvent->IsRecurring());
499         pItem->SetPriority(pEvent->GetPriority());
500         pItem->UpdateElements();
501
502         return pItem;
503 }
504
505 bool
506 EventDeleterForm::DeleteGroupItem(int groupIndex, GroupItem* pItem, int itemWidth)
507 {
508         AppLogDebug("Enter");
509         delete pItem;
510         return true;
511 }
512
513 bool
514 EventDeleterForm::DeleteItem(int groupIndex, int itemIndex, ListItemBase* pItem, int itemWidth)
515 {
516         AppLogDebug("Enter");
517         delete pItem;
518         return true;
519 }
520
521 void
522 EventDeleterForm::OnGroupedListViewItemStateChanged(GroupedListView& listView, int groupIndex, int itemIndex,
523                                                                                                          int elementId, ListItemStatus status)
524 {
525         int actualGroupIndex = groupIndex;
526         if (__topBottomItemEnabled == true)
527         {
528                 DateTime today = EventListPresentationModel::GetToday();
529                 today.SetValue(today.GetYear(), today.GetMonth(), today.GetDay(), 0, 0, 0);
530                 actualGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
531
532                 if (groupIndex == 0)
533                 {
534                         DateTime startDate = __pPm->GetDateTimeFromGroupIndex(0);
535                         DateTime minRange = __pPm->GetViewMinRange();
536                         minRange.AddMonths(PREVIOUS_DATE);
537                         DateTime minDateTime = Calendarbook::GetMinDateTime();
538                         minDateTime.AddYears(1);
539                         if (minRange < minDateTime)
540                         {
541                                 minRange = minDateTime;
542                         }
543
544                         __pPm->SetViewRange(minRange, __pPm->GetViewMaxRange());
545
546                         __pGroupedListViewDeleteList->RefreshList(0, 0, LIST_REFRESH_TYPE_ITEM_MODIFY);
547                         int addedGroupCount = __pPm->GetGroupIndex(startDate);
548                         for (int i = 0; i < addedGroupCount; ++i)
549                         {
550                                 __pGroupedListViewDeleteList->RefreshList(i + TOP_BOTTOM_ITEM_COUNT, -1, LIST_REFRESH_TYPE_ITEM_ADD);
551
552                                 DateTime date = __pPm->GetDateTimeFromGroupIndex(i);
553                                 int itemCount = __pPm->GetWholeEventCount(i);
554                                 for (int j = 0; j < itemCount; ++j)
555                                 {
556                                         if (__pSelectAll->IsSelected() == true || (__pSelectBeforeToday->IsSelected() == true && date < today))
557                                         {
558                                                 __pGroupedListViewDeleteList->SetItemChecked(i + TOP_BOTTOM_ITEM_COUNT, j, true);
559                                         }
560                                 }
561                         }
562
563                         if (__pGroupedListViewDeleteList->IsGroupExpanded(__pGroupedListViewDeleteList->GetGroupCount() - 1) == false)
564                         {
565                                 __pGroupedListViewDeleteList->ExpandGroup(__pGroupedListViewDeleteList->GetGroupCount() - 1);
566                         }
567
568                         __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
569                         UpdateSelectedLabel();
570                         return;
571                 }
572                 else if (groupIndex == __pGroupedListViewDeleteList->GetGroupCount() - 1)
573                 {
574                         int prevGroupCount = __pPm->GetWholeDayCount();
575                         DateTime maxRange = __pPm->GetViewMaxRange();
576                         maxRange.AddMonths(NEXT_DATE);
577                         DateTime maxDateTime = Calendarbook::GetMaxDateTime();
578                         maxDateTime.AddYears(-1);
579                         if (maxRange > maxDateTime)
580                         {
581                                 maxRange = maxDateTime;
582                         }
583
584                         __pPm->SetViewRange(__pPm->GetViewMinRange(), maxRange);
585
586                         int groupCount = __pPm->GetWholeDayCount();
587                         for (int i = prevGroupCount; i < groupCount; ++i)
588                         {
589                                 __pGroupedListViewDeleteList->RefreshList(i + TOP_BOTTOM_ITEM_COUNT, -1, LIST_REFRESH_TYPE_ITEM_ADD);
590
591                                 DateTime date = __pPm->GetDateTimeFromGroupIndex(i);
592                                 int itemCount = __pPm->GetWholeEventCount(i);
593                                 for (int j = 0; j < itemCount; ++j)
594                                 {
595                                         if (__pSelectAll->IsSelected() == true || (__pSelectBeforeToday->IsSelected() == true && date < today))
596                                         {
597                                                 __pGroupedListViewDeleteList->SetItemChecked(i + TOP_BOTTOM_ITEM_COUNT, j, true);
598                                         }
599                                 }
600                         }
601
602                         __pGroupedListViewDeleteList->RefreshList(__pGroupedListViewDeleteList->GetGroupCount() - 1, 0, LIST_REFRESH_TYPE_ITEM_MODIFY);
603                         if (__pGroupedListViewDeleteList->IsGroupExpanded(__pGroupedListViewDeleteList->GetGroupCount() - 1) == false)
604                         {
605                                 __pGroupedListViewDeleteList->ExpandGroup(__pGroupedListViewDeleteList->GetGroupCount() - 1);
606                         }
607
608                         __pGroupedListViewDeleteList->ScrollToItem(__pGroupedListViewDeleteList->GetGroupCount() - 1, 0);
609                         __pGroupedListViewDeleteList->Invalidate(false);
610
611                         __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
612                         UpdateSelectedLabel();
613                         return;
614                 }
615         }
616
617         SetAllSelectedEvents(listView, groupIndex, itemIndex, status);
618
619         if (__pGroupedListViewDeleteList->IsItemChecked(groupIndex, itemIndex) == true)
620         {
621                 __pSelectAll->SetSelected(IsSelectedAllEvent());
622                 __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
623         }
624         else if (__pGroupedListViewDeleteList->IsItemChecked(groupIndex, itemIndex) == false)
625         {
626                 __pSelectAll->SetSelected(false);
627
628                 int actualIndex = (__topBottomItemEnabled == true) ? groupIndex - TOP_BOTTOM_ITEM_COUNT : groupIndex;
629                 if (__pPm->GetCurrentDate() > __pPm->GetDateTimeFromGroupIndex(actualIndex))
630                 {
631                         __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
632                 }
633                 Invalidate(true);
634         }
635         UpdateSelectedLabel();
636 }
637
638 void
639 EventDeleterForm::OnGroupedListViewItemSwept(GroupedListView& listView, int groupIndex, int itemIndex,
640                                                                                          SweepDirection direction)
641 {
642 }
643
644 void
645 EventDeleterForm::OnGroupedListViewContextItemStateChanged(GroupedListView& listView,
646                                                                                                                          int groupIndex, int itemIndex,
647                                                                                                                          int elementId, ListContextItemStatus status)
648 {
649 }
650
651 void
652 EventDeleterForm::OnGroupedListViewItemLongPressed(GroupedListView& listView, int groupIndex, int itemIndex,
653                                                                                                         int elementId, bool& invokeListViewItemCallback)
654 {
655 }
656
657 void
658 EventDeleterForm::OnGroupedListViewGroupItemSelected(GroupedListView& listView, int groupIndex)
659 {
660         if (listView.IsGroupExpanded(groupIndex) == true)
661         {
662                 listView.CollapseGroup(groupIndex);
663         }
664         else
665         {
666                 listView.ExpandGroup(groupIndex);
667         }
668         listView.Invalidate(true);
669 }
670
671 void
672 EventDeleterForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
673 {
674         DateTime minRange = __pPm->GetCurrentDate();
675         minRange.SetValue(minRange.GetYear(), minRange.GetMonth(), minRange.GetDay());
676         DateTime maxRange;
677
678         if (previousSceneId == IDSCN_YEAR)
679         {
680                 minRange.SetValue(minRange.GetYear(), 1, 1);
681                 maxRange = minRange;
682                 maxRange.AddYears(NEXT_DATE);
683                 maxRange.AddSeconds(PREVIOUS_DATE);
684                 __pSelectAll->SetText(ResourceManager::GetString(IDS_CLD_MBODY_ALL_THIS_YEAR));
685         }
686         else if (previousSceneId == IDSCN_MONTH)
687         {
688                 minRange.SetValue(minRange.GetYear(), minRange.GetMonth(), 1);
689                 maxRange = minRange;
690                 maxRange.AddMonths(NEXT_DATE);
691                 maxRange.AddSeconds(PREVIOUS_DATE);
692                 __pSelectAll->SetText(ResourceManager::GetString(IDS_CLD_BODY_ALL_THIS_MONTH));
693         }
694         else if (previousSceneId == IDSCN_DAY)
695         {
696                 maxRange = minRange;
697                 maxRange.AddDays(NEXT_DATE);
698                 maxRange.AddSeconds(PREVIOUS_DATE);
699                 __pSelectAll->SetText(ResourceManager::GetString(IDS_CLD_BODY_ALL_THIS_DAY));
700         }
701         else if (previousSceneId == IDSCN_LIST)
702         {
703                 minRange = __pPm->GetViewMinRange();
704                 maxRange = __pPm->GetViewMaxRange();
705
706                 __topBottomItemEnabled = true;
707
708                 __pSelectBeforeToday->SetShowState(true);
709                 __pPanel->SetSize(Dimension(GetClientAreaBounds().width, H_ITEM * 2));
710         }
711
712         __pPm->SetViewType(VIEW_TYPE_DELETE);
713         __pPm->SetViewRange(minRange, maxRange);
714         __pPm->AddCalendarEventChangedEventListener(*this);
715         UpdateSelectedLabel();
716
717         if (__pSelectedEvent != null)
718         {
719                 __pSelectedEvent->RemoveAll(true);
720         }
721         GetFooter()->SetItemEnabled(0, false);
722         Invalidate(true);
723 }
724
725 void
726 EventDeleterForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
727 {
728
729 }
730
731 void
732 EventDeleterForm::OnSettingChanged(Tizen::Base::String& key)
733 {
734         if (key.Equals(KEY_SYSTEM_DATE,false))
735         {
736                 __pGroupedListViewDeleteList->UpdateList();
737         }
738 }
739
740 void
741 EventDeleterForm::OnActionPerformed(const Control& source, int actionId)
742 {
743         TwoButtonPopupStyle style = TWO_BUTTON_POPUP_STYLE_SINGLE_EVENT;
744
745         DateTime today = EventListPresentationModel::GetToday();
746         today.SetValue(today.GetYear(), today.GetMonth(), today.GetDay(), 0, 0, 0);
747
748         int startGroupIndex = 0;
749         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
750         if (__topBottomItemEnabled == true)
751         {
752                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
753                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
754         }
755
756         switch (actionId)
757         {
758         case IDA_EVENT_DELETER_FORM_FOOTER_DELETE:
759                 for (int i = startGroupIndex; i <= endGroupIndex; i++)
760                 {
761                         int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
762                         for (int j = 0; j < itemCount; j++)
763                         {
764                                 if (__pGroupedListViewDeleteList->IsItemChecked(i, j) == true)
765                                 {
766                                         const CalEventInstance* pEventInstance = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
767                                         if (pEventInstance != null && pEventInstance->IsRecurring() == true)
768                                         {
769                                                 style = TWO_BUTTON_POPUP_STYLE_REPEATED_EVENT;
770                                                 break;
771                                         }
772                                 }
773                         }
774                 }
775
776                 __pTwoButtonPopup->RequestPopup(style, this);
777                 break;
778         case IDA_SELECT_ALL_CHECKED:
779                 for (int i = startGroupIndex; i <= endGroupIndex; i++)
780                 {
781                         int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
782                         for (int j = 0; j < itemCount; j++)
783                         {
784                                 const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
785                                 if (pEventInst != null)
786                                 {
787                                         RecordId id = pEventInst->GetOriginalEventId();
788                                         __pSelectedEvent->Add(new (std::nothrow) Integer(id));
789                                         __pGroupedListViewDeleteList->SetItemChecked(i, j, true);
790                                 }
791                         }
792
793                 }
794                 __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
795                 UpdateSelectedLabel();
796                 break;
797         case IDA_SELECT_ALL_UNCHECKED:
798                 for (int i = startGroupIndex; i <= endGroupIndex; i++)
799                 {
800                         int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
801                         for (int j = 0; j < itemCount; j++)
802                         {
803                                 const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
804                                 if (pEventInst != null)
805                                 {
806                                         RecordId id = pEventInst->GetOriginalEventId();
807                                         __pSelectedEvent->Remove(Integer(id));
808                                         __pGroupedListViewDeleteList->SetItemChecked(i, j, false);
809                                 }
810                         }
811                 }
812                 __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
813                 UpdateSelectedLabel();
814                 break;
815         case IDA_BEFORE_TODAY_CHECKED:
816                 if (__topBottomItemEnabled == true)
817                 {
818                         for (int i = startGroupIndex; i <= endGroupIndex && __pPm->GetDateTimeFromGroupIndex(i - startGroupIndex) < today; i++)
819                         {
820                                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
821                                 for (int j = 0; j < itemCount; j++)
822                                 {
823                                         const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
824                                         if (pEventInst != null)
825                                         {
826                                                 RecordId id = pEventInst->GetOriginalEventId();
827                                                 __pSelectedEvent->Add(new (std::nothrow) Integer(id));
828                                                 __pGroupedListViewDeleteList->SetItemChecked(i, j, true);
829                                         }
830                                 }
831                         }
832                         __pSelectAll->SetSelected(IsSelectedAllEvent());
833                         __pSelectBeforeToday->SetSelected(IsSelectedAllEventBeforeToday());
834                         UpdateSelectedLabel();
835                 }
836                 break;
837         case IDA_BEFORE_TODAY_UNCHECKED:
838                 if (__topBottomItemEnabled == true)
839                 {
840                         for (int i = startGroupIndex; i <= endGroupIndex && __pPm->GetDateTimeFromGroupIndex(i - startGroupIndex) < today; i++)
841                         {
842                                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
843                                 for (int j = 0; j < itemCount; j++)
844                                 {
845                                         const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
846                                         if (pEventInst != null)
847                                         {
848                                                 RecordId id = pEventInst->GetOriginalEventId();
849                                                 __pSelectedEvent->Remove(Integer(id));
850                                                 __pGroupedListViewDeleteList->SetItemChecked(i, j, false);
851                                         }
852                                 }
853                         }
854                         __pSelectAll->SetSelected(IsSelectedAllEvent());
855                         UpdateSelectedLabel();
856                 }
857                 break;
858         }
859 }
860
861 void
862 EventDeleterForm::SetAllSelectedEvents(GroupedListView& listView, int groupIndex, int itemIndex,
863                                                 ListItemStatus status)
864 {
865         int actualGroupIndex = groupIndex;
866         if (__topBottomItemEnabled == true)
867         {
868                 actualGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
869         }
870         const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(actualGroupIndex, itemIndex);
871         if (pEventInst != null)
872         {
873                 RecordId id = pEventInst->GetOriginalEventId();
874                 if (status == LIST_ITEM_STATUS_CHECKED)
875                 {
876                         __pSelectedEvent->Add(new (std::nothrow) Integer(id));
877                 }
878                 else if (status == LIST_ITEM_STATUS_UNCHECKED)
879                 {
880                         __pSelectedEvent->Remove(Integer(id));
881                 }
882         }
883
884 }
885
886 void
887 EventDeleterForm::AutoCheckSameEvents(RecordId id_original, ListItemStatus status)
888 {
889         int startGroupIndex = 0;
890         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
891
892         if (__topBottomItemEnabled == true)
893         {
894                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
895                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
896         }
897
898
899         for (int i = startGroupIndex; i <= endGroupIndex; i++)
900         {
901                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
902                 for (int j = 0; j < itemCount; j++)
903                 {
904                                 const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
905                                 if (pEventInst != null)
906                                 {
907                                         RecordId id = pEventInst->GetOriginalEventId();
908                                         if (id == id_original)
909                                         {
910                                                 __pGroupedListViewDeleteList->SetItemChecked(i, j, (status == LIST_ITEM_STATUS_CHECKED)?true:false);
911                                         }
912                                 }
913                 }
914         }
915 }
916 void
917 EventDeleterForm::OnCalendarEventChanged(void)
918 {
919         __pGroupedListViewDeleteList->UpdateList();
920
921         int startGroupIndex = 0;
922         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
923         if (__topBottomItemEnabled == true)
924         {
925                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
926                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
927         }
928
929         for (int i = startGroupIndex; i <= endGroupIndex; i++)
930         {
931                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
932                 for (int j = 0; j < itemCount; j++)
933                 {
934                         __pGroupedListViewDeleteList->SetItemChecked(i, j,false);
935                 }
936         }
937
938         for (int i = startGroupIndex; i <= endGroupIndex; i++)
939         {
940                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
941                 for (int j = 0; j < itemCount; j++)
942                 {
943                         const CalEventInstance* pEventInst = __pPm->GetEventWithWholeIndex(i - startGroupIndex, j);
944                         if (pEventInst != null)
945                         {
946                                 RecordId id = pEventInst->GetOriginalEventId();
947                                 if (__pSelectedEvent->Contains(Integer(id)))
948                                 {
949                                         __pGroupedListViewDeleteList->SetItemChecked(i, j, true);
950                                 }
951                         }
952                 }
953         }
954
955     if (IsSelectedAllEvent())
956     {
957         __pSelectAll->SetSelected(true);
958     }
959     else
960     {
961         __pSelectAll->SetSelected(false);
962     }
963     if (IsSelectedAllEventBeforeToday())
964     {
965         __pSelectBeforeToday->SetSelected(true);
966     }
967     else
968     {
969         __pSelectBeforeToday->SetSelected(false);
970     }
971
972         UpdateSelectedLabel();
973
974         if (!IsSelectedAnyEvent() && __pTwoButtonPopup != null && __pTwoButtonPopup->GetShowState() == true)
975         {
976                 __pTwoButtonPopup->SetShowState(false);
977         }
978
979 }
980
981 int
982 EventDeleterForm::GetSelectedEventCount(void)
983 {
984         int startGroupIndex = 0;
985         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
986         if (__topBottomItemEnabled == true)
987         {
988                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
989                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
990         }
991
992         if (endGroupIndex < 0)
993         {
994                 return 0;
995         }
996
997         int count = 0;
998         for (int i = startGroupIndex; i <= endGroupIndex; i++)
999         {
1000                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
1001                 for (int j = 0; j < itemCount; j++)
1002                 {
1003                         if (__pGroupedListViewDeleteList->IsItemChecked(i, j) == true)
1004                         {
1005                                 count++;
1006                         }
1007                 }
1008         }
1009         return count;
1010 }
1011
1012 bool
1013 EventDeleterForm::IsSelectedAllEvent(void)
1014 {
1015         int startGroupIndex = 0;
1016         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
1017         if (__topBottomItemEnabled == true)
1018         {
1019                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
1020                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
1021         }
1022
1023         if (endGroupIndex < 0)
1024         {
1025                 return false;
1026         }
1027
1028         for (int i = startGroupIndex; i <= endGroupIndex; i++)
1029         {
1030                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
1031                 for (int j = 0; j < itemCount; j++)
1032                 {
1033                         if (__pGroupedListViewDeleteList->IsItemChecked(i, j) == false)
1034                         {
1035                                 return false;
1036                         }
1037                 }
1038         }
1039         return true;
1040 }
1041
1042 bool
1043 EventDeleterForm::IsSelectedAnyEvent(void)
1044 {
1045         int startGroupIndex = 0;
1046         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
1047         if (__topBottomItemEnabled == true)
1048         {
1049                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
1050                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
1051         }
1052
1053         if (endGroupIndex < 0)
1054         {
1055                 return false;
1056         }
1057
1058         for (int i = startGroupIndex; i <= endGroupIndex; i++)
1059         {
1060                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
1061                 for (int j = 0; j < itemCount; j++)
1062                 {
1063                         if (__pGroupedListViewDeleteList->IsItemChecked(i, j) == true)
1064                         {
1065                                 return true;
1066                         }
1067                 }
1068         }
1069         return false;
1070 }
1071 bool
1072 EventDeleterForm::IsSelectedAllEventBeforeToday(void)
1073 {
1074         DateTime today = EventListPresentationModel::GetToday();
1075         today.SetValue(today.GetYear(), today.GetMonth(), today.GetDay());
1076
1077         if (__pPm->GetDateTimeFromGroupIndex(0) >= today)
1078         {
1079                 return false;
1080         }
1081
1082         int startGroupIndex = 0;
1083         int endGroupIndex = __pGroupedListViewDeleteList->GetGroupCount() - 1;
1084         if (__topBottomItemEnabled == true)
1085         {
1086                 startGroupIndex = TOP_BOTTOM_ITEM_COUNT;
1087                 endGroupIndex -= TOP_BOTTOM_ITEM_COUNT;
1088         }
1089
1090         if (endGroupIndex < 0)
1091         {
1092                 return false;
1093         }
1094
1095         for (int i = startGroupIndex; i <= endGroupIndex && __pPm->GetDateTimeFromGroupIndex(i - startGroupIndex) < today; i++)
1096         {
1097                 int itemCount = __pGroupedListViewDeleteList->GetItemCountAt(i);
1098                 for (int j = 0; j < itemCount; j++)
1099                 {
1100                         if (__pGroupedListViewDeleteList->IsItemChecked(i, j) == false)
1101                         {
1102                                 return false;
1103                         }
1104                 }
1105         }
1106         return true;
1107 }
1108
1109 result
1110 EventDeleterForm::UpdateSelectedLabel(void)
1111 {
1112         int itemCount = GetSelectedEventCount();
1113         int groupCount = GetGroupCount();
1114         if (groupCount == 0 || (groupCount == 1 && __pPm->GetEventWithWholeIndex(0, 0) == null))
1115         {
1116                 AppLogDebug("Select button enable.");
1117                 __pSelectAll->SetEnabled(false);
1118                 __pSelectBeforeToday->SetEnabled(false);
1119         }
1120         else
1121         {
1122                 AppLogDebug("Select button disable.");
1123                 __pSelectAll->SetEnabled(true);
1124                 __pSelectBeforeToday->SetEnabled(true);
1125         }
1126
1127         if (itemCount > 0)
1128         {
1129                 GetFooter()->SetItemEnabled(0, true);
1130
1131                 String labelString = ResourceManager::GetString(itemCount > 1 ? IDS_BR_POP_PD_ITEMS_SELECTED : IDS_BR_BODY_PD_ITEM_SELECTED);
1132                 labelString.Format(labelString.GetLength() + Integer::ToString(itemCount).GetLength(), labelString.GetPointer(), itemCount);
1133                 __pLabel->SetText(labelString);
1134                 __pLabel->SetSize(GetClientAreaBounds().width, H_COUNT_LABEL);
1135         }
1136         else
1137         {
1138                 GetFooter()->SetItemEnabled(0, false);
1139
1140                 __pLabel->SetSize(GetClientAreaBounds().width, 0);
1141         }
1142         Invalidate(true);
1143         return E_SUCCESS;
1144 }