Fixed Jira issues 50249, 50683, 50387
[apps/osp/MusicPlayer.git] / src / MpAllListEditorPanel.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                MpAllListEditorPanel.cpp
19  * @brief               This is the implementation file for AllListEditorPanel class.
20  */
21
22 #include "MpAllListEditorPanel.h"
23 #include "MpAllListPresentationModel.h"
24 #include "MpThumbnailInfo.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Content;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Social;
32 using namespace Tizen::Ui;
33 using namespace Tizen::Ui::Controls;
34 using namespace Tizen::Ui::Scenes;
35
36 AllListEditorPanel::AllListEditorPanel(void)
37         : ContentEditPanel::ContentEditPanel()
38         , ThumbnailBase::ThumbnailBase()
39         , __pThumbnail(null)
40         , __editType(0)
41 {
42         AppLogDebug("ENTER");
43         AppLogDebug("EXIT");
44 }
45
46 AllListEditorPanel::~AllListEditorPanel(void)
47 {
48         AppLogDebug("ENTER");
49         AppLogDebug("EXIT");
50 }
51
52 result
53 AllListEditorPanel::OnInitializing(void)
54 {
55         AppLogDebug("ENTER");
56         __pPresentationModel = AllListPresentationModel::GetInstance();
57         __fontSizeValue = CommonUtil::GetFontSizeValue();
58         __itemHeight = CommonUtil::GetItemHeight(__fontSizeValue);
59         UpdateContentList();
60         SetLayoutFitToForm();
61         AppLogDebug("EXIT");
62         return ThumbnailBase::Construct();
63 }
64
65 result
66 AllListEditorPanel::OnTerminating(void)
67 {
68         AppLogDebug("ENTER");
69         ThumbnailBase::Stop();
70         CancelAllThumbnailRequest();
71         AppLogDebug("EXIT");
72         return ContentEditPanel::OnTerminating();
73 }
74
75 void
76 AllListEditorPanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
77 {
78         AppLogDebug("ENTER");
79         if (__pPresentationModel == null)
80         {
81                 return;
82         }
83
84         int totalCount = __pPresentationModel->GetTotalCount();
85
86         switch (actionId)
87         {
88         case IDA_HEADER_BUTTON_SELECTE_ALL:
89                 {
90                         if (totalCount == __checkedItemCount)
91                         {
92                                 SetItemCheckedAll(false);
93                         }
94                         else
95                         {
96                                 SetItemCheckedAll(true);
97                         }
98                 }
99                 break;
100
101         case IDA_FOOTER_BUTTON_DELETE:
102                 {
103                         int totalCount = __pPresentationModel->GetTotalCount();
104                         for (int iCount = totalCount - 1; iCount >= 0; iCount--)
105                         {
106                                 if (__pContentTableView->IsItemChecked(iCount) == true)
107                                 {
108                                         __pPresentationModel->RemoveContent(iCount);
109                                 }
110                         }
111                 }
112                 break;
113
114         case IDA_FOOTER_BUTTON_ADD_TO_PLAYLIST:
115                 {
116                         LanucherPicker(PICKER_TYPE_PLAY_LIST_PICKER);
117                 }
118                 break;
119
120         case IDA_FOOTER_BUTTON_SHARE:
121                 {
122                         LanucherPicker(PICKER_TYPE_SHARE_PICKER);
123                 }
124                 break;
125
126         default:
127                 break;
128         }
129         AppLogDebug("EXIT");
130 }
131
132 void
133 AllListEditorPanel::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
134                                                                         const Tizen::Ui::Scenes::SceneId& currentSceneId,
135                                                                         Tizen::Base::Collection::IList* pArgs)
136 {
137         AppLogDebug("ENTER");
138         ContentPanel::OnSceneActivatedN(previousSceneId, currentSceneId, null);
139
140         Form* pForm = dynamic_cast<Form*>(GetParent());
141         Footer* pFooter = null;
142         AppAssert(pForm);
143
144         pForm->SetFormBackEventListener(this);
145         if (pArgs != null && (previousSceneId.Equals(IDSCN_ALL_LIST, true)))
146         {
147                 __editType = static_cast<Integer*>(pArgs->GetAt(0))->ToInt();
148         }
149
150         SetEditHeaderStyle();
151
152         if (__editType == IDA_CONTEXT_MENU_ITEM_EDIT)
153         {
154                 SetFooterStyle(STYLE_ADDTO_DELETE_ADD);
155                 SetFormStyle();
156                 pFooter = CommonUtil::CreateBackButtonStyleFooter(*pForm, STYLE_ADDTO_DELETE_ADD);
157         }
158         else
159         {
160                 SetFooterStyle(STYLE_SHARE_ADD);
161                 SetFormStyle();
162                 pFooter = CommonUtil::CreateBackButtonStyleFooter(*pForm, STYLE_SHARE_ADD);
163         }
164
165         if (pFooter != null)
166         {
167                 pFooter->AddActionEventListener(*this);
168                 pFooter->SetShowState(true);
169                 CommonUtil::SetFooterItemEnabled(*pFooter, false);
170         }
171
172         UpdateContentEditScreenState();
173
174         if (previousSceneId.Equals(IDSCN_PLAYLIST_CREATOR, true))
175         {
176                 if (pArgs != null)
177                 {
178                         int totalCount = 0;
179                         __pContentTableView->UpdateTableView();
180                         __checkedItemCount = INIT_VALUE;
181                         SetCheckedCountBallonTooltip(__checkedItemCount);
182
183                         totalCount = __pPresentationModel->GetTotalCount();
184                         for (int count = 0; count < totalCount; count++)
185                         {
186                                 __pContentTableView->SetItemChecked(count, false);
187                         }
188                 }
189                 else
190                 {
191                         if (pFooter != null)
192                         {
193                                 CommonUtil::SetFooterItemEnabled(*pFooter, true);
194                         }
195                 }
196         }
197
198         if (pArgs != null)
199         {
200                 pArgs->RemoveAll(true);
201                 delete pArgs;
202         }
203         AppLogDebug("EXIT");
204 }
205
206 void
207 AllListEditorPanel::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
208                                                                                 const Tizen::Ui::Scenes::SceneId& nextSceneId)
209 {
210         AppLogDebug("ENTER");
211         ContentPanel::OnSceneDeactivated(currentSceneId, nextSceneId);
212
213         if (!(nextSceneId.Equals(IDSCN_PLAYLIST_CREATOR, false)))
214         {
215                 Footer* pFooter = GetFooter();
216                 if (pFooter != null)
217                 {
218                         pFooter->RemoveAllButtons();
219                         pFooter->RemoveAllItems();
220                         pFooter->RemoveActionEventListener(*this);
221                 }
222
223                 __checkedItemCount = INIT_VALUE;
224                 SetCheckedCountBallonTooltip(__checkedItemCount);
225
226                 int totalCount = __pPresentationModel->GetTotalCount();
227                 for (int iCount = 0; iCount < totalCount; iCount++)
228                 {
229                         __pContentTableView->SetItemChecked(iCount, false);
230                 }
231         }
232         CancelAllThumbnailRequest();
233         AppLogDebug("EXIT");
234 }
235
236 void
237 AllListEditorPanel::OnFontSizeChanged(void)
238 {
239         __fontSizeValue = CommonUtil::GetFontSizeValue();
240         __itemHeight = CommonUtil::GetItemHeight(__fontSizeValue);
241         __pContentTableView->UpdateTableView();
242
243 }
244
245 int
246 AllListEditorPanel::GetItemCount(void)
247 {
248         AppLogDebug("ENTER");
249         int musicItem = INIT_VALUE;
250         if (__pPresentationModel == null)
251         {
252                 return musicItem;
253         }
254
255         musicItem = __pPresentationModel->GetTotalCount();
256         AppLogDebug("EXIT");
257         return musicItem;
258 }
259
260 Tizen::Ui::Controls::TableViewItem*
261 AllListEditorPanel::CreateItem(const int itemIndex, int itemWidth)
262 {
263         AppLogDebug("ENTER");
264         RelativeLayout layout;
265         layout.Construct();
266
267         TableViewItem* pItem = new (std::nothrow) TableViewItem();
268         ContentInformation* pContentInfo = __pPresentationModel->GetContentInfoN(itemIndex);
269
270         result r = pItem->Construct(layout, Dimension(itemWidth, __itemHeight), TABLE_VIEW_ANNEX_STYLE_MARK);
271         TryCatch(r == E_SUCCESS, delete pItem, "pItem->Construct(%s)", GetErrorMessage(r));
272         TryCatch(pContentInfo != null, delete pItem, "pContentInfo is null", GetErrorMessage(GetLastResult()));
273
274         r = CreateTableViewItem(*pItem, *pContentInfo);
275         TryCatch(r == E_SUCCESS, delete pItem, "CreateTableViewItem failed(%s)", GetErrorMessage(r));
276
277         RequestThumbnail(pContentInfo->contentId, (new (std::nothrow) Integer(itemIndex)));
278         delete pContentInfo;
279
280         AppLogDebug("EXIT");
281         return pItem;
282
283 CATCH:
284         AppLogDebug("EXIT(%ls)", GetErrorMessage(GetLastResult()));
285         delete pContentInfo;
286         return null;
287 }
288
289 void
290 AllListEditorPanel::OnTableViewItemStateChanged(Tizen::Ui::Controls::TableView& tableView,
291                                                         int itemIndex,
292                                                         Tizen::Ui::Controls::TableViewItem* pItem,
293                                                         Tizen::Ui::Controls::TableViewItemStatus status)
294 {
295         AppLogDebug("ENTER");
296         Form* pForm = dynamic_cast<Form*>(GetParent());
297         AppAssert(pForm);
298
299         Footer* pFooter = pForm->GetFooter();
300
301         switch (status)
302         {
303         case TABLE_VIEW_ITEM_STATUS_CHECKED:
304                 {
305                         __checkedItemCount++;
306                         tableView.SetItemChecked(itemIndex, true);
307                 }
308                 break;
309
310         case TABLE_VIEW_ITEM_STATUS_UNCHECKED:
311                 {
312                         __checkedItemCount--;
313                         tableView.SetItemChecked(itemIndex, false);
314                 }
315                 break;
316
317         default:
318                 break;
319         }
320
321         if (__checkedItemCount > INIT_VALUE)
322         {
323                 if (pFooter != null)
324                 {
325                         CommonUtil::SetFooterItemEnabled(*pFooter, true);
326                 }
327         }
328         else
329         {
330                 if (pFooter != null)
331                 {
332                         CommonUtil::SetFooterItemEnabled(*pFooter, false);
333                 }
334         }
335
336         SetCheckedCountBallonTooltip(__checkedItemCount);
337         Invalidate(true);
338         AppLogDebug("EXIT");
339 }
340
341 result
342 AllListEditorPanel::CreateTableViewItem(Tizen::Ui::Container& parent,
343                                                         const ContentInformation& contentInfoStruct)
344 {
345         AppLogDebug("ENTER");
346         Panel* pTableViewItem =  new (std::nothrow) Panel();
347
348         if (__fontSizeValue.Equals(STRING_FONT_SIZE_GIANT, false))
349         {
350                 if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL_FONT_SIZE_GIANT)))
351                 {
352                         AppLogDebug("Construct(IDL_CONTENTS_LIBARY_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
353                         return E_FAILURE;
354                 }
355         }
356         else if (__fontSizeValue.Equals(STRING_FONT_SIZE_HUGE, false))
357         {
358                 if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL_FONT_SIZE_HUGE)))
359                 {
360                         AppLogDebug("Construct(IDL_CONTENTS_LIBARY_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
361                         return E_FAILURE;
362                 }
363         }
364         else if (__fontSizeValue.Equals(STRING_FONT_SIZE_LARGE, false))
365         {
366                 if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL_FONT_SIZE_LARGE)))
367                 {
368                         AppLogDebug("Construct(IDL_CONTENTS_LIBARY_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
369                         return E_FAILURE;
370                 }
371         }
372         else if (__fontSizeValue.Equals(STRING_FONT_SIZE_SMALL, false))
373         {
374                 if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL_FONT_SIZE_SMALL)))
375                 {
376                         AppLogDebug("Construct(IDL_CONTENTS_LIBARY_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
377                         return E_FAILURE;
378                 }
379         }
380         else
381         {
382                 if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL_DEFAULT)))
383                 {
384                         AppLogDebug("Construct(IDL_CONTENTS_LIBARY_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
385                         return E_FAILURE;
386                 }
387         }
388
389         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_TITLE_NAME))->SetText(contentInfoStruct.TitleName);
390         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_ARTIST_NAME))->SetText(contentInfoStruct.ArtistName);
391         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_THUMBNAIL))->SetBackgroundBitmap(*GetDefaultThumbnail());
392
393         parent.AddControl(pTableViewItem);
394         CommonUtil::SetLayoutFitToContainer(parent, *pTableViewItem);
395
396         AppLogDebug("EXIT");
397         return E_SUCCESS;
398 }
399
400 void
401 AllListEditorPanel::UpdateContentList(void)
402 {
403         AppLogDebug("ENTER");
404         if (__pPresentationModel != null)
405         {
406                 __pPresentationModel->UpdateContentList();
407         }
408         AppLogDebug("EXIT");
409 }
410
411 void
412 AllListEditorPanel::SetEditHeaderStyle(void)
413 {
414         AppLogDebug("ENTER");
415         Header* pHeader = GetHeader();
416         if(__editType == IDA_CONTEXT_MENU_ITEM_EDIT)
417         {
418                 String titleText(ResourceManager::GetString(L"IDS_COM_BODY_EDIT"));
419                 CommonUtil::SetEditHeaderStyle(*pHeader, titleText);
420         }
421         else
422         {
423                 String titleText(ResourceManager::GetString(L"IDS_IV_BODY_SHARE_VIA"));
424                 CommonUtil::SetEditHeaderStyle(*pHeader, titleText);
425         }
426         pHeader->Invalidate(true);
427         AppLogDebug("EXIT");
428 }
429
430 void
431 AllListEditorPanel::OnThumbnailInfoReveivedN(ThumbnailInfo* pThumbnailInfo)
432 {
433         AppLogDebug("ENTER");
434         __pThumbnail = pThumbnailInfo->GetBitmapN();
435         Object* pParam = pThumbnailInfo->GetUserParamN();
436         if (pParam != null)
437         {
438                 __pContentTableView->RefreshItem((static_cast<Integer*>(pParam))->ToInt(), TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
439                 delete pParam;
440         }
441
442         delete pThumbnailInfo;
443         AppLogDebug("EXIT");
444 }
445
446 void
447 AllListEditorPanel::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
448 {
449         AppLogDebug("ENTER");
450         SceneManager* pSceneManager = SceneManager::GetInstance();
451         AppAssert(pSceneManager);
452         if (CommonUtil::GetAddtoPlaylistState())
453         {
454                 ArrayList* pList = new (std::nothrow) ArrayList();
455                 pList->Construct();
456
457                 CommonUtil::SetAddtoPlaylistState(false);
458                 pSceneManager->GoBackward(BackwardSceneTransition(),pList);
459         }
460         else
461         {
462                 pSceneManager->GoBackward(BackwardSceneTransition());
463         }
464         AppLogDebug("EXIT");
465 }
466
467 void
468 AllListEditorPanel::UpdateItem(int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
469 {
470         AppLogDebug("ENTER");
471         if (pItem == null)
472         {
473                 AppLogDebug("pItem is null");
474                 delete __pThumbnail;
475                 __pThumbnail = null;
476                 return;
477         }
478
479         Label* pThumbnailLabel = static_cast<Label*>(pItem->GetControl(IDC_CONTENTS_THUMBNAIL, true));
480         if (__pThumbnail == null || pThumbnailLabel == null)
481         {
482                 AppLogDebug("__pThumbnail or pThumbnailLabel is null");
483                 delete __pThumbnail;
484                 __pThumbnail = null;
485                 return;
486         }
487         pThumbnailLabel->SetBackgroundBitmap(*__pThumbnail);
488         delete __pThumbnail;
489         __pThumbnail = null;
490         pThumbnailLabel->Invalidate(true);
491         AppLogDebug("EXIT");
492 }
493
494 bool
495 AllListEditorPanel::IsEmptyContentList(void)
496 {
497         AppLogDebug("ENTER");
498         if (__pPresentationModel->GetTotalCount() == INIT_VALUE)
499         {
500                 AppLogDebug("EXIT");
501                 return true;
502         }
503         AppLogDebug("EXIT");
504         return false;
505 }
506
507 Tizen::Base::Collection::IList*
508 AllListEditorPanel::GetPickerArgumentListN(PickerType pickerType, PickerArgumentType argumentType)
509 {
510         AppLogDebug("ENTER");
511         ArrayList* pContentPathList = new (std::nothrow) ArrayList(SingleObjectDeleter);
512         result r = pContentPathList->Construct();
513         if (IsFailed(r))
514         {
515                 AppLogDebug("pContentList->Construct failed(%s)", GetErrorMessage(r));
516                 delete pContentPathList;
517                 return null;
518         }
519
520         if (pickerType == PICKER_TYPE_PLAY_LIST_PICKER && argumentType == PICKER_ARGUMENT_TYPE_CHECKED_ITEM_ALL)
521         {
522                 int totalCount = __pPresentationModel->GetTotalCount();
523                 for (int iCount = 0; iCount < totalCount; iCount++)
524                 {
525                         if (__pContentTableView->IsItemChecked(iCount) == true)
526                         {
527                                 pContentPathList->Add(__pPresentationModel->GetCheckedContent(iCount));
528                         }
529                 }
530         }
531         else if (pickerType == PICKER_TYPE_SHARE_PICKER && argumentType == PICKER_ARGUMENT_TYPE_CHECKED_ITEM_ALL)
532         {
533                 int totalCount = __pPresentationModel->GetTotalCount();
534                 for (int iCount = 0; iCount < totalCount; iCount++)
535                 {
536                         if (__pContentTableView->IsItemChecked(iCount) == true)
537                         {
538                                 ContentInformation* pContentInfo = __pPresentationModel->GetContentInfoN(iCount);
539                                 if (pContentInfo == null)
540                                 {
541                                         pContentPathList->RemoveAll(true);
542                                         delete pContentPathList;
543
544                                         AppLogDebug("EXIT");
545                                         return null;
546                                 }
547                                 pContentPathList->Add(*(new (std::nothrow) String(pContentInfo->ContentFilePath)));
548                                 delete pContentInfo;
549                         }
550                 }
551         }
552         else
553         {
554                 AppLogDebug("EXIT");
555                 delete pContentPathList;
556                 return null;
557         }
558
559         AppLogDebug("EXIT");
560         return pContentPathList;
561 }