diff options
author | Yoann Lopes <yoann.lopes@nokia.com> | 2011-10-22 21:21:10 +0200 |
---|---|---|
committer | Yoann Lopes <yoann.lopes@nokia.com> | 2011-10-22 21:21:10 +0200 |
commit | 49ce7f225fc49793fe628f7c5a923577ae648522 (patch) | |
tree | bd09b67dc242bf7a990a116663def335c0619d99 /libQtSpotify | |
parent | 0d73477754400ffb91b1ea10490c33c69b501075 (diff) |
Handle offline status per track and not per playlist.
Diffstat (limited to 'libQtSpotify')
-rw-r--r-- | libQtSpotify/qspotifyplaylist.cpp | 81 | ||||
-rw-r--r-- | libQtSpotify/qspotifyplaylist.h | 23 | ||||
-rw-r--r-- | libQtSpotify/qspotifyplayqueue.cpp | 7 | ||||
-rw-r--r-- | libQtSpotify/qspotifyplayqueue.h | 1 | ||||
-rw-r--r-- | libQtSpotify/qspotifysession.cpp | 2 | ||||
-rw-r--r-- | libQtSpotify/qspotifytrack.cpp | 33 | ||||
-rw-r--r-- | libQtSpotify/qspotifytrack.h | 28 | ||||
-rw-r--r-- | libQtSpotify/qspotifytracklist.cpp | 10 |
8 files changed, 144 insertions, 41 deletions
diff --git a/libQtSpotify/qspotifyplaylist.cpp b/libQtSpotify/qspotifyplaylist.cpp index 95c991e..17f15b9 100644 --- a/libQtSpotify/qspotifyplaylist.cpp +++ b/libQtSpotify/qspotifyplaylist.cpp @@ -5,22 +5,22 @@ ** 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 @@ -169,6 +169,7 @@ QSpotifyPlaylist::QSpotifyPlaylist(Type type, sp_playlist *playlist, bool incrRe , m_collaborative(false) , m_offlineDownloadProgress(0) , m_availableOffline(false) + , m_skipUpdateTracks(false) { m_trackList = new QSpotifyTrackList(type == Starred || type == Inbox); @@ -235,7 +236,7 @@ bool QSpotifyPlaylist::updateData() updated = true; } - if (m_trackList->m_tracks.isEmpty()) { + if (m_trackList->m_tracks.isEmpty() && !m_skipUpdateTracks) { int count = sp_playlist_num_tracks(m_sp_playlist); for (int i = 0; i < count; ++i) addTrack(sp_playlist_track(m_sp_playlist, i)); @@ -244,7 +245,10 @@ bool QSpotifyPlaylist::updateData() OfflineStatus os = OfflineStatus(sp_playlist_get_offline_status(QSpotifySession::instance()->spsession(), m_sp_playlist)); if (m_offlineStatus != os) { - m_offlineStatus = os; + if (os == Waiting && m_offlineTracks.count() == m_availableTracks.count()) + m_offlineStatus = Yes; + else + m_offlineStatus = os; if (m_offlineStatus != No) { m_availableOffline = true; @@ -268,12 +272,17 @@ bool QSpotifyPlaylist::updateData() void QSpotifyPlaylist::addTrack(sp_track *track, int pos) { QSpotifyTrack *qtrack = new QSpotifyTrack(track, this); + + registerTrackType(qtrack); + if (pos == -1) m_trackList->m_tracks.append(qtrack); else m_trackList->m_tracks.insert(pos, qtrack); m_tracksSet.insert(track); connect(qtrack, SIGNAL(trackDataChanged()), this, SIGNAL(playlistDataChanged())); + connect(qtrack, SIGNAL(offlineStatusChanged()), this, SLOT(onTrackChanged())); + connect(qtrack, SIGNAL(isAvailableChanged()), this, SLOT(onTrackChanged())); if (m_type != Starred) { connect(QSpotifySession::instance()->user()->starredList(), SIGNAL(tracksAdded(QVector<sp_track*>)), qtrack, SLOT(onStarredListTracksAdded(QVector<sp_track*>))); connect(QSpotifySession::instance()->user()->starredList(), SIGNAL(tracksRemoved(QVector<sp_track*>)), qtrack, SLOT(onStarredListTracksRemoved(QVector<sp_track*>))); @@ -287,7 +296,9 @@ void QSpotifyPlaylist::addTrack(sp_track *track, int pos) bool QSpotifyPlaylist::event(QEvent *e) { if (e->type() == QEvent::User) { + m_skipUpdateTracks = true; metadataUpdated(); + m_skipUpdateTracks = false; e->accept(); return true; } else if (e->type() == QEvent::User + 1) { @@ -307,17 +318,15 @@ bool QSpotifyPlaylist::event(QEvent *e) // TracksAdded event QSpotifyTracksAddedEvent *ev = static_cast<QSpotifyTracksAddedEvent *>(e); QVector<sp_track*> tracks = ev->tracks(); - if (m_trackList->m_tracks.count() + tracks.count() == sp_playlist_num_tracks(m_sp_playlist)) { - int pos = ev->position(); - for (int i = 0; i < tracks.count(); ++i) - addTrack(tracks.at(i), pos++); - emit dataChanged(); - if (m_type == Starred || m_type == Inbox) - emit tracksAdded(tracks); - m_trackList->setShuffle(m_trackList->isShuffle()); - if (QSpotifySession::instance()->playQueue()->isCurrentTrackList(m_trackList)) - QSpotifySession::instance()->playQueue()->tracksUpdated(); - } + int pos = ev->position(); + for (int i = 0; i < tracks.count(); ++i) + addTrack(tracks.at(i), pos++); + emit dataChanged(); + if (m_type == Starred || m_type == Inbox) + emit tracksAdded(tracks); + m_trackList->setShuffle(m_trackList->isShuffle()); + if (QSpotifySession::instance()->playQueue()->isCurrentTrackList(m_trackList)) + QSpotifySession::instance()->playQueue()->tracksUpdated(); e->accept(); return true; } else if (e->type() == QEvent::User + 4) { @@ -330,6 +339,9 @@ bool QSpotifyPlaylist::event(QEvent *e) if (pos < 0 || pos >= m_trackList->m_tracks.count()) continue; QSpotifyTrack *tr = m_trackList->m_tracks[pos]; + unregisterTrackType(tr); + disconnect(tr, SIGNAL(offlineStatusChanged()), this, SLOT(onTrackChanged())); + disconnect(tr, SIGNAL(isAvailableChanged()), this, SLOT(onTrackChanged())); tracksSignal.append(tr->m_sp_track); m_tracksSet.remove(tr->m_sp_track); tr->release(); @@ -524,3 +536,36 @@ int QSpotifyPlaylist::unseenCount() const } return c; } + +void QSpotifyPlaylist::onTrackChanged() +{ + if (!sender()) + return; + + QSpotifyTrack *tr = dynamic_cast<QSpotifyTrack *>(sender()); + if (!tr) + return; + + registerTrackType(tr); +} + +void QSpotifyPlaylist::registerTrackType(QSpotifyTrack *t) +{ + int oldCount = m_offlineTracks.count(); + if (t->offlineStatus() == QSpotifyTrack::Yes) + m_offlineTracks.insert(t); + else + m_offlineTracks.remove(t); + if ((oldCount == 0 && m_offlineTracks.count() > 0) || (oldCount == 1 && m_offlineTracks.count() == 0)) + emit hasOfflineTracksChanged(); + + if (t->m_isAvailable) { + m_availableTracks.insert(t); + } +} + +void QSpotifyPlaylist::unregisterTrackType(QSpotifyTrack *t) +{ + m_offlineTracks.remove(t); + m_availableTracks.remove(t); +} diff --git a/libQtSpotify/qspotifyplaylist.h b/libQtSpotify/qspotifyplaylist.h index 8dbc392..59ca4e4 100644 --- a/libQtSpotify/qspotifyplaylist.h +++ b/libQtSpotify/qspotifyplaylist.h @@ -5,22 +5,22 @@ ** 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 @@ -73,6 +73,7 @@ class QSpotifyPlaylist : public QSpotifyObject Q_PROPERTY(int offlineDownloadProgress READ offlineDownloadProgress NOTIFY playlistDataChanged) Q_PROPERTY(bool availableOffline READ availableOffline WRITE setAvailableOffline NOTIFY availableOfflineChanged) Q_PROPERTY(int unseenCount READ unseenCount NOTIFY seenCountChanged) + Q_PROPERTY(bool hasOfflineTracks READ hasOfflineTracks NOTIFY hasOfflineTracksChanged) Q_ENUMS(Type) Q_ENUMS(OfflineStatus) public: @@ -108,6 +109,7 @@ public: QList<QSpotifyTrack *> tracks() const { return m_trackList->m_tracks; } QList<QObject *> tracksAsQObject() const; int unseenCount() const; + bool hasOfflineTracks() const { return m_offlineTracks.count() > 0; } bool contains(sp_track *t) const { return m_tracksSet.contains(t); } @@ -134,14 +136,20 @@ Q_SIGNALS: void tracksRemoved(QVector<sp_track *>); void availableOfflineChanged(); void seenCountChanged(); + void hasOfflineTracksChanged(); protected: bool updateData(); bool event(QEvent *); +private Q_SLOTS: + void onTrackChanged(); + private: QSpotifyPlaylist(Type type, sp_playlist *playlist, bool incrRefCount = true); void addTrack(sp_track *track, int pos = -1); + void registerTrackType(QSpotifyTrack *t); + void unregisterTrackType(QSpotifyTrack *t); sp_playlist *m_sp_playlist; sp_playlist_callbacks *m_callbacks; @@ -157,7 +165,12 @@ private: int m_offlineDownloadProgress; bool m_availableOffline; + QSet<QSpotifyTrack *> m_offlineTracks; + QSet<QSpotifyTrack *> m_availableTracks; + QString m_uri; + + bool m_skipUpdateTracks; friend class QSpotifyPlaylistContainer; friend class QSpotifyUser; diff --git a/libQtSpotify/qspotifyplayqueue.cpp b/libQtSpotify/qspotifyplayqueue.cpp index d190eb3..ccf3b1c 100644 --- a/libQtSpotify/qspotifyplayqueue.cpp +++ b/libQtSpotify/qspotifyplayqueue.cpp @@ -289,3 +289,10 @@ void QSpotifyPlayQueue::tracksUpdated() { emit tracksChanged(); } + +void QSpotifyPlayQueue::onOfflineModeChanged() +{ + if (m_shuffle && m_implicitTracks) + m_implicitTracks->setShuffle(true); + emit tracksChanged(); +} diff --git a/libQtSpotify/qspotifyplayqueue.h b/libQtSpotify/qspotifyplayqueue.h index d8bca5e..8365b9f 100644 --- a/libQtSpotify/qspotifyplayqueue.h +++ b/libQtSpotify/qspotifyplayqueue.h @@ -84,6 +84,7 @@ Q_SIGNALS: private Q_SLOTS: void onTrackReady(); + void onOfflineModeChanged(); private: diff --git a/libQtSpotify/qspotifysession.cpp b/libQtSpotify/qspotifysession.cpp index 2722bb8..924f5f6 100644 --- a/libQtSpotify/qspotifysession.cpp +++ b/libQtSpotify/qspotifysession.cpp @@ -526,6 +526,8 @@ void QSpotifySession::init() bool repeat = settings.value("repeat", false).toBool(); setRepeat(repeat); + + connect(this, SIGNAL(offlineModeChanged()), m_playQueue, SLOT(onOfflineModeChanged())); } } diff --git a/libQtSpotify/qspotifytrack.cpp b/libQtSpotify/qspotifytrack.cpp index 6dac538..edee623 100644 --- a/libQtSpotify/qspotifytrack.cpp +++ b/libQtSpotify/qspotifytrack.cpp @@ -5,22 +5,22 @@ ** 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 @@ -63,6 +63,7 @@ QSpotifyTrack::QSpotifyTrack(sp_track *track, QSpotifyPlaylist *playlist) , m_numArtists(0) , m_popularity(0) , m_seen(true) + , m_offlineStatus(No) , m_isCurrentPlayingTrack(false) { sp_track_add_ref(track); @@ -71,6 +72,7 @@ QSpotifyTrack::QSpotifyTrack(sp_track *track, QSpotifyPlaylist *playlist) connect(QSpotifySession::instance(), SIGNAL(currentTrackChanged()), this, SLOT(onSessionCurrentTrackChanged())); connect(this, SIGNAL(dataChanged()), this, SIGNAL(trackDataChanged())); + connect(QSpotifySession::instance(), SIGNAL(offlineModeChanged()), this, SLOT(onSessionOfflineModeChanged())); metadataUpdated(); } @@ -87,6 +89,7 @@ QSpotifyTrack::QSpotifyTrack(sp_track *track, QSpotifyTrackList *tracklist) , m_numArtists(0) , m_popularity(0) , m_seen(true) + , m_offlineStatus(No) , m_isCurrentPlayingTrack(false) { sp_track_add_ref(track); @@ -130,6 +133,7 @@ bool QSpotifyTrack::updateData() bool isAvailable = sp_track_is_available(QSpotifySession::instance()->m_sp_session, m_sp_track); int numArtists = sp_track_num_artists(m_sp_track); int popularity = sp_track_popularity(m_sp_track); + OfflineStatus offlineSt = OfflineStatus(sp_track_offline_get_status(m_sp_track)); if (m_playlist && m_playlist->type() == QSpotifyPlaylist::Inbox) { int tindex = m_trackList->m_tracks.indexOf(this); @@ -173,6 +177,7 @@ bool QSpotifyTrack::updateData() } if (m_isAvailable != isAvailable) { m_isAvailable = isAvailable; + emit isAvailableChanged(); updated = true; } if (m_numArtists != numArtists) { @@ -183,6 +188,11 @@ bool QSpotifyTrack::updateData() m_popularity = popularity; updated = true; } + if (m_offlineStatus != offlineSt) { + m_offlineStatus = offlineSt; + emit offlineStatusChanged(); + updated = true; + } if (m_artists.isEmpty()) { int count = sp_track_num_artists(m_sp_track); @@ -312,5 +322,16 @@ void QSpotifyTrack::setSeen(bool s) bool QSpotifyTrack::isAvailableOffline() const { - return m_playlist && m_playlist->offlineStatus() == QSpotifyPlaylist::Yes; + return m_offlineStatus == Yes; +} + +bool QSpotifyTrack::isAvailable() const +{ + return m_isAvailable && (!QSpotifySession::instance()->offlineMode() || m_offlineStatus == Yes); +} + +void QSpotifyTrack::onSessionOfflineModeChanged() +{ + if (m_offlineStatus != Yes) + emit isAvailableChanged(); } diff --git a/libQtSpotify/qspotifytrack.h b/libQtSpotify/qspotifytrack.h index 9c8022b..8034abb 100644 --- a/libQtSpotify/qspotifytrack.h +++ b/libQtSpotify/qspotifytrack.h @@ -5,22 +5,22 @@ ** 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 @@ -64,7 +64,7 @@ class QSpotifyTrack : public QSpotifyObject Q_PROPERTY(int durationMs READ duration NOTIFY trackDataChanged) Q_PROPERTY(Error error READ error NOTIFY trackDataChanged) Q_PROPERTY(int discIndex READ discIndex NOTIFY trackDataChanged) - Q_PROPERTY(bool isAvailable READ isAvailable NOTIFY trackDataChanged) + Q_PROPERTY(bool isAvailable READ isAvailable NOTIFY isAvailableChanged) Q_PROPERTY(bool isStarred READ isStarred WRITE setIsStarred NOTIFY isStarredChanged) Q_PROPERTY(int popularity READ popularity NOTIFY trackDataChanged) Q_PROPERTY(bool isCurrentPlayingTrack READ isCurrentPlayingTrack NOTIFY isCurrentPlayingTrackChanged) @@ -73,7 +73,9 @@ class QSpotifyTrack : public QSpotifyObject Q_PROPERTY(QDateTime creationDate READ creationDate NOTIFY trackDataChanged) Q_PROPERTY(QSpotifyAlbum *albumObject READ albumObject NOTIFY trackDataChanged) Q_PROPERTY(QSpotifyArtist *artistObject READ artistObject NOTIFY trackDataChanged) + Q_PROPERTY(OfflineStatus offlineStatus READ offlineStatus NOTIFY offlineStatusChanged) Q_ENUMS(Error) + Q_ENUMS(OfflineStatus) public: enum Error { Ok = SP_ERROR_OK, @@ -81,6 +83,13 @@ public: OtherPermanent = SP_ERROR_OTHER_PERMANENT }; + enum OfflineStatus { + No = 0, + Waiting = 1, + Downloading = 2, + Yes = 3 + }; + ~QSpotifyTrack(); bool isLoaded(); @@ -94,7 +103,7 @@ public: QString durationString() const { return m_durationString; } Error error() const { return m_error; } int discIndex() const { return m_discIndex; } - bool isAvailable() const { return m_isAvailable; } + bool isAvailable() const; bool isStarred() const; void setIsStarred(bool v); QString name() const { return m_name; } @@ -105,6 +114,7 @@ public: void setSeen(bool s); QString creator() const { return m_creator; } QDateTime creationDate() const { return m_creationDate; } + OfflineStatus offlineStatus() const { return m_offlineStatus; } bool isCurrentPlayingTrack() const { return m_isCurrentPlayingTrack; } @@ -128,6 +138,8 @@ Q_SIGNALS: void trackDataChanged(); void isStarredChanged(); void seenChanged(); + void isAvailableChanged(); + void offlineStatusChanged(); protected: bool updateData(); @@ -136,6 +148,7 @@ private Q_SLOTS: void onSessionCurrentTrackChanged(); void onStarredListTracksAdded(QVector<sp_track *>); void onStarredListTracksRemoved(QVector<sp_track *>); + void onSessionOfflineModeChanged(); private: QSpotifyTrack(sp_track *track, QSpotifyPlaylist *playlist); @@ -161,6 +174,7 @@ private: bool m_seen; QString m_creator; QDateTime m_creationDate; + OfflineStatus m_offlineStatus; bool m_isCurrentPlayingTrack; diff --git a/libQtSpotify/qspotifytracklist.cpp b/libQtSpotify/qspotifytracklist.cpp index 857fc5c..f4ef3a5 100644 --- a/libQtSpotify/qspotifytracklist.cpp +++ b/libQtSpotify/qspotifytracklist.cpp @@ -5,22 +5,22 @@ ** 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 |