Fix : Remove exception handling codes at the sample code
[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(L"");
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         r = nameExpr.Append(playListName);
106         SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for nameExpr.");
107
108         if (!nameExpr.IsEmpty())
109         {
110                 r = nameExpr.Replace(L"\'", L"''");
111                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Replace operation for nameExpr.");
112
113                 r = nameExpr.Insert('\'', 0);
114                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
115
116                 r = nameExpr.Insert('\'', nameExpr.GetLength());
117                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Insert operation for nameExpr.");
118
119                 r = inputCondition.Append(nameExpr);
120                 SysTryReturnResult(NID_CNT, r == E_SUCCESS, r, "Failed to perform Append operation for inputCondition.");
121         }
122
123         if (!inputCondition.IsEmpty())
124         {
125                 //CopyToCharArrayN: utility function, converts a osp string to char*
126                 pInputCond = std::unique_ptr<char[]>(_StringConverter::CopyToCharArrayN(inputCondition));
127                 SysTryReturnResult(NID_CNT, pInputCond, E_OUT_OF_MEMORY, "The memory is insufficient.");
128
129                 SysLog(NID_CNT, "pInputCond is [%s]", pInputCond.get());
130
131                 ret = media_filter_set_condition(pFilterHandle.get(), pInputCond.get(), MEDIA_CONTENT_COLLATE_DEFAULT);
132                 r = MapCoreErrorToNativeResult(ret);
133                 SysTryReturnResult(NID_CNT, ret == MEDIA_CONTENT_ERROR_NONE, r, "Failed to perform media_filter_set_condition operation.");
134         }
135
136         __pFilterHandle.reset(pFilterHandle.release());
137
138         return  r;
139 }
140
141 PlayList*
142 _PlayListManagerImpl::GetPlayListN(const Tizen::Base::String& playListName) const
143 {
144         std::unique_ptr<GList, GListDeleter> pItemList;
145         GList* pTempList = null;
146         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
147         int playlistId = 0;
148         std::unique_ptr<PlayList> pFinalOutplayList;
149         int ret = MEDIA_CONTENT_ERROR_NONE;
150
151         result r = CreateFilter(playListName);
152         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_DATABASE, "[E_DATABASE] Failed to perform CreateFilter operation.");
153
154         pTempList = pItemList.get();
155         ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
156         r = MapCoreErrorToNativeResult(ret);
157         SysTryReturn(NID_CNT, r == E_SUCCESS , null, r, "[%s] Failed to perform media_playlist_foreach_playlist_from_db operation.", GetErrorMessage(r));
158
159         if (pTempList == NULL)
160         {
161                 r = E_INVALID_ARG; // No match found.
162         }
163         else
164         {
165                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
166                 if (playListHandle.get() != NULL)
167                 {
168                         ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
169                         r = MapCoreErrorToNativeResult(ret);
170                         SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_playlist_id operation.", GetErrorMessage(r));
171
172                         pFinalOutplayList = std::unique_ptr<PlayList>(new (std::nothrow) PlayList());
173                         SysTryReturn(NID_CNT, pFinalOutplayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FinalOutList  is null.");
174
175                         r = pFinalOutplayList->ConstructPlayList(playListName);
176                         SysTryReturn(NID_CNT, r == E_SUCCESS, null, r, "[%s] Failed to perform ConstructPlayList operation for pFinalOutplayList.", GetErrorMessage(r));
177                 }
178         }
179
180         SetLastResult(r);
181         return pFinalOutplayList.release();
182 }
183
184 result
185 _PlayListManagerImpl::RemovePlayList(const Tizen::Base::String& playListName)
186 {
187         std::unique_ptr<GList, GListDeleter> pItemList;
188         GList* pTempList = null;
189         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
190         int playlistId = 0;
191         int ret = MEDIA_CONTENT_ERROR_NONE;
192
193         result r = CreateFilter(playListName);
194         SysTryReturnResult(NID_CNT, r == E_SUCCESS, E_DATABASE, "Failed to perform CreateFilter operation.");
195
196         pTempList = pItemList.get();
197         ret = media_playlist_foreach_playlist_from_db(__pFilterHandle.get(), MediaPlayListCb, &pTempList);
198         r = MapCoreErrorToNativeResult(ret);
199         SysTryReturnResult(NID_CNT, r == E_SUCCESS , r, "Failed to perform media_playlist_foreach_playlist_from_db operation.");
200
201         if (pTempList == NULL)
202         {
203                 r = E_INVALID_ARG; // No match found.
204         }
205         else
206         {
207                 playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, 0)));
208
209                 if (playListHandle.get() != NULL)
210                 {
211                         ret = media_playlist_get_playlist_id(playListHandle.get(), &playlistId);
212                         r = MapCoreErrorToNativeResult(ret);
213                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_get_playlist_id operation.");
214
215                         ret = media_playlist_delete_from_db(playlistId);
216                         r = MapCoreErrorToNativeResult(ret);
217                         SysTryReturnResult(NID_CNT, !IsFailed(r), r, "Failed to perform media_playlist_delete_from_db operation.");
218                 }
219         }
220
221         return r;
222 }
223
224 int
225 _PlayListManagerImpl::GetAllPlayListCount(void) const
226 {
227         int playlistCount = 0;
228
229         int ret = media_playlist_get_playlist_count_from_db(NULL, &playlistCount);
230         result r = MapCoreDatabaseErrorToNativeResult(ret);
231
232         SysLog(NID_CNT, "GetAllPlayListCount is [%d] and result is [%s]", playlistCount, GetErrorMessage(r));
233
234         SetLastResult(r);
235         return playlistCount;
236 }
237
238 Tizen::Base::Collection::IList*
239 _PlayListManagerImpl::GetAllPlayListNameN(void) const
240 {
241         std::unique_ptr<GList, GListDeleter> pItemList;
242         GList* pTempList = null;
243         std::unique_ptr<media_playlist_s, PlayListHandleDeleter> playListHandle;
244         char* pTempListName = null;
245         std::unique_ptr<Object> pValue;
246         std::unique_ptr<ArrayList, AllElementsDeleter> pNamesList;
247
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));
252
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.");
255
256         r = pNamesList->Construct();
257         SysTryReturn(NID_CNT, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
258
259         if (pTempList)
260         {
261                 for (int idx = 0; idx < (int)g_list_length(pTempList); idx++)
262                 {
263                         playListHandle.reset(static_cast<media_playlist_h>(g_list_nth_data(pTempList, idx)));
264
265                         if (playListHandle.get() != NULL)
266                         {
267                                 ret = media_playlist_get_name(playListHandle.get(), &pTempListName);
268                                 r = MapCoreDatabaseErrorToNativeResult(ret);
269                                 SysTryReturn(NID_CNT, !IsFailed(r), null, r, "[%s] Failed to perform media_playlist_get_name operation.", GetErrorMessage(r));
270
271                                 if (pTempListName != null)
272                                 {
273                                         SysLog(NID_CNT, "pPlayListName is [%s]", pTempListName);
274
275                                         std::unique_ptr<char[]> pPlayListName(pTempListName);
276                                         SysTryReturn(NID_CNT, pPlayListName != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pPlayListName is null.");
277
278                                         pValue = std::unique_ptr<Object>(new (std::nothrow) String(pPlayListName.get()));
279                                         SysTryReturn(NID_CNT, pValue != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
280                                 }
281                                 if (pValue.get() != NULL)
282                                 {
283                                         r = pNamesList->Add(*(pValue.release()));
284                                         SysTryReturn(NID_CNT, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
285                                 }
286                         }
287                 }
288         }
289
290         SetLastResult(r);
291         return pNamesList.release();
292 }
293
294 result
295 _PlayListManagerImpl::MapCoreErrorToNativeResult(int reason) const
296 {
297         result r = E_SUCCESS;
298
299         switch (reason)
300         {
301         case MEDIA_CONTENT_ERROR_NONE:
302                 r = E_SUCCESS;
303                 break;
304
305         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
306                 r = E_OUT_OF_MEMORY;
307                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
308                 break;
309
310         case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
311                 r = E_INVALID_ARG;
312                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_INVALID_PARAMETER");
313                 break;
314
315         case MEDIA_CONTENT_ERROR_DB_FAILED:
316                 r = E_DATABASE;
317                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
318                 break;
319
320         case MEDIA_CONTENT_ERROR_DB_BUSY:
321                 r = E_SERVICE_BUSY;
322                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
323                 break;
324
325         default:
326                 SysLog(NID_CNT, "default");
327                 r = E_DATABASE;
328                 break;
329         }
330         return r;
331 }
332
333 result
334 _PlayListManagerImpl::MapCoreDatabaseErrorToNativeResult(int reason) const
335 {
336         result r = E_SUCCESS;
337
338         switch (reason)
339         {
340         case MEDIA_CONTENT_ERROR_NONE:
341                 r = E_SUCCESS;
342                 break;
343
344         case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
345                 r = E_OUT_OF_MEMORY;
346                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_OUT_OF_MEMORY");
347                 break;
348
349         case MEDIA_CONTENT_ERROR_DB_FAILED:
350                 r = E_DATABASE;
351                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_FAILED");
352                 break;
353
354         case MEDIA_CONTENT_ERROR_DB_BUSY:
355                 r = E_SERVICE_BUSY;
356                 SysLog(NID_CNT, "MEDIA_CONTENT_ERROR_DB_BUSY");
357                 break;
358
359         default:
360                 SysLog(NID_CNT, "default");
361                 r = E_DATABASE;
362                 break;
363         }
364         return r;
365 }
366
367 bool
368 MediaPlayListCb(media_playlist_h playlistHandle, void *pUserData)
369 {
370         media_playlist_h newPlayListHandle = NULL;
371         media_playlist_clone(&newPlayListHandle, playlistHandle);
372
373         GList** pList = (GList**)pUserData;
374         *pList = g_list_append(*pList, newPlayListHandle);
375
376         return true;
377 }
378
379 }}