2 // C++ Implementation: medialib
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
6 // Copyright: See COPYING file that comes with this distribution
13 QMedialib::QMedialib(QClient
* parent
) : QObject(parent
), m_Connection(0), m_ActivePlaylist(0) {
14 connect(parent
, SIGNAL(connectionChanged(xmmsc_connection_t
*)),
15 this, SLOT(setConnection(xmmsc_connection_t
*)));
16 if (parent
->isConnectedToServer())
17 setConnection(parent
->connection());
22 QMedialib::~QMedialib() {
24 foreach(QPlaylist
* i
, m_Playlists
)
29 void QMedialib::playlistChangedCallback(xmmsc_result_t
* result
, void * instance
) {
30 if (xmmsc_result_iserror(result
)) {
31 qDebug() << "Error (QMedialib::playlistChangedCallback): " << xmmsc_result_get_error(result
);
36 QMedialib
* medialib
= static_cast<QMedialib
*>(instance
);
37 const char * strBuffer
;
38 xmmsc_result_get_dict_entry_string(result
, "name", &strBuffer
);
39 QString name
= strBuffer
;
40 QPlaylist::changedCallback(result
, medialib
->m_Playlists
[name
]);
44 void QMedialib::currentPosCallback(xmmsc_result_t
* result
, void * instance
) {
45 if (xmmsc_result_iserror(result
)) {
46 qDebug() << "Error (QMedialib::currentPosCallback): " << xmmsc_result_get_error(result
);
51 QMedialib
* medialib
= static_cast<QMedialib
*>(instance
);
53 QPlaylist::currentPosCallback(result
, medialib
->m_ActivePlaylist
);
55 //xmmsc_result_unref(result);
58 void QMedialib::activePlaylistCallback(xmmsc_result_t
* result
, void * instance
) {
59 if (xmmsc_result_iserror(result
)) {
60 qDebug() << "Error (QMedialib::activePlaylistCallback): " << xmmsc_result_get_error(result
);
65 QMedialib
* medialib
= static_cast<QMedialib
*>(instance
);
66 const char * strBuffer
;
67 xmmsc_result_get_string(result
, &strBuffer
);
68 medialib
->m_ActivePlaylist
= medialib
->m_Playlists
[QString(strBuffer
)];
70 emit medialib
->activePlaylistChanged();
72 //xmmsc_result_unref(result);
75 void QMedialib::fillPlaylists(xmmsc_result_t
* result
, void * instance
) {
76 if (xmmsc_result_iserror(result
)) {
77 qDebug() << "Error: (QMedialib::fillPlaylists)" << xmmsc_result_get_error(result
);
82 QMedialib
* medialib
= static_cast<QMedialib
*>(instance
);
84 const char * strBuffer
;
86 xmmsc_result_t
* i
= result
;
87 while (xmmsc_result_list_valid(i
)) {
88 xmmsc_result_get_string(i
, &strBuffer
);
89 if (strBuffer
[0] != '_')
90 medialib
->m_Playlists
.insert(QString(strBuffer
), new QPlaylist((QClient
*)(medialib
->parent()), QString(strBuffer
)));
91 xmmsc_result_list_next(i
);
93 emit medialib
->playlistsReseted();
97 void QMedialib::collectionChangedCallback(xmmsc_result_t
* result
, void * instance
) {
98 if (xmmsc_result_iserror(result
)) {
99 qDebug() << "Error: (QMedialib::collectionChangedCallback)" << xmmsc_result_get_error(result
);
104 QMedialib
* medialib
= static_cast<QMedialib
*>(instance
);
106 xmms_collection_changed_actions_t type
;
107 xmmsc_result_get_dict_entry_int(result
, "type", (qint32
*)(&type
));
109 const char * strBuffer
;
111 xmmsc_result_get_dict_entry_string(result
, "namespace", &strBuffer
);
112 QString collNamespace
= strBuffer
;
114 xmmsc_result_get_dict_entry_string(result
, "name", &strBuffer
);
115 QString collName
= strBuffer
;
117 //currently we only support playlists
118 if (QString(collNamespace
) != "Playlists")
122 case XMMS_COLLECTION_CHANGED_ADD
:
123 medialib
->m_Playlists
.insert(collName
, new QPlaylist((QClient
*)(medialib
->parent()), collName
));
124 emit medialib
->collectionAdded(collName
);
126 case XMMS_COLLECTION_CHANGED_RENAME
:
127 xmmsc_result_get_dict_entry_string(result
, "newname", &strBuffer
);
129 QString collNewName
= strBuffer
;
130 medialib
->m_Playlists
.insert(collNewName
, medialib
->m_Playlists
.take(collName
));
132 emit medialib
->collectionRenamed(collName
, collNewName
);
135 case XMMS_COLLECTION_CHANGED_UPDATE
:
136 emit medialib
->collectionUpdated(collName
);
138 case XMMS_COLLECTION_CHANGED_REMOVE
:
139 if (medialib
->m_Playlists
.contains(collName
)) {
140 delete medialib
->m_Playlists
.take(collName
);
141 emit medialib
->collectionRemoved(collName
);
150 void QMedialib::recievedMediaInfoCallback(xmmsc_result_t
* result
, void * instance
) {
151 if (xmmsc_result_iserror(result
)) {
152 qDebug() << "Error: (QMedialib::recievedMediaInfoCallback)" << xmmsc_result_get_error(result
);
157 QMedialib
* medialib
= static_cast<QMedialib
*>(instance
);
159 const char * strBuffer
;
161 xmmsc_result_get_dict_entry_int(result
, "id", &id
);
162 MediaInfo
* newMediaInfo
= new MediaInfo();
164 xmmsc_result_get_dict_entry_string(result
, "genre", &strBuffer
);
165 newMediaInfo
->genre
= strBuffer
;
166 xmmsc_result_get_dict_entry_string(result
, "album", &strBuffer
);
167 newMediaInfo
->album
= strBuffer
;
168 xmmsc_result_get_dict_entry_string(result
, "artist", &strBuffer
);
169 newMediaInfo
->artist
= strBuffer
;
170 xmmsc_result_get_dict_entry_string(result
, "title", &strBuffer
);
171 newMediaInfo
->title
= strBuffer
;
173 xmmsc_result_get_dict_entry_int(result
, "duration", &intBuffer
);
174 newMediaInfo
->duration
= intBuffer
;
175 xmmsc_result_get_dict_entry_int(result
, "channels", &intBuffer
);
176 newMediaInfo
->channels
= intBuffer
;
177 xmmsc_result_get_dict_entry_int(result
, "samplerate", &intBuffer
);
178 newMediaInfo
->sampleRate
= intBuffer
;
179 xmmsc_result_get_dict_entry_int(result
, "sample_format", &intBuffer
);
180 newMediaInfo
->sampleFormat
= intBuffer
;
182 qDebug() << "Inserting media info #" << id
<< " with "
183 << newMediaInfo
->artist
<< ", "
184 << newMediaInfo
->title
<< ", "
185 << newMediaInfo
->album
<< ", "
186 << newMediaInfo
->duration
;
187 medialib
->m_CachedMedia
.insert(id
, newMediaInfo
);
188 emit medialib
->mediaInfoReady(id
);
190 xmmsc_result_unref(result
);
193 void QMedialib::setConnection(xmmsc_connection_t
* connection
) {
194 if (m_Connection
== connection
)
199 disconnectNotifierUnref(xmmsc_broadcast_playlist_loaded(m_Connection
));
200 disconnectNotifierUnref(xmmsc_broadcast_playlist_changed(m_Connection
));
201 disconnectNotifierUnref(xmmsc_broadcast_playlist_current_pos(m_Connection
));
202 disconnectNotifierUnref(xmmsc_broadcast_collection_changed(m_Connection
));
205 m_Connection
= connection
;
209 connectNotifierUnref(xmmsc_broadcast_playlist_loaded(m_Connection
), activePlaylistCallback
, this);
210 connectNotifierUnref(xmmsc_broadcast_playlist_changed(m_Connection
), playlistChangedCallback
, this);
211 connectNotifierUnref(xmmsc_broadcast_playlist_current_pos(m_Connection
), currentPosCallback
, this);
212 connectNotifierUnref(xmmsc_broadcast_collection_changed(m_Connection
), collectionChangedCallback
, this);
214 connectNotifierUnref(xmmsc_playlist_list(m_Connection
), fillPlaylists
, this);
215 connectNotifierUnref(xmmsc_playlist_current_active(m_Connection
), activePlaylistCallback
, this);
221 void QMedialib::setActivePlaylist(QString name
) {
222 xmmsc_result_unref(xmmsc_playlist_load(m_Connection
, name
.toAscii().data()));
225 void QMedialib::clearMediaInfo() {
226 foreach (MediaInfo
* i
, m_CachedMedia
)
228 m_CachedMedia
.clear();
231 void QMedialib::removeMediaInfo(quint32 id
) {
232 delete m_CachedMedia
.take(id
);
235 bool QMedialib::checkMediaInfo(quint32 id
) {
236 if (m_CachedMedia
.contains(id
))
239 requestMediaInfo(id
);
244 void QMedialib::requestMediaInfo(quint32 id
) {
245 connectNotifierUnref(xmmsc_medialib_get_info(m_Connection
, id
), recievedMediaInfoCallback
, this);