24bb722481fb6c391f31b6d4501ca25715ec2888
[framework/web/wrt-plugins-tizen.git] / src / Content / IContentManager.cpp
1 //
2 // Tizen Web Device API
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 #include <Commons/StringUtils.h>
19 #include <Commons/ThreadPool.h>
20 #include "IContentManager.h"
21 #include "ContentListener.h"
22 #include "ContentImage.h"
23 #include "ContentMedia.h"
24 #include "ContentVideo.h"
25 #include "ContentAudio.h"
26 #include "ContentManager.h"
27
28 namespace DeviceAPI {
29 namespace Content {
30
31
32 IMediacontentManager::IMediacontentManager() :
33         WrtDeviceApis::Commons::EventRequestReceiver<IEventFindFolder>(WrtDeviceApis::Commons::ThreadEnum::MEDIACONTENT_THREAD),
34         WrtDeviceApis::Commons::EventRequestReceiver<IEventBrowseFolder>(WrtDeviceApis::Commons::ThreadEnum::MEDIACONTENT_THREAD),
35         WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMedia>(WrtDeviceApis::Commons::ThreadEnum::MEDIACONTENT_THREAD),
36         WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMediaItems>(WrtDeviceApis::Commons::ThreadEnum::MEDIACONTENT_THREAD)
37
38 {
39 }
40
41 IMediacontentManager::~IMediacontentManager()
42 {
43 }
44
45 void IMediacontentManager::findFolder(IEventFindFolderPtr &ptr)
46 {
47         LogDebug("IMediacontentManager::called findFolders");
48     WrtDeviceApis::Commons::EventRequestReceiver<IEventFindFolder>::PostRequest(ptr);
49 }
50
51 void IMediacontentManager::updateMedia(IEventUpdateMediaPtr &ptr)
52 {
53         LogDebug("IMediacontentManager::called updateMedia");
54     WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMedia>::PostRequest(ptr);
55 }
56
57 void IMediacontentManager::browseFolder(IEventBrowseFolderPtr &ptr)
58 {
59         LogDebug("IMediacontentManager::called IEventBrowseFolder");
60     WrtDeviceApis::Commons::EventRequestReceiver<IEventBrowseFolder>::PostRequest(ptr);
61 }
62
63 void IMediacontentManager::updateMediaItems(IEventUpdateMediaItemsPtr &ptr)
64 {
65         LogDebug("IMediacontentManager::called IEventBrowseFolder");
66     WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMediaItems>::PostRequest(ptr);
67 }
68
69
70 struct scanCallbackData
71 {
72     std::string path;
73         int result;
74     scanCompletedCallback callback;
75     void *user_data;
76 };
77
78 static gboolean _scan_file_completed_cb(void *user_data)
79 {
80
81         LogDebug("called _scan_file_completed_cb:");
82
83         scanCallbackData *data = static_cast<scanCallbackData*>(user_data);
84
85         if(data != NULL)
86         {
87                 string path = data->path;
88                 void* _user_data = data->user_data;
89                 scanCompletedCallback _callback = data->callback;
90
91                 std::string err_msg;
92
93                 if( data->result == MEDIA_CONTENT_ERROR_NONE)
94                 {
95                         err_msg = "";
96                 }
97                 else if( data->result == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
98                 {
99                         err_msg = "scanning is failed by out of memory";
100                 }
101                 else if( data->result == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
102                 {
103                         err_msg = "scanning is failed by invalid operation";
104                 }
105                 else if( data->result == MEDIA_CONTENT_ERROR_DB_FAILED)
106                 {
107                         err_msg = "scanning is failed because db operation is failed";
108                 }
109                 else if( data->result == MEDIA_CONTENT_ERROR_DB_BUSY)
110                 {
111                         err_msg = "scanning is failed because db operation is failed";
112                 }
113                 else
114                 {
115                         err_msg = "scanning is failed by unknown reason";
116                 }
117                 _callback(err_msg, path, _user_data);
118
119                 delete data;
120         }
121         return false;
122 }
123
124
125 static void _scan_file_thread(void *user_data, Ecore_Thread *thread){
126         LogDebug("_scan_file_thread::called");
127         scanCallbackData *data = static_cast<scanCallbackData*>(user_data);
128         data->result = media_content_scan_file(data->path.c_str());
129         LogDebug("native error code:" << data->result);
130         g_idle_add(_scan_file_completed_cb, data);
131 }
132
133 bool IMediacontentManager::scanFile(scanCompletedCallback callback, std::string path, void* user_data)
134 {
135         LogDebug("ContentManager::called scan");
136         bool ret = false;
137         if( !path.empty() )
138         {
139                 LogDebug("ContentManager::path:" << path);
140
141                 scanCallbackData *data = new scanCallbackData();
142                 data->path = path;
143                 data->callback = callback;
144                 data->user_data = user_data;
145
146                 ecore_thread_run( _scan_file_thread, NULL, NULL, static_cast<void*>(data));
147                 ret = true;
148
149         }
150         else
151         {
152                 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "URI is not available");
153         }
154         return ret;
155 }
156
157 static bool mediaItemCallback(media_info_h info, void* user_data)
158 {
159         media_content_type_e type;
160         LogDebug("mediaItemCallback is called");
161
162         if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
163         {
164                 if ( type == MEDIA_CONTENT_TYPE_IMAGE)
165                 {
166                         MediacontentManager::readImageFromMediaInfo(info, static_cast<MediacontentImage*>(user_data));
167                 }
168                 else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
169                 {
170                         MediacontentManager::readVideoFromMediaInfo(info, static_cast<MediacontentVideo*>(user_data));
171                 }
172                 else if ( type == MEDIA_CONTENT_TYPE_MUSIC || type == MEDIA_CONTENT_TYPE_SOUND)
173                 {
174                         MediacontentManager::readMusicFromMediaInfo(info, static_cast<MediacontentAudio*>(user_data));
175                 }
176                 else if( type == MEDIA_CONTENT_TYPE_OTHERS)
177                 {
178                         MediacontentManager::readCommonDataFromMediaInfo(info, static_cast<MediacontentMedia*>(user_data));
179                 }
180
181         }
182         return false;
183 }
184
185 static void content_notification_cb(
186                                                                         media_content_error_e error,
187                                                                         int pid,
188                                                                         media_content_db_update_item_type_e update_item,
189                                                                         media_content_db_update_type_e update_type,
190                                                                         media_content_type_e media_type,
191                                                                         char *uuid,
192                                                                         char *path,
193                                                                         char *mime_type,
194                                                                         void *user_data)
195 {
196
197         ContentListener *listener = static_cast<ContentListener*>(user_data);
198         std::string err_msg;
199         if( error == MEDIA_CONTENT_ERROR_NONE)
200         {
201                 err_msg = "";
202                 if( update_item == MEDIA_ITEM_FILE)
203                 {
204                         string condition = "MEDIA_ID=\"";
205                         condition += uuid;
206                         condition += "\"";
207
208                         MediacontentMedia *p_content;
209                         if(update_type == MEDIA_CONTENT_INSERT)
210                         {
211                                 filter_h filter = NULL;
212                                 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
213                                 {
214                                         media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
215
216                                         if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
217                                         {
218                                                 p_content = new MediacontentImage();
219                                         }
220                                         else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
221                                         {
222                                                 p_content = new MediacontentVideo();
223                                         }
224                                         else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
225                                         {
226                                                 p_content = new MediacontentAudio();
227                                         }
228                                         else
229                                         {
230                                                 p_content = new MediacontentMedia();
231                                         }
232
233                                         if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
234                                         {
235                                                 MediacontentMediaPtr result(p_content);
236                                                 listener->oncontentadded(result);
237                                         }
238                                         media_filter_destroy(filter);
239                                 }
240                         }
241                         else if(update_type == MEDIA_CONTENT_UPDATE)
242                         {
243
244                                 filter_h filter = NULL;
245                                 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
246                                 {
247                                         media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
248
249                                         if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
250                                         {
251                                                 p_content = new MediacontentImage();
252                                         }
253                                         else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
254                                         {
255                                                 p_content = new MediacontentVideo();
256                                         }
257                                         else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
258                                         {
259                                                 p_content = new MediacontentAudio();
260                                         }
261                                         else
262                                         {
263                                                 p_content = new MediacontentMedia();
264                                         }
265
266                                         if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
267                                         {
268                                                 MediacontentMediaPtr result(p_content);
269                                                 listener->oncontentupdated(result);
270                                         }
271                                         media_filter_destroy(filter);
272                                 }
273
274                         }
275                         else if(update_type == MEDIA_CONTENT_DELETE)
276                         {
277                                 listener->oncontentremoved(uuid);
278                         }
279                 }
280         }
281         else if( error == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
282         {
283                 err_msg = "scanning is failed by out of memory";
284         }
285         else if( error == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
286         {
287                 err_msg = "scanning is failed by invalid operation";
288         }
289         else if( error == MEDIA_CONTENT_ERROR_DB_FAILED)
290         {
291                 err_msg = "scanning is failed because db operation is failed";
292         }
293         else if( error == MEDIA_CONTENT_ERROR_DB_BUSY)
294         {
295                 err_msg = "scanning is failed because db operation is failed";
296         }
297         else
298         {
299                 err_msg = "scanning is failed by unknown reason";
300         }
301
302         LogDebug("ContentListener is called:" + err_msg );
303
304 }
305
306 bool IMediacontentManager::setListener( void* user_data)
307 {
308         LogDebug("ContentManager::called setListener");
309         bool ret = false;
310
311         if(media_content_set_db_updated_cb(content_notification_cb,user_data) == MEDIA_CONTENT_ERROR_NONE)
312         {
313                 ret = true;
314         }
315
316         return ret;
317 }
318
319 bool IMediacontentManager::unsetListener()
320 {
321         LogDebug("ContentManager::called unsetListener");
322         bool ret = false;
323
324         if(media_content_unset_db_updated_cb() == MEDIA_CONTENT_ERROR_NONE)
325         {
326                 ret = true;
327         }
328
329         return ret;
330 }
331
332 }
333 }