2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QWidget>
|
|
|
|
#include <QDialog>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QImage>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QLineEdit>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QUrl>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
#include "core/utilities.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "widgets/busyindicator.h"
|
2021-02-26 21:03:51 +01:00
|
|
|
#include "albumcoverimageresult.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "coverfromurldialog.h"
|
|
|
|
#include "ui_coverfromurldialog.h"
|
|
|
|
|
|
|
|
CoverFromURLDialog::CoverFromURLDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_CoverFromURLDialog), network_(new NetworkAccessManager(this)) {
|
|
|
|
|
|
|
|
ui_->setupUi(this);
|
|
|
|
ui_->busy->hide();
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CoverFromURLDialog::~CoverFromURLDialog() {
|
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
AlbumCoverImageResult CoverFromURLDialog::Exec() {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// reset state
|
|
|
|
ui_->url->setText("");;
|
2021-02-26 21:03:51 +01:00
|
|
|
last_album_cover_ = AlbumCoverImageResult();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
ui_->url->setText(clipboard->text());
|
|
|
|
|
|
|
|
exec();
|
2021-02-26 21:03:51 +01:00
|
|
|
return last_album_cover_;
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CoverFromURLDialog::accept() {
|
|
|
|
|
|
|
|
ui_->busy->show();
|
|
|
|
|
2020-08-14 20:31:04 +02:00
|
|
|
QNetworkRequest req(QUrl::fromUserInput(ui_->url->text()));
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
|
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
#else
|
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QNetworkReply *reply = network_->get(req);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(reply, &QNetworkReply::finished, this, &CoverFromURLDialog::LoadCoverFromURLFinished);
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CoverFromURLDialog::LoadCoverFromURLFinished() {
|
|
|
|
|
|
|
|
ui_->busy->hide();
|
|
|
|
|
|
|
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
QMessageBox::information(this, tr("Fetching cover error"), tr("The site you requested does not exist!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
AlbumCoverImageResult result;
|
|
|
|
result.image_data = reply->readAll();
|
|
|
|
result.image.loadFromData(result.image_data);
|
|
|
|
result.mime_type = Utilities::MimeTypeFromData(result.image_data);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-02-26 21:03:51 +01:00
|
|
|
if (!result.image.isNull()) {
|
|
|
|
last_album_cover_ = result;
|
2018-02-27 18:06:05 +01:00
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QMessageBox::information(this, tr("Fetching cover error"), tr("The site you requested is not an image!"));
|
|
|
|
}
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|