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