From e83e63bc9edb60980daa66bacf284297aa5a565f Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Wed, 21 Oct 2020 21:06:43 +0000 Subject: lyrics: added widget for qsui git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@9515 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/lyrics/lyrics.cpp | 20 ++- src/plugins/General/lyrics/lyrics.h | 6 +- src/plugins/General/lyrics/lyrics.pro | 13 +- src/plugins/General/lyrics/lyricsfactory.cpp | 13 +- src/plugins/General/lyrics/lyricsfactory.h | 12 +- src/plugins/General/lyrics/lyricswidget.cpp | 243 +++++++++++++++++++++++++++ src/plugins/General/lyrics/lyricswidget.h | 66 ++++++++ src/plugins/General/lyrics/lyricswidget.ui | 241 ++++++++++++++++++++++++++ src/plugins/General/lyrics/lyricswindow.cpp | 228 ------------------------- src/plugins/General/lyrics/lyricswindow.h | 64 ------- src/plugins/General/lyrics/lyricswindow.ui | 241 -------------------------- 11 files changed, 594 insertions(+), 553 deletions(-) create mode 100644 src/plugins/General/lyrics/lyricswidget.cpp create mode 100644 src/plugins/General/lyrics/lyricswidget.h create mode 100644 src/plugins/General/lyrics/lyricswidget.ui delete mode 100644 src/plugins/General/lyrics/lyricswindow.cpp delete mode 100644 src/plugins/General/lyrics/lyricswindow.h delete mode 100644 src/plugins/General/lyrics/lyricswindow.ui (limited to 'src') diff --git a/src/plugins/General/lyrics/lyrics.cpp b/src/plugins/General/lyrics/lyrics.cpp index 24e0131fb..4ef1fe4e9 100644 --- a/src/plugins/General/lyrics/lyrics.cpp +++ b/src/plugins/General/lyrics/lyrics.cpp @@ -25,15 +25,16 @@ #include #include #include -#include "lyricswindow.h" +#include "lyricswidget.h" #include "lyrics.h" -Lyrics::Lyrics(QObject *parent) : QObject(parent) +Lyrics::Lyrics(QPointer *lyricsWidget, QObject *parent) : QObject(parent) { + m_lyricsWidget = lyricsWidget; m_action = new QAction(tr("View Lyrics"), this); m_action->setShortcut(tr("Ctrl+L")); UiHelper::instance()->addAction(m_action, UiHelper::PLAYLIST_MENU); - connect (m_action, SIGNAL(triggered ()), SLOT(showLyrics())); + connect(m_action, SIGNAL(triggered ()), SLOT(showLyrics())); } Lyrics::~Lyrics() @@ -46,8 +47,17 @@ void Lyrics::showLyrics() if (!tracks.isEmpty()) { if (tracks.at(0)->value(Qmmp::ARTIST).isEmpty() || tracks.at(0)->value(Qmmp::TITLE).isEmpty()) - return; - LyricsWindow *w = new LyricsWindow(tracks.first(), qApp->activeWindow ()); + return; + + if(!m_lyricsWidget->isNull() && m_lyricsWidget->data()->isVisible()) + { + m_lyricsWidget->data()->fetch(tracks.first()); + } + else + { + LyricsWidget *w = new LyricsWidget(true, qApp->activeWindow()); + w->fetch(tracks.first()); w->show(); + } } } diff --git a/src/plugins/General/lyrics/lyrics.h b/src/plugins/General/lyrics/lyrics.h index 1e4b8c2d7..e0a216a80 100644 --- a/src/plugins/General/lyrics/lyrics.h +++ b/src/plugins/General/lyrics/lyrics.h @@ -21,13 +21,12 @@ #define LYRICS_H #include - #include #include class QAction; - class SoundCore; +class LyricsWidget; /** @author Ilya Kotov @@ -37,7 +36,7 @@ class Lyrics : public QObject { Q_OBJECT public: - Lyrics(QObject *parent = nullptr); + explicit Lyrics(QPointer *lyricsWidget, QObject *parent = nullptr); ~Lyrics(); @@ -46,6 +45,7 @@ private slots: private: QAction *m_action; + QPointer *m_lyricsWidget; }; diff --git a/src/plugins/General/lyrics/lyrics.pro b/src/plugins/General/lyrics/lyrics.pro index 46d0b5292..7091ae11d 100644 --- a/src/plugins/General/lyrics/lyrics.pro +++ b/src/plugins/General/lyrics/lyrics.pro @@ -6,20 +6,21 @@ QT += network HEADERS += lyricsfactory.h \ lyrics.h \ - lyricswindow.h \ lyricsprovider.h \ ultimatelyricsparser.h \ - settingsdialog.h + settingsdialog.h \ + lyricswidget.h SOURCES += lyricsfactory.cpp \ lyrics.cpp \ - lyricswindow.cpp \ lyricsprovider.cpp \ ultimatelyricsparser.cpp \ - settingsdialog.cpp + settingsdialog.cpp \ + lyricswidget.cpp -FORMS += lyricswindow.ui \ - settingsdialog.ui +FORMS += \ + settingsdialog.ui \ + lyricswidget.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 7ef3a3df4..6a64689af 100644 --- a/src/plugins/General/lyrics/lyricsfactory.cpp +++ b/src/plugins/General/lyrics/lyricsfactory.cpp @@ -21,7 +21,7 @@ #include #include "settingsdialog.h" #include "lyrics.h" -#include "lyricswindow.h" +#include "lyricswidget.h" #include "lyricsfactory.h" GeneralProperties LyricsFactory::properties() const @@ -32,19 +32,22 @@ GeneralProperties LyricsFactory::properties() const properties.hasAbout = true; properties.hasSettings = true; properties.visibilityControl = false; - properties.widgets = { { 0, tr("Lyrics"), Qt::LeftDockWidgetArea, Qt::AllDockWidgetAreas } }; + properties.widgets = { { LYRICS_WIDGET, tr("Lyrics"), Qt::LeftDockWidgetArea, Qt::AllDockWidgetAreas } }; return properties; } QObject *LyricsFactory::create(QObject *parent) { - return new Lyrics(parent); + return new Lyrics(&m_lyricsWidget, parent); } QWidget *LyricsFactory::createWidget(int id, QWidget *parent) { - if(id == 0) - return new LyricsWindow(nullptr, parent); + if(id == LYRICS_WIDGET) + { + m_lyricsWidget = new LyricsWidget(false, parent); + return m_lyricsWidget; + } return nullptr; } diff --git a/src/plugins/General/lyrics/lyricsfactory.h b/src/plugins/General/lyrics/lyricsfactory.h index 4593dedf7..df518bee8 100644 --- a/src/plugins/General/lyrics/lyricsfactory.h +++ b/src/plugins/General/lyrics/lyricsfactory.h @@ -25,10 +25,12 @@ */ #include #include - +#include #include #include +class LyricsWidget; + class LyricsFactory : public QObject, public GeneralFactory { Q_OBJECT @@ -41,6 +43,14 @@ public: QDialog *createConfigDialog(QWidget *parent) override; void showAbout(QWidget *parent) override; QString translation() const override; + +private: + enum WidgetType + { + LYRICS_WIDGET = 0 + }; + + QPointer m_lyricsWidget; }; #endif diff --git a/src/plugins/General/lyrics/lyricswidget.cpp b/src/plugins/General/lyrics/lyricswidget.cpp new file mode 100644 index 000000000..375a9034c --- /dev/null +++ b/src/plugins/General/lyrics/lyricswidget.cpp @@ -0,0 +1,243 @@ +/*************************************************************************** + * Copyright (C) 2009-2020 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include "lyricswidget.h" + +LyricsWidget::LyricsWidget(bool dialog, QWidget *parent) : QWidget(parent) +{ + m_ui.setupUi(this); + + if(dialog) + { + setWindowFlags(Qt::Dialog); + setAttribute(Qt::WA_DeleteOnClose); + setAttribute(Qt::WA_QuitOnClose, false); + } + else + { + m_ui.buttonBox->hide(); + } + + m_cachePath = Qmmp::configDir() + "/lyrics/"; + m_ui.editWidget->setVisible(false); + + m_http = new QNetworkAccessManager(this); + //load global proxy settings + QmmpSettings *gs = QmmpSettings::instance(); + if (gs->isProxyEnabled()) + { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); + if(gs->proxyType() == QmmpSettings::SOCKS5_PROXY) + proxy.setType(QNetworkProxy::Socks5Proxy); + if(gs->useProxyAuth()) + { + proxy.setUser(gs->proxy().userName()); + proxy.setPassword(gs->proxy().password()); + } + m_http->setProxy(proxy); + } + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(onRequestFinished(QNetworkReply *))); + + if(!m_parser.load(":/ultimate_providers.xml")) + { + qWarning("LyricsWindow: unable to load ultimate_providers.xml"); + m_ui.textBrowser->setText(m_parser.errorString()); + return; + } + + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + m_enabledProviders = settings.value("Lyrics/enabled_providers", m_parser.defaultProviders()).toStringList(); + + if(dialog) + restoreGeometry(settings.value("Lyrics/geometry").toByteArray()); + + QDir cacheDir(m_cachePath); + if(!cacheDir.exists()) + { + if(!cacheDir.mkpath(cacheDir.absolutePath())) + qWarning("LyricsWindow: unable to create cache directory"); + } +} + +LyricsWidget::~LyricsWidget() +{ + qDebug("%s", Q_FUNC_INFO); +} + +void LyricsWidget::fetch(const TrackInfo *info) +{ + 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()); + + if(!loadFromCache()) + on_refreshButton_clicked(); +} + +void LyricsWidget::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 && code == 200) + { + QByteArray data = reply->readAll(); + LyricsProvider *provider = m_parser.provider(name); + if(provider) + { + QString content = provider->format(data, m_info); + + if(content.startsWith("http") || content.startsWith("https")) + { + 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); + } + } + else if(m_tasks.isEmpty() && m_ui.providerComboBox->count() == 0) + { + m_ui.textBrowser->setHtml(""+ tr("Not found") +""); + } + } + } + else if(redirectTarget.isValid()) + { + 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(); + } + + reply->deleteLater(); +} + +void LyricsWidget::on_refreshButton_clicked() +{ + 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(m_enabledProviders.contains(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 LyricsWidget::on_editButton_clicked(bool checked) +{ + m_ui.editWidget->setVisible(checked); +} + +void LyricsWidget::on_providerComboBox_activated(int index) +{ + m_ui.textBrowser->setHtml(m_ui.providerComboBox->itemData(index).toString()); +} + +QString LyricsWidget::cacheFilePath() const +{ + QString name = m_ui.artistLineEdit->text() + "_" + m_ui.titleLineEdit->text(); + QByteArray hash = QCryptographicHash::hash(name.toUtf8(), QCryptographicHash::Md5); + return m_cachePath + QString::fromLatin1(hash.toHex()) + ".html"; +} + +bool LyricsWidget::loadFromCache() +{ + QFile file(cacheFilePath()); + if(!file.exists()) + return false; + + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qWarning("LyricsWindow: unable to open cache file '%s', error: %s", + qPrintable(file.fileName()), qPrintable(file.errorString())); + return false; + } + + m_ui.textBrowser->setHtml(QString::fromUtf8(file.readAll())); + m_ui.providerComboBox->addItem(tr("cache")); + return true; +} + +void LyricsWidget::saveToCache(const QString &text) +{ + QFile file(cacheFilePath()); + if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) + { + qWarning("LyricsWindow: unable to open cache file '%s', error: %s", + qPrintable(file.fileName()), qPrintable(file.errorString())); + return; + } + file.write(text.toUtf8()); +} + +void LyricsWidget::closeEvent(QCloseEvent *) +{ + if(windowFlags() & Qt::Dialog) + { + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + settings.setValue("Lyrics/geometry", saveGeometry()); + } +} diff --git a/src/plugins/General/lyrics/lyricswidget.h b/src/plugins/General/lyrics/lyricswidget.h new file mode 100644 index 000000000..c4a7c6e20 --- /dev/null +++ b/src/plugins/General/lyrics/lyricswidget.h @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2009-2020 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 LYRICSWIDGET_H +#define LYRICSWIDGET_H + +#include +#include +#include +#include "ultimatelyricsparser.h" +#include "ui_lyricswindow.h" + +class QNetworkAccessManager; +class QNetworkReply; +class TrackInfo; + +/** + @author Ilya Kotov +*/ +class LyricsWidget : public QWidget +{ +Q_OBJECT +public: + explicit LyricsWidget(bool window, QWidget *parent = nullptr); + + ~LyricsWidget(); + + void fetch(const TrackInfo *info); + +private slots: + void onRequestFinished(QNetworkReply *reply); + 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); + +private: + void closeEvent(QCloseEvent *) override; + Ui::LyricsWindow m_ui; + QNetworkAccessManager *m_http; + QString m_cachePath; + UltimateLyricsParser m_parser; + TrackInfo m_info; + QHash m_tasks; + QStringList m_enabledProviders; +}; + +#endif diff --git a/src/plugins/General/lyrics/lyricswidget.ui b/src/plugins/General/lyrics/lyricswidget.ui new file mode 100644 index 000000000..28e901ef1 --- /dev/null +++ b/src/plugins/General/lyrics/lyricswidget.ui @@ -0,0 +1,241 @@ + + + LyricsWindow + + + + 0 + 0 + 656 + 542 + + + + Lyrics Plugin + + + + 6 + + + 6 + + + 6 + + + + + + + Provider: + + + + + + + + 0 + 0 + + + + QComboBox::AdjustToContents + + + + + + + ... + + + + .. + + + true + + + + + + + ... + + + + .. + + + 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 + + + + + + + + + buttonBox + rejected() + LyricsWindow + close() + + + 465 + 405 + + + 513 + 391 + + + + + diff --git a/src/plugins/General/lyrics/lyricswindow.cpp b/src/plugins/General/lyrics/lyricswindow.cpp deleted file mode 100644 index 4f75bf2ac..000000000 --- a/src/plugins/General/lyrics/lyricswindow.cpp +++ /dev/null @@ -1,228 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009-2020 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include "lyricswindow.h" - -LyricsWindow::LyricsWindow(const TrackInfo *info, QWidget *parent) - : QWidget(parent) -{ - m_ui.setupUi(this); - if(!info) - return; - - //setWindowFlags(Qt::Dialog); - //setAttribute(Qt::WA_DeleteOnClose); - //setAttribute(Qt::WA_QuitOnClose, false); - m_cachePath = Qmmp::configDir() + "/lyrics/"; - 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(); - if (gs->isProxyEnabled()) - { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); - if(gs->proxyType() == QmmpSettings::SOCKS5_PROXY) - proxy.setType(QNetworkProxy::Socks5Proxy); - if(gs->useProxyAuth()) - { - proxy.setUser(gs->proxy().userName()); - proxy.setPassword(gs->proxy().password()); - } - m_http->setProxy(proxy); - } - connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(onRequestFinished(QNetworkReply *))); - - if(!m_parser.load(":/ultimate_providers.xml")) - { - qWarning("LyricsWindow: unable to load ultimate_providers.xml"); - m_ui.textBrowser->setText(m_parser.errorString()); - return; - } - - QSettings settings(Qmmp::configFile(), QSettings::IniFormat); - restoreGeometry(settings.value("Lyrics/geometry").toByteArray()); - m_enabledProviders = settings.value("Lyrics/enabled_providers", m_parser.defaultProviders()).toStringList(); - - QDir cacheDir(m_cachePath); - if(!cacheDir.exists()) - { - if(!cacheDir.mkpath(cacheDir.absolutePath())) - qWarning("LyricsWindow: unable to create cache directory"); - } - if(!loadFromCache()) - on_refreshButton_clicked(); -} - -LyricsWindow::~LyricsWindow() -{ - qDebug("%s", Q_FUNC_INFO); -} - -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 && code == 200) - { - QByteArray data = reply->readAll(); - LyricsProvider *provider = m_parser.provider(name); - if(provider) - { - QString content = provider->format(data, m_info); - - if(content.startsWith("http") || content.startsWith("https")) - { - 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); - } - } - else if(m_tasks.isEmpty() && m_ui.providerComboBox->count() == 0) - { - m_ui.textBrowser->setHtml(""+ tr("Not found") +""); - } - } - } - else if(redirectTarget.isValid()) - { - 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(); - } - - reply->deleteLater(); -} - -void LyricsWindow::on_refreshButton_clicked() -{ - 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(m_enabledProviders.contains(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(); - QByteArray hash = QCryptographicHash::hash(name.toUtf8(), QCryptographicHash::Md5); - return m_cachePath + QString::fromLatin1(hash.toHex()) + ".html"; -} - -bool LyricsWindow::loadFromCache() -{ - QFile file(cacheFilePath()); - if(!file.exists()) - return false; - - if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) - { - qWarning("LyricsWindow: unable to open cache file '%s', error: %s", - qPrintable(file.fileName()), qPrintable(file.errorString())); - return false; - } - - m_ui.textBrowser->setHtml(QString::fromUtf8(file.readAll())); - m_ui.providerComboBox->addItem(tr("cache")); - return true; -} - -void LyricsWindow::saveToCache(const QString &text) -{ - QFile file(cacheFilePath()); - if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) - { - qWarning("LyricsWindow: unable to open cache file '%s', error: %s", - qPrintable(file.fileName()), qPrintable(file.errorString())); - return; - } - file.write(text.toUtf8()); -} - -void LyricsWindow::closeEvent(QCloseEvent *) -{ - QSettings settings(Qmmp::configFile(), QSettings::IniFormat); - settings.setValue("Lyrics/geometry", saveGeometry()); -} diff --git a/src/plugins/General/lyrics/lyricswindow.h b/src/plugins/General/lyrics/lyricswindow.h deleted file mode 100644 index 5ea962461..000000000 --- a/src/plugins/General/lyrics/lyricswindow.h +++ /dev/null @@ -1,64 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2009-2020 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 LYRICSWINDOW_H -#define LYRICSWINDOW_H - -#include -#include -#include -#include "ultimatelyricsparser.h" -#include "ui_lyricswindow.h" - -class QNetworkAccessManager; -class QNetworkReply; -class TrackInfo; - -/** - @author Ilya Kotov -*/ -class LyricsWindow : public QWidget -{ -Q_OBJECT -public: - explicit LyricsWindow(const TrackInfo *info, QWidget *parent = nullptr); - - ~LyricsWindow(); - -private slots: - void onRequestFinished(QNetworkReply *reply); - 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); - -private: - void closeEvent(QCloseEvent *) override; - Ui::LyricsWindow m_ui; - QNetworkAccessManager *m_http; - QString m_cachePath; - 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 deleted file mode 100644 index 28e901ef1..000000000 --- a/src/plugins/General/lyrics/lyricswindow.ui +++ /dev/null @@ -1,241 +0,0 @@ - - - LyricsWindow - - - - 0 - 0 - 656 - 542 - - - - Lyrics Plugin - - - - 6 - - - 6 - - - 6 - - - - - - - Provider: - - - - - - - - 0 - 0 - - - - QComboBox::AdjustToContents - - - - - - - ... - - - - .. - - - true - - - - - - - ... - - - - .. - - - 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 - - - - - - - - - buttonBox - rejected() - LyricsWindow - close() - - - 465 - 405 - - - 513 - 391 - - - - - -- cgit v1.2.3-13-gbd6f