[general] "poor programming style" fix
[libxmms2client-qt.git] / medialib.cpp
blob838981ac7be77a1b77d308b62906fb8fcc598901
1 //
2 // C++ Implementation: medialib
3 //
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
5 //
6 // Copyright: See COPYING file that comes with this distribution
7 //
8 #include "medialib.h"
9 #include "client.h"
10 #include "common.h"
11 #include <QDebug>
12 namespace XmmsQt {
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());
18 else
19 m_Connection = NULL;
22 QMedialib::~QMedialib() {
23 clearMediaInfo();
24 foreach(QPlaylist * i, m_Playlists)
25 delete i;
26 m_Playlists.clear();
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);
32 return;
35 if (instance) {
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);
47 return;
50 if (instance) {
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);
61 return;
64 if (instance) {
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);
78 return;
81 if (instance) {
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);
100 return;
103 if (instance) {
104 QMedialib * medialib = static_cast<QMedialib *>(instance);
106 qint32 type;
107 xmmsc_result_get_dict_entry_int(result, "type", &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")
119 return;
121 switch (type) {
122 case XMMS_COLLECTION_CHANGED_ADD:
123 medialib->m_Playlists.insert(collName, new QPlaylist((QClient *)(medialib->parent()), collName));
124 emit medialib->collectionAdded(collName);
125 break;
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);
134 break;
135 case XMMS_COLLECTION_CHANGED_UPDATE:
136 emit medialib->collectionUpdated(collName);
137 break;
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);
143 break;
144 default:
145 break;
150 void QMedialib::updatedMediaInfoCallback(xmmsc_result_t * result, void * instance) {
151 if (xmmsc_result_iserror(result)) {
152 qDebug() << "Error: (QMedialib::updatedMediaInfoCallback)" << xmmsc_result_get_error(result);
153 return;
156 if (instance) {
157 QMedialib * medialib = static_cast<QMedialib *>(instance);
158 quint32 id;
159 xmmsc_result_get_uint(result, &id);
160 qDebug() << "Media info updated for: #" << id;
161 if (medialib->checkMediaInfo(id)) {
162 medialib->removeMediaInfo(id);
163 medialib->requestMediaInfo(id);
168 void QMedialib::recievedMediaInfoCallback(xmmsc_result_t * result, void * instance) {
169 if (xmmsc_result_iserror(result)) {
170 qDebug() << "Error: (QMedialib::recievedMediaInfoCallback)" << xmmsc_result_get_error(result);
171 return;
174 if (instance) {
175 QMedialib * medialib = static_cast<QMedialib *>(instance);
176 int intBuffer, id;
177 const char * strBuffer;
179 xmmsc_result_get_dict_entry_int(result, "id", &id);
180 if (medialib->m_CachedMedia.contains(id)) {
181 xmmsc_result_unref(result);
182 return;
185 MediaInfo * newMediaInfo = new MediaInfo();
187 if (resultHasKey(result, "genre")) {
188 xmmsc_result_get_dict_entry_string(result, "genre", &strBuffer);
189 newMediaInfo->genre = strBuffer;
191 if (resultHasKey(result, "album")) {
192 xmmsc_result_get_dict_entry_string(result, "album", &strBuffer);
193 newMediaInfo->album = strBuffer;
195 if (resultHasKey(result, "artist")) {
196 xmmsc_result_get_dict_entry_string(result, "artist", &strBuffer);
197 newMediaInfo->artist = strBuffer;
199 if (resultHasKey(result, "title")) {
200 xmmsc_result_get_dict_entry_string(result, "title", &strBuffer);
201 newMediaInfo->title = strBuffer;
203 if (resultHasKey(result, "url")) {
204 xmmsc_result_get_dict_entry_string(result, "url", &strBuffer);
205 newMediaInfo->url = strBuffer;
208 if (resultHasKey(result, "duration")) {
209 if (xmmsc_result_get_dict_entry_int(result, "duration", &intBuffer))
210 newMediaInfo->duration = intBuffer;
211 else
212 newMediaInfo->duration = 0;
215 if (resultHasKey(result, "channels")) {
216 xmmsc_result_get_dict_entry_int(result, "channels", &intBuffer);
217 newMediaInfo->channels = intBuffer;
219 if (resultHasKey(result, "samplerate")) {
220 xmmsc_result_get_dict_entry_int(result, "samplerate", &intBuffer);
221 newMediaInfo->sampleRate = intBuffer;
223 if (resultHasKey(result, "sample_format")) {
224 xmmsc_result_get_dict_entry_int(result, "sample_format", &intBuffer);
225 newMediaInfo->sampleFormat = intBuffer;
228 qDebug() << "Inserting media info #" << id << " with "
229 << newMediaInfo->artist << ", "
230 << newMediaInfo->title << ", "
231 << newMediaInfo->album << ", "
232 << newMediaInfo->duration;
233 qDebug() << "-> url: " << newMediaInfo->url;
234 medialib->m_CachedMedia.insert(id, newMediaInfo);
235 emit medialib->mediaInfoReady(id);
237 xmmsc_result_unref(result);
240 void QMedialib::setConnection(xmmsc_connection_t * connection) {
241 if (m_Connection == connection)
242 return;
244 //disconnect
245 if (m_Connection) {
246 disconnectNotifierUnref(xmmsc_broadcast_playlist_loaded(m_Connection));
247 disconnectNotifierUnref(xmmsc_broadcast_playlist_changed(m_Connection));
248 disconnectNotifierUnref(xmmsc_broadcast_playlist_current_pos(m_Connection));
249 disconnectNotifierUnref(xmmsc_broadcast_collection_changed(m_Connection));
250 disconnectNotifierUnref(xmmsc_broadcast_medialib_entry_changed(m_Connection));
253 m_Connection = connection;
255 //connect
256 if (m_Connection) {
257 connectNotifierUnref(xmmsc_broadcast_playlist_loaded(m_Connection), activePlaylistCallback, this);
258 connectNotifierUnref(xmmsc_broadcast_playlist_changed(m_Connection), playlistChangedCallback, this);
259 connectNotifierUnref(xmmsc_broadcast_playlist_current_pos(m_Connection), currentPosCallback, this);
260 connectNotifierUnref(xmmsc_broadcast_collection_changed(m_Connection), collectionChangedCallback, this);
261 connectNotifierUnref(xmmsc_broadcast_medialib_entry_changed(m_Connection), updatedMediaInfoCallback, this);
263 connectNotifierUnref(xmmsc_playlist_list(m_Connection), fillPlaylists, this);
264 connectNotifierUnref(xmmsc_playlist_current_active(m_Connection), activePlaylistCallback, this);
267 clearMediaInfo();
270 void QMedialib::setActivePlaylist(QString name) {
271 xmmsc_result_unref(xmmsc_playlist_load(m_Connection, name.toAscii().data()));
274 void QMedialib::clearMediaInfo() {
275 foreach (MediaInfo * i, m_CachedMedia)
276 delete i;
277 m_CachedMedia.clear();
280 void QMedialib::removeMediaInfo(quint32 id) {
281 delete m_CachedMedia.take(id);
284 bool QMedialib::checkMediaInfo(quint32 id) {
285 if (m_CachedMedia.contains(id))
286 return true;
287 else {
288 requestMediaInfo(id);
289 return false;
293 void QMedialib::requestMediaInfo(quint32 id) {
294 connectNotifierUnref(xmmsc_medialib_get_info(m_Connection, id), recievedMediaInfoCallback, this);