2020-05-08 18:35:36 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2020-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2020-05-08 18:35:36 +02:00
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QList>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QListWidget>
|
|
|
|
#include <QListWidgetItem>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
#include "settingsdialog.h"
|
|
|
|
#include "lyricssettingspage.h"
|
|
|
|
#include "ui_lyricssettingspage.h"
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/iconloader.h"
|
|
|
|
#include "lyrics/lyricsproviders.h"
|
2020-05-08 18:44:07 +02:00
|
|
|
#include "lyrics/lyricsprovider.h"
|
2020-05-08 18:35:36 +02:00
|
|
|
#include "widgets/loginstatewidget.h"
|
|
|
|
|
|
|
|
const char *LyricsSettingsPage::kSettingsGroup = "Lyrics";
|
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
LyricsSettingsPage::LyricsSettingsPage(SettingsDialog *dialog, QWidget *parent)
|
|
|
|
: SettingsPage(dialog, parent),
|
|
|
|
ui_(new Ui::LyricsSettingsPage),
|
|
|
|
provider_selected_(false) {
|
2020-05-08 18:35:36 +02:00
|
|
|
|
|
|
|
ui_->setupUi(this);
|
2023-01-04 21:24:57 +01:00
|
|
|
setWindowIcon(IconLoader::Load("view-media-lyrics", true, 0, 32));
|
2020-05-08 18:35:36 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(ui_->providers_up, &QPushButton::clicked, this, &LyricsSettingsPage::ProvidersMoveUp);
|
|
|
|
QObject::connect(ui_->providers_down, &QPushButton::clicked, this, &LyricsSettingsPage::ProvidersMoveDown);
|
|
|
|
QObject::connect(ui_->providers, &QListWidget::currentItemChanged, this, &LyricsSettingsPage::CurrentItemChanged);
|
|
|
|
QObject::connect(ui_->providers, &QListWidget::itemSelectionChanged, this, &LyricsSettingsPage::ItemSelectionChanged);
|
|
|
|
QObject::connect(ui_->providers, &QListWidget::itemChanged, this, &LyricsSettingsPage::ItemChanged);
|
2020-05-08 18:35:36 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(ui_->button_authenticate, &QPushButton::clicked, this, &LyricsSettingsPage::AuthenticateClicked);
|
|
|
|
QObject::connect(ui_->login_state, &LoginStateWidget::LogoutClicked, this, &LyricsSettingsPage::LogoutClicked);
|
2020-05-08 18:35:36 +02:00
|
|
|
|
2020-05-12 22:15:53 +02:00
|
|
|
ui_->login_state->AddCredentialGroup(ui_->widget_authenticate);
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
NoProviderSelected();
|
|
|
|
DisableAuthentication();
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
dialog->installEventFilter(this);
|
2020-05-08 18:35:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LyricsSettingsPage::~LyricsSettingsPage() { delete ui_; }
|
|
|
|
|
|
|
|
void LyricsSettingsPage::Load() {
|
|
|
|
|
|
|
|
ui_->providers->clear();
|
|
|
|
|
|
|
|
QList<LyricsProvider*> lyrics_providers_sorted = dialog()->app()->lyrics_providers()->List();
|
|
|
|
std::stable_sort(lyrics_providers_sorted.begin(), lyrics_providers_sorted.end(), ProviderCompareOrder);
|
|
|
|
|
|
|
|
for (LyricsProvider *provider : lyrics_providers_sorted) {
|
|
|
|
QListWidgetItem *item = new QListWidgetItem(ui_->providers);
|
|
|
|
item->setText(provider->name());
|
|
|
|
item->setCheckState(provider->is_enabled() ? Qt::Checked : Qt::Unchecked);
|
|
|
|
item->setForeground(provider->is_enabled() ? palette().color(QPalette::Active, QPalette::Text) : palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:56:54 +02:00
|
|
|
Init(ui_->layout_lyricssettingspage->parentWidget());
|
|
|
|
|
2020-10-12 17:20:18 +02:00
|
|
|
if (!QSettings().childGroups().contains(kSettingsGroup)) set_changed();
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::Save() {
|
|
|
|
|
|
|
|
QStringList providers;
|
2021-08-23 21:21:08 +02:00
|
|
|
for (int i = 0; i < ui_->providers->count(); ++i) {
|
2020-05-08 18:35:36 +02:00
|
|
|
const QListWidgetItem *item = ui_->providers->item(i);
|
2021-06-20 19:04:08 +02:00
|
|
|
if (item->checkState() == Qt::Checked) providers << item->text(); // clazy:exclude=reserve-candidates
|
2020-05-08 18:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue("providers", providers);
|
|
|
|
s.endGroup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::CurrentItemChanged(QListWidgetItem *item_current, QListWidgetItem *item_previous) {
|
|
|
|
|
|
|
|
if (item_previous) {
|
|
|
|
LyricsProvider *provider = dialog()->app()->lyrics_providers()->ProviderByName(item_previous->text());
|
|
|
|
if (provider && provider->AuthenticationRequired()) DisconnectAuthentication(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item_current) {
|
|
|
|
const int row = ui_->providers->row(item_current);
|
|
|
|
ui_->providers_up->setEnabled(row != 0);
|
|
|
|
ui_->providers_down->setEnabled(row != ui_->providers->count() - 1);
|
|
|
|
LyricsProvider *provider = dialog()->app()->lyrics_providers()->ProviderByName(item_current->text());
|
2021-03-21 04:47:11 +01:00
|
|
|
if (provider) {
|
|
|
|
if (provider->AuthenticationRequired()) {
|
2023-02-18 14:09:27 +01:00
|
|
|
ui_->login_state->SetLoggedIn(provider->IsAuthenticated() ? LoginStateWidget::State::LoggedIn : LoginStateWidget::State::LoggedOut);
|
2021-03-21 04:47:11 +01:00
|
|
|
ui_->button_authenticate->setEnabled(true);
|
|
|
|
ui_->button_authenticate->show();
|
|
|
|
ui_->login_state->show();
|
|
|
|
ui_->label_auth_info->setText(QString("%1 needs authentication.").arg(provider->name()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DisableAuthentication();
|
|
|
|
ui_->label_auth_info->setText(QString("%1 does not need authentication.").arg(provider->name()));
|
|
|
|
}
|
|
|
|
provider_selected_ = true;
|
2020-05-08 18:35:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DisableAuthentication();
|
|
|
|
NoProviderSelected();
|
|
|
|
ui_->providers_up->setEnabled(false);
|
|
|
|
ui_->providers_down->setEnabled(false);
|
|
|
|
provider_selected_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::ItemSelectionChanged() {
|
|
|
|
|
|
|
|
if (ui_->providers->selectedItems().count() == 0) {
|
|
|
|
DisableAuthentication();
|
|
|
|
NoProviderSelected();
|
|
|
|
ui_->providers_up->setEnabled(false);
|
|
|
|
ui_->providers_down->setEnabled(false);
|
|
|
|
provider_selected_ = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (ui_->providers->currentItem() && !provider_selected_) {
|
|
|
|
CurrentItemChanged(ui_->providers->currentItem(), nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::ProvidersMoveUp() { ProvidersMove(-1); }
|
|
|
|
|
|
|
|
void LyricsSettingsPage::ProvidersMoveDown() { ProvidersMove(+1); }
|
|
|
|
|
|
|
|
void LyricsSettingsPage::ProvidersMove(const int d) {
|
|
|
|
|
|
|
|
const int row = ui_->providers->currentRow();
|
|
|
|
QListWidgetItem *item = ui_->providers->takeItem(row);
|
|
|
|
ui_->providers->insertItem(row + d, item);
|
|
|
|
ui_->providers->setCurrentRow(row + d);
|
|
|
|
|
2020-05-25 23:56:54 +02:00
|
|
|
set_changed();
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::ItemChanged(QListWidgetItem *item) {
|
|
|
|
|
|
|
|
item->setForeground((item->checkState() == Qt::Checked) ? palette().color(QPalette::Active, QPalette::Text) : palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
|
2020-05-25 23:56:54 +02:00
|
|
|
set_changed();
|
|
|
|
|
2020-05-08 18:35:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::NoProviderSelected() {
|
|
|
|
ui_->label_auth_info->setText(tr("No provider selected."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::DisableAuthentication() {
|
|
|
|
|
2023-02-18 14:09:27 +01:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::State::LoggedOut);
|
2020-05-08 18:35:36 +02:00
|
|
|
ui_->button_authenticate->setEnabled(false);
|
|
|
|
ui_->login_state->hide();
|
|
|
|
ui_->button_authenticate->hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:45:29 +02:00
|
|
|
void LyricsSettingsPage::DisconnectAuthentication(LyricsProvider *provider) const {
|
2020-05-08 18:35:36 +02:00
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::disconnect(provider, &LyricsProvider::AuthenticationFailure, this, &LyricsSettingsPage::AuthenticationFailure);
|
|
|
|
QObject::disconnect(provider, &LyricsProvider::AuthenticationSuccess, this, &LyricsSettingsPage::AuthenticationSuccess);
|
2020-05-08 18:35:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::AuthenticateClicked() {
|
|
|
|
|
|
|
|
if (!ui_->providers->currentItem()) return;
|
|
|
|
LyricsProvider *provider = dialog()->app()->lyrics_providers()->ProviderByName(ui_->providers->currentItem()->text());
|
|
|
|
if (!provider) return;
|
|
|
|
ui_->button_authenticate->setEnabled(false);
|
2023-02-18 14:09:27 +01:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::State::LoginInProgress);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(provider, &LyricsProvider::AuthenticationFailure, this, &LyricsSettingsPage::AuthenticationFailure);
|
|
|
|
QObject::connect(provider, &LyricsProvider::AuthenticationSuccess, this, &LyricsSettingsPage::AuthenticationSuccess);
|
2020-05-08 18:35:36 +02:00
|
|
|
provider->Authenticate();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::LogoutClicked() {
|
|
|
|
|
|
|
|
if (!ui_->providers->currentItem()) return;
|
|
|
|
LyricsProvider *provider = dialog()->app()->lyrics_providers()->ProviderByName(ui_->providers->currentItem()->text());
|
|
|
|
if (!provider) return;
|
|
|
|
provider->Deauthenticate();
|
|
|
|
|
|
|
|
ui_->button_authenticate->setEnabled(true);
|
2023-02-18 14:09:27 +01:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::State::LoggedOut);
|
2020-05-08 18:35:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::AuthenticationSuccess() {
|
|
|
|
|
|
|
|
LyricsProvider *provider = qobject_cast<LyricsProvider*>(sender());
|
|
|
|
if (!provider) return;
|
|
|
|
DisconnectAuthentication(provider);
|
|
|
|
|
2021-09-13 20:49:33 +02:00
|
|
|
if (!isVisible() || !ui_->providers->currentItem() || ui_->providers->currentItem()->text() != provider->name()) return;
|
2020-05-08 18:35:36 +02:00
|
|
|
|
2023-02-18 14:09:27 +01:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::State::LoggedIn);
|
2020-05-08 18:35:36 +02:00
|
|
|
ui_->button_authenticate->setEnabled(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LyricsSettingsPage::AuthenticationFailure(const QStringList &errors) {
|
|
|
|
|
|
|
|
LyricsProvider *provider = qobject_cast<LyricsProvider*>(sender());
|
|
|
|
if (!provider) return;
|
|
|
|
DisconnectAuthentication(provider);
|
|
|
|
|
2021-09-13 20:49:33 +02:00
|
|
|
if (!isVisible() || !ui_->providers->currentItem() || ui_->providers->currentItem()->text() != provider->name()) return;
|
2020-05-08 18:35:36 +02:00
|
|
|
|
|
|
|
QMessageBox::warning(this, tr("Authentication failed"), errors.join("\n"));
|
|
|
|
|
2023-02-18 14:09:27 +01:00
|
|
|
ui_->login_state->SetLoggedIn(LoginStateWidget::State::LoggedOut);
|
2020-05-08 18:35:36 +02:00
|
|
|
ui_->button_authenticate->setEnabled(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LyricsSettingsPage::ProviderCompareOrder(LyricsProvider *a, LyricsProvider *b) {
|
|
|
|
return a->order() < b->order();
|
|
|
|
}
|