2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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)
44 IMediacontentManager::~IMediacontentManager()
48 void IMediacontentManager::findFolder(IEventFindFolderPtr &ptr)
50 LogDebug("IMediacontentManager::called findFolders");
51 WrtDeviceApis::Commons::EventRequestReceiver<IEventFindFolder>::PostRequest(ptr);
54 void IMediacontentManager::updateMedia(IEventUpdateMediaPtr &ptr)
56 LogDebug("IMediacontentManager::called updateMedia");
57 WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMedia>::PostRequest(ptr);
60 void IMediacontentManager::browseFolder(IEventBrowseFolderPtr &ptr)
62 LogDebug("IMediacontentManager::called IEventBrowseFolder");
63 WrtDeviceApis::Commons::EventRequestReceiver<IEventBrowseFolder>::PostRequest(ptr);
66 void IMediacontentManager::updateMediaItems(IEventUpdateMediaItemsPtr &ptr)
68 LogDebug("IMediacontentManager::called IEventBrowseFolder");
69 WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMediaItems>::PostRequest(ptr);
73 struct scanCallbackData
77 scanCompletedCallback callback;
81 static gboolean _scan_file_completed_cb(void *user_data)
84 LogDebug("called _scan_file_completed_cb:");
86 scanCallbackData *data = static_cast<scanCallbackData*>(user_data);
90 string path = data->path;
91 void* _user_data = data->user_data;
92 scanCompletedCallback _callback = data->callback;
96 if( data->result == MEDIA_CONTENT_ERROR_NONE)
100 else if( data->result == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
102 err_msg = "scanning is failed by out of memory";
104 else if( data->result == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
106 err_msg = "scanning is failed by invalid operation";
108 else if( data->result == MEDIA_CONTENT_ERROR_DB_FAILED)
110 err_msg = "scanning is failed because db operation is failed";
112 else if( data->result == MEDIA_CONTENT_ERROR_DB_BUSY)
114 err_msg = "scanning is failed because db operation is failed";
118 err_msg = "scanning is failed by unknown reason";
120 _callback(err_msg, path, _user_data);
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);
136 bool IMediacontentManager::scanFile(scanCompletedCallback callback, std::string path, void* user_data)
138 LogDebug("ContentManager::called scan");
142 LogDebug("ContentManager::path:" << path);
144 scanCallbackData *data = new scanCallbackData();
146 data->callback = callback;
147 data->user_data = user_data;
149 ecore_thread_run( _scan_file_thread, NULL, NULL, static_cast<void*>(data));
155 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "URI is not available");
160 static bool mediaItemCallback(media_info_h info, void* user_data)
162 media_content_type_e type;
163 LogDebug("mediaItemCallback is called");
165 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
167 if ( type == MEDIA_CONTENT_TYPE_IMAGE)
169 MediacontentManager::readImageFromMediaInfo(info, static_cast<MediacontentImage*>(user_data));
171 else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
173 MediacontentManager::readVideoFromMediaInfo(info, static_cast<MediacontentVideo*>(user_data));
175 else if ( type == MEDIA_CONTENT_TYPE_MUSIC || type == MEDIA_CONTENT_TYPE_SOUND)
177 MediacontentManager::readMusicFromMediaInfo(info, static_cast<MediacontentAudio*>(user_data));
179 else if( type == MEDIA_CONTENT_TYPE_OTHERS)
181 MediacontentManager::readCommonDataFromMediaInfo(info, static_cast<MediacontentMedia*>(user_data));
188 static void content_notification_cb(
189 media_content_error_e error,
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,
200 ContentListener *listener = static_cast<ContentListener*>(user_data);
202 if( error == MEDIA_CONTENT_ERROR_NONE)
205 if( update_item == MEDIA_ITEM_FILE)
207 string condition = "MEDIA_ID=\"";
211 MediacontentMedia *p_content;
212 if(update_type == MEDIA_CONTENT_INSERT)
214 filter_h filter = NULL;
215 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
217 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
219 if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
221 p_content = new MediacontentImage();
223 else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
225 p_content = new MediacontentVideo();
227 else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
229 p_content = new MediacontentAudio();
233 p_content = new MediacontentMedia();
236 if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
238 MediacontentMediaPtr result(p_content);
239 listener->oncontentadded(result);
241 media_filter_destroy(filter);
244 else if(update_type == MEDIA_CONTENT_UPDATE)
247 filter_h filter = NULL;
248 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
250 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
252 if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
254 p_content = new MediacontentImage();
256 else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
258 p_content = new MediacontentVideo();
260 else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
262 p_content = new MediacontentAudio();
266 p_content = new MediacontentMedia();
269 if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
271 MediacontentMediaPtr result(p_content);
272 listener->oncontentupdated(result);
274 media_filter_destroy(filter);
278 else if(update_type == MEDIA_CONTENT_DELETE)
280 listener->oncontentremoved(uuid);
284 else if( error == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
286 err_msg = "scanning is failed by out of memory";
288 else if( error == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
290 err_msg = "scanning is failed by invalid operation";
292 else if( error == MEDIA_CONTENT_ERROR_DB_FAILED)
294 err_msg = "scanning is failed because db operation is failed";
296 else if( error == MEDIA_CONTENT_ERROR_DB_BUSY)
298 err_msg = "scanning is failed because db operation is failed";
302 err_msg = "scanning is failed by unknown reason";
305 LogDebug("ContentListener is called:" + err_msg );
309 bool IMediacontentManager::setListener( void* user_data)
311 LogDebug("ContentManager::called setListener");
314 if(media_content_set_db_updated_cb(content_notification_cb,user_data) == MEDIA_CONTENT_ERROR_NONE)
322 bool IMediacontentManager::unsetListener()
324 LogDebug("ContentManager::called unsetListener");
327 if(media_content_unset_db_updated_cb() == MEDIA_CONTENT_ERROR_NONE)