-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlyricswidget.cpp
82 lines (70 loc) · 2.93 KB
/
lyricswidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <QDebug>
#include <QDomDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "lyricswidget.h"
LyricsWidget::LyricsWidget(QWidget* parent): QTextEdit(parent),
m_networkAccessManager(new QNetworkAccessManager)
{
setReadOnly(true);
setWordWrapMode(QTextOption::WordWrap);
}
LyricsWidget::~LyricsWidget()
{
delete m_networkAccessManager;
}
void LyricsWidget::setTrack(const QString& artist, const QString& title)
{
setHtml("<i>Loading...</i>");
QUrl listUrl("http://lyrics.wikia.com/api.php");
listUrl.addQueryItem("action", "lyrics");
listUrl.addQueryItem("func", "getSong");
listUrl.addQueryItem("fmt", "xml");
listUrl.addQueryItem("artist", artist);
listUrl.addQueryItem("song", title);
connect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveListReply(QNetworkReply*)));
m_networkAccessManager->get(QNetworkRequest(listUrl));
}
void LyricsWidget::receiveListReply(QNetworkReply* reply)
{
disconnect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(receiveListReply(QNetworkReply*)));
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Error while fetching lyrics: " << reply->errorString();
setHtml("<span style='color:red'>Error while retrieving lyrics!</span>");
return;
}
QDomDocument document;
document.setContent(reply);
QString artist = document.elementsByTagName("artist").at(0).toElement().text();
QString title = document.elementsByTagName("song").at(0).toElement().text();
QUrl url("http://lyrics.wikia.com/api.php");
url.addQueryItem("action", "query");
url.addQueryItem("prop", "revisions");
url.addQueryItem("rvprop", "content");
url.addQueryItem("format", "xml");
url.addQueryItem("titles", artist + ":" + title);
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) {
qWarning() << "Error while fetching lyrics: " << reply->errorString();
setHtml("<span style='color:red'>Error while retrieving lyrics!</span>");
return;
}
QString content = QString::fromUtf8(reply->readAll());
int lIndex = content.indexOf("<lyrics>");
int rIndex = content.indexOf("</lyrics>");
if (lIndex == -1 || rIndex == -1) {
qWarning() << Q_FUNC_INFO << "Unable to find lyrics in text";
setText("No lyrics available.");
return;
}
lIndex += 15; // We skip the tag
content = content.mid(lIndex, rIndex - lIndex).trimmed();
content.replace("\n", "<br />");
//setText(content);
setHtml(content);
}