Clementine-audio-player-Mac.../src/internet/podcasts/itunessearchpage.cpp

117 lines
3.8 KiB
C++
Raw Normal View History

2012-03-07 13:27:31 +01:00
/* This file is part of Clementine.
Copyright 2012, David Sansome <me@davidsansome.com>
Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
2014-11-02 19:50:39 +01:00
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
2012-03-07 13:27:31 +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.
2012-03-07 13:27:31 +01:00
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.
2012-03-07 13:27:31 +01:00
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "itunessearchpage.h"
2020-09-18 16:15:19 +02:00
#include <QJsonArray>
2015-04-11 22:52:31 +02:00
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
2020-09-18 16:15:19 +02:00
#include <QMessageBox>
#include <QNetworkReply>
#include <QUrlQuery>
2012-03-07 13:27:31 +01:00
2014-12-13 02:24:20 +01:00
#include "core/closure.h"
#include "core/network.h"
#include "podcast.h"
#include "podcastdiscoverymodel.h"
#include "ui/iconloader.h"
2020-09-18 16:15:19 +02:00
#include "ui_itunessearchpage.h"
2014-12-13 02:24:20 +01:00
2012-03-07 13:27:31 +01:00
const char* ITunesSearchPage::kUrlBase =
"http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/"
"wa/wsSearch?country=US&media=podcast";
2012-03-07 13:27:31 +01:00
ITunesSearchPage::ITunesSearchPage(Application* app, QWidget* parent)
: AddPodcastPage(app, parent),
ui_(new Ui_ITunesSearchPage),
network_(new NetworkAccessManager(this)) {
2012-03-07 13:27:31 +01:00
ui_->setupUi(this);
connect(ui_->search, SIGNAL(clicked()), SLOT(SearchClicked()));
setWindowIcon(IconLoader::Load("itunes", IconLoader::Provider));
2012-03-07 13:27:31 +01:00
}
ITunesSearchPage::~ITunesSearchPage() { delete ui_; }
2012-03-07 13:27:31 +01:00
void ITunesSearchPage::SearchClicked() {
emit Busy(true);
QUrl url(QUrl::fromEncoded(kUrlBase));
2015-04-11 22:52:31 +02:00
QUrlQuery url_query;
url_query.addQueryItem("term", ui_->query->text());
url.setQuery(url_query);
2012-03-07 13:27:31 +01:00
QNetworkReply* reply = network_->get(QNetworkRequest(url));
NewClosure(reply, SIGNAL(finished()), this,
SLOT(SearchFinished(QNetworkReply*)), reply);
2012-03-07 13:27:31 +01:00
}
void ITunesSearchPage::SearchFinished(QNetworkReply* reply) {
reply->deleteLater();
emit Busy(false);
model()->clear();
// Was there a network error?
if (reply->error() != QNetworkReply::NoError) {
QMessageBox::warning(this, tr("Failed to fetch podcasts"),
reply->errorString());
2012-03-07 13:27:31 +01:00
return;
}
2015-04-11 22:52:31 +02:00
QJsonParseError error;
2020-09-18 16:15:19 +02:00
QJsonDocument json_document =
QJsonDocument::fromJson(reply->readAll(), &error);
2012-03-07 13:27:31 +01:00
2015-04-11 22:52:31 +02:00
if (error.error != QJsonParseError::NoError) {
QMessageBox::warning(
this, tr("Failed to fetch podcasts"),
tr("There was a problem parsing the response from the iTunes Store"));
2012-03-07 13:27:31 +01:00
return;
}
2015-04-11 22:52:31 +02:00
QJsonObject json_data = json_document.object();
2012-03-07 13:27:31 +01:00
// Was there an error message in the JSON?
if (json_data.contains("errorMessage")) {
2012-03-07 13:27:31 +01:00
QMessageBox::warning(this, tr("Failed to fetch podcasts"),
2015-04-11 22:52:31 +02:00
json_data["errorMessage"].toString());
2012-03-07 13:27:31 +01:00
return;
}
2015-04-11 22:52:31 +02:00
for (const QJsonValue& result : json_data["results"].toArray()) {
QJsonObject json_result = result.toObject();
if (json_result["kind"].toString() != "podcast") {
2012-03-07 13:27:31 +01:00
continue;
}
Podcast podcast;
2015-04-11 22:52:31 +02:00
podcast.set_author(json_result["artistName"].toString());
podcast.set_title(json_result["trackName"].toString());
podcast.set_url(QUrl(json_result["feedUrl"].toString()));
podcast.set_link(QUrl(json_result["trackViewUrl"].toString()));
podcast.set_image_url_small(QUrl(json_result["artworkUrl30"].toString()));
podcast.set_image_url_large(QUrl(json_result["artworkUrl100"].toString()));
2012-03-07 13:27:31 +01:00
model()->appendRow(model()->CreatePodcastItem(podcast));
}
}
void ITunesSearchPage::Show() { ui_->query->setFocus(); }