[general] "poor programming style" fix
[libxmms2client-qt.git] / playlist.cpp
blob51176e2a05d4d428ff89b930be421899becb3001
1 //
2 // C++ Implementation: playlist
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 "common.h"
9 #include "playlist.h"
10 #include "client.h"
11 #include <QDir>
12 #include <QDebug>
13 namespace XmmsQt {
14 QPlaylist::QPlaylist(QClient * parent, QString identifier) : QObject(parent), m_Identifier(identifier) {
15 connect(parent, SIGNAL(connectionChanged(xmmsc_connection_t *)),
16 this, SLOT(setConnection(xmmsc_connection_t *)));
17 if (parent->isConnectedToServer())
18 setConnection(parent->connection());
19 else
20 m_Connection = NULL;
23 QPlaylist::~QPlaylist() {
26 void QPlaylist::changedCallback(xmmsc_result_t * result, void * instance) {
27 if (xmmsc_result_iserror(result)) {
28 qDebug() << "Error (QPlaylist::changedCallback): " << xmmsc_result_get_error(result);
29 return;
32 if (instance) {
33 QPlaylist * playlist = static_cast<QPlaylist *>(instance);
35 qint32 type;
36 xmmsc_result_get_dict_entry_int(result, "type", &type);
38 qDebug() << "Playlist " << playlist->m_Identifier << " has changed (" << type << ')';
40 switch (type) {
41 case XMMS_PLAYLIST_CHANGED_ADD:
43 quint32 id;
45 xmmsc_result_get_dict_entry_uint(result, "id", &id);
46 qDebug() << "Adding #" << id;
47 emit playlist->entryAboutToBeInserted(playlist->m_IDList.size(), id);
48 playlist->m_IDList << id;
49 emit playlist->entryInserted(playlist->m_IDList.size());
51 break;
52 case XMMS_PLAYLIST_CHANGED_INSERT:
54 quint32 id;
55 qint32 position;
57 xmmsc_result_get_dict_entry_int(result, "position", &position);
58 xmmsc_result_get_dict_entry_uint(result, "id", &id);
59 qDebug() << "Inserting #" << id << " at " << position;
60 emit playlist->entryAboutToBeInserted(position, id);
61 playlist->m_IDList.insert(position, id);
62 emit playlist->entryInserted(position);
64 break;
65 case XMMS_PLAYLIST_CHANGED_UPDATE:
67 quint32 oldid;
68 qint32 position;
70 xmmsc_result_get_dict_entry_int(result, "position", &position);
71 oldid = playlist->m_IDList[position];
72 xmmsc_result_get_dict_entry_uint(result, "id", &playlist->m_IDList[position]);
73 qDebug() << "Updating " << position << " form #" << oldid << " to #" << playlist->m_IDList[position];
74 emit playlist->entryUpdated(position, oldid);
76 break;
77 case XMMS_PLAYLIST_CHANGED_REMOVE:
79 quint32 id;
80 qint32 position;
82 xmmsc_result_get_dict_entry_int(result, "position", &position);
83 id = playlist->m_IDList[position];
84 qDebug() << "Removing #" << id << " at " << position;
85 emit playlist->entryAboutToBeRemoved(position, id);
86 playlist->m_IDList.removeAt(position);
87 emit playlist->entryRemoved(position);
89 break;
90 case XMMS_PLAYLIST_CHANGED_MOVE:
92 quint32 from, to;
94 xmmsc_result_get_dict_entry_uint(result, "position", &from);
95 xmmsc_result_get_dict_entry_uint(result, "newposition", &to);
96 playlist->m_IDList.move(from, to);
97 qDebug() << "Moving " << from << "to" << to;
98 emit playlist->entryMoved(from, to);
100 break;
101 case XMMS_PLAYLIST_CHANGED_SORT:
102 case XMMS_PLAYLIST_CHANGED_SHUFFLE:
103 case XMMS_PLAYLIST_CHANGED_CLEAR:
104 qDebug() << "Reseting";
105 playlist->reset();
106 break;
107 default:
108 break;
111 //xmmsc_result_unref(result);
114 void QPlaylist::fillIDListCallback(xmmsc_result_t * result, void * instance) {
115 if (xmmsc_result_iserror(result)) {
116 qDebug() << "Error (QPlaylist::fillIDListCallback): " << xmmsc_result_get_error(result);
117 return;
120 if (instance) {
121 QPlaylist * playlist = static_cast<QPlaylist *>(instance);
123 quint32 buffer;
125 xmmsc_result_t * i = result;
126 while (xmmsc_result_list_valid(i)) {
127 xmmsc_result_get_uint(i, &buffer);
128 playlist->m_IDList << buffer;
129 xmmsc_result_list_next(i);
131 emit playlist->reseted();
133 //xmmsc_result_unref(result);
136 void QPlaylist::currentPosCallback(xmmsc_result_t * result, void * instance) {
137 if (xmmsc_result_iserror(result)) {
138 qDebug() << "Error (QPlaylist::currentPosCallback): " << xmmsc_result_get_error(result);
139 return;
142 if (instance) {
143 QPlaylist * playlist = static_cast<QPlaylist *>(instance);
144 xmmsc_result_get_uint(result, &playlist->m_Position);
145 emit playlist->positionChanged(playlist->m_Position);
147 //xmmsc_result_unref(result);
150 inline const char * QPlaylist::identifierToCString() {
151 return m_Identifier.isEmpty() ? NULL : m_Identifier.toAscii().data();
154 void QPlaylist::setConnection(xmmsc_connection_t * connection) {
155 if (m_Connection == connection)
156 return;
158 //disconnect
159 /* if (m_Connection) {
160 disconnectNotifierUnref(xmmsc_broadcast_playlist_loaded(m_Connection));
161 disconnectNotifierUnref(xmmsc_broadcast_playlist_changed(m_Connection));
162 disconnectNotifierUnref(xmmsc_broadcast_playlist_current_pos(m_Connection));
165 m_Connection = connection;
167 m_IDList.clear();
168 m_Position = 0;
170 //connect
171 if (m_Connection) {
172 /* connectNotifierUnref(xmmsc_broadcast_playlist_loaded(m_Connection), loadedCallback, this);
173 connectNotifierUnref(xmmsc_broadcast_playlist_changed(m_Connection), changedCallback, this);
174 connectNotifierUnref(xmmsc_broadcast_playlist_current_pos(m_Connection), currentPosCallback, this);*/
176 connectNotifierUnref(xmmsc_playlist_list_entries(m_Connection, identifierToCString()), fillIDListCallback, this);
177 connectNotifierUnref(xmmsc_playlist_current_pos(m_Connection, identifierToCString()), currentPosCallback, this);
179 else {
180 emit reseted();
181 emit positionChanged(0);
185 void QPlaylist::reset() {
186 m_IDList.clear();
187 emit reseted();
188 if (m_Connection) {
189 connectNotifierUnref(xmmsc_playlist_list_entries(m_Connection, identifierToCString()), fillIDListCallback, this);
190 connectNotifierUnref(xmmsc_playlist_current_pos(m_Connection, identifierToCString()), currentPosCallback, this);
195 void QPlaylist::append(quint32 id) {
196 xmmsc_result_unref(xmmsc_playlist_add_id(m_Connection, identifierToCString(), id));
199 void QPlaylist::append(QString url) {
200 xmmsc_result_unref(xmmsc_playlist_add_url(m_Connection, identifierToCString(), url.toAscii().data()));
203 void QPlaylist::append(QStringList urls) {
204 foreach(QString i, urls)
205 append(i);
208 void QPlaylist::append(QList<quint32> ids) {
209 foreach(quint32 i, ids)
210 append(i);
213 void QPlaylist::appendRecursive(QString directory) {
214 xmmsc_result_unref(xmmsc_playlist_radd(m_Connection, identifierToCString(), directory.toAscii().data()));
217 void QPlaylist::insert(quint32 id, quint32 position) {
218 xmmsc_result_unref(xmmsc_playlist_insert_id(m_Connection, identifierToCString(), position, id));
221 void QPlaylist::insert(QString url, quint32 position) {
222 xmmsc_result_unref(xmmsc_playlist_insert_url(m_Connection, identifierToCString(), position, url.toAscii().data()));
225 void QPlaylist::insert(QList<quint32> ids, quint32 position) {
226 if (ids.count() == 0)
227 return;
229 QList<quint32>::iterator i = ids.end();
230 do {
231 i--;
232 insert(*i, position);
234 while (i != ids.begin());
237 void QPlaylist::insert(QStringList urls, quint32 position) {
238 if (urls.count() == 0)
239 return;
241 QStringList::iterator i = urls.end();
242 do {
243 i--;
244 insert(*i, position);
246 while (i != urls.begin());
249 void QPlaylist::remove(quint32 position) {
250 xmmsc_result_unref(xmmsc_playlist_remove_entry(m_Connection, identifierToCString(), position));
253 void QPlaylist::move(quint32 from, quint32 to) {
254 xmmsc_result_unref(xmmsc_playlist_move_entry(m_Connection, identifierToCString(), from, to));
257 void QPlaylist::clear() {
258 xmmsc_result_unref(xmmsc_playlist_clear(m_Connection, identifierToCString()));
261 void QPlaylist::sort(QStringList properties) {
262 const char ** prop = new const char *[properties.count()+1];
263 prop[properties.count()] = NULL;
265 for (int i = 0; i < properties.count(); i++)
266 prop[i] = properties[i].toAscii().data();
268 xmmsc_result_unref(xmmsc_playlist_sort(m_Connection, "Default", prop));
270 delete [] prop;
273 void QPlaylist::randomize() {
274 xmmsc_result_unref(xmmsc_playlist_shuffle(m_Connection, identifierToCString()));