1 /* This file is part of Clementine.
2    Copyright 2012, David Sansome <me@davidsansome.com>
3    Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
4    Copyright 2014, John Maguire <john.maguire@gmail.com>
5 
6    Clementine is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Clementine is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "gpoddersearchpage.h"
21 
22 #include <QMessageBox>
23 
24 #include "podcast.h"
25 #include "podcastdiscoverymodel.h"
26 #include "ui_gpoddersearchpage.h"
27 #include "core/closure.h"
28 #include "core/network.h"
29 #include "ui/iconloader.h"
30 
GPodderSearchPage(Application * app,QWidget * parent)31 GPodderSearchPage::GPodderSearchPage(Application* app, QWidget* parent)
32     : AddPodcastPage(app, parent),
33       ui_(new Ui_GPodderSearchPage),
34       network_(new NetworkAccessManager(this)),
35       api_(new mygpo::ApiRequest(network_)) {
36   ui_->setupUi(this);
37   connect(ui_->search, SIGNAL(clicked()), SLOT(SearchClicked()));
38   setWindowIcon(IconLoader::Load("mygpo", IconLoader::Provider));
39 }
40 
~GPodderSearchPage()41 GPodderSearchPage::~GPodderSearchPage() {
42   delete ui_;
43   delete api_;
44 }
45 
SearchClicked()46 void GPodderSearchPage::SearchClicked() {
47   emit Busy(true);
48 
49   mygpo::PodcastListPtr list(api_->search(ui_->query->text()));
50   NewClosure(list, SIGNAL(finished()), this,
51              SLOT(SearchFinished(mygpo::PodcastListPtr)), list);
52   NewClosure(list, SIGNAL(parseError()), this,
53              SLOT(SearchFailed(mygpo::PodcastListPtr)), list);
54   NewClosure(list, SIGNAL(requestError(QNetworkReply::NetworkError)), this,
55              SLOT(SearchFailed(mygpo::PodcastListPtr)), list);
56 }
57 
SearchFinished(mygpo::PodcastListPtr list)58 void GPodderSearchPage::SearchFinished(mygpo::PodcastListPtr list) {
59   emit Busy(false);
60 
61   model()->clear();
62 
63   for (mygpo::PodcastPtr gpo_podcast : list->list()) {
64     Podcast podcast;
65     podcast.InitFromGpo(gpo_podcast.data());
66 
67     model()->appendRow(model()->CreatePodcastItem(podcast));
68   }
69 }
70 
SearchFailed(mygpo::PodcastListPtr list)71 void GPodderSearchPage::SearchFailed(mygpo::PodcastListPtr list) {
72   emit Busy(false);
73 
74   model()->clear();
75 
76   if (QMessageBox::warning(
77           nullptr, tr("Failed to fetch podcasts"),
78           tr("There was a problem communicating with gpodder.net"),
79           QMessageBox::Retry | QMessageBox::Close,
80           QMessageBox::Retry) != QMessageBox::Retry) {
81     return;
82   }
83 
84   // Try doing the search again.
85   SearchClicked();
86 }
87 
Show()88 void GPodderSearchPage::Show() { ui_->query->setFocus(); }
89