From 74ec7eeee3de7e5b1a7f07206da65b604356f9be Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Mon, 4 Nov 2019 13:25:05 +0000 Subject: lyrics: added settings dialog, fixed remaining issues git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@9093 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/lyrics/lyrics.pro | 9 +- src/plugins/General/lyrics/lyricsfactory.cpp | 9 +- src/plugins/General/lyrics/lyricsprovider.cpp | 9 +- src/plugins/General/lyrics/lyricsprovider.h | 3 +- src/plugins/General/lyrics/lyricswindow.cpp | 87 ++++++--- src/plugins/General/lyrics/lyricswindow.h | 5 +- src/plugins/General/lyrics/lyricswindow.ui | 200 ++++++++++++++++++--- src/plugins/General/lyrics/settingsdialog.cpp | 61 +++++++ src/plugins/General/lyrics/settingsdialog.h | 45 +++++ src/plugins/General/lyrics/settingsdialog.ui | 83 +++++++++ .../General/lyrics/ultimatelyricsparser.cpp | 12 ++ src/plugins/General/lyrics/ultimatelyricsparser.h | 1 + 12 files changed, 462 insertions(+), 62 deletions(-) create mode 100644 src/plugins/General/lyrics/settingsdialog.cpp create mode 100644 src/plugins/General/lyrics/settingsdialog.h create mode 100644 src/plugins/General/lyrics/settingsdialog.ui (limited to 'src') diff --git a/src/plugins/General/lyrics/lyrics.pro b/src/plugins/General/lyrics/lyrics.pro index fc3a717a7..46d0b5292 100644 --- a/src/plugins/General/lyrics/lyrics.pro +++ b/src/plugins/General/lyrics/lyrics.pro @@ -8,15 +8,18 @@ HEADERS += lyricsfactory.h \ lyrics.h \ lyricswindow.h \ lyricsprovider.h \ - ultimatelyricsparser.h + ultimatelyricsparser.h \ + settingsdialog.h SOURCES += lyricsfactory.cpp \ lyrics.cpp \ lyricswindow.cpp \ lyricsprovider.cpp \ - ultimatelyricsparser.cpp + ultimatelyricsparser.cpp \ + settingsdialog.cpp -FORMS += lyricswindow.ui +FORMS += lyricswindow.ui \ + settingsdialog.ui RESOURCES = translations/translations.qrc \ providers/providers.qrc diff --git a/src/plugins/General/lyrics/lyricsfactory.cpp b/src/plugins/General/lyrics/lyricsfactory.cpp index aef4322f9..be8b2fee5 100644 --- a/src/plugins/General/lyrics/lyricsfactory.cpp +++ b/src/plugins/General/lyrics/lyricsfactory.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2009 by Ilya Kotov * + * Copyright (C) 2009-2019 by Ilya Kotov * * forkotov02@ya.ru * * * * This program is free software; you can redistribute it and/or modify * @@ -18,8 +18,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ - #include +#include "settingsdialog.h" #include "lyrics.h" #include "lyricsfactory.h" @@ -29,7 +29,7 @@ GeneralProperties LyricsFactory::properties() const properties.name = tr("Lyrics Plugin"); properties.shortName = "lyrics"; properties.hasAbout = true; - properties.hasSettings = false; + properties.hasSettings = true; properties.visibilityControl = false; return properties; } @@ -41,8 +41,7 @@ QObject *LyricsFactory::create(QObject *parent) QDialog *LyricsFactory::createConfigDialog(QWidget *parent) { - Q_UNUSED(parent); - return nullptr; + return new SettingsDialog(parent); } void LyricsFactory::showAbout(QWidget *parent) diff --git a/src/plugins/General/lyrics/lyricsprovider.cpp b/src/plugins/General/lyrics/lyricsprovider.cpp index 4dae451dd..755e1ba06 100644 --- a/src/plugins/General/lyrics/lyricsprovider.cpp +++ b/src/plugins/General/lyrics/lyricsprovider.cpp @@ -101,6 +101,9 @@ QString LyricsProvider::format(const QByteArray &data, const TrackInfo &track) c return QString(); } + if(m_skipRules) + return content; + const QMap replaceMap = generateReplaceMap(track); for(const Rule &rule : qAsConst(m_extractRules)) @@ -157,6 +160,11 @@ const QString &LyricsProvider::name() const return m_name; } +void LyricsProvider::skipRules(bool skip) +{ + m_skipRules = skip; +} + QString LyricsProvider::fixCase(const QString &title) const { QString out; @@ -201,7 +209,6 @@ QString LyricsProvider::extract(const QString &content, const Rule &rule) const for(const Item &item : qAsConst(rule)) { - if(!item.url.isEmpty()) { QString url = item.url; diff --git a/src/plugins/General/lyrics/lyricsprovider.h b/src/plugins/General/lyrics/lyricsprovider.h index 3e9e7bf81..734f0749c 100644 --- a/src/plugins/General/lyrics/lyricsprovider.h +++ b/src/plugins/General/lyrics/lyricsprovider.h @@ -23,6 +23,7 @@ public: QString getUrl(const TrackInfo &track) const; QString format(const QByteArray &data, const TrackInfo &track) const; const QString &name() const; + void skipRules(bool skip); private: @@ -50,7 +51,7 @@ private: QList m_extractRules; QList m_excludeRules; QStringList m_invalidIndicators; - + bool m_skipRules = false; }; #endif // LYRICSPROVIDER_H diff --git a/src/plugins/General/lyrics/lyricswindow.cpp b/src/plugins/General/lyrics/lyricswindow.cpp index 5036bc4c8..39dee905a 100644 --- a/src/plugins/General/lyrics/lyricswindow.cpp +++ b/src/plugins/General/lyrics/lyricswindow.cpp @@ -23,9 +23,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -38,10 +38,14 @@ LyricsWindow::LyricsWindow(const TrackInfo *info, QWidget *parent) setWindowFlags(Qt::Dialog); setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_QuitOnClose, false); - m_info = *info; m_cachePath = Qmmp::configDir() + "/lyrics/"; - //m_ui.artistLineEdit->setText(info); - //m_ui.titleLineEdit->setText(title); + m_ui.editWidget->setVisible(false); + m_ui.titleLineEdit->setText(info->value(Qmmp::TITLE)); + m_ui.artistLineEdit->setText(info->value(Qmmp::ARTIST)); + m_ui.albumLineEdit->setText(info->value(Qmmp::ALBUM)); + m_ui.trackSpinBox->setValue(info->value(Qmmp::TRACK).toInt()); + m_ui.yearSpinBox->setValue(info->value(Qmmp::YEAR).toInt()); + m_http = new QNetworkAccessManager(this); //load global proxy settings QmmpSettings *gs = QmmpSettings::instance(); @@ -62,6 +66,9 @@ LyricsWindow::LyricsWindow(const TrackInfo *info, QWidget *parent) if(!m_parser.load(":/ultimate_providers.xml")) qWarning("LyricsWindow: unable to load ultimate_providers.xml"); + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + m_enabledProviders = settings.value("Lyrics/enabled_providers", m_parser.defaultProviders()).toStringList(); + QDir cacheDir(m_cachePath); if(!cacheDir.exists()) { @@ -69,10 +76,9 @@ LyricsWindow::LyricsWindow(const TrackInfo *info, QWidget *parent) qWarning("LyricsWindow: unable to create cache directory"); } if(!loadFromCache()) - on_searchPushButton_clicked(); + on_refreshButton_clicked(); } - LyricsWindow::~LyricsWindow() { } @@ -81,20 +87,34 @@ void LyricsWindow::onRequestFinished(QNetworkReply *reply) { QString name = m_tasks.take(reply); QVariant redirectTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); + int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if(reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) + if(reply->error() == QNetworkReply::NoError && code == 200) { QByteArray data = reply->readAll(); LyricsProvider *provider = m_parser.provider(name); if(provider) { QString content = provider->format(data, m_info); - qDebug() << content; - if(!content.isEmpty()) + + if(content.startsWith("http") || content.startsWith("https")) { - m_ui.textBrowser->append(QString("%1").arg(provider->name())); - m_ui.textBrowser->append(content); - m_ui.textBrowser->append("
-------------------------------------
"); + QNetworkRequest request; + request.setUrl(content); + request.setRawHeader("User-Agent", QString("qmmp/%1").arg(Qmmp::strVersion()).toLatin1()); + m_tasks.insert(m_http->get(request), provider->name()); + provider->skipRules(true); + } + else if(!content.isEmpty()) + { + content.prepend(tr("

