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