[content] Add impl classes for ContentInfo
[platform/framework/native/content.git] / src / FCnt_PlayListManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 /**
17  * @file                FCnt_PlayListStatisticsImpl.cpp
18  * @brief               This is the implementation file for the %_PlayListStatisticsImpl class.
19  *
20  * This file contains implementation of the %_PlayListStatisticsImpl class.
21  */
22
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"
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Io;
36
37 namespace Tizen { namespace Content
38 {
39
40 // Declaration for Callback function registered to playlist details
41 bool MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData);
42
43 _PlayListManagerImpl::_PlayListManagerImpl(void)
44         : __pFilterHandle(NULL)
45 {
46
47 }
48
49
50 //(disconnects the DB connection)
51 _PlayListManagerImpl::~_PlayListManagerImpl(void)
52 {
53         int ret = MEDIA_CONTENT_ERROR_NONE;
54         result r = E_SUCCESS;
55
56         ret = media_content_disconnect();
57         r = MapCoreErrorToNativeResult(ret);
58         SysTryLog(r == E_SUCCESS, "[%s] Propagating for media_content_disconnect.", GetErrorMessage(r));
59
60         SysLog(NID_CNT, "media_content_disconnect result[%d].", ret);
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         SysLog(NID_CNT, "media_content_connect result[%d].", ret);
86
87         return r;
88 }
89
90 result
91 _PlayListManagerImpl::CreateFilter(const Tizen::Base::String& playListName) const
92 {
93         std::unique_ptr<char[]> pInputCond;
94         filter_h tempFilter = NULL;
95         String inputCondition = L"PLAYLIST_NAME = ";
96         String nameExpr(playListName);
97
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.");
101
102         std::unique_ptr<filter_s, FilterDeleter> pFilterHandle(tempFilter);
103         SysTryReturnResult(NID_CNT, pFilterHandle != null, E_OUT_OF_MEMORY, "pFilterHandle is null.");
104
105         if (!nameExpr.IsEmpty())
106         {
107                 r = nameExpr.Replace("\'", "''");
108                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Replace operation for nameExpr.");
109
110                 r = nameExpr.Insert('\'', 0);
111                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
112
113                 r = nameExpr.Insert('\'', nameExpr.GetLength());
114                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
115
116                 r = inputCondition.Append(nameExpr);
117                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
118         }
119
120         if (!inputCondition.IsEmpty())
121         {
122                 //CopyToCharArrayN: utility function, converts a osp string to char*
123                 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
124                 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "The memory is insufficient.");
125
126                 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
127
128                 ret = media_filter_set_condition(pFilterHandle.get(), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
129                 r = MapCoreErrorToNativeResult(ret);
130                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
131         }
132
133         __pFilterHandle.reset(pFilterHandle.release());
134
135         return  r;
136 }
137
138 PlayList*
139 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
140 {
141         std::unique_ptr<GList, GListDeleter> pItemList;
142         GList* pTempList = null;
143         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
144         int playlistId = 0;
145         std::unique_ptr<PlayList> pFinalOutplayList;
146         int ret = MEDIA_CONTENT_ERROR_NONE;
147
148         result r = CreateFilter(playListName);
149         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
150
151         pTempList = pItemList.get();
152         ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
153         r = MapCoreErrorToNativeResult(ret);
154         SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
155
156         if (pTempList == NULL)
157         {
158                 r = E_INVALID_ARG; // No match found.
159         }
160         else
161         {
162                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
163                 if (playListHandle.get() != NULL)
164                 {
165                         ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
166                         r = MapCoreErrorToNativeResult(ret);
167                         SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
168
169                         pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
170                         SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList  is null.");
171
172                         r = pFinalOutplayList->ConstructPlayList(playListName);
173                         SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
174                 }
175         }
176
177         SetLastResult(r);
178         return pFinalOutplayList.release();
179 }
180
181 result
182 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
183 {
184         std::unique_ptr<GList, GListDeleter> pItemList;
185         GList* pTempList = null;
186         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
187         int playlistId = 0;
188         int ret = MEDIA_CONTENT_ERROR_NONE;
189
190         result r = CreateFilter(playListName);
191         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
192
193         pTempList = pItemList.get();
194         ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
195         r = MapCoreErrorToNativeResult(ret);
196         SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
197
198         if (pTempList == NULL)
199         {
200                 r = E_INVALID_ARG; // No match found.
201         }
202         else
203         {
204                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
205
206                 if (playListHandle.get() != NULL)
207                 {
208                         ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
209                         r = MapCoreErrorToNativeResult(ret);
210                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
211
212                         ret = media_playlist_delete_from_db(playlistId);
213                         r = MapCoreErrorToNativeResult(ret);
214                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
215                 }
216         }
217
218         return r;
219 }
220
221 int
222 _PlayListManagerImpl::GetAllPlayListCount(void) const
223 {
224         int playlistCount = 0;
225
226         int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
227         result r = MapCoreDatabaseErrorToNativeResult(ret);
228
229         SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
230
231         SetLastResult(r);
232         return playlistCount;
233 }
234
235 Tizen::Base::Collection::IList*
236 _PlayListManagerImpl::GetAllPlayListNameN(void) const
237 {
238         std::unique_ptr<GList, GListDeleter> pItemList;
239         GList* pTempList = null;
240         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
241         char* pTempListName = null;
242         std::unique_ptr<Object> pValue;
243         std::unique_ptr<ArrayList, AllElementsDeleter> pNamesList;
244
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));
249
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.");
252
253         r = pNamesList->Construct();
254         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
255
256         if (pTempList)
257         {
258                 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
259                 {
260                         playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, idx)));
261
262                         if (playListHandle.get() != NULL)
263                         {
264                                 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
265                                 r = MapCoreDatabaseErrorToNativeResult(ret);
266                                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
267
268                                 if (pTempListName != null)
269                                 {
270                                         SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
271
272                                         std::unique_ptr<char[]> pPlayListName(pTempListName);
273                                         SysTryReturn(NID_CNT, pPlayListName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPlayListName is null.");
274
275                                         pValue = std::unique_ptr<Object>(new (std::nothrow) String(pPlayListName.get()));
276                                         SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
277                                 }
278                                 if (pValue.get() != NULL)
279                                 {
280                                         r = pNamesList->Add(*(pValue.release()));
281                                         SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
282                                 }
283                         }
284                 }
285         }
286
287         SetLastResult(r);
288         return pNamesList.release();
289 }
290
291 result
292 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
293 {
294         result r = E_SUCCESS;
295
296         switch (reason)
297         {
298         case MEDIA_CONTENT_ERROR_NONE:
299                 r = E_SUCCESS;
300                 break;
301
302         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
303                 r = E_OUT_OF_MEMORY;
304                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
305                 break;
306
307         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
308                 r = E_INVALID_ARG;
309                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
310                 break;
311
312         case MEDIA_CONTENT_ERROR_DB_FAILED:
313                 r = E_DATABASE;
314                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
315                 break;
316
317         case MEDIA_CONTENT_ERROR_DB_BUSY:
318                 r = E_SERVICE_BUSY;
319                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
320                 break;
321
322         default:
323                 SysLog(NID_CNT, "default");
324                 r = E_DATABASE;
325                 break;
326         }
327         return r;
328 }
329
330 result
331 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
332 {
333         result r = E_SUCCESS;
334
335         switch (reason)
336         {
337         case MEDIA_CONTENT_ERROR_NONE:
338                 r = E_SUCCESS;
339                 break;
340
341         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
342                 r = E_OUT_OF_MEMORY;
343                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
344                 break;
345
346         case MEDIA_CONTENT_ERROR_DB_FAILED:
347                 r = E_DATABASE;
348                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
349                 break;
350
351         case MEDIA_CONTENT_ERROR_DB_BUSY:
352                 r = E_SERVICE_BUSY;
353                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
354                 break;
355
356         default:
357                 SysLog(NID_CNT, "default");
358                 r = E_DATABASE;
359                 break;
360         }
361         return r;
362 }
363
364 bool
365 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
366 {
367         media_playlist_h newPlayListHandle = NULL;
368         media_playlist_clone(&newPlayListHandle, playlistHandle);
369
370         GList** pList = (GList**)pUserData;
371         *pList = g_list_append(*pList, newPlayListHandle);
372
373         return true;
374 }
375
376 }}