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.
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"
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)
43 IMediacontentManager::~IMediacontentManager()
47 void IMediacontentManager::findFolder(IEventFindFolderPtr &ptr)
49 LoggerD("IMediacontentManager::called findFolders");
50 WrtDeviceApis::Commons::EventRequestReceiver<IEventFindFolder>::PostRequest(ptr);
53 void IMediacontentManager::updateMedia(IEventUpdateMediaPtr &ptr)
55 LoggerD("IMediacontentManager::called updateMedia");
56 WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMedia>::PostRequest(ptr);
59 void IMediacontentManager::browseFolder(IEventBrowseFolderPtr &ptr)
61 LoggerD("IMediacontentManager::called IEventBrowseFolder");
62 WrtDeviceApis::Commons::EventRequestReceiver<IEventBrowseFolder>::PostRequest(ptr);
65 void IMediacontentManager::updateMediaItems(IEventUpdateMediaItemsPtr &ptr)
67 LoggerD("IMediacontentManager::called IEventBrowseFolder");
68 WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMediaItems>::PostRequest(ptr);
72 struct scanCallbackData
76 scanCompletedCallback callback;
80 static gboolean _scan_file_completed_cb(void *user_data)
83 LoggerD("called _scan_file_completed_cb:");
85 scanCallbackData *data = static_cast<scanCallbackData*>(user_data);
89 string path = data->path;
90 void* _user_data = data->user_data;
91 scanCompletedCallback _callback = data->callback;
95 if( data->result == MEDIA_CONTENT_ERROR_NONE)
99 else if( data->result == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
101 err_msg = "scanning is failed by out of memory";
103 else if( data->result == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
105 err_msg = "scanning is failed by invalid operation";
107 else if( data->result == MEDIA_CONTENT_ERROR_DB_FAILED)
109 err_msg = "scanning is failed because db operation is failed";
111 else if( data->result == MEDIA_CONTENT_ERROR_DB_BUSY)
113 err_msg = "scanning is failed because db operation is failed";
117 err_msg = "scanning is failed by unknown reason";
119 _callback(err_msg, path, _user_data);
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);
135 bool IMediacontentManager::scanFile(scanCompletedCallback callback, std::string path, void* user_data)
137 LoggerD("ContentManager::called scan");
141 LoggerD("ContentManager::path:" << path);
143 scanCallbackData *data = new scanCallbackData();
145 data->callback = callback;
146 data->user_data = user_data;
148 ecore_thread_run( _scan_file_thread, NULL, NULL, static_cast<void*>(data));
154 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "URI is not available");
159 static bool mediaItemCallback(media_info_h info, void* user_data)
161 media_content_type_e type;
162 LoggerD("mediaItemCallback is called");
164 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
166 if ( type == MEDIA_CONTENT_TYPE_IMAGE)
168 MediacontentManager::readImageFromMediaInfo(info, static_cast<MediacontentImage*>(user_data));
170 else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
172 MediacontentManager::readVideoFromMediaInfo(info, static_cast<MediacontentVideo*>(user_data));
174 else if ( type == MEDIA_CONTENT_TYPE_MUSIC || type == MEDIA_CONTENT_TYPE_SOUND)
176 MediacontentManager::readMusicFromMediaInfo(info, static_cast<MediacontentAudio*>(user_data));
178 else if( type == MEDIA_CONTENT_TYPE_OTHERS)
180 MediacontentManager::readCommonDataFromMediaInfo(info, static_cast<MediacontentMedia*>(user_data));
187 static void content_notification_cb(
188 media_content_error_e error,
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,
199 ContentListener *listener = static_cast<ContentListener*>(user_data);
201 if( error == MEDIA_CONTENT_ERROR_NONE)
204 if( update_item == MEDIA_ITEM_FILE)
206 string condition = "MEDIA_ID=\"";
210 MediacontentMedia *p_content;
211 if(update_type == MEDIA_CONTENT_INSERT)
213 filter_h filter = NULL;
214 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
216 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
218 if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
220 p_content = new MediacontentImage();
222 else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
224 p_content = new MediacontentVideo();
226 else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
228 p_content = new MediacontentAudio();
232 p_content = new MediacontentMedia();
235 if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
237 MediacontentMediaPtr result(p_content);
238 listener->oncontentadded(result);
240 media_filter_destroy(filter);
243 else if(update_type == MEDIA_CONTENT_UPDATE)
246 filter_h filter = NULL;
247 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
249 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
251 if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
253 p_content = new MediacontentImage();
255 else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
257 p_content = new MediacontentVideo();
259 else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
261 p_content = new MediacontentAudio();
265 p_content = new MediacontentMedia();
268 if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
270 MediacontentMediaPtr result(p_content);
271 listener->oncontentupdated(result);
273 media_filter_destroy(filter);
277 else if(update_type == MEDIA_CONTENT_DELETE)
279 listener->oncontentremoved(uuid);
283 else if( error == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
285 err_msg = "scanning is failed by out of memory";
287 else if( error == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
289 err_msg = "scanning is failed by invalid operation";
291 else if( error == MEDIA_CONTENT_ERROR_DB_FAILED)
293 err_msg = "scanning is failed because db operation is failed";
295 else if( error == MEDIA_CONTENT_ERROR_DB_BUSY)
297 err_msg = "scanning is failed because db operation is failed";
301 err_msg = "scanning is failed by unknown reason";
304 LoggerD("ContentListener is called:" + err_msg );
308 bool IMediacontentManager::setListener( void* user_data)
310 LoggerD("ContentManager::called setListener");
313 if(media_content_set_db_updated_cb(content_notification_cb,user_data) == MEDIA_CONTENT_ERROR_NONE)
320 bool IMediacontentManager::unsetListener()
322 LoggerD("ContentManager::called unsetListener");
325 if(media_content_unset_db_updated_cb() == MEDIA_CONTENT_ERROR_NONE)