[Mediacontroller] Fixed issue with invalid name of enum
[platform/core/api/webapi-plugins.git] / src / mediacontroller / mediacontroller_utils.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "mediacontroller/mediacontroller_utils.h"
18
19 #include <media_controller_client.h>
20
21 #include "common/logger.h"
22 #include "common/platform_result.h"
23 #include "common/scope_exit.h"
24
25 namespace extension {
26 namespace mediacontroller {
27
28 using common::PlatformResult;
29 using common::ErrorCode;
30
31 namespace types {
32
33 const common::PlatformEnum<mc_server_state_e> MediaControllerServerStateEnum{
34     {"NONE", MC_SERVER_STATE_NONE},
35     {"ACTIVE", MC_SERVER_STATE_ACTIVATE},
36     {"INACTIVE", MC_SERVER_STATE_DEACTIVATE}};
37
38 const common::PlatformEnum<mc_playback_states_e> MediaControllerPlaybackStateEnum{
39     {"PLAY", MC_PLAYBACK_STATE_PLAYING},
40     {"PAUSE", MC_PLAYBACK_STATE_PAUSED},
41     {"STOP", MC_PLAYBACK_STATE_STOPPED},
42     {"NEXT", MC_PLAYBACK_STATE_MOVING_TO_NEXT},
43     {"PREV", MC_PLAYBACK_STATE_MOVING_TO_PREVIOUS},
44     {"FORWARD", MC_PLAYBACK_STATE_FAST_FORWARDING},
45     {"REWIND", MC_PLAYBACK_STATE_REWINDING}};
46
47 const common::PlatformEnum<mc_playback_action_e> MediaControllerPlaybackActionEnum{
48     {"PLAY", MC_PLAYBACK_ACTION_PLAY},    {"PAUSE", MC_PLAYBACK_ACTION_PAUSE},
49     {"STOP", MC_PLAYBACK_ACTION_STOP},    {"NEXT", MC_PLAYBACK_ACTION_NEXT},
50     {"PREV", MC_PLAYBACK_ACTION_PREV},    {"FORWARD", MC_PLAYBACK_ACTION_FAST_FORWARD},
51     {"REWIND", MC_PLAYBACK_ACTION_REWIND}};
52
53 const common::PlatformEnum<mc_meta_e> MediaControllerMetadataAttributeEnum{
54     {"title", MC_META_MEDIA_TITLE},
55     {"artist", MC_META_MEDIA_ARTIST},
56     {"album", MC_META_MEDIA_ALBUM},
57     {"author", MC_META_MEDIA_AUTHOR},
58     {"genre", MC_META_MEDIA_GENRE},
59     {"duration", MC_META_MEDIA_DURATION},
60     {"date", MC_META_MEDIA_DATE},
61     {"copyright", MC_META_MEDIA_COPYRIGHT},
62     {"description", MC_META_MEDIA_DESCRIPTION},
63     {"trackNum", MC_META_MEDIA_TRACK_NUM},
64     {"picture", MC_META_MEDIA_PICTURE}};
65
66 const common::PlatformEnum<mc_repeat_mode_e> MediaControllerRepeatModeEnum{
67     {"REPEAT_OFF", MC_REPEAT_MODE_OFF},
68     {"REPEAT_ONE", MC_REPEAT_MODE_ONE_MEDIA},
69     {"REPEAT_ALL", MC_REPEAT_MODE_ON}};
70
71 const common::PlatformEnum<mc_content_age_rating_e> MediaControllerContentAgeRatingEnum{
72     {"ALL", MC_CONTENT_RATING_ALL},    {"1", MC_CONTENT_RATING_1_PLUS},
73     {"2", MC_CONTENT_RATING_2_PLUS},   {"3", MC_CONTENT_RATING_3_PLUS},
74     {"4", MC_CONTENT_RATING_4_PLUS},   {"5", MC_CONTENT_RATING_5_PLUS},
75     {"6", MC_CONTENT_RATING_6_PLUS},   {"7", MC_CONTENT_RATING_7_PLUS},
76     {"8", MC_CONTENT_RATING_8_PLUS},   {"9", MC_CONTENT_RATING_9_PLUS},
77     {"10", MC_CONTENT_RATING_10_PLUS}, {"11", MC_CONTENT_RATING_11_PLUS},
78     {"12", MC_CONTENT_RATING_12_PLUS}, {"13", MC_CONTENT_RATING_13_PLUS},
79     {"14", MC_CONTENT_RATING_14_PLUS}, {"15", MC_CONTENT_RATING_15_PLUS},
80     {"16", MC_CONTENT_RATING_16_PLUS}, {"17", MC_CONTENT_RATING_17_PLUS},
81     {"18", MC_CONTENT_RATING_18_PLUS}, {"19", MC_CONTENT_RATING_19_PLUS}};
82
83 const common::PlatformEnum<mc_content_type_e> MediaControllerContentTypeEnum{
84     {"IMAGE", MC_CONTENT_TYPE_IMAGE},
85     {"MUSIC", MC_CONTENT_TYPE_MUSIC},
86     {"VIDEO", MC_CONTENT_TYPE_VIDEO},
87     {"OTHER", MC_CONTENT_TYPE_OTHER},
88     {"UNDECIDED", MC_CONTENT_TYPE_UNDECIDED}};
89
90 const common::PlatformEnum<mc_search_category_e> MediaControllerSearchCategoryEnum{
91     {"NO_CATEGORY", MC_SEARCH_NO_CATEGORY},
92     {"TITLE", MC_SEARCH_TITLE},
93     {"ARTIST", MC_SEARCH_ARTIST},
94     {"ALBUM", MC_SEARCH_ALBUM},
95     {"GENRE", MC_SEARCH_GENRE},
96     {"TPO", MC_SEARCH_TPO}};
97
98 PlatformResult ConvertPlaybackState(mc_playback_h playback_h, std::string* state) {
99   ScopeLogger();
100
101   int ret;
102   mc_playback_states_e state_e;
103   ret = mc_client_get_playback_state(playback_h, &state_e);
104   if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
105     return LogAndCreateResult(
106         ErrorCode::UNKNOWN_ERR, "Error getting playback state",
107         ("mc_client_get_playback_state() error: %d, message: %s", ret, get_error_message(ret)));
108   }
109   if (state_e == MC_PLAYBACK_STATE_NONE) {
110     state_e = MC_PLAYBACK_STATE_STOPPED;
111   }
112
113   PlatformResult result = MediaControllerPlaybackStateEnum.getName(state_e, state);
114   if (!result) {
115     LoggerE("MediaControllerPlaybackStateEnum.getName() failed, error: %s",
116             result.message().c_str());
117     return result;
118   }
119
120   return PlatformResult(ErrorCode::NO_ERROR);
121 }
122
123 PlatformResult ConvertContentAgeRating(mc_playback_h playback_h, std::string* rating) {
124   ScopeLogger();
125
126   mc_content_age_rating_e rating_e = MC_CONTENT_RATING_ALL;
127   int ret = mc_client_get_age_rating(playback_h, &rating_e);
128   if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
129     return LogAndCreateResult(
130         ErrorCode::UNKNOWN_ERR, "Error getting content age rating",
131         ("mc_client_get_age_rating() error: %d, message: %s", ret, get_error_message(ret)));
132   }
133
134   PlatformResult result = MediaControllerContentAgeRatingEnum.getName(rating_e, rating);
135   if (!result) {
136     LoggerE("MediaControllerContentAgeRatingEnum.getName() failed, error: %s",
137             result.message().c_str());
138     return result;
139   }
140
141   return PlatformResult(ErrorCode::NO_ERROR);
142 }
143
144 PlatformResult ConvertContentType(mc_playback_h playback_h, std::string* contentType) {
145   ScopeLogger();
146   mc_content_type_e content_type_e = MC_CONTENT_TYPE_UNDECIDED;
147   int ret = mc_client_get_playback_content_type(playback_h, &content_type_e);
148   if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
149     return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error getting content type",
150                               ("mc_client_get_playback_content_type() error: %d, message: %s", ret,
151                                get_error_message(ret)));
152   }
153
154   PlatformResult result = MediaControllerContentTypeEnum.getName(content_type_e, contentType);
155   if (!result) {
156     LoggerE("MediaControllerContentTypeEnum.getName() failed, error: %s", result.message().c_str());
157     return result;
158   }
159
160   return PlatformResult(ErrorCode::NO_ERROR);
161 }
162
163 PlatformResult ConvertPlaybackPosition(mc_playback_h playback_h, double* position) {
164   ScopeLogger();
165
166   int ret;
167
168   unsigned long long pos;
169   ret = mc_client_get_playback_position(playback_h, &pos);
170   if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
171     return LogAndCreateResult(
172         ErrorCode::UNKNOWN_ERR, "Error getting playback position",
173         ("mc_client_get_playback_position() error: %d, message: %s", ret, get_error_message(ret)));
174   }
175
176   *position = static_cast<double>(pos);
177
178   return PlatformResult(ErrorCode::NO_ERROR);
179 }
180
181 PlatformResult ConvertMetadata(mc_metadata_h metadata_h, picojson::object* metadata) {
182   ScopeLogger();
183
184   char* value = nullptr;
185   SCOPE_EXIT {
186     free(value);
187   };
188
189   for (auto entry : MediaControllerMetadataAttributeEnum) {
190     int ret = mc_metadata_get(metadata_h, entry.second, &value);
191     if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
192       return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Error getting metadata",
193                                 ("mc_metadata_get(%s) error: %d, message: %s", entry.first.c_str(),
194                                  ret, get_error_message(ret)));
195     }
196
197     (*metadata)[entry.first] = picojson::value(std::string(value ? value : ""));
198   }
199
200   return PlatformResult(ErrorCode::NO_ERROR);
201 }
202
203 }  // types
204
205 PlatformResult utils::GetAllPlaylists(const std::string& app_id, picojson::array* playlists) {
206   ScopeLogger();
207
208   auto OnPlaylists = [](mc_playlist_h playlist, void* user_data) -> bool {
209
210     char* name = nullptr;
211
212     SCOPE_EXIT {
213       free(name);
214     };
215
216     int ret = mc_playlist_get_name(playlist, &name);
217
218     if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
219       return false;
220     }
221
222     auto playlists = static_cast<picojson::array*>(user_data);
223
224     picojson::value value = picojson::value(picojson::object());
225     picojson::object& obj = value.get<picojson::object>();
226
227     obj.insert(std::make_pair("name", picojson::value{name}));
228     playlists->push_back(value);
229
230     return true;
231   };
232
233   int ret = mc_playlist_foreach_playlist(app_id.c_str(), OnPlaylists, playlists);
234
235   if (MEDIA_CONTROLLER_ERROR_NONE != ret) {
236     return LogAndCreateResult(
237         ErrorCode::UNKNOWN_ERR, "Error while get playlists",
238         ("mc_playlist_foreach_playlist() error: %d, message: %s", ret, get_error_message(ret)));
239   }
240
241   return PlatformResult(ErrorCode::NO_ERROR);
242 }
243
244 }  // namespace mediacontroller
245 }  // namespace extension