2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FCnt_ContentDirectoryImpl.cpp
19 * @brief This is the implementation file for the %_ContentDirectoryImpl class.
21 * This file contains implementation of the %_ContentDirectoryImpl class.
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>
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Io;
40 using namespace Tizen::System;
42 namespace Tizen { namespace Content
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);
49 _ContentDirectoryImpl::_ContentDirectoryImpl(void)
51 , __pFilterHandle(null)
52 , __pFinalOutList(null)
53 , __contentType(CONTENT_TYPE_UNKNOWN)
54 , __isMultiContentType(false)
55 , __multiContentTypeExpr(L"")
60 // (disconnects the DB connection)
61 _ContentDirectoryImpl::~_ContentDirectoryImpl(void)
63 int ret = MEDIA_CONTENT_ERROR_NONE;
66 ret = media_content_disconnect();
67 r = MapCoreErrorToNativeResult(ret);
68 SysTryLog(NID_CNT, r == E_SUCCESS, "[%s] Propagating for media_content_disconnect.", GetErrorMessage(r));
70 SysLog(NID_CNT, "media_content_disconnect result[%d].", ret);
73 _ContentDirectoryImpl*
74 _ContentDirectoryImpl::GetInstance(ContentDirectory& contentDirectory)
76 return (&contentDirectory != null) ? contentDirectory.__pImpl : null;
79 const _ContentDirectoryImpl*
80 _ContentDirectoryImpl::GetInstance(const ContentDirectory& contentDirectory)
82 return (&contentDirectory != null) ? contentDirectory.__pImpl : null;
85 //make a connection to DB
87 _ContentDirectoryImpl::Construct(ContentType type)
90 int ret = MEDIA_CONTENT_ERROR_NONE;
92 ret = media_content_connect();
93 r = MapCoreErrorToNativeResult(ret);
94 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Propagating for media_content_connect.");
96 SysLog(NID_CNT, "media_content_connect result[%d].", ret);
99 __isMultiContentType = false;
100 __multiContentTypeExpr.Clear();
105 //make a connection to DB
107 _ContentDirectoryImpl::Construct(const Tizen::Base::Collection::IListT<ContentType>& contentTypeList)
109 result r = E_SUCCESS;
110 int ret = MEDIA_CONTENT_ERROR_NONE;
111 ContentType contentType = CONTENT_TYPE_UNKNOWN;
113 ret = media_content_connect();
114 r = MapCoreErrorToNativeResult(ret);
115 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Propagating for media_content_connect.");
117 SysLog(NID_CNT, "media_content_connect result[%d].", ret);
119 __multiContentTypeExpr.Clear();
121 std::unique_ptr<IEnumeratorT<ContentType> > pEnum(contentTypeList.GetEnumeratorN());
123 while ((pEnum.get() != NULL) && (pEnum->MoveNext() == E_SUCCESS))
125 if (!__multiContentTypeExpr.IsEmpty())
127 r = __multiContentTypeExpr.Append("OR ");
128 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
130 pEnum->GetCurrent(contentType);
133 // Image-0,video-1,sound-2,music-3,other-4
134 case CONTENT_TYPE_OTHER:
135 r = __multiContentTypeExpr.Append("MEDIA_TYPE=4 ");
136 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
138 case CONTENT_TYPE_IMAGE:
139 r = __multiContentTypeExpr.Append("MEDIA_TYPE=0 ");
140 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
142 case CONTENT_TYPE_AUDIO:
143 r = __multiContentTypeExpr.Append("(MEDIA_TYPE=2 or MEDIA_TYPE=3) ");
144 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");;
146 case CONTENT_TYPE_VIDEO:
147 r = __multiContentTypeExpr.Append("MEDIA_TYPE=1 ");
148 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
150 case CONTENT_TYPE_ALL:
151 //If content type is CONTENT_TYPE_ALL, then MEDIA_TYPE is empty
158 r = __multiContentTypeExpr.Insert('(', 0);
159 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
161 r = __multiContentTypeExpr.Insert(')', __multiContentTypeExpr.GetLength());
162 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
164 __isMultiContentType = true;
170 _ContentDirectoryImpl::CreateFolderFilter(bool isMultiContentType, const Tizen::Base::String& inputFolderPath) const
172 result r = E_SUCCESS;
173 std::unique_ptr<filter_h, FilterHandleDeleter> pFilterHandle(new (std::nothrow) filter_h);
174 int ret = MEDIA_CONTENT_ERROR_NONE;
175 String inputCondition = L"";
176 String folderPath(inputFolderPath);
178 ret = media_filter_create(pFilterHandle.get());
179 r = MapCoreErrorToNativeResult(ret);
180 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_create operation.");
182 if (isMultiContentType)
184 r = inputCondition.Append(__multiContentTypeExpr);
185 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
189 switch (__contentType)
191 // Image-0,video-1,sound-2,music-3,other-4
192 case CONTENT_TYPE_OTHER:
193 inputCondition = "MEDIA_TYPE=4 ";
195 case CONTENT_TYPE_IMAGE:
196 inputCondition = "MEDIA_TYPE=0 ";
198 case CONTENT_TYPE_AUDIO:
199 inputCondition = "(MEDIA_TYPE=2 or MEDIA_TYPE=3) ";
201 case CONTENT_TYPE_VIDEO:
202 inputCondition = "MEDIA_TYPE=1 ";
204 case CONTENT_TYPE_ALL:
205 //If content type is CONTENT_TYPE_ALL, then MEDIA_TYPE is empty
212 if (!folderPath.IsEmpty())
214 if (!inputCondition.IsEmpty()) //For CONTENT_TYPE_ALL inputCondition is empty
216 r = inputCondition.Append("AND FOLDER_PATH = ");
217 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
221 r = inputCondition.Append("FOLDER_PATH = ");
222 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
225 r = folderPath.Insert('"', 0);
226 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation.");
227 r = folderPath.Insert('"', folderPath.GetLength());
228 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation.");
230 r = inputCondition.Append(folderPath);
231 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation.");
234 if (!inputCondition.IsEmpty())
236 //CopyToCharArrayN: utility function, converts a osp string to char*
237 std::unique_ptr<char[]> pInputCond(_StringConverter::CopyToCharArrayN(inputCondition));
238 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "The memory is insufficient.");
240 SysLog(NID_CNT, "pInputCond is [%s].", pInputCond.get());
242 ret = media_filter_set_condition(*(pFilterHandle.get()), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
243 r = MapCoreErrorToNativeResult(ret);
244 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
247 __pFilterHandle.reset(pFilterHandle.release());
253 _ContentDirectoryImpl::GetContentDirectoryCount(void) const
255 int directoryCount = 0;
256 result r = E_SUCCESS;
257 int ret = MEDIA_CONTENT_ERROR_NONE;
259 r = CreateFolderFilter(__isMultiContentType, L"");
260 SysTryReturnResult(NID_CNT, !IsFailed(r), E_SYSTEM, "Propagating for CreateFolderFilter.");
262 ret = media_folder_get_folder_count_from_db(*(__pFilterHandle.get()), &directoryCount);
263 r = MapCoreErrorToNativeResult(ret);
264 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_folder_get_folder_count_from_db operation.");
266 SysLog(NID_CNT, "directoryCount is [%d].", directoryCount);
269 return directoryCount;
273 _ContentDirectoryImpl::GetContentDirectoryPathListN(Tizen::Base::SortOrder sortOrder) const
275 result r = E_SUCCESS;
276 int ret = MEDIA_CONTENT_ERROR_NONE;
277 std::unique_ptr<GList, GListDeleter> pItemList;
278 GList* pTempList = null;
279 std::unique_ptr<char> pFolderPath;
280 char* pTempFolderPath = null;
281 std::unique_ptr<media_folder_s, FolderHandleDeleter> pFolderHandle;
282 std::unique_ptr<Object> pValue;
284 __pFinalOutList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
285 SysTryReturn(NID_CNT, __pFinalOutList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
287 r = __pFinalOutList->Construct();
288 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to construct __pFinalOutList ArrayList.", GetErrorMessage(r));
290 r = CreateFolderFilter(__isMultiContentType, L"");
291 SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
293 if (sortOrder == SORT_ORDER_ASCENDING)
295 ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_ASC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_DEFAULT);
296 r = MapCoreErrorToNativeResult(ret);
297 SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
298 "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
300 else if (sortOrder == SORT_ORDER_DESCENDING)
302 ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_DESC, MEDIA_PATH, MEDIA_CONTENT_COLLATE_DEFAULT);
303 r = MapCoreErrorToNativeResult(ret);
304 SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
305 "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
308 pTempList = pItemList.get();
309 ret = media_folder_foreach_folder_from_db(*(__pFilterHandle.get()), MediaFoldersCb, &pTempList);
310 r = MapCoreErrorToNativeResult(ret);
311 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_folder_foreach_folder_from_db operation.", GetErrorMessage(r));
313 SysTryReturn(NID_CNT, pTempList != NULL, null, r, "[%s] pItemList for media_folder_foreach_folder_from_db is null.", GetErrorMessage(r));
315 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
317 pFolderHandle.reset(static_cast<media_folder_h>(g_list_nth_data(pTempList, idx)));
319 if (pFolderHandle.get() != NULL)
321 pTempFolderPath = pFolderPath.get();
322 ret = media_folder_get_path(pFolderHandle.get(), &pTempFolderPath);
323 r = MapCoreErrorToNativeResult(ret);
324 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_folder_get_path operation.", GetErrorMessage(r));
326 if (pTempFolderPath != NULL)
328 SysLog(NID_CNT, "pFolderPath is [%s].", pTempFolderPath);
330 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pTempFolderPath));
334 r = __pFinalOutList->Add(*(pValue.release()));
335 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform Add operation to __pFinalOutList.", GetErrorMessage(r));
341 return __pFinalOutList.release();
345 _ContentDirectoryImpl::GetContentDirectoryItemCount(const Tizen::Base::String& contentDirectoryPath) const
347 SysLog(NID_CNT, "contentDirectoryPath is [%ls]", contentDirectoryPath.GetPointer());
349 int directoryItemCount = 0;
350 result r = E_SUCCESS;
351 int ret = MEDIA_CONTENT_ERROR_NONE;
352 GList* pItemList = NULL;
353 std::unique_ptr<char> pFolderId;
354 char* pTempFolderId = null;
355 std::unique_ptr<media_folder_s, FolderHandleDeleter> pFolderHandle;
357 SysTryReturn(NID_CNT, CheckMediaPath(contentDirectoryPath), directoryItemCount, E_INVALID_ARG,
358 "[E_INVALID_ARG] Failed to perform CheckMediaPath operation.");
360 String detachedPath = contentDirectoryPath;
362 if (contentDirectoryPath.EndsWith(L"/"))
364 r = detachedPath.Remove(detachedPath.GetLength() - 1, 1);
365 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, E_INVALID_ARG, "[E_INVALID_ARG] Failed to remove the path string.");
368 r = CreateFolderFilter(__isMultiContentType, detachedPath);
369 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
371 ret = media_folder_foreach_folder_from_db(*(__pFilterHandle.get()), MediaFoldersCb, &pItemList);
372 r = MapCoreErrorToNativeResult(ret);
373 SysTryReturn(NID_CNT, r == E_SUCCESS, directoryItemCount, r,
374 "[%s] Failed to perform media_folder_foreach_folder_from_db operation.", GetErrorMessage(r));
376 SysTryReturn(NID_CNT, pItemList != NULL, directoryItemCount, r,
377 "[%s] pItemList for media_folder_foreach_folder_from_db is null.", GetErrorMessage(r));
379 for (int idx = 0; idx < (int)g_list_length(pItemList); idx++)
381 SysLog(NID_CNT, "idx is [%d] and (int)g_list_length(pItemList) is [%d].", idx, (int)g_list_length(pItemList));
383 pFolderHandle.reset(static_cast<media_folder_h>(g_list_nth_data(pItemList, idx)));
385 if (pFolderHandle.get() != NULL)
387 pTempFolderId = pFolderId.get();
388 ret = media_folder_get_folder_id(pFolderHandle.get(), &pTempFolderId);
389 r = MapCoreErrorToNativeResult(ret);
390 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, r,
391 "[%s] Failed to perform media_folder_get_folder_id operation.", GetErrorMessage(r));
396 SysTryReturn(NID_CNT, r != E_SUCCESS, directoryItemCount, r, "[E_SYSTEM] pFolderHandle is null.");
400 if (pTempFolderId != NULL)
402 r = CreateFolderFilter(__isMultiContentType, L"");
403 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
405 ret = media_folder_get_media_count_from_db(pTempFolderId, *(__pFilterHandle.get()), &directoryItemCount);
406 r = MapCoreErrorToNativeResult(ret);
407 SysTryReturn(NID_CNT, !IsFailed(r), directoryItemCount, r,
408 "[%s] Failed to perform media_folder_get_media_count_from_db operation.", GetErrorMessage(r));
411 SysLog(NID_CNT, "directoryItemCount is [%d].", directoryItemCount);
414 return directoryItemCount;
417 // Osp column names are mapped with slp column names
418 // CONTENT_TYPE_OTHER and CONTENT_TYPE_IMAGE (0 - 13 ) are valid columns
419 // CONTENT_TYPE_VIDEO (0 - 16 ) are valid columns
420 // CONTENT_TYPE_ALL and CONTENT_TYPE_VIDEO (0 - 18 ) are valid columns
421 // if the given osp column is out of the specified range of the type, E_INVALID_ARG is retuned.
423 _ContentDirectoryImpl::GetSlpColumnName(String& inputCol, String sortCol) const
425 String ospColumnName(L"");
426 String slpColumnName(L"");
427 String columnName(sortCol);
428 result r = E_SUCCESS;
429 int maxCols = MAX_QUERY_COLUMNS;
431 if(!__isMultiContentType)
433 switch (__contentType)
435 case CONTENT_TYPE_OTHER:
437 case CONTENT_TYPE_IMAGE:
438 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
440 case CONTENT_TYPE_VIDEO:
441 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
443 case CONTENT_TYPE_AUDIO:
445 case CONTENT_TYPE_ALL:
446 maxCols = MAX_QUERY_COLUMNS;
454 if(!__multiContentTypeExpr.IsEmpty())
456 if(__multiContentTypeExpr.Contains("MEDIA_TYPE=2"))
458 maxCols = MAX_QUERY_COLUMNS;
460 else if(__multiContentTypeExpr.Contains("MEDIA_TYPE=1"))
462 maxCols = MAX_QUERY_COLUMNS_FOR_VIDEO;
464 else if(__multiContentTypeExpr.Contains("MEDIA_TYPE=0"))
466 maxCols = MAX_QUERY_COLUMNS_FOR_IMAGE_OTHERS;
471 for (int colIndex=0; colIndex < maxCols; colIndex++)
473 ospColumnName.Clear();
474 slpColumnName.Clear();
476 ospColumnName = dbfieldinfo[colIndex].dbFieldOspName ;
477 slpColumnName = dbfieldinfo[colIndex].dbFieldSlpName ;
479 ospColumnName.ToUpper();
480 columnName.ToUpper();
482 if (columnName == ospColumnName)
484 inputCol = slpColumnName;
488 return E_INVALID_ARG;
492 _ContentDirectoryImpl::GetContentDirectoryItemListN(const Tizen::Base::String& contentDirectoryPath, int pageNo, int countPerPage,
493 const Tizen::Base::String& column, Tizen::Base::SortOrder sortOrder) const
495 SysLog(NID_CNT, "contentDirectoryPath is [%ls].", contentDirectoryPath.GetPointer());
497 result r = E_SUCCESS;
499 int totalPageCount = 0;
500 int ret = MEDIA_CONTENT_ERROR_NONE;
501 String slpColumn = L"";
502 std::unique_ptr<GList, GListDeleter> pItemList;
503 GList* pTempList = null;
504 std::unique_ptr<char> pFolderId;
505 char* pTempFolderId = null;
506 std::unique_ptr<media_folder_s, FolderHandleDeleter> pFolderHandle;
509 SysTryReturn(NID_CNT, CheckMediaPath(contentDirectoryPath), null, E_INVALID_ARG,
510 "[E_INVALID_ARG] Failed to perform CheckMediaPath operation.");
512 String detachedPath = contentDirectoryPath;
514 if (contentDirectoryPath.EndsWith(L"/"))
516 r = detachedPath.Remove(detachedPath.GetLength() - 1, 1);
517 SysTryReturn(NID_CNT, !IsFailed(r), null, E_INVALID_ARG, "[E_INVALID_ARG] Failed to remove the path string.");
520 __pFinalOutList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
521 SysTryReturn(NID_CNT, __pFinalOutList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
523 r = __pFinalOutList->Construct();
524 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to construct __pFinalOutList ArrayList.", GetErrorMessage(r));
526 r = CreateFolderFilter(__isMultiContentType, detachedPath);
527 SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
529 if ((!column.IsEmpty()) && (sortOrder != SORT_ORDER_NONE))
531 //__inputColumnName (osp column name) is replaced with slpColumn (slp column name).
532 r = GetSlpColumnName(slpColumn, column);
533 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform GetSlpColumnName operation.", GetErrorMessage(r));
535 //CopyToCharArrayN: utility function, converts a osp string to char*
536 std::unique_ptr<char[]> pSortCol(_StringConverter::CopyToCharArrayN(slpColumn));
537 SysTryReturn(NID_CNT, pSortCol, null, E_OUT_OF_MEMORY, "The memory is insufficient.");
539 if (sortOrder == SORT_ORDER_ASCENDING)
541 ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_ASC, pSortCol.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
542 r = MapCoreErrorToNativeResult(ret);
543 SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
544 "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
546 else if (sortOrder == SORT_ORDER_DESCENDING)
548 ret = media_filter_set_order(*(__pFilterHandle.get()), MEDIA_CONTENT_ORDER_DESC, pSortCol.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
549 r = MapCoreErrorToNativeResult(ret);
550 SysTryReturn(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, null, r,
551 "[%s] Failed to perform media_filter_set_order operation.", GetErrorMessage(r));
555 pTempList = pItemList.get();
556 ret = media_folder_foreach_folder_from_db(*(__pFilterHandle.get()), MediaFoldersCb, &pTempList);
557 r = MapCoreErrorToNativeResult(ret);
558 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
559 "[%s] Failed to perform media_folder_foreach_folder_from_db operation.", GetErrorMessage(r));
561 if (pTempList != NULL)
563 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
565 SysLog(NID_CNT, "idx is [%d] and (int)g_list_length(pItemList) is [%d].", idx, (int)g_list_length(pTempList));
567 pFolderHandle.reset(static_cast<media_folder_h>(g_list_nth_data(pTempList, idx)));
569 if (pFolderHandle.get() != NULL)
571 pTempFolderId = pFolderId.get();
572 ret = media_folder_get_folder_id(pFolderHandle.get(), &pTempFolderId);
573 r = MapCoreErrorToNativeResult(ret);
574 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
575 "[%s] Failed to perform media_folder_get_folder_id operation.", GetErrorMessage(r));
580 SysTryReturn(NID_CNT, r != E_SUCCESS, null, r, "[E_SYSTEM] pFolderHandle is null.");
585 if (pTempFolderId != NULL)
587 r = CreateFolderFilter(__isMultiContentType, L"");
588 SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform CreateFolderFilter operation.");
590 ret = media_folder_get_media_count_from_db(pTempFolderId, *(__pFilterHandle.get()), &totalCount);
591 r = MapCoreErrorToNativeResult(ret);
592 SysTryReturn(NID_CNT, !IsFailed(r), null, r,
593 "[%s] Failed to perform media_folder_get_media_count_from_db operation.", GetErrorMessage(r));
595 SysLog(NID_CNT, "totalCount is [%d].", totalCount);
600 if ((totalCount % countPerPage) == 0)
602 totalPageCount = totalCount / countPerPage;
606 totalPageCount = (totalCount / countPerPage) + 1;
609 SysTryReturn(NID_CNT, ((pageNo >= 1) && (pageNo <= totalPageCount)) , NULL, E_INVALID_ARG, "[E_INVALID_ARG] (pageNo < 1) || (pageNo > totalPageCount).");
611 offset = (pageNo * countPerPage) - countPerPage;
613 SysLog(NID_CNT, "totalCount [%d] totalPageCount[%d] __countPerPage[%d] __pageNo[%d] offset[%d]",
614 totalCount, totalPageCount, countPerPage, pageNo, offset);
616 ret = media_filter_set_offset(*(__pFilterHandle.get()),offset,countPerPage);
617 r = MapCoreErrorToNativeResult(ret);
618 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_filter_set_offset operation.", GetErrorMessage(r));
620 r = FillFinalOutList(pTempFolderId);
621 SysTryReturn(NID_CNT, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Failed to perform FillFinalOutList operation.");
626 SysTryReturn(NID_CNT, !(IsFailed(r)), null, r, "[E_INVALID_ARG] (pageNo > 1) and (totalcount = 0).");
630 return __pFinalOutList.release();
634 _ContentDirectoryImpl::FillFinalOutList(char* pFolderId) const
636 int ret = MEDIA_CONTENT_ERROR_NONE;
637 result r = E_SUCCESS;
638 std::unique_ptr<GList, GListDeleter> pItemList;
639 GList* pTempList = NULL;
640 std::unique_ptr<media_info_s, MediaHandleDeleter> pMediaHandle;
642 ContentInfo::_ContentData* pContentData = null;
643 ImageContentInfo::_ImageContentData* pImageContentData = null;
644 AudioContentInfo::_AudioContentData* pAudioContentData = null;
645 VideoContentInfo::_VideoContentData* pVideoContentData = null;
647 std::unique_ptr<ImageContentInfo> pImageContentInfo;
648 std::unique_ptr<AudioContentInfo> pAudioContentInfo;
649 std::unique_ptr<VideoContentInfo> pVideoContentInfo;
650 std::unique_ptr<OtherContentInfo> pOtherContentInfo;
652 pTempList = pItemList.get();
653 ret = media_folder_foreach_media_from_db(pFolderId, *(__pFilterHandle.get()), MediaFolderItemsCb, &pTempList);
654 r = MapCoreErrorToNativeResult(ret);
655 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform media_folder_foreach_media_from_db operation.");
657 SysTryReturnResult(NID_CNT, pTempList != NULL, r, "pItemList for media_info_foreach_media_from_db is null.");
659 media_content_type_e mediaType;
661 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
663 pMediaHandle.reset(static_cast<media_info_h>(g_list_nth_data(pTempList, idx)));
665 ret = media_info_get_media_type(pMediaHandle.get(), &mediaType);
666 r = MapCoreErrorToNativeResult(ret);
667 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_info_get_media_type operation.");
671 case MEDIA_CONTENT_TYPE_OTHERS:
672 pOtherContentInfo = std::unique_ptr<OtherContentInfo>(new (std::nothrow) OtherContentInfo);
673 SysTryReturnResult(NID_CNT, pOtherContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pOtherContentInfo.");
675 pContentData = pOtherContentInfo->GetContentData();
676 SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
678 r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
679 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
681 // Shallow copy, adds just the pointer: not the element
682 r = __pFinalOutList->Add(*(pOtherContentInfo.release()));
683 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to pFinalOutList.");
686 case MEDIA_CONTENT_TYPE_IMAGE:
687 pImageContentInfo = std::unique_ptr<ImageContentInfo>(new (std::nothrow) ImageContentInfo);
688 SysTryReturnResult(NID_CNT, pImageContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pImageContentInfo.");
690 pContentData = pImageContentInfo->GetContentData();
691 SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
693 pImageContentData = pImageContentInfo->GetImageContentData();
694 SysTryReturnResult(NID_CNT, pImageContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
696 r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
697 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
699 r = _ContentUtility::FillImageContentData(pMediaHandle.get(), pImageContentData);
700 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform GetDataFromImageTable operation.");
702 // Shallow copy, adds just the pointer: not the element
703 r = __pFinalOutList->Add(*(pImageContentInfo.release()));
704 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to __pFinalOutList.");
707 case MEDIA_CONTENT_TYPE_MUSIC:
709 case MEDIA_CONTENT_TYPE_SOUND:
710 pAudioContentInfo = std::unique_ptr<AudioContentInfo>(new (std::nothrow) AudioContentInfo);
711 SysTryReturnResult(NID_CNT, pAudioContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pAudioContentInfo.");
713 pContentData = pAudioContentInfo->GetContentData();
714 SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
716 pAudioContentData = pAudioContentInfo->GetAudioContentData();
717 SysTryReturnResult(NID_CNT, pAudioContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
719 r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
720 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
722 r = _ContentUtility::FillAudioContentData(pMediaHandle.get(), pAudioContentData);
723 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillAudioContentData operation.");
725 // Shallow copy, adds just the pointer: not the element
726 r = __pFinalOutList->Add(*(pAudioContentInfo.release()));
727 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to __pFinalOutList.");
730 case MEDIA_CONTENT_TYPE_VIDEO:
731 pVideoContentInfo = std::unique_ptr<VideoContentInfo>(new (std::nothrow) VideoContentInfo);
732 SysTryReturnResult(NID_CNT, pVideoContentInfo.get() != null, E_OUT_OF_MEMORY, "Failed to create pVideoContentInfo.");
734 pContentData = pVideoContentInfo->GetContentData();
735 SysTryReturnResult(NID_CNT, pContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
737 pVideoContentData = pVideoContentInfo->GetVideoContentData();
738 SysTryReturnResult(NID_CNT, pVideoContentData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
740 r = _ContentUtility::FillContentData(pMediaHandle.get(), pContentData);
741 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillContentData operation.");
743 r = _ContentUtility::FillVideoContentData(pMediaHandle.get(), pVideoContentData);
744 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform FillVideoContentData operation.");
746 // Shallow copy, adds just the pointer: not the element
747 r = __pFinalOutList->Add(*(pVideoContentInfo.release()));
748 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform Add operation to __pFinalOutList.");
760 _ContentDirectoryImpl::CheckMediaPath(const Tizen::Base::String& directoryPath) const
762 String phoneStr = Environment::GetMediaPath();
763 String mmcStr = Environment::GetExternalStoragePath();
765 if (!(directoryPath.StartsWith(phoneStr, 0) || directoryPath.StartsWith(mmcStr , 0)))
768 result r = phoneStr.SubString(0, (phoneStr.GetLength() - 1), checkPhone);
769 SysTryReturn(NID_CNT, !IsFailed(r), false, E_INVALID_ARG, "[E_INVALID_ARG] Failed to substring operation.");
772 r = mmcStr.SubString(0, (mmcStr.GetLength() - 1), checkMmc);
773 SysTryReturn(NID_CNT, !IsFailed(r), false, E_INVALID_ARG, "[E_INVALID_ARG] Failed to substring operation.");
775 SysTryReturn(NID_CNT, (directoryPath.Equals(checkPhone) || directoryPath.Equals(checkMmc)), false, E_INVALID_ARG,
776 "[E_INVALID_ARG] The contentDirectoryPath is not valid[%ls].", directoryPath.GetPointer());
783 _ContentDirectoryImpl::MapCoreErrorToNativeResult(int reason) const
785 result r = E_SUCCESS;
789 case MEDIA_CONTENT_ERROR_NONE:
793 case MEDIA_CONTENT_ERROR_DB_BUSY:
795 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
798 case MEDIA_CONTENT_ERROR_DB_FAILED:
800 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
803 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
805 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
809 SysLog(NID_CNT, "default");
816 // Callback function registered to each media info details
817 // all items are appended to the list
819 MediaFoldersCb(media_folder_h folder, void* pUserdata)
821 int ret = MEDIA_CONTENT_ERROR_NONE;
822 media_folder_h new_folder = NULL;
823 ret = media_folder_clone(&new_folder, folder);
824 SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Propagating for media_folder_clone.");
826 GList** pList = (GList**)pUserdata;
827 *pList = g_list_append(*pList, new_folder);
832 // Callback function registered to each media info details
833 // all items are appended to the list
835 MediaFolderItemsCb(media_info_h media, void* pUserdata)
837 int ret = MEDIA_CONTENT_ERROR_NONE;
838 media_info_h new_media = NULL;
839 ret = media_info_clone(&new_media, media);
840 SysTryLog(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, "[E_SYSTEM] Propagating for media_info_clone.");
842 GList** pList = (GList**)pUserdata;
843 *pList = g_list_append(*pList, new_media);