2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FCnt_PlayListStatisticsImpl.cpp
18 * @brief This is the implementation file for the %_PlayListStatisticsImpl class.
20 * This file contains implementation of the %_PlayListStatisticsImpl class.
23 #include <FBaseSysLog.h>
24 #include <FBaseInteger.h>
25 #include <FBaseLongLong.h>
26 #include <FBaseFloat.h>
27 #include <FCntPlayList.h>
28 #include <FCntPlayListManager.h>
29 #include <FBase_StringConverter.h>
30 #include "FCnt_PlayListImpl.h"
31 #include "FCnt_PlayListManagerImpl.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Io;
37 namespace Tizen { namespace Content
40 // Declaration for Callback function registered to playlist details
41 bool MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData);
43 _PlayListManagerImpl::_PlayListManagerImpl(void)
44 : __pFilterHandle(NULL)
50 //(disconnects the DB connection)
51 _PlayListManagerImpl::~_PlayListManagerImpl(void)
53 int ret = MEDIA_CONTENT_ERROR_NONE;
56 ret = media_content_disconnect();
57 r = MapCoreErrorToNativeResult(ret);
58 SysTryLog(r == E_SUCCESS, "[%s] Propagating for media_content_disconnect.", GetErrorMessage(r));
60 SysLog(NID_CNT, "media_content_disconnect result[%d].", ret);
64 _PlayListManagerImpl::GetInstance(PlayListManager& playListManager)
66 return (&playListManager != null) ? playListManager.__pImpl : null;
69 const _PlayListManagerImpl*
70 _PlayListManagerImpl::GetInstance(const PlayListManager& playListManager)
72 return (&playListManager != null) ? playListManager.__pImpl : null;
76 _PlayListManagerImpl::Construct(void)
79 int ret = MEDIA_CONTENT_ERROR_NONE;
81 ret = media_content_connect();
82 r = MapCoreErrorToNativeResult(ret);
83 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Propagating for media_content_disconnect.");
85 SysLog(NID_CNT, "media_content_connect result[%d].", ret);
91 _PlayListManagerImpl::CreateFilter(const Tizen::Base::String& playListName) const
93 std::unique_ptr<char[]> pInputCond;
94 filter_h tempFilter = NULL;
95 String inputCondition = L"PLAYLIST_NAME = ";
96 String nameExpr(playListName);
98 int ret = media_filter_create(&tempFilter);
99 result r = MapCoreErrorToNativeResult(ret);
100 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_create operation.");
102 std::unique_ptr<filter_s, FilterDeleter> pFilterHandle(tempFilter);
103 SysTryReturnResult(NID_CNT, pFilterHandle != null, E_OUT_OF_MEMORY, "pFilterHandle is null.");
105 if (!nameExpr.IsEmpty())
107 r = nameExpr.Replace("\'", "''");
108 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Replace operation for nameExpr.");
110 r = nameExpr.Insert('\'', 0);
111 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
113 r = nameExpr.Insert('\'', nameExpr.GetLength());
114 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
116 r = inputCondition.Append(nameExpr);
117 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
120 if (!inputCondition.IsEmpty())
122 //CopyToCharArrayN: utility function, converts a osp string to char*
123 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
124 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "The memory is insufficient.");
126 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
128 ret = media_filter_set_condition(pFilterHandle.get(), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
129 r = MapCoreErrorToNativeResult(ret);
130 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
133 __pFilterHandle.reset(pFilterHandle.release());
139 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
141 std::unique_ptr<GList, GListDeleter> pItemList;
142 GList* pTempList = null;
143 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
145 std::unique_ptr<PlayList> pFinalOutplayList;
146 int ret = MEDIA_CONTENT_ERROR_NONE;
148 result r = CreateFilter(playListName);
149 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
151 pTempList = pItemList.get();
152 ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
153 r = MapCoreErrorToNativeResult(ret);
154 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
156 if (pTempList == NULL)
158 r = E_INVALID_ARG; // No match found.
162 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
163 if (playListHandle.get() != NULL)
165 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
166 r = MapCoreErrorToNativeResult(ret);
167 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
169 pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
170 SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
172 r = pFinalOutplayList->ConstructPlayList(playListName);
173 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
178 return pFinalOutplayList.release();
182 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
184 std::unique_ptr<GList, GListDeleter> pItemList;
185 GList* pTempList = null;
186 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
188 int ret = MEDIA_CONTENT_ERROR_NONE;
190 result r = CreateFilter(playListName);
191 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
193 pTempList = pItemList.get();
194 ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
195 r = MapCoreErrorToNativeResult(ret);
196 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
198 if (pTempList == NULL)
200 r = E_INVALID_ARG; // No match found.
204 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
206 if (playListHandle.get() != NULL)
208 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
209 r = MapCoreErrorToNativeResult(ret);
210 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
212 ret = media_playlist_delete_from_db(playlistId);
213 r = MapCoreErrorToNativeResult(ret);
214 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
222 _PlayListManagerImpl::GetAllPlayListCount(void) const
224 int playlistCount = 0;
226 int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
227 result r = MapCoreDatabaseErrorToNativeResult(ret);
229 SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
232 return playlistCount;
235 Tizen::Base::Collection::IList*
236 _PlayListManagerImpl::GetAllPlayListNameN(void) const
238 std::unique_ptr<GList, GListDeleter> pItemList;
239 GList* pTempList = null;
240 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
241 char* pTempListName = null;
242 std::unique_ptr<Object> pValue;
243 std::unique_ptr<ArrayList, AllElementsDeleter> pNamesList;
245 pTempList = pItemList.get();
246 int ret = media_playlist_foreach_playlist_from_db(NULL, MediaPlayListCb, &pTempList);
247 result r = MapCoreDatabaseErrorToNativeResult(ret);
248 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
250 pNamesList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
251 SysTryReturn(NID_CNT, pNamesList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
253 r = pNamesList->Construct();
254 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
258 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
260 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, idx)));
262 if (playListHandle.get() != NULL)
264 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
265 r = MapCoreDatabaseErrorToNativeResult(ret);
266 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
268 if (pTempListName != null)
270 SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
272 std::unique_ptr<char[]> pPlayListName(pTempListName);
273 SysTryReturn(NID_CNT, pPlayListName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPlayListName is null.");
275 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pPlayListName.get()));
276 SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
278 if (pValue.get() != NULL)
280 r = pNamesList->Add(*(pValue.release()));
281 SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
288 return pNamesList.release();
292 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
294 result r = E_SUCCESS;
298 case MEDIA_CONTENT_ERROR_NONE:
302 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
304 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
307 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
309 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
312 case MEDIA_CONTENT_ERROR_DB_FAILED:
314 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
317 case MEDIA_CONTENT_ERROR_DB_BUSY:
319 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
323 SysLog(NID_CNT, "default");
331 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
333 result r = E_SUCCESS;
337 case MEDIA_CONTENT_ERROR_NONE:
341 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
343 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
346 case MEDIA_CONTENT_ERROR_DB_FAILED:
348 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
351 case MEDIA_CONTENT_ERROR_DB_BUSY:
353 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
357 SysLog(NID_CNT, "default");
365 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
367 media_playlist_h newPlayListHandle = NULL;
368 media_playlist_clone(&newPlayListHandle, playlistHandle);
370 GList** pList = (GList**)pUserData;
371 *pList = g_list_append(*pList, newPlayListHandle);