Commit 903c4714 authored by Martin Tobias Holmedahl Sandsmark's avatar Martin Tobias Holmedahl Sandsmark Committed by Martin Tobias Holmedahl Sandsmark
Browse files

add lyricswidget

parent 727d72d5
......@@ -71,6 +71,7 @@ set(juk_SRCS ${tunepimp_SRCS}
juk.cpp
k3bexporter.cpp
keydialog.cpp
lyricswidget.cpp
main.cpp
mediafiles.cpp
mpris2/mediaplayer2.cpp
......
#include <QDebug>
#include <QDomDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "lyricswidget.h"
#include "tag.h"
LyricsWidget::LyricsWidget(QWidget* parent): QTextEdit(parent),
m_networkAccessManager(new QNetworkAccessManager)
{
setReadOnly(true);
setWordWrapMode(QTextOption::WordWrap);
}
LyricsWidget::~LyricsWidget()
{
delete m_networkAccessManager;
}
void LyricsWidget::playing(const FileHandle &file)
{
qWarning() << "LOL";
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", file.tag()->artist());
listUrl.addQueryItem("song", file.tag()->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("&lt;lyrics&gt;");
int rIndex = content.indexOf("&lt;/lyrics&gt;");
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);
}
#ifndef LYRICSWIDGET_H
#define LYRICSWIDGET_H
#include <QTextEdit>
#include "filehandle.h"
class QNetworkAccessManager;
class QNetworkReply;
class LyricsWidget : public QTextEdit
{
Q_OBJECT
public:
explicit LyricsWidget(QWidget *parent);
virtual ~LyricsWidget();
public Q_SLOTS:
void playing(const FileHandle &file);
private Q_SLOTS:
void receiveListReply(QNetworkReply*);
void receiveLyricsReply(QNetworkReply*);
private:
QNetworkAccessManager *m_networkAccessManager;
};
#endif//LYRICSWIDGET_H
......@@ -41,6 +41,7 @@
#include "playermanager.h"
#include "nowplaying.h"
#include "playlistbox.h"
#include "lyricswidget.h"
////////////////////////////////////////////////////////////////////////////////
// public methods
......@@ -55,7 +56,8 @@ PlaylistSplitter::PlaylistSplitter(PlayerManager *player, QWidget *parent) :
m_editor(0),
m_nowPlaying(0),
m_player(player),
m_editorSplitter(0)
m_editorSplitter(0),
m_lyricsWidget(0)
{
setObjectName(QLatin1String("playlistSplitter"));
......@@ -190,8 +192,11 @@ void PlaylistSplitter::setupLayout()
m_editor = new TagEditor(m_editorSplitter);
m_editor->setObjectName( QLatin1String("TagEditor" ));
// Create the lyrics widget
m_lyricsWidget = new LyricsWidget(this);
insertWidget(2, m_lyricsWidget);
// Create the PlaylistBox
m_playlistBox = new PlaylistBox(m_player, this, m_playlistStack);
m_playlistBox->setObjectName( QLatin1String( "playlistBox" ) );
......@@ -202,6 +207,8 @@ void PlaylistSplitter::setupLayout()
connect(m_playlistBox, SIGNAL(startupComplete()), SLOT(slotEnable()));
connect(m_playlistBox, SIGNAL(startFilePlayback(FileHandle)),
m_player, SLOT(play(FileHandle)));
connect(m_playlistBox, SIGNAL(startFilePlayback(FileHandle)),
m_lyricsWidget, SLOT(playing(FileHandle)));
m_player->setPlaylistInterface(m_playlistBox);
......
......@@ -28,6 +28,7 @@ class PlaylistBox;
class NowPlaying;
class PlayerManager;
class FileHandle;
class LyricsWidget;
/**
* This is the main layout class of JuK. It should contain a PlaylistBox and
......@@ -94,6 +95,7 @@ private:
TagEditor *m_editor;
NowPlaying *m_nowPlaying;
PlayerManager *m_player;
LyricsWidget *m_lyricsWidget;
QSplitter *m_editorSplitter;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment