1 //////////////////////////////////////////////////////////////////////
2 //
3 // BeeBEEP Copyright (C) 2010-2021 Marco Mastroddi
4 //
5 // BeeBEEP is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published
7 // by the Free Software Foundation, either version 3 of the License,
8 // or (at your option) any later version.
9 //
10 // BeeBEEP is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with BeeBEEP. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Author: Marco Mastroddi <marco.mastroddi(AT)gmail.com>
19 //
20 // $Id: HttpDownloader.h 1455 2020-12-23 10:17:53Z mastroddi $
21 //
22 //////////////////////////////////////////////////////////////////////
23 
24 #ifndef BEEBEEP_DOWNLOADER_H
25 #define BEEBEEP_DOWNLOADER_H
26 
27 #include "Config.h"
28 class QSslError;
29 
30 
31 class HttpDownloader: public QObject
32 {
33   Q_OBJECT
34 
35 public:
36   explicit HttpDownloader( QObject* parent = Q_NULLPTR );
37 
38   inline void addUrl( const QUrl& );
39   inline const QStringList& downloadedFilePaths() const;
40   inline void setOverwriteExistingFiles( bool );
41   void cleanUp();
42 
43 public slots:
44   void startDownload();
45 
46 signals:
47   void downloadCompleted( const QString& );
48   void jobFinished();
49 
50 protected slots:
51   void onReplyFinished( QNetworkReply* );
52   void onSslErrors( const QList<QSslError>& );
53   void onDownloadProgress( qint64, qint64 );
54 
55 protected:
56   QString filePathFromFileName( const QString& );
57   QString filePathFromUrl( const QUrl& );
58   QString fileNameFromUrl( const QUrl& );
59   void doDownload( const QUrl& );
60   bool saveToDisk( const QString&, QIODevice* );
61 
62 private:
63   QNetworkAccessManager* mp_manager;
64   QList<QUrl> m_queuedUrls;
65   QStringList m_downloadedFilePaths;
66   bool m_overwriteExistingFiles;
67 
68 };
69 
70 // Inline Functions
addUrl(const QUrl & new_value)71 inline void HttpDownloader::addUrl( const QUrl& new_value ) { m_queuedUrls.append( new_value ); }
downloadedFilePaths()72 inline const QStringList& HttpDownloader::downloadedFilePaths() const { return m_downloadedFilePaths; }
setOverwriteExistingFiles(bool new_value)73 inline void HttpDownloader::setOverwriteExistingFiles( bool new_value ) { m_overwriteExistingFiles = new_value; }
74 
75 #endif // BEEBEEP_DOWNLOADER_H
76