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"
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)
41 IMediacontentManager::~IMediacontentManager()
45 void IMediacontentManager::findFolder(IEventFindFolderPtr &ptr)
47 LogDebug("IMediacontentManager::called findFolders");
48 WrtDeviceApis::Commons::EventRequestReceiver<IEventFindFolder>::PostRequest(ptr);
51 void IMediacontentManager::updateMedia(IEventUpdateMediaPtr &ptr)
53 LogDebug("IMediacontentManager::called updateMedia");
54 WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMedia>::PostRequest(ptr);
57 void IMediacontentManager::browseFolder(IEventBrowseFolderPtr &ptr)
59 LogDebug("IMediacontentManager::called IEventBrowseFolder");
60 WrtDeviceApis::Commons::EventRequestReceiver<IEventBrowseFolder>::PostRequest(ptr);
63 void IMediacontentManager::updateMediaItems(IEventUpdateMediaItemsPtr &ptr)
65 LogDebug("IMediacontentManager::called IEventBrowseFolder");
66 WrtDeviceApis::Commons::EventRequestReceiver<IEventUpdateMediaItems>::PostRequest(ptr);
70 struct scanCallbackData
74 scanCompletedCallback callback;
78 static gboolean _scan_file_completed_cb(void *user_data)
81 LogDebug("called _scan_file_completed_cb:");
83 scanCallbackData *data = static_cast<scanCallbackData*>(user_data);
87 string path = data->path;
88 void* _user_data = data->user_data;
89 scanCompletedCallback _callback = data->callback;
93 if( data->result == MEDIA_CONTENT_ERROR_NONE)
97 else if( data->result == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
99 err_msg = "scanning is failed by out of memory";
101 else if( data->result == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
103 err_msg = "scanning is failed by invalid operation";
105 else if( data->result == MEDIA_CONTENT_ERROR_DB_FAILED)
107 err_msg = "scanning is failed because db operation is failed";
109 else if( data->result == MEDIA_CONTENT_ERROR_DB_BUSY)
111 err_msg = "scanning is failed because db operation is failed";
115 err_msg = "scanning is failed by unknown reason";
117 _callback(err_msg, path, _user_data);
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);
133 bool IMediacontentManager::scanFile(scanCompletedCallback callback, std::string path, void* user_data)
135 LogDebug("ContentManager::called scan");
139 LogDebug("ContentManager::path:" << path);
141 scanCallbackData *data = new scanCallbackData();
143 data->callback = callback;
144 data->user_data = user_data;
146 ecore_thread_run( _scan_file_thread, NULL, NULL, static_cast<void*>(data));
152 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "URI is not available");
157 static bool mediaItemCallback(media_info_h info, void* user_data)
159 media_content_type_e type;
160 LogDebug("mediaItemCallback is called");
162 if ( MEDIA_CONTENT_ERROR_NONE == media_info_get_media_type( info, &type) )
164 if ( type == MEDIA_CONTENT_TYPE_IMAGE)
166 MediacontentManager::readImageFromMediaInfo(info, static_cast<MediacontentImage*>(user_data));
168 else if ( type == MEDIA_CONTENT_TYPE_VIDEO)
170 MediacontentManager::readVideoFromMediaInfo(info, static_cast<MediacontentVideo*>(user_data));
172 else if ( type == MEDIA_CONTENT_TYPE_MUSIC || type == MEDIA_CONTENT_TYPE_SOUND)
174 MediacontentManager::readMusicFromMediaInfo(info, static_cast<MediacontentAudio*>(user_data));
176 else if( type == MEDIA_CONTENT_TYPE_OTHERS)
178 MediacontentManager::readCommonDataFromMediaInfo(info, static_cast<MediacontentMedia*>(user_data));
185 static void content_notification_cb(
186 media_content_error_e error,
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,
197 ContentListener *listener = static_cast<ContentListener*>(user_data);
199 if( error == MEDIA_CONTENT_ERROR_NONE)
202 if( update_item == MEDIA_ITEM_FILE)
204 string condition = "MEDIA_ID=\"";
208 MediacontentMedia *p_content;
209 if(update_type == MEDIA_CONTENT_INSERT)
211 filter_h filter = NULL;
212 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
214 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
216 if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
218 p_content = new MediacontentImage();
220 else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
222 p_content = new MediacontentVideo();
224 else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
226 p_content = new MediacontentAudio();
230 p_content = new MediacontentMedia();
233 if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
235 MediacontentMediaPtr result(p_content);
236 listener->oncontentadded(result);
238 media_filter_destroy(filter);
241 else if(update_type == MEDIA_CONTENT_UPDATE)
244 filter_h filter = NULL;
245 if ( MEDIA_CONTENT_ERROR_NONE == media_filter_create(&filter))
247 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
249 if(media_type == MEDIA_CONTENT_TYPE_IMAGE)
251 p_content = new MediacontentImage();
253 else if(media_type == MEDIA_CONTENT_TYPE_VIDEO)
255 p_content = new MediacontentVideo();
257 else if(media_type == MEDIA_CONTENT_TYPE_SOUND || media_type == MEDIA_CONTENT_TYPE_MUSIC)
259 p_content = new MediacontentAudio();
263 p_content = new MediacontentMedia();
266 if( MEDIA_CONTENT_ERROR_NONE == media_info_foreach_media_from_db(filter,mediaItemCallback,(void*)p_content))
268 MediacontentMediaPtr result(p_content);
269 listener->oncontentupdated(result);
271 media_filter_destroy(filter);
275 else if(update_type == MEDIA_CONTENT_DELETE)
277 listener->oncontentremoved(uuid);
281 else if( error == MEDIA_CONTENT_ERROR_OUT_OF_MEMORY)
283 err_msg = "scanning is failed by out of memory";
285 else if( error == MEDIA_CONTENT_ERROR_INVALID_OPERATION)
287 err_msg = "scanning is failed by invalid operation";
289 else if( error == MEDIA_CONTENT_ERROR_DB_FAILED)
291 err_msg = "scanning is failed because db operation is failed";
293 else if( error == MEDIA_CONTENT_ERROR_DB_BUSY)
295 err_msg = "scanning is failed because db operation is failed";
299 err_msg = "scanning is failed by unknown reason";
302 LogDebug("ContentListener is called:" + err_msg );
306 bool IMediacontentManager::setListener( void* user_data)
308 LogDebug("ContentManager::called setListener");
311 if(media_content_set_db_updated_cb(content_notification_cb,user_data) == MEDIA_CONTENT_ERROR_NONE)
319 bool IMediacontentManager::unsetListener()
321 LogDebug("ContentManager::called unsetListener");
324 if(media_content_unset_db_updated_cb() == MEDIA_CONTENT_ERROR_NONE)