summaryrefslogtreecommitdiffstats
path: root/qtplaylist.cpp
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2009-10-30 12:03:32 +0100
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2009-10-30 12:03:32 +0100
commitbeb0cc36b4dcbe9cd5de0a3cbb2d2e998eb42d03 (patch)
treead7b969fd8905611e44d344ddb203d75d47d4e46 /qtplaylist.cpp
parent2dcbb6e5867529270e6cae985a8c5c7005a77f9b (diff)
Try to separate out ownership of tracks into its own
class. We can't separate out ownership of playlists, as only the root playlist can be owned.
Diffstat (limited to 'qtplaylist.cpp')
-rw-r--r--qtplaylist.cpp131
1 files changed, 131 insertions, 0 deletions
diff --git a/qtplaylist.cpp b/qtplaylist.cpp
new file mode 100644
index 0000000..6534200
--- /dev/null
+++ b/qtplaylist.cpp
@@ -0,0 +1,131 @@
+#include "qtplaylist.h"
+
+#include "despotify_cpp.h"
+
+QtPlaylist::QtPlaylist(despotify_session *session, QObject *parent)
+ : QObject(parent), m_session(session), m_searchResult(0)
+{
+}
+
+QtPlaylist::~QtPlaylist()
+{
+ cleanUp();
+}
+
+void QtPlaylist::cleanUp()
+{
+ switch (m_type) {
+ case Playlist:
+ if (m_playlist != 0) {
+ // Can't free playlist, as only the root playlist can have an owner
+ // Main window owns root playlist
+ m_playlist = 0;
+ }
+ break;
+ case Search:
+ if (m_searchResult != 0) {
+ despotify_free_search(m_searchResult);
+ m_searchResult = 0;
+ }
+ break;
+ case Album:
+ if (m_album != 0) {
+ despotify_free_album_browse(m_album);
+ m_album = 0;
+ }
+ break;
+ case Artist:
+ if (m_artist != 0) {
+ despotify_free_artist_browse(m_artist);
+ m_artist = 0;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+QList<track *> QtPlaylist::makeList(track *t) const
+{
+ QList<track *> list;
+
+ while (t != 0) {
+ list.append(t);
+ t = t->next;
+ }
+
+ return list;
+}
+
+void QtPlaylist::setPlaylist(playlist *pl)
+{
+ cleanUp();
+
+ m_type = Playlist;
+ m_playlist = pl;
+}
+
+void QtPlaylist::setSearchTerm(const QString &searchTerm)
+{
+ cleanUp();
+
+ m_type = Search;
+ m_searchResult = despotify_search(m_session, searchTerm.toUtf8().data(), 50);
+}
+
+void QtPlaylist::searchMore()
+{
+ if (m_type == Search && m_searchResult != 0)
+ despotify_search_more(m_session, m_searchResult, m_searchResult->total_tracks, 50);
+}
+
+void QtPlaylist::setAlbum(const QByteArray &albumId)
+{
+ cleanUp();
+
+ m_type = Album;
+ m_album = despotify_get_album(m_session, QByteArray(albumId).data());
+}
+
+void QtPlaylist::setArtist(const QByteArray &artistId)
+{
+ cleanUp();
+
+ m_type = Artist;
+ m_artist = despotify_get_artist(m_session, QByteArray(artistId).data());
+}
+
+QList<track *> QtPlaylist::tracks() const
+{
+ switch (m_type) {
+ case Playlist:
+ if (m_playlist != 0)
+ return makeList(m_playlist->tracks);
+ break;
+ case Search:
+ if (m_searchResult != 0)
+ return makeList(m_searchResult->tracks);
+ break;
+ case Artist:
+ if (m_artist != 0) {
+ QList<track *> trackList;
+
+ album_browse *album = m_artist->albums;
+ while (album != 0) {
+ trackList += makeList(album->tracks);
+ album = album->next;
+ }
+
+ return trackList;
+ }
+ break;
+ case Album:
+ if (m_album != 0)
+ return makeList(m_album->tracks);
+ break;
+ default:
+ break;
+ }
+
+ return QList<track*>();
+}