Tizen 2.1 base
[platform/framework/native/content.git] / src / FCnt_ContentDirectoryImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18  * @file                FCnt_ContentDirectoryImpl.cpp
19  * @brief               This is the implementation file for the %_ContentDirectoryImpl class.
20  *
21  * This file contains implementation of the %_ContentDirectoryImpl class.
22  */
23
24 #include <FBaseSysLog.h>
25 #include <FBaseInteger.h>
26 #include <FBaseLongLong.h>
27 #include <FBaseFloat.h>
28 #include <FBaseColIList.h>
29 #include <FBaseColIEnumeratorT.h>
30 #include <FCntContentDirectory.h>
31 #include <FCntContentSearchResult.h>
32 #include <FSysEnvironment.h>
33 #include <FBase_StringConverter.h>
34 #include <FCnt_ContentUtility.h>
35 #include <FCnt_ContentDirectoryImpl.h>
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Io;
40 using namespace Tizen::System;
41
42 namespace Tizen { namespace Content
43 {
44 // Declaration for Callback function registered to each media info details
45 bool MediaFoldersCb(media_folder_h folder, void* pUserdata);
46 // Declaration for Callback function registered to each media info details
47 bool MediaFolderItemsCb(media_info_h media, void* pUserdata);
48
49 _ContentDirectoryImpl::_ContentDirectoryImpl(void)
50         : Object()
51         , __pFilterHandle(null)
52         , __pFinalOutList(null)
53         , __contentType(CONTENT_TYPE_UNKNOWN)
54         , __isMultiContentType(false)
55         , __multiContentTypeExpr(L"")
56 {
57
58 }
59
60 // (disconnects the DB connection)
61 _ContentDirectoryImpl::~_ContentDirectoryImpl(void)
62 {
63         int ret = MEDIA_CONTENT_ERROR_NONE;
64         result r = E_SUCCESS;
65
66         ret = media_content_disconnect();
67         r = MapCoreErrorToNativeResult(ret);
68         SysTryLog(NID_CNT, r == E_SUCCESS, "[%s] Propagating for media_content_disconnect.", GetErrorMessage(r));
69 }
70
71  _ContentDirectoryImpl*
72  _ContentDirectoryImpl::GetInstance(ContentDirectory& contentDirectory)
73 {
74         return (&contentDirectory != null) ? contentDirectory.__pImpl : null;
75 }
76
77 const _ContentDirectoryImpl*
78 _ContentDirectoryImpl::GetInstance(const ContentDirectory& contentDirectory)
79 {
80         return (&contentDirectory != null) ? contentDirectory.__pImpl : null;
81 }
82
83 //make a connection to DB
84 result
85 _ContentDirectoryImpl::Construct(ContentType type)
86 {
87         result r = E_SUCCESS;
88         int ret = MEDIA_CONTENT_ERROR_NONE;
89
90         ret = media_content_connect();
91         r = MapCoreErrorToNativeResult(ret);
92         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Propagating for media_content_connect.");
93
94         __contentType = type;
95         __isMultiContentType = false;
96         __multiContentTypeExpr.Clear();
97
98         return  r;
99 }
100
101 //make a connection to DB
102 result
103 _ContentDirectoryImpl::Construct(const Tizen::Base::Collection::IListT<ContentType>& contentTypeList)
104 {
105         result r = E_SUCCESS;
106         int ret = MEDIA_CONTENT_ERROR_NONE;
107         ContentType     contentType = CONTENT_TYPE_UNKNOWN;
108
109         ret = media_content_connect();
110         r = MapCoreErrorToNativeResult(ret);
111         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Propagating for media_content_connect.");
112
113         __multiContentTypeExpr.Clear();
114
115         std::unique_ptr<IEnumeratorT<ContentType> > pEnum(contentTypeList.GetEnumeratorN());
116
117         while ((pEnum.get() != NULL) && (pEnum->MoveNext() == E_SUCCESS))
118         {
119                 if (!__multiContentTypeExpr.IsEmpty())
120                 {
121                         r = __multiContentTypeExpr.Append("OR ");
122                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
123                 }
124                 pEnum->GetCurrent(contentType);
125                 switch (contentType)
126                 {
127                         // Image-0,video-1,sound-2,music-3,other-4
128                 case CONTENT_TYPE_OTHER:
129                         r = __multiContentTypeExpr.Append("MEDIA_TYPE=4 ");
130                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
131                         break;
132                 case CONTENT_TYPE_IMAGE:
133                         r = __multiContentTypeExpr.Append("MEDIA_TYPE=0 ");
134                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
135                         break;
136                 case CONTENT_TYPE_AUDIO:
137                         r = __multiContentTypeExpr.Append("(MEDIA_TYPE=2 or MEDIA_TYPE=3) ");
138                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");;
139                         break;
140                 case CONTENT_TYPE_VIDEO:
141                         r = __multiContentTypeExpr.Append("MEDIA_TYPE=1 ");
142                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
143                         break;
144                 case CONTENT_TYPE_ALL:
145                         //If content type is CONTENT_TYPE_ALL, then MEDIA_TYPE is empty
146                         break;
147                 default:
148                         break;
149                 }
150         }
151
152         r = __multiContentTypeExpr.Insert('(', 0);
153         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
154
155         r = __multiContentTypeExpr.Insert(')', __multiContentTypeExpr.GetLength());
156         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
157
158         __isMultiContentType = true;
159
160         return  r;
161 }
162
163 result
164 _ContentDirectoryImpl::CreateFolderFilter(bool isMultiContentType, const Tizen::Base::String& inputFolderPath) const
165 {
166         result r = E_SUCCESS;
167         std::unique_ptr<filter_h, FilterHandleDeleter> pFilterHandle(new (std::nothrow) filter_h);
168         int ret = MEDIA_CONTENT_ERROR_NONE;
169         String inputCondition = L"";
170         String folderPath(inputFolderPath);
171
172         ret = media_filter_create(pFilterHandle.get());
173         r = MapCoreErrorToNativeResult(ret);
174         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_create operation.");
175
176         if (isMultiContentType)
177         {
178                 r = inputCondition.Append(__multiContentTypeExpr);
179                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
180         }
181         else
182         {
183                 switch (__contentType)
184                 {
185                         // Image-0,video-1,sound-2,music-3,other-4
186                 case CONTENT_TYPE_OTHER:
187                         inputCondition = "MEDIA_TYPE=4 ";
188                         break;
189                 case CONTENT_TYPE_IMAGE:
190                         inputCondition = "MEDIA_TYPE=0 ";
191                         break;
192                 case CONTENT_TYPE_AUDIO:
193                         inputCondition = "(MEDIA_TYPE=2 or MEDIA_TYPE=3) ";
194                         break;
195                 case CONTENT_TYPE_VIDEO:
196                         inputCondition = "MEDIA_TYPE=1 ";
197                         break;
198                 case CONTENT_TYPE_ALL:
199                         //If content type is CONTENT_TYPE_ALL, then MEDIA_TYPE is empty
200                         break;
201                 default:
202                         break;
203                 }
204         }
205
206         if (!folderPath.IsEmpty())
207         {
208                 if (!inputCondition.IsEmpty()) //For CONTENT_TYPE_ALL inputCondition is empty
209                 {
210                         r = inputCondition.Append("AND FOLDER_PATH = ");
211                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
212                 }
213                 else
214                 {
215                         r = inputCondition.Append("FOLDER_PATH = ");
216                         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
217                 }
218
219                 r = folderPath.Insert('"', 0);
220                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation.");
221                 r = folderPath.Insert('"', folderPath.GetLength());
222                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation.");
223
224                 r = inputCondition.Append(folderPath);
225                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
226         }
227
228         if (!inputCondition.IsEmpty())
229         {
230                 //CopyToCharArrayN: utility function, converts a osp string to char*
231                 std::unique_ptr<char[]> pInputCond(_StringConverter::CopyToCharArrayN(inputCondition));
232                 SysTryReturnResult(NID_CNT, (pInputCond.get())[0] != null, E_SYSTEM, "pInputCond is NULL.");
233
234                 SysLog(NID_CNT, "pInputCond is [%s].", pInputCond.get());
235
236                 if ((pInputCond.get())[0] != null)
237                 {
238                         ret = media_filter_set_condition(*(pFilterHandle.get()), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
239                         r = MapCoreErrorToNativeResult(ret);
240                         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
241                 }
242         }
243
244         __pFilterHandle.reset(pFilterHandle.release());
245
246         return  r;
247 }
248
249 int
250 _ContentDirectoryImpl::GetContentDirectoryCount(void) const
251 {
252         int directoryCount = 0;
253         result r = E_SUCCESS;
254         int ret = MEDIA_CONTENT_ERROR_NONE;
255
256         r = CreateFolderFilter(__isMultiContentType, L"");
257         SysTryReturnResult(NID_CNT, !IsFailed(r), E_SYSTEM, "Propagating for CreateFolderFilter.");
258
259         ret = media_folder_get_folder_count_from_db(*(__pFilterHandle.get()), &directoryCount);
260         r = MapCoreErrorToNativeResult(ret);
261         SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_folder_get_folder_count_from_db operation.");
262
263         SysLog(NID_CNT, "directoryCount is [%d].", directoryCount);
264
265         SetLastResult(r);
266         return directoryCount;
267 }
268
269 IList*
270 _ContentDirectoryImpl::GetContentDirectoryPathListN(Tizen::Base::SortOrder sortOrder) const
271 {
272         result r = E_SUCCESS;
273         int ret = MEDIA_CONTENT_ERROR_NONE;
274         std::unique_ptr<GList, GListDeleter> pItemList;
275         GList* pTempList = null;
276         std::unique_ptr<char> pFolderPath;
277         char* pTempFolderPath = null;
278         std::unique_ptr<media_folder_s, FolderHandleDeleter> pFolderHandle;
279         std::unique_ptr<Object> pValue;
280
281         __pFinalOutList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
282         SysTryReturn(NID_CNT, __pFinalOutList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
283
284         r = __pFinalOutList->Construct();
285         SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to construct __pFinalOutList ArrayList.", GetErrorMessage(r));
286
287         r = CreateFolderFilter(__isMultiContentType, L"");
288         SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
289
290         if (sortOrder == SORT_ORDER_ASCENDING)
291         {
292                 ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_ASC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_DEFAULT);
293                 r = MapCoreErrorToNativeResult(ret);
294                 SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
295                                 "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
296         }
297         else if (sortOrder == SORT_ORDER_DESCENDING)
298         {
299                 ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_DEFAULT);
300                 r = MapCoreErrorToNativeResult(ret);
301                 SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
302                                 "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
303         }
304
305         pTempList = pItemList.get();
306         ret = media_folder_foreach_folder_from_db(*(__pFilterHandle.get()), MediaFoldersCb, &pTempList);
307         r = MapCoreErrorToNativeResult(ret);
308         SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_folder_foreach_folder_from_db operation.", GetErrorMessage(r));
309
310         SysTryReturn(NID_CNT, pTempList != NULL, null, r, "[%s] pItemList for media_folder_foreach_folder_from_db is null.", GetErrorMessage(r));
311
312         for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
313         {
314                 pFolderHandle.reset(static_cast<media_folder_h>(g_list_nth_data(pTempList, idx)));
315
316                 if (pFolderHandle.get() != NULL)
317                 {
318                         pTempFolderPath = pFolderPath.get();
319                         ret = media_folder_get_path(pFolderHandle.get(), &pTempFolderPath);
320                         r = MapCoreErrorToNativeResult(ret);
321                         SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_folder_get_path operation.", GetErrorMessage(r));
322
323                         if (pTempFolderPath != NULL)
324                         {
325                                 SysLog(NID_CNT, "pFolderPath is [%s].", pTempFolderPath);
326
327                                 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pTempFolderPath));
328                         }
329                         if (pValue != NULL)
330                         {
331                                 r = __pFinalOutList->Add(*(pValue.release()));
332                                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform Add operation to __pFinalOutList.", GetErrorMessage(r));
333                         }
334                 }
335         }
336
337         SetLastResult(r);
338         return __pFinalOutList.release();
339 }
340
341 int
342 _ContentDirectoryImpl::GetContentDirectoryItemCount(const Tizen::Base::String& contentDirectoryPath) const
343 {
344         SysLog(NID_CNT, "contentDirectoryPath is [%ls]", contentDirectoryPath.GetPointer());
345
346         int directoryItemCount = 0;
347         result r = E_SUCCESS;
348         int ret = MEDIA_CONTENT_ERROR_NONE;
349         GList* pItemList = NULL;
350         std::unique_ptr<char> pFolderId;
351         char* pTempFolderId = null;
352         std::unique_ptr<media_folder_s, FolderHandleDeleter> pFolderHandle;
353
354         SysTryReturn(NID_CNT, CheckMediaPath(contentDirectoryPath), directoryItemCount, E_INVALID_ARG,
355                         "[E_INVALID_ARG] Failed to perform CheckMediaPath operation.");
356
357         r = CreateFolderFilter(__isMultiContentType, contentDirectoryPath);
358         SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
359
360         ret = media_folder_foreach_folder_from_db(*(__pFilterHandle.get()), MediaFoldersCb, &pItemList);
361         r = MapCoreErrorToNativeResult(ret);
362         SysTryReturn(NID_CNT, r == E_SUCCESS, directoryItemCount, r,
363                         "[%s] Failed to perform media_folder_foreach_folder_from_db operation.", GetErrorMessage(r));
364
365         SysTryReturn(NID_CNT, pItemList != NULL, directoryItemCount, r,
366                         "[%s] pItemList for media_folder_foreach_folder_from_db is null.", GetErrorMessage(r));
367
368         for (int idx = 0; idx < (int)g_list_length(pItemList); idx++)
369         {
370                 SysLog(NID_CNT, "idx is [%d] and (int)g_list_length(pItemList) is [%d].", idx, (int)g_list_length(pItemList));
371
372                 pFolderHandle.reset(static_cast<media_folder_h>(g_list_nth_data(pItemList, idx)));
373
374                 if (pFolderHandle.get() != NULL)
375                 {
376                         pTempFolderId = pFolderId.get();
377                         ret = media_folder_get_folder_id(pFolderHandle.get(), &pTempFolderId);
378                         r = MapCoreErrorToNativeResult(ret);
379                         SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, r,
380                                         "[%s] Failed to perform media_folder_get_folder_id operation.", GetErrorMessage(r));
381                 }
382                 else
383                 {
384                         r = E_SYSTEM;
385                         SysTryReturn(NID_CNT, r != E_SUCCESS, directoryItemCount, r, "[E_SYSTEM] pFolderHandle is null.");
386                 }
387         }
388
389         if (pTempFolderId != NULL)
390         {
391                 r = CreateFolderFilter(__isMultiContentType, L"");
392                 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
393
394                 ret = media_folder_get_media_count_from_db(pTempFolderId, *(__pFilterHandle.get()), &directoryItemCount);
395                 r = MapCoreErrorToNativeResult(ret);
396                 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, r,
397                                 "[%s] Failed to perform media_folder_get_media_count_from_db operation.", GetErrorMessage(r));
398         }
399
400         SysLog(NID_CNT, "directoryItemCount is [%d].", directoryItemCount);
401
402         SetLastResult(r);
403         return directoryItemCount;
404 }
405
406 // Osp column names are mapped with slp column names
407 // CONTENT_TYPE_OTHER and CONTENT_TYPE_IMAGE (0 - 13 ) are valid columns
408 // CONTENT_TYPE_VIDEO  (0 - 16 ) are valid columns
409 // CONTENT_TYPE_ALL and  CONTENT_TYPE_VIDEO (0 - 18 ) are valid columns
410 // if the given osp column is out of the specified range of the type, E_INVALID_ARG is retuned.
411 result
412 _ContentDirectoryImpl::GetSlpColumnName(String& inputCol, String sortCol) const
413 {
414         String          ospColumnName(L"");
415         String          slpColumnName(L"");
416         String          columnName(sortCol);
417         result          r = E_SUCCESS;
418         int             maxCols = MAX_QUERY_COLUMNS;
419         
420         if(!__isMultiContentType)
421         {
422                 switch (__contentType)
423                 {
424                 case CONTENT_TYPE_OTHER:
425                         //fall through
426                 case CONTENT_TYPE_IMAGE:
427                         maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
428                         break;
429                 case CONTENT_TYPE_VIDEO:
430                         maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
431                         break;
432                 case CONTENT_TYPE_AUDIO:
433                         //fall through
434                 case CONTENT_TYPE_ALL:
435                         maxCols = MAX_QUERY_COLUMNS;
436                         break;
437                 default:
438                         break;
439                 }
440         }
441         else
442         {
443                 if(!__multiContentTypeExpr.IsEmpty())
444                 {
445                         if(__multiContentTypeExpr.Contains("MEDIA_TYPE=2"))
446                         {
447                                 maxCols = MAX_QUERY_COLUMNS;
448                         }
449                         else if(__multiContentTypeExpr.Contains("MEDIA_TYPE=1"))
450                         {
451                                 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
452                         }
453                         else if(__multiContentTypeExpr.Contains("MEDIA_TYPE=0"))
454                         {
455                                 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
456                         }
457                 }
458         }
459
460         for (int colIndex=0; colIndex < maxCols; colIndex++)
461         {
462                 ospColumnName.Clear();
463                 slpColumnName.Clear();
464
465                 ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
466                 slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
467
468                 ospColumnName.ToUpper();
469                 columnName.ToUpper();
470
471                 if (columnName == ospColumnName)
472                 {
473                         inputCol = slpColumnName;
474                         return r;
475                 }
476         }
477         return E_INVALID_ARG;
478 }
479
480 IList*
481 _ContentDirectoryImpl::GetContentDirectoryItemListN(const Tizen::Base::String& contentDirectoryPath, int pageNo, int countPerPage,
482                                                     const Tizen::Base::String& column, Tizen::Base::SortOrder sortOrder) const
483 {
484         SysLog(NID_CNT, "contentDirectoryPath is [%ls].", contentDirectoryPath.GetPointer());
485
486         result r = E_SUCCESS;
487         int totalCount = 0;
488         int totalPageCount = 0;
489         int ret = MEDIA_CONTENT_ERROR_NONE;
490         String slpColumn = L"";
491         std::unique_ptr<GList, GListDeleter> pItemList;
492         GList* pTempList = null;
493         std::unique_ptr<char> pFolderId;
494         char* pTempFolderId = null;
495         std::unique_ptr<media_folder_s, FolderHandleDeleter> pFolderHandle;
496         int offset = 0;
497
498         SysTryReturn(NID_CNT, CheckMediaPath(contentDirectoryPath), null, E_INVALID_ARG,
499                         "[E_INVALID_ARG] Failed to perform CheckMediaPath operation.");
500
501         __pFinalOutList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
502         SysTryReturn(NID_CNT, __pFinalOutList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
503
504         r = __pFinalOutList->Construct();
505         SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to construct __pFinalOutList ArrayList.", GetErrorMessage(r));
506
507         r = CreateFolderFilter(__isMultiContentType, contentDirectoryPath);
508         SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
509
510         if ((!column.IsEmpty()) && (sortOrder != SORT_ORDER_NONE))
511         {
512                 //__inputColumnName (osp column name) is replaced with slpColumn (slp column name).
513                 r = GetSlpColumnName(slpColumn, column);
514                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform GetSlpColumnName operation.", GetErrorMessage(r));
515
516                 //CopyToCharArrayN: utility function, converts a osp string to char*
517                 std::unique_ptr<char[]> pSortCol(_StringConverter::CopyToCharArrayN(slpColumn));
518                 SysTryReturn(NID_CNT, pSortCol.get() != null, null, E_SYSTEM, "[E_SYSTEM] pFolderId is NULL.");
519
520                 if (sortOrder == SORT_ORDER_ASCENDING)
521                 {
522                         ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_ASC, pSortCol.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
523                         r = MapCoreErrorToNativeResult(ret);
524                         SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
525                                         "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
526                 }
527                 else if (sortOrder == SORT_ORDER_DESCENDING)
528                 {
529                         ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_DESC, pSortCol.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
530                         r = MapCoreErrorToNativeResult(ret);
531                         SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
532                                         "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
533                 }
534         }
535
536         pTempList = pItemList.get();
537         ret = media_folder_foreach_folder_from_db(*(__pFilterHandle.get()), MediaFoldersCb, &pTempList);
538         r = MapCoreErrorToNativeResult(ret);
539         SysTryReturn(NID_CNT, !IsFailed(r), null, r,
540                         "[%s] Failed to perform media_folder_foreach_folder_from_db operation.", GetErrorMessage(r));
541
542         if (pTempList != NULL)
543         {
544                 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
545                 {
546                         SysLog(NID_CNT, "idx is [%d] and (int)g_list_length(pItemList) is [%d].", idx, (int)g_list_length(pTempList));
547
548                         pFolderHandle.reset(static_cast<media_folder_h>(g_list_nth_data(pTempList, idx)));
549
550                         if (pFolderHandle.get() != NULL)
551                         {
552                                 pTempFolderId = pFolderId.get();
553                                 ret = media_folder_get_folder_id(pFolderHandle.get(), &pTempFolderId);
554                                 r = MapCoreErrorToNativeResult(ret);
555                                 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
556                                                 "[%s] Failed to perform media_folder_get_folder_id operation.", GetErrorMessage(r));
557                         }
558                         else
559                         {
560                                 r = E_SYSTEM;
561                                 SysTryReturn(NID_CNT, r != E_SUCCESS, null, r, "[E_SYSTEM] pFolderHandle is null.");
562                         }
563                 }
564         }
565
566         if (pTempFolderId != NULL)
567         {
568                 r = CreateFolderFilter(__isMultiContentType, L"");
569                 SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
570
571                 ret = media_folder_get_media_count_from_db(pTempFolderId, *(__pFilterHandle.get()), &totalCount);
572                 r = MapCoreErrorToNativeResult(ret);
573                 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
574                                 "[%s] Failed to perform media_folder_get_media_count_from_db operation.", GetErrorMessage(r));
575
576                 SysLog(NID_CNT, "totalCount is [%d].", totalCount);
577         }
578                 
579         if (totalCount > 0)
580         {
581                 if ((totalCount % countPerPage) == 0)
582                 {
583                         totalPageCount = totalCount / countPerPage;
584                 }
585                 else
586                 {
587                         totalPageCount = (totalCount / countPerPage) + 1;
588                 }
589
590                 SysTryReturn(NID_CNT, ((pageNo >= 1) && (pageNo <= totalPageCount)) , NULL, E_INVALID_ARG, "[E_INVALID_ARG] (pageNo < 1) || (pageNo > totalPageCount).");
591
592                 offset = (pageNo * countPerPage) - countPerPage;
593
594                 SysLog(NID_CNT, "totalCount [%d] totalPageCount[%d] __countPerPage[%d] __pageNo[%d] offset[%d]",
595                                 totalCount, totalPageCount, countPerPage, pageNo, offset);
596
597                 ret = media_filter_set_offset(*(__pFilterHandle.get()),offset,countPerPage);
598                 r = MapCoreErrorToNativeResult(ret);
599                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_filter_set_offset operation.", GetErrorMessage(r));
600
601                 r = FillFinalOutList(pTempFolderId);
602                 SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform FillFinalOutList operation.");
603         }
604         else if (pageNo > 1)
605         {
606                 r = E_INVALID_ARG;
607                 SysTryReturn(NID_CNT, !(IsFailed(r)), null, r, "[E_INVALID_ARG] (pageNo > 1) and (totalcount = 0).");
608         }
609         
610         SetLastResult(r);
611         return __pFinalOutList.release();
612 }
613
614 result
615 _ContentDirectoryImpl::FillFinalOutList(char* pFolderId) const
616 {
617         int ret = MEDIA_CONTENT_ERROR_NONE;
618         result r = E_SUCCESS;
619         std::unique_ptr<GList, GListDeleter> pItemList;
620         GList* pTempList = NULL;
621         std::unique_ptr<media_info_s, MediaHandleDeleter> pMediaHandle;
622
623         ContentInfo::_ContentData*                      pContentData = null;
624         ImageContentInfo::_ImageContentData*            pImageContentData = null;
625         AudioContentInfo::_AudioContentData*            pAudioContentData = null;
626         VideoContentInfo::_VideoContentData*            pVideoContentData = null;
627
628         std::unique_ptr<ImageContentInfo> pImageContentInfo;
629         std::unique_ptr<AudioContentInfo> pAudioContentInfo;
630         std::unique_ptr<VideoContentInfo> pVideoContentInfo;
631         std::unique_ptr<OtherContentInfo> pOtherContentInfo;
632
633         pTempList = pItemList.get();
634         ret = media_folder_foreach_media_from_db(pFolderId, *(__pFilterHandle.get()), MediaFolderItemsCb, &pTempList);
635         r = MapCoreErrorToNativeResult(ret);
636         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_folder_foreach_media_from_db operation.");
637
638         SysTryReturnResult(NID_CNT, pTempList != NULL, r, "pItemList for media_info_foreach_media_from_db is null.");
639
640         media_content_type_e mediaType;
641
642         for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
643         {
644                 pMediaHandle.reset(static_cast<media_info_h>(g_list_nth_data(pTempList, idx)));
645
646                 ret = media_info_get_media_type(pMediaHandle.get(), &mediaType);
647                 r = MapCoreErrorToNativeResult(ret);
648                 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_info_get_media_type operation.");
649
650                 switch (mediaType)
651                 {
652                 case MEDIA_CONTENT_TYPE_OTHERS:
653                         pOtherContentInfo = std::unique_ptr<OtherContentInfo>(new (std::nothrow) OtherContentInfo);
654                         SysTryReturnResult(NID_CNT, pOtherContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pOtherContentInfo.");
655
656                         pContentData = pOtherContentInfo->GetContentData();
657                         SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
658
659                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
660                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
661
662                         // Shallow copy, adds just the pointer: not the element
663                         r = __pFinalOutList->Add(*(pOtherContentInfo.release()));
664                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to pFinalOutList.");
665
666                         break;
667                 case MEDIA_CONTENT_TYPE_IMAGE:
668                         pImageContentInfo = std::unique_ptr<ImageContentInfo>(new (std::nothrow) ImageContentInfo);
669                         SysTryReturnResult(NID_CNT, pImageContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pImageContentInfo.");
670
671                         pContentData = pImageContentInfo->GetContentData();
672                         SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
673
674                         pImageContentData = pImageContentInfo->GetImageContentData();
675                         SysTryReturnResult(NID_CNT, pImageContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
676
677                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
678                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
679
680                         r = _ContentUtility::FillImageContentData(pMediaHandle.get(), pImageContentData);
681                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform GetDataFromImageTable operation.");
682
683                         // Shallow copy, adds just the pointer: not the element
684                         r = __pFinalOutList->Add(*(pImageContentInfo.release()));
685                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to __pFinalOutList.");
686
687                         break;
688                 case MEDIA_CONTENT_TYPE_MUSIC:
689                         //fall through
690                 case MEDIA_CONTENT_TYPE_SOUND:
691                         pAudioContentInfo = std::unique_ptr<AudioContentInfo>(new (std::nothrow) AudioContentInfo);
692                         SysTryReturnResult(NID_CNT, pAudioContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pAudioContentInfo.");
693
694                         pContentData = pAudioContentInfo->GetContentData();
695                         SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
696
697                         pAudioContentData = pAudioContentInfo->GetAudioContentData();
698                         SysTryReturnResult(NID_CNT, pAudioContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
699
700                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
701                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
702
703                         r = _ContentUtility::FillAudioContentData(pMediaHandle.get(), pAudioContentData);
704                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillAudioContentData operation.");
705
706                         // Shallow copy, adds just the pointer: not the element
707                         r = __pFinalOutList->Add(*(pAudioContentInfo.release()));
708                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to __pFinalOutList.");
709
710                         break;
711                 case MEDIA_CONTENT_TYPE_VIDEO:
712                         pVideoContentInfo = std::unique_ptr<VideoContentInfo>(new (std::nothrow) VideoContentInfo);
713                         SysTryReturnResult(NID_CNT, pVideoContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pVideoContentInfo.");
714
715                         pContentData = pVideoContentInfo->GetContentData();
716                         SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
717
718                         pVideoContentData = pVideoContentInfo->GetVideoContentData();
719                         SysTryReturnResult(NID_CNT, pVideoContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
720
721                         r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
722                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
723
724                         r = _ContentUtility::FillVideoContentData(pMediaHandle.get(), pVideoContentData);
725                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillVideoContentData operation.");
726
727                         // Shallow copy, adds just the pointer: not the element
728                         r = __pFinalOutList->Add(*(pVideoContentInfo.release()));
729                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to __pFinalOutList.");
730
731                         break;
732                 default:
733                         break;
734                 }
735         }
736
737         return r;
738 }
739
740 bool
741 _ContentDirectoryImpl::CheckMediaPath(const Tizen::Base::String& directoryPath) const
742 {
743         String phoneStr = Environment::GetMediaPath();
744         String mmcStr = Environment::GetExternalStoragePath();
745
746         if (!(directoryPath.StartsWith(phoneStr, 0) || directoryPath.StartsWith(mmcStr , 0)))
747         {
748                 String checkPhone;
749                 result r = phoneStr.SubString(0, (phoneStr.GetLength() - 1), checkPhone);
750                 SysTryReturn(NID_CNT, !IsFailed(r), false, E_INVALID_ARG, "[E_INVALID_ARG] Failed to substring operation.");
751
752                 String checkMmc;
753                 r = mmcStr.SubString(0, (mmcStr.GetLength() - 1), checkMmc);
754                 SysTryReturn(NID_CNT, !IsFailed(r), false, E_INVALID_ARG, "[E_INVALID_ARG] Failed to substring operation.");
755
756                 SysTryReturn(NID_CNT, (directoryPath.Equals(checkPhone) || directoryPath.Equals(checkMmc)), false, E_INVALID_ARG,
757                                 "[E_INVALID_ARG] The contentDirectoryPath is not valid[%ls].", directoryPath.GetPointer());
758         }
759
760         return true;
761 }
762
763 result
764 _ContentDirectoryImpl::MapCoreErrorToNativeResult(int reason) const
765 {
766         result r = E_SUCCESS;
767
768         switch (reason)
769         {
770         case MEDIA_CONTENT_ERROR_NONE:
771                 r = E_SUCCESS;
772                 break;
773
774         case MEDIA_CONTENT_ERROR_DB_BUSY:
775                 r = E_SERVICE_BUSY;
776                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
777                 break;
778
779         case MEDIA_CONTENT_ERROR_DB_FAILED:
780                 r = E_SYSTEM;
781                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
782                 break;
783
784         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
785                 r = E_OUT_OF_MEMORY;
786                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
787                 break;
788
789         default:
790                 SysLog(NID_CNT, "default");
791                 r = E_SYSTEM;
792                 break;
793         }
794         return r;
795 }
796
797 // Callback function registered to each media info details
798 // all items are appended to the list
799 bool
800 MediaFoldersCb(media_folder_h folder, void* pUserdata)
801 {
802         int ret  = MEDIA_CONTENT_ERROR_NONE;
803         media_folder_h new_folder = NULL;
804         ret = media_folder_clone(&new_folder, folder);
805         SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Propagating for media_folder_clone.");
806
807         GList** pList = (GList**)pUserdata;
808         *pList = g_list_append(*pList, new_folder);
809
810         return true;
811 }
812
813 // Callback function registered to each media info details
814 // all items are appended to the list
815 bool
816 MediaFolderItemsCb(media_info_h media, void* pUserdata)
817 {
818         int ret  = MEDIA_CONTENT_ERROR_NONE;
819         media_info_h new_media = NULL;
820         ret = media_info_clone(&new_media, media);
821         SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Propagating for media_info_clone.");
822
823         GList** pList = (GList**)pUserdata;
824         *pList = g_list_append(*pList, new_media);
825
826         return true;
827 }
828
829 }}