Fixed jira issue and fixed prevent issue
[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.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                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         UpdateContentList();
58         SetLayoutFitToForm();
59         AppLogDebug("EXIT");
60         return ThumbnailBase::Construct();
61 }
62
63 result
64 AllListEditorPanel::OnTerminating(void)
65 {
66         AppLogDebug("ENTER");
67         ThumbnailBase::Stop();
68         AppLogDebug("EXIT");
69         return ContentEditPanel::OnTerminating();
70 }
71
72 void
73 AllListEditorPanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
74 {
75         AppLogDebug("ENTER");
76         if (__pPresentationModel == null)
77         {
78                 return;
79         }
80
81         int totalCount = __pPresentationModel->GetTotalCount();
82
83         switch (actionId)
84         {
85         case IDA_HEADER_BUTTON_SELECTE_ALL:
86                 {
87                         if (totalCount == __checkedItemCount)
88                         {
89                                 SetItemCheckedAll(false);
90                         }
91                         else
92                         {
93                                 SetItemCheckedAll(true);
94                         }
95                 }
96                 break;
97
98         case IDA_FOOTER_BUTTON_DELETE:
99                 {
100                         int totalCount = __pPresentationModel->GetTotalCount();
101                         for (int iCount = totalCount - 1; iCount >= 0; iCount--)
102                         {
103                                 if (__pContentTableView->IsItemChecked(iCount) == true)
104                                 {
105                                         __pPresentationModel->RemoveContent(iCount);
106                                 }
107                         }
108                 }
109                 break;
110
111         case IDA_FOOTER_BUTTON_ADD_TO_PLAYLIST:
112                 {
113                         LanucherPicker(PICKER_TYPE_PLAY_LIST_PICKER);
114                 }
115                 break;
116
117         case IDA_FOOTER_BUTTON_SHARE:
118                 {
119                         LanucherPicker(PICKER_TYPE_SHARE_PICKER);
120                 }
121                 break;
122
123         default:
124                 break;
125         }
126         AppLogDebug("EXIT");
127 }
128
129 void
130 AllListEditorPanel::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
131                                                                         const Tizen::Ui::Scenes::SceneId& currentSceneId,
132                                                                         Tizen::Base::Collection::IList* pArgs)
133 {
134         AppLogDebug("ENTER");
135         ContentPanel::OnSceneActivatedN(previousSceneId, currentSceneId, null);
136
137         Form* pForm = dynamic_cast<Form*>(GetParent());
138         Footer* pFooter = null;
139         AppAssert(pForm);
140
141         pForm->SetFormBackEventListener(this);
142         if (pArgs != null && (previousSceneId.Equals(IDSCN_ALL_LIST, true)))
143         {
144                 __editType = static_cast<Integer*>(pArgs->GetAt(0))->ToInt();
145         }
146
147         SetEditHeaderStyle();
148
149         if (__editType == IDA_CONTEXT_MENU_ITEM_EDIT)
150         {
151                 pFooter = CommonUtil::CreateBackButtonStyleFooter(*pForm, STYLE_ADDTO_DELETE_ADD);
152         }
153         else
154         {
155                 pFooter = CommonUtil::CreateBackButtonStyleFooter(*pForm, STYLE_SHARE_ADD);
156         }
157         pFooter->AddActionEventListener(*this);
158         pFooter->SetShowState(true);
159         CommonUtil::SetFooterItemEnabled(*pFooter, false);
160
161         UpdateContentEditScreenState();
162
163         if (pArgs != null)
164         {
165                 pArgs->RemoveAll(true);
166                 delete pArgs;
167         }
168         AppLogDebug("EXIT");
169 }
170
171 void
172 AllListEditorPanel::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
173                                                                                 const Tizen::Ui::Scenes::SceneId& nextSceneId)
174 {
175         AppLogDebug("ENTER");
176         ContentPanel::OnSceneDeactivated(currentSceneId, nextSceneId);
177
178         Footer* pFooter = GetFooter();
179         pFooter->RemoveAllButtons();
180         pFooter->RemoveAllItems();
181         pFooter->RemoveActionEventListener(*this);
182
183         __checkedItemCount = INIT_VALUE;
184         SetCheckedCountBallonTooltip(__checkedItemCount);
185
186         int totalCount = __pPresentationModel->GetTotalCount();
187         for (int iCount = 0; iCount < totalCount; iCount++)
188         {
189                 __pContentTableView->SetItemChecked(iCount, false);
190         }
191         CancelAllThumbnailRequest();
192         AppLogDebug("EXIT");
193 }
194
195 int
196 AllListEditorPanel::GetItemCount(void)
197 {
198         AppLogDebug("ENTER");
199         int musicItem = INIT_VALUE;
200         if (__pPresentationModel == null)
201         {
202                 return musicItem;
203         }
204
205         musicItem = __pPresentationModel->GetTotalCount();
206         AppLogDebug("EXIT");
207         return musicItem;
208 }
209
210 Tizen::Ui::Controls::TableViewItem*
211 AllListEditorPanel::CreateItem(const int itemIndex, int itemWidth)
212 {
213         AppLogDebug("ENTER");
214         RelativeLayout layout;
215         layout.Construct();
216
217         TableViewItem* pItem = new (std::nothrow) TableViewItem();
218         ContentInformation* pContentInfo = __pPresentationModel->GetContentInfoN(itemIndex);
219
220         result r = pItem->Construct(layout, Dimension(itemWidth, ITEM_HEIGHT), TABLE_VIEW_ANNEX_STYLE_MARK);
221         TryCatch(r == E_SUCCESS, delete pItem, "pItem->Construct(%s)", GetErrorMessage(r));
222         TryCatch(pContentInfo != null, delete pItem, "pContentInfo is null", GetErrorMessage(GetLastResult()));
223
224         r = CreateTableViewItem(*pItem, *pContentInfo);
225         TryCatch(r == E_SUCCESS, delete pItem, "CreateTableViewItem failed(%s)", GetErrorMessage(r));
226
227         RequestThumbnail(pContentInfo->contentId, (new (std::nothrow) Integer(itemIndex)));
228         delete pContentInfo;
229
230         AppLogDebug("EXIT");
231         return pItem;
232
233 CATCH:
234         AppLogDebug("EXIT(%ls)", GetErrorMessage(GetLastResult()));
235         delete pContentInfo;
236         return null;
237 }
238
239 void
240 AllListEditorPanel::OnTableViewItemStateChanged(Tizen::Ui::Controls::TableView& tableView,
241                                                         int itemIndex,
242                                                         Tizen::Ui::Controls::TableViewItem* pItem,
243                                                         Tizen::Ui::Controls::TableViewItemStatus status)
244 {
245         AppLogDebug("ENTER");
246         Form* pForm = dynamic_cast<Form*>(GetParent());
247         AppAssert(pForm);
248
249         Footer* pFooter = pForm->GetFooter();
250
251         switch (status)
252         {
253         case TABLE_VIEW_ITEM_STATUS_CHECKED:
254                 {
255                         __checkedItemCount++;
256                         tableView.SetItemChecked(itemIndex, true);
257                 }
258                 break;
259
260         case TABLE_VIEW_ITEM_STATUS_UNCHECKED:
261                 {
262                         __checkedItemCount--;
263                         tableView.SetItemChecked(itemIndex, false);
264                 }
265                 break;
266
267         default:
268                 break;
269         }
270
271         if (__checkedItemCount > INIT_VALUE)
272         {
273                 CommonUtil::SetFooterItemEnabled(*pFooter, true);
274         }
275         else
276         {
277                 CommonUtil::SetFooterItemEnabled(*pFooter, false);
278         }
279
280         SetCheckedCountBallonTooltip(__checkedItemCount);
281         Invalidate(true);
282         AppLogDebug("EXIT");
283 }
284
285 result
286 AllListEditorPanel::CreateTableViewItem(Tizen::Ui::Container& parent,
287                                                         const ContentInformation& contentInfoStruct)
288 {
289         AppLogDebug("ENTER");
290         Panel* pTableViewItem =  new (std::nothrow) Panel();
291         if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL)))
292         {
293                 AppLogDebug("Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
294                 return E_FAILURE;
295         }
296
297         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_TITLE_NAME))->SetText(contentInfoStruct.TitleName);
298         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_ARTIST_NAME))->SetText(contentInfoStruct.ArtistName);
299         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_THUMBNAIL))->SetBackgroundBitmap(*GetDefaultThumbnail());
300
301         parent.AddControl(*pTableViewItem);
302         CommonUtil::SetLayoutFitToContainer(parent, *pTableViewItem);
303
304         AppLogDebug("EXIT");
305         return E_SUCCESS;
306 }
307
308 void
309 AllListEditorPanel::UpdateContentList(void)
310 {
311         AppLogDebug("ENTER");
312         if (__pPresentationModel != null)
313         {
314                 __pPresentationModel->UpdateContentList();
315         }
316         AppLogDebug("EXIT");
317 }
318
319 void
320 AllListEditorPanel::SetEditHeaderStyle(void)
321 {
322         AppLogDebug("ENTER");
323         Header* pHeader = GetHeader();
324         if(__editType == IDA_CONTEXT_MENU_ITEM_EDIT)
325         {
326                 String titleText(ResourceManager::GetString(L"IDS_COM_BODY_EDIT"));
327                 CommonUtil::SetEditHeaderStyle(*pHeader, titleText);
328         }
329         else
330         {
331                 String titleText(ResourceManager::GetString(L"IDS_IV_BODY_SHARE_VIA"));
332                 CommonUtil::SetEditHeaderStyle(*pHeader, titleText);
333         }
334         pHeader->Invalidate(true);
335         AppLogDebug("EXIT");
336 }
337
338 void
339 AllListEditorPanel::OnThumbnailInfoReveivedN(ThumbnailInfo* pThumbnailInfo)
340 {
341         AppLogDebug("ENTER");
342         __pThumbnail = pThumbnailInfo->GetBitmapN();
343         Object* pParam = pThumbnailInfo->GetUserParamN();
344         if (pParam != null)
345         {
346                 __pContentTableView->RefreshItem((static_cast<Integer*>(pParam))->ToInt(), TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
347                 delete pParam;
348         }
349
350         delete pThumbnailInfo;
351         AppLogDebug("EXIT");
352 }
353
354 void
355 AllListEditorPanel::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
356 {
357         AppLogDebug("ENTER");
358         SceneManager* pSceneManager = SceneManager::GetInstance();
359         AppAssert(pSceneManager);
360         if (CommonUtil::GetAddtoPlaylistState())
361         {
362                 CommonUtil::SetAddtoPlaylistState(false);
363                 pSceneManager->GoBackward(BackwardSceneTransition());
364         }
365         else
366         {
367                 pSceneManager->GoBackward(BackwardSceneTransition());
368         }
369         AppLogDebug("EXIT");
370 }
371
372 void
373 AllListEditorPanel::UpdateItem(int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
374 {
375         AppLogDebug("ENTER");
376         Panel* pItemPanel =  static_cast<Panel*>(pItem->GetControl(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL));
377         if (pItemPanel == null)
378         {
379                 AppLogDebug("pItemPanel is null");
380                 delete __pThumbnail;
381                 __pThumbnail = null;
382                 return;
383         }
384
385         Label* pThumbnailLabel = static_cast<Label*>(pItemPanel->GetControl(IDC_CONTENTS_THUMBNAIL));
386         if (__pThumbnail == null || pThumbnailLabel == null)
387         {
388                 AppLogDebug("__pThumbnail or pThumbnailLabel is null");
389                 delete __pThumbnail;
390                 __pThumbnail = null;
391                 return;
392         }
393         pThumbnailLabel->SetBackgroundBitmap(*__pThumbnail);
394         delete __pThumbnail;
395         __pThumbnail = null;
396         pThumbnailLabel->Invalidate(true);
397         AppLogDebug("EXIT");
398 }
399
400 bool
401 AllListEditorPanel::IsEmptyContentList(void)
402 {
403         AppLogDebug("ENTER");
404         if (__pPresentationModel->GetTotalCount() == INIT_VALUE)
405         {
406                 AppLogDebug("EXIT");
407                 return true;
408         }
409         AppLogDebug("EXIT");
410         return false;
411 }
412
413 Tizen::Base::Collection::IList*
414 AllListEditorPanel::GetPickerArgumentListN(PickerType pickerType, PickerArgumentType argumentType)
415 {
416         AppLogDebug("ENTER");
417         ArrayList* pContentPathList = new (std::nothrow) ArrayList(SingleObjectDeleter);
418         result r = pContentPathList->Construct();
419         if (IsFailed(r))
420         {
421                 AppLogDebug("pContentList->Construct failed(%s)", GetErrorMessage(r));
422                 delete pContentPathList;
423                 return null;
424         }
425
426         if (pickerType == PICKER_TYPE_PLAY_LIST_PICKER && argumentType == PICKER_ARGUMENT_TYPE_CHECKED_ITEM_ALL)
427         {
428                 int totalCount = __pPresentationModel->GetTotalCount();
429                 for (int iCount = 0; iCount < totalCount; iCount++)
430                 {
431                         if (__pContentTableView->IsItemChecked(iCount) == true)
432                         {
433                                 pContentPathList->Add(__pPresentationModel->GetCheckedContent(iCount));
434                         }
435                 }
436         }
437         else if (pickerType == PICKER_TYPE_SHARE_PICKER && argumentType == PICKER_ARGUMENT_TYPE_CHECKED_ITEM_ALL)
438         {
439                 int totalCount = __pPresentationModel->GetTotalCount();
440                 for (int iCount = 0; iCount < totalCount; iCount++)
441                 {
442                         if (__pContentTableView->IsItemChecked(iCount) == true)
443                         {
444                                 ContentInformation* pContentInfo = __pPresentationModel->GetContentInfoN(iCount);
445                                 if (pContentInfo == null)
446                                 {
447                                         pContentPathList->RemoveAll(true);
448                                         delete pContentPathList;
449
450                                         AppLogDebug("EXIT");
451                                         return null;
452                                 }
453                                 pContentPathList->Add(*(new (std::nothrow) String(L"attachment:" + pContentInfo->ContentFilePath)));
454                                 delete pContentInfo;
455                         }
456                 }
457         }
458         else
459         {
460                 AppLogDebug("EXIT");
461                 delete pContentPathList;
462                 return null;
463         }
464
465         AppLogDebug("EXIT");
466         return pContentPathList;
467 }