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 = ";
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 r = nameExpr.Append(playListName);
106 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for nameExpr.");
108 if (!nameExpr.IsEmpty())
110 r = nameExpr.Replace(L"\'", L"''");
111 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Replace operation for nameExpr.");
113 r = nameExpr.Insert('\'', 0);
114 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
116 r = nameExpr.Insert('\'', nameExpr.GetLength());
117 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
119 r = inputCondition.Append(nameExpr);
120 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
123 if (!inputCondition.IsEmpty())
125 //CopyToCharArrayN: utility function, converts a osp string to char*
126 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
127 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "The memory is insufficient.");
129 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
131 ret = media_filter_set_condition(pFilterHandle.get(), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
132 r = MapCoreErrorToNativeResult(ret);
133 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
136 __pFilterHandle.reset(pFilterHandle.release());
142 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
144 std::unique_ptr<GList, GListDeleter> pItemList;
145 GList* pTempList = null;
146 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
148 std::unique_ptr<PlayList> pFinalOutplayList;
149 int ret = MEDIA_CONTENT_ERROR_NONE;
151 result r = CreateFilter(playListName);
152 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
154 pTempList = pItemList.get();
155 ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
156 r = MapCoreErrorToNativeResult(ret);
157 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
159 if (pTempList == NULL)
161 r = E_INVALID_ARG; // No match found.
165 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
166 if (playListHandle.get() != NULL)
168 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
169 r = MapCoreErrorToNativeResult(ret);
170 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
172 pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
173 SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
175 r = pFinalOutplayList->ConstructPlayList(playListName);
176 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
181 return pFinalOutplayList.release();
185 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
187 std::unique_ptr<GList, GListDeleter> pItemList;
188 GList* pTempList = null;
189 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
191 int ret = MEDIA_CONTENT_ERROR_NONE;
193 result r = CreateFilter(playListName);
194 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
196 pTempList = pItemList.get();
197 ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
198 r = MapCoreErrorToNativeResult(ret);
199 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
201 if (pTempList == NULL)
203 r = E_INVALID_ARG; // No match found.
207 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
209 if (playListHandle.get() != NULL)
211 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
212 r = MapCoreErrorToNativeResult(ret);
213 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
215 ret = media_playlist_delete_from_db(playlistId);
216 r = MapCoreErrorToNativeResult(ret);
217 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
225 _PlayListManagerImpl::GetAllPlayListCount(void) const
227 int playlistCount = 0;
229 int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
230 result r = MapCoreDatabaseErrorToNativeResult(ret);
232 SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
235 return playlistCount;
238 Tizen::Base::Collection::IList*
239 _PlayListManagerImpl::GetAllPlayListNameN(void) const
241 std::unique_ptr<GList, GListDeleter> pItemList;
242 GList* pTempList = null;
243 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
244 char* pTempListName = null;
245 std::unique_ptr<Object> pValue;
246 std::unique_ptr<ArrayList, AllElementsDeleter> pNamesList;
248 pTempList = pItemList.get();
249 int ret = media_playlist_foreach_playlist_from_db(NULL, MediaPlayListCb, &pTempList);
250 result r = MapCoreDatabaseErrorToNativeResult(ret);
251 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
253 pNamesList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
254 SysTryReturn(NID_CNT, pNamesList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
256 r = pNamesList->Construct();
257 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
261 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
263 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, idx)));
265 if (playListHandle.get() != NULL)
267 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
268 r = MapCoreDatabaseErrorToNativeResult(ret);
269 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
271 if (pTempListName != null)
273 SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
275 std::unique_ptr<char[]> pPlayListName(pTempListName);
276 SysTryReturn(NID_CNT, pPlayListName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPlayListName is null.");
278 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pPlayListName.get()));
279 SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
281 if (pValue.get() != NULL)
283 r = pNamesList->Add(*(pValue.release()));
284 SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
291 return pNamesList.release();
295 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
297 result r = E_SUCCESS;
301 case MEDIA_CONTENT_ERROR_NONE:
305 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
307 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
310 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
312 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
315 case MEDIA_CONTENT_ERROR_DB_FAILED:
317 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
320 case MEDIA_CONTENT_ERROR_DB_BUSY:
322 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
326 SysLog(NID_CNT, "default");
334 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
336 result r = E_SUCCESS;
340 case MEDIA_CONTENT_ERROR_NONE:
344 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
346 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
349 case MEDIA_CONTENT_ERROR_DB_FAILED:
351 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
354 case MEDIA_CONTENT_ERROR_DB_BUSY:
356 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
360 SysLog(NID_CNT, "default");
368 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
370 media_playlist_h newPlayListHandle = NULL;
371 media_playlist_clone(&newPlayListHandle, playlistHandle);
373 GList** pList = (GList**)pUserData;
374 *pList = g_list_append(*pList, newPlayListHandle);