Fixed jira issue and fixed prevent issue
[apps/osp/MusicPlayer.git] / src / MpPlaylistListEditorPanel.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                MpPlaylistListEditorPanel.cpp
19  * @brief               This is the implementation file for PlaylistListEditorPanel class.
20  */
21
22 #include "MpPlaylistEditorPresentationModel.h"
23 #include "MpPlaylistListEditorPanel.h"
24 #include "MpPlaylistPickerPopup.h"
25 #include "MpSharePopup.h"
26 #include "MpThumbnailInfo.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Content;
31 using namespace Tizen::Graphics;
32 using namespace Tizen::Social;
33 using namespace Tizen::Ui;
34 using namespace Tizen::Ui::Controls;
35 using namespace Tizen::Ui::Scenes;
36
37 static const int IDA_RENAME_EDIT_BUTTON = 101;
38
39 PlaylistListEditorPanel::PlaylistListEditorPanel(void)
40         : ContentEditPanel::ContentEditPanel()
41         , ThumbnailBase::ThumbnailBase()
42         , __selectedItemIndex(0)
43         , __pSelectAllCheckedButton(null)
44         , __pThumbnail(null)
45         , __pPlaylistName(null)
46 {
47         AppLogDebug("ENTER");
48         AppLogDebug("EXIT");
49 }
50
51 PlaylistListEditorPanel::~PlaylistListEditorPanel(void)
52 {
53         AppLogDebug("ENTER");
54         AppLogDebug("EXIT");
55 }
56
57 result
58 PlaylistListEditorPanel::OnInitializing(void)
59 {
60         AppLogDebug("ENTER");
61         __pPresentationModel = PlaylistEditorPresentationModel::GetInstance();
62         SetLayoutFitToForm();
63         AppLogDebug("EXIT");
64         return ThumbnailBase::Construct();
65 }
66
67 result
68 PlaylistListEditorPanel::OnTerminating(void)
69 {
70         AppLogDebug("ENTER");
71         ThumbnailBase::Stop();
72         AppLogDebug("EXIT");
73         return ContentEditPanel::OnTerminating();
74 }
75
76 void
77 PlaylistListEditorPanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
78 {
79         AppLogDebug("ENTER");
80         SceneManager* pSceneManager = SceneManager::GetInstance();
81         AppAssert(pSceneManager);
82
83         int totalCount = __pPresentationModel->GetAllPlayListCount();
84
85         Form* pForm = dynamic_cast<Form*>(GetParent());
86         AppAssert(pForm);
87
88         Footer* pFooter = pForm->GetFooter();
89
90         switch (actionId)
91         {
92         case IDA_HEADER_BUTTON_SELECTE_ALL:
93                 {
94                         if (totalCount == __checkedItemCount)
95                         {
96                                 SetItemCheckedAll(false);
97                         }
98                         else
99                         {
100                                 SetItemCheckedAll(true);
101                         }
102                 }
103                 break;
104
105         case IDA_FOOTER_BUTTON_DELETE:
106                 {
107                         ContentManager contentManager;
108                         contentManager.Construct();
109
110                         int totalCount = __pPresentationModel->GetAllPlayListCount();
111                         ArrayList* pTempList = new (std::nothrow) ArrayList();
112                         pTempList->Construct();
113                         for (int index = totalCount - 1; index >= 0; index--)
114                         {
115                                 if (__pContentTableView->IsItemChecked(index) == true)
116                                 {
117                                         pTempList->Add(*(new (std::nothrow) String(__pPresentationModel->GetPlayListName(index))));
118                                         __pContentTableView->RefreshItem(index, TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE);
119                                 }
120                         }
121
122                         if (pTempList != null)
123                         {
124                                 for (int i = 0; i < pTempList->GetCount(); i++)
125                                 {
126                                         __pPresentationModel->RemovePlayList(*static_cast<String*>(pTempList->GetAt(i)));
127                                 }
128                         }
129
130                         __checkedItemCount = INIT_VALUE;
131                         CommonUtil::SetFooterItemEnabled(*pFooter, false);
132                         UpdateContentEditScreenState();
133                 }
134                 break;
135
136         case IDA_RENAME_EDIT_BUTTON:
137                 {
138                         Integer::Parse(source.GetName(), __selectedItemIndex);
139                         AppLogDebug("%d", __selectedItemIndex);
140                         ArrayList* pData = new (std::nothrow) ArrayList();
141                         pData->Construct();
142                         pData->Add(*(new (std::nothrow) Integer(__selectedItemIndex)));
143                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_PLAYLIST_RENAME),pData);
144                 }
145                 break;
146
147         default:
148                 break;
149         }
150         AppLogDebug("EXIT");
151 }
152
153 void
154 PlaylistListEditorPanel::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
155                                                                                 const Tizen::Ui::Scenes::SceneId& currentSceneId,
156                                                                                 Tizen::Base::Collection::IList* pArgs)
157 {
158         AppLogDebug("ENTER");
159         ContentPanel::OnSceneActivatedN(previousSceneId, currentSceneId, null);
160
161         Form* pForm = dynamic_cast<Form*>(GetParent());
162         AppAssert(pForm);
163
164         SetEditHeaderStyle();
165         pForm->SetFormBackEventListener(this);
166
167         Footer* pFooter = CommonUtil::CreateBackButtonStyleFooter(*pForm, STYLE_DELETE_ADD);
168         pFooter->AddActionEventListener(*this);
169         pFooter->SetShowState(true);
170         CommonUtil::SetFooterItemEnabled(*pFooter, false);
171
172         UpdateContentEditScreenState();
173
174         if (pArgs != null)
175         {
176                 pArgs->RemoveAll(true);
177                 delete pArgs;
178         }
179         AppLogDebug("EXIT");
180 }
181
182 void
183 PlaylistListEditorPanel::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
184                                                         const Tizen::Ui::Scenes::SceneId& nextSceneId)
185 {
186         AppLogDebug("ENTER");
187         ContentPanel::OnSceneDeactivated(currentSceneId, nextSceneId);
188
189         Footer* pFooter = GetFooter();
190         pFooter->SetItemEnabled(0, true);
191
192         pFooter->RemoveAllButtons();
193         pFooter->RemoveAllItems();
194         pFooter->RemoveActionEventListener(*this);
195
196         __checkedItemCount = INIT_VALUE;
197         SetCheckedCountBallonTooltip(__checkedItemCount);
198
199         int totalCount = __pPresentationModel->GetAllPlayListCount();
200         for (int iCount = 0; iCount < totalCount; iCount++)
201         {
202                 __pContentTableView->SetItemChecked(iCount, false);
203         }
204         CancelAllThumbnailRequest();
205         AppLogDebug("EXIT");
206 }
207
208 int
209 PlaylistListEditorPanel::GetItemCount(void)
210 {
211         AppLogDebug("ENTER");
212         if (__pPresentationModel == null)
213         {
214                 AppLogDebug("EXIT");
215                 return 0;
216         }
217
218         AppLogDebug("EXIT");
219         return __pPresentationModel->GetAllPlayListCount();
220 }
221
222 Tizen::Ui::Controls::TableViewItem*
223 PlaylistListEditorPanel::CreateItem(const int itemIndex, int itemWidth)
224 {
225         AppLogDebug("ENTER");
226         RelativeLayout layout;
227         layout.Construct();
228
229         TableViewItem* pItem = new (std::nothrow) TableViewItem();
230         Tizen::Base::String playlistName = __pPresentationModel->GetPlayListName(itemIndex);
231         Panel* pPanel = null;
232
233         result r = pItem->Construct(layout, Dimension(itemWidth, ITEM_HEIGHT), TABLE_VIEW_ANNEX_STYLE_MARK);
234         TryCatch(r == E_SUCCESS, delete pItem, "pItem->Construct(%s)", GetErrorMessage(r));
235
236         r = CreateTableViewItem(*pItem, playlistName, itemIndex);
237         TryCatch(r == E_SUCCESS, delete pItem, "CreateTableViewItem failed(%s)", GetErrorMessage(r));
238
239         pPanel = static_cast<Panel*>(pItem->GetControl(IDL_CONTENTS_PLAYLIST_EDITOR_ITEM_PANEL));
240         if (__pPresentationModel->GetPlayListItemCount(playlistName) > 0 && !IsFailed(GetLastResult()))
241         {
242                 static_cast<Label*>(pPanel->GetControl(IDC_CONTENTS_THUMBNAIL))->SetBackgroundBitmap(*GetDefaultThumbnail());
243                 RequestThumbnail(__pPresentationModel->GetPlayListItemContentId(playlistName), (new (std::nothrow) Integer(itemIndex)));
244         }
245         else
246         {
247                 Bitmap* thumbnail = (ResourceManager::GetBitmapN(L"34_player_list_thum_noitems.png"));
248                 thumbnail->Scale(Dimension(W_THUMBNAIL, H_THUMBNAIL));
249                 static_cast<Label*>(pPanel->GetControl(IDC_CONTENTS_THUMBNAIL))->SetBackgroundBitmap(*thumbnail);
250                 delete thumbnail;
251         }
252
253         AppLogDebug("EXIT");
254         return pItem;
255
256 CATCH:
257         AppLogDebug("EXIT(%ls)", GetErrorMessage(GetLastResult()));
258         return null;
259 }
260
261 void
262 PlaylistListEditorPanel::UpdateItem(int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
263 {
264         AppLogDebug("ENTER");
265         Panel* pItemPanel =  static_cast<Panel*>(pItem->GetControl(IDL_CONTENTS_PLAYLIST_EDITOR_ITEM_PANEL));
266         if (pItemPanel == null)
267         {
268                 AppLogDebug("pItemPanel is null");
269                 delete __pThumbnail;
270                 __pThumbnail = null;
271                 return;
272         }
273
274         Label* pThumbnailLabel = static_cast<Label*>(pItemPanel->GetControl(IDC_CONTENTS_THUMBNAIL));
275         if (__pThumbnail == null || pThumbnailLabel == null)
276         {
277                 AppLogDebug("__pThumbnail or pThumbnailLabel is null");
278                 delete __pThumbnail;
279                 __pThumbnail = null;
280                 return;
281         }
282         pThumbnailLabel->SetBackgroundBitmap(*__pThumbnail);
283         delete __pThumbnail;
284         __pThumbnail = null;
285         pThumbnailLabel->Invalidate(true);
286
287         AppLogDebug("EXIT");
288 }
289
290 void
291 PlaylistListEditorPanel::OnTableViewItemStateChanged(Tizen::Ui::Controls::TableView& tableView,
292                                                                 int itemIndex,
293                                                                 Tizen::Ui::Controls::TableViewItem* pItem,
294                                                                 Tizen::Ui::Controls::TableViewItemStatus status)
295 {
296         AppLogDebug("ENTER");
297         Form* pForm = dynamic_cast<Form*>(GetParent());
298         AppAssert(pForm);
299         Footer* pFooter = pForm->GetFooter();
300         __selectedItemIndex = itemIndex;
301
302         switch (status)
303         {
304         case TABLE_VIEW_ITEM_STATUS_CHECKED:
305                 {
306                         __checkedItemCount++;
307                         tableView.SetItemChecked(itemIndex, true);
308                 }
309                 break;
310
311         case TABLE_VIEW_ITEM_STATUS_UNCHECKED:
312                 {
313                         if (__checkedItemCount > INIT_VALUE)
314                         {
315                                 __checkedItemCount--;
316                         }
317                         tableView.SetItemChecked(itemIndex, false);
318                 }
319                 break;
320
321         default:
322                 break;
323         }
324
325         if (__checkedItemCount > INIT_VALUE)
326         {
327                 CommonUtil::SetFooterItemEnabled(*pFooter, true);
328         }
329         else
330         {
331                 CommonUtil::SetFooterItemEnabled(*pFooter, false);
332         }
333
334         SetCheckedCountBallonTooltip(__checkedItemCount);
335         Invalidate(true);
336         AppLogDebug("EXIT");
337 }
338
339 void
340 PlaylistListEditorPanel::UpdateContentList(void)
341 {
342         AppLogDebug( "ENTER");
343         AppLogDebug( "EXIT");
344 }
345
346 void
347 PlaylistListEditorPanel::SetEditHeaderStyle(void)
348 {
349         AppLogDebug("ENTER");
350         Header* pHeader = GetHeader();
351         String titleText(ResourceManager::GetString(L"IDS_COM_BODY_EDIT"));
352         CommonUtil::SetEditHeaderStyle(*pHeader, titleText);
353         pHeader->Invalidate(true);
354         AppLogDebug("EXIT");
355 }
356
357 void
358 PlaylistListEditorPanel::OnThumbnailInfoReveivedN(ThumbnailInfo* pThumbnailInfo)
359 {
360         AppLogDebug("ENTER");
361         __pThumbnail = pThumbnailInfo->GetBitmapN();
362         Object* pParam = pThumbnailInfo->GetUserParamN();
363         if (pParam != null)
364         {
365                 __pContentTableView->RefreshItem((static_cast<Integer*>(pParam))->ToInt(), TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
366                 delete pParam;
367         }
368
369         delete pThumbnailInfo;
370         AppLogDebug("EXIT");
371 }
372
373 void
374 PlaylistListEditorPanel::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
375 {
376         AppLogDebug("ENTER");
377         SceneManager* pSceneManager = SceneManager::GetInstance();
378         AppAssert(pSceneManager);
379
380         pSceneManager->GoBackward(BackwardSceneTransition());
381         AppLogDebug("EXIT");
382 }
383
384 result
385 PlaylistListEditorPanel::CreateTableViewItem(Tizen::Ui::Controls::TableViewItem& parent, const Tizen::Base::String& title, int itemIndex)
386 {
387         AppLogDebug("ENTER");
388         Panel* pTableViewItem = new (std::nothrow) Panel();
389         if (IsFailed(pTableViewItem->Construct(IDL_CONTENTS_PLAYLIST_EDITOR_ITEM_PANEL)))
390         {
391                 AppLogDebug("Construct(IDL_CONTENTS_LIBARY_EDITOR_ITEM_PANEL) failed(%s)", GetErrorMessage(GetLastResult()));
392                 return E_FAILURE;
393         }
394
395         static_cast<Label*>(pTableViewItem->GetControl(IDC_CONTENTS_TITLE_NAME))->SetText(title);
396
397         String str = L"";
398         str.Append(itemIndex);
399
400         Button* pEditButton = static_cast<Button*>(pTableViewItem->GetControl(IDC_BUTTON_EDIT));
401         pEditButton->SetName(str);
402
403         Bitmap* pNormalBitmap = ResourceManager::GetBitmapN(L"00_button_rename_normal.png");
404         Bitmap* pPressedBitmap = ResourceManager::GetBitmapN(L"00_button_rename_press.png");
405         Bitmap* pBackgroundNormalBitmap = ResourceManager::GetBitmapN(L"00_btn_circle_bg_normal.png");
406         Bitmap* pBackgroundPressBitmap = ResourceManager::GetBitmapN(L"00_btn_circle_bg_press.png");
407
408         pEditButton->SetNormalBitmap(Point(0, 0), *pNormalBitmap);
409         pEditButton->SetNormalBackgroundBitmap(*pBackgroundNormalBitmap);
410         pEditButton->SetPressedBitmap(Point(0, 0), *pPressedBitmap);
411         pEditButton->SetPressedBackgroundBitmap(*pBackgroundPressBitmap);
412
413         pEditButton->SetActionId(IDA_RENAME_EDIT_BUTTON);
414         pEditButton->AddActionEventListener(*this);
415
416         delete pNormalBitmap;
417         delete pPressedBitmap;
418         delete pBackgroundNormalBitmap;
419         delete pBackgroundPressBitmap;
420
421         parent.AddControl(*pTableViewItem);
422         parent.SetIndividualSelectionEnabled(pEditButton, true);
423         CommonUtil::SetLayoutFitToContainer(parent, *pTableViewItem);
424
425         AppLogDebug("EXIT");
426         return E_SUCCESS;
427 }
428
429 bool
430 PlaylistListEditorPanel::IsEmptyContentList(void)
431 {
432         AppLogDebug("ENTER");
433         if (__pPresentationModel->GetAllPlayListCount() == INIT_VALUE)
434         {
435                 AppLogDebug("EXIT");
436                 return true;
437         }
438         AppLogDebug("EXIT");
439         return false;
440 }