Fix for N_SE-49387
[apps/osp/Calendar.git] / src / ClEventSearcherForm.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        ClEventSearcherForm.cpp
19  * @brief       This is the implementation file for the EventSearcherForm class.
20  */
21
22 #include <FGraphics.h>
23 #include <FSocial.h>
24 #include <FUi.h>
25 #include "ClEventItem.h"
26 #include "ClEventSearcherForm.h"
27 #include "ClEventSearchPresentationModel.h"
28 #include "ClResourceManager.h"
29 #include "ClSharePopup.h"
30 #include "ClThreeButtonPopup.h"
31 #include "ClTwoButtonPopup.h"
32 #include "ClTypes.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
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
42 static const RecordId ID_DEFAULT_BIRTHDAY_CALENDAR = 3;
43
44 static const int IDA_EVENT_SEARCHER_CONTEXT_MENU_EDIT = 50101;
45 static const int IDA_EVENT_SEARCHER_CONTEXT_MENU_SHARE = 50102;
46 static const int IDA_EVENT_SEARCHER_CONTEXT_MENU_DELETE = 50103;
47
48 static const int GROUP_ITEM_HEIGHT = 76;
49 static const unsigned int COLOR_GROUP_ITEM_BACKGROUND = Color32<248, 246, 239>::Value;
50 static const unsigned int COLOR_GROUP_ITEM_TODAY_BACKGROUND = Color32<239, 236, 224>::Value;
51 static const unsigned int COLOR_GROUP_ITEM_TEXT = Color32<128, 128, 128>::Value;
52 static const unsigned int COLOR_GROUP_ITEM_TODAY_TEXT = Color32<59, 115, 182>::Value;
53 static const unsigned int COLOR_ITEM_TODAY_BACKGROUND = Color32<239, 236, 224>::Value;
54 static const unsigned int COLOR_ITEM_DELETE_TEXT = Color32<255, 255, 255>::Value;
55 static const unsigned int COLOR_ITEM_DELETE_BACKGROUND = Color32<208, 82, 82>::Value;
56 static const unsigned int COLOR_ITEM_DELETE_PRESSED_BACKGROUND = Color32<194, 71, 71>::Value;
57
58
59 EventSearcherForm::EventSearcherForm(void)
60         : __pPm(null)
61         , __pSearchBarInput(null)
62         , __pGroupedListViewEvents(null)
63         , __pContextItem(null)
64         , __pContextItemNoEdit(null)
65         , __pSharePopup(null)
66         , __pThreeButtonPopup(null)
67         , __pTwoButtonPopup(null)
68         , __fontSize(44.f)
69         , __itemHeight(112.f)
70 {
71 }
72
73 EventSearcherForm::~EventSearcherForm(void)
74 {
75 }
76
77 result
78 EventSearcherForm::Initialize(void)
79 {
80         return Construct(L"IDL_EVENT_SEARCHER_FORM");
81 }
82
83 result
84 EventSearcherForm::OnInitializing(void)
85 {
86         String fontSizeString;
87         Tizen::System::SettingInfo::GetValue(L"http://tizen.org/setting/font.size", fontSizeString);
88
89         AppLogDebug("fontSize: %ls", fontSizeString.GetPointer());
90         if (fontSizeString == L"small")
91         {
92                 __fontSize = 36.f;
93                 __itemHeight = 120.f;
94         }
95         else if (fontSizeString == L"medium")
96         {
97                 __fontSize = 44.f;
98                 __itemHeight = 140.f;
99         }
100         else if (fontSizeString == L"large")
101         {
102                 __fontSize = 64.f;
103                 __itemHeight = 157.f;
104         }
105         else if (fontSizeString == L"huge")
106         {
107                 __fontSize = 81.f;
108                 __itemHeight = 176.f;
109         }
110         else if (fontSizeString == L"giant")
111         {
112                 __fontSize = 106.f;
113                 __itemHeight = 205.f;
114         }
115         __pPm = new (std::nothrow) EventSearchPresentationModel();
116         __pPm->Construct();
117
118         __pSearchBarInput = dynamic_cast<SearchBar*>(GetControl(L"IDC_SEARCHBAR"));
119         TryReturn(__pSearchBarInput != null, E_FAILURE, "[E_FAILURE] Unable to get searchbar.");
120
121         __pGroupedListViewEvents = dynamic_cast<GroupedListView*>(GetControl(L"IDC_GROUPEDLISTVIEW"));
122         TryReturn(__pGroupedListViewEvents != null, E_FAILURE, "[E_FAILURE] Unable to get groupedlistview.");
123
124         __pSearchBarInput->AddKeypadEventListener(*this);
125         __pSearchBarInput->AddActionEventListener(*this);
126         __pSearchBarInput->AddTextEventListener(*this);
127         __pSearchBarInput->SetContentAreaVisible(false);
128         __pSearchBarInput->SetMode(SEARCH_BAR_MODE_INPUT);
129         __pGroupedListViewEvents->SetItemProvider(*this);
130         __pGroupedListViewEvents->AddGroupedListViewItemEventListener(*this);
131
132         SetFormBackEventListener(this);
133         Tizen::System::SettingInfo::AddSettingEventListener(*this);
134
135         __pSharePopup = new (std::nothrow) SharePopup();
136         __pSharePopup->Initialize();
137
138         __pThreeButtonPopup = new (std::nothrow) ThreeButtonPopup();
139         __pThreeButtonPopup->Initialize();
140
141         __pTwoButtonPopup = new (std::nothrow) TwoButtonPopup();
142         __pTwoButtonPopup->Initialize();
143
144         __pContextItem = new (std::nothrow) ListContextItem();
145         __pContextItem->Construct();
146         __pContextItem->AddElement(IDA_EVENT_SEARCHER_CONTEXT_MENU_EDIT, ResourceManager::GetString(IDS_CLD_SK3_EDIT), true);
147         __pContextItem->AddElement(IDA_EVENT_SEARCHER_CONTEXT_MENU_SHARE, ResourceManager::GetString(IDS_COM_SK4_SHARE), true);
148         __pContextItem->AddElement(IDA_EVENT_SEARCHER_CONTEXT_MENU_DELETE, ResourceManager::GetString(IDS_CLD_SK3_DELETE), true);
149         __pContextItem->SetElementBackgroundColor(IDA_EVENT_SEARCHER_CONTEXT_MENU_DELETE, LIST_CONTEXT_ITEM_ELEMENT_STATUS_NORMAL, Color(COLOR_ITEM_DELETE_BACKGROUND));
150         __pContextItem->SetElementBackgroundColor(IDA_EVENT_SEARCHER_CONTEXT_MENU_DELETE, LIST_CONTEXT_ITEM_ELEMENT_STATUS_PRESSED, Color(COLOR_ITEM_DELETE_PRESSED_BACKGROUND));
151         __pContextItem->SetElementTextColor(IDA_EVENT_SEARCHER_CONTEXT_MENU_DELETE, LIST_CONTEXT_ITEM_ELEMENT_STATUS_NORMAL, Color(COLOR_ITEM_DELETE_TEXT));
152
153         __pContextItemNoEdit = new (std::nothrow) ListContextItem();
154         __pContextItemNoEdit->Construct();
155         __pContextItemNoEdit->AddElement(IDA_EVENT_SEARCHER_CONTEXT_MENU_SHARE, ResourceManager::GetString(IDS_COM_SK4_SHARE), true);
156
157         return E_SUCCESS;
158 }
159
160 result
161 EventSearcherForm::OnTerminating(void)
162 {
163         delete __pPm;
164         delete __pContextItem;
165         delete __pContextItemNoEdit;
166         if (__pSharePopup)
167         {
168                 __pSharePopup->Destroy();
169         }
170         if (__pThreeButtonPopup)
171         {
172                 __pThreeButtonPopup->Destroy();
173         }
174         if (__pTwoButtonPopup)
175         {
176                 __pTwoButtonPopup->Destroy();
177         }
178         Tizen::System::SettingInfo::RemoveSettingEventListener(*this);
179         return E_SUCCESS;
180 }
181
182 void
183 EventSearcherForm::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
184 {
185         switch (requestId)
186         {
187         case IDA_EVENT_POPUP_DELETE:
188                 __pPm->RemoveEvent(*static_cast<const CalEventInstance*>(pArgs->GetAt(0)));
189                 break;
190         case IDA_EVENT_POPUP_ALL_REPETITIVE_EVENT_EDIT:
191                 pArgs->Add(new (std::nothrow) Boolean(true));
192                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_EVENT_EDITOR,
193                                                                                                                                   SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pArgs);
194                 pArgs = null;
195                 break;
196         default:
197                 AppLogException("[%d] invalid requestId", requestId);
198                 break;
199         }
200
201         if (pArgs)
202         {
203                 pArgs->RemoveAll(true);
204                 delete pArgs;
205         }
206 }
207
208 void
209 EventSearcherForm::OnActionPerformed(const Control& source, int actionId)
210 {
211         if (actionId == __pSearchBarInput->GetButtonActionId())
212         {
213                 __pSearchBarInput->SetMode(SEARCH_BAR_MODE_NORMAL);
214                 __pSearchBarInput->Invalidate(false);
215                 if (__pPm->GetSearchText() != __pSearchBarInput->GetText())
216                 {
217                         __pPm->SearchEvent(__pSearchBarInput->GetText());
218                         __pGroupedListViewEvents->UpdateList();
219                 }
220         }
221 }
222
223 void
224 EventSearcherForm::OnTextValueChanged(const Control& source)
225 {
226         if (__pPm->GetSearchText() != __pSearchBarInput->GetText())
227         {
228                 __pPm->SearchEvent(__pSearchBarInput->GetText());
229                 __pGroupedListViewEvents->UpdateList();
230         }
231 }
232
233 void
234 EventSearcherForm::OnTextValueChangeCanceled(const Control& source)
235 {
236         AppLogDebug("Enter");
237         if (__pPm->GetSearchText() != __pSearchBarInput->GetText())
238         {
239                 __pPm->SearchEvent(__pSearchBarInput->GetText());
240                 __pGroupedListViewEvents->UpdateList();
241         }
242 }
243
244 void
245 EventSearcherForm::OnFormBackRequested(Form& source)
246 {
247         SetFocus();
248         SceneManager::GetInstance()->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
249 }
250
251 int
252 EventSearcherForm::GetGroupCount(void)
253 {
254         return __pPm->GetWholeDayCount();
255 }
256
257 int
258 EventSearcherForm::GetItemCount(int groupIndex)
259 {
260         return __pPm->GetWholeEventCount(groupIndex);
261 }
262
263 GroupItem*
264 EventSearcherForm::CreateGroupItem(int groupIndex, int itemWidth)
265 {
266         GroupItem* pItem = new (std::nothrow) GroupItem();
267         pItem->Construct(Dimension(itemWidth, GROUP_ITEM_HEIGHT));
268
269         DateTime date = __pPm->GetDateTimeFromGroupIndex(groupIndex);
270         pItem->SetElement(__pPm->GetDateString(date));
271
272         if (EventSearchPresentationModel::IsToday(date) == true)
273         {
274                 pItem->SetTextColor(Color(COLOR_GROUP_ITEM_TODAY_TEXT));
275         }
276         else
277         {
278                 pItem->SetTextColor(Color(COLOR_GROUP_ITEM_TEXT));
279         }
280
281         return pItem;
282 }
283
284 bool
285 EventSearcherForm::DeleteGroupItem(int groupIndex, GroupItem* pItem, int itemWidth)
286 {
287         delete pItem;
288         return true;
289 }
290
291 ListItemBase*
292 EventSearcherForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
293 {
294         const CalEvent* pEvent = __pPm->GetEvent(groupIndex, itemIndex);
295         if (pEvent == null)
296         {
297                 return null;
298         }
299
300         DateTime startWallTime = ResourceManager::ConvertUtcTimeToWallTime(pEvent->GetStartTime());
301         DateTime endWallTime = ResourceManager::ConvertUtcTimeToWallTime(pEvent->GetEndTime());
302
303         EventItem* pItem = new (std::nothrow) EventItem();
304         pItem->Initialize(EVENT_ITEM_STYLE_NORMAL, itemWidth, __itemHeight);
305         pItem->SetTitle(pEvent->GetSubject());
306         pItem->SetFontSize(__fontSize);
307         pItem->SetLocation(pEvent->GetLocation());
308         pItem->SetSearchText(__pPm->GetSearchText());
309         pItem->SetCalendarColor(__pPm->GetCalendarColor(pEvent->GetCalendarId()));
310         if (pEvent->IsAllDayEvent() == true)
311         {
312                 pItem->SetAllDayEvent();
313         }
314         else
315         {
316                 pItem->SetDateRange(startWallTime, endWallTime, __pPm->GetDateTimeFromGroupIndex(groupIndex), __pPm->GetTimeFormatter());
317         }
318
319         pItem->SetReminder(pEvent->GetAllReminders().GetCount() > 0);
320         pItem->SetRepeat(pEvent->IsRecurring());
321         pItem->SetPriority(pEvent->GetPriority());
322
323         pItem->SetContextItem((pEvent->GetCalendarId() == ID_DEFAULT_BIRTHDAY_CALENDAR) ? __pContextItemNoEdit : __pContextItem);
324
325         pItem->UpdateElements();
326         return pItem;
327 }
328
329 bool
330 EventSearcherForm::DeleteItem(int groupIndex, int itemIndex, ListItemBase* pItem, int itemWidth)
331 {
332         delete pItem;
333         return true;
334 }
335
336 void
337 EventSearcherForm::OnGroupedListViewItemStateChanged(GroupedListView& listView, int groupIndex, int itemIndex,
338                                                                                                          int elementId, ListItemStatus status)
339 {
340         SetFocus();
341         if (status == LIST_ITEM_STATUS_SELECTED)
342         {
343                 LinkedList* pList = new (std::nothrow) LinkedList(SingleObjectDeleter);
344                 pList->Add(__pPm->GetEventInstanceN(groupIndex, itemIndex));
345                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_EVENT_DETAILS,
346                                                                                                                                                   SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pList);
347         }
348 }
349
350 void
351 EventSearcherForm::OnGroupedListViewItemSwept(GroupedListView& listView, int groupIndex, int itemIndex, SweepDirection direction)
352 {
353 }
354
355 void
356 EventSearcherForm::OnGroupedListViewContextItemStateChanged(GroupedListView& listView, int groupIndex, int itemIndex,
357                                                                                                                         int elementId, ListContextItemStatus status)
358 {
359         SetFocus();
360         CalEventInstance* pEventInstance = __pPm->GetEventInstanceN(groupIndex, itemIndex);
361         if (pEventInstance && status == LIST_CONTEXT_ITEM_STATUS_SELECTED)
362         {
363                 switch (elementId)
364                 {
365                 case IDA_EVENT_SEARCHER_CONTEXT_MENU_EDIT:
366                 {
367                         LinkedList* pList = new (std::nothrow) LinkedList(SingleObjectDeleter);
368                         pList->Add(new (std::nothrow) CalEventInstance(*pEventInstance));
369                         pList->Add(new (std::nothrow) Boolean(true));
370                         SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_EVENT_EDITOR,
371                                                                                                                                                   SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pList);
372                         break;
373                 }
374                 case IDA_EVENT_SEARCHER_CONTEXT_MENU_SHARE:
375                         __pSharePopup->RequestShare(pEventInstance->GetOriginalEventId());
376                         break;
377                 case IDA_EVENT_SEARCHER_CONTEXT_MENU_DELETE:
378                         if (__pPm->IsEditableAllEvent(*pEventInstance))
379                         {
380                                 __pTwoButtonPopup->RequestPopup(TWO_BUTTON_POPUP_STYLE_REPEATED_EVENT, this, pEventInstance);
381                         }
382                         else
383                         {
384                                 __pTwoButtonPopup->RequestPopup(TWO_BUTTON_POPUP_STYLE_SINGLE_EVENT, this, pEventInstance);
385                         }
386                         break;
387                 }
388         }
389         delete pEventInstance;
390 }
391
392 void
393 EventSearcherForm::OnGroupedListViewItemLongPressed(GroupedListView& listView, int groupIndex, int itemIndex,
394                                                                                                         int elementId, bool& invokeListViewItemCallback)
395 {
396 }
397
398 void
399 EventSearcherForm::OnGroupedListViewGroupItemSelected(GroupedListView& listView, int groupIndex)
400 {
401         if (__pGroupedListViewEvents->IsGroupExpanded(groupIndex) == true)
402         {
403                 __pGroupedListViewEvents->CollapseGroup(groupIndex);
404         }
405         else
406         {
407                 __pGroupedListViewEvents->ExpandGroup(groupIndex);
408         }
409 }
410
411 void
412 EventSearcherForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
413 {
414         __pGroupedListViewEvents->UpdateList();
415         __pPm->AddCalendarEventChangedEventListener(*this);
416 }
417
418 void
419 EventSearcherForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
420 {
421         __pPm->RemoveCalendarEventChangedEventListener(*this);
422 }
423
424 void
425 EventSearcherForm::OnSettingChanged(Tizen::Base::String& key)
426 {
427         if (key.Equals(KEY_SYSTEM_DATE,false))
428         {
429                 __pGroupedListViewEvents->UpdateList();
430         }
431 }
432
433 void
434 EventSearcherForm::OnKeypadWillOpen(Control& source)
435 {
436 }
437
438 void
439 EventSearcherForm::OnKeypadOpened(Control& source)
440 {
441 }
442
443 void
444 EventSearcherForm::OnKeypadClosed(Control& source)
445 {
446 }
447
448 void
449 EventSearcherForm::OnKeypadActionPerformed(Control& source, KeypadAction keypadAction)
450 {
451         AppLogDebug("Enter");
452         if (keypadAction == KEYPAD_ACTION_SEARCH)
453         {
454                 if (__pPm->GetSearchText() != __pSearchBarInput->GetText())
455                 {
456                         __pPm->SearchEvent(__pSearchBarInput->GetText());
457                         __pGroupedListViewEvents->UpdateList();
458                 }
459                 __pSearchBarInput->HideKeypad();
460         }
461 }
462
463 void
464 EventSearcherForm::OnCalendarEventChanged(void)
465 {
466         __pGroupedListViewEvents->UpdateList();
467 }