Fixed Jira issues 50249, 50683, 50387
[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 "MpCommonUtil.h"
24 #include "MpPlaylistCreatorForm.h"
25 #include "MpPlaylistEditorPresentationModel.h"
26 #include "MpResourceManager.h"
27 #include "MpSceneRegister.h"
28 #include "MpTypes.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Content;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Media;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Controls;
37 using namespace Tizen::Ui::Scenes;
38
39 PlaylistCreatorForm::PlaylistCreatorForm(void)
40         : __pPlaylistEditorPresentationModel(null)
41         , __pPlaylistEditField(null)
42         , __pDataList(null)
43         , __previousSceneId(L"")
44 {
45         AppLogDebug("ENTER");
46         AppLogDebug("EXIT");
47 }
48
49 PlaylistCreatorForm::~PlaylistCreatorForm(void)
50 {
51         AppLogDebug("ENTER");
52         AppLogDebug("EXIT");
53 }
54
55 void
56 PlaylistCreatorForm::Initialize(void)
57 {
58         AppLogDebug("ENTER");
59         Form::Construct(IDL_PLAYLIST_CREATOR_FORM);
60         AppLogDebug("EXIT");
61 }
62
63 result
64 PlaylistCreatorForm::OnInitializing(void)
65 {
66         AppLogDebug("ENTER");
67         __pPlaylistEditField = static_cast<EditField*>(GetControl(IDC_EDITFIELD));
68         __pPlaylistEditField->AddTextEventListener(*this);
69         __pPlaylistEditField->AddKeypadEventListener(*this);
70         __pPlaylistEditField->SetOverlayKeypadCommandButtonVisible(false);
71         __pPlaylistEditField->SetFocus();
72         __pPlaylistEditorPresentationModel = PlaylistEditorPresentationModel::GetInstance();
73
74         SetFormBackEventListener(this);
75         if (GetFooter() != null)
76         {
77                 GetFooter()->AddActionEventListener(*this);
78         }
79         AppLogDebug("EXIT");
80         return E_SUCCESS;
81 }
82
83 result
84 PlaylistCreatorForm::OnTerminating(void)
85 {
86         AppLogDebug("ENTER");
87         if (__pDataList != null)
88         {
89                 __pDataList->RemoveAll(true);
90                 delete __pDataList;
91         }
92         AppLogDebug("EXIT");
93         return E_SUCCESS;
94 }
95
96 void
97 PlaylistCreatorForm::OnKeypadActionPerformed(Tizen::Ui::Control& source,
98                                                         Tizen::Ui::KeypadAction keypadAction)
99 {
100         AppLogDebug("ENTER");
101         AppLogDebug("EXIT");
102 }
103
104 void
105 PlaylistCreatorForm::OnKeypadClosed(Tizen::Ui::Control& source)
106 {
107         AppLogDebug("ENTER");
108         AppLogDebug("EXIT");
109 }
110
111 void
112 PlaylistCreatorForm::OnKeypadOpened(Tizen::Ui::Control& source)
113 {
114         AppLogDebug("ENTER");
115         AppLogDebug("EXIT");
116 }
117
118 void
119 PlaylistCreatorForm::OnKeypadWillOpen(Tizen::Ui::Control &source)
120 {
121         AppLogDebug("ENTER");
122         AppLogDebug("EXIT");
123 }
124
125 void
126 PlaylistCreatorForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
127                                                 const Tizen::Ui::Scenes::SceneId& currentSceneId,
128                                                 Tizen::Base::Collection::IList* pArgs)
129 {
130         AppLogDebug("ENTER");
131         String PlayListName = ResourceManager::GetString(L"IDS_MP_HEADER_MENU_PLAYLIST");
132         PlayListName.Append(L"_");
133         ArrayList* pNumberList = new (std::nothrow) ArrayList();
134         pNumberList->Construct();
135
136         int numericPlaylist = 1;
137         int totalPlaylistCount = __pPlaylistEditorPresentationModel->GetAllPlayListCount();
138         __previousSceneId = previousSceneId;
139         AppLogDebug("%d", totalPlaylistCount);
140
141         if (totalPlaylistCount != 0)
142         {
143                 int lastNum = 0;
144                 for (int i = 0; i < totalPlaylistCount; i++)
145                 {
146                         String currentPlayListName = __pPlaylistEditorPresentationModel->GetPlayListName(i);
147                         if (currentPlayListName.GetLength() >= PlayListName.GetLength())
148                         {
149                                 String compareStr;
150                                 currentPlayListName.SubString(0, PlayListName.GetLength(), compareStr);
151                                 if (compareStr.Equals(PlayListName, true))
152                                 {
153                                         String pLastStr;
154                                         wchar_t ch;
155                                         for (int index = currentPlayListName.GetLength()-1; index >= 0; index--)
156                                         {
157                                                 currentPlayListName.GetCharAt(index, ch);
158                                                 if (Character::IsDigit(ch))
159                                                 {
160                                                         continue;
161                                                 }
162                                                 else
163                                                 {
164                                                         currentPlayListName.SubString(index + 1, currentPlayListName.GetLength() - (index + 1), pLastStr);
165                                                         break;
166                                                 }
167                                         }
168
169                                         if (pLastStr != null)
170                                         {
171                                                 Integer::Parse(pLastStr, lastNum);
172                                                 pNumberList->Add((new (std::nothrow) Integer(lastNum)));
173                                         }
174                                 }
175                         }
176                 }
177                 if (pNumberList != null)
178                 {
179                         int pNumberListCount = pNumberList->GetCount();
180                         IntegerComparer integerComparer;
181                         pNumberList->Sort(integerComparer);
182                         for (int index = 0; index < pNumberListCount; index++)
183                         {
184                                 if (static_cast<Integer*>(pNumberList->GetAt(0))->ToInt() != 1)
185                                 {
186                                         break;
187                                 }
188
189                                 int firstNumber = 0;
190                                 int secondNumber = 0;
191
192                                 firstNumber = static_cast<Integer*>(pNumberList->GetAt(index))->ToInt();
193                                 if (index + 1 < pNumberListCount)
194                                 {
195                                         secondNumber = static_cast<Integer*>(pNumberList->GetAt(index + 1))->ToInt();
196                                 }
197
198                                 if (firstNumber + 1 < secondNumber)
199                                 {
200                                         numericPlaylist = firstNumber + 1;
201                                         break;
202                                 }
203                                 else
204                                 {
205                                         numericPlaylist = firstNumber + 1;
206                                 }
207                         }
208                 }
209                 PlayListName.Append(numericPlaylist);
210                 if (__pPlaylistEditField->GetTextLength() == 0)
211                 {
212                         __pPlaylistEditField->SetText(PlayListName);
213                 }
214         }
215         else
216         {
217                 PlayListName.Append(numericPlaylist);
218                 if (__pPlaylistEditField->GetTextLength() == 0)
219                 {
220                         __pPlaylistEditField->SetText(PlayListName);
221                 }
222         }
223
224         __pPlaylistEditField->ShowKeypad();
225
226         if (pNumberList != null)
227         {
228                 pNumberList->RemoveAll(true);
229                 delete pNumberList;
230         }
231         if (pArgs != null)
232         {
233                 __pDataList = pArgs;
234         }
235         AppLogDebug("EXIT");
236 }
237
238 void
239 PlaylistCreatorForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
240                                                                 const Tizen::Ui::Scenes::SceneId& nextSceneId)
241 {
242         AppLogDebug("ENTER");
243         AppLogDebug("EXIT");
244 }
245
246 void
247 PlaylistCreatorForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
248 {
249         AppLogDebug("ENTER");
250         SceneManager* pSceneManager = SceneManager::GetInstance();
251         AppAssert(pSceneManager);
252         result r = E_FAILURE;
253         ArrayList* pList = null;
254
255         switch (actionId)
256         {
257         case IDA_FOOTER_BUTTON_SAVE:
258         {
259                 String inputString = __pPlaylistEditField->GetText();
260                 inputString.Trim();
261                 __pPlaylistEditField->HideKeypad();
262                 if (inputString.GetLength() == 0)
263                 {
264                         MessageBox messageBox;
265                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_MUSIC_POP_UNABLE_CREATE_PLAYLIST"), MSGBOX_STYLE_NONE, 3000);
266                         int modalResult = 0;
267                         messageBox.ShowAndWait(modalResult);
268                         __pPlaylistEditField->SetFocus();
269                         __pPlaylistEditField->ShowKeypad();
270                         break;
271                 }
272
273                 r = __pPlaylistEditorPresentationModel->SetPlayListName(null, inputString);
274                 if (r != E_SUCCESS)
275                 {
276                         MessageBox messageBox;
277                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_MUSIC_POP_UNABLE_CREATE_PLAYLIST"), MSGBOX_STYLE_NONE, 3000);
278                         int modalResult = 0;
279                         messageBox.ShowAndWait(modalResult);
280                         __pPlaylistEditField->SetFocus();
281                         __pPlaylistEditField->ShowKeypad();
282                         break;
283                 }
284
285                 if (__pDataList != null)
286                 {
287                         for (int index = 0; index < __pDataList->GetCount(); index++)
288                         {
289                                 ContentId contentId = CommonUtil::GetContentId(*(static_cast<String*>(__pDataList->GetAt(index))));
290                                 if (!IsFailed(GetLastResult()))
291                                 {
292                                         __pPlaylistEditorPresentationModel->AddItem(contentId, inputString);
293                                 }
294                                 else
295                                 {
296                                         r = E_FAILURE;
297                                         MessageBox messageBox;
298                                         messageBox.Construct(L"", ResourceManager::GetString(L"IDS_MUSIC_POP_UNABLE_TO_ADD"),
299                                                         MSGBOX_STYLE_OK,
300                                                         3000);
301                                         int modalResult = 0;
302                                         messageBox.ShowAndWait(modalResult);
303                                         break;
304                                 }
305                         }
306
307                         if (r == E_SUCCESS)
308                         {
309                                 MessageBox messageBox;
310                                 messageBox.Construct(L"", ResourceManager::GetString(L"IDS_COM_POP_SUCCESS"),
311                                                 MSGBOX_STYLE_OK,
312                                                 3000);
313                                 int modalResult = 0;
314                                 messageBox.ShowAndWait(modalResult);
315                                 pList = new (std::nothrow) ArrayList();
316                                 pList->Construct();
317                 pList->Add(new (std::nothrow) Integer(1));
318                         }
319                         else
320                         {
321                                 __pPlaylistEditorPresentationModel->RemovePlayList(inputString);
322                         }
323
324                         pSceneManager->GoBackward(BackwardSceneTransition(),pList);
325                 }
326                 else
327                 {
328                         AllListPresentationModel* pPresentationModel = AllListPresentationModel::GetInstance();
329                         if (pPresentationModel->GetTotalCount() == 0)
330                         {
331                                 pSceneManager->GoBackward(BackwardSceneTransition());
332                         }
333                         else
334                         {
335                                 CommonUtil::SetAddtoPlaylistState(true);
336                                 CommonUtil::SetAddtoPlaylistName(inputString);
337                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALL_CONTENT_PICKER,
338                                                 SCENE_TRANSITION_ANIMATION_TYPE_NONE, SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY));
339                         }
340                 }
341         }
342                 break;
343
344         default:
345                 break;
346         }
347         AppLogDebug("EXIT");
348 }
349
350 void
351 PlaylistCreatorForm::OnTextValueChanged(const Tizen::Ui::Control& source)
352 {
353         AppLogDebug("ENTER");
354         Footer* pFooter = GetFooter();
355         EditArea* pEditArea = const_cast<EditArea*>(static_cast<const EditArea*>(&source));
356         String editareaString = pEditArea->GetText();
357         if (editareaString.GetLength() == INIT_VALUE)
358         {
359                 if (pFooter != null)
360                 {
361                         CommonUtil::SetFooterItemEnabled(*pFooter, false);
362                 }
363         }
364         else
365         {
366                 if (pFooter != null)
367                 {
368                         CommonUtil::SetFooterItemEnabled(*pFooter, true);
369                 }
370         }
371         AppLogDebug("EXIT");
372 }
373
374 void
375 PlaylistCreatorForm::OnTextValueChangeCanceled(const Tizen::Ui::Control& source)
376 {
377         AppLogDebug("ENTER");
378         AppLogDebug("EXIT");
379 }
380
381 void
382 PlaylistCreatorForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
383 {
384         AppLogDebug("ENTER");
385         SceneManager* pSceneManager = SceneManager::GetInstance();
386         pSceneManager->GoBackward(BackwardSceneTransition());
387         AppLogDebug("EXIT");
388 }