From 75a897e92dc71bc34a4106fe0d51a92c3137a6d6 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sun, 11 Mar 2012 14:44:43 +0000 Subject: [PATCH] Don't require each InternetService to keep track of its own current index - do it in the model instead --- src/internet/digitallyimportedservicebase.cpp | 9 +---- src/internet/digitallyimportedservicebase.h | 5 +-- src/internet/groovesharkservice.cpp | 35 ++++++++++--------- src/internet/groovesharkservice.h | 5 +-- src/internet/icecastservice.cpp | 17 +++------ src/internet/icecastservice.h | 6 +--- src/internet/internetmodel.cpp | 16 ++++++--- src/internet/internetmodel.h | 11 +++++- src/internet/internetservice.cpp | 6 ++-- src/internet/internetservice.h | 6 +--- src/internet/internetview.cpp | 3 +- src/internet/jamendoservice.cpp | 19 +++------- src/internet/jamendoservice.h | 6 +--- src/internet/lastfmservice.cpp | 17 ++++----- src/internet/lastfmservice.h | 6 +--- src/internet/magnatuneservice.cpp | 21 ++++------- src/internet/magnatuneservice.h | 6 +--- src/internet/savedradio.cpp | 30 ++++++++-------- src/internet/savedradio.h | 6 +--- src/internet/somafmservice.cpp | 7 +--- src/internet/somafmservice.h | 6 +--- src/internet/spotifyservice.cpp | 8 ++--- src/internet/spotifyservice.h | 6 +--- src/podcasts/podcastservice.cpp | 15 +++----- src/podcasts/podcastservice.h | 5 +-- 25 files changed, 103 insertions(+), 174 deletions(-) diff --git a/src/internet/digitallyimportedservicebase.cpp b/src/internet/digitallyimportedservicebase.cpp index 20d6b4246..d6ff96283 100644 --- a/src/internet/digitallyimportedservicebase.cpp +++ b/src/internet/digitallyimportedservicebase.cpp @@ -56,7 +56,6 @@ DigitallyImportedServiceBase::DigitallyImportedServiceBase( basic_audio_type_(1), premium_audio_type_(2), root_(NULL), - context_item_(NULL), saved_channels_(kSettingsGroup, api_service_name, kStreamsCacheDurationSecs), api_client_(new DigitallyImportedClient(api_service_name, this)) { @@ -171,8 +170,7 @@ void DigitallyImportedServiceBase::ReloadSettings() { saved_channels_.Load(); } -void DigitallyImportedServiceBase::ShowContextMenu( - const QModelIndex& index, const QPoint& global_pos) { +void DigitallyImportedServiceBase::ShowContextMenu(const QPoint& global_pos) { if (!context_menu_) { context_menu_.reset(new QMenu); context_menu_->addActions(GetPlaylistActions()); @@ -188,7 +186,6 @@ void DigitallyImportedServiceBase::ShowContextMenu( this, SLOT(ShowSettingsDialog())); } - context_item_ = model()->itemFromIndex(index); context_menu_->popup(global_pos); } @@ -244,10 +241,6 @@ void DigitallyImportedServiceBase::LoadStation(const QString& key) { connect(reply, SIGNAL(finished()), SLOT(LoadPlaylistFinished())); } -QModelIndex DigitallyImportedServiceBase::GetCurrentIndex() { - return context_item_->index(); -} - DigitallyImportedService::DigitallyImportedService( Application* app, InternetModel* model, QObject* parent) diff --git a/src/internet/digitallyimportedservicebase.h b/src/internet/digitallyimportedservicebase.h index e52e826cf..4abce9e47 100644 --- a/src/internet/digitallyimportedservicebase.h +++ b/src/internet/digitallyimportedservicebase.h @@ -50,7 +50,7 @@ public: QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* parent); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); void ReloadSettings(); @@ -72,9 +72,6 @@ public slots: signals: void StreamsChanged(); -protected: - QModelIndex GetCurrentIndex(); - private slots: void LoadPlaylistFinished(); void Homepage(); diff --git a/src/internet/groovesharkservice.cpp b/src/internet/groovesharkservice.cpp index 37c620f6b..9517b141c 100644 --- a/src/internet/groovesharkservice.cpp +++ b/src/internet/groovesharkservice.cpp @@ -428,7 +428,7 @@ void GroovesharkService::ResetSessionId() { s.setValue("sessionid", session_id_); } -void GroovesharkService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) { +void GroovesharkService::ShowContextMenu(const QPoint& global_pos) { EnsureMenuCreated(); // Check if we should display actions @@ -436,6 +436,8 @@ void GroovesharkService::ShowContextMenu(const QModelIndex& index, const QPoint& display_remove_from_playlist_action = false, display_remove_from_favorites_action = false; + QModelIndex index(model()->current_index()); + if (index.data(InternetModel::Role_Type).toInt() == InternetModel::Type_UserPlaylist && index.data(Role_PlaylistType).toInt() == UserPlaylist) { display_delete_playlist_action = true; @@ -457,11 +459,6 @@ void GroovesharkService::ShowContextMenu(const QModelIndex& index, const QPoint& remove_from_favorites_->setVisible(display_remove_from_favorites_action); context_menu_->popup(global_pos); - context_item_ = index; -} - -QModelIndex GroovesharkService::GetCurrentIndex() { - return context_item_; } void GroovesharkService::UpdateTotalSongCount(int count) { @@ -1153,12 +1150,12 @@ void GroovesharkService::NewPlaylistCreated(QNetworkReply* reply, const QString& } void GroovesharkService::DeleteCurrentPlaylist() { - if (context_item_.data(InternetModel::Role_Type).toInt() != + if (model()->current_index().data(InternetModel::Role_Type).toInt() != InternetModel::Type_UserPlaylist) { return; } - int playlist_id = context_item_.data(Role_UserPlaylistId).toInt(); + int playlist_id = model()->current_index().data(Role_UserPlaylistId).toInt(); DeletePlaylist(playlist_id); } @@ -1197,12 +1194,14 @@ void GroovesharkService::PlaylistDeleted(QNetworkReply* reply, int playlist_id) } void GroovesharkService::RenameCurrentPlaylist() { - if (context_item_.data(InternetModel::Role_Type).toInt() != InternetModel::Type_UserPlaylist - || context_item_.data(Role_PlaylistType).toInt() != UserPlaylist) { + const QModelIndex& index(model()->current_index()); + + if (index.data(InternetModel::Role_Type).toInt() != InternetModel::Type_UserPlaylist + || index.data(Role_PlaylistType).toInt() != UserPlaylist) { return; } - int playlist_id = context_item_.data(Role_UserPlaylistId).toInt(); + const int playlist_id = index.data(Role_UserPlaylistId).toInt(); RenamePlaylist(playlist_id); } @@ -1269,13 +1268,15 @@ void GroovesharkService::UserFavoriteSongAdded(QNetworkReply* reply, int task_id } void GroovesharkService::RemoveCurrentFromPlaylist() { - if (context_item_.parent().data(InternetModel::Role_Type).toInt() != + const QModelIndex& index(model()->current_index()); + + if (index.parent().data(InternetModel::Role_Type).toInt() != InternetModel::Type_UserPlaylist) { return; } - int playlist_id = context_item_.data(Role_UserPlaylistId).toInt(); - int song_id = ExtractSongId(context_item_.data(InternetModel::Role_Url).toUrl()); + int playlist_id = index.data(Role_UserPlaylistId).toInt(); + int song_id = ExtractSongId(index.data(InternetModel::Role_Url).toUrl()); if (song_id) { RemoveFromPlaylist(playlist_id, song_id); } @@ -1293,11 +1294,13 @@ void GroovesharkService::RemoveFromPlaylist(int playlist_id, int song_id) { } void GroovesharkService::RemoveCurrentFromFavorites() { - if (context_item_.parent().data(Role_PlaylistType).toInt() != UserFavorites) { + const QModelIndex& index(model()->current_index()); + + if (index.parent().data(Role_PlaylistType).toInt() != UserFavorites) { return; } - int song_id = ExtractSongId(context_item_.data(InternetModel::Role_Url).toUrl()); + int song_id = ExtractSongId(index.data(InternetModel::Role_Url).toUrl()); if (song_id) { RemoveFromFavorites(song_id); } diff --git a/src/internet/groovesharkservice.h b/src/internet/groovesharkservice.h index 69a7c3702..e799891d4 100644 --- a/src/internet/groovesharkservice.h +++ b/src/internet/groovesharkservice.h @@ -72,7 +72,7 @@ class GroovesharkService : public InternetService { smart_playlists::GeneratorPtr CreateGenerator(QStandardItem* item); void DropMimeData(const QMimeData* data, const QModelIndex& index); QList playlistitem_actions(const Song& song); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); void Search(const QString& text, Playlist* playlist, bool now = false); // User should be logged in to be able to generate streaming urls @@ -128,8 +128,6 @@ class GroovesharkService : public InternetService { void ShowConfig(); protected: - QModelIndex GetCurrentIndex(); - struct PlaylistInfo { PlaylistInfo() {} PlaylistInfo(int id, QString name, QStandardItem* item) @@ -245,7 +243,6 @@ class GroovesharkService : public InternetService { NetworkAccessManager* network_; QMenu* context_menu_; - QModelIndex context_item_; int current_song_id_; QAction* create_playlist_; diff --git a/src/internet/icecastservice.cpp b/src/internet/icecastservice.cpp index 9d696bb57..43b860e4a 100644 --- a/src/internet/icecastservice.cpp +++ b/src/internet/icecastservice.cpp @@ -273,17 +273,12 @@ QWidget* IcecastService::HeaderWidget() const { return filter_; } -void IcecastService::ShowContextMenu(const QModelIndex& index, - const QPoint& global_pos) { +void IcecastService::ShowContextMenu(const QPoint& global_pos) { EnsureMenuCreated(); - if (index.model() == model_) - context_item_ = index; - else - context_item_ = QModelIndex(); - - const bool can_play = context_item_.isValid() && - model_->GetSong(context_item_).is_valid(); + const bool can_play = model()->current_index().isValid() && + model()->current_index().model() == model_ && + model_->GetSong(model()->current_index()).is_valid(); GetAppendToPlaylistAction()->setEnabled(can_play); GetReplacePlaylistAction()->setEnabled(can_play); @@ -308,7 +303,3 @@ void IcecastService::EnsureMenuCreated() { void IcecastService::Homepage() { QDesktopServices::openUrl(QUrl(kHomepage)); } - -QModelIndex IcecastService::GetCurrentIndex() { - return context_item_; -} diff --git a/src/internet/icecastservice.h b/src/internet/icecastservice.h index fb0748f96..a062d1bf2 100644 --- a/src/internet/icecastservice.h +++ b/src/internet/icecastservice.h @@ -48,13 +48,10 @@ public: QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); QWidget* HeaderWidget() const; -protected: - QModelIndex GetCurrentIndex(); - private slots: void LoadDirectory(); void Homepage(); @@ -70,7 +67,6 @@ private: QStandardItem* root_; NetworkAccessManager* network_; QMenu* context_menu_; - QModelIndex context_item_; IcecastBackend* backend_; IcecastModel* model_; diff --git a/src/internet/internetmodel.cpp b/src/internet/internetmodel.cpp index ed39eabf3..c666ec558 100644 --- a/src/internet/internetmodel.cpp +++ b/src/internet/internetmodel.cpp @@ -255,11 +255,19 @@ bool InternetModel::dropMimeData(const QMimeData* data, Qt::DropAction action, i return true; } -void InternetModel::ShowContextMenu(const QModelIndex& merged_model_index, - const QPoint& global_pos) { - InternetService* service = ServiceForIndex(merged_model_index); +void InternetModel::ShowContextMenu(const QModelIndexList& selected_merged_model_indexes, + const QModelIndex& current_merged_model_index, + const QPoint& global_pos) { + current_index_ = merged_model_->mapToSource(current_merged_model_index); + + selected_indexes_.clear(); + foreach (const QModelIndex& index, selected_merged_model_indexes) { + selected_indexes_ << merged_model_->mapToSource(index); + } + + InternetService* service = ServiceForIndex(current_merged_model_index); if (service) - service->ShowContextMenu(merged_model_->mapToSource(merged_model_index), global_pos); + service->ShowContextMenu(global_pos); } void InternetModel::ReloadSettings() { diff --git a/src/internet/internetmodel.h b/src/internet/internetmodel.h index 508ad507f..7b9d1c782 100644 --- a/src/internet/internetmodel.h +++ b/src/internet/internetmodel.h @@ -141,13 +141,17 @@ public: bool hasChildren(const QModelIndex& parent) const; int rowCount(const QModelIndex& parent) const; - void ShowContextMenu(const QModelIndex& merged_model_index, + void ShowContextMenu(const QModelIndexList& selected_merged_model_indexes, + const QModelIndex& current_merged_model_index, const QPoint& global_pos); void ReloadSettings(); Application* app() const { return app_; } MergedProxyModel* merged_model() const { return merged_model_; } + const QModelIndex& current_index() const { return current_index_; } + const QModelIndexList& selected_indexes() const { return selected_indexes_; } + signals: void StreamError(const QString& message); void StreamMetadataFound(const QUrl& original_url, const Song& song); @@ -162,6 +166,11 @@ private: Application* app_; MergedProxyModel* merged_model_; + + // Set when a context menu is requested, can be accessed by context menu + // actions to do things to the current item. + QModelIndexList selected_indexes_; + QModelIndex current_index_; }; #endif // INTERNETMODEL_H diff --git a/src/internet/internetservice.cpp b/src/internet/internetservice.cpp index d0b5e2cf2..8adfe6a45 100644 --- a/src/internet/internetservice.cpp +++ b/src/internet/internetservice.cpp @@ -94,13 +94,13 @@ void InternetService::AddItemsToPlaylist(const QModelIndexList& indexes, AddMode } void InternetService::AppendToPlaylist() { - AddItemToPlaylist(GetCurrentIndex(), AddMode_Append); + AddItemsToPlaylist(model()->selected_indexes(), AddMode_Append); } void InternetService::ReplacePlaylist() { - AddItemToPlaylist(GetCurrentIndex(), AddMode_Replace); + AddItemsToPlaylist(model()->selected_indexes(), AddMode_Replace); } void InternetService::OpenInNewPlaylist() { - AddItemToPlaylist(GetCurrentIndex(), AddMode_OpenInNew); + AddItemsToPlaylist(model()->selected_indexes(), AddMode_OpenInNew); } diff --git a/src/internet/internetservice.h b/src/internet/internetservice.h index fee98dd6e..ef97105f1 100644 --- a/src/internet/internetservice.h +++ b/src/internet/internetservice.h @@ -48,7 +48,7 @@ public: virtual QStandardItem* CreateRootItem() = 0; virtual void LazyPopulate(QStandardItem* parent) = 0; - virtual void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) {} + virtual void ShowContextMenu(const QPoint& global_pos) {} virtual void ItemDoubleClicked(QStandardItem* item) {} // Create a generator for smart playlists virtual smart_playlists::GeneratorPtr CreateGenerator(QStandardItem* item) { return smart_playlists::GeneratorPtr(); } @@ -77,10 +77,6 @@ private slots: void OpenInNewPlaylist(); protected: - // Subclass provides the currently selected QModelIndex on InternetService's - // request. - virtual QModelIndex GetCurrentIndex() = 0; - // Returns all the playlist insertion related QActions (see below). QList GetPlaylistActions(); diff --git a/src/internet/internetview.cpp b/src/internet/internetview.cpp index 56f22c68d..9a8c94b49 100644 --- a/src/internet/internetview.cpp +++ b/src/internet/internetview.cpp @@ -39,7 +39,8 @@ void InternetView::contextMenuEvent(QContextMenuEvent* e) { MergedProxyModel* merged_model = static_cast(model()); InternetModel* internet_model = static_cast(merged_model->sourceModel()); - internet_model->ShowContextMenu(index, e->globalPos()); + internet_model->ShowContextMenu(selectionModel()->selectedRows(), index, + e->globalPos()); } void InternetView::currentChanged(const QModelIndex ¤t, const QModelIndex&) { diff --git a/src/internet/jamendoservice.cpp b/src/internet/jamendoservice.cpp index bf1efeca7..6c9fd64a7 100644 --- a/src/internet/jamendoservice.cpp +++ b/src/internet/jamendoservice.cpp @@ -427,16 +427,11 @@ void JamendoService::EnsureMenuCreated() { } } -void JamendoService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) { +void JamendoService::ShowContextMenu(const QPoint& global_pos) { EnsureMenuCreated(); - if (index.model() == library_sort_model_) { - context_item_ = index; - } else { - context_item_ = QModelIndex(); - } - - const bool enabled = accepted_download_ && context_item_.isValid(); + const bool enabled = accepted_download_ && + model()->current_index().model() == library_sort_model_; //make menu items visible and enabled only when needed GetAppendToPlaylistAction()->setVisible(accepted_download_); @@ -458,13 +453,9 @@ QWidget* JamendoService::HeaderWidget() const { return library_filter_; } -QModelIndex JamendoService::GetCurrentIndex() { - return context_item_; -} - void JamendoService::AlbumInfo() { SongList songs(library_model_->GetChildSongs( - library_sort_model_->mapToSource(context_item_))); + library_sort_model_->mapToSource(model()->current_index()))); if (songs.isEmpty()) return; @@ -478,7 +469,7 @@ void JamendoService::AlbumInfo() { void JamendoService::DownloadAlbum() { SongList songs(library_model_->GetChildSongs( - library_sort_model_->mapToSource(context_item_))); + library_sort_model_->mapToSource(model()->current_index()))); if (songs.isEmpty()) return; diff --git a/src/internet/jamendoservice.h b/src/internet/jamendoservice.h index e0f3b0f2f..10a8ea434 100644 --- a/src/internet/jamendoservice.h +++ b/src/internet/jamendoservice.h @@ -41,7 +41,7 @@ class JamendoService : public InternetService { QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); QWidget* HeaderWidget() const; @@ -66,9 +66,6 @@ class JamendoService : public InternetService { static const int kBatchSize; static const int kApproxDatabaseSize; - protected: - QModelIndex GetCurrentIndex(); - private: void ParseDirectory(QIODevice* device) const; @@ -102,7 +99,6 @@ class JamendoService : public InternetService { NetworkAccessManager* network_; QMenu* context_menu_; - QModelIndex context_item_; QAction* album_info_; QAction* download_album_; diff --git a/src/internet/lastfmservice.cpp b/src/internet/lastfmservice.cpp index fe334c604..0af6f4124 100644 --- a/src/internet/lastfmservice.cpp +++ b/src/internet/lastfmservice.cpp @@ -581,10 +581,8 @@ void LastFMService::Ban() { app_->player()->Next(); } -void LastFMService::ShowContextMenu(const QModelIndex& index, const QPoint &global_pos) { - context_item_ = model()->itemFromIndex(index); - - switch (index.parent().data(InternetModel::Role_Type).toInt()) { +void LastFMService::ShowContextMenu(const QPoint& global_pos) { + switch (model()->current_index().parent().data(InternetModel::Role_Type).toInt()) { case Type_Artists: case Type_Tags: case Type_Custom: @@ -596,7 +594,7 @@ void LastFMService::ShowContextMenu(const QModelIndex& index, const QPoint &glob break; } - const bool playable = model()->IsPlayable(index); + const bool playable = model()->IsPlayable(model()->current_index()); GetAppendToPlaylistAction()->setEnabled(playable); GetReplacePlaylistAction()->setEnabled(playable); GetOpenInNewPlaylistAction()->setEnabled(playable); @@ -753,10 +751,6 @@ void LastFMService::RefreshNeighboursFinished() { } } -QModelIndex LastFMService::GetCurrentIndex() { - return context_item_->index(); -} - void LastFMService::AddArtistRadio() { AddArtistOrTag("artists", LastFMStationDialog::Artist, kUrlArtist, tr(kTitleArtist), @@ -855,9 +849,10 @@ void LastFMService::RestoreList(const QString& name, } void LastFMService::Remove() { - int type = context_item_->parent()->data(InternetModel::Role_Type).toInt(); + QStandardItem* context_item = model()->itemFromIndex(model()->current_index()); + int type = context_item->parent()->data(InternetModel::Role_Type).toInt(); - context_item_->parent()->removeRow(context_item_->row()); + context_item->parent()->removeRow(context_item->row()); if (type == Type_Artists) SaveList("artists", artist_list_); diff --git a/src/internet/lastfmservice.h b/src/internet/lastfmservice.h index 78d4a0046..8ed35a7c8 100644 --- a/src/internet/lastfmservice.h +++ b/src/internet/lastfmservice.h @@ -87,7 +87,7 @@ class LastFMService : public InternetService { QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* parent); - void ShowContextMenu(const QModelIndex& index, const QPoint &global_pos); + void ShowContextMenu(const QPoint &global_pos); PlaylistItem::Options playlistitem_options() const; @@ -138,9 +138,6 @@ class LastFMService : public InternetService { void SavedItemsChanged(); - protected: - QModelIndex GetCurrentIndex(); - private slots: void AuthenticateReplyFinished(); void UpdateSubscriberStatusFinished(); @@ -207,7 +204,6 @@ class LastFMService : public InternetService { QAction* add_tag_action_; QAction* add_custom_action_; QAction* refresh_friends_action_; - QStandardItem* context_item_; QUrl last_url_; bool initial_tune_; diff --git a/src/internet/magnatuneservice.cpp b/src/internet/magnatuneservice.cpp index 9354077b8..d12a0aab1 100644 --- a/src/internet/magnatuneservice.cpp +++ b/src/internet/magnatuneservice.cpp @@ -288,25 +288,18 @@ void MagnatuneService::EnsureMenuCreated() { context_menu_->addMenu(library_filter_->menu()); } -void MagnatuneService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) { +void MagnatuneService::ShowContextMenu(const QPoint& global_pos) { EnsureMenuCreated(); - if (index.model() == library_sort_model_) - context_item_ = index; - else - context_item_ = QModelIndex(); + const bool is_valid = model()->current_index().model() == library_sort_model_; - GetAppendToPlaylistAction()->setEnabled(context_item_.isValid()); - GetReplacePlaylistAction()->setEnabled(context_item_.isValid()); - GetOpenInNewPlaylistAction()->setEnabled(context_item_.isValid()); - download_->setEnabled(context_item_.isValid() && membership_ == Membership_Download); + GetAppendToPlaylistAction()->setEnabled(is_valid); + GetReplacePlaylistAction()->setEnabled(is_valid); + GetOpenInNewPlaylistAction()->setEnabled(is_valid); + download_->setEnabled(is_valid && membership_ == Membership_Download); context_menu_->popup(global_pos); } -QModelIndex MagnatuneService::GetCurrentIndex() { - return context_item_; -} - void MagnatuneService::Homepage() { QDesktopServices::openUrl(QUrl(kHomepage)); } @@ -345,7 +338,7 @@ void MagnatuneService::ShowConfig() { } void MagnatuneService::Download() { - QModelIndex index = library_sort_model_->mapToSource(context_item_); + QModelIndex index = library_sort_model_->mapToSource(model()->current_index()); SongList songs = library_model_->GetChildSongs(index); MagnatuneDownloadDialog* dialog = new MagnatuneDownloadDialog(this, 0); diff --git a/src/internet/magnatuneservice.h b/src/internet/magnatuneservice.h index 97da2239d..bb70a6d94 100644 --- a/src/internet/magnatuneservice.h +++ b/src/internet/magnatuneservice.h @@ -71,7 +71,7 @@ class MagnatuneService : public InternetService { QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); QWidget* HeaderWidget() const; @@ -89,9 +89,6 @@ class MagnatuneService : public InternetService { signals: void DownloadFinished(const QStringList& albums); - protected: - QModelIndex GetCurrentIndex(); - private slots: void UpdateTotalSongCount(int count); void ReloadDatabase(); @@ -110,7 +107,6 @@ class MagnatuneService : public InternetService { MagnatuneUrlHandler* url_handler_; QMenu* context_menu_; - QModelIndex context_item_; QStandardItem* root_; QAction* download_; diff --git a/src/internet/savedradio.cpp b/src/internet/savedradio.cpp index 9489d60e3..0b4e22d91 100644 --- a/src/internet/savedradio.cpp +++ b/src/internet/savedradio.cpp @@ -93,8 +93,7 @@ void SavedRadio::SaveStreams() { emit StreamsChanged(); } -void SavedRadio::ShowContextMenu(const QModelIndex& index, - const QPoint& global_pos) { +void SavedRadio::ShowContextMenu(const QPoint& global_pos) { if (!context_menu_) { context_menu_ = new QMenu; context_menu_->addActions(GetPlaylistActions()); @@ -104,8 +103,9 @@ void SavedRadio::ShowContextMenu(const QModelIndex& index, context_menu_->addAction(IconLoader::Load("document-open-remote"), tr("Add another stream..."), this, SIGNAL(ShowAddStreamDialog())); } - context_item_ = model()->itemFromIndex(index); - const bool is_root = index.data(InternetModel::Role_Type).toInt() == InternetModel::Type_Service; + const bool is_root = + model()->current_index().data(InternetModel::Role_Type).toInt() == + InternetModel::Type_Service; GetAppendToPlaylistAction()->setEnabled(!is_root); GetReplacePlaylistAction()->setEnabled(!is_root); @@ -117,37 +117,37 @@ void SavedRadio::ShowContextMenu(const QModelIndex& index, } void SavedRadio::Remove() { - streams_.removeAll(Stream(QUrl(context_item_->data(InternetModel::Role_Url).toUrl()))); - context_item_->parent()->removeRow(context_item_->row()); + QStandardItem* context_item = model()->itemFromIndex(model()->current_index()); + + streams_.removeAll(Stream(QUrl(context_item->data(InternetModel::Role_Url).toUrl()))); + context_item->parent()->removeRow(context_item->row()); SaveStreams(); } void SavedRadio::Edit() { + QStandardItem* context_item = model()->itemFromIndex(model()->current_index()); + if (!edit_dialog_) { edit_dialog_.reset(new AddStreamDialog); edit_dialog_->set_save_visible(false); } - edit_dialog_->set_name(context_item_->text()); - edit_dialog_->set_url(context_item_->data(InternetModel::Role_Url).toUrl()); + edit_dialog_->set_name(context_item->text()); + edit_dialog_->set_url(context_item->data(InternetModel::Role_Url).toUrl()); if (edit_dialog_->exec() == QDialog::Rejected) return; - int i = streams_.indexOf(Stream(QUrl(context_item_->data(InternetModel::Role_Url).toUrl()))); + int i = streams_.indexOf(Stream(QUrl(context_item->data(InternetModel::Role_Url).toUrl()))); Stream& stream = streams_[i]; stream.name_ = edit_dialog_->name(); stream.url_ = edit_dialog_->url(); - context_item_->setText(stream.name_); - context_item_->setData(stream.url_, InternetModel::Role_Url); + context_item->setText(stream.name_); + context_item->setData(stream.url_, InternetModel::Role_Url); SaveStreams(); } -QModelIndex SavedRadio::GetCurrentIndex() { - return context_item_->index(); -} - void SavedRadio::AddStreamToList(const Stream& stream, QStandardItem* parent) { QStandardItem* s = new QStandardItem(QIcon(":last.fm/icon_radio.png"), stream.name_); s->setData(stream.url_, InternetModel::Role_Url); diff --git a/src/internet/savedradio.h b/src/internet/savedradio.h index 7edac16d6..d3a1d9ec6 100644 --- a/src/internet/savedradio.h +++ b/src/internet/savedradio.h @@ -55,7 +55,7 @@ class SavedRadio : public InternetService { QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); void Add(const QUrl& url, const QString& name = QString()); @@ -65,9 +65,6 @@ class SavedRadio : public InternetService { void ShowAddStreamDialog(); void StreamsChanged(); - protected: - QModelIndex GetCurrentIndex(); - private slots: void Remove(); void Edit(); @@ -79,7 +76,6 @@ class SavedRadio : public InternetService { private: QMenu* context_menu_; - QStandardItem* context_item_; QStandardItem* root_; QAction* remove_action_; diff --git a/src/internet/somafmservice.cpp b/src/internet/somafmservice.cpp index 58b6b438d..5f4cc9e42 100644 --- a/src/internet/somafmservice.cpp +++ b/src/internet/somafmservice.cpp @@ -79,7 +79,7 @@ void SomaFMService::LazyPopulate(QStandardItem* item) { } } -void SomaFMService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) { +void SomaFMService::ShowContextMenu(const QPoint& global_pos) { if (!context_menu_) { context_menu_ = new QMenu; context_menu_->addActions(GetPlaylistActions()); @@ -87,7 +87,6 @@ void SomaFMService::ShowContextMenu(const QModelIndex& index, const QPoint& glob context_menu_->addAction(IconLoader::Load("view-refresh"), tr("Refresh channels"), this, SLOT(RefreshStreams())); } - context_item_ = model()->itemFromIndex(index); context_menu_->popup(global_pos); } @@ -176,10 +175,6 @@ void SomaFMService::Homepage() { QDesktopServices::openUrl(QUrl(kHomepage)); } -QModelIndex SomaFMService::GetCurrentIndex() { - return context_item_->index(); -} - PlaylistItem::Options SomaFMService::playlistitem_options() const { return PlaylistItem::PauseDisabled; } diff --git a/src/internet/somafmservice.h b/src/internet/somafmservice.h index 8583ce264..12c3f509e 100644 --- a/src/internet/somafmservice.h +++ b/src/internet/somafmservice.h @@ -57,7 +57,7 @@ public: QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* item); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); PlaylistItem::Options playlistitem_options() const; QNetworkAccessManager* network() const { return network_; } @@ -70,9 +70,6 @@ public: signals: void StreamsChanged(); -protected: - QModelIndex GetCurrentIndex(); - private slots: void ForceRefreshStreams(); void RefreshStreams(); @@ -89,7 +86,6 @@ private: QStandardItem* root_; QMenu* context_menu_; - QStandardItem* context_item_; QNetworkAccessManager* network_; diff --git a/src/internet/spotifyservice.cpp b/src/internet/spotifyservice.cpp index 642032a5f..0c4a5d9fd 100644 --- a/src/internet/spotifyservice.cpp +++ b/src/internet/spotifyservice.cpp @@ -126,10 +126,6 @@ void SpotifyService::LazyPopulate(QStandardItem* item) { return; } -QModelIndex SpotifyService::GetCurrentIndex() { - return QModelIndex(); -} - void SpotifyService::Login(const QString& username, const QString& password) { Logout(); EnsureServerCreated(username, password); @@ -584,9 +580,9 @@ SpotifyServer* SpotifyService::server() const { return server_; } -void SpotifyService::ShowContextMenu(const QModelIndex& index, const QPoint& global_pos) { +void SpotifyService::ShowContextMenu(const QPoint& global_pos) { EnsureMenuCreated(); - QStandardItem* item = model()->itemFromIndex(index); + QStandardItem* item = model()->itemFromIndex(model()->current_index()); if (item) { int type = item->data(InternetModel::Role_Type).toInt(); if (type == Type_InboxPlaylist || diff --git a/src/internet/spotifyservice.h b/src/internet/spotifyservice.h index 68ce7f9e3..34944b2a2 100644 --- a/src/internet/spotifyservice.h +++ b/src/internet/spotifyservice.h @@ -52,7 +52,7 @@ public: QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* parent); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); void ItemDoubleClicked(QStandardItem* item); void DropMimeData(const QMimeData* data, const QModelIndex& index); PlaylistItem::Options playlistitem_options() const; @@ -81,9 +81,6 @@ signals: public slots: void ShowConfig(); -protected: - virtual QModelIndex GetCurrentIndex(); - private: void StartBlobProcess(); void FillPlaylist(QStandardItem* item, const pb::spotify::LoadPlaylistResponse& response); @@ -132,7 +129,6 @@ private: QMenu* context_menu_; QMenu* playlist_context_menu_; QAction* playlist_sync_action_; - QModelIndex context_item_; QTimer* search_delay_; diff --git a/src/podcasts/podcastservice.cpp b/src/podcasts/podcastservice.cpp index 992276af6..c25abf349 100644 --- a/src/podcasts/podcastservice.cpp +++ b/src/podcasts/podcastservice.cpp @@ -249,8 +249,7 @@ QStandardItem* PodcastService::CreatePodcastEpisodeItem(const PodcastEpisode& ep return item; } -void PodcastService::ShowContextMenu(const QModelIndex& index, - const QPoint& global_pos) { +void PodcastService::ShowContextMenu(const QPoint& global_pos) { if (!context_menu_) { context_menu_ = new QMenu; context_menu_->addAction( @@ -277,16 +276,16 @@ void PodcastService::ShowContextMenu(const QModelIndex& index, this, SLOT(ShowConfig())); } - current_index_ = index; + current_index_ = model()->current_index(); bool is_episode = false; - switch (index.data(InternetModel::Role_Type).toInt()) { + switch (current_index_.data(InternetModel::Role_Type).toInt()) { case Type_Podcast: - current_podcast_index_ = index; + current_podcast_index_ = current_index_; break; case Type_Episode: - current_podcast_index_ = index.parent(); + current_podcast_index_ = current_index_.parent(); is_episode = true; break; @@ -324,10 +323,6 @@ void PodcastService::ReloadSettings() { // TODO: reload the podcast icons that are already loaded? } -QModelIndex PodcastService::GetCurrentIndex() { - return current_index_; -} - void PodcastService::AddPodcast() { if (!add_podcast_dialog_) { add_podcast_dialog_.reset(new AddPodcastDialog(app_)); diff --git a/src/podcasts/podcastservice.h b/src/podcasts/podcastservice.h index a573787b0..20c5e4f19 100644 --- a/src/podcasts/podcastservice.h +++ b/src/podcasts/podcastservice.h @@ -56,12 +56,9 @@ public: QStandardItem* CreateRootItem(); void LazyPopulate(QStandardItem* parent); - void ShowContextMenu(const QModelIndex& index, const QPoint& global_pos); + void ShowContextMenu(const QPoint& global_pos); void ReloadSettings(); -protected: - QModelIndex GetCurrentIndex(); - private slots: void AddPodcast(); void UpdateSelectedPodcast();