2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file MpPlaylistListPresentationModel.cpp
19 * @brief This is the implementation file for PlaylistListPresentationModel class.
22 #include "MpCommonUtil.h"
23 #include "MpPlaylistDB.h"
24 #include "MpPlaylistListPresentationModel.h"
25 #include "MpResourceManager.h"
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;
34 PlaylistListPresentationModel* PlaylistListPresentationModel::pPlaylistListPresentationModel = null;
36 PlaylistListPresentationModel*
37 PlaylistListPresentationModel::GetInstance(void)
40 if (pPlaylistListPresentationModel == null)
43 pPlaylistListPresentationModel = new (std::nothrow) PlaylistListPresentationModel();
44 TryReturn(pPlaylistListPresentationModel != null, null, "PlaylistListPresentationModel instance is not created.");
46 result r = pPlaylistListPresentationModel->Construct();
47 TryCatch(r == E_SUCCESS, , null, "PlayerPresentationModel instance is not constructed.");
50 return pPlaylistListPresentationModel;
53 //AppLogException("PlaylistListPresentationModel::GetInstance(void) Error");
54 delete pPlaylistListPresentationModel;
59 PlaylistListPresentationModel::PlaylistListPresentationModel(void)
60 : __pPlaylistList(null)
61 , __pContentList(null)
69 PlaylistListPresentationModel::~PlaylistListPresentationModel(void)
72 if (__pPlaylistList != null)
74 __pPlaylistList->RemoveAll(true);
75 delete __pPlaylistList;
77 if (__pContentList != null)
79 __pContentList->RemoveAll(true);
80 delete __pContentList;
86 PlaylistListPresentationModel::DestroyPlaylistListPresentationModel(void)
89 if (pPlaylistListPresentationModel != null)
91 delete pPlaylistListPresentationModel;
92 pPlaylistListPresentationModel = null;
98 PlaylistListPresentationModel::Construct(void)
100 AppLogDebug("ENTER");
106 PlaylistListPresentationModel::GetPlayListItemCount(Tizen::Base::String& playlist)
108 AppLogDebug("ENTER");
109 PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(playlist);
111 TryReturn(pPlayList != null, itemCount,"No such playlist");
112 if (pPlayList != null)
114 itemCount = pPlayList->GetPlayListItemCount();
123 Tizen::Graphics::Bitmap*
124 PlaylistListPresentationModel::GetPlayListItemBitmap(Tizen::Base::String& playList)
126 AppLogDebug("ENTER");
127 PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(playList);
128 IList* pSearchList = pPlayList->GetContentInfoListN();
129 if (pSearchList == null)
134 ContentInfo* pContentInfo = static_cast<ContentInfo*>(pSearchList->GetAt(0));
135 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
136 Tizen::Graphics::Bitmap* pBitmap = null;
138 if (pAudioContentInfo == null)
140 pSearchList->RemoveAll(true);
143 pBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
147 pBitmap = pAudioContentInfo->GetThumbnailN();
150 pBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
153 pSearchList->RemoveAll(true);
164 Tizen::Content::ContentId
165 PlaylistListPresentationModel::GetPlayListItemContentId(Tizen::Base::String& playlist)
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)
176 ContentInfo* pContentInfo = static_cast<ContentInfo*>(pSearchList->GetAt(0));
177 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
179 if (pAudioContentInfo == null)
181 pSearchList->RemoveAll(true);
184 SetLastResult(E_FAILURE);
188 ContentId contentId = pAudioContentInfo->GetContentId();
189 pSearchList->RemoveAll(true);
200 PlaylistListPresentationModel::GetAllPlayListCount(void)
202 AppLogDebug("ENTER");
203 if (__pPlaylistList == null)
208 return __pPlaylistList->GetCount();
209 // return PlayListManager::GetInstance()->GetAllPlayListCount();
212 Tizen::Base::Collection::ArrayList*
213 PlaylistListPresentationModel::GetContentPathListN(int contentType)
215 AppLogDebug("ENTER");
216 Tizen::Base::Collection::ArrayList* pPlaylistContentList = new (std::nothrow) ArrayList();
217 pPlaylistContentList->Construct();
219 if (contentType == MOST_PLAYED_CONTENT || contentType == RECENTLY_PLAYED_CONTENT)
221 PlaylistDB* playlistDB = new (std::nothrow) PlaylistDB();
222 playlistDB->CreatePlaylistDatabase();
224 Tizen::Io::DbEnumerator* pDbEnumerator = playlistDB->SearchN(contentType);
225 Tizen::Base::String contentId;
227 Tizen::Base::Collection::ArrayList* pTempList = new (std::nothrow) ArrayList();
228 pTempList->Construct();
230 if (pDbEnumerator != null)
232 while (pDbEnumerator->MoveNext()== E_SUCCESS)
234 pDbEnumerator->GetStringAt(0, contentId);
236 String contentPath = null;
237 ContentId outContentId;
239 CommonUtil::StringToContentId(contentId, outContentId);
240 ContentInfo* pContentInfo = CommonUtil::GetContentInfoN(outContentId);
241 if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
243 contentPath = pContentInfo->GetContentPath();
248 pTempList->Add(*new (std::nothrow) String(contentId));
251 if (contentPath == null)
253 pTempList->Add(*new (std::nothrow) String(contentId));
257 pPlaylistContentList->Add(*(new (std::nothrow) String(contentPath)));
260 delete pDbEnumerator;
263 if (pTempList != null)
265 for (int i = 0; i < pTempList->GetCount(); i++)
267 playlistDB->Delete(*static_cast<String*>(pTempList->GetAt(i)));
270 pTempList->RemoveAll();
275 else if (contentType == RECENTLY_ADDED_CONTENT)
278 Tizen::System::SystemTime::GetCurrentTime(Tizen::System::TIME_MODE_STANDARD, playTime);
280 String whereQuery = L"DateTime<'";
281 whereQuery.Append(playTime.ToString());
282 whereQuery.Append(L"'");
284 IList* pSearchList = CommonUtil::SearchContentList(whereQuery, L"DateTime", SORT_ORDER_DESCENDING);
285 if (pSearchList == null)
290 int totalCount = pSearchList->GetCount();
291 for (int iCount = 0; iCount < totalCount; iCount++)
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())));
298 pSearchList->RemoveAll(true);
302 else if (contentType >= USER_CONTENT)
304 if (__pContentList == null)
310 String StrTemp = null;
311 ArrayList* pTempPath = new (std::nothrow) ArrayList();
312 pTempPath->Construct();
314 AudioContentInfo* pAudioContentInfo;
315 for (int iCount = INIT_VALUE; iCount < __pContentList->GetCount(); iCount++)
317 pAudioContentInfo = static_cast<AudioContentInfo*>(__pContentList->GetAt(iCount));
318 if (pAudioContentInfo != null)
320 StrTemp = pAudioContentInfo->GetContentPath();
321 pTempPath->Add(*(new (std::nothrow) String(StrTemp)));
329 return pPlaylistContentList;
333 PlaylistListPresentationModel::SetPlayListName(const Tizen::Base::String& oldPlayListName, const Tizen::Base::String& newPlayListName)
335 AppLogDebug("ENTER");
336 result r = E_SUCCESS;
337 if (oldPlayListName != null)
339 PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(oldPlayListName);
340 r = pPlaylist->SetPlayListName(newPlayListName);
347 PlayList* pPlaylist = new (std::nothrow) PlayList();
348 r = pPlaylist->Construct(newPlayListName);
357 PlaylistListPresentationModel::GetPlayListName(int playlistIndex)
359 AppLogDebug("ENTER");
360 /*Tizen::Base::Collection::IList* pAllPlaylistNameList = PlayListManager::GetInstance()->GetAllPlayListNameN();
361 if (pAllPlaylistNameList != null)
363 // AppLogDebug("EXIT");
364 IEnumerator* pEnum = pAllPlaylistNameList->GetEnumeratorN();
367 while (pEnum->MoveNext() == E_SUCCESS)
369 String* pTempName = dynamic_cast< String* >(pEnum->GetCurrent());
374 return static_cast<String*>(pAllPlaylistNameList->GetAt(playlistIndex));
376 if (__pPlaylistList != null)
378 String* pPlayListName = static_cast<String*>(__pPlaylistList->GetAt(playlistIndex));
379 return pPlayListName;
386 PlaylistListPresentationModel::RemovePlayList(Tizen::Base::String& playlistName)
388 AppLogDebug("ENTER");
389 AppLogDebug("%ls", playlistName.GetPointer());
390 PlayListManager::GetInstance()->RemovePlayList(playlistName);
391 AppLogDebug("EXIT %s",GetErrorMessage(GetLastResult()));
395 PlaylistListPresentationModel::AddContent(const Tizen::Content::ContentId& contentId, int playlistIndex)
397 AppLogDebug("ENTER");
398 Tizen::Base::String* pPlayListName;
399 pPlayListName = static_cast<String*>(__pPlaylistList->GetAt(playlistIndex));
401 PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(*pPlayListName);
402 TryReturnVoid(pPlayList != null, "No playlist with the name %ls",pPlayListName->GetPointer());
403 pPlayList->AddItem(contentId);
410 PlaylistListPresentationModel::UpdatePlaylistContentList(const int contentType, const Tizen::Base::String& playlistName)
412 AppLogDebug("ENTER");
413 if (__pQueryName != null)
419 if (__pContentList != null)
421 __pContentList->RemoveAll(true);
422 delete __pContentList;
423 __pContentList = null;
426 __contentType = contentType;
427 __pQueryName = new (std::nothrow) String(playlistName);
429 if (contentType == MOST_PLAYED_CONTENT || contentType == RECENTLY_PLAYED_CONTENT)
431 PlaylistDB* pPlaylistDB = new (std::nothrow) PlaylistDB();
432 pPlaylistDB->CreatePlaylistDatabase();
433 Tizen::Base::Collection::ArrayList* pTempList = new (std::nothrow) ArrayList();
434 pTempList->Construct();
436 Tizen::Io::DbEnumerator* pDbEnumerator = pPlaylistDB->SearchN(__contentType);
437 if (pDbEnumerator != null)
439 Tizen::Base::Collection::ArrayList* pContentIdList = new (std::nothrow) ArrayList(SingleObjectDeleter);
440 pContentIdList->Construct();
442 while (pDbEnumerator->MoveNext()== E_SUCCESS)
444 Tizen::Base::String contentId;
445 pDbEnumerator->GetStringAt(0, contentId);
447 ContentId outContentId;
448 CommonUtil::StringToContentId(contentId, outContentId);
449 ContentInfo* pContentInfo = CommonUtil::GetContentInfoN(outContentId);
450 if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
452 pTempList->Add(*pContentInfo);
456 pContentIdList->Add(*new (std::nothrow) String(contentId));
461 if (pTempList != null)
463 __pContentList = pTempList;
466 if (pContentIdList != null)
468 for (int i = 0; i < pContentIdList->GetCount(); i++)
470 pPlaylistDB->Delete(*static_cast<String*>(pContentIdList->GetAt(i)));
473 delete pContentIdList;
474 pContentIdList = null;
478 delete pDbEnumerator;
479 pDbEnumerator = null;
484 else if (contentType == RECENTLY_ADDED_CONTENT)
487 Tizen::System::SystemTime::GetCurrentTime(Tizen::System::TIME_MODE_STANDARD, playTime);
489 String whereQuery = L"DateTime<'";
490 whereQuery.Append(playTime.ToString());
491 whereQuery.Append(L"'");
493 __pContentList = CommonUtil::SearchContentList(whereQuery, L"DateTime", SORT_ORDER_DESCENDING);
495 else if (contentType == USER_CONTENT)
497 PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(*__pQueryName);
498 if (pPlaylist != null)
500 __pContentList = pPlaylist->GetContentInfoListN();
509 PlaylistListPresentationModel::GetContentCount(void)
511 AppLogDebug("ENTER");
512 if (__pContentList == null)
517 return __pContentList->GetCount();
521 PlaylistListPresentationModel::UpdatePlaylistList(void)
523 AppLogDebug("ENTER");
524 if (__pPlaylistList != null)
526 __pPlaylistList->RemoveAll(true);
527 delete __pPlaylistList;
528 __pPlaylistList = null;
531 __pPlaylistList = PlayListManager::GetInstance()->GetAllPlayListNameN();
536 PlaylistListPresentationModel::GetContentInfoN(int contentIndex)
538 AppLogDebug("ENTER");
539 if (__pContentList == null || contentIndex < INIT_VALUE)
541 AppLogDebug("EXIT(index(%d) MUST be greater than or equal to 0 OR __pContentList is null)", contentIndex);
545 ContentInformation* pContentInfoStruct = null;
546 switch (__contentType)
548 case MOST_PLAYED_CONTENT:
550 case RECENTLY_PLAYED_CONTENT:
552 AudioContentInfo* pAudioContentInfo;
553 pAudioContentInfo = static_cast<AudioContentInfo*>(__pContentList->GetAt(contentIndex));
554 if (pAudioContentInfo == null)
556 return pContentInfoStruct;
558 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
562 case RECENTLY_ADDED_CONTENT:
564 ContentSearchResult* pResult = static_cast<ContentSearchResult*>(__pContentList->GetAt(contentIndex));
565 if (pResult != null && pResult->GetContentType() == CONTENT_TYPE_AUDIO)
567 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pResult->GetContentInfo());
568 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
575 ContentInfo* pContentInfo = static_cast<ContentInfo*>(__pContentList->GetAt(contentIndex));
576 if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
578 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
579 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
588 return pContentInfoStruct;
592 PlaylistListPresentationModel::DeleteContent(int contentType, const ContentInformation& contentInfoStruct)
594 AppLogDebug("ENTER");
602 case MOST_PLAYED_CONTENT:
604 case RECENTLY_PLAYED_CONTENT:
606 PlaylistDB* playlistDB = new (std::nothrow) PlaylistDB();
607 String contentId = contentInfoStruct.contentId.ToString();
609 ArrayList* pTempList = new (std::nothrow) ArrayList();
610 pTempList->Construct();
612 pTempList = playlistDB->ReadValueN(contentId);
613 int playCount = static_cast<Integer*>(pTempList->GetAt(0))->ToInt();
615 playTime.Append(*static_cast<String*>(pTempList->GetAt(1)));
616 if (contentType == MOST_PLAYED_CONTENT)
618 playlistDB->Update(contentId, null, playTime);
623 tempPlayTime.Append(IDS_BLANK);
624 playlistDB->Update(contentId, playCount, tempPlayTime);
629 pTempList = playlistDB->ReadValueN(contentId);
630 playCount = static_cast<Integer*>(pTempList->GetAt(0))->ToInt();
632 playTime.Append(*static_cast<String*>(pTempList->GetAt(1)));
634 if (playCount == 0 && playTime.Equals(IDS_BLANK, true))
636 playlistDB->Delete(contentId);
644 case RECENTLY_ADDED_CONTENT:
651 PlayList* pPlaylist = PlayListManager::GetInstance()->GetPlayListN(*__pQueryName);
652 pPlaylist->RemoveItem(contentInfoStruct.contentId);