forked from Elleo/libQtSpotify
-
Notifications
You must be signed in to change notification settings - Fork 3
/
qspotifyplaylistcontainer.cpp
296 lines (263 loc) · 10.3 KB
/
qspotifyplaylistcontainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/****************************************************************************
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Yoann Lopes (yoann.lopes@nokia.com)
**
** This file is part of the MeeSpot project.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
**
** Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the names of its
** contributors may be used to endorse or promote products derived from
** this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
****************************************************************************/
#include "qspotifyplaylistcontainer.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStack>
#include <QtQml/QQmlEngine>
#include <libspotify/api.h>
#include "qspotifyplaylist.h"
#include "qspotifysession.h"
class QSpotifyPlaylistAddedEvent : public QEvent
{
public:
QSpotifyPlaylistAddedEvent(sp_playlist *playlist, int pos)
: QEvent(Type(User + 1))
, m_playlist(playlist)
, m_position(pos)
{ }
sp_playlist *playlist() const { return m_playlist; }
int position() const { return m_position; }
private:
sp_playlist *m_playlist;
int m_position;
};
class QSpotifyPlaylistRemovedEvent : public QEvent
{
public:
QSpotifyPlaylistRemovedEvent(sp_playlist *playlist, int position)
: QEvent(Type(User + 2))
, m_playlist(playlist)
, m_position(position)
{ }
sp_playlist *playlist() const { return m_playlist; }
int position() const { return m_position; }
private:
sp_playlist *m_playlist;
int m_position;
};
class QSpotifyPlaylistMovedEvent : public QEvent
{
public:
QSpotifyPlaylistMovedEvent(int oldpos, int newpos)
: QEvent(Type(User + 3))
, m_position(oldpos)
, m_newposition(newpos)
{ }
int position() const { return m_position; }
int newPosition() const { return m_newposition; }
private:
int m_position;
int m_newposition;
};
static void callback_container_loaded(sp_playlistcontainer *, void *objectPtr)
{
QCoreApplication::postEvent(static_cast<QSpotifyPlaylistContainer *>(objectPtr), new QEvent(QEvent::User));
}
static void callback_playlist_added(sp_playlistcontainer *, sp_playlist *playlist, int position, void *objectPtr)
{
QCoreApplication::postEvent(static_cast<QSpotifyPlaylistContainer *>(objectPtr), new QSpotifyPlaylistAddedEvent(playlist, position));
}
static void callback_playlist_removed(sp_playlistcontainer *, sp_playlist *playlist, int position, void *objectPtr)
{
QCoreApplication::postEvent(static_cast<QSpotifyPlaylistContainer *>(objectPtr), new QSpotifyPlaylistRemovedEvent(playlist, position));
}
static void callback_playlist_moved(sp_playlistcontainer *, sp_playlist *, int position, int new_position, void *objectPtr)
{
QCoreApplication::postEvent(static_cast<QSpotifyPlaylistContainer *>(objectPtr), new QSpotifyPlaylistMovedEvent(position, new_position));
}
QSpotifyPlaylistContainer::QSpotifyPlaylistContainer(sp_playlistcontainer *container)
: QSpotifyObject(true)
, m_container{container}
{
Q_ASSERT(container);
m_callbacks = new sp_playlistcontainer_callbacks;
m_callbacks->playlist_added = callback_playlist_added;
m_callbacks->playlist_removed = callback_playlist_removed;
m_callbacks->playlist_moved = callback_playlist_moved;
m_callbacks->container_loaded = callback_container_loaded;
sp_playlistcontainer_add_callbacks(m_container, m_callbacks, this);
connect(QSpotifySession::instance(), SIGNAL(offlineModeChanged()), this, SLOT(updatePlaylists()));
}
QSpotifyPlaylistContainer::~QSpotifyPlaylistContainer()
{
if (m_container) {
sp_playlistcontainer_remove_callbacks(m_container, m_callbacks, this);
sp_playlistcontainer_release(m_container);
}
qDeleteAll(m_playlists);
m_playlists.clear();
delete m_callbacks;
}
bool QSpotifyPlaylistContainer::isLoaded()
{
return sp_playlistcontainer_is_loaded(m_container);
}
bool QSpotifyPlaylistContainer::updateData()
{
bool updated = false;
if (m_playlists.isEmpty()) {
int count = sp_playlistcontainer_num_playlists(m_container);
for (int i = 0; i < count; ++i) {
addPlaylist(sp_playlistcontainer_playlist(m_container, i), i);
if(sp_playlistcontainer_playlist_type(m_container, i) == SP_PLAYLIST_TYPE_PLACEHOLDER)
sp_playlistcontainer_remove_playlist(m_container, i);
}
updated = true;
updatePlaylists();
}
return updated;
}
void QSpotifyPlaylistContainer::updatePlaylists()
{
m_formattedAvailablePlaylists.clear();
m_formattedUnavailablePlaylists.clear();
m_playlistsFlat.clear();
QStack<QSpotifyPlaylist *> folders;
for (int i = 0; i < m_playlists.count(); ++i) {
QSpotifyPlaylist *playlist = m_playlists.at(i);
if (playlist->type() == QSpotifyPlaylist::Playlist) {
m_playlistsFlat.append((QObject*)playlist);
if (!QSpotifySession::instance()->offlineMode() || playlist->availableOffline()) {
if (folders.isEmpty())
m_formattedAvailablePlaylists.append((QObject*)(playlist));
else
folders.top()->m_availablePlaylists.append((QObject*)(playlist));
} else {
if (folders.isEmpty())
m_formattedUnavailablePlaylists.append((QObject*)(playlist));
else
folders.top()->m_unavailablePlaylists.append((QObject*)(playlist));
}
} else if (playlist->type() == QSpotifyPlaylist::Folder) {
playlist->clearPlaylists();
folders.push(playlist);
} else if (playlist->type() == QSpotifyPlaylist::FolderEnd) {
if (folders.isEmpty())
continue;
QSpotifyPlaylist *folder = folders.pop();
if (folders.isEmpty())
m_formattedAvailablePlaylists.append((QObject *)(folder));
else
folders.top()->m_availablePlaylists.append(folder);
emit folder->playlistsChanged();
}
}
emit playlistContainerDataChanged();
}
void QSpotifyPlaylistContainer::addPlaylist(sp_playlist *playlist, int pos)
{
Q_ASSERT(playlist);
if (playlist != sp_playlistcontainer_playlist(m_container, pos)) {
int count = sp_playlistcontainer_num_playlists(m_container);
for (int i = 0; i < count; ++i) {
if (playlist == sp_playlistcontainer_playlist(m_container, i)) {
pos = i;
break;
}
}
}
sp_playlist_type type = sp_playlistcontainer_playlist_type(m_container, pos);
QSpotifyPlaylist *pl = new QSpotifyPlaylist(QSpotifyPlaylist::Type(type), playlist);
pl->init();
QQmlEngine::setObjectOwnership(pl, QQmlEngine::CppOwnership);
if (pos == -1)
m_playlists.append(pl);
else
m_playlists.insert(pos, pl);
if (type == SP_PLAYLIST_TYPE_START_FOLDER) {
char buffer[200];
sp_playlistcontainer_playlist_folder_name(m_container, pos, buffer, 200);
pl->m_name = QString::fromUtf8(buffer);
}
connect(pl, SIGNAL(nameChanged()), this, SIGNAL(playlistsNameChanged()));
}
bool QSpotifyPlaylistContainer::event(QEvent *e)
{
if (e->type() == QEvent::User) {
metadataUpdated();
e->accept();
return true;
} else if (e->type() == QEvent::User + 1) {
// PlaylistAdded event
QSpotifyPlaylistAddedEvent *ev = static_cast<QSpotifyPlaylistAddedEvent *>(e);
addPlaylist(ev->playlist(), ev->position());
postUpdateEvent();
e->accept();
return true;
} else if (e->type() == QEvent::User + 2) {
// PlaylistRemoved event
qDebug() << "Playlist removed event";
QSpotifyPlaylistRemovedEvent *ev = static_cast<QSpotifyPlaylistRemovedEvent *>(e);
int i = ev->position();
if (i >= 0 && i < m_playlists.count() && m_playlists.at(i)->m_sp_playlist == ev->playlist()) {
QSpotifyPlaylist *pl = m_playlists.takeAt(i);
pl->deleteLater();
postUpdateEvent();
}
e->accept();
return true;
} else if (e->type() == QEvent::User + 3) {
// PlaylistMoved event
QSpotifyPlaylistMovedEvent *ev = static_cast<QSpotifyPlaylistMovedEvent *>(e);
int i = ev->position();
int newpos = ev->newPosition();
if (i >= 0 && i < m_playlists.count()) {
QSpotifyPlaylist *pl = m_playlists.takeAt(i);
m_playlists.insert(newpos > i ? newpos - 1 : newpos, pl);
postUpdateEvent();
}
e->accept();
return true;
} else if (e->type() == QEvent::User + 4) {
updatePlaylists();
m_updateEventPosted = false;
e->accept();
return true;
}
return QSpotifyObject::event(e);
}
void QSpotifyPlaylistContainer::postUpdateEvent()
{
if (!m_updateEventPosted) {
QCoreApplication::postEvent(this, new QEvent(QEvent::Type(QEvent::User + 4)));
m_updateEventPosted = true;
}
}