diff --git a/lyricswidget.cpp b/lyricswidget.cpp index 1b77816c..d928a44a 100644 --- a/lyricswidget.cpp +++ b/lyricswidget.cpp @@ -1,163 +1,170 @@ /** * Copyright (C) 2012 Martin Sandsmark * * 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, see . */ #include "lyricswidget.h" #include #include #include #include #include #include #include #include #include #include #include #include "tag.h" #include "actioncollection.h" #include "juk_debug.h" LyricsWidget::LyricsWidget(QWidget* parent): QTextBrowser(parent), m_networkAccessManager(new QNetworkAccessManager), m_lyricsCurrent(false) { setMinimumWidth(200); setReadOnly(true); setWordWrapMode(QTextOption::WordWrap); setOpenExternalLinks(true); KToggleAction *show = new KToggleAction(QIcon::fromTheme(QLatin1String("view-media-lyrics")), i18n("Show &Lyrics"), this); ActionCollection::actions()->addAction("showLyrics", show); connect(show, SIGNAL(toggled(bool)), this, SLOT(setVisible(bool))); KConfigGroup config(KSharedConfig::openConfig(), "LyricsWidget"); bool shown = config.readEntry("Show", true); show->setChecked(shown); setVisible(shown); } LyricsWidget::~LyricsWidget() { delete m_networkAccessManager; saveConfig(); } void LyricsWidget::saveConfig() { KConfigGroup config(KSharedConfig::openConfig(), "LyricsWidget"); config.writeEntry("Show", ActionCollection::action("showLyrics")->isChecked()); } void LyricsWidget::makeLyricsRequest() { m_lyricsCurrent = true; if(m_playingFile.isNull()) { setHtml(i18n("No file playing.")); return; } setHtml(i18n("Loading...")); // TODO time for https (as long as it doesn't break this) QUrl listUrl("http://lyrics.wikia.com/api.php"); QUrlQuery listUrlQuery; listUrlQuery.addQueryItem("action", "lyrics"); listUrlQuery.addQueryItem("func", "getSong"); listUrlQuery.addQueryItem("fmt", "xml"); listUrlQuery.addQueryItem("artist", m_playingFile.tag()->artist()); listUrlQuery.addQueryItem("song", m_playingFile.tag()->title()); listUrl.setQuery(listUrlQuery); m_title = m_playingFile.tag()->artist() + " – " + m_playingFile.tag()->title(); connect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveListReply(QNetworkReply*))); m_networkAccessManager->get(QNetworkRequest(listUrl)); } void LyricsWidget::playing(const FileHandle &file) { m_playingFile = file; m_lyricsCurrent = false; if(isVisible()) { makeLyricsRequest(); } } void LyricsWidget::showEvent(QShowEvent *) { if(!m_lyricsCurrent) { makeLyricsRequest(); } } void LyricsWidget::receiveListReply(QNetworkReply* reply) { disconnect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveListReply(QNetworkReply*))); if (reply->error() != QNetworkReply::NoError) { qCWarning(JUK_LOG) << "Error while fetching lyrics: " << reply->errorString(); setHtml(i18n("Error while retrieving lyrics!")); return; } QDomDocument document; document.setContent(reply); QString artist = document.elementsByTagName("artist").at(0).toElement().text(); QString title = document.elementsByTagName("song").at(0).toElement().text(); // TODO time for https (as long as it doesn't break this) QUrl url("http://lyrics.wikia.com/api.php"); QUrlQuery urlQuery; urlQuery.addQueryItem("action", "query"); urlQuery.addQueryItem("prop", "revisions"); urlQuery.addQueryItem("rvprop", "content"); urlQuery.addQueryItem("format", "xml"); urlQuery.addQueryItem("titles", artist + ':' + title); url.setQuery(urlQuery); connect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveLyricsReply(QNetworkReply*))); m_networkAccessManager->get(QNetworkRequest(url)); } void LyricsWidget::receiveLyricsReply(QNetworkReply* reply) { disconnect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveLyricsReply(QNetworkReply*))); if (reply->error() != QNetworkReply::NoError) { qCWarning(JUK_LOG) << "Error while fetching lyrics: " << reply->errorString(); setHtml(i18n("Error while retrieving lyrics!")); return; } + const QUrlQuery replyUrlQuery(reply->url()); + QString titlesUrlPart = replyUrlQuery.queryItemValue(QStringLiteral("titles"), QUrl::FullyEncoded); + if (titlesUrlPart.isEmpty()) { + // default homepage, but this code path should never happen at this point. + titlesUrlPart = QStringLiteral("Lyrics_Wiki"); + } + const QString lyricsUrl = QStringLiteral("http://lyrics.wikia.com/wiki/") + titlesUrlPart; QString content = QString::fromUtf8(reply->readAll()); int lIndex = content.indexOf("<lyrics>"); int rIndex = content.indexOf("</lyrics>"); if (lIndex == -1 || rIndex == -1) { qCWarning(JUK_LOG) << Q_FUNC_INFO << "Unable to find lyrics in text"; setText(i18n("No lyrics available.")); return; } lIndex += 15; // We skip the tag content = content.mid(lIndex, rIndex - lIndex).trimmed(); content.replace('\n', "
"); //setText(content); setHtml("

" + m_title + "

" + content + - i18n("

Lyrics provided by LyricWiki")); + i18n("

Lyrics provided by LyricWiki", lyricsUrl)); }