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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qtplaylist.h | 51 +++++++++++++++++++++ qtspotify.pro | 6 ++- qtspotifymain.cpp | 71 +++++++++++++++-------------- qtspotifymain.h | 9 ++-- 5 files changed, 229 insertions(+), 39 deletions(-) create mode 100644 qtplaylist.cpp create mode 100644 qtplaylist.h 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(); +} diff --git a/qtplaylist.h b/qtplaylist.h new file mode 100644 index 0000000..d43ceef --- /dev/null +++ b/qtplaylist.h @@ -0,0 +1,51 @@ +#ifndef QTPLAYLIST_H +#define QTPLAYLIST_H + +#include + +struct despotify_session; +struct playlist; +struct track; +struct search_result; +struct album_browse; +struct artist_browse; +class QtPlaylist: public QObject +{ + Q_OBJECT +public: + QtPlaylist(despotify_session *session, QObject *parent = 0); + ~QtPlaylist(); + + void setArtist(const QByteArray &artistId); + void setAlbum(const QByteArray &albumId); + void setSearchTerm(const QString &searchTerm); + void setPlaylist(playlist *pl); + void searchMore(); + + QList tracks() const; + +private: + void cleanUp(); + QList makeList(track *firstTrack) const; + + enum Type { + NoType, + Playlist, + Search, + Album, + Artist + }; + + despotify_session *m_session; + + union { + playlist *m_playlist; + search_result *m_searchResult; + album_browse *m_album; + artist_browse *m_artist; + }; + Type m_type; + +}; + +#endif // QTPLAYLIST_H diff --git a/qtspotify.pro b/qtspotify.pro index b284ff0..2159f3a 100644 --- a/qtspotify.pro +++ b/qtspotify.pro @@ -1,9 +1,11 @@ HEADERS += qtspotifymain.h \ logindialog.h \ - despotify_cpp.h + despotify_cpp.h \ + qtplaylist.h SOURCES += qtspotifymain.cpp \ main.cpp \ - logindialog.cpp + logindialog.cpp \ + qtplaylist.cpp FORMS += qtspotify.ui \ logindialog.ui LIBS += -ldespotify diff --git a/qtspotifymain.cpp b/qtspotifymain.cpp index cc96de3..32176c6 100644 --- a/qtspotifymain.cpp +++ b/qtspotifymain.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "qtspotifymain.h" #include "logindialog.h" +#include "qtplaylist.h" #include #include @@ -40,7 +41,7 @@ #include "despotify_cpp.h" -Q_DECLARE_METATYPE(playlist *) +Q_DECLARE_METATYPE(QtPlaylist *) Q_DECLARE_METATYPE(track *) namespace { @@ -58,7 +59,7 @@ namespace { QtSpotifyMain::QtSpotifyMain(QWidget *parent) - : QMainWindow(parent), m_session(0), m_searchResult(0), m_storedPlaylist(0), + : QMainWindow(parent), m_session(0), m_rootPlaylist(0), m_machine(new QStateMachine), m_debugging(false), m_authenticationWatcher(0), m_retrievingPlayListWatcher(0) { @@ -138,6 +139,7 @@ void QtSpotifyMain::selectTrack(track *newTrack) } } // Playlist changed + debug(tr("Track could not be found")); } void QtSpotifyMain::retrievePlayLists() @@ -146,8 +148,10 @@ void QtSpotifyMain::retrievePlayLists() m_ui.searchComboBox->clear(); m_ui.searchComboBox->clearEditText(); - if (m_storedPlaylist != 0) - despotify_free_playlist(m_storedPlaylist); + if (m_rootPlaylist != 0) { + despotify_free_playlist(m_rootPlaylist); + m_rootPlaylist = 0; + } QFuture retrieving = QtConcurrent::run(despotify_get_stored_playlists, m_session); m_retrievingPlayListWatcher->setFuture(retrieving); @@ -156,44 +160,42 @@ void QtSpotifyMain::retrievePlayLists() void QtSpotifyMain::selectPlayList() { QString searchTerm = m_ui.searchComboBox->currentText(); - if (m_searchResult != 0) { - despotify_free_search(m_searchResult); - m_searchResult = 0; - } - m_ui.playList->clear(); int idx = m_ui.searchComboBox->currentIndex(); if (idx >= 0) { QVariant data = m_ui.searchComboBox->itemData(idx); - playlist *pl = data.value(); + QtPlaylist *pl = data.value(); if (pl != 0) { - debug(tr("Selecting playlist '%1'").arg(QString::fromUtf8(pl->name))); - setPlayList(pl->tracks); + debug(tr("Selecting playlist '%1'").arg(m_ui.searchComboBox->itemText(idx))); + setPlaylist(pl->tracks()); return; } } - m_searchResult = despotify_search(m_session, searchTerm.toUtf8().data(), 50); - if (m_searchResult != 0) { - debug(tr("Searching for '%1'").arg(searchTerm)); - setPlayList(m_searchResult->tracks); - } else { - debug(tr("No results when searching for '%1'").arg(searchTerm)); - } + QtPlaylist *newSearch = new QtPlaylist(m_session, this); + newSearch->setSearchTerm(searchTerm); + m_searches.append(newSearch); + + debug(tr("Searching for '%1'").arg(searchTerm)); + setPlaylist(newSearch->tracks()); } void QtSpotifyMain::populateSearchBox() { debug(tr("Populating search box")); - m_storedPlaylist = m_retrievingPlayListWatcher->result(); - if (m_storedPlaylist == 0) + m_rootPlaylist = m_retrievingPlayListWatcher->result(); + if (m_rootPlaylist == 0) debug(tr("Cannot get stored playlists, error==%1").arg(QString::fromUtf8(m_session->last_error))); - playlist *pl = m_storedPlaylist; + playlist *pl = m_rootPlaylist; while (pl != 0) { - m_ui.searchComboBox->addItem(QString::fromUtf8(pl->name), QVariant::fromValue(pl)); + QtPlaylist *playlist = new QtPlaylist(m_session, this); + playlist->setPlaylist(pl); + + m_ui.searchComboBox->addItem(QString::fromUtf8(pl->name), QVariant::fromValue(playlist)); debug(tr("Adding '%1'").arg(QString::fromUtf8(pl->name))); + pl = pl->next; } @@ -406,11 +408,11 @@ void QtSpotifyMain::initMachine() m_machine->start(); } -void QtSpotifyMain::setPlayList(track *t) +void QtSpotifyMain::setPlaylist(QList tracks) { debug(tr("Setting new playlist")); m_ui.playList->clear(); - while (t != 0) { + foreach (track *t, tracks) { QTreeWidgetItem *w = new QTreeWidgetItem(); w->setData(0, Qt::DisplayRole, QString::fromUtf8(t->artist->name)); @@ -419,8 +421,6 @@ void QtSpotifyMain::setPlayList(track *t) w->setData(0, Qt::UserRole, QVariant::fromValue(t)); m_ui.playList->addTopLevelItem(w); - - t = t->next; } } @@ -441,16 +441,19 @@ static void callback(despotify_session *session, int signal, void *data, void *c void QtSpotifyMain::endSession() { - debug("Ending session"); - if (m_storedPlaylist != 0) - despotify_free_playlist(m_storedPlaylist); + debug("Ending session"); + if (m_rootPlaylist != 0) + despotify_free_playlist(m_rootPlaylist); - if (m_searchResult != 0) { - despotify_free_search(m_searchResult); - m_searchResult = 0; - } + qDeleteAll(m_searches); + for (int i=0; icount(); ++i) { + QVariant v = m_ui.searchComboBox->itemData(i); + QtPlaylist *pl = v.value(); + delete pl; + } m_ui.searchComboBox->clear(); + despotify_exit(m_session); } diff --git a/qtspotifymain.h b/qtspotifymain.h index 179a3d3..aaa7376 100644 --- a/qtspotifymain.h +++ b/qtspotifymain.h @@ -61,6 +61,7 @@ struct despotify_session; struct track; struct playlist; struct search_result; +class QtPlaylist; class QStateMachine; class QState; @@ -102,7 +103,7 @@ signals: void loginFailed(); private: - void setPlayList(track *t); + void setPlaylist(QList tracks); void initUi(); void initWatchers(); @@ -118,10 +119,12 @@ private: Ui_QtSpotifyMain m_ui; despotify_session *m_session; - search_result *m_searchResult; - playlist *m_storedPlaylist; + + playlist *m_rootPlaylist; QStateMachine *m_machine; + QList m_searches; + bool m_debugging; QFutureWatcher *m_authenticationWatcher; -- cgit v1.2.3