%1 - %2

").arg(m_info.value(Qmmp::ARTIST)).arg(m_info.value(Qmmp::TITLE))); + m_ui.providerComboBox->addItem(name, content); + if(m_ui.providerComboBox->count() == 1) + { + m_ui.providerComboBox->setCurrentIndex(0); + m_ui.textBrowser->setHtml(content); + saveToCache(content); + } } } } @@ -102,6 +122,11 @@ void LyricsWindow::onRequestFinished(QNetworkReply *reply) { m_tasks.insert(m_http->get(QNetworkRequest(redirectTarget.toUrl())), name); } + else if(m_tasks.isEmpty() && m_ui.providerComboBox->count() == 0) + { + m_ui.textBrowser->setText(tr("Error: %1 - %2").arg(code).arg(reply->errorString())); + qWarning() << "error:" << reply->errorString(); + } else { qWarning() << "error:" << reply->errorString(); @@ -110,24 +135,42 @@ void LyricsWindow::onRequestFinished(QNetworkReply *reply) reply->deleteLater(); } -void LyricsWindow::on_searchPushButton_clicked() +void LyricsWindow::on_refreshButton_clicked() { - m_ui.stateLabel->setText(tr("Receiving")); + m_ui.textBrowser->setHtml(QString("%1").arg(tr("Receiving"))); + m_ui.providerComboBox->clear(); + + m_info.clear(); + m_info.setValue(Qmmp::TITLE, m_ui.titleLineEdit->text()); + m_info.setValue(Qmmp::ARTIST, m_ui.artistLineEdit->text()); + m_info.setValue(Qmmp::ALBUM, m_ui.albumLineEdit->text()); + m_info.setValue(Qmmp::TRACK, m_ui.trackSpinBox->value()); + m_info.setValue(Qmmp::YEAR, m_ui.yearSpinBox->value()); for(LyricsProvider *provider : m_parser.providers()) { - //if(provider->name() == "lyrics.wikia.com") + if(m_enabledProviders.contains(provider->name())) { - QString url = provider->getUrl(m_info); - qDebug() << provider->name() << url; - QNetworkRequest request; - request.setUrl(url); - request.setRawHeader("User-Agent", QString("qmmp/%1").arg(Qmmp::strVersion()).toLatin1()); - m_tasks.insert(m_http->get(request), provider->name()); + QString url = provider->getUrl(m_info); + QNetworkRequest request; + request.setUrl(url); + request.setRawHeader("User-Agent", QString("qmmp/%1").arg(Qmmp::strVersion()).toLatin1()); + m_tasks.insert(m_http->get(request), provider->name()); + provider->skipRules(false); } } } +void LyricsWindow::on_editButton_clicked(bool checked) +{ + m_ui.editWidget->setVisible(checked); +} + +void LyricsWindow::on_providerComboBox_activated(int index) +{ + m_ui.textBrowser->setHtml(m_ui.providerComboBox->itemData(index).toString()); +} + QString LyricsWindow::cacheFilePath() const { QString name = m_ui.artistLineEdit->text() + "_" + m_ui.titleLineEdit->text(); @@ -149,7 +192,7 @@ bool LyricsWindow::loadFromCache() } m_ui.textBrowser->setHtml(QString::fromUtf8(file.readAll())); - m_ui.stateLabel->setText(tr("Done")); + m_ui.providerComboBox->addItem(tr("cache")); return true; } diff --git a/src/plugins/General/lyrics/lyricswindow.h b/src/plugins/General/lyrics/lyricswindow.h index 87f3d578c..6d4bfaa77 100644 --- a/src/plugins/General/lyrics/lyricswindow.h +++ b/src/plugins/General/lyrics/lyricswindow.h @@ -43,7 +43,9 @@ public: private slots: void onRequestFinished(QNetworkReply *reply); - void on_searchPushButton_clicked(); + void on_refreshButton_clicked(); + void on_editButton_clicked(bool checked); + void on_providerComboBox_activated(int index); QString cacheFilePath() const; bool loadFromCache(); void saveToCache(const QString &text); @@ -55,6 +57,7 @@ private: UltimateLyricsParser m_parser; TrackInfo m_info; QHash m_tasks; + QStringList m_enabledProviders; }; #endif diff --git a/src/plugins/General/lyrics/lyricswindow.ui b/src/plugins/General/lyrics/lyricswindow.ui index 235bafdb6..28e901ef1 100644 --- a/src/plugins/General/lyrics/lyricswindow.ui +++ b/src/plugins/General/lyrics/lyricswindow.ui @@ -6,14 +6,14 @@ 0 0 - 513 - 420 + 656 + 542 Lyrics Plugin - + 6 @@ -23,52 +23,194 @@ 6 - - - - true - - - - - - - No connection - - - - + - + - Artist: + Provider: - + + + + 0 + 0 + + + + QComboBox::AdjustToContents + + - + - Title: + ... + + + + .. + + + true - - - - + - Search + ... + + + + .. + + + true + + + true + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 40 + 20 + + + + - + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Title: + + + + + + + Album: + + + + + + + + 0 + 0 + + + + + + + + Track: + + + + + + + Artist: + + + + + + + + + + + + + + + Year: + + + + + + + + 73 + 0 + + + + 2050 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 0 + 0 + + + + + + + + + + + true + + + + QDialogButtonBox::Close diff --git a/src/plugins/General/lyrics/settingsdialog.cpp b/src/plugins/General/lyrics/settingsdialog.cpp new file mode 100644 index 000000000..59a900d23 --- /dev/null +++ b/src/plugins/General/lyrics/settingsdialog.cpp @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (C) 2019 by Ilya Kotov * + * forkotov02@ya.ru * + * * + * This program 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 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include +#include "settingsdialog.h" +#include "ultimatelyricsparser.h" +#include "ui_settingsdialog.h" + +SettingsDialog::SettingsDialog(QWidget *parent) : + QDialog(parent), + m_ui(new Ui::SettingsDialog) +{ + m_ui->setupUi(this); + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + UltimateLyricsParser parser; + parser.load(":/ultimate_providers.xml"); + QStringList enabledProviders = settings.value("Lyrics/enabled_providers", parser.defaultProviders()).toStringList(); + + for(const LyricsProvider *provider : parser.providers()) + { + QListWidgetItem *item = new QListWidgetItem(provider->name()); + item->setCheckState(enabledProviders.contains(provider->name()) ? Qt::Checked : Qt::Unchecked); + m_ui->providersListWidget->addItem(item); + } +} + +SettingsDialog::~SettingsDialog() +{ + delete m_ui; +} + +void SettingsDialog::accept() +{ + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + QStringList enabledProviders; + for(int i = 0; i < m_ui->providersListWidget->count(); ++i) + { + if(m_ui->providersListWidget->item(i)->checkState() == Qt::Checked) + enabledProviders << m_ui->providersListWidget->item(i)->text(); + } + settings.setValue("Lyrics/enabled_providers", enabledProviders); + QDialog::accept(); +} diff --git a/src/plugins/General/lyrics/settingsdialog.h b/src/plugins/General/lyrics/settingsdialog.h new file mode 100644 index 000000000..b266d1028 --- /dev/null +++ b/src/plugins/General/lyrics/settingsdialog.h @@ -0,0 +1,45 @@ +/*************************************************************************** + * Copyright (C) 2019 by Ilya Kotov * + * forkotov02@ya.ru * + * * + * This program 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 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef SETTINGSDIALOG_H +#define SETTINGSDIALOG_H + +#include + +namespace Ui { +class SettingsDialog; +} + +class SettingsDialog : public QDialog +{ + Q_OBJECT + +public: + explicit SettingsDialog(QWidget *parent = 0); + ~SettingsDialog(); + +public slots: + void accept() override; + +private: + Ui::SettingsDialog *m_ui; +}; + +#endif // SETTINGSDIALOG_H diff --git a/src/plugins/General/lyrics/settingsdialog.ui b/src/plugins/General/lyrics/settingsdialog.ui new file mode 100644 index 000000000..b71eed632 --- /dev/null +++ b/src/plugins/General/lyrics/settingsdialog.ui @@ -0,0 +1,83 @@ + + + SettingsDialog + + + + 0 + 0 + 431 + 371 + + + + Lyrics Plugin Settings + + + + 6 + + + 6 + + + 6 + + + + + Lyrics providers: + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + SettingsDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + SettingsDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/plugins/General/lyrics/ultimatelyricsparser.cpp b/src/plugins/General/lyrics/ultimatelyricsparser.cpp index fb5281bdb..07a89684b 100644 --- a/src/plugins/General/lyrics/ultimatelyricsparser.cpp +++ b/src/plugins/General/lyrics/ultimatelyricsparser.cpp @@ -118,3 +118,15 @@ LyricsProvider *UltimateLyricsParser::provider(const QString &name) const } return nullptr; } + +QStringList UltimateLyricsParser::defaultProviders() +{ + QStringList out = { + "lyrics.wikia.com", + "Encyclopaedia Metallum", + "letras.mus.br", + "darklyrics.com" + }; + + return out; +} diff --git a/src/plugins/General/lyrics/ultimatelyricsparser.h b/src/plugins/General/lyrics/ultimatelyricsparser.h index 4f2967597..a74c57ddb 100644 --- a/src/plugins/General/lyrics/ultimatelyricsparser.h +++ b/src/plugins/General/lyrics/ultimatelyricsparser.h @@ -17,6 +17,7 @@ public: const QString &errorString() const; const QList &providers(); LyricsProvider *provider(const QString &name) const; + static QStringList defaultProviders(); private: QString m_errorString; -- cgit v1.2.3-13-gbd6f