Add Deezer support

This commit is contained in:
Jonas Kvinge
2018-10-14 00:08:33 +02:00
parent 4aad44cb62
commit 0a81fa99fc
78 changed files with 5309 additions and 630 deletions

View File

@@ -31,6 +31,7 @@
#include "internetmodel.h"
#include "internetservice.h"
#include "tidal/tidalservice.h"
#include "deezer/deezerservice.h"
QMap<Song::Source, InternetService*>* InternetModel::sServices = nullptr;
@@ -41,6 +42,7 @@ InternetModel::InternetModel(Application *app, QObject *parent)
if (!sServices) sServices = new QMap<Song::Source, InternetService*>;
Q_ASSERT(sServices->isEmpty());
AddService(new TidalService(app, this));
AddService(new DeezerService(app, this));
}

View File

@@ -106,7 +106,6 @@ class InternetModel : public QStandardItemModel {
// Needs to be static for InternetPlaylistItem::restore
static InternetService *ServiceBySource(const Song::Source &source);
//static InternetService *ServiceByName(const QString &name);
template <typename T>
static T *Service() {

View File

@@ -0,0 +1,111 @@
/*
* This file was part of Clementine.
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
* Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "localredirectserver.h"
#include <QApplication>
#include <QBuffer>
#include <QFile>
#include <QRegExp>
#include <QStyle>
#include <QTcpServer>
#include <QTcpSocket>
#include "core/closure.h"
LocalRedirectServer::LocalRedirectServer(QObject* parent)
: QObject(parent), server_(new QTcpServer(this)) {}
void LocalRedirectServer::Listen() {
server_->listen(QHostAddress::LocalHost);
// We have to calculate this and store it now as the server port is cleared once we close the socket.
url_.setScheme("http");
url_.setHost("localhost");
url_.setPort(server_->serverPort());
url_.setPath("/");
connect(server_, SIGNAL(newConnection()), SLOT(NewConnection()));
}
void LocalRedirectServer::NewConnection() {
QTcpSocket* socket = server_->nextPendingConnection();
server_->close();
QByteArray buffer;
NewClosure(socket, SIGNAL(readyRead()), this, SLOT(ReadyRead(QTcpSocket*, QByteArray)), socket, buffer);
}
void LocalRedirectServer::ReadyRead(QTcpSocket* socket, QByteArray buffer) {
buffer.append(socket->readAll());
if (socket->atEnd() || buffer.endsWith("\r\n\r\n")) {
WriteTemplate(socket);
socket->deleteLater();
request_url_ = ParseUrlFromRequest(buffer);
emit Finished();
}
else {
NewClosure(socket, SIGNAL(readyRead()), this, SLOT(ReadyReady(QTcpSocket*, QByteArray)), socket, buffer);
}
}
void LocalRedirectServer::WriteTemplate(QTcpSocket* socket) const {
QFile page_file(":/misc/oauthsuccess.html");
page_file.open(QIODevice::ReadOnly);
QString page_data = QString::fromUtf8(page_file.readAll());
QRegExp tr_regexp("tr\\(\"([^\"]+)\"\\)");
int offset = 0;
forever {
offset = tr_regexp.indexIn(page_data, offset);
if (offset == -1) {
break;
}
page_data.replace(offset, tr_regexp.matchedLength(), tr(tr_regexp.cap(1).toUtf8()));
offset += tr_regexp.matchedLength();
}
QBuffer image_buffer;
image_buffer.open(QIODevice::ReadWrite);
QApplication::style()
->standardIcon(QStyle::SP_DialogOkButton)
.pixmap(16)
.toImage()
.save(&image_buffer, "PNG");
page_data.replace("@IMAGE_DATA@", image_buffer.data().toBase64());
socket->write("HTTP/1.0 200 OK\r\n");
socket->write("Content-type: text/html;charset=UTF-8\r\n");
socket->write("\r\n\r\n");
socket->write(page_data.toUtf8());
socket->flush();
}
QUrl LocalRedirectServer::ParseUrlFromRequest(const QByteArray& request) const {
QList<QByteArray> lines = request.split('\r');
const QByteArray& request_line = lines[0];
QByteArray path = request_line.split(' ')[1];
QUrl base_url = url();
QUrl request_url(base_url.toString() + path.mid(1), QUrl::StrictMode);
return request_url;
}

View File

@@ -0,0 +1,63 @@
/*
* This file was part of Clementine.
* Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
* Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef LOCALREDIRECTSERVER_H
#define LOCALREDIRECTSERVER_H
#include <QByteArray>
#include <QObject>
#include <QUrl>
class QTcpServer;
class QTcpSocket;
class LocalRedirectServer : public QObject {
Q_OBJECT
public:
explicit LocalRedirectServer(QObject* parent = nullptr);
// Causes the server to listen for _one_ request.
void Listen();
// Returns the HTTP URL of this server.
const QUrl& url() const { return url_; }
// Returns the URL requested by the OAuth redirect.
const QUrl& request_url() const { return request_url_; }
signals:
void Finished();
private slots:
void NewConnection();
void ReadyRead(QTcpSocket* socket, QByteArray buffer);
private:
void WriteTemplate(QTcpSocket* socket) const;
QUrl ParseUrlFromRequest(const QByteArray& request) const;
private:
QTcpServer* server_;
QUrl url_;
QUrl request_url_;
};
#endif