2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-12-19 00:46:38 +01:00
|
|
|
Copyright 2010-2012, David Sansome <me@davidsansome.com>
|
2014-12-17 19:02:21 +01:00
|
|
|
Copyright 2011, Tyler Rhodes <tyler.s.rhodes@gmail.com>
|
|
|
|
Copyright 2011, Paweł Bara <keirangtp@gmail.com>
|
|
|
|
Copyright 2012, Arnaud Bienner <arnaud.bienner@gmail.com>
|
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2010-03-24 00:11:46 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
2014-12-18 23:35:21 +01:00
|
|
|
#include "internet/core/internetservice.h"
|
2014-12-19 00:40:30 +01:00
|
|
|
|
2014-12-27 16:40:13 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QIcon>
|
2020-02-14 08:50:15 +01:00
|
|
|
#include <QJsonDocument>
|
2014-12-19 00:40:30 +01:00
|
|
|
#include <QMenu>
|
2014-12-27 16:40:13 +01:00
|
|
|
#include <QMessageBox>
|
2020-02-14 08:50:15 +01:00
|
|
|
#include <QNetworkReply>
|
2014-12-27 16:40:13 +01:00
|
|
|
#include <QPushButton>
|
2014-12-19 00:40:30 +01:00
|
|
|
#include <QStandardItem>
|
|
|
|
|
2020-02-14 08:50:15 +01:00
|
|
|
#include "core/application.h"
|
2011-05-30 16:54:48 +02:00
|
|
|
#include "core/logging.h"
|
2011-01-10 23:26:13 +01:00
|
|
|
#include "core/mergedproxymodel.h"
|
|
|
|
#include "core/mimedata.h"
|
2020-02-14 08:50:15 +01:00
|
|
|
#include "internet/core/internetmodel.h"
|
2011-02-10 23:24:17 +01:00
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
InternetService::InternetService(const QString& name, Application* app,
|
|
|
|
InternetModel* model, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent),
|
|
|
|
app_(app),
|
|
|
|
model_(model),
|
|
|
|
name_(name),
|
2016-02-11 18:07:00 +01:00
|
|
|
append_to_playlist_([&]() {
|
|
|
|
QAction* action = new QAction(
|
2016-02-11 14:45:50 +01:00
|
|
|
IconLoader::Load("media-playback-start", IconLoader::Base),
|
|
|
|
tr("Append to current playlist"), nullptr);
|
2016-02-11 18:07:00 +01:00
|
|
|
connect(action, SIGNAL(triggered()), this, SLOT(AppendToPlaylist()));
|
|
|
|
return action;
|
2016-02-11 14:45:50 +01:00
|
|
|
}),
|
2016-02-11 18:07:00 +01:00
|
|
|
replace_playlist_([&]() {
|
|
|
|
QAction* action = new QAction(
|
2016-02-11 14:45:50 +01:00
|
|
|
IconLoader::Load("media-playback-start", IconLoader::Base),
|
|
|
|
tr("Replace current playlist"), nullptr);
|
2016-02-11 18:07:00 +01:00
|
|
|
connect(action, SIGNAL(triggered()), this, SLOT(ReplacePlaylist()));
|
|
|
|
return action;
|
2016-02-11 14:45:50 +01:00
|
|
|
}),
|
2016-02-11 18:07:00 +01:00
|
|
|
open_in_new_playlist_([&]() {
|
|
|
|
QAction* action =
|
|
|
|
new QAction(IconLoader::Load("document-new", IconLoader::Base),
|
|
|
|
tr("Open in new playlist"), nullptr);
|
|
|
|
connect(action, SIGNAL(triggered()), this, SLOT(OpenInNewPlaylist()));
|
|
|
|
return action;
|
2016-02-11 14:45:50 +01:00
|
|
|
}),
|
|
|
|
separator_([]() {
|
|
|
|
QAction* action = new QAction(nullptr);
|
|
|
|
action->setSeparator(true);
|
|
|
|
return action;
|
|
|
|
}) {}
|
2010-01-18 03:23:55 +01:00
|
|
|
|
2014-12-27 16:40:13 +01:00
|
|
|
void InternetService::ShowUrlBox(const QString& title, const QString& url) {
|
|
|
|
QMessageBox url_box;
|
|
|
|
url_box.setWindowTitle(title);
|
|
|
|
url_box.setWindowIcon(QIcon(":/icon.png"));
|
|
|
|
url_box.setText(url);
|
|
|
|
url_box.setStandardButtons(QMessageBox::Ok);
|
|
|
|
QPushButton* copy_to_clipboard_button =
|
|
|
|
url_box.addButton(tr("Copy to clipboard"), QMessageBox::ActionRole);
|
|
|
|
|
|
|
|
url_box.exec();
|
|
|
|
|
|
|
|
if (url_box.clickedButton() == copy_to_clipboard_button) {
|
|
|
|
QApplication::clipboard()->setText(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
QList<QAction*> InternetService::GetPlaylistActions() {
|
2011-02-10 23:24:17 +01:00
|
|
|
return QList<QAction*>() << GetAppendToPlaylistAction()
|
|
|
|
<< GetReplacePlaylistAction()
|
2016-02-11 14:45:50 +01:00
|
|
|
<< GetOpenInNewPlaylistAction() << separator_.get();
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
QAction* InternetService::GetAppendToPlaylistAction() {
|
2016-02-11 14:45:50 +01:00
|
|
|
return append_to_playlist_.get();
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
QAction* InternetService::GetReplacePlaylistAction() {
|
2016-02-11 14:45:50 +01:00
|
|
|
return replace_playlist_.get();
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
QAction* InternetService::GetOpenInNewPlaylistAction() {
|
2016-02-11 14:45:50 +01:00
|
|
|
return open_in_new_playlist_.get();
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void InternetService::AddItemToPlaylist(const QModelIndex& index,
|
|
|
|
AddMode add_mode) {
|
2011-02-09 19:23:14 +01:00
|
|
|
AddItemsToPlaylist(QModelIndexList() << index, add_mode);
|
2011-01-10 23:26:13 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void InternetService::AddItemsToPlaylist(const QModelIndexList& indexes,
|
|
|
|
AddMode add_mode) {
|
2011-01-10 23:26:13 +01:00
|
|
|
QMimeData* data = model()->merged_model()->mimeData(
|
2014-02-07 16:34:20 +01:00
|
|
|
model()->merged_model()->mapFromSource(indexes));
|
2011-01-10 23:26:13 +01:00
|
|
|
if (MimeData* mime_data = qobject_cast<MimeData*>(data)) {
|
2011-02-09 19:23:14 +01:00
|
|
|
mime_data->clear_first_ = add_mode == AddMode_Replace;
|
2011-02-16 19:29:35 +01:00
|
|
|
mime_data->open_in_new_playlist_ = add_mode == AddMode_OpenInNew;
|
2011-01-10 23:26:13 +01:00
|
|
|
}
|
|
|
|
emit AddToPlaylistSignal(data);
|
|
|
|
}
|
2011-02-10 23:24:17 +01:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetService::AppendToPlaylist() {
|
2012-03-11 15:44:43 +01:00
|
|
|
AddItemsToPlaylist(model()->selected_indexes(), AddMode_Append);
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetService::ReplacePlaylist() {
|
2012-03-11 15:44:43 +01:00
|
|
|
AddItemsToPlaylist(model()->selected_indexes(), AddMode_Replace);
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
void InternetService::OpenInNewPlaylist() {
|
2012-03-11 15:44:43 +01:00
|
|
|
AddItemsToPlaylist(model()->selected_indexes(), AddMode_OpenInNew);
|
2011-02-10 23:24:17 +01:00
|
|
|
}
|
2012-06-27 21:19:30 +02:00
|
|
|
|
|
|
|
QStandardItem* InternetService::CreateSongItem(const Song& song) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItem* item = new QStandardItem(song.PrettyTitleWithArtist());
|
|
|
|
item->setData(InternetModel::Type_Track, InternetModel::Role_Type);
|
|
|
|
item->setData(QVariant::fromValue(song), InternetModel::Role_SongMetadata);
|
|
|
|
item->setData(InternetModel::PlayBehaviour_SingleItem,
|
|
|
|
InternetModel::Role_PlayBehaviour);
|
|
|
|
item->setData(song.url(), InternetModel::Role_Url);
|
|
|
|
|
|
|
|
return item;
|
2012-06-27 21:19:30 +02:00
|
|
|
}
|
2020-02-14 08:50:15 +01:00
|
|
|
|
|
|
|
QJsonDocument InternetService::ParseJsonReply(QNetworkReply* reply) {
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
app_->AddError(
|
|
|
|
tr("%1 request failed:\n%2").arg(name_).arg(reply->errorString()));
|
|
|
|
return QJsonDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(reply->readAll(), &error);
|
|
|
|
if (error.error != QJsonParseError::NoError) {
|
|
|
|
app_->AddError(tr("Failed to parse %1 response:\n%2")
|
|
|
|
.arg(name_)
|
|
|
|
.arg(error.errorString()));
|
|
|
|
return QJsonDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
return document;
|
|
|
|
}
|