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