Fixed jira issue and fixed prevent issue
[apps/osp/MusicPlayer.git] / src / MpFolderListPresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (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                MpFolderListPresentationModel.cpp
19  * @brief               This is the implementation file for FolderListPresentationModel class.
20  */
21
22 #include "MpFolderListPresentationModel.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::Io;
29 using namespace Tizen::Social;
30 using namespace Tizen::Ui::Controls;
31
32 FolderListPresentationModel* FolderListPresentationModel::pFolderListPresentationModelInstance = null;
33
34 FolderListPresentationModel::FolderListPresentationModel(void)
35         : __pFolderPathList(null)
36         , __pContentList(null)
37         , __currentFolderIndex(-1)
38 {
39         AppLogDebug("ENTER");
40         AppLogDebug("EXIT");
41 }
42
43 FolderListPresentationModel::~FolderListPresentationModel(void)
44 {
45         AppLogDebug("ENTER");
46         AppLogDebug("EXIT");
47 }
48
49 FolderListPresentationModel*
50 FolderListPresentationModel::GetInstance(void)
51 {
52         AppLogDebug("ENTER");
53         if (pFolderListPresentationModelInstance == null)
54         {
55                 pFolderListPresentationModelInstance = new (std::nothrow) FolderListPresentationModel();
56                 TryReturn(pFolderListPresentationModelInstance != null, null, "FolderListPresentationModel instance is not created.");
57
58                 result r = pFolderListPresentationModelInstance->Construct();
59                 TryCatch(r == E_SUCCESS, , null, "FolderListPresentationModel instance is not constrcuted.");
60         }
61         AppLogDebug("EXIT");
62         return pFolderListPresentationModelInstance;
63
64 CATCH:
65         delete pFolderListPresentationModelInstance;
66         return null;
67 }
68
69 Tizen::Base::Collection::IList*
70 FolderListPresentationModel::ContentDirectoryPathList(void)
71 {
72         AppLogDebug("ENTER");
73         ContentDirectory directory;
74         result r = directory.Construct(CONTENT_TYPE_AUDIO);
75         if (IsFailed(r))
76         {
77                 AppLogDebug("EXIT");
78                 return null;
79         }
80         AppLogDebug("EXIT");
81         return directory.GetContentDirectoryPathListN(SORT_ORDER_ASCENDING);
82 }
83
84 result
85 FolderListPresentationModel::Construct(void)
86 {
87         AppLogDebug("ENTER");
88         result r = E_SUCCESS;
89         AppLogDebug("EXIT");
90         return r;
91 }
92
93 int
94 FolderListPresentationModel::GetAllFolderCount(void)
95 {
96         AppLogDebug("ENTER");
97         if (__pFolderPathList == null)
98         {
99                 return INIT_VALUE;
100         }
101         AppLogDebug("EXIT");
102         return __pFolderPathList->GetCount();
103 }
104
105 int
106 FolderListPresentationModel::GetContentCount(int folderIndex)
107 {
108         AppLogDebug("ENTER");
109         result r = E_SUCCESS;
110         int totalCount = INIT_VALUE;
111
112         if ((__pContentList != null) && (__currentFolderIndex == folderIndex))
113         {
114                 totalCount = __pContentList->GetCount();
115         }
116         else
117         {
118                 if (__pFolderPathList == null)
119                 {
120                         AppLogDebug("EXIT");
121                         return INIT_VALUE;
122                 }
123
124                 String* pStrPath = static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
125                 ContentDirectory directory;
126                 r = directory.Construct(CONTENT_TYPE_AUDIO);
127
128                 if (IsFailed(r) || pStrPath == null)
129                 {
130                         AppLogDebug("EXIT");
131                         return INIT_VALUE;
132                 }
133
134                 totalCount = directory.GetContentDirectoryItemCount(*pStrPath);
135                 if (IsFailed(GetLastResult()))
136                 {
137                         AppLogDebug("EXIT");
138                         return INIT_VALUE;
139                 }
140         }
141         AppLogDebug("EXIT");
142         return totalCount;
143 }
144
145 ContentInformation*
146 FolderListPresentationModel::GetFolderInfoN(int folderIndex)
147 {
148         AppLogDebug("ENTER");
149         if (__pFolderPathList == null)
150         {
151                 return null;
152         }
153
154         String* pFolderPath = static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
155
156         ContentDirectory contentDirectory;
157         result r = contentDirectory.Construct(CONTENT_TYPE_AUDIO);
158         if (IsFailed(r) || pFolderPath == null)
159         {
160                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
161                 return null;
162         }
163
164         IList* pSearchList = contentDirectory.GetContentDirectoryItemListN(*pFolderPath, SEARCH_PAGE_NO, SEARCH_COUNT_PER_PAGE, STRING_SORT_TITLE, SORT_ORDER_ASCENDING);
165         if (pSearchList == null || IsFailed(GetLastResult()))
166         {
167                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
168                 if (pSearchList != null)
169                 {
170                         pSearchList->RemoveAll();
171                         delete pSearchList;
172                 }
173                 return null;
174         }
175
176         ContentInformation* pContentInfoStruct = null;
177         ContentInfo* pContentInfo = static_cast<ContentInfo*>(pSearchList->GetAt(INIT_VALUE));
178         AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
179
180         if (pAudioContentInfo != null && pAudioContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
181         {
182                 pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
183         }
184
185         pSearchList->RemoveAll(true);
186         delete pSearchList;
187         pSearchList = null;
188
189         AppLogDebug("EXIT");
190         return pContentInfoStruct;
191 }
192
193 Tizen::Base::Collection::ArrayList*
194 FolderListPresentationModel::GetContentPathListN(int folderIndex)
195 {
196         AppLogDebug("ENTER");
197         if (__pFolderPathList == null)
198         {
199                 AppLogDebug("EXIT");
200                 return null;
201         }
202
203         ContentDirectory contentDirectory;
204         result r = contentDirectory.Construct(CONTENT_TYPE_AUDIO);
205         if (IsFailed(r))
206         {
207                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
208                 return null;
209         }
210
211         IList* pSearchList = null;
212         String* pFolderPath = static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
213         if (pFolderPath != null)
214         {
215                 pSearchList = contentDirectory.GetContentDirectoryItemListN(*pFolderPath, SEARCH_PAGE_NO, SEARCH_COUNT_PER_PAGE, STRING_SORT_TITLE, SORT_ORDER_ASCENDING);
216         }
217
218         if (pSearchList == null || IsFailed(GetLastResult()))
219         {
220                 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
221                 if (pSearchList != null)
222                 {
223                         pSearchList->RemoveAll();
224                 }
225                 delete pSearchList;
226                 return null;
227         }
228
229         ArrayList* pContentPathList = new (std::nothrow) ArrayList();
230         pContentPathList->Construct();
231
232         for (int iCount = 0; iCount < pSearchList->GetCount(); iCount++)
233         {
234                 ContentInfo* pContentInfo = static_cast<ContentInfo*>(pSearchList->GetAt(iCount));
235                 if (pContentInfo != null)
236                 {
237                         pContentPathList->Add(*(new (std::nothrow) String(pContentInfo->GetContentPath())));
238                 }
239         }
240
241         pSearchList->RemoveAll(true);
242         delete pSearchList;
243
244         AppLogDebug("EXIT");
245         return pContentPathList;
246 }
247
248 Tizen::Base::String*
249 FolderListPresentationModel::GetFolderPath(int folderIndex)
250 {
251         AppLogDebug("ENTER");
252         if (__pFolderPathList == null)
253         {
254                 AppLogDebug("EXIT");
255                 return null;
256         }
257         AppLogDebug("EXIT");
258         return static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
259 }
260
261 void
262 FolderListPresentationModel::InitializeContentList(int folderIndex)
263 {
264         AppLogDebug("ENTER");
265         if (__pContentList != null)
266         {
267                 __pContentList->RemoveAll(true);
268                 delete __pContentList;
269                 __pContentList = null;
270         }
271
272         ContentDirectory contentDirectory;
273         result r = contentDirectory.Construct(CONTENT_TYPE_AUDIO);
274         if (IsFailed(r) || __pFolderPathList == null)
275         {
276                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
277                 return;
278         }
279
280         String* pFolderPath = static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
281         if (pFolderPath != null)
282         {
283                 __pContentList = contentDirectory.GetContentDirectoryItemListN(*pFolderPath, SEARCH_PAGE_NO, SEARCH_COUNT_PER_PAGE, STRING_SORT_TITLE, SORT_ORDER_ASCENDING);
284         }
285         __currentFolderIndex = folderIndex;
286         AppLogDebug("EXIT");
287 }
288
289 ContentInformation*
290 FolderListPresentationModel::GetContentInfoN(int contentIndex)
291 {
292         AppLogDebug("ENTER");
293         if (__pContentList == null || contentIndex < INIT_VALUE)
294         {
295                 AppLogDebug("EXIT(index(%d) MUST be greater than or equal to 0 OR __pContentList is null)", contentIndex);
296                 return null;
297         }
298
299         ContentInformation* pContentInfoStruct = null;
300         ContentInfo* pContentInfo = static_cast<ContentInfo*>(__pContentList->GetAt(contentIndex));
301         if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
302         {
303                 AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
304                 if (pAudioContentInfo != null)
305                 {
306                         pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
307                 }
308         }
309
310         AppLogDebug("EXIT");
311         return pContentInfoStruct;
312 }
313
314 Tizen::Base::String*
315 FolderListPresentationModel::GetFolderName(int folderIndex)
316 {
317         AppLogDebug("ENTER");
318         if (__pFolderPathList == null)
319         {
320                 return null;
321         }
322
323         AppLogDebug("EXIT");
324         return static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
325 }
326
327 void
328 FolderListPresentationModel::RemoveFolderList(int folderIndex)
329 {
330         AppLogDebug("ENTER");
331         IList* pRemoveFolderList = null;
332         int totalCount = 0;
333
334         ContentManager contentManager;
335         result r = contentManager.Construct();
336         if (IsFailed(r))
337         {
338                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
339                 return ;
340         }
341
342         ContentDirectory contentDirectory;
343         r = contentDirectory.Construct(CONTENT_TYPE_AUDIO);
344         if (IsFailed(r))
345         {
346                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
347                 return;
348         }
349
350         if (__pFolderPathList != null)
351         {
352                 String* pFolderPath = static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
353                 if (pFolderPath != null)
354                 {
355                         pRemoveFolderList = contentDirectory.GetContentDirectoryItemListN(*pFolderPath, SEARCH_PAGE_NO, SEARCH_COUNT_PER_PAGE, STRING_SORT_TITLE, SORT_ORDER_ASCENDING);
356                 }
357         }
358         else
359         {
360                 AppLogDebug("EXIT(__pFolderPathList is null)");
361                 return;
362         }
363
364         totalCount = pRemoveFolderList->GetCount();
365         for (int iCount = 0; iCount < totalCount; iCount++)
366         {
367                 ContentInfo* pContentInfo = static_cast<ContentInfo*>(pRemoveFolderList->GetAt(iCount));
368                 if (pContentInfo != null && pContentInfo->GetContentType() == CONTENT_TYPE_AUDIO)
369                 {
370                         AudioContentInfo* pAudioContentInfo = static_cast<AudioContentInfo*>(pContentInfo);
371                         ContentInformation* pContentInfoStruct = CommonUtil::GetContentInformationN(*pAudioContentInfo);
372                         if (pContentInfoStruct != null)
373                         {
374                                 contentManager.DeleteContent(pContentInfoStruct->contentId);
375                                 delete pContentInfoStruct;
376                         }
377                 }
378         }
379
380         pRemoveFolderList->RemoveAll(true);
381         delete pRemoveFolderList;
382         AppLogDebug("EXIT");
383 }
384
385 void
386 FolderListPresentationModel::RefreshContentList(int folderIndex)
387 {
388         AppLogDebug("ENTER");
389         ContentDirectory contentDirectory;
390         result r = contentDirectory.Construct(CONTENT_TYPE_AUDIO);
391         if (IsFailed(r))
392         {
393                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
394                 return;
395         }
396
397         if (__pContentList != null)
398         {
399                 __pContentList->RemoveAll(true);
400                 delete __pContentList;
401                 __pContentList = null;
402         }
403
404         String* pFolderPath = static_cast<String*>(__pFolderPathList->GetAt(folderIndex));
405         if (pFolderPath != null)
406         {
407                 __pContentList = contentDirectory.GetContentDirectoryItemListN(*pFolderPath, SEARCH_PAGE_NO, SEARCH_COUNT_PER_PAGE, STRING_SORT_TITLE, SORT_ORDER_ASCENDING);
408         }
409
410         __currentFolderIndex = folderIndex;
411         AppLogDebug("EXIT");
412 }
413
414 int
415 FolderListPresentationModel::GetCurrentFolderIndex(void)
416 {
417         AppLogDebug("ENTER");
418         AppLogDebug("EXIT");
419         return __currentFolderIndex;
420 }
421
422 void
423 FolderListPresentationModel::AddContent(String path, int playlistIndex)
424 {
425         AppLogDebug("ENTER");
426         ContentInformation* pContentInfoStruct = GetContentInfoN(playlistIndex);
427         PlayList* pPlayList = PlayListManager::GetInstance()->GetPlayListN(path);
428         if (pContentInfoStruct != null)
429         {
430                 pPlayList->AddItem(pContentInfoStruct->contentId);
431                 delete pContentInfoStruct;
432         }
433
434         delete pPlayList;
435         AppLogDebug("EXIT");
436 }
437
438 result
439 FolderListPresentationModel::UpdateFolderPathList(void)
440 {
441         AppLogDebug("ENTER");
442         if (__pFolderPathList != null)
443         {
444                 __pFolderPathList->RemoveAll(true);
445                 delete __pFolderPathList;
446         }
447
448         __pFolderPathList = ContentDirectoryPathList();
449         result r = GetLastResult();
450         if (__pFolderPathList == null)
451         {
452                 r = E_FAILURE;
453         }
454
455         AppLogDebug("EXIT");
456         return r;
457 }