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 "gpoddertoptagspage.h"
21 
22 #include <QMessageBox>
23 
24 #include "gpoddertoptagsmodel.h"
25 #include "core/closure.h"
26 #include "core/network.h"
27 #include "ui/iconloader.h"
28 
29 const int GPodderTopTagsPage::kMaxTagCount = 100;
30 
GPodderTopTagsPage(Application * app,QWidget * parent)31 GPodderTopTagsPage::GPodderTopTagsPage(Application* app, QWidget* parent)
32     : AddPodcastPage(app, parent),
33       network_(new NetworkAccessManager(this)),
34       api_(new mygpo::ApiRequest(network_)),
35       done_initial_load_(false) {
36   setWindowTitle(tr("gpodder.net directory"));
37   setWindowIcon(IconLoader::Load("mygpo", IconLoader::Provider));
38 
39   SetModel(new GPodderTopTagsModel(api_, app, this));
40 }
41 
~GPodderTopTagsPage()42 GPodderTopTagsPage::~GPodderTopTagsPage() { delete api_; }
43 
Show()44 void GPodderTopTagsPage::Show() {
45   if (!done_initial_load_) {
46     // Start the request for list of top-level tags
47     emit Busy(true);
48     done_initial_load_ = true;
49 
50     mygpo::TagListPtr tag_list(api_->topTags(kMaxTagCount));
51     NewClosure(tag_list, SIGNAL(finished()), this,
52                SLOT(TagListLoaded(mygpo::TagListPtr)), tag_list);
53     NewClosure(tag_list, SIGNAL(parseError()), this,
54                SLOT(TagListFailed(mygpo::TagListPtr)), tag_list);
55     NewClosure(tag_list, SIGNAL(requestError(QNetworkReply::NetworkError)),
56                this, SLOT(TagListFailed(mygpo::TagListPtr)), tag_list);
57   }
58 }
59 
TagListLoaded(mygpo::TagListPtr tag_list)60 void GPodderTopTagsPage::TagListLoaded(mygpo::TagListPtr tag_list) {
61   emit Busy(false);
62 
63   for (mygpo::TagPtr tag : tag_list->list()) {
64     model()->appendRow(model()->CreateFolder(tag->tag()));
65   }
66 }
67 
TagListFailed(mygpo::TagListPtr list)68 void GPodderTopTagsPage::TagListFailed(mygpo::TagListPtr list) {
69   emit Busy(false);
70   done_initial_load_ = false;
71 
72   if (QMessageBox::warning(
73           nullptr, tr("Failed to fetch directory"),
74           tr("There was a problem communicating with gpodder.net"),
75           QMessageBox::Retry | QMessageBox::Close,
76           QMessageBox::Retry) != QMessageBox::Retry) {
77     return;
78   }
79 
80   // Try doing the search again.
81   Show();
82 }
83