2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "content/content_manager.h"
19 #include <media_content_internal.h>
20 #include <metadata_extractor.h>
30 #include "common/converter.h"
31 #include "common/filesystem/filesystem_provider.h"
32 #include "common/logger.h"
33 #include "common/scope_exit.h"
34 #include "common/tools.h"
36 #include "content/content_filter.h"
39 using namespace common;
41 using common::tools::ReportSuccess;
42 using common::tools::ReportError;
48 static const std::string uri_prefix = "file://";
49 static const std::string uri_absolute_prefix = "file:///";
52 std::string get_date(char* tmpStr) {
55 struct tm* result = (struct tm*)calloc(1, sizeof(struct tm));
56 if (nullptr != result) {
57 if (strptime(tmpStr, "%Y:%m:%d %H:%M:%S", result) == NULL) {
61 time_t t = mktime(result); // + get_utc_offset() * 3600;
62 std::stringstream str_date;
65 return str_date.str();
72 void ContentToJson(media_info_h info, picojson::object& o) {
80 long long unsigned int tmpLong;
81 media_content_type_e type;
83 ret = media_info_get_media_type(info, &type);
85 if (ret != MEDIA_CONTENT_ERROR_NONE) {
86 LoggerE("Get media type failed: %d", ret);
87 type = MEDIA_CONTENT_TYPE_OTHERS;
90 if (type == MEDIA_CONTENT_TYPE_IMAGE) {
91 o["type"] = picojson::value(std::string("IMAGE"));
93 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_image(info, &img)) {
94 std::unique_ptr<std::remove_pointer<image_meta_h>::type, int (*)(image_meta_h)> img_ptr(
95 img, &image_meta_destroy); // automatically release the memory
96 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_date_taken(img, &tmpStr)) {
98 o["releaseDate"] = picojson::value(get_date(tmpStr));
103 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_width(img, &tmpInt)) {
104 o["width"] = picojson::value(static_cast<double>(tmpInt));
106 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_height(img, &tmpInt)) {
107 o["height"] = picojson::value(static_cast<double>(tmpInt));
109 picojson::object geo;
110 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble)) {
111 geo["latitude"] = picojson::value(tmpDouble);
113 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble)) {
114 geo["longitude"] = picojson::value(tmpDouble);
116 o["geolocation"] = picojson::value(geo);
118 media_content_orientation_e orientation;
119 if (MEDIA_CONTENT_ERROR_NONE == image_meta_get_orientation(img, &orientation)) {
120 switch (orientation) {
121 case MEDIA_CONTENT_ORIENTATION_NOT_AVAILABLE:
122 case MEDIA_CONTENT_ORIENTATION_NORMAL:
125 case MEDIA_CONTENT_ORIENTATION_HFLIP:
126 ori = "FLIP_HORIZONTAL";
128 case MEDIA_CONTENT_ORIENTATION_ROT_180:
131 case MEDIA_CONTENT_ORIENTATION_VFLIP:
132 ori = "FLIP_VERTICAL";
134 case MEDIA_CONTENT_ORIENTATION_TRANSPOSE:
137 case MEDIA_CONTENT_ORIENTATION_ROT_90:
140 case MEDIA_CONTENT_ORIENTATION_TRANSVERSE:
143 case MEDIA_CONTENT_ORIENTATION_ROT_270:
147 o["orientation"] = picojson::value(ori);
150 } else if (type == MEDIA_CONTENT_TYPE_VIDEO) {
151 o["type"] = picojson::value(std::string("VIDEO"));
153 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_video(info, &video)) {
154 std::unique_ptr<std::remove_pointer<video_meta_h>::type, int (*)(video_meta_h)> video_ptr(
155 video, &video_meta_destroy); // automatically release the memory
156 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_width(video, &tmpInt)) {
157 o["width"] = picojson::value(static_cast<double>(tmpInt));
160 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_height(video, &tmpInt)) {
161 o["height"] = picojson::value(static_cast<double>(tmpInt));
163 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_artist(video, &tmpStr)) {
164 picojson::array artists;
166 artists.push_back(picojson::value(std::string(tmpStr)));
170 o["artists"] = picojson::value(artists);
172 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_album(video, &tmpStr)) {
174 o["album"] = picojson::value(tmpStr);
179 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_duration(video, &tmpInt)) {
180 o["duration"] = picojson::value(static_cast<double>(tmpInt));
182 if (MEDIA_CONTENT_ERROR_NONE == video_meta_get_recorded_date(video, &tmpStr)) {
184 o["releaseDate"] = picojson::value(get_date(tmpStr));
190 picojson::object geo;
191 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_latitude(info, &tmpDouble)) {
192 geo["latitude"] = picojson::value(tmpDouble);
194 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_longitude(info, &tmpDouble)) {
195 geo["longitude"] = picojson::value(tmpDouble);
197 o["geolocation"] = picojson::value(geo);
198 } else if (type == MEDIA_CONTENT_TYPE_SOUND || type == MEDIA_CONTENT_TYPE_MUSIC) {
199 o["type"] = picojson::value(std::string("AUDIO"));
201 if (MEDIA_CONTENT_ERROR_NONE == media_info_get_audio(info, &audio)) {
202 std::unique_ptr<std::remove_pointer<audio_meta_h>::type, int (*)(audio_meta_h)> audio_ptr(
203 audio, &audio_meta_destroy); // automatically release the memory
204 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_recorded_date(audio, &tmpStr)) {
206 o["releaseDate"] = picojson::value(get_date(tmpStr));
211 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_album(audio, &tmpStr)) {
213 o["album"] = picojson::value(std::string(tmpStr));
218 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_artist(audio, &tmpStr)) {
220 picojson::array artists;
221 artists.push_back(picojson::value(std::string(tmpStr)));
222 o["artists"] = picojson::value(artists);
227 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_genre(audio, &tmpStr)) {
229 picojson::array genres;
230 genres.push_back(picojson::value(std::string(tmpStr)));
231 o["genres"] = picojson::value(genres);
236 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_composer(audio, &tmpStr)) {
238 picojson::array composers;
239 composers.push_back(picojson::value(std::string(tmpStr)));
240 o["composers"] = picojson::value(composers);
245 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_copyright(audio, &tmpStr)) {
247 o["copyright"] = picojson::value(std::string(tmpStr));
252 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_bit_rate(audio, &tmpInt)) {
253 o["bitrate"] = picojson::value(static_cast<double>(tmpInt));
255 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_track_num(audio, &tmpStr)) {
257 o["trackNumber"] = picojson::value(static_cast<double>(std::atoi(tmpStr)));
261 o["trackNumber"] = picojson::value();
264 if (MEDIA_CONTENT_ERROR_NONE == audio_meta_get_duration(audio, &tmpInt)) {
265 o["duration"] = picojson::value(static_cast<double>(tmpInt));
269 o["type"] = picojson::value(std::string("OTHER"));
272 ret = media_info_get_media_id(info, &tmpStr);
273 if (ret == MEDIA_CONTENT_ERROR_NONE) {
275 o["id"] = picojson::value(std::string(tmpStr));
280 ret = media_info_get_display_name(info, &tmpStr);
281 if (ret == MEDIA_CONTENT_ERROR_NONE) {
283 o["name"] = picojson::value(std::string(tmpStr));
289 ret = media_info_get_mime_type(info, &tmpStr);
290 if (ret == MEDIA_CONTENT_ERROR_NONE) {
292 o["mimeType"] = picojson::value(std::string(tmpStr));
297 ret = media_info_get_title(info, &tmpStr);
298 if (ret == MEDIA_CONTENT_ERROR_NONE) {
300 o["title"] = picojson::value(std::string(tmpStr));
305 ret = media_info_get_file_path(info, &tmpStr);
306 if (ret == MEDIA_CONTENT_ERROR_NONE) {
308 o["contentURI"] = picojson::value(std::string(tmpStr));
313 ret = media_info_get_thumbnail_path(info, &tmpStr);
314 if (ret == MEDIA_CONTENT_ERROR_NONE) {
316 picojson::array thumbnails;
317 thumbnails.push_back(picojson::value(std::string(tmpStr)));
318 o["thumbnailURIs"] = picojson::value(thumbnails);
323 ret = media_info_get_description(info, &tmpStr);
324 if (ret == MEDIA_CONTENT_ERROR_NONE) {
326 o["description"] = picojson::value(std::string(tmpStr));
331 ret = media_info_get_rating(info, &tmpInt);
332 if (ret == MEDIA_CONTENT_ERROR_NONE) {
333 o["rating"] = picojson::value(static_cast<double>(tmpInt));
335 ret = media_info_get_size(info, &tmpLong);
336 if (ret == MEDIA_CONTENT_ERROR_NONE) {
337 o["size"] = picojson::value(static_cast<double>(tmpLong));
339 ret = media_info_get_favorite(info, &tmpBool);
340 if (ret == MEDIA_CONTENT_ERROR_NONE) {
341 o["isFavorite"] = picojson::value(tmpBool);
343 ret = media_info_get_modified_time(info, &tmpDate);
344 if (ret == MEDIA_CONTENT_ERROR_NONE) {
345 o["modifiedDate"] = picojson::value(static_cast<double>(tmpDate));
349 void ContentDirToJson(media_folder_h folder, picojson::object& o) {
355 ret = media_folder_get_folder_id(folder, &tmpStr);
356 if (ret == MEDIA_CONTENT_ERROR_NONE) {
358 o["id"] = picojson::value(std::string(tmpStr));
365 std::string folder_path;
366 ret = media_folder_get_path(folder, &tmpStr);
367 if (ret == MEDIA_CONTENT_ERROR_NONE) {
369 o["directoryURI"] = picojson::value(std::string(tmpStr));
370 // folder_path value kept for modifiedDate property gathering
371 folder_path = tmpStr;
378 ret = media_folder_get_name(folder, &tmpStr);
379 if (ret == MEDIA_CONTENT_ERROR_NONE) {
381 o["title"] = picojson::value(std::string(tmpStr));
388 struct stat stat_res;
389 if (stat(folder_path.c_str(), &stat_res) == 0) {
390 auto mod_time = stat_res.st_mtime;
391 o["modifiedDate"] = picojson::value(static_cast<double>(mod_time));
395 static int setContent(media_info_h media, const picojson::value& content) {
398 int ret = MEDIA_CONTENT_ERROR_NONE;
399 bool is_fav = content.get("isFavorite").get<bool>();
402 LoggerE("MEDIA_CONTENT_ERROR_DB_FAILED");
403 return MEDIA_CONTENT_ERROR_DB_FAILED;
406 media_content_type_e type;
407 ret = media_info_get_media_type(media, &type);
408 if (ret != MEDIA_CONTENT_ERROR_NONE) {
409 LoggerE("Failed: media_info_get_media_type()");
413 ret = media_info_set_favorite(media, is_fav);
414 if (ret != MEDIA_CONTENT_ERROR_NONE) {
415 LoggerE("Updating isFavorite failed.");
418 if (ret != MEDIA_CONTENT_ERROR_NONE) {
419 LoggerE("Updating favorite failed.");
422 return MEDIA_CONTENT_ERROR_NONE;
425 static int updateContent(const picojson::value& content) {
428 int ret = MEDIA_CONTENT_ERROR_NONE;
429 std::string id = content.get("id").to_str();
430 media_info_h media = nullptr;
433 media_info_destroy(media);
437 ret = media_info_get_media_from_db(id.c_str(), &media);
438 if (ret != MEDIA_CONTENT_ERROR_NONE) {
439 LoggerE("media_info_get_media_from_db failed: %d", ret);
442 ret = setContent(media, content);
443 if (ret != MEDIA_CONTENT_ERROR_NONE) {
444 LoggerE("setContent failed: %d", ret);
447 ret = media_info_update_to_db(media);
448 if (ret != MEDIA_CONTENT_ERROR_NONE) {
449 LoggerE("media_info_update_to_db failed: %d", ret);
456 static void FolderToJson(media_folder_h folder, picojson::object* out) {
466 ret = media_folder_get_folder_id(folder, &id);
467 if (ret != MEDIA_CONTENT_ERROR_NONE) {
468 LogAndReportError(ContentManager::convertError(ret), out,
469 ("Failed: media_folder_get_folder_id"));
472 std::unique_ptr<char, decltype(&free)> id_ptr(id, &free);
474 ret = media_folder_get_name(folder, &name);
475 if (ret != MEDIA_CONTENT_ERROR_NONE) {
476 LogAndReportError(ContentManager::convertError(ret), out, ("Failed: media_folder_get_name"));
479 std::unique_ptr<char, decltype(&free)> name_ptr(name, &free);
481 ret = media_folder_get_path(folder, &path);
482 if (ret != MEDIA_CONTENT_ERROR_NONE) {
483 LogAndReportError(ContentManager::convertError(ret), out, ("Failed: media_folder_get_path"));
486 std::unique_ptr<char, decltype(&free)> path_ptr(path, &free);
488 struct stat stat_res;
489 ret = stat(path, &stat_res);
491 LogAndReportError(ContentManager::convertError(errno), out, ("Failed: stat"));
494 date = stat_res.st_mtime;
496 (*out)["id"] = picojson::value(std::string(id));
497 (*out)["directoryURI"] = picojson::value(std::string(path));
498 (*out)["title"] = picojson::value(std::string(name));
500 (*out)["modifiedDate"] = picojson::value(static_cast<double>(date));
503 static bool media_foreach_directory_cb(media_folder_h folder, void* user_data) {
505 picojson::array* array = static_cast<picojson::array*>(user_data);
506 picojson::object json;
507 FolderToJson(folder, &json);
508 array->push_back(picojson::value(json));
512 static bool media_foreach_content_cb(media_info_h media, void* user_data) {
514 picojson::value::array* contents = static_cast<picojson::value::array*>(user_data);
515 picojson::value::object o;
516 ContentToJson(media, o);
517 contents->push_back(picojson::value(o));
521 static bool playlist_foreach_cb(media_playlist_h playlist, void* user_data) {
523 picojson::value::array* playlists = static_cast<picojson::value::array*>(user_data);
524 picojson::value::object o;
525 if (playlist != NULL) {
527 char* thumb_path = NULL;
529 filter_h filter = NULL;
530 if (media_playlist_get_playlist_id(playlist, &id) == MEDIA_CONTENT_ERROR_NONE) {
531 std::stringstream str_id;
533 o["id"] = picojson::value(std::to_string(id));
535 LoggerD("Invalid ID for playlist.");
537 if (media_playlist_get_thumbnail_path(playlist, &thumb_path) == MEDIA_CONTENT_ERROR_NONE) {
538 if (thumb_path != NULL) {
539 std::string thumbnail_uri(thumb_path);
540 if (thumbnail_uri != " ") {
541 thumbnail_uri = uri_prefix + thumbnail_uri;
543 o["thumbnailURI"] = picojson::value(thumbnail_uri);
546 o["thumbnailURI"] = picojson::value(); // picojson::value(std::string(""));
549 LoggerD("Invalid thumbnail path for playlist.");
551 if (media_playlist_get_name(playlist, &name) == MEDIA_CONTENT_ERROR_NONE) {
552 o["name"] = picojson::value(std::string(name));
555 LoggerD("Invalid name for playlist.");
558 media_filter_create(&filter);
559 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
560 filter, &media_filter_destroy); // automatically release the memory
561 if (media_playlist_get_media_count_from_db(id, filter, &cnt) == MEDIA_CONTENT_ERROR_NONE) {
562 o["numberOfTracks"] = picojson::value(static_cast<double>(cnt));
564 LoggerE("Invalid count for playlist.");
566 playlists->push_back(picojson::value(o));
571 static bool playlist_content_member_cb(int playlist_member_id, media_info_h media,
574 picojson::value::array* contents = static_cast<picojson::value::array*>(user_data);
575 picojson::value::object o;
577 o["playlist_member_id"] = picojson::value(static_cast<double>(playlist_member_id));
578 ContentToJson(media, o);
579 contents->push_back(picojson::value(o));
583 ContentManager::ContentManager() {
584 ScopeLogger("ContentManager called");
585 if (media_content_connect() == MEDIA_CONTENT_ERROR_NONE) {
586 m_dbConnected = true;
588 m_dbConnected = false;
590 m_contentInstance = nullptr;
593 ContentManager::~ContentManager() {
596 if (media_content_disconnect() == MEDIA_CONTENT_ERROR_NONE) {
597 m_dbConnected = false;
602 ContentManager* ContentManager::getInstance() {
604 static ContentManager instance;
608 ContentInstance* ContentManager::getContentInstance() {
610 return m_contentInstance;
613 void ContentManager::setContentInstance(ContentInstance* const content_instance) {
615 m_contentInstance = content_instance;
618 bool ContentManager::isConnected() {
620 return m_dbConnected;
623 void ContentManager::getDirectories(const std::shared_ptr<ReplyCallbackData>& user_data) {
627 picojson::array pico_dirs;
628 ret = media_folder_foreach_folder_from_db(nullptr, media_foreach_directory_cb,
630 if (ret != MEDIA_CONTENT_ERROR_NONE) {
631 PlatformResult err = LogAndCreateResult(
632 ErrorCode::UNKNOWN_ERR, "Getting the directories failed.",
633 ("Failed: Getting the directories failed %d (%s)", ret, get_error_message(ret)));
634 user_data->isSuccess = err;
638 user_data->result = picojson::value(pico_dirs);
641 void ContentManager::find(const std::shared_ptr<ReplyCallbackData>& user_data) {
648 picojson::value::array arrayContent;
649 filter_h filter = nullptr;
650 media_filter_create(&filter);
653 media_filter_destroy(filter);
657 if (!IsNull(user_data->args.get("filter"))) {
658 ContentFilter filterMechanism;
660 picojson::object argsObject = JsonCast<picojson::object>(user_data->args);
661 if (filterMechanism.BuildQuery(FromJson<picojson::object>(argsObject, "filter"), &query)) {
662 LoggerD("Filter query: %s", query.c_str());
663 ret = media_filter_set_condition(filter, query.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
664 if (MEDIA_CONTENT_ERROR_NONE != ret) {
665 LoggerE("Platform filter setting failed, error %d", ret);
670 if (user_data->args.contains("sortMode")) {
671 picojson::value vSortMode = user_data->args.get("sortMode");
673 if (vSortMode.is<picojson::object>()) {
674 std::string sortModeName, sortModeOrder;
676 ContentFilter::MapField(vSortMode.get("attributeName").to_str(), &sortModeName);
678 sortModeOrder = vSortMode.get("order").to_str();
679 if (!sortModeOrder.empty()) {
680 media_content_order_e order = MEDIA_CONTENT_ORDER_ASC;
682 if (sortModeOrder == "ASC") {
683 order = MEDIA_CONTENT_ORDER_ASC;
684 } else if (sortModeOrder == "DESC") {
685 order = MEDIA_CONTENT_ORDER_DESC;
688 ret = media_filter_set_order(filter, order, sortModeName.c_str(),
689 MEDIA_CONTENT_COLLATE_DEFAULT);
690 if (MEDIA_CONTENT_ERROR_NONE != ret) {
691 LoggerE("Platform SortMode setting failed, error: %d", ret);
697 if (!IsNull(user_data->args.get("count"))) {
698 count = static_cast<int>(user_data->args.get("count").get<double>());
702 if (!IsNull(user_data->args.get("offset"))) {
703 offset = static_cast<int>(user_data->args.get("offset").get<double>());
707 ret = media_filter_set_offset(filter, offset, count);
708 if (MEDIA_CONTENT_ERROR_NONE != ret) {
709 LoggerE("A platform error occurs in media_filter_set_offset: %d", ret);
711 if (!IsNull(user_data->args.get("directoryId"))) {
712 dirId = user_data->args.get("directoryId").get<std::string>();
713 ret = media_folder_foreach_media_from_db(dirId.c_str(), filter, media_foreach_content_cb,
714 static_cast<void*>(&arrayContent));
716 ret = media_info_foreach_media_from_db(filter, media_foreach_content_cb,
717 static_cast<void*>(&arrayContent));
720 if (ret == MEDIA_CONTENT_ERROR_NONE) {
721 user_data->result = picojson::value(arrayContent);
723 PlatformResult err = LogAndCreateResult(
724 ErrorCode::UNKNOWN_ERR, "The iteration failed in platform",
725 ("The iteration failed in platform: %d (%s)", ret, get_error_message(ret)));
726 user_data->isSuccess = err;
730 int ContentManager::scanFile(std::string& uri) {
732 return media_content_scan_file(uri.c_str());
735 PlatformResult ContentManager::scanDirectory(media_scan_completed_cb callback,
736 ReplyCallbackData* cbData) {
738 const std::string& contentDirURI = cbData->args.get("contentDirURI").get<std::string>();
739 std::string real_path = common::FilesystemProvider::Create().GetRealPath(contentDirURI);
740 const bool recursive = cbData->args.get("recursive").get<bool>();
742 int ret = media_content_scan_folder(real_path.c_str(), recursive, callback, (void*)cbData);
744 if (ret != MEDIA_CONTENT_ERROR_NONE) {
745 if (MEDIA_CONTENT_ERROR_INVALID_PARAMETER == ret) {
746 return LogAndCreateResult(
747 ErrorCode::INVALID_VALUES_ERR, "Scanning content directory failed",
748 ("Scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
751 return LogAndCreateResult(
752 ErrorCode::UNKNOWN_ERR, "Scanning content directory failed",
753 ("Scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
756 return PlatformResult(ErrorCode::NO_ERROR);
759 PlatformResult ContentManager::cancelScanDirectory(const std::string& content_dir_uri) {
762 int ret = media_content_cancel_scan_folder(content_dir_uri.c_str());
763 if (ret != MEDIA_CONTENT_ERROR_NONE) {
764 return LogAndCreateResult(
765 ErrorCode::UNKNOWN_ERR, "Cancel scan content directory failed",
766 ("Cancel scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
768 return PlatformResult(ErrorCode::NO_ERROR);
771 PlatformResult ContentManager::addChangeListener(media_content_noti_h* noti_handle,
772 media_content_db_update_cb callback,
776 int ret = media_content_add_db_updated_cb(callback, user_data, noti_handle);
778 if (MEDIA_CONTENT_ERROR_NONE != ret) {
779 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to add the listener.",
780 ("Failed to add the listener: %d (%s)", ret, get_error_message(ret)));
783 return PlatformResult(ErrorCode::NO_ERROR);
786 PlatformResult ContentManager::removeChangeListener(media_content_noti_h noti_handle) {
789 int ret = media_content_remove_db_updated_cb(noti_handle);
792 case MEDIA_CONTENT_ERROR_NONE:
793 return PlatformResult(ErrorCode::NO_ERROR);
794 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
795 // Trying to remove non-existent listener, ignoring
796 LoggerI("Failed to remove the listener: %d (%s)", ret, get_error_message(ret));
797 return PlatformResult(ErrorCode::NO_ERROR);
799 return LogAndCreateResult(
800 ErrorCode::ABORT_ERR, "Failed to remove the listener.",
801 ("Failed to remove the listener: %d (%s)", ret, get_error_message(ret)));
805 void ContentManager::createPlaylist(std::string name,
806 const std::shared_ptr<ReplyCallbackData>& user_data) {
808 media_playlist_h playlist = NULL;
810 int ret = media_playlist_insert_to_db(name.c_str(), &playlist);
811 std::unique_ptr<std::remove_pointer<media_playlist_h>::type, int (*)(media_playlist_h)>
812 playlist_ptr(playlist, &media_playlist_destroy); // automatically release the memory
813 if (ret != MEDIA_CONTENT_ERROR_NONE) {
814 // MEDIA_CONTENT_ERROR_DB_FAILED means that playlist probably already exists
815 PlatformResult err = LogAndCreateResult(
816 MEDIA_CONTENT_ERROR_DB_FAILED == ret ? ErrorCode::INVALID_VALUES_ERR
817 : ErrorCode::UNKNOWN_ERR,
818 "Creation of playlist has failed.",
819 ("Failed: creation of playlist is failed: %d (%s)", ret, get_error_message(ret)));
820 user_data->isSuccess = err;
823 picojson::value::object o;
825 if (playlist != NULL) {
827 char* thumb_path = NULL;
828 char* name_playlist = NULL;
829 filter_h filter = NULL;
830 if (media_playlist_get_playlist_id(playlist, &id) == MEDIA_CONTENT_ERROR_NONE) {
831 o["id"] = picojson::value(std::to_string(id));
834 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "loading of playlist is failed.");
835 user_data->isSuccess = err;
838 if (media_playlist_get_thumbnail_path(playlist, &thumb_path) == MEDIA_CONTENT_ERROR_NONE) {
839 if (thumb_path != NULL) {
840 o["thumbnailURI"] = picojson::value(std::string(thumb_path));
843 o["thumbnailURI"] = picojson::value();
846 LoggerE("Invalid thumbnail path for playlist.");
848 if (media_playlist_get_name(playlist, &name_playlist) == MEDIA_CONTENT_ERROR_NONE) {
849 o["name"] = picojson::value(std::string(name_playlist));
852 LoggerE("Invalid name for playlist.");
854 media_filter_create(&filter);
855 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
856 filter, &media_filter_destroy); // automatically release the memory
858 if (media_playlist_get_media_count_from_db(id, filter, &cnt) == MEDIA_CONTENT_ERROR_NONE) {
859 o["numberOfTracks"] = picojson::value(static_cast<double>(cnt));
861 LoggerE("Invalid count for playlist.");
865 user_data->result = picojson::value(o);
868 void ContentManager::getPlaylists(const std::shared_ptr<ReplyCallbackData>& user_data) {
871 filter_h filter = nullptr;
872 media_filter_create(&filter);
873 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
874 filter, &media_filter_destroy); // automatically release the memory
875 picojson::value::array playlists;
877 ret = media_playlist_foreach_playlist_from_db(filter, playlist_foreach_cb,
878 static_cast<void*>(&playlists));
880 if (ret != MEDIA_CONTENT_ERROR_NONE) {
881 PlatformResult err = LogAndCreateResult(
882 ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
883 ("Failed: Getting playlist is failed %d (%s)", ret, get_error_message(ret)));
884 user_data->isSuccess = err;
887 user_data->result = picojson::value(playlists);
890 void ContentManager::removePlaylist(std::string playlistId,
891 const std::shared_ptr<ReplyCallbackData>& user_data) {
893 int id = std::atoi(playlistId.c_str());
895 PlatformResult err = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "PlaylistId is wrong.");
896 user_data->isSuccess = err;
900 int ret = media_playlist_delete_from_db(id);
901 if (ret != MEDIA_CONTENT_ERROR_NONE) {
903 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Removal of playlist is failed.");
904 user_data->isSuccess = err;
908 int ContentManager::update(const picojson::value& args) {
910 const picojson::value content = args.get("content");
911 int ret = updateContent(content);
912 if (ret != MEDIA_CONTENT_ERROR_NONE) {
913 LoggerE("updateContent failed: %d", ret);
918 int ContentManager::updateBatch(const picojson::value& args) {
920 int ret = MEDIA_CONTENT_ERROR_NONE;
921 std::vector<picojson::value> contents = args.get("contents").get<picojson::array>();
923 for (picojson::value::array::iterator it = contents.begin(); it != contents.end(); ++it) {
924 const picojson::value content = *it;
925 ret = updateContent(content);
926 if (ret != MEDIA_CONTENT_ERROR_NONE) {
927 LoggerE("updateContent failed: %d", ret);
934 int ContentManager::playlistAdd(std::string playlist_id, std::string content_id) {
937 media_playlist_h playlist = NULL;
938 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
940 if (playlist != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
941 ret = media_playlist_add_media(playlist, content_id.c_str());
942 if (ret != MEDIA_CONTENT_ERROR_NONE) {
943 LoggerE("The content(id:%s) can't add to playlist", content_id.c_str());
946 ret = media_playlist_update_to_db(playlist);
947 if (ret != MEDIA_CONTENT_ERROR_NONE) {
948 LoggerE("The content(id:%s) can't add to playlist", content_id.c_str());
951 LoggerE("Playlist(id:%s) is not exist", playlist_id.c_str());
954 media_playlist_destroy(playlist);
958 int ContentManager::playlistRemove(std::string playlist_id, int member_id) {
961 media_playlist_h playlist = NULL;
962 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
963 if (playlist != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
964 ret = media_playlist_remove_media(playlist, member_id);
965 if (ret != MEDIA_CONTENT_ERROR_NONE) {
966 LoggerE("The content can't remove to playlist");
969 ret = media_playlist_update_to_db(playlist);
970 if (ret != MEDIA_CONTENT_ERROR_NONE) {
971 LoggerE("The content can't remove to playlist");
974 LoggerE("Playlist(id:%s) is not exist", playlist_id.c_str());
976 media_playlist_destroy(playlist);
981 void ContentManager::playlistAddbatch(const std::shared_ptr<ReplyCallbackData>& user_data) {
983 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
985 media_playlist_h playlist = NULL;
986 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
988 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
990 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
991 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
992 user_data->isSuccess = err;
996 std::vector<picojson::value> contents = user_data->args.get("contents").get<picojson::array>();
997 for (picojson::value::array::iterator it = contents.begin(); it != contents.end(); ++it) {
998 picojson::value content = *it;
999 std::string id = content.get("id").to_str();
1000 ret = media_playlist_add_media(playlist, id.c_str());
1001 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1002 LoggerE("Adding Content(id:%s) is failed.", id.c_str());
1006 ret = media_playlist_update_to_db(playlist);
1007 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1008 PlatformResult err =
1009 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Adding playlist is failed.",
1010 ("Adding playlist is failed: %d (%s)", ret, get_error_message(ret)));
1011 user_data->isSuccess = err;
1013 media_playlist_destroy(playlist);
1016 void ContentManager::playlistGet(const std::shared_ptr<ReplyCallbackData>& user_data) {
1018 media_playlist_h playlist = NULL;
1019 media_content_order_e order = MEDIA_CONTENT_ORDER_ASC;
1020 const std::string playOrder("playlist_member_order");
1024 media_playlist_destroy(playlist);
1028 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1029 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1030 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1031 PlatformResult err =
1032 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1033 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1034 user_data->isSuccess = err;
1038 filter_h filter = NULL;
1039 ret = media_filter_create(&filter);
1040 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1041 PlatformResult err =
1042 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Creating a filter is failed.",
1043 ("Creating a filter is failed: %d (%s)", ret, get_error_message(ret)));
1044 user_data->isSuccess = err;
1048 int count = user_data->args.get("count").get<double>();
1049 int offset = user_data->args.get("offset").get<double>();
1050 ret = media_filter_set_offset(filter, offset, count);
1051 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1052 LoggerD("Setting a offset/count is failed.");
1054 ret = media_filter_set_order(filter, order, playOrder.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
1055 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1056 LoggerD("Setting a offset/count is failed.");
1059 picojson::value::array arrayContent;
1060 ret = media_playlist_foreach_media_from_db(std::stoi(playlist_id), filter,
1061 playlist_content_member_cb,
1062 static_cast<void*>(&arrayContent));
1064 media_filter_destroy(filter);
1065 if (ret == MEDIA_CONTENT_ERROR_NONE) {
1066 user_data->result = picojson::value(arrayContent);
1068 PlatformResult err =
1069 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Creating a filter is failed.",
1070 ("Creating a filter is failed: %d (%s)", ret, get_error_message(ret)));
1071 user_data->isSuccess = err;
1075 void ContentManager::playlistRemovebatch(const std::shared_ptr<ReplyCallbackData>& user_data) {
1077 media_playlist_h playlist = NULL;
1081 media_playlist_destroy(playlist);
1085 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1086 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1087 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1088 PlatformResult err =
1089 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1090 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1091 user_data->isSuccess = err;
1095 std::vector<picojson::value> members = user_data->args.get("members").get<picojson::array>();
1096 std::size_t members_size = members.size();
1097 for (std::size_t i = 0; i < members_size; ++i) {
1098 int member_id = static_cast<int>(members.at(i).get<double>());
1099 ret = media_playlist_remove_media(playlist, member_id);
1101 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1102 LoggerD("Removing a content is failed.");
1106 ret = media_playlist_update_to_db(playlist);
1107 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1108 PlatformResult err = LogAndCreateResult(
1109 ErrorCode::UNKNOWN_ERR, "Removing the contents is failed.",
1110 ("Removing the contents is failed: %d (%s)", ret, get_error_message(ret)));
1111 user_data->isSuccess = err;
1115 void ContentManager::playlistSetOrder(const std::shared_ptr<ReplyCallbackData>& user_data) {
1117 media_playlist_h playlist = NULL;
1121 media_playlist_destroy(playlist);
1125 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1126 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1127 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1128 PlatformResult err =
1129 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1130 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1131 user_data->isSuccess = err;
1136 std::vector<picojson::value> members = user_data->args.get("members").get<picojson::array>();
1138 ret = media_playlist_get_media_count_from_db(std::stoi(playlist_id), NULL, &cnt);
1139 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1140 LoggerE("Failed: media_playlist_get_media_count_from_db");
1141 PlatformResult err = convertError(ret);
1142 user_data->isSuccess = err;
1145 std::size_t members_size = members.size();
1146 if (cnt < 0 || static_cast<size_t>(cnt) != members_size) {
1147 PlatformResult err = LogAndCreateResult(
1148 ErrorCode::INVALID_VALUES_ERR,
1149 "The items array does not contain all items from the playlist.",
1150 ("Failed: The items array does not contain all items from the playlist: %d (%s)", ret,
1151 get_error_message(ret)));
1152 user_data->isSuccess = err;
1156 for (std::size_t i = 0; i < members_size; ++i) {
1157 int member_id = static_cast<int>(members.at(i).get<double>());
1158 ret = media_playlist_set_play_order(playlist, member_id, i);
1159 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1160 LoggerD("Removing a content is failed.");
1164 ret = media_playlist_update_to_db(playlist);
1165 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1166 PlatformResult err = LogAndCreateResult(
1167 ErrorCode::UNKNOWN_ERR, "Removing the contents is failed.",
1168 ("Removing the contents is failed: %d (%s)", ret, get_error_message(ret)));
1169 user_data->isSuccess = err;
1173 void ContentManager::playlistMove(const std::shared_ptr<ReplyCallbackData>& user_data) {
1175 media_playlist_h playlist = NULL;
1179 media_playlist_destroy(playlist);
1183 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1184 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1185 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1186 PlatformResult err =
1187 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1188 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1189 user_data->isSuccess = err;
1193 double member_id = user_data->args.get("memberId").get<double>();
1194 double delta = user_data->args.get("delta").get<double>();
1195 ret = media_playlist_get_play_order(playlist, static_cast<int>(member_id), &old_order);
1196 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1197 PlatformResult err = LogAndCreateResult(
1198 ErrorCode::UNKNOWN_ERR, "The content can't find form playlist.",
1199 ("The content can't find form playlist: %d (%s)", ret, get_error_message(ret)));
1200 user_data->isSuccess = err;
1203 int new_order = static_cast<int>(old_order) + static_cast<int>(delta);
1204 ret = media_playlist_set_play_order(playlist, static_cast<int>(member_id), new_order);
1205 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1206 PlatformResult err = LogAndCreateResult(
1207 ErrorCode::UNKNOWN_ERR, "The content can't update play_order.",
1208 ("The content can't update play_order: %d (%s)", ret, get_error_message(ret)));
1209 user_data->isSuccess = err;
1212 ret = media_playlist_update_to_db(playlist);
1213 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1214 PlatformResult err = LogAndCreateResult(
1215 ErrorCode::UNKNOWN_ERR, "Updateing play_order is failed.",
1216 ("Updateing play_order is failed: %d (%s)", ret, get_error_message(ret)));
1217 user_data->isSuccess = err;
1221 int ContentManager::getLyrics(const picojson::value& args, picojson::object& result) {
1224 int ret = METADATA_EXTRACTOR_ERROR_NONE;
1225 const std::string& contentURI = args.get("contentURI").to_str();
1226 if (contentURI.empty()) {
1227 LoggerE("contentURI empty - skipping media extractor");
1231 metadata_extractor_h extractor;
1232 ret = metadata_extractor_create(&extractor);
1233 if (METADATA_EXTRACTOR_ERROR_NONE != ret) {
1234 LoggerE("metadata_extractor_create failed, error: %d", ret);
1236 std::unique_ptr<std::remove_pointer<metadata_extractor_h>::type, int (*)(metadata_extractor_h)>
1237 extractor_ptr(extractor, &metadata_extractor_destroy); // automatically release the memory
1239 ret = metadata_extractor_set_path(extractor, contentURI.c_str());
1240 if (ret != METADATA_EXTRACTOR_ERROR_NONE) {
1241 LoggerE("metadata_extractor_set_path failed, error: %d", ret);
1244 picojson::array timestamps;
1245 picojson::array texts = picojson::array();
1246 char* strSyncTextNum = NULL;
1248 ret = metadata_extractor_get_metadata(extractor, METADATA_SYNCLYRICS_NUM, &strSyncTextNum);
1249 if (ret != METADATA_EXTRACTOR_ERROR_NONE) {
1250 LoggerE("Media extractor error %d", ret);
1254 int nSyncTextNum = 0;
1255 if (strSyncTextNum) {
1256 nSyncTextNum = atoi(strSyncTextNum);
1257 free(strSyncTextNum);
1258 strSyncTextNum = NULL;
1260 if (nSyncTextNum > 0) {
1261 result["type"] = picojson::value(std::string("SYNCHRONIZED"));
1262 for (int i = 0; i < nSyncTextNum; i++) {
1263 unsigned long time_info = 0;
1264 char* lyrics = NULL;
1265 ret = metadata_extractor_get_synclyrics(extractor, i, &time_info, &lyrics);
1266 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
1267 timestamps.push_back(picojson::value(static_cast<double>(time_info)));
1268 texts.push_back(picojson::value(std::string(lyrics)));
1272 result["texts"] = picojson::value(texts);
1273 result["timestamps"] = picojson::value(timestamps);
1274 ret = METADATA_EXTRACTOR_ERROR_NONE;
1276 char* unSyncText = nullptr;
1277 ret = metadata_extractor_get_metadata(extractor, METADATA_UNSYNCLYRICS, &unSyncText);
1278 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
1279 result["type"] = picojson::value(std::string("UNSYNCHRONIZED"));
1280 if (nullptr == unSyncText) {
1281 LoggerE("Unsynchronized lyrics text is NULL");
1283 texts.push_back(picojson::value(unSyncText ? unSyncText : ""));
1284 result["texts"] = picojson::value(texts);
1292 media_playlist_h getPlaylistHandle(int id) {
1294 media_playlist_h playlist_handle = nullptr;
1295 int ret_code = media_playlist_get_playlist_from_db(id, &playlist_handle);
1296 if (MEDIA_CONTENT_ERROR_NONE != ret_code || playlist_handle == nullptr) {
1297 LoggerE("could not get playlist handle for id: %d", id);
1301 return playlist_handle;
1304 void destroyMediaPlaylistHandle(media_playlist_h playlist_handle) {
1306 if (playlist_handle) {
1307 int ret_code = media_playlist_destroy(playlist_handle);
1308 playlist_handle = nullptr;
1310 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1311 LoggerE("media_playlist_destroy failed");
1316 int ContentManager::getPlaylistName(int id, std::string* result) {
1318 media_playlist_h playlist_handle = getPlaylistHandle(id);
1319 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1321 char* tmp_playlist_name = nullptr;
1322 const int ret_code = media_playlist_get_name(playlist_handle, &tmp_playlist_name);
1324 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1325 LoggerE("media_playlist_get_name failed");
1326 return TIZEN_ERROR_UNKNOWN;
1329 std::string playlist_name;
1330 if (tmp_playlist_name) {
1331 playlist_name = tmp_playlist_name;
1332 free(tmp_playlist_name);
1333 tmp_playlist_name = nullptr;
1336 *result = playlist_name;
1337 return MEDIA_CONTENT_ERROR_NONE;
1340 int updatePlaylistInDB(media_playlist_h playlist_handle) {
1342 int ret_code = media_playlist_update_to_db(playlist_handle);
1343 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1344 LoggerE("media_playlist_update_to_db failed");
1347 return MEDIA_CONTENT_ERROR_NONE;
1350 int ContentManager::setPlaylistName(int id, const std::string& name) {
1353 LoggerE("Cannot set empty playlist name!");
1354 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1357 media_playlist_h playlist_handle = getPlaylistHandle(id);
1358 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1360 const int ret_code = media_playlist_set_name(playlist_handle, name.c_str());
1361 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1362 LoggerE("media_playlist_set_name failed");
1363 // Setting name that is used by other playlist does not return bad error code here.
1364 // MEDIA_CONTENT_ERROR_INVALID_OPERATION is being returned in updatePlaylistInDB
1365 return TIZEN_ERROR_UNKNOWN;
1368 int ret = updatePlaylistInDB(playlist_handle);
1369 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1370 LoggerE("Error while updating playlist: %d", ret);
1371 if (MEDIA_CONTENT_ERROR_DB_FAILED == ret) {
1372 // We could fetch list of playlists and check if other playlist is using this
1373 // name, but that seems to be to much work in synchronous method
1374 LoggerE("Playlist name: %s is probably already used", name.c_str());
1375 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1379 return MEDIA_CONTENT_ERROR_NONE;
1382 int ContentManager::getThumbnailUri(int id, std::string* result) {
1384 media_playlist_h playlist_handle = getPlaylistHandle(id);
1385 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1387 char* tmp_playlist_thb_path = nullptr;
1388 const int ret_code = media_playlist_get_thumbnail_path(playlist_handle, &tmp_playlist_thb_path);
1390 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1391 LoggerE("media_playlist_get_name failed");
1392 return TIZEN_ERROR_UNKNOWN;
1395 std::string playlist_thb_path;
1396 if (tmp_playlist_thb_path) {
1397 playlist_thb_path = tmp_playlist_thb_path;
1398 free(tmp_playlist_thb_path);
1399 tmp_playlist_thb_path = nullptr;
1402 if (playlist_thb_path != " ") {
1403 playlist_thb_path = uri_prefix + playlist_thb_path;
1406 *result = playlist_thb_path;
1407 return MEDIA_CONTENT_ERROR_NONE;
1410 int ContentManager::setThumbnailUri(int id, const std::string& thb_uri) {
1413 // Allow setting empty URI, unfortunately Core API does not allow to set empty
1414 // path so we need to set one empty space. This is probably issue of Core API.
1415 if (!thb_uri.empty() && " " != thb_uri) {
1416 if (thb_uri.find(uri_absolute_prefix) != 0) {
1417 LoggerE("thumbnail URI is not valid: [%s]", thb_uri.c_str());
1418 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1422 media_playlist_h playlist_handle = getPlaylistHandle(id);
1423 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1425 std::string real_path = common::FilesystemProvider::Create().GetRealPath(thb_uri);
1426 const int ret_code = media_playlist_set_thumbnail_path(playlist_handle, real_path.c_str());
1427 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1428 LoggerE("media_playlist_set_thumbnail_path failed");
1429 return TIZEN_ERROR_UNKNOWN;
1432 int ret = updatePlaylistInDB(playlist_handle);
1436 int ContentManager::getNumberOfTracks(int id, int* result) {
1440 const int ret_code = media_playlist_get_media_count_from_db(id, nullptr, &count);
1442 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1443 LoggerE("media_playlist_get_media_count_from_db failed");
1444 return TIZEN_ERROR_UNKNOWN;
1448 return MEDIA_CONTENT_ERROR_NONE;
1451 common::PlatformResult ContentManager::createThumbnail(const std::string& id,
1452 picojson::object* obj) {
1455 media_info_h media_h = nullptr;
1456 int ret = media_info_get_media_from_db(id.c_str(), &media_h);
1457 if (MEDIA_CONTENT_ERROR_NONE != ret || nullptr == media_h) {
1458 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Getting media failed.",
1459 ("Getting media failed: %d (%s)", ret, get_error_message(ret)));
1462 media_info_destroy(media_h);
1465 ret = media_info_generate_thumbnail(media_h);
1466 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1467 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Creating thumbnail failed.",
1468 ("Creating thumbnail failed: %d (%s)", ret, get_error_message(ret)));
1471 char* path = nullptr;
1472 ret = media_info_get_thumbnail_path(media_h, &path);
1473 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1474 return LogAndCreateResult(
1475 ErrorCode::ABORT_ERR, "Creating thumbnail succeeded, but failed to get thumbnail path.",
1476 ("Getting thumbnail path failed: %d (%s)", ret, get_error_message(ret)));
1478 obj->emplace("result", picojson::value(path));
1479 std::unique_ptr<char[], decltype(&free)>(path, free);
1480 return PlatformResult(ErrorCode::NO_ERROR);
1483 PlatformResult ContentManager::convertError(int err) {
1484 char* error_msg = get_error_message(err);
1486 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
1487 return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, error_msg);
1488 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
1489 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1490 case MEDIA_CONTENT_ERROR_INVALID_OPERATION:
1491 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1492 case MEDIA_CONTENT_FILE_NO_SPACE_ON_DEVICE:
1493 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1494 case MEDIA_CONTENT_ERROR_PERMISSION_DENIED:
1495 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1496 case MEDIA_CONTENT_ERROR_DB_FAILED:
1497 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1498 case MEDIA_CONTENT_ERROR_DB_BUSY:
1499 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1500 case MEDIA_CONTENT_ERROR_NETWORK:
1501 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1502 case MEDIA_CONTENT_ERROR_UNSUPPORTED_CONTENT:
1503 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1505 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown error.");
1509 } // namespace content
1510 } // namespace extension