Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnTabLogs.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        PhnTabLogs.cpp
19  * @brief       The Logs Tab
20  */
21
22 #include <FApp.h>
23 #include <FMedia.h>
24 #include <FGraphics.h>
25 #include "PhnPhoneApp.h"
26 #include "PhnAppUtility.h"
27 #include "PhnSettingsConstants.h"
28 #include "PhnCommonUtils.h"
29 #include "PhnTabLogs.h"
30 #include "PhnSceneRegister.h"
31 #include "PhnCalllogManager.h"
32 #include "PhnCallPresentationModel.h"
33 #include "PhnTypes.h"
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Ui::Controls;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Media;
41 using namespace Tizen::Ui;
42 using namespace Tizen::Ui::Scenes;
43
44 const int W_NOCONTENTS_IMAGE = 280;
45 const int H_NOCONTENTS_IMAGE = 280;
46 //List view height
47 const int H_TOAST_MSG = 48;
48 const int H_SELECTALL_CHK_BOX = 112;
49
50 const wchar_t* IDL_PANEL_LOGS = L"IDL_PANEL_LOGS";
51 const wchar_t* IDC_LABEL_SELECTED_POPUP = L"IDC_LABEL_SELECTED_POPUP";
52 const unsigned int COLOR_LABEL_SELECTED = Color32<215, 225, 232, 255>::Value;
53
54 DialTabLogs::DialTabLogs(void)
55 {
56         __pDataProvider = new CustomDataProvider();
57         __pFormFooter = null;
58         __plogsListView = null;
59         __isEditMode = false;
60         __pButtonSelectAll = null;
61         __groupCount = 0;
62         __itemCount = 0;
63         __isSelectAll = false;
64         __selectedItemCount = 0;
65         __pCallMgr = null;
66         __pArgs = null;
67 }
68
69 DialTabLogs::~DialTabLogs(void)
70 {
71         if (__pDataProvider != null)
72         {
73                 delete __pDataProvider;
74                 __pDataProvider = null;
75         }
76         if (__pCallMgr != null)
77         {
78                 __pCallMgr = null;
79         }
80 }
81
82 bool
83 DialTabLogs::Initialize(void)
84 {
85         result r = Construct(IDL_PANEL_LOGS);
86         TryCatch(r == E_SUCCESS, , "DialTabLogs::Initialise() - Fail to initialise Call log panel");
87         return true;
88
89         CATCH:
90         return false;
91 }
92
93 result
94 DialTabLogs::OnInitializing(void)
95 {
96         result r = E_SUCCESS;
97
98         // Layout setting
99         const Form* pForm = dynamic_cast<Form*>(GetParent());
100         if(pForm == null)
101         {
102                 return E_FAILURE;
103         }
104
105         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(pForm->GetLandscapeLayoutN());
106         if (pRelativeLayout != null)
107         {
108                 pRelativeLayout->SetHorizontalFitPolicy(*this, FIT_POLICY_PARENT);
109                 pRelativeLayout->SetVerticalFitPolicy(*this, FIT_POLICY_PARENT);
110                 delete pRelativeLayout;
111         }
112
113         pRelativeLayout = dynamic_cast<RelativeLayout*>(pForm->GetPortraitLayoutN());
114         if (pRelativeLayout != null)
115         {
116                 pRelativeLayout->SetHorizontalFitPolicy(*this, FIT_POLICY_PARENT);
117                 pRelativeLayout->SetVerticalFitPolicy(*this, FIT_POLICY_PARENT);
118                 delete pRelativeLayout;
119         }
120
121         __pFormFooter = pForm->GetFooter();
122         if (__pFormFooter != null)
123         {
124                 __pFormFooter->SetShowState(true);
125                 __pFormFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
126                 __pFormFooter->AddActionEventListener(*this);
127
128                 __footerItemDelete.Construct(IDA_FOOTER_LOG_DELETE);
129                 String delStr = AppUtility::GetResourceString(IDS_DELETE_BTN_STRING);
130                 __footerItemDelete.SetText(delStr);
131                 __pFormFooter->AddItem(__footerItemDelete);
132                 String cancelStr = AppUtility::GetResourceString(IDS_CANCEL_BTN_STRING);
133                 __footerItemCancel.Construct(IDA_FOOTER_LOG_CANCEL);
134                 __footerItemCancel.SetText(cancelStr);
135         }
136
137         __pButtonSelectAll = static_cast<CheckButton*>(GetControl("IDC_CHECKBUTTON_SELECTALL"));
138         if (__pButtonSelectAll != null)
139         {
140                 __pButtonSelectAll->SetShowState(false);
141                 __pButtonSelectAll->SetActionId(IDA_CHECKBUTTON_SELECT_ALL, IDA_CHECKBUTTON_UNSELECT_ALL);
142                 __pButtonSelectAll->AddActionEventListener(*this);
143                 __pButtonSelectAll->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
144         }
145
146         //create a list view
147         __plogsListView = new (std::nothrow) GroupedListView();
148         if (__plogsListView != null)
149         {
150                 __plogsListView->Construct(Rectangle(pForm->GetX(), pForm->GetY(), pForm->GetWidth(), pForm->GetClientAreaBounds().height), GROUPED_LIST_VIEW_STYLE_INDEXED, true, SCROLL_STYLE_FADE_OUT);
151                 __plogsListView->AddGroupedListViewItemEventListener(*this);
152                 Bitmap* pNoContentBitmap = AppUtility::GetBitmapFromResourcesN(IDB_LOG_NO_CONTENT,W_NOCONTENTS_IMAGE,H_NOCONTENTS_IMAGE);
153                 if(pNoContentBitmap != null)
154                 {
155                         __plogsListView->SetBitmapOfEmptyList(pNoContentBitmap);
156                 }
157                 __plogsListView->SetTextOfEmptyList(AppUtility::GetResourceString(IDS_NO_LOGS));
158                 __plogsListView->SetTextColorOfEmptyList(Color(128,128,128));
159                 r = AddControl(*__plogsListView);
160                 TryCatch(r == E_SUCCESS, , "Calllog list control not added");
161         }
162
163         __pSelectedLabel = static_cast<Label*>(GetControl(IDC_LABEL_SELECTED_POPUP));
164         if(__pSelectedLabel != null)
165         {
166                 SetControlAlwaysOnTop(*__pSelectedLabel,true);
167                 __pSelectedLabel->SetBackgroundColor(COLOR_LABEL_SELECTED);
168                 __pSelectedLabel->SetShowState(false);
169         }
170
171         CATCH:
172         return r;
173 }
174
175 result
176 DialTabLogs::OnTerminating(void)
177 {
178         result r = E_SUCCESS;
179         return r;
180 }
181
182 void
183 DialTabLogs::DeleteSelectedCalllog(void)
184 {
185         CallLogDetails* pCalllogInfo = null;
186         int itemCount = 0;
187
188         if(__selectedItemCount == __pCallMgr->GetAllCalllogCount())
189         {
190                 __pCallMgr->DeleteAllCalllog();
191                 __pDataProvider->DeleteAllItems();
192         }
193         else
194         {
195                 for(int groupIndex = 0; groupIndex < __groupCount; groupIndex++)
196                 {
197                         itemCount = __plogsListView->GetItemCountAt(groupIndex);
198                         for(int itemIndex = itemCount-1; itemIndex >=0 ; itemIndex--)
199                         {
200                                 bool isSelected = __plogsListView->IsItemChecked(groupIndex, itemIndex);
201                                 if(isSelected == true)
202                                 {
203                                         __pDataProvider->GetItemAtGroupIndex(groupIndex, itemIndex, &pCalllogInfo);
204                                         if(pCalllogInfo != null)
205                                         {
206                                                 __pCallMgr->DeleteCalllogByDbId(pCalllogInfo->GetCalllogDbId());
207                                                 __pDataProvider->DeleteItemAtGroupIndex(groupIndex, itemIndex);
208                                         }
209                                 }
210                         }
211                 }
212         }
213 }
214
215 void
216 DialTabLogs::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
217 {
218         __pCallMgr = CallLogManager::GetInstance();
219         __pCallMgr->AddCalllogChangeListener(*this);
220
221         __plogsListView->SetItemProvider(*__pDataProvider);
222         //Reset the delete status
223         //SetCallLogDisplayMode(false);
224
225         //Check if opened in edit mode
226         if(__isEditMode == true)
227         {
228                 //Check if "select all" is already selected
229                 if (__pButtonSelectAll->IsSelected() == true)
230                 {
231                         HandleAllItemSelection();
232                 }
233                 //If there is no items selected the reset the display mode
234                 else if(__selectedItemCount == 0)
235                 {
236                         SetCallLogDisplayMode(false);
237                 }
238         }
239
240         if (__pFormFooter != null)
241         {
242                 __pFormFooter->SetShowState(true);
243                 __pFormFooter->SetBackButton();
244                 if(__pCallMgr->GetAllCalllogCount() > 0)
245                 {
246                         __pFormFooter->SetItemEnabled(0, true);
247                 }
248                 else
249                 {
250                         __pFormFooter->SetItemEnabled(0, false);
251                 }
252         }
253 }
254
255 void
256 DialTabLogs::HandleAllItemSelection(void)
257 {
258         SetListItemSelectStatus(true);
259         __itemCount = __pCallMgr->GetAllCalllogCount();
260         __selectedItemCount = __itemCount;
261         String text;
262         String selItemStr = L"";
263         if(__selectedItemCount > 1)
264         {
265                 selItemStr = AppUtility::GetResourceString(IDS_SELECTED_ITEMS_STRING);
266         }
267         else
268         {
269                 selItemStr = AppUtility::GetResourceString(IDS_SELECTED_ITEM_STRING);
270         }
271         text.Format(100, selItemStr.GetPointer(), __itemCount);
272         __pSelectedLabel->SetText(text);
273         __pSelectedLabel->Invalidate(false);
274 }
275
276 void
277 DialTabLogs::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
278 {
279         if (__pCallMgr != null)
280         {
281                 __pCallMgr->RemoveCalllogChangeListner(*this);
282                 __pCallMgr = null;
283         }
284         __pFormFooter->RemoveBackButton();
285 }
286
287 void
288 DialTabLogs::OnGroupedListViewItemStateChanged(GroupedListView& listView, int groupIndex, int itemIndex, int elementId, ListItemStatus state)
289 {
290         if (__isEditMode == false)
291         {
292                 __pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
293                 __pArgs->Construct();
294                 CallLogDetails* pCalllogInfo = null;
295                 __pDataProvider->GetItemAtGroupIndex(groupIndex, itemIndex, &pCalllogInfo);
296                 if(pCalllogInfo != null && pCalllogInfo->GetPhoneNumber() == null)
297                 {
298                         //handling hidden call
299                         return;
300                 }
301                 if(pCalllogInfo != null)
302                 {
303                         __pArgs->Add((new String(pCalllogInfo->GetPhoneNumber())));
304                 }
305                 SceneManager* pSceneManager = SceneManager::GetInstance();
306                 pSceneManager->GoForward( ForwardSceneTransition(IDSCN_CALL_LOG_DETAILS, SCENE_TRANSITION_ANIMATION_TYPE_NONE,
307                                 SCENE_HISTORY_OPTION_NO_HISTORY), __pArgs);
308                 __pArgs = null;
309         }
310         else
311         {
312                 String text;
313                 if (state == LIST_ITEM_STATUS_CHECKED)
314                 {
315                         __selectedItemCount++;
316                 }
317                 else if (state == LIST_ITEM_STATUS_UNCHECKED)
318                 {
319                         __selectedItemCount--;
320                 }
321                 if (__selectedItemCount == __itemCount)
322                 {
323                         __pButtonSelectAll->SetSelected(true);
324                 }
325                 else
326                 {
327                         __pButtonSelectAll->SetSelected(false);
328                 }
329                 if (__selectedItemCount == 0)
330                 {
331                         __pFormFooter->SetItemEnabled(0, false);
332                         String selStr = AppUtility::GetResourceString(IDS_SELECT_CALL_LOG);
333                         __pSelectedLabel->SetText(selStr);
334                 }
335                 else
336                 {
337                         __pFormFooter->SetItemEnabled(0, true);
338                         String selItemStr = L"";
339                         if(__selectedItemCount > 1)
340                         {
341                                 selItemStr = AppUtility::GetResourceString(IDS_SELECTED_ITEMS_STRING);
342                         }
343                         else
344                         {
345                                 selItemStr = AppUtility::GetResourceString(IDS_SELECTED_ITEM_STRING);
346                         }
347                         text.Format(100, selItemStr.GetPointer(), __selectedItemCount);
348                         __pSelectedLabel->SetText(text);
349                 }
350                 __pFormFooter->Invalidate(true);
351                 __pButtonSelectAll->Invalidate(false);
352                 __pSelectedLabel->Invalidate(false);
353         }
354 }
355
356 void
357 DialTabLogs::OnGroupedListViewContextItemStateChanged(GroupedListView& listView, int groupIndex, int itemIndex, int elementId, ListContextItemStatus state)
358 {
359         //Empty implementation
360         CallLogDetails* pCalllogInfo = null;
361         __pDataProvider->GetItemAtGroupIndex(groupIndex, itemIndex, &pCalllogInfo);
362         if(pCalllogInfo != null)
363         {
364
365                 switch (elementId)
366                 {
367                 case IDA_LIST_ELEMENT_DELETE:
368                 {
369                         if(pCalllogInfo != null)
370                         {
371                                 __pCallMgr->DeleteCalllogByDbId(pCalllogInfo->GetCalllogDbId());
372                                 __pDataProvider->DeleteItemAtGroupIndex(groupIndex, itemIndex);
373                                 if(__pCallMgr->GetAllCalllogCount() > 0)
374                                 {
375                                         __pFormFooter->SetItemEnabled(0, true);
376                                 }
377                                 else
378                                 {
379                                         __pFormFooter->SetItemEnabled(0, false);
380                                 }
381                                 __pFormFooter->Invalidate(true);
382                         }
383                 }
384                 break;
385                 case IDA_LIST_ELEMENT_CALL:
386                 {
387                         SceneManager* pSceneManager = SceneManager::GetInstance();
388                         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
389                         char* pPhoneNumber = pCalllogInfo->GetPhoneNumber();
390                         if(pPhoneNumber == null)
391                         {
392                                 break;
393                         }
394                         String phoneNumber=L"";
395                         phoneNumber.Append(pPhoneNumber);
396                         //make an outgoing call with given number
397                         String* contactTxt = new (std::nothrow) String(phoneNumber);
398                         //switch to dialing outgoing call form
399                         IList* pContact = new (std::nothrow) ArrayList(SingleObjectDeleter);
400                         pContact->Add(contactTxt);
401                         //Check if dialing an emergency call
402                         bool isEmergencyCall = pCallPresentor->IsEmergencyNumber(*contactTxt, true);
403                         if (isEmergencyCall)
404                         {
405                                 pSceneManager->GoForward( ForwardSceneTransition( IDSCN_SCENE_OUT_EMERGENCYCALL), pContact);
406                         }
407                         else
408                         {
409                                 pSceneManager->GoForward( ForwardSceneTransition(IDSCN_SCENE_OUTCALL), pContact);
410                         }
411
412                 }
413                         break;
414                 case IDA_LIST_ELEMENT_MESSAGE:
415                 {
416                         result r = E_SUCCESS;
417                         char* pPhoneNumber = pCalllogInfo->GetPhoneNumber();
418                         if(pPhoneNumber == null)
419                         {
420                                 break;
421                         }
422                         String phoneNumber=L"";
423                         phoneNumber.Append(pPhoneNumber);
424
425                         HashMap extraData;
426                         extraData.Construct();
427
428                         extraData.Add(new (std::nothrow) String(MESSAGE_TYPE), new (std::nothrow) String(MESSAGE_SMS_TYPE));
429                         extraData.Add(new (std::nothrow) String(MESSAGE_TO), new (std::nothrow) String(phoneNumber));
430
431                         AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
432                         if (pAc != null)
433                         {
434                                 r = pAc->Start(null, null, &extraData, null);
435
436                                 delete pAc;
437                         }
438
439                         extraData.RemoveAll(true);
440
441                 }
442                         break;
443                 case IDA_LIST_ELEMENT_VIDEO_CALL:
444                 {
445                         //todo: call once video call implemented
446                 }
447                         break;
448                 default:
449                         break;
450                 }
451         }
452         listView.UpdateList();
453 }
454
455 void
456 DialTabLogs::OnGroupedListViewItemSwept(GroupedListView& listView, int groupIndex, int itemIndex, SweepDirection direction)
457 {
458         //Empty implementation
459 }
460
461 void
462 DialTabLogs::OnActionPerformed(const Control& source, int actionId)
463 {
464         switch (actionId)
465         {
466         case IDA_FOOTER_LOG_DELETE:
467         {
468                 if (__isEditMode == false)
469                 {
470                         SetCallLogDisplayMode(true);
471                 }
472                 else
473                 {
474                         //Delete selected items
475                         DeleteSelectedCalllog();
476                         SetCallLogDisplayMode(false);
477                 }
478         }
479         break;
480
481         case IDA_FOOTER_LOG_CANCEL:
482         {
483                 SetCallLogDisplayMode(false);
484         }
485         break;
486
487         case IDA_CHECKBUTTON_SELECT_ALL:
488         {
489                 HandleAllItemSelection();
490         }
491         break;
492
493         case IDA_CHECKBUTTON_UNSELECT_ALL:
494         {
495                 SetListItemSelectStatus(false);
496                 __selectedItemCount = 0;
497                 String selStr = AppUtility::GetResourceString(IDS_SELECT_CALL_LOG);
498                 __pSelectedLabel->SetText(selStr);
499                 __pSelectedLabel->Invalidate(false);
500         }
501         break;
502
503         default:
504                 break;
505         }
506 }
507
508 void
509 DialTabLogs::SetListItemSelectStatus(bool status)
510 {
511         int groupIndex = 0;
512         int itemIndex = 0;
513         int itemCount = 0;
514
515         __isSelectAll = status;
516         for (; groupIndex < __groupCount; groupIndex++)
517         {
518                 itemCount = __plogsListView->GetItemCountAt(groupIndex);
519                 for (itemIndex = 0; itemIndex < itemCount; itemIndex++)
520                 {
521                         __plogsListView->SetItemChecked(groupIndex, itemIndex, status);
522                 }
523         }
524         __pFormFooter->SetItemEnabled(0, status);
525         __pFormFooter->Invalidate(true);
526         __plogsListView->Invalidate(true);
527 }
528
529 void
530 DialTabLogs::SetCallLogDisplayMode(bool isEdit)
531 {
532         int groupIndex = 0;
533         int itemCount = 0;
534
535         if(isEdit == __isEditMode)
536         {
537                 return;
538         }
539
540         for (; groupIndex < __groupCount; groupIndex++)
541         {
542                 itemCount += __plogsListView->GetItemCountAt(groupIndex);
543         }
544         __itemCount = __pCallMgr->GetAllCalllogCount();//itemCount;
545         __groupCount = __plogsListView->GetGroupCount();
546         __pButtonSelectAll->SetSelected(false);
547         if (isEdit == false)
548         {
549                 __isEditMode = false;
550                 __pDataProvider->SetListItemStyle(false);
551                 __plogsListView->SetSweepEnabled(true);
552                 const Form* pForm = dynamic_cast<Form*>(GetParent());
553                 if(pForm == null)
554                 {
555                         return;
556                 }
557                 __plogsListView->SetBounds(pForm->GetX(), pForm->GetY(), pForm->GetWidth(), pForm->GetClientAreaBounds().height);
558                 __plogsListView->UpdateList();
559                 __pFormFooter->RemoveItemAt(1);
560                 __pFormFooter->SetBackButton();
561                 if(__pCallMgr->GetAllCalllogCount() > 0)
562                 {
563                         __pFormFooter->SetItemEnabled(0, true);
564                 }
565                 else
566                 {
567                         __pFormFooter->SetItemEnabled(0, false);
568                 }
569
570                 __pButtonSelectAll->SetShowState(false);
571                 __pSelectedLabel->SetShowState(false);
572                 __pSelectedLabel->Invalidate(false);
573         }
574         else
575         {
576                 __isEditMode = true;
577                 const Form* pForm = dynamic_cast<Form*>(GetParent());
578                 if(pForm == null)
579                 {
580                         return;
581                 }
582                 __pDataProvider->SetListItemStyle(isEdit);
583                 __plogsListView->SetSweepEnabled(false);
584
585                 //reset size to allow "SELECT ALL" & "TOAST MSG"
586                 Rectangle newRect(pForm->GetX(), H_SELECTALL_CHK_BOX, pForm->GetWidth(), (pForm->GetClientAreaBounds().height - H_SELECTALL_CHK_BOX - H_TOAST_MSG));
587                 __plogsListView->SetBounds(newRect);
588                 UnCheckAllItems();
589                 __plogsListView->UpdateList();
590                 __pFormFooter->AddItem(__footerItemCancel);
591                 __pFormFooter->RemoveBackButton();
592                 __pButtonSelectAll->SetShowState(true);
593                 __pFormFooter->SetItemEnabled(0, false);
594                 __pFormFooter->Invalidate(false);
595                 __selectedItemCount = 0;
596                 String selStr = AppUtility::GetResourceString(IDS_SELECT_CALL_LOG);
597                 __pSelectedLabel->SetText(selStr);
598                 __pSelectedLabel->SetShowState(true);
599                 __pSelectedLabel->Invalidate(false);
600         }
601         Draw();
602 }
603
604 void
605 DialTabLogs::UnCheckAllItems(void)
606 {
607         int itemCount = 0;
608         for(int groupIndex = 0; groupIndex < __groupCount; groupIndex++)
609         {
610                 itemCount = __plogsListView->GetItemCountAt(groupIndex);
611                 for(int itemIndex = itemCount-1; itemIndex >=0 ; itemIndex--)
612                 {
613                         bool isSelected = __plogsListView->IsItemChecked(groupIndex, itemIndex);
614                         if(isSelected == true)
615                         {
616                                 __plogsListView->SetItemChecked(groupIndex,itemIndex,false);
617                         }
618                 }
619         }
620         __selectedItemCount = 0;
621 }
622
623 void DialTabLogs::OnCalllogChanged(void)
624 {
625         // If select all was selected before and some new calls are added (Auto rejeted call)
626         //Then we will not update the call log list.
627         if(__isEditMode == false)
628         {
629                 if(__pCallMgr->GetAllCalllogCount() > 0)
630                 {
631                         __pFormFooter->SetShowState(true);
632                         __pFormFooter->SetItemEnabled(0,true);
633                         __pFormFooter->Invalidate(true);
634                 }
635         __plogsListView->UpdateList();
636         __plogsListView->Invalidate(false);
637         }
638 }