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));
62 SysLog(NID_CNT, "media_content_disconnect result[%d].", ret);
66 _PlayListManagerImpl::GetInstance(PlayListManager& playListManager)
68 return (&playListManager != null) ? playListManager.__pImpl : null;
71 const _PlayListManagerImpl*
72 _PlayListManagerImpl::GetInstance(const PlayListManager& playListManager)
74 return (&playListManager != null) ? playListManager.__pImpl : null;
78 _PlayListManagerImpl::Construct(void)
81 int ret = MEDIA_CONTENT_ERROR_NONE;
83 ret = media_content_connect();
84 r = MapCoreErrorToNativeResult(ret);
85 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Propagating for media_content_disconnect.");
87 SysLog(NID_CNT, "media_content_connect result[%d].", ret);
93 _PlayListManagerImpl::CreateFilter(const Tizen::Base::String& playListName) const
95 std::unique_ptr<char[]> pInputCond;
96 std::unique_ptr<filter_h, FilterDeleter> pFilterHandle(new (std::nothrow) filter_h);
97 String inputCondition = L"PLAYLIST_NAME = ";
98 String nameExpr(playListName);
100 int ret = media_filter_create(pFilterHandle.get());
101 result r = MapCoreErrorToNativeResult(ret);
102 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_create operation.");
104 if (!nameExpr.IsEmpty())
106 r = nameExpr.Replace("\'", "''");
107 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Replace operation for nameExpr.");
109 r = nameExpr.Insert('\'', 0);
110 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
112 r = nameExpr.Insert('\'', nameExpr.GetLength());
113 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
115 r = inputCondition.Append(nameExpr);
116 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
119 if (!inputCondition.IsEmpty())
121 //CopyToCharArrayN: utility function, converts a osp string to char*
122 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
123 SysTryReturnResult(NID_CNT, (pInputCond.get())[0] != null, E_SYSTEM, "pInputCond is NULL.");
125 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
127 if ((pInputCond.get())[0] != null)
129 ret = media_filter_set_condition(*(pFilterHandle.get()), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
130 r = MapCoreErrorToNativeResult(ret);
131 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
135 __pFilterHandle.reset(pFilterHandle.release());
141 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
143 std::unique_ptr<GList, GListDeleter> pItemList;
144 GList* pTempList = null;
145 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
147 std::unique_ptr<PlayList> pFinalOutplayList;
148 int ret = MEDIA_CONTENT_ERROR_NONE;
150 result r = CreateFilter(playListName);
151 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
153 pTempList = pItemList.get();
154 ret = media_playlist_foreach_playlist_from_db(*(__pFilterHandle.get()), MediaPlayListCb, &pTempList);
155 r = MapCoreErrorToNativeResult(ret);
156 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
158 if (pTempList == NULL)
160 r = E_INVALID_ARG; // No match found.
164 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
165 if (playListHandle.get() != NULL)
167 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
168 r = MapCoreErrorToNativeResult(ret);
169 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
171 pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
172 SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
174 r = pFinalOutplayList->ConstructPlayList(playListName);
175 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
180 return pFinalOutplayList.release();
184 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
186 std::unique_ptr<GList, GListDeleter> pItemList;
187 GList* pTempList = null;
188 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
190 int ret = MEDIA_CONTENT_ERROR_NONE;
192 result r = CreateFilter(playListName);
193 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
195 pTempList = pItemList.get();
196 ret = media_playlist_foreach_playlist_from_db(*(__pFilterHandle.get()), MediaPlayListCb, &pTempList);
197 r = MapCoreErrorToNativeResult(ret);
198 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
200 if (pTempList == NULL)
202 r = E_INVALID_ARG; // No match found.
206 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
208 if (playListHandle.get() != NULL)
210 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
211 r = MapCoreErrorToNativeResult(ret);
212 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
214 ret = media_playlist_delete_from_db(playlistId);
215 r = MapCoreErrorToNativeResult(ret);
216 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
224 _PlayListManagerImpl::GetAllPlayListCount(void) const
226 int playlistCount = 0;
228 int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
229 result r = MapCoreDatabaseErrorToNativeResult(ret);
231 SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
234 return playlistCount;
237 Tizen::Base::Collection::IList*
238 _PlayListManagerImpl::GetAllPlayListNameN(void) const
240 std::unique_ptr<GList, GListDeleter> pItemList;
241 GList* pTempList = null;
242 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
243 std::unique_ptr<char> pPlayListName;
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 pTempListName = pPlayListName.get();
268 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
269 r = MapCoreDatabaseErrorToNativeResult(ret);
270 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
272 if (pTempListName != null)
274 SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
276 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pTempListName));
277 SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
279 if (pValue.get() != NULL)
281 r = pNamesList->Add(*(pValue.release()));
282 SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
289 return pNamesList.release();
293 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
295 result r = E_SUCCESS;
299 case MEDIA_CONTENT_ERROR_NONE:
303 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
305 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
308 case MEDIA_CONTENT_ERROR_DB_FAILED:
310 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
313 case MEDIA_CONTENT_ERROR_DB_BUSY:
315 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
319 SysLog(NID_CNT, "default");
327 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
329 result r = E_SUCCESS;
333 case MEDIA_CONTENT_ERROR_NONE:
337 case MEDIA_CONTENT_ERROR_DB_FAILED:
339 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
342 case MEDIA_CONTENT_ERROR_DB_BUSY:
344 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
348 SysLog(NID_CNT, "default");
356 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
358 media_playlist_h newPlayListHandle = NULL;
359 media_playlist_clone(&newPlayListHandle, playlistHandle);
361 GList** pList = (GList**)pUserData;
362 *pList = g_list_append(*pList, newPlayListHandle);