aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/plugins/General/lyrics/lyrics.pro11
-rw-r--r--src/plugins/General/lyrics/lyricsprovider.cpp63
-rw-r--r--src/plugins/General/lyrics/lyricsprovider.h45
-rw-r--r--src/plugins/General/lyrics/lyricswindow.cpp4
-rw-r--r--src/plugins/General/lyrics/ultimatelyricsparser.cpp110
-rw-r--r--src/plugins/General/lyrics/ultimatelyricsparser.h26
6 files changed, 256 insertions, 3 deletions
diff --git a/src/plugins/General/lyrics/lyrics.pro b/src/plugins/General/lyrics/lyrics.pro
index e3833d68c..fc3a717a7 100644
--- a/src/plugins/General/lyrics/lyrics.pro
+++ b/src/plugins/General/lyrics/lyrics.pro
@@ -6,15 +6,20 @@ QT += network
HEADERS += lyricsfactory.h \
lyrics.h \
- lyricswindow.h
+ lyricswindow.h \
+ lyricsprovider.h \
+ ultimatelyricsparser.h
SOURCES += lyricsfactory.cpp \
lyrics.cpp \
- lyricswindow.cpp
+ lyricswindow.cpp \
+ lyricsprovider.cpp \
+ ultimatelyricsparser.cpp
FORMS += lyricswindow.ui
-RESOURCES = translations/translations.qrc
+RESOURCES = translations/translations.qrc \
+ providers/providers.qrc
LIBS += $$QMMPUI_LIB
diff --git a/src/plugins/General/lyrics/lyricsprovider.cpp b/src/plugins/General/lyrics/lyricsprovider.cpp
new file mode 100644
index 000000000..b37f8ccff
--- /dev/null
+++ b/src/plugins/General/lyrics/lyricsprovider.cpp
@@ -0,0 +1,63 @@
+#include "lyricsprovider.h"
+
+LyricsProvider::LyricsProvider()
+{
+
+}
+
+void LyricsProvider::setName(const QString &name)
+{
+ m_name = name;
+}
+
+void LyricsProvider::setTitle(const QString &title)
+{
+ m_title = title;
+}
+
+void LyricsProvider::setCharset(const QString &charset)
+{
+ m_charser = charset;
+}
+
+void LyricsProvider::setUrl(const QString &url)
+{
+ m_url = url;
+}
+
+void LyricsProvider::addUrlFormat(const QString &replace, const QString &with)
+{
+ m_urlFormats << UrlFormat{ .replace = replace, .with = with };
+}
+
+void LyricsProvider::addRule(const QList<QPair<QString, QString> > &args, bool exclude)
+{
+ Rule rule;
+ for(const QPair<QString, QString> &i : qAsConst(args))
+ {
+ Item item;
+ if(!i.first.isEmpty() && !i.second.isEmpty())
+ {
+ item.begin = i.first;
+ item.end = i.second;
+ }
+ else if(i.first.contains("://")) //url
+ {
+ item.url = i.first;
+ }
+ else
+ {
+ item.tag = i.first;
+ }
+ rule << item;
+ }
+ if(exclude)
+ m_excludeRules << rule;
+ else
+ m_extractRules << rule;
+}
+
+void LyricsProvider::addInvalidIndicator(const QString &indicator)
+{
+ m_invalidIndicators << indicator;
+}
diff --git a/src/plugins/General/lyrics/lyricsprovider.h b/src/plugins/General/lyrics/lyricsprovider.h
new file mode 100644
index 000000000..c54e70f46
--- /dev/null
+++ b/src/plugins/General/lyrics/lyricsprovider.h
@@ -0,0 +1,45 @@
+#ifndef LYRICSPROVIDER_H
+#define LYRICSPROVIDER_H
+
+#include <QString>
+#include <QList>
+#include <QPair>
+
+class LyricsProvider
+{
+public:
+ LyricsProvider();
+
+ void setName(const QString &name);
+ void setTitle(const QString &title);
+ void setCharset(const QString &charset);
+ void setUrl(const QString &url);
+ void addUrlFormat(const QString &replace, const QString &with);
+ void addRule(const QList<QPair<QString, QString> > &args, bool exclude = false);
+ void addInvalidIndicator(const QString &indicator);
+
+private:
+ QString m_name, m_title;
+ QString m_charser = QLatin1String("utf-8");
+ QString m_url;
+
+ struct UrlFormat
+ {
+ QString replace, with;
+ };
+
+ struct Item
+ {
+ QString begin, end, tag, url;
+ };
+
+ typedef QList<Item> Rule;
+
+ QList<UrlFormat> m_urlFormats;
+ QList<Rule> m_extractRules;
+ QList<Rule> m_excludeRules;
+ QStringList m_invalidIndicators;
+
+};
+
+#endif // LYRICSPROVIDER_H
diff --git a/src/plugins/General/lyrics/lyricswindow.cpp b/src/plugins/General/lyrics/lyricswindow.cpp
index 0ef6c83d6..1754413b3 100644
--- a/src/plugins/General/lyrics/lyricswindow.cpp
+++ b/src/plugins/General/lyrics/lyricswindow.cpp
@@ -28,6 +28,7 @@
#include <QCryptographicHash>
#include <qmmp/qmmpsettings.h>
#include <qmmp/qmmp.h>
+//#include "ultimatelyricsparser.h"
#include "lyricswindow.h"
LyricsWindow::LyricsWindow(const QString &artist, const QString &title, QWidget *parent)
@@ -66,6 +67,9 @@ LyricsWindow::LyricsWindow(const QString &artist, const QString &title, QWidget
}
if(!loadFromCache())
on_searchPushButton_clicked();
+
+ //UltimateLyricsParser parser;
+ //parser.load(":/ultimate_providers.xml");
}
diff --git a/src/plugins/General/lyrics/ultimatelyricsparser.cpp b/src/plugins/General/lyrics/ultimatelyricsparser.cpp
new file mode 100644
index 000000000..ca74769f6
--- /dev/null
+++ b/src/plugins/General/lyrics/ultimatelyricsparser.cpp
@@ -0,0 +1,110 @@
+#include <QXmlStreamReader>
+#include <QFile>
+#include <QtDebug>
+#include "ultimatelyricsparser.h"
+
+UltimateLyricsParser::UltimateLyricsParser()
+{
+
+}
+
+UltimateLyricsParser::~UltimateLyricsParser()
+{
+ qDeleteAll(m_providers);
+ m_providers.clear();
+}
+
+bool UltimateLyricsParser::load(const QString &path)
+{
+ qDeleteAll(m_providers);
+ m_providers.clear();
+
+ QFile file(path);
+ if(!file.open(QIODevice::ReadOnly))
+ {
+ m_errorString = file.errorString();
+ return false;
+ }
+
+ QXmlStreamReader reader(&file);
+
+ QString parentElement;
+ QList<QPair<QString, QString> > args;
+
+ while(!reader.atEnd())
+ {
+ reader.readNext();
+
+ if(reader.isStartElement())
+ {
+ if(reader.name() == "provider")
+ {
+ LyricsProvider *provider = new LyricsProvider;
+ QXmlStreamAttributes attrs = reader.attributes();
+ provider->setName(attrs.value("name").toString());
+ provider->setTitle(attrs.value("title").toString());
+ provider->setUrl(attrs.value("url").toString());
+ provider->setCharset(attrs.value("charser").toString());
+ m_providers << provider;
+ }
+ else if(reader.name() == "urlFormat" && !m_providers.isEmpty())
+ {
+ m_providers.last()->addUrlFormat(reader.attributes().value("replace").toString(),
+ reader.attributes().value("with").toString());
+ }
+ else if(reader.name() == "extract" || reader.name() == "exclude")
+ {
+ parentElement = reader.name().toString();
+ }
+ else if(reader.name() == "invalidIndicator" && !m_providers.isEmpty())
+ {
+ m_providers.last()->addInvalidIndicator(reader.attributes().value("value").toString());
+ }
+ else if(reader.name() == "item")
+ {
+ QXmlStreamAttributes attrs = reader.attributes();
+ QString arg1, arg2;
+ if(attrs.hasAttribute("begin") && attrs.hasAttribute("end"))
+ {
+ arg1 = attrs.value("begin").toString();
+ arg2 = attrs.value("end").toString();
+ }
+ else if(attrs.hasAttribute("tag"))
+ {
+ arg1 = attrs.value("tag").toString();
+ }
+ else if(attrs.hasAttribute("url"))
+ {
+ arg1 = attrs.value("url").toString();
+ }
+ args << qMakePair(arg1, arg2);
+ }
+ }
+ else if(reader.isEndElement())
+ {
+ if(reader.name() == "extract" || reader.name() == "exclude")
+ {
+ parentElement.clear();
+ m_providers.last()->addRule(args, reader.name() == "exclude");
+ args.clear();
+ }
+ }
+
+ if(reader.hasError())
+ {
+ m_errorString = tr("%1 (line: %2)").arg(reader.errorString()).arg(reader.lineNumber());
+ return false;
+ }
+ }
+ return true;
+}
+
+const QString &UltimateLyricsParser::errorString() const
+{
+ return m_errorString;
+}
+
+const QList<LyricsProvider *> &UltimateLyricsParser::providers()
+{
+ return m_providers;
+}
diff --git a/src/plugins/General/lyrics/ultimatelyricsparser.h b/src/plugins/General/lyrics/ultimatelyricsparser.h
new file mode 100644
index 000000000..fb7ce1df6
--- /dev/null
+++ b/src/plugins/General/lyrics/ultimatelyricsparser.h
@@ -0,0 +1,26 @@
+#ifndef ULTIMATELYRICSPARSER_H
+#define ULTIMATELYRICSPARSER_H
+
+#include <QString>
+#include <QCoreApplication>
+#include <QList>
+#include "lyricsprovider.h"
+
+class UltimateLyricsParser
+{
+ Q_DECLARE_TR_FUNCTIONS(UltimateLyricsParser)
+public:
+ UltimateLyricsParser();
+ ~UltimateLyricsParser();
+
+ bool load(const QString &path);
+ const QString &errorString() const;
+ const QList<LyricsProvider *> &providers();
+
+private:
+ QString m_errorString;
+ QList<LyricsProvider *> m_providers;
+
+};
+
+#endif // ULTIMATELYRICSPARSER_H