Tizen 2.1 base
[platform/framework/native/content.git] / src / FCnt_PlayListManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file                FCnt_PlayListStatisticsImpl.cpp
19  * @brief               This is the implementation file for the %_PlayListStatisticsImpl class.
20  *
21  * This file contains implementation of the %_PlayListStatisticsImpl class.
22  */
23
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>
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Io;
37
38 namespace Tizen { namespace Content
39 {
40
41 // Declaration for Callback function registered to playlist details
42 bool MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData);
43
44 _PlayListManagerImpl::_PlayListManagerImpl(void)
45         : Object()
46         , __pFilterHandle(NULL)
47 {
48
49 }
50
51
52 //(disconnects the DB connection)
53 _PlayListManagerImpl::~_PlayListManagerImpl(void)
54 {
55         int ret = MEDIA_CONTENT_ERROR_NONE;
56         result r = E_SUCCESS;
57
58         ret = media_content_disconnect();
59         r = MapCoreErrorToNativeResult(ret);
60         SysTryLog(r == E_SUCCESS, "[%s] Propagating for media_content_disconnect.", GetErrorMessage(r));
61 }
62
63  _PlayListManagerImpl*
64  _PlayListManagerImpl::GetInstance(PlayListManager& playListManager)
65 {
66         return (&playListManager != null) ? playListManager.__pImpl : null;
67 }
68
69 const _PlayListManagerImpl*
70 _PlayListManagerImpl::GetInstance(const PlayListManager& playListManager)
71 {
72         return (&playListManager != null) ? playListManager.__pImpl : null;
73 }
74
75 result
76 _PlayListManagerImpl::Construct(void)
77 {
78         result r = E_SUCCESS;
79         int ret = MEDIA_CONTENT_ERROR_NONE;
80
81         ret = media_content_connect();
82         r = MapCoreErrorToNativeResult(ret);
83         SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Propagating for media_content_disconnect.");
84
85         return r;
86 }
87
88 result
89 _PlayListManagerImpl::CreateFilter(const Tizen::Base::String& playListName) const
90 {
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);
95
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.");
99
100         if (!nameExpr.IsEmpty())
101         {
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.");
106
107                 r = inputCondition.Append(nameExpr);
108                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
109         }
110
111         if (!inputCondition.IsEmpty())
112         {
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.");
116
117                 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
118
119                 if ((pInputCond.get())[0] != null)
120                 {
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.");
124                 }
125         }
126
127         __pFilterHandle.reset(pFilterHandle.release());
128
129         return  r;
130 }
131
132 PlayList*
133 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
134 {
135         std::unique_ptr<GList, GListDeleter> pItemList;
136         GList* pTempList = null;
137         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
138         int playlistId = 0;
139         std::unique_ptr<PlayList> pFinalOutplayList;
140         int ret = MEDIA_CONTENT_ERROR_NONE;
141
142         result r = CreateFilter(playListName);
143         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
144
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));
149
150         if (pTempList == NULL)
151         {
152                 r = E_INVALID_ARG; // No match found.
153         }
154         else
155         {
156                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
157                 if (playListHandle.get() != NULL)
158                 {
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));
162
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.");
165
166                         r = pFinalOutplayList->ConstructPlayList(playListName);
167                         SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
168                 }
169         }
170
171         SetLastResult(r);
172         return pFinalOutplayList.release();
173 }
174
175 result
176 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
177 {
178         std::unique_ptr<GList, GListDeleter> pItemList;
179         GList* pTempList = null;
180         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
181         int playlistId = 0;
182         int ret = MEDIA_CONTENT_ERROR_NONE;
183
184         result r = CreateFilter(playListName);
185         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
186
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.");
191
192         if (pTempList == NULL)
193         {
194                 r = E_INVALID_ARG; // No match found.
195         }
196         else
197         {
198                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
199
200                 if (playListHandle.get() != NULL)
201                 {
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.");
205
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.");
209                 }
210         }
211
212         return r;
213 }
214
215 int
216 _PlayListManagerImpl::GetAllPlayListCount(void) const
217 {
218         int playlistCount = 0;
219
220         int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
221         result r = MapCoreDatabaseErrorToNativeResult(ret);
222
223         SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
224
225         SetLastResult(r);
226         return playlistCount;
227 }
228
229 Tizen::Base::Collection::IList*
230 _PlayListManagerImpl::GetAllPlayListNameN(void) const
231 {
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;
239
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));
244
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.");
247
248         r = pNamesList->Construct();
249         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
250
251         if (pTempList)
252         {
253                 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
254                 {
255                         playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, idx)));
256
257                         if (playListHandle.get() != NULL)
258                         {
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));
263
264                                 if (pTempListName != null)
265                                 {
266                                         SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
267
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.");
270                                 }
271                                 if (pValue.get() != NULL)
272                                 {
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.");
275                                 }
276                         }
277                 }
278         }
279
280         SetLastResult(r);
281         return pNamesList.release();
282 }
283
284 result
285 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
286 {
287         result r = E_SUCCESS;
288
289         switch (reason)
290         {
291         case MEDIA_CONTENT_ERROR_NONE:
292                 r = E_SUCCESS;
293                 break;
294
295         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
296                 r = E_INVALID_ARG;
297                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
298                 break;
299
300         case MEDIA_CONTENT_ERROR_DB_FAILED:
301                 r = E_DATABASE;
302                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
303                 break;
304
305         case MEDIA_CONTENT_ERROR_DB_BUSY:
306                 r = E_SERVICE_BUSY;
307                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
308                 break;
309
310         default:
311                 SysLog(NID_CNT, "default");
312                 r = E_DATABASE;
313                 break;
314         }
315         return r;
316 }
317
318 result
319 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
320 {
321         result r = E_SUCCESS;
322
323         switch (reason)
324         {
325         case MEDIA_CONTENT_ERROR_NONE:
326                 r = E_SUCCESS;
327                 break;
328
329         case MEDIA_CONTENT_ERROR_DB_FAILED:
330                 r = E_DATABASE;
331                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
332                 break;
333
334         case MEDIA_CONTENT_ERROR_DB_BUSY:
335                 r = E_SERVICE_BUSY;
336                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
337                 break;
338
339         default:
340                 SysLog(NID_CNT, "default");
341                 r = E_DATABASE;
342                 break;
343         }
344         return r;
345 }
346
347 bool
348 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
349 {
350         media_playlist_h newPlayListHandle = NULL;
351         media_playlist_clone(&newPlayListHandle, playlistHandle);
352
353         GList** pList = (GList**)pUserData;
354         *pList = g_list_append(*pList, newPlayListHandle);
355
356         return true;
357 }
358
359 }}