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