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, E_OUT_OF_MEMORY, "The memory is insufficient.");
125 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
127 ret = media_filter_set_condition(*(pFilterHandle.get()), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
128 r = MapCoreErrorToNativeResult(ret);
129 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
132 __pFilterHandle.reset(pFilterHandle.release());
138 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
140 std::unique_ptr<GList, GListDeleter> pItemList;
141 GList* pTempList = null;
142 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
144 std::unique_ptr<PlayList> pFinalOutplayList;
145 int ret = MEDIA_CONTENT_ERROR_NONE;
147 result r = CreateFilter(playListName);
148 SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
150 pTempList = pItemList.get();
151 ret = media_playlist_foreach_playlist_from_db(*(__pFilterHandle.get()), MediaPlayListCb, &pTempList);
152 r = MapCoreErrorToNativeResult(ret);
153 SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
155 if (pTempList == NULL)
157 r = E_INVALID_ARG; // No match found.
161 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
162 if (playListHandle.get() != NULL)
164 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
165 r = MapCoreErrorToNativeResult(ret);
166 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
168 pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
169 SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList is null.");
171 r = pFinalOutplayList->ConstructPlayList(playListName);
172 SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
177 return pFinalOutplayList.release();
181 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
183 std::unique_ptr<GList, GListDeleter> pItemList;
184 GList* pTempList = null;
185 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
187 int ret = MEDIA_CONTENT_ERROR_NONE;
189 result r = CreateFilter(playListName);
190 SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
192 pTempList = pItemList.get();
193 ret = media_playlist_foreach_playlist_from_db(*(__pFilterHandle.get()), MediaPlayListCb, &pTempList);
194 r = MapCoreErrorToNativeResult(ret);
195 SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
197 if (pTempList == NULL)
199 r = E_INVALID_ARG; // No match found.
203 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
205 if (playListHandle.get() != NULL)
207 ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
208 r = MapCoreErrorToNativeResult(ret);
209 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
211 ret = media_playlist_delete_from_db(playlistId);
212 r = MapCoreErrorToNativeResult(ret);
213 SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
221 _PlayListManagerImpl::GetAllPlayListCount(void) const
223 int playlistCount = 0;
225 int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
226 result r = MapCoreDatabaseErrorToNativeResult(ret);
228 SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
231 return playlistCount;
234 Tizen::Base::Collection::IList*
235 _PlayListManagerImpl::GetAllPlayListNameN(void) const
237 std::unique_ptr<GList, GListDeleter> pItemList;
238 GList* pTempList = null;
239 std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
240 std::unique_ptr<char> pPlayListName;
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 pTempListName = pPlayListName.get();
265 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
266 r = MapCoreDatabaseErrorToNativeResult(ret);
267 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
269 if (pTempListName != null)
271 SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
273 pValue = std::unique_ptr<Object>(new (std::nothrow) String(pTempListName));
274 SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
276 if (pValue.get() != NULL)
278 r = pNamesList->Add(*(pValue.release()));
279 SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
286 return pNamesList.release();
290 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
292 result r = E_SUCCESS;
296 case MEDIA_CONTENT_ERROR_NONE:
300 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
302 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
305 case MEDIA_CONTENT_ERROR_DB_FAILED:
307 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
310 case MEDIA_CONTENT_ERROR_DB_BUSY:
312 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
316 SysLog(NID_CNT, "default");
324 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
326 result r = E_SUCCESS;
330 case MEDIA_CONTENT_ERROR_NONE:
334 case MEDIA_CONTENT_ERROR_DB_FAILED:
336 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
339 case MEDIA_CONTENT_ERROR_DB_BUSY:
341 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
345 SysLog(NID_CNT, "default");
353 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
355 media_playlist_h newPlayListHandle = NULL;
356 media_playlist_clone(&newPlayListHandle, playlistHandle);
358 GList** pList = (GList**)pUserData;
359 *pList = g_list_append(*pList, newPlayListHandle);