forked from Elleo/libQtSpotify
-
Notifications
You must be signed in to change notification settings - Fork 3
/
qspotifyuser.cpp
301 lines (261 loc) · 10.2 KB
/
qspotifyuser.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
297
298
299
300
301
/****************************************************************************
**
** 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 "qspotifyuser.h"
#include <QtQml/QQmlEngine>
#include <libspotify/api.h>
#include "qspotifyalbumbrowse.h"
#include "qspotifyalbum.h"
#include "qspotifyplaylistcontainer.h"
#include "qspotifyplaylist.h"
#include "qspotifysession.h"
#include "qspotifytrack.h"
QSpotifyUser::QSpotifyUser(sp_user *user)
: QSpotifyObject(true)
{
Q_ASSERT(user);
sp_user_add_ref(user);
m_sp_user = user;
m_canonicalName = QString::fromUtf8(sp_user_canonical_name(m_sp_user));
connect(this, SIGNAL(dataChanged()), this, SIGNAL(userDataChanged()));
}
QSpotifyUser::~QSpotifyUser()
{
if (m_playlistContainer) {
// This is deleted directly as we have to make sure that its
// gone as soon as this destructor is done.
delete m_playlistContainer;
}
if (m_starredList)
m_starredList->deleteLater();
if (m_inbox)
m_inbox->deleteLater();
if (m_sp_user)
sp_user_release(m_sp_user);
}
bool QSpotifyUser::isLoaded()
{
return sp_user_is_loaded(m_sp_user);
}
bool QSpotifyUser::updateData()
{
QString canonicalName = QString::fromUtf8(sp_user_canonical_name(m_sp_user));
QString displayName = QString::fromUtf8(sp_user_display_name(m_sp_user));
bool updated = false;
if (m_canonicalName != canonicalName) {
m_canonicalName = canonicalName;
updated = true;
}
if (m_displayName != displayName) {
m_displayName = displayName;
updated = true;
}
return updated;
}
QSpotifyPlaylistContainer *QSpotifyUser::playlistContainer() const
{
if (!m_playlistContainer) {
sp_playlistcontainer *pc;
if (QSpotifySession::instance()->user() == this) {
pc = sp_session_playlistcontainer(QSpotifySession::instance()->m_sp_session);
sp_playlistcontainer_add_ref(pc);
} else {
pc = sp_session_publishedcontainer_for_user_create(QSpotifySession::instance()->m_sp_session, m_canonicalName.toUtf8().constData());
}
m_playlistContainer = new QSpotifyPlaylistContainer(pc);
m_playlistContainer->init();
connect(m_playlistContainer, SIGNAL(playlistContainerDataChanged()), this, SIGNAL(playlistsChanged()));
connect(m_playlistContainer, SIGNAL(playlistsNameChanged()), this, SIGNAL(playlistsNameChanged()));
}
return m_playlistContainer;
}
QSpotifyPlaylist *QSpotifyUser::starredList() const
{
if (!m_starredList) {
sp_playlist *sl;
if (QSpotifySession::instance()->user() == this) {
sl = sp_session_starred_create(QSpotifySession::instance()->m_sp_session);
} else {
sl = sp_session_starred_for_user_create(QSpotifySession::instance()->m_sp_session, m_canonicalName.toUtf8().constData());
}
m_starredList = new QSpotifyPlaylist(QSpotifyPlaylist::Starred, sl, false);
m_starredList->init();
QQmlEngine::setObjectOwnership(m_starredList, QQmlEngine::CppOwnership);
}
return m_starredList;
}
QSpotifyPlaylist *QSpotifyUser::inbox() const
{
if (QSpotifySession::instance()->user() != this)
return nullptr;
if (!m_inbox) {
sp_playlist *in;
in = sp_session_inbox_create(QSpotifySession::instance()->m_sp_session);
m_inbox = new QSpotifyPlaylist(QSpotifyPlaylist::Inbox, in, false);
m_inbox->init();
QQmlEngine::setObjectOwnership(m_inbox, QQmlEngine::CppOwnership);
}
return m_inbox;
}
QList<QObject*> QSpotifyUser::playlistsAsQObject() const
{
QList<QObject*> list;
if (auto inb = inbox()) {
list.append((QObject*)inb);
}
list.append((QObject*)starredList());
list.append(playlistContainer()->formattedPlaylists());
return list;
}
QList<QSpotifyPlaylist *> QSpotifyUser::playlists() const
{
return playlistContainer()->playlists();
}
QList<QObject*> QSpotifyUser::playlistsFlat() const
{
return playlistContainer()->playlistsFlat();
}
bool QSpotifyUser::createPlaylist(const QString &name)
{
if (name.trimmed().isEmpty())
return false;
QString n = name;
if (n.size() > 255)
n.resize(255);
sp_playlist *pl = sp_playlistcontainer_add_new_playlist(m_playlistContainer->m_container, n.toUtf8().constData());
return pl != nullptr;
}
bool QSpotifyUser::createPlaylistInFolder(const QString &name, QSpotifyPlaylist *folder)
{
if (!folder || folder->type() != QSpotifyPlaylist::Folder)
return createPlaylist(name);
if (name.trimmed().isEmpty())
return false;
QString n = name;
if (n.size() > 255)
n.resize(255);
sp_playlist *pl = sp_playlistcontainer_add_new_playlist(m_playlistContainer->m_container, n.toUtf8().constData());
if (!pl)
return false;
int i = m_playlistContainer->m_playlists.indexOf(folder);
if (i == -1)
return true;
sp_uint64 folderId = sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, i);
int count = sp_playlistcontainer_num_playlists(m_playlistContainer->m_container);
for (int j = i + 1; j < count; ++j) {
if (folderId == sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, j))
i = j;
}
sp_playlistcontainer_move_playlist(m_playlistContainer->m_container, count - 1, i, false);
return true;
}
bool QSpotifyUser::createPlaylistFromTrack(QSpotifyTrack *track)
{
if (!track)
return false;
sp_playlist *pl = sp_playlistcontainer_add_new_playlist(m_playlistContainer->m_container, track->name().toUtf8().constData());
if (pl == nullptr)
return false;
sp_playlist_add_tracks(pl, const_cast<sp_track* const*>(&track->m_sp_track), 1, 0, QSpotifySession::instance()->spsession());
return true;
}
bool QSpotifyUser::createPlaylistFromAlbum(QSpotifyAlbumBrowse *album)
{
if (!album || !album->m_albumTracks || album->m_albumTracks->count() < 1)
return false;
QString playlistName = album->album()->artist() + QLatin1String(" - ") + album->album()->name();
sp_playlist *pl = sp_playlistcontainer_add_new_playlist(m_playlistContainer->m_container, playlistName.toUtf8().constData());
if (pl == nullptr)
return false;
int c = album->m_albumTracks->count();
const sp_track *tracks[c];
for (int i = 0; i < c; ++i)
tracks[i] = album->m_albumTracks->at(i)->sptrack();
sp_playlist_add_tracks(pl, const_cast<sp_track* const*>(tracks), c, 0, QSpotifySession::instance()->spsession());
return true;
}
void QSpotifyUser::removePlaylist(QSpotifyPlaylist *playlist)
{
if (!playlist)
return;
int i = m_playlistContainer->m_playlists.indexOf(playlist);
if (i == -1)
return;
if (playlist->type() == QSpotifyPlaylist::Folder) {
sp_uint64 folderId = sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, i);
int count = sp_playlistcontainer_num_playlists(m_playlistContainer->m_container);
for (int j = i + 1; j < count; ++j) {
if (folderId == sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, j)) {
sp_playlistcontainer_remove_playlist(m_playlistContainer->m_container, j);
break;
}
}
}
sp_playlistcontainer_remove_playlist(m_playlistContainer->m_container, i);
}
void QSpotifyUser::deleteFolderAndContent(QSpotifyPlaylist *playlist)
{
if (!playlist || playlist->type() != QSpotifyPlaylist::Folder)
return;
int i = m_playlistContainer->m_playlists.indexOf(playlist);
if (i == -1)
return;
sp_uint64 folderId = sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, i);
int count;
sp_uint64 currId;
do {
sp_playlistcontainer_remove_playlist(m_playlistContainer->m_container, i);
count = sp_playlistcontainer_num_playlists(m_playlistContainer->m_container);
currId = sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, i);
} while (i < count && currId != folderId);
if (currId == folderId)
sp_playlistcontainer_remove_playlist(m_playlistContainer->m_container, i);
}
bool QSpotifyUser::ownsPlaylist(QSpotifyPlaylist *playlist)
{
if (!playlist)
return false;
return playlist->owner() == m_canonicalName;
}
bool QSpotifyUser::canModifyPlaylist(QSpotifyPlaylist *playlist)
{
if (!playlist)
return false;
return ownsPlaylist(playlist) || playlist->collaborative();
}