tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Content / PlaylistCopyUtil.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "PlaylistCopyUtil.h"
19 #include "PlaylistUtils.h"
20
21 #include <Logger.h>
22 #include <PlatformException.h>
23
24 #include "PlaylistUtils.h"
25 #include "Playlist.h"
26
27 namespace DeviceAPI {
28 namespace Content {
29
30 PlaylistCopyUtil::PlaylistCopyUtil() :
31             m_dest_playlist_handle(NULL),
32             m_copy_failed(false)
33 {
34 }
35
36 PlaylistCopyUtil::~PlaylistCopyUtil()
37 {
38     destroyMediaPlaylistHandle(m_dest_playlist_handle);
39 }
40
41 void PlaylistCopyUtil::copyItems(PlaylistPtr source_playlist,
42         PlaylistPtr dest_playlist)
43 {
44     LOGD("Entered");
45     std::unique_ptr<PlaylistCopyUtil> util(new PlaylistCopyUtil());
46     util->copyItemsPriv(source_playlist, dest_playlist);
47 }
48
49 bool PlaylistCopyUtil::receivedPlaylistMemberCB(int playlist_member_id, media_info_h media,
50         void *user_data)
51 {
52     PlaylistCopyUtil* util = static_cast<PlaylistCopyUtil*>(user_data);
53     if(!util) {
54         LOGE("user_data is NULL, nothing to do!");
55         return false;
56     }
57     return util->process(playlist_member_id, media);
58 }
59
60 bool PlaylistCopyUtil::process(int playlist_member_id, media_info_h media)
61 {
62     //LOGD("Entered member_id:%d", playlist_member_id);
63     char* media_id = NULL;
64     int ret_code = media_info_get_media_id(media, &media_id);
65     if(MEDIA_CONTENT_ERROR_NONE != ret_code) {
66         LOGE("%s", ContentUtility::getMediaContentLogMessage(
67                 ret_code, "media_info_get_media_id()").c_str());
68         m_copy_failed = true;
69         return false;   //Do not continue
70     }
71
72     ret_code = media_playlist_add_media(m_dest_playlist_handle, media_id);
73     LOGD("cloned item:%s", media_id);
74     free(media_id);
75
76     if(MEDIA_CONTENT_ERROR_NONE != ret_code) {
77         LOGE("%s", ContentUtility::getMediaContentLogMessage(
78                 ret_code, "media_playlist_add_media()").c_str());
79         m_copy_failed = true;
80         return false;   //Do not continue
81     }
82     return true;
83 }
84
85 void PlaylistCopyUtil::copyItemsPriv(PlaylistPtr source_playlist,
86         PlaylistPtr dest_playlist)
87 {
88     LOGD("Entered");
89
90     if(!source_playlist) {
91         LOGE("source_playlist is NULL");
92         throw UnknownException("Source playlist is invalid!");
93     }
94
95     if(!dest_playlist) {
96         LOGE("dest_playlist is NULL");
97         throw UnknownException("Destination playlist is invalid!");
98     }
99
100     m_src_playlist = source_playlist;
101     const int src_playlist_id = m_src_playlist->getId();
102
103     m_dest_playlist = dest_playlist;
104     m_dest_playlist_handle = dest_playlist->getPlaylistHandle();
105
106     int ret_code = media_playlist_foreach_media_from_db(src_playlist_id, NULL,
107             receivedPlaylistMemberCB, this);
108     if(MEDIA_CONTENT_ERROR_NONE != ret_code) {
109         LOGE("ret : %d", ret_code);
110         destroyMediaPlaylistHandle(m_dest_playlist_handle);
111         ContentUtility::throwMediaContentException(ret_code, "media_playlist_foreach_media_from_db()");
112     }
113
114     if(!m_copy_failed) {
115         LOGD("Updating DB with new playlist items");
116         ret_code = media_playlist_update_to_db(m_dest_playlist_handle);
117         if(MEDIA_CONTENT_ERROR_NONE != ret_code) {
118             LOGE("%s", ContentUtility::getMediaContentLogMessage(
119                     ret_code, "media_playlist_update_to_db()").c_str());
120             m_copy_failed = true;
121         }
122     }
123
124     destroyMediaPlaylistHandle(m_dest_playlist_handle);
125
126     if(m_copy_failed) {
127         LOGE("Copy operation failed!");
128         throw UnknownException("Playlist clone failed");
129     }
130
131     LOGD("Success");
132 }
133
134 } //namespace Content
135 } //namespace DeviceAPI