[content] Change secure log
[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         SysLog(NID_CNT, "media_content_disconnect result[%d].", ret);
63 }
64
65  _PlayListManagerImpl*
66  _PlayListManagerImpl::GetInstance(PlayListManager& playListManager)
67 {
68         return (&playListManager != null) ? playListManager.__pImpl : null;
69 }
70
71 const _PlayListManagerImpl*
72 _PlayListManagerImpl::GetInstance(const PlayListManager& playListManager)
73 {
74         return (&playListManager != null) ? playListManager.__pImpl : null;
75 }
76
77 result
78 _PlayListManagerImpl::Construct(void)
79 {
80         result r = E_SUCCESS;
81         int ret = MEDIA_CONTENT_ERROR_NONE;
82
83         ret = media_content_connect();
84         r = MapCoreErrorToNativeResult(ret);
85         SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Propagating for media_content_disconnect.");
86
87         SysLog(NID_CNT, "media_content_connect result[%d].", ret);
88
89         return r;
90 }
91
92 result
93 _PlayListManagerImpl::CreateFilter(const Tizen::Base::String& playListName) const
94 {
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);
99
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.");
103
104         if (!nameExpr.IsEmpty())
105         {
106                 r = nameExpr.Replace("\'", "''");
107                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Replace operation for nameExpr.");
108
109                 r = nameExpr.Insert('\'', 0);
110                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
111
112                 r = nameExpr.Insert('\'', nameExpr.GetLength());
113                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
114
115                 r = inputCondition.Append(nameExpr);
116                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
117         }
118
119         if (!inputCondition.IsEmpty())
120         {
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.");
124
125                 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
126
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.");
130         }
131
132         __pFilterHandle.reset(pFilterHandle.release());
133
134         return  r;
135 }
136
137 PlayList*
138 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
139 {
140         std::unique_ptr<GList, GListDeleter> pItemList;
141         GList* pTempList = null;
142         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
143         int playlistId = 0;
144         std::unique_ptr<PlayList> pFinalOutplayList;
145         int ret = MEDIA_CONTENT_ERROR_NONE;
146
147         result r = CreateFilter(playListName);
148         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
149
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));
154
155         if (pTempList == NULL)
156         {
157                 r = E_INVALID_ARG; // No match found.
158         }
159         else
160         {
161                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
162                 if (playListHandle.get() != NULL)
163                 {
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));
167
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.");
170
171                         r = pFinalOutplayList->ConstructPlayList(playListName);
172                         SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
173                 }
174         }
175
176         SetLastResult(r);
177         return pFinalOutplayList.release();
178 }
179
180 result
181 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
182 {
183         std::unique_ptr<GList, GListDeleter> pItemList;
184         GList* pTempList = null;
185         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
186         int playlistId = 0;
187         int ret = MEDIA_CONTENT_ERROR_NONE;
188
189         result r = CreateFilter(playListName);
190         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
191
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.");
196
197         if (pTempList == NULL)
198         {
199                 r = E_INVALID_ARG; // No match found.
200         }
201         else
202         {
203                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
204
205                 if (playListHandle.get() != NULL)
206                 {
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.");
210
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.");
214                 }
215         }
216
217         return r;
218 }
219
220 int
221 _PlayListManagerImpl::GetAllPlayListCount(void) const
222 {
223         int playlistCount = 0;
224
225         int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
226         result r = MapCoreDatabaseErrorToNativeResult(ret);
227
228         SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
229
230         SetLastResult(r);
231         return playlistCount;
232 }
233
234 Tizen::Base::Collection::IList*
235 _PlayListManagerImpl::GetAllPlayListNameN(void) const
236 {
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;
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                                 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));
268
269                                 if (pTempListName != null)
270                                 {
271                                         SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
272
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.");
275                                 }
276                                 if (pValue.get() != NULL)
277                                 {
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.");
280                                 }
281                         }
282                 }
283         }
284
285         SetLastResult(r);
286         return pNamesList.release();
287 }
288
289 result
290 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
291 {
292         result r = E_SUCCESS;
293
294         switch (reason)
295         {
296         case MEDIA_CONTENT_ERROR_NONE:
297                 r = E_SUCCESS;
298                 break;
299
300         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
301                 r = E_INVALID_ARG;
302                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
303                 break;
304
305         case MEDIA_CONTENT_ERROR_DB_FAILED:
306                 r = E_DATABASE;
307                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
308                 break;
309
310         case MEDIA_CONTENT_ERROR_DB_BUSY:
311                 r = E_SERVICE_BUSY;
312                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
313                 break;
314
315         default:
316                 SysLog(NID_CNT, "default");
317                 r = E_DATABASE;
318                 break;
319         }
320         return r;
321 }
322
323 result
324 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
325 {
326         result r = E_SUCCESS;
327
328         switch (reason)
329         {
330         case MEDIA_CONTENT_ERROR_NONE:
331                 r = E_SUCCESS;
332                 break;
333
334         case MEDIA_CONTENT_ERROR_DB_FAILED:
335                 r = E_DATABASE;
336                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
337                 break;
338
339         case MEDIA_CONTENT_ERROR_DB_BUSY:
340                 r = E_SERVICE_BUSY;
341                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
342                 break;
343
344         default:
345                 SysLog(NID_CNT, "default");
346                 r = E_DATABASE;
347                 break;
348         }
349         return r;
350 }
351
352 bool
353 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
354 {
355         media_playlist_h newPlayListHandle = NULL;
356         media_playlist_clone(&newPlayListHandle, playlistHandle);
357
358         GList** pList = (GList**)pUserData;
359         *pList = g_list_append(*pList, newPlayListHandle);
360
361         return true;
362 }
363
364 }}