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_PlayListStatisticsImpl.cpp
19 * @brief This is the implementation file for the %_PlayListStatisticsImpl class.
21 * This file contains implementation of the %_PlayListStatisticsImpl class.
24 #include <FBaseSysLog.h>
25 #include <FBaseInteger.h>
26 #include <FBaseLongLong.h>
27 #include <FBaseFloat.h>
28 #include <FCntPlayList.h>
29 #include <FCntPlayListManager.h>
30 #include <FBase_StringConverter.h>
31 #include <FCnt_PlayListImpl.h>
32 #include <FCnt_PlayListManagerImpl.h>
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Io;
38 namespace Tizen { namespace Content
41 // Declaration for Callback function registered to playlist details
42 bool MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData);
44 _PlayListManagerImpl::_PlayListManagerImpl(void)
46 , __pFilterHandle(NULL)
52 //(disconnects the DB connection)
53 _PlayListManagerImpl::~_PlayListManagerImpl(void)
55 int ret = MEDIA_CONTENT_ERROR_NONE;
58 ret = media_content_disconnect();
59 r = MapCoreErrorToNativeResult(ret);
60 SysTryLog(r == E_SUCCESS, "[%s] Propagating for media_content_disconnect.", GetErrorMessage(r));
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.");
89 _PlayListManagerImpl::CreateFilter(const Tizen::Base::String& playListName) const
91 std::unique_ptr<char[]> pInputCond;
92 std::unique_ptr<filter_h, FilterDeleter> pFilterHandle(new (std::nothrow) filter_h);
93 String inputCondition = L"PLAYLIST_NAME = ";
94 String nameExpr(playListName);
96 int ret = media_filter_create(pFilterHandle.get());
97 result r = MapCoreErrorToNativeResult(ret);
98 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_create operation.");
100 if (!nameExpr.IsEmpty())
102 r = nameExpr.Insert('"', 0);
103 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
104 r = nameExpr.Insert('"', nameExpr.GetLength());
105 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
107 r = inputCondition.Append(nameExpr);
108 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
111 if (!inputCondition.IsEmpty())
113 //CopyToCharArrayN: utility function, converts a osp string to char*
114 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
115 SysTryReturnResult(NID_CNT, (pInputCond.get())[0] != null, E_SYSTEM, "pInputCond is NULL.");
117 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
119 if ((pInputCond.get())[0] != null)
121 ret = media_filter_set_condition(*(pFilterHandle.get()), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
122 r = MapCoreErrorToNativeResult(ret);
123 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
127 __pFilterHandle.reset(pFilterHandle.release());
133 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
135 std::unique_ptr<GList, GListDeleter> pItemList;
136 GList* pTempList = null;
137 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
139 std::unique_ptr<PlayList> pFinalOutplayList;
140 int ret = MEDIA_CONTENT_ERROR_NONE;
142 result r = CreateFilter(playListName);
143 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
145 pTempList = pItemList.get();
146 ret = media_playlist_foreach_playlist_from_db(*(__pFilterHandle.get()), MediaPlayListCb, &pTempList);
147 r = MapCoreErrorToNativeResult(ret);
148 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
150 if (pTempList == NULL)
152 r = E_INVALID_ARG; // No match found.
156 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
157 if (playListHandle.get() != NULL)
159 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
160 r = MapCoreErrorToNativeResult(ret);
161 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
163 pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
164 SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
166 r = pFinalOutplayList->ConstructPlayList(playListName);
167 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
172 return pFinalOutplayList.release();
176 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
178 std::unique_ptr<GList, GListDeleter> pItemList;
179 GList* pTempList = null;
180 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
182 int ret = MEDIA_CONTENT_ERROR_NONE;
184 result r = CreateFilter(playListName);
185 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
187 pTempList = pItemList.get();
188 ret = media_playlist_foreach_playlist_from_db(*(__pFilterHandle.get()), MediaPlayListCb, &pTempList);
189 r = MapCoreErrorToNativeResult(ret);
190 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
192 if (pTempList == NULL)
194 r = E_INVALID_ARG; // No match found.
198 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
200 if (playListHandle.get() != NULL)
202 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
203 r = MapCoreErrorToNativeResult(ret);
204 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
206 ret = media_playlist_delete_from_db(playlistId);
207 r = MapCoreErrorToNativeResult(ret);
208 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
216 _PlayListManagerImpl::GetAllPlayListCount(void) const
218 int playlistCount = 0;
220 int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
221 result r = MapCoreDatabaseErrorToNativeResult(ret);
223 SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
226 return playlistCount;
229 Tizen::Base::Collection::IList*
230 _PlayListManagerImpl::GetAllPlayListNameN(void) const
232 std::unique_ptr<GList, GListDeleter> pItemList;
233 GList* pTempList = null;
234 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
235 std::unique_ptr<char> pPlayListName;
236 char* pTempListName = null;
237 std::unique_ptr<Object> pValue;
238 std::unique_ptr<ArrayList, AllElementsDeleter> pNamesList;
240 pTempList = pItemList.get();
241 int ret = media_playlist_foreach_playlist_from_db(NULL, MediaPlayListCb, &pTempList);
242 result r = MapCoreDatabaseErrorToNativeResult(ret);
243 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
245 pNamesList = std::unique_ptr<ArrayList, AllElementsDeleter>(new (std::nothrow) ArrayList());
246 SysTryReturn(NID_CNT, pNamesList.get() != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
248 r = pNamesList->Construct();
249 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
253 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
255 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, idx)));
257 if (playListHandle.get() != NULL)
259 pTempListName = pPlayListName.get();
260 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
261 r = MapCoreDatabaseErrorToNativeResult(ret);
262 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
264 if (pTempListName != null)
266 SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
268 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pTempListName));
269 SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
271 if (pValue.get() != NULL)
273 r = pNamesList->Add(*(pValue.release()));
274 SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
281 return pNamesList.release();
285 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
287 result r = E_SUCCESS;
291 case MEDIA_CONTENT_ERROR_NONE:
295 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
297 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
300 case MEDIA_CONTENT_ERROR_DB_FAILED:
302 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
305 case MEDIA_CONTENT_ERROR_DB_BUSY:
307 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
311 SysLog(NID_CNT, "default");
319 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
321 result r = E_SUCCESS;
325 case MEDIA_CONTENT_ERROR_NONE:
329 case MEDIA_CONTENT_ERROR_DB_FAILED:
331 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
334 case MEDIA_CONTENT_ERROR_DB_BUSY:
336 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
340 SysLog(NID_CNT, "default");
348 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
350 media_playlist_h newPlayListHandle = NULL;
351 media_playlist_clone(&newPlayListHandle, playlistHandle);
353 GList** pList = (GList**)pUserData;
354 *pList = g_list_append(*pList, newPlayListHandle);