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