2011-04-29 21:44:51 +02:00
|
|
|
/* This file is part of Clementine.
|
2014-12-17 19:02:21 +01:00
|
|
|
Copyright 2011-2012, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2011-04-29 21:44:51 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spotifyblobdownloader.h"
|
2014-02-06 19:46:08 +01:00
|
|
|
|
|
|
|
#include "config.h"
|
2011-04-29 21:44:51 +02:00
|
|
|
#include "spotifyservice.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
2011-11-05 02:12:28 +01:00
|
|
|
#include "core/utilities.h"
|
2011-04-29 21:44:51 +02:00
|
|
|
|
2012-02-26 16:05:46 +01:00
|
|
|
#include <QCoreApplication>
|
2011-04-29 21:44:51 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QProgressDialog>
|
2011-11-05 16:31:29 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_QCA
|
2014-02-07 16:34:20 +01:00
|
|
|
#include <QtCrypto>
|
|
|
|
#endif // HAVE_QCA
|
2011-11-05 02:12:28 +01:00
|
|
|
|
2014-02-06 19:46:08 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
2014-02-07 16:34:20 +01:00
|
|
|
#include <unistd.h>
|
2014-02-06 19:46:08 +01:00
|
|
|
#endif
|
|
|
|
|
2011-11-05 02:12:28 +01:00
|
|
|
const char* SpotifyBlobDownloader::kSignatureSuffix = ".sha1";
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SpotifyBlobDownloader::SpotifyBlobDownloader(const QString& version,
|
|
|
|
const QString& path,
|
|
|
|
QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
version_(version),
|
|
|
|
path_(path),
|
|
|
|
network_(new NetworkAccessManager(this)),
|
|
|
|
progress_(new QProgressDialog(tr("Downloading Spotify plugin"),
|
|
|
|
tr("Cancel"), 0, 0)) {
|
2011-04-29 21:44:51 +02:00
|
|
|
progress_->setWindowTitle(QCoreApplication::applicationName());
|
|
|
|
connect(progress_, SIGNAL(canceled()), SLOT(Cancel()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SpotifyBlobDownloader::~SpotifyBlobDownloader() {
|
|
|
|
qDeleteAll(replies_);
|
|
|
|
replies_.clear();
|
|
|
|
|
|
|
|
delete progress_;
|
|
|
|
}
|
|
|
|
|
2011-04-30 14:31:20 +02:00
|
|
|
bool SpotifyBlobDownloader::Prompt() {
|
2014-02-07 16:34:20 +01:00
|
|
|
QMessageBox::StandardButton ret = QMessageBox::question(
|
|
|
|
nullptr, tr("Spotify plugin not installed"),
|
|
|
|
tr("An additional plugin is required to use Spotify in Clementine. "
|
|
|
|
"Would you like to download and install it now?"),
|
2011-04-30 14:31:20 +02:00
|
|
|
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
|
|
|
|
return ret == QMessageBox::Yes;
|
|
|
|
}
|
|
|
|
|
2011-04-29 21:44:51 +02:00
|
|
|
void SpotifyBlobDownloader::Start() {
|
|
|
|
qDeleteAll(replies_);
|
|
|
|
replies_.clear();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const QStringList filenames =
|
|
|
|
QStringList() << "blob"
|
|
|
|
<< "blob" + QString(kSignatureSuffix)
|
|
|
|
<< "libspotify.so.12.1.45"
|
|
|
|
<< "libspotify.so.12.1.45" + QString(kSignatureSuffix);
|
2011-04-29 21:44:51 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& filename : filenames) {
|
2014-02-07 16:34:20 +01:00
|
|
|
const QUrl url(SpotifyService::kBlobDownloadUrl + version_ + "/" +
|
|
|
|
filename);
|
2011-04-29 21:44:51 +02:00
|
|
|
qLog(Info) << "Downloading" << url;
|
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(QNetworkRequest(url));
|
|
|
|
connect(reply, SIGNAL(finished()), SLOT(ReplyFinished()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
|
|
|
SLOT(ReplyProgress()));
|
2011-04-29 21:44:51 +02:00
|
|
|
|
|
|
|
replies_ << reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyBlobDownloader::ReplyFinished() {
|
|
|
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
// Handle network errors
|
|
|
|
ShowError(reply->errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is everything finished?
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkReply* reply : replies_) {
|
2011-04-29 21:44:51 +02:00
|
|
|
if (!reply->isFinished()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
// Read files into memory first.
|
|
|
|
QMap<QString, QByteArray> file_data;
|
|
|
|
QStringList signature_filenames;
|
2011-04-29 21:44:51 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkReply* reply : replies_) {
|
2011-04-29 21:44:51 +02:00
|
|
|
const QString filename = reply->url().path().section('/', -1, -1);
|
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
if (filename.endsWith(kSignatureSuffix)) {
|
|
|
|
signature_filenames << filename;
|
2011-11-05 02:12:28 +01:00
|
|
|
}
|
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
file_data[filename] = reply->readAll();
|
2011-04-29 21:44:51 +02:00
|
|
|
}
|
|
|
|
|
2011-11-05 16:31:29 +01:00
|
|
|
#ifdef HAVE_QCA
|
2011-11-05 02:12:28 +01:00
|
|
|
// Load the public key
|
|
|
|
QCA::ConvertResult conversion_result;
|
2014-02-07 16:34:20 +01:00
|
|
|
QCA::PublicKey key = QCA::PublicKey::fromPEMFile(
|
|
|
|
":/clementine-spotify-public.pem", &conversion_result);
|
2011-11-05 02:12:28 +01:00
|
|
|
if (QCA::ConvertGood != conversion_result) {
|
|
|
|
ShowError("Failed to load Spotify public key");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify signatures
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& signature_filename : signature_filenames) {
|
2011-11-05 16:14:22 +01:00
|
|
|
QString actual_filename = signature_filename;
|
|
|
|
actual_filename.remove(kSignatureSuffix);
|
2011-11-05 02:12:28 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Verifying" << actual_filename << "against"
|
|
|
|
<< signature_filename;
|
2011-11-05 02:12:28 +01:00
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
if (!key.verifyMessage(file_data[actual_filename],
|
2014-02-07 16:34:20 +01:00
|
|
|
file_data[signature_filename], QCA::EMSA3_SHA1)) {
|
2011-11-05 16:14:22 +01:00
|
|
|
ShowError("Invalid signature: " + actual_filename);
|
2011-11-05 02:12:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // HAVE_QCA
|
2011-11-05 02:12:28 +01:00
|
|
|
|
|
|
|
// Make the destination directory and write the files into it
|
|
|
|
QDir().mkpath(path_);
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& filename : file_data.keys()) {
|
2011-11-05 02:12:28 +01:00
|
|
|
const QString dest_path = path_ + "/" + filename;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (filename.endsWith(kSignatureSuffix)) continue;
|
2011-11-05 02:12:28 +01:00
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
qLog(Info) << "Writing" << dest_path;
|
2011-11-05 02:12:28 +01:00
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
QFile file(dest_path);
|
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
ShowError("Failed to open " + dest_path + " for writing");
|
2011-11-05 02:12:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-05 16:14:22 +01:00
|
|
|
file.write(file_data[filename]);
|
|
|
|
file.close();
|
|
|
|
file.setPermissions(QFile::Permissions(0x7755));
|
2011-11-12 19:44:48 +01:00
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
const int so_pos = filename.lastIndexOf(".so.");
|
|
|
|
if (so_pos != -1) {
|
|
|
|
QString link_path = path_ + "/" + filename.left(so_pos + 3);
|
|
|
|
QStringList version_parts = filename.mid(so_pos + 4).split('.');
|
|
|
|
|
|
|
|
while (!version_parts.isEmpty()) {
|
|
|
|
qLog(Debug) << "Linking" << dest_path << "to" << link_path;
|
2011-11-12 20:32:44 +01:00
|
|
|
int ret = symlink(dest_path.toLocal8Bit().constData(),
|
|
|
|
link_path.toLocal8Bit().constData());
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
qLog(Warning) << "Creating symlink failed with return code" << ret;
|
|
|
|
}
|
2011-11-12 19:44:48 +01:00
|
|
|
|
|
|
|
link_path += "." + version_parts.takeFirst();
|
|
|
|
}
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // Q_OS_UNIX
|
2011-11-05 16:14:22 +01:00
|
|
|
}
|
2011-11-05 02:12:28 +01:00
|
|
|
|
2011-04-29 21:44:51 +02:00
|
|
|
EmitFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyBlobDownloader::ReplyProgress() {
|
|
|
|
int progress = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkReply* reply : replies_) {
|
2011-04-29 21:44:51 +02:00
|
|
|
progress += reply->bytesAvailable();
|
|
|
|
total += reply->rawHeader("Content-Length").toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_->setMaximum(total);
|
|
|
|
progress_->setValue(progress);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SpotifyBlobDownloader::Cancel() { deleteLater(); }
|
2011-04-29 21:44:51 +02:00
|
|
|
|
|
|
|
void SpotifyBlobDownloader::ShowError(const QString& message) {
|
|
|
|
// Stop any remaining replies before showing the dialog so they don't
|
|
|
|
// carry on in the background
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QNetworkReply* reply : replies_) {
|
2011-04-29 21:44:51 +02:00
|
|
|
disconnect(reply, 0, this, 0);
|
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
qLog(Warning) << message;
|
2014-02-06 16:49:49 +01:00
|
|
|
QMessageBox::warning(nullptr, tr("Error downloading Spotify plugin"), message,
|
2011-04-29 21:44:51 +02:00
|
|
|
QMessageBox::Close);
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpotifyBlobDownloader::EmitFinished() {
|
|
|
|
emit Finished();
|
|
|
|
deleteLater();
|
|
|
|
}
|