Applied latest source code
[apps/native/preloaded/MusicPlayer.git] / src / MpPlaylistListPresentationModel.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                MpPlaylistListPresentationModel.cpp
19  * @brief               This is the implementation file for PlaylistListPresentationModel class.
20  */
21
22 #include "MpCommonUtil.h"
23 #include "MpPlaylistDB.h"
24 #include "MpPlaylistListPresentationModel.h"
25 #include "MpResourceManager.h"
26 #include "MpTypes.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Content;
32 using namespace Tizen::Io;
33
34 PlaylistListPresentationModel* PlaylistListPresentationModel::pPlaylistListPresentationModel = null;
35
36 PlaylistListPresentationModel*
37 PlaylistListPresentationModel::GetInstance(void)
38 {
39         AppLogDebug("ENTER");
40         if (pPlaylistListPresentationModel == null)
41         {
42                 AppLogDebug("ENTER");
43                 pPlaylistListPresentationModel = new (std::nothrow) PlaylistListPresentationModel();
44                 TryReturn(pPlaylistListPresentationModel != null, null, "PlaylistListPresentationModel instance is not created.");
45
46                 result r = pPlaylistListPresentationModel->Construct();
47                 TryCatch(r == E_SUCCESS, , null, "PlayerPresentationModel instance is not constructed.");
48         }
49         AppLogDebug("EXIT");
50         return pPlaylistListPresentationModel;
51
52 CATCH:
53         //AppLogException("PlaylistListPresentationModel::GetInstance(void) Error");
54         delete pPlaylistListPresentationModel;
55
56         return null;
57 }
58
59 PlaylistListPresentationModel::PlaylistListPresentationModel(void)
60         : __pPlaylistList(null)
61         , __pContentList(null)
62         , __contentType(0)
63         , __pQueryName(null)
64 {
65         AppLogDebug("ENTER");
66         AppLogDebug("EXIT");
67 }
68
69 PlaylistListPresentationModel::~PlaylistListPresentationModel(void)
70 {
71         AppLogDebug("ENTER");
72         if (__pPlaylistList != null)
73         {
74                 __pPlaylistList->RemoveAll(true);
75                 delete __pPlaylistList;
76         }
77         if (__pContentList != null)
78         {
79                 __pContentList->RemoveAll(true);
80                 delete __pContentList;
81         }
82         AppLogDebug("EXIT");
83 }
84
85 void
86 PlaylistListPresentationModel::DestroyPlaylistListPresentationModel(void)
87 {
88         AppLogDebug("ENTER");
89         if (pPlaylistListPresentationModel != null)
90         {
91                 delete pPlaylistListPresentationModel;
92                 pPlaylistListPresentationModel = null;
93         }
94         AppLogDebug("EXIT");
95 }
96
97 result
98 PlaylistListPresentationModel::Construct(void)
99 {
100         AppLogDebug("ENTER");
101         AppLogDebug("EXIT");
102         return E_SUCCESS;
103 }
104
105 int
106 PlaylistListPresentationModel::GetPlayListItemCount(Tizen::Base::String& playlist)
107 {
108         AppLogDebug("ENTER");
109         PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(playlist);
110         int itemCount = 0;
111         TryReturn(pPlayList != null, itemCount,"No such playlist");
112         if (pPlayList != null)
113         {
114                 itemCount = pPlayList->GetPlayListItemCount();
115                 delete pPlayList;
116                 pPlayList = null;
117         }
118
119         AppLogDebug("EXIT");
120         return itemCount;
121 }
122
123 Tizen::Graphics::Bitmap*
124 PlaylistListPresentationModel::GetPlayListItemBitmap(Tizen::Base::String& playList)
125 {
126         AppLogDebug("ENTER");
127         PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(playList);
128         IList* pSearchList = pPlayList->GetContentInfoListN();
129         if (pSearchList == null)
130         {
131                 return null;
132         }
133
134         ContentInfo* pContentInfo = static_cast<ContentInfo*>(pSearchList->GetAt(0));
135         AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
136         Tizen::Graphics::Bitmap* pBitmap = null;
137
138         if (pAudioContentInfo == null)
139         {
140                 pSearchList->RemoveAll(true);
141                 delete pSearchList;
142                 pSearchList = null;
143                 pBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
144                 return pBitmap;
145         }
146
147         pBitmap = pAudioContentInfo->GetThumbnailN();
148         if (pBitmap == null)
149         {
150                 pBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
151         }
152
153         pSearchList->RemoveAll(true);
154         delete pSearchList;
155         pSearchList = null;
156
157         delete pPlayList;
158         pPlayList = null;
159
160         AppLogDebug("EXIT");
161         return pBitmap;
162 }
163
164 Tizen::Content::ContentId
165 PlaylistListPresentationModel::GetPlayListItemContentId(Tizen::Base::String& playlist)
166 {
167         AppLogDebug("ENTER");
168         PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(playlist);
169         TryReturn(pPlaylist != null, ContentId(),"No such playlist");
170         IList* pSearchList = pPlaylist->GetContentInfoListN();
171         if (pSearchList == null)
172         {
173                 delete pPlaylist;
174                 return ContentId();
175         }
176         ContentInfo* pContentInfo = static_cast<ContentInfo*>(pSearchList->GetAt(0));
177         AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
178
179         if (pAudioContentInfo == null)
180         {
181                 pSearchList->RemoveAll(true);
182                 delete pSearchList;
183                 pSearchList = null;
184                 SetLastResult(E_FAILURE);
185                 return ContentId();
186         }
187
188         ContentId contentId = pAudioContentInfo->GetContentId();
189         pSearchList->RemoveAll(true);
190         delete pSearchList;
191         pSearchList = null;
192         delete pPlaylist;
193         pPlaylist = null;
194
195         AppLogDebug("EXIT");
196         return contentId;
197 }
198
199 int
200 PlaylistListPresentationModel::GetAllPlayListCount(void)
201 {
202         AppLogDebug("ENTER");
203         if (__pPlaylistList == null)
204         {
205                 return INIT_VALUE;
206         }
207         AppLogDebug("EXIT");
208         return __pPlaylistList->GetCount();
209 //      return PlayListManager::GetInstance()->GetAllPlayListCount();
210 }
211
212 Tizen::Base::Collection::ArrayList*
213 PlaylistListPresentationModel::GetContentPathListN(int contentType)
214 {
215         AppLogDebug("ENTER");
216         Tizen::Base::Collection::ArrayList* pPlaylistContentList = new (std::nothrow) ArrayList();
217         pPlaylistContentList->Construct();
218
219         if (contentType == MOST_PLAYED_CONTENT || contentType == RECENTLY_PLAYED_CONTENT)
220         {
221                 PlaylistDB* playlistDB = new (std::nothrow) PlaylistDB();
222                 playlistDB->CreatePlaylistDatabase();
223
224                 Tizen::Io::DbEnumerator* pDbEnumerator = playlistDB->SearchN(contentType);
225                 Tizen::Base::String contentId;
226
227                 Tizen::Base::Collection::ArrayList* pTempList = new (std::nothrow) ArrayList();
228                 pTempList->Construct();
229
230                 if (pDbEnumerator != null)
231                 {
232                         while (pDbEnumerator->MoveNext()== E_SUCCESS)
233                         {
234                                 pDbEnumerator->GetStringAt(0, contentId);
235
236                                 String contentPath = null;
237                                 ContentId outContentId;
238
239                                 CommonUtil::StringToContentId(contentId, outContentId);
240                                 ContentInfo* pContentInfo = CommonUtil::GetContentInfoN(outContentId);
241                                 if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
242                                 {
243                                         contentPath = pContentInfo->GetContentPath();
244                                         delete pContentInfo;
245                                 }
246                                 else
247                                 {
248                                         pTempList->Add(*new (std::nothrow) String(contentId));
249                                 }
250
251                                 if (contentPath == null)
252                                 {
253                                         pTempList->Add(*new (std::nothrow) String(contentId));
254                                 }
255                                 else
256                                 {
257                                         pPlaylistContentList->Add(*(new (std::nothrow) String(contentPath)));
258                                 }
259                         }
260                         delete pDbEnumerator;
261                 }
262
263                 if (pTempList != null)
264                 {
265                         for (int i = 0; i < pTempList->GetCount(); i++)
266                         {
267                                 playlistDB->Delete(*static_cast<String*>(pTempList->GetAt(i)));
268                         }
269
270                         pTempList->RemoveAll();
271                         delete pTempList;
272                 }
273                 delete playlistDB;
274         }
275         else if (contentType == RECENTLY_ADDED_CONTENT)
276         {
277                 DateTime playTime;
278                 Tizen::System::SystemTime::GetCurrentTime(Tizen::System::TIME_MODE_STANDARD, playTime);
279
280                 String whereQuery = L"DateTime<'";
281                 whereQuery.Append(playTime.ToString());
282                 whereQuery.Append(L"'");
283
284                 IList* pSearchList = CommonUtil::SearchContentList(whereQuery, L"DateTime", SORT_ORDER_DESCENDING);
285                 if (pSearchList == null)
286                 {
287                         return null;
288                 }
289
290                 int totalCount = pSearchList->GetCount();
291                 for (int iCount = 0; iCount < totalCount; iCount++)
292                 {
293                         ContentSearchResult* pResult = static_cast<ContentSearchResult*>(pSearchList->GetAt(iCount));
294                         AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pResult->GetContentInfo());
295                         pPlaylistContentList->Add(*(new (std::nothrow) String(pAudioContentInfo->GetContentPath())));
296                 }
297
298                 pSearchList->RemoveAll(true);
299                 delete pSearchList;
300                 pSearchList = null;
301         }
302         else if (contentType >= USER_CONTENT)
303         {
304                 if (__pContentList == null)
305                 {
306                         AppLogDebug("EXIT");
307                         return null;
308                 }
309
310                 String StrTemp = null;
311                 ArrayList* pTempPath = new (std::nothrow) ArrayList();
312                 pTempPath->Construct();
313
314                 AudioContentInfo* pAudioContentInfo;
315                 for (int iCount = INIT_VALUE; iCount < __pContentList->GetCount(); iCount++)
316                 {
317                         pAudioContentInfo = static_cast<AudioContentInfo*>(__pContentList->GetAt(iCount));
318                         if (pAudioContentInfo != null)
319                         {
320                                 StrTemp = pAudioContentInfo->GetContentPath();
321                                 pTempPath->Add(*(new (std::nothrow) String(StrTemp)));
322                         }
323                 }
324                 AppLogDebug("EXIT");
325                 return pTempPath;
326         }
327
328         AppLogDebug("EXIT");
329         return pPlaylistContentList;
330 }
331
332 result
333 PlaylistListPresentationModel::SetPlayListName(const Tizen::Base::String& oldPlayListName, const Tizen::Base::String& newPlayListName)
334 {
335         AppLogDebug("ENTER");
336         result r = E_SUCCESS;
337         if (oldPlayListName != null)
338         {
339                 PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(oldPlayListName);
340                 r = pPlaylist->SetPlayListName(newPlayListName);
341
342                 delete pPlaylist;
343                 pPlaylist = null;
344         }
345         else
346         {
347                 PlayList* pPlaylist = new (std::nothrow) PlayList();
348                 r = pPlaylist->Construct(newPlayListName);
349                 delete pPlaylist;
350                 pPlaylist = null;
351         }
352         AppLogDebug("EXIT");
353         return r;
354 }
355
356 Tizen::Base::String*
357 PlaylistListPresentationModel::GetPlayListName(int playlistIndex)
358 {
359         AppLogDebug("ENTER");
360         /*Tizen::Base::Collection::IList* pAllPlaylistNameList = PlayListManager::GetInstance()->GetAllPlayListNameN();
361         if (pAllPlaylistNameList != null)
362         {
363 //              AppLogDebug("EXIT");
364                 IEnumerator* pEnum = pAllPlaylistNameList->GetEnumeratorN();
365                 if(pEnum != null)
366                 {
367                         while (pEnum->MoveNext() == E_SUCCESS)
368                         {
369                                 String* pTempName = dynamic_cast< String* >(pEnum->GetCurrent());
370                         }
371                         delete pEnum;
372                 }
373
374                 return static_cast<String*>(pAllPlaylistNameList->GetAt(playlistIndex));
375         }*/
376         if (__pPlaylistList != null)
377         {
378                 String* pPlayListName = static_cast<String*>(__pPlaylistList->GetAt(playlistIndex));
379                 return pPlayListName;
380         }
381         AppLogDebug("EXIT");
382         return null;
383 }
384
385 void
386 PlaylistListPresentationModel::RemovePlayList(Tizen::Base::String& playlistName)
387 {
388         AppLogDebug("ENTER");
389         AppLogDebug("%ls", playlistName.GetPointer());
390         PlayListManager::GetInstance()->RemovePlayList(playlistName);
391         AppLogDebug("EXIT %s",GetErrorMessage(GetLastResult()));
392 }
393
394 void
395 PlaylistListPresentationModel::AddContent(const Tizen::Content::ContentId& contentId, int playlistIndex)
396 {
397         AppLogDebug("ENTER");
398         Tizen::Base::String* pPlayListName;
399         pPlayListName = static_cast<String*>(__pPlaylistList->GetAt(playlistIndex));
400
401         PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(*pPlayListName);
402         TryReturnVoid(pPlayList != null, "No playlist with the name %ls",pPlayListName->GetPointer());
403         pPlayList->AddItem(contentId);
404         delete pPlayList;
405         pPlayList = null;
406         AppLogDebug("EXIT");
407 }
408
409 void
410 PlaylistListPresentationModel::UpdatePlaylistContentList(const int contentType, const Tizen::Base::String& playlistName)
411 {
412         AppLogDebug("ENTER");
413         if (__pQueryName != null)
414         {
415                 delete __pQueryName;
416                 __pQueryName = null;
417         }
418
419         if (__pContentList != null)
420         {
421                 __pContentList->RemoveAll(true);
422                 delete __pContentList;
423                 __pContentList = null;
424         }
425
426         __contentType = contentType;
427         __pQueryName = new (std::nothrow) String(playlistName);
428
429         if (contentType == MOST_PLAYED_CONTENT || contentType == RECENTLY_PLAYED_CONTENT)
430         {
431                 PlaylistDB* pPlaylistDB = new (std::nothrow) PlaylistDB();
432                 pPlaylistDB->CreatePlaylistDatabase();
433                 Tizen::Base::Collection::ArrayList* pTempList = new (std::nothrow) ArrayList();
434                 pTempList->Construct();
435
436                 Tizen::Io::DbEnumerator* pDbEnumerator = pPlaylistDB->SearchN(__contentType);
437                 if (pDbEnumerator != null)
438                 {
439                         Tizen::Base::Collection::ArrayList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
440                         pContentIdList->Construct();
441
442                         while (pDbEnumerator->MoveNext()== E_SUCCESS)
443                         {
444                                 Tizen::Base::String contentId;
445                                 pDbEnumerator->GetStringAt(0, contentId);
446
447                                 ContentId outContentId;
448                                 CommonUtil::StringToContentId(contentId, outContentId);
449                                 ContentInfo* pContentInfo = CommonUtil::GetContentInfoN(outContentId);
450                                 if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
451                                 {
452                                         pTempList->Add(*pContentInfo);
453                                 }
454                                 else
455                                 {
456                                         pContentIdList->Add(*new (std::nothrow) String(contentId));
457                                         delete pContentInfo;
458                                 }
459                         }
460
461                         if (pTempList != null)
462                         {
463                                 __pContentList = pTempList;
464                         }
465
466                         if (pContentIdList != null)
467                         {
468                                 for (int i = 0; i < pContentIdList->GetCount(); i++)
469                                 {
470                                         pPlaylistDB->Delete(*static_cast<String*>(pContentIdList->GetAt(i)));
471                                 }
472
473                                 delete pContentIdList;
474                                 pContentIdList = null;
475                         }
476                 }
477
478                 delete pDbEnumerator;
479                 pDbEnumerator = null;
480
481                 delete pPlaylistDB;
482                 pPlaylistDB = null;
483         }
484         else if (contentType == RECENTLY_ADDED_CONTENT)
485         {
486                 DateTime playTime;
487                 Tizen::System::SystemTime::GetCurrentTime(Tizen::System::TIME_MODE_STANDARD, playTime);
488
489                 String whereQuery = L"DateTime<'";
490                 whereQuery.Append(playTime.ToString());
491                 whereQuery.Append(L"'");
492
493                 __pContentList = CommonUtil::SearchContentList(whereQuery, L"DateTime", SORT_ORDER_DESCENDING);
494         }
495         else if (contentType == USER_CONTENT)
496         {
497                 PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(*__pQueryName);
498                 if (pPlaylist != null)
499                 {
500                         __pContentList = pPlaylist->GetContentInfoListN();
501                         delete pPlaylist;
502                         pPlaylist = null;
503                 }
504         }
505         AppLogDebug("EXIT");
506 }
507
508 int
509 PlaylistListPresentationModel::GetContentCount(void)
510 {
511         AppLogDebug("ENTER");
512         if (__pContentList == null)
513         {
514                 return INIT_VALUE;
515         }
516         AppLogDebug("EXIT");
517         return __pContentList->GetCount();
518 }
519
520 void
521 PlaylistListPresentationModel::UpdatePlaylistList(void)
522 {
523         AppLogDebug("ENTER");
524         if (__pPlaylistList != null)
525         {
526                 __pPlaylistList->RemoveAll(true);
527                 delete __pPlaylistList;
528                 __pPlaylistList = null;
529         }
530
531         __pPlaylistList = PlayListManager::GetInstance()->GetAllPlayListNameN();
532         AppLogDebug("EXIT");
533 }
534
535 ContentInformation*
536 PlaylistListPresentationModel::GetContentInfoN(int contentIndex)
537 {
538         AppLogDebug("ENTER");
539         if (__pContentList == null || contentIndex < INIT_VALUE)
540         {
541                 AppLogDebug("EXIT(index(%d) MUST be greater than or equal to 0 OR __pContentList is null)", contentIndex);
542                 return null;
543         }
544
545         ContentInformation* pContentInfoStruct = null;
546         switch (__contentType)
547         {
548         case MOST_PLAYED_CONTENT:
549                 // fall through
550         case RECENTLY_PLAYED_CONTENT:
551                 {
552                         AudioContentInfo* pAudioContentInfo;
553                         pAudioContentInfo = static_cast<AudioContentInfo*>(__pContentList->GetAt(contentIndex));
554                         if (pAudioContentInfo == null)
555                         {
556                                 return pContentInfoStruct;
557                         }
558                         pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
559                 }
560                 break;
561
562         case RECENTLY_ADDED_CONTENT:
563                 {
564                         ContentSearchResult* pResult = static_cast<ContentSearchResult*>(__pContentList->GetAt(contentIndex));
565                         if (pResult != null && pResult->GetContentType() == CONTENT_TYPE_AUDIO)
566                         {
567                                 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pResult->GetContentInfo());
568                                 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
569                         }
570                 }
571                 break;
572
573         case USER_CONTENT:
574                 {
575                         ContentInfo* pContentInfo = static_cast<ContentInfo*>(__pContentList->GetAt(contentIndex));
576                         if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
577                         {
578                                 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
579                                 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
580                         }
581                 }
582                 break;
583
584         default:
585                 break;
586         }
587         AppLogDebug("EXIT");
588         return pContentInfoStruct;
589 }
590
591 void
592 PlaylistListPresentationModel::DeleteContent(int contentType, const ContentInformation& contentInfoStruct)
593 {
594         AppLogDebug("ENTER");
595         switch (contentType)
596         {
597         case NORMAL_CONTENT:
598                 {
599                 }
600                 break;
601
602         case MOST_PLAYED_CONTENT:
603                 // fall through
604         case RECENTLY_PLAYED_CONTENT:
605                 {
606                         PlaylistDB* playlistDB = new (std::nothrow) PlaylistDB();
607                         String contentId = contentInfoStruct.contentId.ToString();
608
609                         ArrayList* pTempList = new (std::nothrow) ArrayList();
610                         pTempList->Construct();
611
612                         pTempList = playlistDB->ReadValueN(contentId);
613                         int playCount = static_cast<Integer*>(pTempList->GetAt(0))->ToInt();
614                         String playTime;
615                         playTime.Append(*static_cast<String*>(pTempList->GetAt(1)));
616                         if (contentType == MOST_PLAYED_CONTENT)
617                         {
618                                 playlistDB->Update(contentId, null, playTime);
619                         }
620                         else
621                         {
622                                 String tempPlayTime;
623                                 tempPlayTime.Append(IDS_BLANK);
624                                 playlistDB->Update(contentId, playCount, tempPlayTime);
625                         }
626
627                         delete pTempList;
628
629                         pTempList = playlistDB->ReadValueN(contentId);
630                         playCount = static_cast<Integer*>(pTempList->GetAt(0))->ToInt();
631                         playTime = null;
632                         playTime.Append(*static_cast<String*>(pTempList->GetAt(1)));
633
634                         if (playCount == 0 && playTime.Equals(IDS_BLANK, true))
635                         {
636                                 playlistDB->Delete(contentId);
637                         }
638
639                         delete playlistDB;
640                         delete pTempList;
641                 }
642                 break;
643
644         case RECENTLY_ADDED_CONTENT:
645                 {
646                 }
647                 break;
648
649         case USER_CONTENT:
650                 {
651                         PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(*__pQueryName);
652                         pPlaylist->RemoveItem(contentInfoStruct.contentId);
653
654                         delete pPlaylist;
655                         pPlaylist = null;
656                 }
657                 break;
658
659         default:
660                 break;
661         }
662         AppLogDebug("EXIT");
663 }