From 03e623148364c9a125df8d364375a6d2a5a9411d Mon Sep 17 00:00:00 2001 From: David Sansome Date: Wed, 12 Jan 2011 20:18:17 +0000 Subject: [PATCH] Make the RadioModel's AddService method public, add a RemoveService method, and clean up the last.fm mess --- src/main.cpp | 2 +- src/radio/radiomodel.cpp | 37 ++++++++++++++++++++++++++++++------- src/radio/radiomodel.h | 18 +++++++++++------- src/radio/radioservice.h | 1 - src/ui/mainwindow.cpp | 14 +++++++------- 5 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0482f3148..ae1ba08cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -300,7 +300,7 @@ int main(int argc, char *argv[]) { RadioModel radio_model(database.get(), &task_manager, NULL); Player player(&playlists #ifdef HAVE_LIBLASTFM - ,radio_model.GetLastFMService() + ,RadioModel::Service() #endif ); diff --git a/src/radio/radiomodel.cpp b/src/radio/radiomodel.cpp index 59c848ec7..f026036bf 100644 --- a/src/radio/radiomodel.cpp +++ b/src/radio/radiomodel.cpp @@ -60,8 +60,10 @@ RadioModel::RadioModel(BackgroundThread* db_thread, void RadioModel::AddService(RadioService *service) { QStandardItem* root = service->CreateRootItem(); - if (!root) + if (!root) { + qWarning() << "Radio service" << service->name() << "did not return a root item"; return; + } root->setData(Type_Service, Role_Type); root->setData(QVariant::fromValue(service), Role_Service); @@ -74,6 +76,33 @@ void RadioModel::AddService(RadioService *service) { connect(service, SIGNAL(StreamMetadataFound(QUrl,Song)), SIGNAL(StreamMetadataFound(QUrl,Song))); connect(service, SIGNAL(OpenSettingsAtPage(SettingsDialog::Page)), SIGNAL(OpenSettingsAtPage(SettingsDialog::Page))); connect(service, SIGNAL(AddToPlaylistSignal(QMimeData*)), SIGNAL(AddToPlaylist(QMimeData*))); + connect(service, SIGNAL(destroyed()), SLOT(ServiceDeleted())); +} + +void RadioModel::RemoveService(RadioService* service) { + if (!sServices->contains(service->name())) + return; + + // Find and remove the root item that this service created + for (int i=0 ; irowCount() ; ++i) { + if (invisibleRootItem()->child(i)->data(Role_Service).value() + == service) { + invisibleRootItem()->removeRow(i); + break; + } + } + + // Remove the service from the list + sServices->remove(service->name()); + + // Disconnect the service + disconnect(service, 0, this, 0); +} + +void RadioModel::ServiceDeleted() { + RadioService* service = qobject_cast(sender()); + if (service) + RemoveService(service); } RadioService* RadioModel::ServiceByName(const QString& name) { @@ -160,12 +189,6 @@ QMimeData* RadioModel::mimeData(const QModelIndexList& indexes) const { return data; } -#ifdef HAVE_LIBLASTFM -LastFMService* RadioModel::GetLastFMService() const { - return Service(); -} -#endif - void RadioModel::ShowContextMenu(const QModelIndex& merged_model_index, const QPoint& global_pos) { RadioService* service = ServiceForIndex(merged_model_index); diff --git a/src/radio/radiomodel.h b/src/radio/radiomodel.h index fffabd7f4..9d7e224e3 100644 --- a/src/radio/radiomodel.h +++ b/src/radio/radiomodel.h @@ -104,16 +104,20 @@ public: return static_cast(ServiceByName(T::kServiceName)); } + // Add and remove services. Ownership is not transferred and the service + // is not reparented. If the service is deleted it will be automatically + // removed from the model. + void AddService(RadioService* service); + void RemoveService(RadioService* service); + + // Returns the service that is a parent of this item. Works by walking up + // the tree until it finds an item with Role_Service set. RadioService* ServiceForItem(const QStandardItem* item) const; RadioService* ServiceForIndex(const QModelIndex& index) const; + // Returns true if the given item has a PlayBehaviour other than None. bool IsPlayable(const QModelIndex& index) const; - // This is special because Player needs it for scrobbling -#ifdef HAVE_LIBLASTFM - LastFMService* GetLastFMService() const; -#endif - // QAbstractItemModel Qt::ItemFlags flags(const QModelIndex& index) const; QStringList mimeTypes() const; @@ -137,8 +141,8 @@ signals: void AddToPlaylist(QMimeData* data); -private: - void AddService(RadioService* service); +private slots: + void ServiceDeleted(); private: static QMap* sServices; diff --git a/src/radio/radioservice.h b/src/radio/radioservice.h index 4b1924317..4c93c7211 100644 --- a/src/radio/radioservice.h +++ b/src/radio/radioservice.h @@ -55,7 +55,6 @@ public: virtual void ReloadSettings() {} - // TODO: remove? virtual QString Icon() { return QString(); } signals: diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index a5e63cb01..6cdc98bb8 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -294,7 +294,7 @@ MainWindow::MainWindow( connect(ui_->action_stop_after_this_track, SIGNAL(triggered()), SLOT(StopAfterCurrent())); connect(ui_->action_mute, SIGNAL(triggered()), player_, SLOT(Mute())); #ifdef HAVE_LIBLASTFM - connect(ui_->action_ban, SIGNAL(triggered()), radio_model_->GetLastFMService(), SLOT(Ban())); + connect(ui_->action_ban, SIGNAL(triggered()), RadioModel::Service(), SLOT(Ban())); connect(ui_->action_love, SIGNAL(triggered()), SLOT(Love())); #endif connect(ui_->action_clear_playlist, SIGNAL(triggered()), playlists_, SLOT(ClearCurrent())); @@ -459,14 +459,14 @@ MainWindow::MainWindow( connect(radio_model_, SIGNAL(OpenSettingsAtPage(SettingsDialog::Page)), SLOT(OpenSettingsDialogAtPage(SettingsDialog::Page))); connect(radio_model_, SIGNAL(AddToPlaylist(QMimeData*)), SLOT(AddToPlaylist(QMimeData*))); #ifdef HAVE_LIBLASTFM - connect(radio_model_->GetLastFMService(), SIGNAL(ScrobblingEnabledChanged(bool)), SLOT(ScrobblingEnabledChanged(bool))); - connect(radio_model_->GetLastFMService(), SIGNAL(ButtonVisibilityChanged(bool)), SLOT(LastFMButtonVisibilityChanged(bool))); + connect(RadioModel::Service(), SIGNAL(ScrobblingEnabledChanged(bool)), SLOT(ScrobblingEnabledChanged(bool))); + connect(RadioModel::Service(), SIGNAL(ButtonVisibilityChanged(bool)), SLOT(LastFMButtonVisibilityChanged(bool))); #endif connect(radio_model_->Service(), SIGNAL(DownloadFinished(QStringList)), osd_, SLOT(MagnatuneDownloadFinished(QStringList))); connect(radio_view_->tree(), SIGNAL(AddToPlaylistSignal(QMimeData*)), SLOT(AddToPlaylist(QMimeData*))); #ifdef HAVE_LIBLASTFM - LastFMButtonVisibilityChanged(radio_model_->GetLastFMService()->AreButtonsVisible()); + LastFMButtonVisibilityChanged(RadioModel::Service()->AreButtonsVisible()); #else LastFMButtonVisibilityChanged(false); #endif @@ -720,7 +720,7 @@ void MainWindow::MediaPlaying() { #ifdef HAVE_LIBLASTFM bool is_lastfm = (player_->GetCurrentItem()->options() & PlaylistItem::LastFMControls); - LastFMService* lastfm = radio_model_->GetLastFMService(); + LastFMService* lastfm = RadioModel::Service(); ui_->action_ban->setEnabled(lastfm->IsScrobblingEnabled() && is_lastfm); ui_->action_love->setEnabled(lastfm->IsScrobblingEnabled()); @@ -883,7 +883,7 @@ void MainWindow::UpdateTrackPosition() { if (!playlists_->active()->has_scrobbled() && position >= playlists_->active()->scrobble_point()) { #ifdef HAVE_LIBLASTFM - radio_model_->GetLastFMService()->Scrobble(); + radio_model_->RadioModel::Service()->Scrobble(); #endif playlists_->active()->set_scrobbled(true); @@ -904,7 +904,7 @@ void MainWindow::UpdateTrackPosition() { #ifdef HAVE_LIBLASTFM void MainWindow::Love() { - radio_model_->GetLastFMService()->Love(); + RadioModel::Service()->Love(); ui_->action_love->setEnabled(false); } #endif