2012-03-07 13:27:31 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2014-11-01 19:38:39 +01:00
|
|
|
Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
|
2014-11-02 19:50:39 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-02-07 16:34:20 +01:00
|
|
|
|
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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QNetworkReply>
|
2015-04-11 22:52:31 +02:00
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
#include <QJsonArray>
|
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_itunessearchpage.h"
|
2015-10-14 03:01:08 +02:00
|
|
|
#include "ui/iconloader.h"
|
2014-12-13 02:24:20 +01:00
|
|
|
|
2012-03-07 13:27:31 +01:00
|
|
|
const char* ITunesSearchPage::kUrlBase =
|
2014-02-07 16:34:20 +01:00
|
|
|
"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)
|
2014-02-07 16:34:20 +01:00
|
|
|
: 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()));
|
2015-10-14 03:01:08 +02:00
|
|
|
setWindowIcon(IconLoader::Load("itunes", IconLoader::Provider));
|
2012-03-07 13:27:31 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +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));
|
2014-02-07 16:34:20 +01:00
|
|
|
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) {
|
2014-02-07 16:34:20 +01:00
|
|
|
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;
|
|
|
|
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) {
|
2014-02-07 16:34:20 +01:00
|
|
|
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?
|
2015-04-11 22:52:31 +02:00
|
|
|
if (!json_data["errorMessage"].isUndefined()) {
|
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));
|
|
|
|
}
|
|
|
|
}
|
2012-03-09 20:12:45 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void ITunesSearchPage::Show() { ui_->query->setFocus(); }
|