13483f3e58dcee5b75ff08800b94ba70fe09d011
[apps/osp/Calendar.git] / src / ClEventSelectorForm.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        ClEventSelectorForm.cpp
19  * @brief       This is the implementation file for the EventSelectorForm class.
20  */
21
22 #include "ClCalendarApp.h"
23 #include "ClEventItem.h"
24 #include "ClEventSelectorForm.h"
25 #include "ClMainFrame.h"
26 #include "ClResourceManager.h"
27 #include "ClTypes.h"
28
29 using namespace Tizen;
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Social;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Controls;
37 using namespace Tizen::Ui::Scenes;
38
39 const int IDA_EVENT_SELECTOR_FORM_FOOTER_DONE = 70001;
40
41 EventSelectorForm::EventSelectorForm(void)
42         : __selectionType(EVENT_SELECTOR_FORM_SELECTION_TYPE_SINGLE)
43         , __pEvents(null)
44         , __pListViewContents(null)
45         , __pCalendarbook(null)
46         , __pTimeFormatter(null)
47 {
48 }
49
50 EventSelectorForm::~EventSelectorForm(void)
51 {
52 }
53
54 result
55 EventSelectorForm::Initialize(void)
56 {
57         return Construct(L"IDL_EVENT_SELECTOR_FORM");
58 }
59
60 result
61 EventSelectorForm::OnInitializing(void)
62 {
63         SetFormBackEventListener(this);
64         Footer* pFooter = GetFooter();
65         pFooter->AddActionEventListener(*this);
66
67         Calendarbook* pCalendarbook = new (std::nothrow) Calendarbook();
68         pCalendarbook->Construct(*this);
69
70         CalendarbookFilter calendarFilter(CB_FI_TYPE_CALENDAR);
71         calendarFilter.AppendInt(FI_CONJ_OP_NONE, CALENDAR_FI_PR_ITEM_TYPE, FI_CMP_OP_EQUAL, CALENDAR_ITEM_TYPE_EVENT_ONLY);
72         IList* pAllCalendars = pCalendarbook->SearchN(calendarFilter);
73         __allCalendars.Construct(pAllCalendars->GetCount());
74         IEnumerator* pEnum = pAllCalendars->GetEnumeratorN();
75         while (pEnum->MoveNext() == E_SUCCESS)
76         {
77                 Calendar* pCalendar = static_cast<Calendar*>(pEnum->GetCurrent());
78                 __allCalendars.Add(new (std::nothrow) Integer(pCalendar->GetRecordId()), pCalendar);
79         }
80         delete pEnum;
81         pAllCalendars->RemoveAll(false);
82         delete pAllCalendars;
83
84         __pDateFormatter = ResourceManager::CreateDateFormatterN(Locales::DATE_TIME_STYLE_SHORT);
85         __pTimeFormatter = ResourceManager::CreateTimeFormatterN();
86
87         __pListViewContents = dynamic_cast<ListView*>(GetControl(L"IDC_LISTVIEW"));
88         TryCatch(__pListViewContents != null, , "[E_FAILURE] Unable to get listview.");
89
90         __pListViewContents->SetItemProvider(*this);
91         __pListViewContents->AddListViewItemEventListener(*this);
92         __pCalendarbook = pCalendarbook;
93
94         return E_SUCCESS;
95 CATCH:
96         delete pCalendarbook;
97         return E_FAILURE;
98 }
99
100 result
101 EventSelectorForm::OnTerminating(void)
102 {
103         __allCalendars.RemoveAll(true);
104
105         if (__pEvents != null)
106         {
107                 __pEvents->RemoveAll(true);
108                 delete __pEvents;
109         }
110
111         delete __pCalendarbook;
112         delete __pDateFormatter;
113         delete __pTimeFormatter;
114         return E_SUCCESS;
115 }
116
117 void
118 EventSelectorForm::OnFormBackRequested(Form& source)
119 {
120         result r = SendAppControlResult(APP_CTRL_RESULT_CANCELED);
121         AppLogDebugIf(r != E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
122
123         UiApp::GetInstance()->Terminate();
124 }
125
126 int
127 EventSelectorForm::GetItemCount(void)
128 {
129         if (__pEvents == null)
130         {
131                 return 0;
132         }
133         return __pEvents->GetCount();
134 }
135
136 ListItemBase*
137 EventSelectorForm::CreateItem(int index, int itemWidth)
138 {
139         if (__pEvents == null)
140         {
141                 return null;
142         }
143
144         CalEvent* pEvent = static_cast<CalEvent*>(__pEvents->GetAt(index));
145         EventItem* pItem = new (std::nothrow) EventItem();
146         pItem->Initialize((__selectionType == EVENT_SELECTOR_FORM_SELECTION_TYPE_SINGLE) ? EVENT_ITEM_STYLE_NORMAL : EVENT_ITEM_STYLE_SELECTION, itemWidth);
147         pItem->SetTitle(pEvent->GetSubject());
148         pItem->SetCalendarColor(GetCalendarColor(pEvent->GetCalendarId()));
149         pItem->SetLocation(pEvent->GetLocation());
150         DateTime startTime = ResourceManager::ConvertUtcTimeToWallTime(pEvent->GetStartTime());
151         DateTime endTime = ResourceManager::ConvertUtcTimeToWallTime(pEvent->GetEndTime());
152         pItem->SetDateRangeText(GetDateRangeText(startTime, endTime, pEvent->IsAllDayEvent()));
153         pItem->SetReminder(pEvent->GetAllReminders().GetCount() > 0);
154         pItem->SetRepeat(pEvent->IsRecurring());
155         pItem->SetPriority(pEvent->GetPriority());
156 //      pItem->SetFacebookEvent(false);
157         pItem->UpdateElements();
158
159         return pItem;
160 }
161
162 bool
163 EventSelectorForm::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
164 {
165         delete pItem;
166         return true;
167 }
168
169 void
170 EventSelectorForm::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
171 {
172         if (status == LIST_ITEM_STATUS_SELECTED && __selectionType == EVENT_SELECTOR_FORM_SELECTION_TYPE_SINGLE)
173         {
174                 result r = E_SUCCESS;
175                 LinkedList* pArgs = new (std::nothrow) LinkedList();
176                 pArgs->Add(__pEvents->GetAt(index));
177                 r = SendAppControlResult(APP_CTRL_RESULT_SUCCEEDED, pArgs);
178                 pArgs->RemoveAll(false);
179                 delete pArgs;
180                 TryCatch(r == E_SUCCESS, , "[%s] Unable to return result.", GetErrorMessage(r));
181
182                 UiApp::GetInstance()->Terminate();
183                 return;
184
185 CATCH:
186                 SendAppControlResult(APP_CTRL_RESULT_FAILED);
187                 UiApp::GetInstance()->Terminate();
188         }
189 }
190
191 void
192 EventSelectorForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
193 {
194
195 }
196
197 void
198 EventSelectorForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus status)
199 {
200
201 }
202
203 void
204 EventSelectorForm::OnListViewItemLongPressed(ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
205 {
206
207 }
208
209 void
210 EventSelectorForm::OnListViewItemReordered(ListView& listView, int indexFrom, int indexTo)
211 {
212
213 }
214
215 void
216 EventSelectorForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
217 {
218         if (pArgs != null)
219         {
220                 pArgs->RemoveAll(true);
221                 delete pArgs;
222         }
223
224         CalendarApp* pApp = static_cast<CalendarApp*>(UiApp::GetInstance());
225         const String* pItemType = null;
226         const String* pSelectionMode = pApp->GetArgument(KEY_DATA_SELECTION_MODE);
227         if (pSelectionMode != null && pSelectionMode->Equals(VALUE_CALENDAR_MULTIPLE_SELECTION_MODE, false) == true)
228         {
229                 __selectionType = EVENT_SELECTOR_FORM_SELECTION_TYPE_MULTIPLE;
230         }
231
232         TryCatch(pApp->GetArgument(KEY_SOCIAL_RESULT_TYPE) != null, , "[E_FAILURE] Unable to get result type.");
233
234         pItemType = pApp->GetArgument(KEY_SOCIAL_ITEM_TYPE);
235         TryCatch(pItemType != null, , "[E_FAILURE] Unable to get item type.");
236         TryCatch(pItemType->Equals(VALUE_CALENDAR_EVENT_TYPE, false) == true, , "[E_FAILURE] Invalid item type.");
237
238         if (__selectionType == EVENT_SELECTOR_FORM_SELECTION_TYPE_SINGLE)
239         {
240                 GetFooter()->RemoveAllItems();
241         }
242
243         LoadAllEvents();
244         __pListViewContents->UpdateList();
245         return;
246
247 CATCH:
248         SendAppControlResult(APP_CTRL_RESULT_FAILED);
249         UiApp::GetInstance()->Terminate();
250 }
251
252 void
253 EventSelectorForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
254 {
255
256 }
257
258 void
259 EventSelectorForm::OnActionPerformed(const Control& source, int actionId)
260 {
261         switch (actionId)
262         {
263         case IDA_EVENT_SELECTOR_FORM_FOOTER_DONE:
264                 result r = E_SUCCESS;
265                 int index = 0;
266                 LinkedList* pArgs = new (std::nothrow) LinkedList();
267                 IEnumerator* pEnum = __pEvents->GetEnumeratorN();
268                 while (pEnum->MoveNext() == E_SUCCESS)
269                 {
270                         if (__pListViewContents->IsItemChecked(index++) == true)
271                         {
272                                 pArgs->Add(pEnum->GetCurrent());
273                         }
274                 }
275
276                 r = SendAppControlResult(APP_CTRL_RESULT_SUCCEEDED, pArgs);
277                 pArgs->RemoveAll(false);
278                 delete pArgs;
279                 TryCatch(r == E_SUCCESS, , "[%s] Unable to return result.", GetErrorMessage(r));
280
281                 UiApp::GetInstance()->Terminate();
282                 return;
283
284 CATCH:
285                 SendAppControlResult(APP_CTRL_RESULT_FAILED);
286                 UiApp::GetInstance()->Terminate();
287                 break;
288         }
289 }
290
291 Bitmap*
292 EventSelectorForm::CreateColorBarBitmapN(const Dimension& size, const Color& color)
293 {
294         Bitmap* pBitmap = new (std::nothrow) Bitmap();
295         pBitmap->Construct(size, BITMAP_PIXEL_FORMAT_ARGB8888);
296
297         BufferInfo bufferinfo;
298         pBitmap->Lock(bufferinfo);
299
300         Canvas canvas;
301         canvas.Construct(bufferinfo);
302         canvas.SetBackgroundColor(color);
303         canvas.Clear();
304
305         pBitmap->Unlock();
306
307         return pBitmap;
308 }
309
310 Color
311 EventSelectorForm::GetCalendarColor(const RecordId& calendarId) const
312 {
313         const Calendar* pCalendar = static_cast<const Calendar*>(__allCalendars.GetValue(Integer(calendarId)));
314         byte r;
315         byte g;
316         byte b;
317         pCalendar->GetColor(r, g, b);
318         return Color(r, g, b);
319 }
320
321 String
322 EventSelectorForm::GetDateRangeText(const DateTime& start, const DateTime& end, bool isAllDayEvent) const
323 {
324         if (start.GetYear() == end.GetYear() && start.GetMonth() == end.GetMonth() && start.GetDay() == end.GetDay())
325         {
326                 String dateText;
327                 __pDateFormatter->Format(start, dateText);
328                 if (isAllDayEvent == true)
329                 {
330                         return dateText;
331                 }
332
333                 String startText;
334                 String endText;
335                 __pTimeFormatter->Format(start, startText);
336                 __pTimeFormatter->Format(end, endText);
337                 return dateText + L" " + startText + L" ~ " + endText;
338         }
339
340         String startDateText;
341         String endDateText;
342         __pDateFormatter->Format(start, startDateText);
343         __pDateFormatter->Format(end, endDateText);
344         if (isAllDayEvent == true)
345         {
346                 return startDateText + L" ~ " + endDateText;
347         }
348
349         String startTimeText;
350         String endTimeText;
351         __pTimeFormatter->Format(start, startTimeText);
352         __pTimeFormatter->Format(end, endTimeText);
353         return startDateText + L" " + startTimeText + L" ~ " + endDateText + L" " + endTimeText;
354 }
355
356 result
357 EventSelectorForm::LoadAllEvents(void)
358 {
359         CalendarbookFilter eventFilter(CB_FI_TYPE_EVENT);
360         IList* pList = __pCalendarbook->SearchN(eventFilter);
361         TryReturn(pList != null, E_FAILURE, "[E_FAILURE] Unable to get events.");
362
363         if (__pEvents != null)
364         {
365                 __pEvents->RemoveAll(true);
366                 delete __pEvents;
367         }
368         __pEvents = pList;
369         return E_SUCCESS;
370 }
371
372 result
373 EventSelectorForm::SendAppControlResult(AppCtrlResult appControlResult, const IList* pResultList)
374 {
375         CalendarApp* pApp = static_cast<CalendarApp*>(UiApp::GetInstance());
376         TryReturn(pApp != null, E_FAILURE, "[E_FAILURE] Unable to get app instance.");
377
378         HashMap* pArgs = null;
379         if (pResultList != null && pResultList->GetCount() > 0)
380         {
381                 pArgs = new (std::nothrow) HashMap(SingleObjectDeleter);
382                 pArgs->Construct(2);
383
384                 const String* pResultType = pApp->GetArgument(KEY_SOCIAL_RESULT_TYPE);
385                 if (pResultType != null && *pResultType == VALUE_SOCIAL_ITEM_ID)
386                 {
387                         int itemCount = pResultList->GetCount();
388                         ArrayList* pEvents = new (std::nothrow) ArrayList(SingleObjectDeleter);
389                         pEvents->Construct(itemCount);
390                         for (int i = 0; i < itemCount; ++i)
391                         {
392                                 pEvents->Add(new (std::nothrow) String(Integer::ToString(static_cast<const CalEvent*>(pResultList->GetAt(i))->GetRecordId())));
393                         }
394                         pArgs->Add(new (std::nothrow) String(KEY_SOCIAL_ITEM_ID), pEvents);
395                 }
396                 else
397                 {
398                         String* pPath = new (std::nothrow) String(CalendarApp::GetExportVcsFilePath());
399                         __pCalendarbook->ExportEventsToVcalendar(*pResultList, *pPath);
400
401                         ArrayList* pPaths = new (std::nothrow) ArrayList(SingleObjectDeleter);
402                         pPaths->Construct(1);
403                         pPaths->Add(pPath);
404                         pArgs->Add(new (std::nothrow) String(KEY_DATA_PATH), pPaths);
405                 }
406         }
407
408         result r = AppControlProviderManager::GetInstance()->SendAppControlResult(pApp->GetRequestId(), appControlResult, pArgs);
409         delete pArgs;
410
411         AppLogDebugIf(r != E_SUCCESS, "[%s] Unable to return result.", GetErrorMessage(r));
412         return r;
413 }
414
415 void
416 EventSelectorForm::OnCalendarEventsChanged(const IList& eventChangeInfoList)
417 {
418         LoadAllEvents();
419         __pListViewContents->UpdateList();
420 }
421
422 void
423 EventSelectorForm::OnCalendarTodosChanged(const IList& todoChangeInfoList)
424 {
425
426 }