Make the RadioModel's AddService method public, add a RemoveService method, and clean up the last.fm mess

This commit is contained in:
David Sansome 2011-01-12 20:18:17 +00:00
parent 0ba1962fc8
commit 03e6231483
5 changed files with 49 additions and 23 deletions

View File

@ -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<LastFMService>()
#endif
);

View File

@ -60,8 +60,10 @@ RadioModel::RadioModel(BackgroundThread<Database>* 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 ; i<invisibleRootItem()->rowCount() ; ++i) {
if (invisibleRootItem()->child(i)->data(Role_Service).value<RadioService*>()
== 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<RadioService*>(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<LastFMService>();
}
#endif
void RadioModel::ShowContextMenu(const QModelIndex& merged_model_index,
const QPoint& global_pos) {
RadioService* service = ServiceForIndex(merged_model_index);

View File

@ -104,16 +104,20 @@ public:
return static_cast<T*>(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<QString, RadioService*>* sServices;

View File

@ -55,7 +55,6 @@ public:
virtual void ReloadSettings() {}
// TODO: remove?
virtual QString Icon() { return QString(); }
signals:

View File

@ -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<LastFMService>(), 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<LastFMService>(), SIGNAL(ScrobblingEnabledChanged(bool)), SLOT(ScrobblingEnabledChanged(bool)));
connect(RadioModel::Service<LastFMService>(), SIGNAL(ButtonVisibilityChanged(bool)), SLOT(LastFMButtonVisibilityChanged(bool)));
#endif
connect(radio_model_->Service<MagnatuneService>(), 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<LastFMService>()->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<LastFMService>();
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<LastFMService>()->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<LastFMService>()->Love();
ui_->action_love->setEnabled(false);
}
#endif