[general] bugfixes, more debug output and error handling
[libxmms2client-qt.git] / medialib.cpp
blob19c4a44d2a876b3ff30850bc0abb5c5e3cd9c727
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 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")
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::recievedMediaInfoCallback(xmmsc_result_t * result, void * instance) {
151 if (xmmsc_result_iserror(result)) {
152 qDebug() << "Error: (QMedialib::recievedMediaInfoCallback)" << xmmsc_result_get_error(result);
153 return;
156 if (instance) {
157 QMedialib * medialib = static_cast<QMedialib *>(instance);
158 int intBuffer, id;
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)
195 return;
197 //disconnect
198 if (m_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;
207 //connect
208 if (m_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);
218 clearMediaInfo();
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)
227 delete i;
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))
237 return true;
238 else {
239 requestMediaInfo(id);
240 return false;
244 void QMedialib::requestMediaInfo(quint32 id) {
245 connectNotifierUnref(xmmsc_medialib_get_info(m_Connection, id), recievedMediaInfoCallback, this);