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 #ifndef INTERNET_PODCASTS_PODCASTDOWNLOADER_H_
21 #define INTERNET_PODCASTS_PODCASTDOWNLOADER_H_
22 
23 #include "core/network.h"
24 #include "podcast.h"
25 #include "podcastepisode.h"
26 
27 #include <memory>
28 #include <QFile>
29 #include <QList>
30 #include <QObject>
31 #include <QQueue>
32 #include <QRegExp>
33 #include <QSet>
34 
35 #ifdef Q_OS_WIN
36 #include <time.h>
37 #else
38 #include <sys/time.h>
39 #endif
40 
41 class Application;
42 class PodcastBackend;
43 
44 class QNetworkAccessManager;
45 
46 namespace PodcastDownload {
47   enum State { NotDownloading, Queued, Downloading, Finished };
48 }
49 
50 class Task : public QObject {
51   Q_OBJECT
52 
53  public:
54   Task(PodcastEpisode episode, QFile* file, PodcastBackend* backend);
55   PodcastEpisode episode() const;
56 
57  signals:
58   void ProgressChanged(const PodcastEpisode& episode,
59                        PodcastDownload::State state, int percent);
60   void finished(Task* task);
61 
62  public slots:
63   void finishedPublic();
64 
65  private slots:
66   void reading();
67   void downloadProgressInternal(qint64 received, qint64 total);
68   void finishedInternal();
69 
70  private:
71   std::unique_ptr<QFile> file_;
72   PodcastEpisode episode_;
73   QNetworkRequest req_;
74   PodcastBackend* backend_;
75   std::unique_ptr<NetworkAccessManager> network_;
76   std::unique_ptr<RedirectFollower> repl;
77 };
78 
79 class PodcastDownloader : public QObject {
80   Q_OBJECT
81 
82  public:
83   explicit PodcastDownloader(Application* app, QObject* parent = nullptr);
84 
85   static const char* kSettingsGroup;
86   PodcastEpisodeList EpisodesDownloading(const PodcastEpisodeList& episodes);
87   QString DefaultDownloadDir() const;
88 
89  public slots:
90   // Adds the episode to the download queue
91   void DownloadEpisode(const PodcastEpisode& episode);
92   void cancelDownload(const PodcastEpisodeList& episodes);
93 
94  signals:
95   void ProgressChanged(const PodcastEpisode& episode,
96                        PodcastDownload::State state, int percent);
97 
98  private slots:
99   void ReloadSettings();
100 
101   void SubscriptionAdded(const Podcast& podcast);
102   void EpisodesAdded(const PodcastEpisodeList& episodes);
103 
104   void ReplyFinished(Task* task);
105 
106  private:
107   QString FilenameForEpisode(const QString& directory,
108                              const PodcastEpisode& episode) const;
109   QString SanitiseFilenameComponent(const QString& text) const;
110 
111  private:
112   Application* app_;
113   PodcastBackend* backend_;
114   QNetworkAccessManager* network_;
115 
116   QRegExp disallowed_filename_characters_;
117 
118   bool auto_download_;
119   QString download_dir_;
120 
121   QList<Task*> list_tasks_;
122 };
123 
124 #endif  // INTERNET_PODCASTS_PODCASTDOWNLOADER_H_
125