diff options
-rw-r--r-- | qtspotifymain.cpp | 47 | ||||
-rw-r--r-- | qtspotifymain.h | 6 |
2 files changed, 50 insertions, 3 deletions
diff --git a/qtspotifymain.cpp b/qtspotifymain.cpp index 6ad8d17..2fa3192 100644 --- a/qtspotifymain.cpp +++ b/qtspotifymain.cpp @@ -43,6 +43,20 @@ Q_DECLARE_METATYPE(playlist *) Q_DECLARE_METATYPE(track *) +namespace { + class SelectTrackEvent: public QEvent + { + public: + SelectTrackEvent(track *t) : QEvent(QEvent::User), m_track(t) {} + + track *selectedTrack() const { return m_track; } + + private: + track *m_track; + }; +} + + QtSpotifyMain::QtSpotifyMain(QWidget *parent) : QMainWindow(parent), m_session(0), m_searchResult(0), m_storedPlaylist(0), m_machine(new QStateMachine), m_debugging(false), @@ -101,6 +115,33 @@ void QtSpotifyMain::initWatchers() connect(m_retrievingPlayListWatcher, SIGNAL(finished()), this, SLOT(populateSearchBox())); } +bool QtSpotifyMain::event(QEvent *e) +{ + if (e->type() == QEvent::User) { + SelectTrackEvent *ste = static_cast<SelectTrackEvent *>(e); + selectTrack(ste->selectedTrack()); + return true; + } + + return QMainWindow::event(e); +} + +void QtSpotifyMain::selectTrack(track *newTrack) +{ + debug(tr("Selecting new track, trying to find it in the playlist")); + for (int i=0; i<m_ui.playList->topLevelItemCount(); ++i) { + QVariant data = m_ui.playList->topLevelItem(i)->data(0, Qt::UserRole); + track *t = data.value<track *>(); + if (t == newTrack) { + m_ui.playList->setCurrentItem(m_ui.playList->topLevelItem(i)); + stop(); + play(); + return; + } + } + // Playlist changed +} + void QtSpotifyMain::retrievePlayLists() { debug(tr("Retrieving playlists")); @@ -190,7 +231,7 @@ void QtSpotifyMain::play() track *t = data.value<track *>(); if (t != 0) { debug(tr("Playing '%1'").arg(t->title)); - if (!despotify_play(m_session, t, false)) { + if (!despotify_play(m_session, t, true)) { QMessageBox::information(this, tr("Error when playing track"), tr("Despotify error: %1").arg(QString::fromUtf8(m_session->last_error))); } else { @@ -284,11 +325,13 @@ void QtSpotifyMain::initPlayListHandlingState(QState *playListHandlingState) requestedRetrieve->assignProperty(m_ui.searchComboBox, "enabled", false); requestedRetrieve->assignProperty(m_ui.playList, "enabled", true); requestedRetrieve->assignProperty(m_ui.actionRetrieveStoredPlaylists, "enabled", false); + requestedRetrieve->assignProperty(statusBar(), "statusMessage", tr("Retrieving playlists")); connect(requestedRetrieve, SIGNAL(entered()), this, SLOT(retrievePlayLists())); requestedRetrieve->setObjectName("requestedRetrieve"); QState *playListSelected = new QState(playListHandlingState); playListSelected->setObjectName("playListSelected"); + playListSelected->assignProperty(statusBar(), "statusMessage", tr("Fetching playlist contents")); connect(playListSelected, SIGNAL(entered()), this, SLOT(selectPlayList())); idle->addTransition(m_ui.searchComboBox, SIGNAL(activated(QString)), @@ -394,7 +437,7 @@ static void callback(despotify_session *session, int signal, void *data, void *c { track *t = reinterpret_cast<track *>(data); QtSpotifyMain *qsm = reinterpret_cast<QtSpotifyMain*>(session->client_callback_data); - qsm->setNewTrack(t); + QCoreApplication::postEvent(qsm, new SelectTrackEvent(t)); } }; } diff --git a/qtspotifymain.h b/qtspotifymain.h index cfae6fc..179a3d3 100644 --- a/qtspotifymain.h +++ b/qtspotifymain.h @@ -74,13 +74,15 @@ public: QtSpotifyMain(QWidget *parent = 0); ~QtSpotifyMain(); - void setNewTrack(track *t); // Just a dummy so we can use the property mechanism to call the debug() function QString debuggingMessage() const { return QString(); } bool debugging() const { return m_debugging; } +protected: + bool event(QEvent *event); + private slots: void logIn(); void endSession(); @@ -111,6 +113,8 @@ private: void initPlayListHandlingState(QState *); void initPlayBackHandlingState(QState *); void initPlayingState(QState *); + void setNewTrack(track *t); + void selectTrack(track *t); Ui_QtSpotifyMain m_ui; despotify_session *m_session; |