From beb0cc36b4dcbe9cd5de0a3cbb2d2e998eb42d03 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Fri, 30 Oct 2009 12:03:32 +0100 Subject: 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. --- qtplaylist.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 qtplaylist.cpp (limited to 'qtplaylist.cpp') 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 QtPlaylist::makeList(track *t) const +{ + QList 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 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 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(); +} -- cgit v1.2.3