From ceeaad08a10b4d9257f21f1fdbfbb8d4e8fb38d9 Mon Sep 17 00:00:00 2001 From: "craig.p.drummond" Date: Fri, 15 Feb 2013 17:19:22 +0000 Subject: [PATCH] Download lyrics from MPD server via HTTP --- ChangeLog | 2 ++ lyrics/lyricspage.cpp | 64 +++++++++++++++++++++++++++++-------------- lyrics/lyricspage.h | 3 ++ 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 415799a14..a7b7c575b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -94,6 +94,8 @@ had this) 55. Hack-around size of QComboBox in page classes so that height is same as buttons. (QGtkStyle only) +56. Support reading of lyrics from HTTP server - specify a HTTP url as MPD + music path. 0.9.2 ----- diff --git a/lyrics/lyricspage.cpp b/lyrics/lyricspage.cpp index 82e465fc0..2da084937 100644 --- a/lyrics/lyricspage.cpp +++ b/lyrics/lyricspage.cpp @@ -38,6 +38,7 @@ #include "utils.h" #include "action.h" #include "actioncollection.h" +#include "networkaccessmanager.h" #include #include #include @@ -72,6 +73,7 @@ LyricsPage::LyricsPage(QWidget *p) // , reader(new UltimateLyricsReader(this)) , currentRequest(0) , mode(Mode_Display) + , job(0) { setupUi(this); @@ -294,29 +296,31 @@ void LyricsPage::update(const Song &s, bool force) songFile=u.hasQueryItem("file") ? u.queryItemValue("file") : QString(); } - #ifdef TAGLIB_FOUND - QString tagLyrics=Tags::readLyrics(MPDConnection::self()->getDetails().dir+songFile); - - if (!tagLyrics.isEmpty()) { - text->setText(tagLyrics); - setMode(Mode_Display); - controls->setVisible(false); - return; - } - #endif - - // Check for MPD file... QString mpdLyrics=Utils::changeExtension(MPDConnection::self()->getDetails().dir+songFile, constExtension); -// if (force && QFile::exists(mpdLyrics)) { -// QFile::remove(mpdLyrics); -// } - - // Stop here if we found lyrics in the cache dir. - if (setLyricsFromFile(mpdLyrics)) { - lyricsFile=mpdLyrics; - setMode(Mode_Display); + if (MPDConnection::self()->getDetails().dir.startsWith(QLatin1String("http:/"))) { + QUrl url(mpdLyrics); + job=NetworkAccessManager::self()->get(url); + job->setProperty("file", songFile); + connect(job, SIGNAL(finished()), SLOT(downloadFinished())); return; + } else { + #ifdef TAGLIB_FOUND + QString tagLyrics=Tags::readLyrics(MPDConnection::self()->getDetails().dir+songFile); + + if (!tagLyrics.isEmpty()) { + text->setText(tagLyrics); + setMode(Mode_Display); + controls->setVisible(false); + return; + } + #endif + // Stop here if we found lyrics in the cache dir. + if (setLyricsFromFile(mpdLyrics)) { + lyricsFile=mpdLyrics; + setMode(Mode_Display); + return; + } } } @@ -343,6 +347,26 @@ void LyricsPage::update(const Song &s, bool force) } } +void LyricsPage::downloadFinished() +{ + QNetworkReply *reply=qobject_cast(sender()); + if (reply) { + reply->deleteLater(); + if (QNetworkReply::NoError==reply->error()) { + QString file=reply->property("file").toString(); + if (!file.isEmpty() && file==currentSong.file) { + QTextStream str(reply); + QString lyrics=str.readAll(); + if (!lyrics.isEmpty()) { + text->setText(lyrics); + return; + } + } + } + } + getLyrics(); +} + void LyricsPage::resultReady(int id, const QString &lyrics) { if (id != currentRequest) diff --git a/lyrics/lyricspage.h b/lyrics/lyricspage.h index d58591fe8..3c362d442 100644 --- a/lyrics/lyricspage.h +++ b/lyrics/lyricspage.h @@ -35,6 +35,7 @@ class UltimateLyricsProvider; class UltimateLyricsProvider; class QImage; class Action; +class QNetworkReply; class LyricsPage : public QWidget, public Ui::LyricsPage { @@ -67,6 +68,7 @@ public Q_SLOTS: void setImage(const QImage &img) { text->setImage(img); } protected Q_SLOTS: + void downloadFinished(); void resultReady(int id, const QString &lyrics); void update(); void search(); @@ -111,6 +113,7 @@ private: Mode mode; QString lyricsFile; QString preEdit; + QNetworkReply *job; }; #endif