Fixed jira issues : N_SE-56423, N_SE-56330, N_SE-56326
[apps/osp/MusicPlayer.git] / src / MpPlaylistCreatorForm.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                MpPlaylistCreatorForm.cpp
19  * @brief               This is the implementation file for PlaylistCreatorForm class.
20  */
21
22 #include "MpAllListPresentationModel.h"
23 #include "MpAlbumListPresentationModel.h"
24 #include "MpArtistListPresentationModel.h"
25 #include "MpFolderListPresentationModel.h"
26 #include "MpCommonUtil.h"
27 #include "MpPlaylistCreatorForm.h"
28 #include "MpPlaylistEditorPresentationModel.h"
29 #include "MpPlaylistListPresentationModel.h"
30 #include "MpResourceManager.h"
31 #include "MpSceneRegister.h"
32 #include "MpTypes.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Content;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Media;
39 using namespace Tizen::Ui;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Ui::Scenes;
42
43 PlaylistCreatorForm::PlaylistCreatorForm(void)
44         : __pPlaylistEditorPresentationModel(null)
45         , __pPlaylistEditField(null)
46         , __pDataList(null)
47         , __previousSceneId(L"")
48 {
49         AppLogDebug("ENTER");
50         AppLogDebug("EXIT");
51 }
52
53 PlaylistCreatorForm::~PlaylistCreatorForm(void)
54 {
55         AppLogDebug("ENTER");
56         AppLogDebug("EXIT");
57 }
58
59 void
60 PlaylistCreatorForm::Initialize(void)
61 {
62         AppLogDebug("ENTER");
63         Form::Construct(IDL_PLAYLIST_CREATOR_FORM);
64         AppLogDebug("EXIT");
65 }
66
67 result
68 PlaylistCreatorForm::OnInitializing(void)
69 {
70         AppLogDebug("ENTER");
71         __pPlaylistEditField = static_cast<EditField*>(GetControl(IDC_EDITFIELD));
72         __pPlaylistEditField->AddTextEventListener(*this);
73         __pPlaylistEditField->AddKeypadEventListener(*this);
74         __pPlaylistEditField->SetOverlayKeypadCommandButtonVisible(false);
75         __pPlaylistEditField->SetFocus();
76         __pPlaylistEditorPresentationModel = PlaylistEditorPresentationModel::GetInstance();
77
78         SetFormBackEventListener(this);
79         if (GetFooter() != null)
80         {
81                 GetFooter()->AddActionEventListener(*this);
82         }
83         AppLogDebug("EXIT");
84         return E_SUCCESS;
85 }
86
87 result
88 PlaylistCreatorForm::OnTerminating(void)
89 {
90         AppLogDebug("ENTER");
91         if (__pDataList != null)
92         {
93                 __pDataList->RemoveAll(true);
94                 delete __pDataList;
95         }
96         AppLogDebug("EXIT");
97         return E_SUCCESS;
98 }
99
100 void
101 PlaylistCreatorForm::OnKeypadActionPerformed(Tizen::Ui::Control& source,
102                                                         Tizen::Ui::KeypadAction keypadAction)
103 {
104         AppLogDebug("ENTER");
105         AppLogDebug("EXIT");
106 }
107
108 void
109 PlaylistCreatorForm::OnKeypadClosed(Tizen::Ui::Control& source)
110 {
111         AppLogDebug("ENTER");
112         AppLogDebug("EXIT");
113 }
114
115 void
116 PlaylistCreatorForm::OnKeypadOpened(Tizen::Ui::Control& source)
117 {
118         AppLogDebug("ENTER");
119         AppLogDebug("EXIT");
120 }
121
122 void
123 PlaylistCreatorForm::OnKeypadWillOpen(Tizen::Ui::Control &source)
124 {
125         AppLogDebug("ENTER");
126         AppLogDebug("EXIT");
127 }
128
129 void
130 PlaylistCreatorForm::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         String PlayListName = ResourceManager::GetString(L"IDS_MP_HEADER_MENU_PLAYLIST");
136         PlayListName.Append(L"_");
137         ArrayList* pNumberList = new (std::nothrow) ArrayList();
138         pNumberList->Construct();
139
140         int numericPlaylist = 1;
141         int totalPlaylistCount = __pPlaylistEditorPresentationModel->GetAllPlayListCount();
142         __previousSceneId = previousSceneId;
143         AppLogDebug("%d", totalPlaylistCount);
144
145         if (totalPlaylistCount != 0)
146         {
147                 int lastNum = 0;
148                 for (int i = 0; i < totalPlaylistCount; i++)
149                 {
150                         String currentPlayListName = __pPlaylistEditorPresentationModel->GetPlayListName(i);
151                         if (currentPlayListName.GetLength() >= PlayListName.GetLength())
152                         {
153                                 String compareStr;
154                                 currentPlayListName.SubString(0, PlayListName.GetLength(), compareStr);
155                                 if (compareStr.Equals(PlayListName, true))
156                                 {
157                                         String pLastStr;
158                                         wchar_t ch;
159                                         for (int index = currentPlayListName.GetLength()-1; index >= 0; index--)
160                                         {
161                                                 currentPlayListName.GetCharAt(index, ch);
162                                                 if (Character::IsDigit(ch))
163                                                 {
164                                                         continue;
165                                                 }
166                                                 else
167                                                 {
168                                                         currentPlayListName.SubString(index + 1, currentPlayListName.GetLength() - (index + 1), pLastStr);
169                                                         break;
170                                                 }
171                                         }
172
173                                         if (pLastStr != null)
174                                         {
175                                                 Integer::Parse(pLastStr, lastNum);
176                                                 pNumberList->Add((new (std::nothrow) Integer(lastNum)));
177                                         }
178                                 }
179                         }
180                 }
181                 if (pNumberList != null)
182                 {
183                         int pNumberListCount = pNumberList->GetCount();
184                         IntegerComparer integerComparer;
185                         pNumberList->Sort(integerComparer);
186                         for (int index = 0; index < pNumberListCount; index++)
187                         {
188                                 if (static_cast<Integer*>(pNumberList->GetAt(0))->ToInt() != 1)
189                                 {
190                                         break;
191                                 }
192
193                                 int firstNumber = 0;
194                                 int secondNumber = 0;
195
196                                 firstNumber = static_cast<Integer*>(pNumberList->GetAt(index))->ToInt();
197                                 if (index + 1 < pNumberListCount)
198                                 {
199                                         secondNumber = static_cast<Integer*>(pNumberList->GetAt(index + 1))->ToInt();
200                                 }
201
202                                 if (firstNumber + 1 < secondNumber)
203                                 {
204                                         numericPlaylist = firstNumber + 1;
205                                         break;
206                                 }
207                                 else
208                                 {
209                                         numericPlaylist = firstNumber + 1;
210                                 }
211                         }
212                 }
213                 PlayListName.Append(numericPlaylist);
214                 if (__pPlaylistEditField->GetTextLength() == 0)
215                 {
216                         __pPlaylistEditField->SetText(PlayListName);
217                 }
218         }
219         else
220         {
221                 PlayListName.Append(numericPlaylist);
222                 if (__pPlaylistEditField->GetTextLength() == 0)
223                 {
224                         __pPlaylistEditField->SetText(PlayListName);
225                 }
226         }
227
228         __pPlaylistEditField->ShowKeypad();
229
230         if (pNumberList != null)
231         {
232                 pNumberList->RemoveAll(true);
233                 delete pNumberList;
234         }
235         if (pArgs != null)
236         {
237                 __pDataList = pArgs;
238         }
239         AppLogDebug("EXIT");
240 }
241
242 void
243 PlaylistCreatorForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
244                                                                 const Tizen::Ui::Scenes::SceneId& nextSceneId)
245 {
246         AppLogDebug("ENTER");
247         AppLogDebug("EXIT");
248 }
249
250 void
251 PlaylistCreatorForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
252 {
253         AppLogDebug("ENTER");
254         SceneManager* pSceneManager = SceneManager::GetInstance();
255         AppAssert(pSceneManager);
256         result r = E_FAILURE;
257         ArrayList* pList = null;
258
259         switch (actionId)
260         {
261         case IDA_FOOTER_BUTTON_SAVE:
262         {
263                 String inputString = __pPlaylistEditField->GetText();
264                 inputString.Trim();
265                 __pPlaylistEditField->HideKeypad();
266                 if (inputString.GetLength() == 0)
267                 {
268                         MessageBox messageBox;
269                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_MUSIC_POP_UNABLE_CREATE_PLAYLIST"), MSGBOX_STYLE_NONE, 3000);
270                         int modalResult = 0;
271                         messageBox.ShowAndWait(modalResult);
272                         __pPlaylistEditField->SetFocus();
273                         __pPlaylistEditField->ShowKeypad();
274                         break;
275                 }
276
277                 r = __pPlaylistEditorPresentationModel->SetPlayListName(L"", inputString);
278                 if (r != E_SUCCESS)
279                 {
280                         MessageBox messageBox;
281                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_MUSIC_POP_UNABLE_CREATE_PLAYLIST"), MSGBOX_STYLE_NONE, 3000);
282                         int modalResult = 0;
283                         messageBox.ShowAndWait(modalResult);
284                         __pPlaylistEditField->SetFocus();
285                         __pPlaylistEditField->ShowKeypad();
286                         break;
287                 }
288
289                 if (__pDataList != null)
290                 {
291                         for (int index = 0; index < __pDataList->GetCount(); index++)
292                         {
293                                 ContentId contentId = CommonUtil::GetContentId(*(static_cast<String*>(__pDataList->GetAt(index))));
294                                 if (!IsFailed(GetLastResult()))
295                                 {
296                                         __pPlaylistEditorPresentationModel->AddItem(contentId, inputString);
297                                         r = GetLastResult();
298                                         if (IsFailed(r))
299                                         {
300                                                 break;
301                                         }
302                                 }
303                                 else
304                                 {
305                                         r = E_FAILURE;
306                                         MessageBox messageBox;
307                                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_MUSIC_POP_UNABLE_TO_ADD"),
308                                                         MSGBOX_STYLE_OK,
309                                                         3000);
310                                         int modalResult = 0;
311                                         messageBox.ShowAndWait(modalResult);
312                                         break;
313                                 }
314                         }
315
316                         if (r == E_SUCCESS)
317                         {
318                                 MessageBox messageBox;
319                                 messageBox.Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_SUCCESS"),
320                                                 MSGBOX_STYLE_OK,
321                                                 3000);
322                                 int modalResult = 0;
323                                 messageBox.ShowAndWait(modalResult);
324                                 if (!__previousSceneId.Equals(IDSCN_PLAYER,true))
325                                 {
326                                         pList = new (std::nothrow) ArrayList();
327                                         pList->Construct();
328                                         pList->Add(new (std::nothrow) Integer(1));
329                                 }
330                         }
331                         else
332                         {
333                                 __pPlaylistEditorPresentationModel->RemovePlayList(inputString);
334                         }
335
336                         if (__previousSceneId.Equals(IDSCN_PLAYER, true))
337                         {
338                                 pSceneManager->GoBackward(BackwardSceneTransition());
339                         }
340                         else
341                         {
342                                 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
343                         }
344                 }
345                 else
346                 {
347                         ArrayList* pList = new (std::nothrow) ArrayList();
348                         pList->Construct();
349                         AllListPresentationModel* pPresentationModel = AllListPresentationModel::GetInstance();
350                         if (pPresentationModel->GetTotalCount() == 0)
351                         {
352                                 pSceneManager->GoBackward(BackwardSceneTransition(), pList);
353                         }
354                         else
355                         {
356                                 CommonUtil::SetAddtoPlaylistState(true);
357                                 CommonUtil::SetAddtoPlaylistName(inputString);
358                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_CONTENT_PICKER,
359                                                 SCENE_TRANSITION_ANIMATION_TYPE_NONE, SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY));
360                         }
361                 }
362         }
363                 break;
364
365         default:
366                 break;
367         }
368         AppLogDebug("EXIT");
369 }
370
371 void
372 PlaylistCreatorForm::OnTextValueChanged(const Tizen::Ui::Control& source)
373 {
374         AppLogDebug("ENTER");
375         Footer* pFooter = GetFooter();
376         EditArea* pEditArea = const_cast<EditArea*>(static_cast<const EditArea*>(&source));
377         String editareaString = pEditArea->GetText();
378         if (editareaString.GetLength() == INIT_VALUE)
379         {
380                 if (pFooter != null)
381                 {
382                         CommonUtil::SetFooterItemEnabled(*pFooter, false);
383                 }
384         }
385         else
386         {
387                 if (pFooter != null)
388                 {
389                         CommonUtil::SetFooterItemEnabled(*pFooter, true);
390                 }
391         }
392         AppLogDebug("EXIT");
393 }
394
395 void
396 PlaylistCreatorForm::OnTextValueChangeCanceled(const Tizen::Ui::Control& source)
397 {
398         AppLogDebug("ENTER");
399         AppLogDebug("EXIT");
400 }
401
402 void
403 PlaylistCreatorForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
404 {
405         AppLogDebug("ENTER");
406         SceneManager* pSceneManager = SceneManager::GetInstance();
407         pSceneManager->GoBackward(BackwardSceneTransition());
408         AppLogDebug("EXIT");
409 }
410
411 void
412 PlaylistCreatorForm::OnMusicContentUpdateCompleted(void)
413 {
414         AppLogDebug("ENTER");
415         int validContents = 0;
416         int totalContents = 0;
417         SceneManager* pSceneManager = SceneManager::GetInstance();
418         
419         if (pSceneManager != null && pSceneManager->GetCurrentSceneId().Equals(IDSCN_PLAYLIST_CREATOR,true))
420         {
421                 if (__pDataList != null && __pDataList->GetCount())
422                 {
423                         totalContents = __pDataList->GetCount();
424
425                         for (int pathIndex = 0; pathIndex < __pDataList->GetCount(); pathIndex++)
426                         {
427                                 ContentId contentId = CommonUtil::GetContentId(*(static_cast<String*>(__pDataList->GetAt(pathIndex))));
428
429                                 if (IsFailed(GetLastResult()))
430                                 {
431                                         continue;
432                                 }
433                                 else
434                                 {
435                                         validContents++;
436                                 }
437                         }
438                 }
439                 else
440                 {
441                         validContents++;
442                 }
443
444                 if ( validContents == 0)
445                 {
446                         IListT<SceneId>* pSceneList = pSceneManager->GetSceneHistoryN();
447                         if(pSceneList != null)
448                         {
449                                 if (pSceneList->GetCount() > 1)
450                                 {
451                                         pSceneManager->ClearSceneHistory();
452                                         for(int sceneIndex = 0; sceneIndex < pSceneList->GetCount(); sceneIndex++)
453                                         {
454                                                 SceneId scenId;
455                                                 pSceneList->GetAt(sceneIndex,scenId);
456
457                                                 if ((sceneIndex == pSceneList->GetCount()-1) && (__previousSceneId.Equals(IDSCN_ALBUM_CONTENT_LIST,true)
458                                                                 || __previousSceneId.Equals(IDSCN_FOLDER_CONTENT_LIST,true)
459                                                                 || __previousSceneId.Equals(IDSCN_ARTIST_CONTENT_LIST,true)))
460                                                 {
461                                                         pSceneManager->DestroyScene(scenId);
462                                                 }
463                                                 else if ((sceneIndex == pSceneList->GetCount()-1)
464                                                                 && (__previousSceneId.Equals(IDSCN_ALBUM_LIST_EDITOR,true)))
465                                                 {
466                                                         AlbumListPresentationModel* pListPresentationModel = AlbumListPresentationModel::GetInstance();
467                                                         if (pListPresentationModel->GetAllAlbumCount() == 0)
468                                                         {
469                                                                 pSceneManager->DestroyScene(scenId);
470                                                         }
471                                                 }
472                                                 else if ((sceneIndex == pSceneList->GetCount()-1)
473                                                                 && (__previousSceneId.Equals(IDSCN_ARTIST_LIST_EDITOR,true)))
474                                                 {
475                                                         ArtistListPresentationModel* pListPresentationModel = ArtistListPresentationModel::GetInstance();
476                                                         if (pListPresentationModel->GetArtistListCount() == 0)
477                                                         {
478                                                                 pSceneManager->DestroyScene(scenId);
479                                                         }
480                                                 }
481                                                 else if ((sceneIndex == pSceneList->GetCount()-1)
482                                                                 && (__previousSceneId.Equals(IDSCN_FOLDER_LIST_EDITOR,true)))
483                                                 {
484                                                         FolderListPresentationModel* pListPresentationModel = FolderListPresentationModel::GetInstance();
485                                                         if (pListPresentationModel->GetAllFolderCount() ==0)
486                                                         {
487                                                                 pSceneManager->DestroyScene(scenId);
488                                                         }
489                                                 }
490                                                 else if ((sceneIndex == pSceneList->GetCount()-1)
491                                                                 && (__previousSceneId.Equals(IDSCN_ALL_LIST_EDITOR,true)))
492                                                 {
493                                                         AllListPresentationModel* pListPresentationModel = AllListPresentationModel::GetInstance();
494                                                         if (pListPresentationModel->GetTotalCount() ==0)
495                                                         {
496                                                                 pSceneManager->DestroyScene(scenId);
497                                                         }
498                                                 }
499                                                 else if ((sceneIndex == pSceneList->GetCount()-1)
500                                                                 && (__previousSceneId.Equals(IDSCN_PLAYLIST_CONTENT_LIST,true)))
501                                                 {
502                                                         PlaylistListPresentationModel* pListPresentationModel = PlaylistListPresentationModel::GetInstance();
503                                                         if (pListPresentationModel->GetContentCount() ==0)
504                                                         {
505                                                                 pSceneManager->DestroyScene(scenId);
506                                                         }
507                                                 }
508                                                 else
509                                                 {
510                                                         pSceneManager->AddToSceneHistory(scenId);
511                                                 }
512                                         }
513                                 }
514                         }
515                 }
516
517                 if (totalContents != 0 && totalContents > validContents)
518                 {
519                         if (!__previousSceneId.Equals(IDSCN_PLAYLIST_LIST,true))
520                         {
521                                 pSceneManager->GoBackward(BackwardSceneTransition());
522                         }
523                 }
524         }
525         AppLogDebug("EXIT");
526 }