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) {
626 filter_h filter = NULL;
627 ret = media_filter_create(&filter);
628 if (ret != MEDIA_CONTENT_ERROR_NONE) {
629 LoggerE("Failed: media_filter_create failed");
634 media_filter_destroy(filter);
637 std::string condition = "(FOLDER_STORAGE_TYPE = 0 OR FOLDER_STORAGE_TYPE = 1)";
638 media_filter_set_condition(filter, condition.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
640 picojson::array pico_dirs;
641 ret = media_folder_foreach_folder_from_db(filter, media_foreach_directory_cb, &pico_dirs);
642 if (ret != MEDIA_CONTENT_ERROR_NONE) {
643 PlatformResult err = LogAndCreateResult(
644 ErrorCode::UNKNOWN_ERR, "Getting the directories failed.",
645 ("Failed: Getting the directories failed %d (%s)", ret, get_error_message(ret)));
646 user_data->isSuccess = err;
650 user_data->result = picojson::value(pico_dirs);
653 void ContentManager::find(const std::shared_ptr<ReplyCallbackData>& user_data) {
660 picojson::value::array arrayContent;
661 filter_h filter = nullptr;
662 media_filter_create(&filter);
665 media_filter_destroy(filter);
669 if (!IsNull(user_data->args.get("filter"))) {
670 ContentFilter filterMechanism;
672 picojson::object argsObject = JsonCast<picojson::object>(user_data->args);
673 if (filterMechanism.BuildQuery(FromJson<picojson::object>(argsObject, "filter"), &query)) {
674 LoggerD("Filter query: %s", query.c_str());
675 ret = media_filter_set_condition(filter, query.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
676 if (MEDIA_CONTENT_ERROR_NONE != ret) {
677 LoggerE("Platform filter setting failed, error %d", ret);
682 if (user_data->args.contains("sortMode")) {
683 picojson::value vSortMode = user_data->args.get("sortMode");
685 if (vSortMode.is<picojson::object>()) {
686 std::string sortModeName, sortModeOrder;
688 ContentFilter::MapField(vSortMode.get("attributeName").to_str(), &sortModeName);
690 sortModeOrder = vSortMode.get("order").to_str();
691 if (!sortModeOrder.empty()) {
692 media_content_order_e order = MEDIA_CONTENT_ORDER_ASC;
694 if (sortModeOrder == "ASC") {
695 order = MEDIA_CONTENT_ORDER_ASC;
696 } else if (sortModeOrder == "DESC") {
697 order = MEDIA_CONTENT_ORDER_DESC;
700 ret = media_filter_set_order(filter, order, sortModeName.c_str(),
701 MEDIA_CONTENT_COLLATE_DEFAULT);
702 if (MEDIA_CONTENT_ERROR_NONE != ret) {
703 LoggerE("Platform SortMode setting failed, error: %d", ret);
709 if (!IsNull(user_data->args.get("count"))) {
710 count = static_cast<int>(user_data->args.get("count").get<double>());
714 if (!IsNull(user_data->args.get("offset"))) {
715 offset = static_cast<int>(user_data->args.get("offset").get<double>());
719 ret = media_filter_set_offset(filter, offset, count);
720 if (MEDIA_CONTENT_ERROR_NONE != ret) {
721 LoggerE("A platform error occurs in media_filter_set_offset: %d", ret);
723 if (!IsNull(user_data->args.get("directoryId"))) {
724 dirId = user_data->args.get("directoryId").get<std::string>();
725 ret = media_folder_foreach_media_from_db(dirId.c_str(), filter, media_foreach_content_cb,
726 static_cast<void*>(&arrayContent));
728 ret = media_info_foreach_media_from_db(filter, media_foreach_content_cb,
729 static_cast<void*>(&arrayContent));
732 if (ret == MEDIA_CONTENT_ERROR_NONE) {
733 user_data->result = picojson::value(arrayContent);
735 PlatformResult err = LogAndCreateResult(
736 ErrorCode::UNKNOWN_ERR, "The iteration failed in platform",
737 ("The iteration failed in platform: %d (%s)", ret, get_error_message(ret)));
738 user_data->isSuccess = err;
742 int ContentManager::scanFile(std::string& uri) {
744 return media_content_scan_file(uri.c_str());
747 PlatformResult ContentManager::scanDirectory(media_scan_completed_cb callback,
748 ReplyCallbackData* cbData) {
750 const std::string& contentDirURI = cbData->args.get("contentDirURI").get<std::string>();
751 std::string real_path = common::FilesystemProvider::Create().GetRealPath(contentDirURI);
752 const bool recursive = cbData->args.get("recursive").get<bool>();
754 int ret = media_content_scan_folder(real_path.c_str(), recursive, callback, (void*)cbData);
756 if (ret != MEDIA_CONTENT_ERROR_NONE) {
757 if (MEDIA_CONTENT_ERROR_INVALID_PARAMETER == ret) {
758 return LogAndCreateResult(
759 ErrorCode::INVALID_VALUES_ERR, "Scanning content directory failed",
760 ("Scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
763 return LogAndCreateResult(
764 ErrorCode::UNKNOWN_ERR, "Scanning content directory failed",
765 ("Scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
768 return PlatformResult(ErrorCode::NO_ERROR);
771 PlatformResult ContentManager::cancelScanDirectory(const std::string& content_dir_uri) {
774 int ret = media_content_cancel_scan_folder(content_dir_uri.c_str());
775 if (ret != MEDIA_CONTENT_ERROR_NONE) {
776 return LogAndCreateResult(
777 ErrorCode::UNKNOWN_ERR, "Cancel scan content directory failed",
778 ("Cancel scan folder failed in platform: %d (%s)", ret, get_error_message(ret)));
780 return PlatformResult(ErrorCode::NO_ERROR);
783 PlatformResult ContentManager::addChangeListener(media_content_noti_h* noti_handle,
784 media_content_db_update_cb callback,
788 int ret = media_content_add_db_updated_cb(callback, user_data, noti_handle);
790 if (MEDIA_CONTENT_ERROR_NONE != ret) {
791 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Failed to add the listener.",
792 ("Failed to add the listener: %d (%s)", ret, get_error_message(ret)));
795 return PlatformResult(ErrorCode::NO_ERROR);
798 PlatformResult ContentManager::removeChangeListener(media_content_noti_h noti_handle) {
801 int ret = media_content_remove_db_updated_cb(noti_handle);
804 case MEDIA_CONTENT_ERROR_NONE:
805 return PlatformResult(ErrorCode::NO_ERROR);
806 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
807 // Trying to remove non-existent listener, ignoring
808 LoggerI("Failed to remove the listener: %d (%s)", ret, get_error_message(ret));
809 return PlatformResult(ErrorCode::NO_ERROR);
811 return LogAndCreateResult(
812 ErrorCode::ABORT_ERR, "Failed to remove the listener.",
813 ("Failed to remove the listener: %d (%s)", ret, get_error_message(ret)));
817 void ContentManager::createPlaylist(std::string name,
818 const std::shared_ptr<ReplyCallbackData>& user_data) {
820 media_playlist_h playlist = NULL;
822 int ret = media_playlist_insert_to_db(name.c_str(), &playlist);
823 std::unique_ptr<std::remove_pointer<media_playlist_h>::type, int (*)(media_playlist_h)>
824 playlist_ptr(playlist, &media_playlist_destroy); // automatically release the memory
825 if (ret != MEDIA_CONTENT_ERROR_NONE) {
826 // MEDIA_CONTENT_ERROR_DB_FAILED means that playlist probably already exists
827 PlatformResult err = LogAndCreateResult(
828 MEDIA_CONTENT_ERROR_DB_FAILED == ret ? ErrorCode::INVALID_VALUES_ERR
829 : ErrorCode::UNKNOWN_ERR,
830 "Creation of playlist has failed.",
831 ("Failed: creation of playlist is failed: %d (%s)", ret, get_error_message(ret)));
832 user_data->isSuccess = err;
835 picojson::value::object o;
837 if (playlist != NULL) {
839 char* thumb_path = NULL;
840 char* name_playlist = NULL;
841 filter_h filter = NULL;
842 if (media_playlist_get_playlist_id(playlist, &id) == MEDIA_CONTENT_ERROR_NONE) {
843 o["id"] = picojson::value(std::to_string(id));
846 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "loading of playlist is failed.");
847 user_data->isSuccess = err;
850 if (media_playlist_get_thumbnail_path(playlist, &thumb_path) == MEDIA_CONTENT_ERROR_NONE) {
851 if (thumb_path != NULL) {
852 o["thumbnailURI"] = picojson::value(std::string(thumb_path));
855 o["thumbnailURI"] = picojson::value();
858 LoggerE("Invalid thumbnail path for playlist.");
860 if (media_playlist_get_name(playlist, &name_playlist) == MEDIA_CONTENT_ERROR_NONE) {
861 o["name"] = picojson::value(std::string(name_playlist));
864 LoggerE("Invalid name for playlist.");
866 media_filter_create(&filter);
867 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
868 filter, &media_filter_destroy); // automatically release the memory
870 if (media_playlist_get_media_count_from_db(id, filter, &cnt) == MEDIA_CONTENT_ERROR_NONE) {
871 o["numberOfTracks"] = picojson::value(static_cast<double>(cnt));
873 LoggerE("Invalid count for playlist.");
877 user_data->result = picojson::value(o);
880 void ContentManager::getPlaylists(const std::shared_ptr<ReplyCallbackData>& user_data) {
883 filter_h filter = nullptr;
884 media_filter_create(&filter);
885 std::unique_ptr<std::remove_pointer<filter_h>::type, int (*)(filter_h)> filter_ptr(
886 filter, &media_filter_destroy); // automatically release the memory
887 picojson::value::array playlists;
889 ret = media_playlist_foreach_playlist_from_db(filter, playlist_foreach_cb,
890 static_cast<void*>(&playlists));
892 if (ret != MEDIA_CONTENT_ERROR_NONE) {
893 PlatformResult err = LogAndCreateResult(
894 ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
895 ("Failed: Getting playlist is failed %d (%s)", ret, get_error_message(ret)));
896 user_data->isSuccess = err;
899 user_data->result = picojson::value(playlists);
902 void ContentManager::removePlaylist(std::string playlistId,
903 const std::shared_ptr<ReplyCallbackData>& user_data) {
905 int id = std::atoi(playlistId.c_str());
907 PlatformResult err = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "PlaylistId is wrong.");
908 user_data->isSuccess = err;
912 int ret = media_playlist_delete_from_db(id);
913 if (ret != MEDIA_CONTENT_ERROR_NONE) {
915 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Removal of playlist is failed.");
916 user_data->isSuccess = err;
920 int ContentManager::update(const picojson::value& args) {
922 const picojson::value content = args.get("content");
923 int ret = updateContent(content);
924 if (ret != MEDIA_CONTENT_ERROR_NONE) {
925 LoggerE("updateContent failed: %d", ret);
930 int ContentManager::updateBatch(const picojson::value& args) {
932 int ret = MEDIA_CONTENT_ERROR_NONE;
933 std::vector<picojson::value> contents = args.get("contents").get<picojson::array>();
935 for (picojson::value::array::iterator it = contents.begin(); it != contents.end(); ++it) {
936 const picojson::value content = *it;
937 ret = updateContent(content);
938 if (ret != MEDIA_CONTENT_ERROR_NONE) {
939 LoggerE("updateContent failed: %d", ret);
946 int ContentManager::playlistAdd(std::string playlist_id, std::string content_id) {
949 media_playlist_h playlist = NULL;
950 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
952 if (playlist != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
953 ret = media_playlist_add_media(playlist, content_id.c_str());
954 if (ret != MEDIA_CONTENT_ERROR_NONE) {
955 LoggerE("The content(id:%s) can't add to playlist", content_id.c_str());
958 ret = media_playlist_update_to_db(playlist);
959 if (ret != MEDIA_CONTENT_ERROR_NONE) {
960 LoggerE("The content(id:%s) can't add to playlist", content_id.c_str());
963 LoggerE("Playlist(id:%s) is not exist", playlist_id.c_str());
966 media_playlist_destroy(playlist);
970 int ContentManager::playlistRemove(std::string playlist_id, int member_id) {
973 media_playlist_h playlist = NULL;
974 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
975 if (playlist != NULL && ret == MEDIA_CONTENT_ERROR_NONE) {
976 ret = media_playlist_remove_media(playlist, member_id);
977 if (ret != MEDIA_CONTENT_ERROR_NONE) {
978 LoggerE("The content can't remove to playlist");
981 ret = media_playlist_update_to_db(playlist);
982 if (ret != MEDIA_CONTENT_ERROR_NONE) {
983 LoggerE("The content can't remove to playlist");
986 LoggerE("Playlist(id:%s) is not exist", playlist_id.c_str());
988 media_playlist_destroy(playlist);
993 void ContentManager::playlistAddbatch(const std::shared_ptr<ReplyCallbackData>& user_data) {
995 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
997 media_playlist_h playlist = NULL;
998 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1000 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1001 PlatformResult err =
1002 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1003 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1004 user_data->isSuccess = err;
1008 std::vector<picojson::value> contents = user_data->args.get("contents").get<picojson::array>();
1009 for (picojson::value::array::iterator it = contents.begin(); it != contents.end(); ++it) {
1010 picojson::value content = *it;
1011 std::string id = content.get("id").to_str();
1012 ret = media_playlist_add_media(playlist, id.c_str());
1013 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1014 LoggerE("Adding Content(id:%s) is failed.", id.c_str());
1018 ret = media_playlist_update_to_db(playlist);
1019 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1020 PlatformResult err =
1021 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Adding playlist is failed.",
1022 ("Adding playlist is failed: %d (%s)", ret, get_error_message(ret)));
1023 user_data->isSuccess = err;
1025 media_playlist_destroy(playlist);
1028 void ContentManager::playlistGet(const std::shared_ptr<ReplyCallbackData>& user_data) {
1030 media_playlist_h playlist = NULL;
1031 media_content_order_e order = MEDIA_CONTENT_ORDER_ASC;
1032 const std::string playOrder("playlist_member_order");
1036 media_playlist_destroy(playlist);
1040 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1041 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1042 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1043 PlatformResult err =
1044 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1045 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1046 user_data->isSuccess = err;
1050 filter_h filter = NULL;
1051 ret = media_filter_create(&filter);
1052 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1053 PlatformResult err =
1054 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Creating a filter is failed.",
1055 ("Creating a filter is failed: %d (%s)", ret, get_error_message(ret)));
1056 user_data->isSuccess = err;
1060 int count = user_data->args.get("count").get<double>();
1061 int offset = user_data->args.get("offset").get<double>();
1062 ret = media_filter_set_offset(filter, offset, count);
1063 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1064 LoggerD("Setting a offset/count is failed.");
1066 ret = media_filter_set_order(filter, order, playOrder.c_str(), MEDIA_CONTENT_COLLATE_DEFAULT);
1067 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1068 LoggerD("Setting a offset/count is failed.");
1071 picojson::value::array arrayContent;
1072 ret = media_playlist_foreach_media_from_db(std::stoi(playlist_id), filter,
1073 playlist_content_member_cb,
1074 static_cast<void*>(&arrayContent));
1076 media_filter_destroy(filter);
1077 if (ret == MEDIA_CONTENT_ERROR_NONE) {
1078 user_data->result = picojson::value(arrayContent);
1080 PlatformResult err =
1081 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Creating a filter is failed.",
1082 ("Creating a filter is failed: %d (%s)", ret, get_error_message(ret)));
1083 user_data->isSuccess = err;
1087 void ContentManager::playlistRemovebatch(const std::shared_ptr<ReplyCallbackData>& user_data) {
1089 media_playlist_h playlist = NULL;
1093 media_playlist_destroy(playlist);
1097 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1098 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1099 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1100 PlatformResult err =
1101 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1102 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1103 user_data->isSuccess = err;
1107 std::vector<picojson::value> members = user_data->args.get("members").get<picojson::array>();
1108 std::size_t members_size = members.size();
1109 for (std::size_t i = 0; i < members_size; ++i) {
1110 int member_id = static_cast<int>(members.at(i).get<double>());
1111 ret = media_playlist_remove_media(playlist, member_id);
1113 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1114 LoggerD("Removing a content is failed.");
1118 ret = media_playlist_update_to_db(playlist);
1119 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1120 PlatformResult err = LogAndCreateResult(
1121 ErrorCode::UNKNOWN_ERR, "Removing the contents is failed.",
1122 ("Removing the contents is failed: %d (%s)", ret, get_error_message(ret)));
1123 user_data->isSuccess = err;
1127 void ContentManager::playlistSetOrder(const std::shared_ptr<ReplyCallbackData>& user_data) {
1129 media_playlist_h playlist = NULL;
1133 media_playlist_destroy(playlist);
1137 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1138 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1139 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1140 PlatformResult err =
1141 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1142 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1143 user_data->isSuccess = err;
1148 std::vector<picojson::value> members = user_data->args.get("members").get<picojson::array>();
1150 ret = media_playlist_get_media_count_from_db(std::stoi(playlist_id), NULL, &cnt);
1151 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1152 LoggerE("Failed: media_playlist_get_media_count_from_db");
1153 PlatformResult err = convertError(ret);
1154 user_data->isSuccess = err;
1157 std::size_t members_size = members.size();
1158 if (cnt < 0 || static_cast<size_t>(cnt) != members_size) {
1159 PlatformResult err = LogAndCreateResult(
1160 ErrorCode::INVALID_VALUES_ERR,
1161 "The items array does not contain all items from the playlist.",
1162 ("Failed: The items array does not contain all items from the playlist: %d (%s)", ret,
1163 get_error_message(ret)));
1164 user_data->isSuccess = err;
1168 for (std::size_t i = 0; i < members_size; ++i) {
1169 int member_id = static_cast<int>(members.at(i).get<double>());
1170 ret = media_playlist_set_play_order(playlist, member_id, i);
1171 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1172 LoggerD("Removing a content is failed.");
1176 ret = media_playlist_update_to_db(playlist);
1177 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1178 PlatformResult err = LogAndCreateResult(
1179 ErrorCode::UNKNOWN_ERR, "Removing the contents is failed.",
1180 ("Removing the contents is failed: %d (%s)", ret, get_error_message(ret)));
1181 user_data->isSuccess = err;
1185 void ContentManager::playlistMove(const std::shared_ptr<ReplyCallbackData>& user_data) {
1187 media_playlist_h playlist = NULL;
1191 media_playlist_destroy(playlist);
1195 std::string playlist_id = user_data->args.get("playlistId").get<std::string>();
1196 int ret = media_playlist_get_playlist_from_db(std::stoi(playlist_id), &playlist);
1197 if (ret != MEDIA_CONTENT_ERROR_NONE && playlist == NULL) {
1198 PlatformResult err =
1199 LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Getting playlist is failed.",
1200 ("Getting playlist is failed: %d (%s)", ret, get_error_message(ret)));
1201 user_data->isSuccess = err;
1205 double member_id = user_data->args.get("memberId").get<double>();
1206 double delta = user_data->args.get("delta").get<double>();
1207 ret = media_playlist_get_play_order(playlist, static_cast<int>(member_id), &old_order);
1208 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1209 PlatformResult err = LogAndCreateResult(
1210 ErrorCode::UNKNOWN_ERR, "The content can't find form playlist.",
1211 ("The content can't find form playlist: %d (%s)", ret, get_error_message(ret)));
1212 user_data->isSuccess = err;
1215 int new_order = static_cast<int>(old_order) + static_cast<int>(delta);
1216 ret = media_playlist_set_play_order(playlist, static_cast<int>(member_id), new_order);
1217 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1218 PlatformResult err = LogAndCreateResult(
1219 ErrorCode::UNKNOWN_ERR, "The content can't update play_order.",
1220 ("The content can't update play_order: %d (%s)", ret, get_error_message(ret)));
1221 user_data->isSuccess = err;
1224 ret = media_playlist_update_to_db(playlist);
1225 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1226 PlatformResult err = LogAndCreateResult(
1227 ErrorCode::UNKNOWN_ERR, "Updateing play_order is failed.",
1228 ("Updateing play_order is failed: %d (%s)", ret, get_error_message(ret)));
1229 user_data->isSuccess = err;
1233 int ContentManager::getLyrics(const picojson::value& args, picojson::object& result) {
1236 int ret = METADATA_EXTRACTOR_ERROR_NONE;
1237 const std::string& contentURI = args.get("contentURI").to_str();
1238 if (contentURI.empty()) {
1239 LoggerE("contentURI empty - skipping media extractor");
1243 metadata_extractor_h extractor;
1244 ret = metadata_extractor_create(&extractor);
1245 if (METADATA_EXTRACTOR_ERROR_NONE != ret) {
1246 LoggerE("metadata_extractor_create failed, error: %d", ret);
1248 std::unique_ptr<std::remove_pointer<metadata_extractor_h>::type, int (*)(metadata_extractor_h)>
1249 extractor_ptr(extractor, &metadata_extractor_destroy); // automatically release the memory
1251 ret = metadata_extractor_set_path(extractor, contentURI.c_str());
1252 if (ret != METADATA_EXTRACTOR_ERROR_NONE) {
1253 LoggerE("metadata_extractor_set_path failed, error: %d", ret);
1256 picojson::array timestamps;
1257 picojson::array texts = picojson::array();
1258 char* strSyncTextNum = NULL;
1260 ret = metadata_extractor_get_metadata(extractor, METADATA_SYNCLYRICS_NUM, &strSyncTextNum);
1261 if (ret != METADATA_EXTRACTOR_ERROR_NONE) {
1262 LoggerE("Media extractor error %d", ret);
1266 int nSyncTextNum = 0;
1267 if (strSyncTextNum) {
1268 nSyncTextNum = atoi(strSyncTextNum);
1269 free(strSyncTextNum);
1270 strSyncTextNum = NULL;
1272 if (nSyncTextNum > 0) {
1273 result["type"] = picojson::value(std::string("SYNCHRONIZED"));
1274 for (int i = 0; i < nSyncTextNum; i++) {
1275 unsigned long time_info = 0;
1276 char* lyrics = NULL;
1277 ret = metadata_extractor_get_synclyrics(extractor, i, &time_info, &lyrics);
1278 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
1279 timestamps.push_back(picojson::value(static_cast<double>(time_info)));
1280 texts.push_back(picojson::value(std::string(lyrics)));
1284 result["texts"] = picojson::value(texts);
1285 result["timestamps"] = picojson::value(timestamps);
1286 ret = METADATA_EXTRACTOR_ERROR_NONE;
1288 char* unSyncText = nullptr;
1289 ret = metadata_extractor_get_metadata(extractor, METADATA_UNSYNCLYRICS, &unSyncText);
1290 if (ret == METADATA_EXTRACTOR_ERROR_NONE) {
1291 result["type"] = picojson::value(std::string("UNSYNCHRONIZED"));
1292 if (nullptr == unSyncText) {
1293 LoggerE("Unsynchronized lyrics text is NULL");
1295 texts.push_back(picojson::value(unSyncText ? unSyncText : ""));
1296 result["texts"] = picojson::value(texts);
1304 media_playlist_h getPlaylistHandle(int id) {
1306 media_playlist_h playlist_handle = nullptr;
1307 int ret_code = media_playlist_get_playlist_from_db(id, &playlist_handle);
1308 if (MEDIA_CONTENT_ERROR_NONE != ret_code || playlist_handle == nullptr) {
1309 LoggerE("could not get playlist handle for id: %d", id);
1313 return playlist_handle;
1316 void destroyMediaPlaylistHandle(media_playlist_h playlist_handle) {
1318 if (playlist_handle) {
1319 int ret_code = media_playlist_destroy(playlist_handle);
1320 playlist_handle = nullptr;
1322 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1323 LoggerE("media_playlist_destroy failed");
1328 int ContentManager::getPlaylistName(int id, std::string* result) {
1330 media_playlist_h playlist_handle = getPlaylistHandle(id);
1331 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1333 char* tmp_playlist_name = nullptr;
1334 const int ret_code = media_playlist_get_name(playlist_handle, &tmp_playlist_name);
1336 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1337 LoggerE("media_playlist_get_name failed");
1338 return TIZEN_ERROR_UNKNOWN;
1341 std::string playlist_name;
1342 if (tmp_playlist_name) {
1343 playlist_name = tmp_playlist_name;
1344 free(tmp_playlist_name);
1345 tmp_playlist_name = nullptr;
1348 *result = playlist_name;
1349 return MEDIA_CONTENT_ERROR_NONE;
1352 int updatePlaylistInDB(media_playlist_h playlist_handle) {
1354 int ret_code = media_playlist_update_to_db(playlist_handle);
1355 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1356 LoggerE("media_playlist_update_to_db failed");
1359 return MEDIA_CONTENT_ERROR_NONE;
1362 int ContentManager::setPlaylistName(int id, const std::string& name) {
1365 LoggerE("Cannot set empty playlist name!");
1366 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1369 media_playlist_h playlist_handle = getPlaylistHandle(id);
1370 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1372 const int ret_code = media_playlist_set_name(playlist_handle, name.c_str());
1373 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1374 LoggerE("media_playlist_set_name failed");
1375 // Setting name that is used by other playlist does not return bad error code here.
1376 // MEDIA_CONTENT_ERROR_INVALID_OPERATION is being returned in updatePlaylistInDB
1377 return TIZEN_ERROR_UNKNOWN;
1380 int ret = updatePlaylistInDB(playlist_handle);
1381 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1382 LoggerE("Error while updating playlist: %d", ret);
1383 if (MEDIA_CONTENT_ERROR_DB_FAILED == ret) {
1384 // We could fetch list of playlists and check if other playlist is using this
1385 // name, but that seems to be to much work in synchronous method
1386 LoggerE("Playlist name: %s is probably already used", name.c_str());
1387 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1391 return MEDIA_CONTENT_ERROR_NONE;
1394 int ContentManager::getThumbnailUri(int id, std::string* result) {
1396 media_playlist_h playlist_handle = getPlaylistHandle(id);
1397 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1399 char* tmp_playlist_thb_path = nullptr;
1400 const int ret_code = media_playlist_get_thumbnail_path(playlist_handle, &tmp_playlist_thb_path);
1402 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1403 LoggerE("media_playlist_get_name failed");
1404 return TIZEN_ERROR_UNKNOWN;
1407 std::string playlist_thb_path;
1408 if (tmp_playlist_thb_path) {
1409 playlist_thb_path = tmp_playlist_thb_path;
1410 free(tmp_playlist_thb_path);
1411 tmp_playlist_thb_path = nullptr;
1414 if (playlist_thb_path != " ") {
1415 playlist_thb_path = uri_prefix + playlist_thb_path;
1418 *result = playlist_thb_path;
1419 return MEDIA_CONTENT_ERROR_NONE;
1422 int ContentManager::setThumbnailUri(int id, const std::string& thb_uri) {
1425 // Allow setting empty URI, unfortunately Core API does not allow to set empty
1426 // path so we need to set one empty space. This is probably issue of Core API.
1427 if (!thb_uri.empty() && " " != thb_uri) {
1428 if (thb_uri.find(uri_absolute_prefix) != 0) {
1429 LoggerE("thumbnail URI is not valid: [%s]", thb_uri.c_str());
1430 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
1434 media_playlist_h playlist_handle = getPlaylistHandle(id);
1435 PlaylistUniquePtr playlist_ptr(playlist_handle, destroyMediaPlaylistHandle);
1437 std::string real_path = common::FilesystemProvider::Create().GetRealPath(thb_uri);
1438 const int ret_code = media_playlist_set_thumbnail_path(playlist_handle, real_path.c_str());
1439 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1440 LoggerE("media_playlist_set_thumbnail_path failed");
1441 return TIZEN_ERROR_UNKNOWN;
1444 int ret = updatePlaylistInDB(playlist_handle);
1448 int ContentManager::getNumberOfTracks(int id, int* result) {
1452 const int ret_code = media_playlist_get_media_count_from_db(id, nullptr, &count);
1454 if (MEDIA_CONTENT_ERROR_NONE != ret_code) {
1455 LoggerE("media_playlist_get_media_count_from_db failed");
1456 return TIZEN_ERROR_UNKNOWN;
1460 return MEDIA_CONTENT_ERROR_NONE;
1463 common::PlatformResult ContentManager::createThumbnail(const std::string& id,
1464 picojson::object* obj) {
1467 media_info_h media_h = nullptr;
1468 int ret = media_info_get_media_from_db(id.c_str(), &media_h);
1469 if (MEDIA_CONTENT_ERROR_NONE != ret || nullptr == media_h) {
1470 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Getting media failed.",
1471 ("Getting media failed: %d (%s)", ret, get_error_message(ret)));
1474 media_info_destroy(media_h);
1477 ret = media_info_generate_thumbnail(media_h);
1478 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1479 return LogAndCreateResult(ErrorCode::ABORT_ERR, "Creating thumbnail failed.",
1480 ("Creating thumbnail failed: %d (%s)", ret, get_error_message(ret)));
1483 char* path = nullptr;
1484 ret = media_info_get_thumbnail_path(media_h, &path);
1485 if (MEDIA_CONTENT_ERROR_NONE != ret) {
1486 return LogAndCreateResult(
1487 ErrorCode::ABORT_ERR, "Creating thumbnail succeeded, but failed to get thumbnail path.",
1488 ("Getting thumbnail path failed: %d (%s)", ret, get_error_message(ret)));
1490 obj->emplace("result", picojson::value(path));
1491 std::unique_ptr<char[], decltype(&free)>(path, free);
1492 return PlatformResult(ErrorCode::NO_ERROR);
1495 PlatformResult ContentManager::convertError(int err) {
1496 char* error_msg = get_error_message(err);
1498 case MEDIA_CONTENT_ERROR_INVALID_PARAMETER:
1499 return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, error_msg);
1500 case MEDIA_CONTENT_ERROR_OUT_OF_MEMORY:
1501 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1502 case MEDIA_CONTENT_ERROR_INVALID_OPERATION:
1503 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1504 case MEDIA_CONTENT_FILE_NO_SPACE_ON_DEVICE:
1505 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1506 case MEDIA_CONTENT_ERROR_PERMISSION_DENIED:
1507 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1508 case MEDIA_CONTENT_ERROR_DB_FAILED:
1509 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1510 case MEDIA_CONTENT_ERROR_DB_BUSY:
1511 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1512 case MEDIA_CONTENT_ERROR_NETWORK:
1513 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1514 case MEDIA_CONTENT_ERROR_UNSUPPORTED_CONTENT:
1515 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, error_msg);
1517 return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown error.");
1521 } // namespace content
1522 } // namespace extension