cloudfileservices: Refactor context menu code

Add PopulateContextMenu, called once to populate the menu items, and
UpdateContextMenu, called ever time a menu is shown, to allow
service-specific behavior.
This commit is contained in:
Jim Broadus 2021-02-24 22:30:47 -08:00 committed by John Maguire
parent b57e66dd51
commit c87409c896
4 changed files with 40 additions and 33 deletions

View File

@ -97,18 +97,22 @@ void CloudFileService::LazyPopulate(QStandardItem* item) {
}
}
void CloudFileService::PopulateContextMenu() {
context_menu_->addActions(GetPlaylistActions());
context_menu_->addAction(IconLoader::Load("download", IconLoader::Base),
tr("Cover Manager"), this, SLOT(ShowCoverManager()));
context_menu_->addSeparator();
context_menu_->addAction(IconLoader::Load("configure", IconLoader::Base),
tr("Configure..."), this,
SLOT(ShowSettingsDialog()));
}
void CloudFileService::ShowContextMenu(const QPoint& global_pos) {
if (!context_menu_) {
context_menu_.reset(new QMenu);
context_menu_->addActions(GetPlaylistActions());
context_menu_->addAction(IconLoader::Load("download", IconLoader::Base),
tr("Cover Manager"), this,
SLOT(ShowCoverManager()));
context_menu_->addSeparator();
context_menu_->addAction(IconLoader::Load("configure", IconLoader::Base),
tr("Configure..."), this,
SLOT(ShowSettingsDialog()));
PopulateContextMenu();
}
UpdateContextMenu();
context_menu_->popup(global_pos);
}

View File

@ -67,6 +67,11 @@ class CloudFileService : public InternetService {
QString GuessMimeTypeForFile(const QString& filename) const;
void AbortReadTagsReplies();
// Called once when context menu is created
virtual void PopulateContextMenu();
// Called every time context menu is shown
virtual void UpdateContextMenu(){};
protected slots:
void ShowCoverManager();
void AddToPlaylist(QMimeData* mime);

View File

@ -210,29 +210,27 @@ QUrl GoogleDriveService::GetStreamingUrlFromSongId(const QString& id) {
return QUrl(response->file().download_url());
}
void GoogleDriveService::ShowContextMenu(const QPoint& global_pos) {
if (!context_menu_) {
context_menu_.reset(new QMenu);
context_menu_->addActions(GetPlaylistActions());
open_in_drive_action_ = context_menu_->addAction(
IconLoader::Load("googledrive", IconLoader::Provider),
tr("Open in Google Drive"), this, SLOT(OpenWithDrive()));
context_menu_->addSeparator();
update_action_ = context_menu_->addAction(
IconLoader::Load("view-refresh", IconLoader::Base),
tr("Check for updates"), this, SLOT(CheckForUpdates()));
full_rescan_action_ = context_menu_->addAction(
IconLoader::Load("view-refresh", IconLoader::Base),
tr("Do a full rescan..."), this, SLOT(FullRescanRequested()));
context_menu_->addSeparator();
context_menu_->addAction(IconLoader::Load("download", IconLoader::Base),
tr("Cover Manager"), this,
SLOT(ShowCoverManager()));
context_menu_->addAction(IconLoader::Load("configure", IconLoader::Base),
tr("Configure..."), this,
SLOT(ShowSettingsDialog()));
}
void GoogleDriveService::PopulateContextMenu() {
context_menu_->addActions(GetPlaylistActions());
open_in_drive_action_ = context_menu_->addAction(
IconLoader::Load("googledrive", IconLoader::Provider),
tr("Open in Google Drive"), this, SLOT(OpenWithDrive()));
context_menu_->addSeparator();
update_action_ = context_menu_->addAction(
IconLoader::Load("view-refresh", IconLoader::Base),
tr("Check for updates"), this, SLOT(CheckForUpdates()));
full_rescan_action_ = context_menu_->addAction(
IconLoader::Load("view-refresh", IconLoader::Base),
tr("Do a full rescan..."), this, SLOT(FullRescanRequested()));
context_menu_->addSeparator();
context_menu_->addAction(IconLoader::Load("download", IconLoader::Base),
tr("Cover Manager"), this, SLOT(ShowCoverManager()));
context_menu_->addAction(IconLoader::Load("configure", IconLoader::Base),
tr("Configure..."), this,
SLOT(ShowSettingsDialog()));
}
void GoogleDriveService::UpdateContextMenu() {
// Only show some actions if there are real songs selected
bool songs_selected = false;
for (const QModelIndex& index : model()->selected_indexes()) {
@ -246,8 +244,6 @@ void GoogleDriveService::ShowContextMenu(const QPoint& global_pos) {
open_in_drive_action_->setEnabled(songs_selected);
update_action_->setEnabled(!is_indexing());
full_rescan_action_->setEnabled(!is_indexing());
context_menu_->popup(global_pos);
}
void GoogleDriveService::OpenWithDrive() {

View File

@ -40,7 +40,6 @@ class GoogleDriveService : public CloudFileService {
static const char* kSettingsGroup;
virtual bool has_credentials() const;
virtual void ShowContextMenu(const QPoint& global_pos);
google_drive::Client* client() const { return client_; }
QString refresh_token() const;
@ -70,6 +69,9 @@ class GoogleDriveService : public CloudFileService {
void RefreshAuthorisation(const QString& refresh_token);
void ListChanges(const QString& cursor);
void PopulateContextMenu() override;
void UpdateContextMenu() override;
google_drive::Client* client_;
QAction* open_in_drive_action_;