Applied latest source code
[apps/native/preloaded/MusicPlayer.git] / src / MpGenreListPresentationModel.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                MpGenreListPresentationModel.cpp
19  * @brief               This is the implementation file for GenreListPresentationModel class.
20  */
21
22 #include "MpGenreListPresentationModel.h"
23
24 using namespace Tizen::Base;
25 using namespace Tizen::Base::Collection;
26 using namespace Tizen::Content;
27 using namespace Tizen::Graphics;
28 using namespace Tizen::Social;
29 using namespace Tizen::Ui::Controls;
30
31 GenreListPresentationModel* GenreListPresentationModel::pGenreListPresentationModelInstance = null;
32
33 GenreListPresentationModel*
34 GenreListPresentationModel::GetInstance(void)
35 {
36         AppLogDebug("ENTER");
37         if (pGenreListPresentationModelInstance == null)
38         {
39                 pGenreListPresentationModelInstance = new (std::nothrow) GenreListPresentationModel();
40                 TryReturn(pGenreListPresentationModelInstance != null, null, "GenreListPresentationModel instance is not created.");
41
42                 result r = pGenreListPresentationModelInstance->Construct();
43                 TryCatch(r == E_SUCCESS, , null, "GenreListPresentationModel instance is not constrcuted.");
44         }
45         AppLogDebug("EXIT");
46         return pGenreListPresentationModelInstance;
47
48 CATCH:
49         delete pGenreListPresentationModelInstance;
50         return null;
51 }
52
53 GenreListPresentationModel::GenreListPresentationModel(void)
54         : __pGnereList(null)
55         , __pContentList(null)
56         , __currentGenreIndex(-1)
57 {
58         AppLogDebug("ENTER");
59         AppLogDebug("EXIT");
60 }
61
62 GenreListPresentationModel::~GenreListPresentationModel(void)
63 {
64         AppLogDebug("ENTER");
65         AppLogDebug("EXIT");
66 }
67
68 result
69 GenreListPresentationModel::Construct(void)
70 {
71         AppLogDebug("ENTER");
72         if (__pGnereList != null)
73         {
74                 __pGnereList->RemoveAll(true);
75                 delete __pGnereList;
76         }
77
78         __pGnereList = CommonUtil::SearchContentList(STRING_SORT_GENRE);
79
80         AppLogDebug("EXIT");
81         return E_SUCCESS;
82 }
83
84 int
85 GenreListPresentationModel::GetContentCount(int genreIndex)
86 {
87         AppLogDebug("ENTER");
88         int totalCount = INIT_VALUE;
89         if (__pGnereList == null)
90         {
91                 return INIT_VALUE;
92         }
93
94         if ((__pContentList != null) && (__currentGenreIndex == genreIndex))
95         {
96                 totalCount = __pContentList->GetCount();
97         }
98         else
99         {
100                 IList* pContentSearchResultList = CommonUtil::ContentSearchResultListN(
101                                                                                         *(static_cast<String*>(__pGnereList->GetAt(genreIndex))), STRING_SORT_GENRE, STRING_SORT_TITLE);
102                 if (pContentSearchResultList == null)
103                 {
104                         AppLogDebug("CommonUtil::ContentSearchResultListN() failed");
105                         return INIT_VALUE;
106                 }
107
108                 totalCount = pContentSearchResultList->GetCount();
109
110                 pContentSearchResultList->RemoveAt(true);
111                 delete pContentSearchResultList;
112         }
113         AppLogDebug("EXIT");
114         return totalCount;
115 }
116
117 int
118 GenreListPresentationModel::GetAllGenreCount(void)
119 {
120         AppLogDebug("ENTER");
121         if (__pGnereList == null)
122         {
123                 return INIT_VALUE;
124         }
125         AppLogDebug("EXIT");
126         return __pGnereList->GetCount();
127 }
128
129 ContentInformation*
130 GenreListPresentationModel::GetGenreInfoN(int genreIndex)
131 {
132         AppLogDebug("ENTER");
133         if (__pGnereList == null)
134         {
135                 return null;
136         }
137
138         IList* pContentSearchResultList = CommonUtil::ContentSearchResultListN(
139                                                                                 *(static_cast<String*>(__pGnereList->GetAt(genreIndex))), STRING_SORT_GENRE, STRING_SORT_TITLE);
140         if (pContentSearchResultList == null)
141         {
142                 AppLogDebug("CommonUtil::ContentSearchResultListN() failed");
143                 return null;
144         }
145
146         ContentInformation* pContentInformation = null;
147         ContentSearchResult* pResult = static_cast<ContentSearchResult*>(pContentSearchResultList->GetAt(INIT_VALUE));
148         if (pResult != null && pResult->GetContentType() == CONTENT_TYPE_AUDIO)
149         {
150                 pContentInformation = CommonUtil::GetContentInformationN(*static_cast<AudioContentInfo*>(pResult->GetContentInfo()));
151         }
152
153         pContentSearchResultList->RemoveAll(true);
154         delete pContentSearchResultList;
155
156         AppLogDebug("EXIT");
157         return pContentInformation;
158 }
159
160 Tizen::Base::String*
161 GenreListPresentationModel::GetGenreName(int genreIndex)
162 {
163         AppLogDebug("ENTER");
164         if (__pGnereList == null)
165         {
166                 return null;
167         }
168
169         AppLogDebug("EXIT");
170         return static_cast<String*>(__pGnereList->GetAt(genreIndex));
171 }
172
173 void
174 GenreListPresentationModel::InitializeContentList(int genreIndex)
175 {
176         AppLogDebug("ENTER");
177         if (__pContentList != null)
178         {
179                 __pContentList->RemoveAll(true);
180                 delete __pContentList;
181         }
182
183         __currentGenreIndex = genreIndex;
184         __pContentList = CommonUtil::ContentSearchResultListN(
185                                                                                 *(static_cast<String*>(__pGnereList->GetAt(genreIndex))), STRING_SORT_GENRE, STRING_SORT_TITLE);
186         AppLogDebug("EXIT");
187 }
188
189 ContentInformation*
190 GenreListPresentationModel::GetContentInfoN(int contentIndex)
191 {
192         AppLogDebug("ENTER");
193         ContentInformation* pContentInfoStruct = null;
194         ContentSearchResult* pResult = static_cast<ContentSearchResult*>(__pContentList->GetAt(contentIndex));
195         if (pResult != null && pResult->GetContentType() == CONTENT_TYPE_AUDIO)
196         {
197                 pContentInfoStruct = CommonUtil::GetContentInformationN(*static_cast<AudioContentInfo*>(pResult->GetContentInfo()));
198         }
199
200         AppLogDebug("EXIT");
201         return pContentInfoStruct;
202 }
203
204 Tizen::Base::Collection::ArrayList*
205 GenreListPresentationModel::GetContentPathListN(int genreIndex)
206 {
207         AppLogDebug("ENTER");
208         if (__pGnereList == null)
209         {
210                 AppLogDebug("EXIT");
211                 return null;
212         }
213
214         IList* pContentSearchResultList = CommonUtil::ContentSearchResultListN(
215                                                                                 *(static_cast<String*>(__pGnereList->GetAt(genreIndex))), STRING_SORT_GENRE, STRING_SORT_TITLE);
216         if (pContentSearchResultList == null)
217         {
218                 AppLogDebug("CommonUtil::ContentSearchResultListN() failed");
219                 return null;
220         }
221
222         ArrayList* pContentPath = new (std::nothrow) ArrayList();
223         pContentPath->Construct();
224
225         int totalCount = pContentSearchResultList->GetCount();
226         for (int iCount = 0; iCount < totalCount; iCount++)
227         {
228                 ContentSearchResult* pResult = static_cast<ContentSearchResult*>(pContentSearchResultList->GetAt(iCount));
229                 pContentPath->Add(*(new (std::nothrow) String(pResult->GetContentInfo()->GetContentPath())));
230         }
231
232         pContentSearchResultList->RemoveAt(true);
233         delete pContentSearchResultList;
234
235         AppLogDebug("EXIT");
236         return pContentPath;
237 }
238
239 void
240 GenreListPresentationModel::RefreshContentList(int genreIndex)
241 {
242         AppLogDebug("ENTER");
243         if (__pContentList != null)
244         {
245                 __pContentList->RemoveAll(true);
246                 delete __pContentList;
247         }
248
249         __pContentList = CommonUtil::ContentSearchResultListN(
250                                                                                 *(static_cast<String*>(__pGnereList->GetAt(genreIndex))), STRING_SORT_GENRE, STRING_SORT_TITLE);
251         if (__pContentList != null && __pContentList->GetCount() == INIT_VALUE)
252         {
253                 delete __pContentList;
254                 __pContentList = null;
255         }
256         __currentGenreIndex = genreIndex;
257         AppLogDebug("EXIT");
258 }
259
260 int
261 GenreListPresentationModel::GetCurrentGenreIndex(void)
262 {
263         AppLogDebug("ENTER");
264         AppLogDebug("EXIT");
265         return __currentGenreIndex;
266 }
267
268 void
269 GenreListPresentationModel::AddContent(Tizen::Base::String path, int playlistIndex)
270 {
271         AppLogDebug("ENTER");
272         ContentInformation* pContentInfoStruct = GetContentInfoN(playlistIndex);
273         PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(path);
274         pPlayList->AddItem(pContentInfoStruct->contentId);
275
276         delete pContentInfoStruct;
277         pContentInfoStruct = null;
278
279         delete pPlayList;
280         pPlayList = null;
281         AppLogDebug("EXIT");
282 }
283
284 result
285 GenreListPresentationModel::UpdateGnereList(void)
286 {
287         AppLogDebug("ENTER");
288         result r = E_SUCCESS;
289
290         if (__pGnereList != null)
291         {
292                 __pGnereList->RemoveAll(true);
293                 delete __pGnereList;
294         }
295
296         __pGnereList = CommonUtil::SearchContentList(STRING_SORT_GENRE);
297         TryCatch(__pGnereList != null, r = E_FAILURE, "__pGnereList is null");
298
299         AppLogDebug("EXIT");
300         return r;
301
302 CATCH:
303         return r;
304 }