1 /* This file is part of Clementine.
2    Copyright 2012, David Sansome <me@davidsansome.com>
3    Copyright 2014, John Maguire <john.maguire@gmail.com>
4    Copyright 2014, Krzysztof Sobiecki <sobkas@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 "addpodcastbyurl.h"
21 
22 #include <QClipboard>
23 #include <QNetworkReply>
24 #include <QMessageBox>
25 
26 #include "podcastdiscoverymodel.h"
27 #include "podcasturlloader.h"
28 #include "ui_addpodcastbyurl.h"
29 #include "core/closure.h"
30 #include "ui/iconloader.h"
31 
AddPodcastByUrl(Application * app,QWidget * parent)32 AddPodcastByUrl::AddPodcastByUrl(Application* app, QWidget* parent)
33     : AddPodcastPage(app, parent),
34       ui_(new Ui_AddPodcastByUrl),
35       loader_(new PodcastUrlLoader(this)) {
36   ui_->setupUi(this);
37   connect(ui_->go, SIGNAL(clicked()), SLOT(GoClicked()));
38   setWindowIcon(IconLoader::Load("podcast", IconLoader::Provider));
39 }
40 
~AddPodcastByUrl()41 AddPodcastByUrl::~AddPodcastByUrl() { delete ui_; }
42 
SetUrlAndGo(const QUrl & url)43 void AddPodcastByUrl::SetUrlAndGo(const QUrl& url) {
44   ui_->url->setText(url.toString());
45   GoClicked();
46 }
47 
SetOpml(const OpmlContainer & opml)48 void AddPodcastByUrl::SetOpml(const OpmlContainer& opml) {
49   ui_->url->setText(opml.url.toString());
50   model()->clear();
51   model()->CreateOpmlContainerItems(opml, model()->invisibleRootItem());
52 }
53 
GoClicked()54 void AddPodcastByUrl::GoClicked() {
55   emit Busy(true);
56   model()->clear();
57 
58   PodcastUrlLoaderReply* reply = loader_->Load(ui_->url->text());
59   ui_->url->setText(reply->url().toString());
60 
61   NewClosure(reply, SIGNAL(Finished(bool)), this,
62              SLOT(RequestFinished(PodcastUrlLoaderReply*)), reply);
63 }
64 
RequestFinished(PodcastUrlLoaderReply * reply)65 void AddPodcastByUrl::RequestFinished(PodcastUrlLoaderReply* reply) {
66   reply->deleteLater();
67 
68   emit Busy(false);
69 
70   if (!reply->is_success()) {
71     QMessageBox::warning(this, tr("Failed to load podcast"),
72                          reply->error_text(), QMessageBox::Close);
73     return;
74   }
75 
76   switch (reply->result_type()) {
77     case PodcastUrlLoaderReply::Type_Podcast:
78       for (const Podcast& podcast : reply->podcast_results()) {
79         model()->appendRow(model()->CreatePodcastItem(podcast));
80       }
81       break;
82 
83     case PodcastUrlLoaderReply::Type_Opml:
84       model()->CreateOpmlContainerItems(reply->opml_results(),
85                                         model()->invisibleRootItem());
86       break;
87   }
88 }
89 
Show()90 void AddPodcastByUrl::Show() {
91   ui_->url->setFocus();
92 
93   const QClipboard* clipboard = QApplication::clipboard();
94   QStringList contents;
95   contents << clipboard->text(QClipboard::Selection)
96            << clipboard->text(QClipboard::Clipboard);
97 
98   for (const QString& content : contents) {
99     if (content.contains("://")) {
100       ui_->url->setText(content);
101       return;
102     }
103   }
104 }
105