2 // C++ Implementation: playlist
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
6 // Copyright: See COPYING file that comes with this distribution
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());
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
);
33 QPlaylist
* playlist
= static_cast<QPlaylist
*>(instance
);
36 xmmsc_result_get_dict_entry_int(result
, "type", &type
);
38 qDebug() << "Playlist " << playlist
->m_Identifier
<< " has changed (" << type
<< ')';
41 case XMMS_PLAYLIST_CHANGED_ADD
:
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());
52 case XMMS_PLAYLIST_CHANGED_INSERT
:
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
);
65 case XMMS_PLAYLIST_CHANGED_UPDATE
:
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
);
77 case XMMS_PLAYLIST_CHANGED_REMOVE
:
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
);
90 case XMMS_PLAYLIST_CHANGED_MOVE
:
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
);
101 case XMMS_PLAYLIST_CHANGED_SORT
:
102 case XMMS_PLAYLIST_CHANGED_SHUFFLE
:
103 case XMMS_PLAYLIST_CHANGED_CLEAR
:
104 qDebug() << "Reseting";
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
);
121 QPlaylist
* playlist
= static_cast<QPlaylist
*>(instance
);
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
);
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
)
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
;
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);
181 emit
positionChanged(0);
185 void QPlaylist::reset() {
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
)
208 void QPlaylist::append(QList
<quint32
> ids
) {
209 foreach(quint32 i
, ids
)
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)
229 QList
<quint32
>::iterator i
= ids
.end();
232 insert(*i
, position
);
234 while (i
!= ids
.begin());
237 void QPlaylist::insert(QStringList urls
, quint32 position
) {
238 if (urls
.count() == 0)
241 QStringList::iterator i
= urls
.end();
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
));
273 void QPlaylist::randomize() {
274 xmmsc_result_unref(xmmsc_playlist_shuffle(m_Connection
, identifierToCString()));