Debian Codegraph @

src/librssguard/network-web/downloadmanager.h

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #ifndef DOWNLOADMANAGER_H
4 #define DOWNLOADMANAGER_H
5
6 #include "gui/tabcontent.h"
7
8 #include <QAbstractListModel>
9 #include <QDateTime>
10 #include <QElapsedTimer>
11 #include <QFile>
12 #include <QNetworkReply>
13
14 class AutoSaver;
15 class DownloadModel;
16 class QFileIconProvider;
17 class QMimeData;
18
19 namespace Ui {
20 class DownloadItem;
21 }
22
23 namespace Ui {
24 class DownloadManager;
25 }
26
27 class RSSGUARD_DLLSPEC DownloadItem : public QWidget {
28 Q_OBJECT
29
30 friend class DownloadManager;
31 friend class DownloadModel;
32
33 public:
34 explicit DownloadItem(QNetworkReply* reply = nullptr,
35 const QString& preferred_file_name = {},
36 const std::function<void(DownloadItem*)>& run_on_finish = {},
37 QWidget* parent = nullptr);
38 virtual ~DownloadItem();
39
40 bool downloading() const;
41 bool downloadedSuccessfully() const;
42
43 qint64 bytesTotal() const;
44 qint64 bytesReceived() const;
45 double remainingTime() const;
46 double currentSpeed() const;
47 const QFile& output() const;
48
49 private slots:
50 void stop();
51 void tryAgain();
52 void openFile();
53 void openFolder();
54
55 void downloadReadyRead();
56 void error(QNetworkReply::NetworkError code);
57 void downloadProgress(qint64 bytes_received, qint64 bytes_total);
58 void metaDataChanged();
59 void finished();
60
61 signals:
62 void statusChanged();
63 void progress(qint64 bytes_received, qint64 bytes_total);
64 void downloadFinished();
65
66 private:
67 void updateInfoAndUrlLabel();
68 void getFileName();
69 void init();
70 void updateDownloadInfoLabel();
71 QString saveFileName(const QString& directory) const;
72
73 Ui::DownloadItem* m_ui;
74 QUrl m_url;
75 QFile m_output;
76 QNetworkReply* m_reply;
77 QString m_preferredFileName;
78 std::function<void(DownloadItem*)> m_runOnFinish;
79 qint64 m_bytesReceived;
80 QElapsedTimer m_downloadTime;
81 QTime m_lastProgressTime;
82 bool m_requestFileName;
83 bool m_startedSaving;
84 bool m_finishedDownloading;
85 bool m_gettingFileName;
86 bool m_canceledFileSelect;
87 };
88
89 class WebBrowser;
90 class SilentNetworkAccessManager;
91
92 class RSSGUARD_DLLSPEC DownloadManager : public TabContent {
93 Q_OBJECT
94 Q_PROPERTY(RemovePolicy removePolicy READ removePolicy WRITE setRemovePolicy NOTIFY removePolicyChanged)
95
96 friend class DownloadModel;
97
98 public:
99 enum class RemovePolicy {
100 Never,
101 OnExit,
102 OnSuccessfullDownload
103 };
104
105 Q_ENUM(RemovePolicy)
106
107 explicit DownloadManager(QWidget* parent = nullptr);
108 virtual ~DownloadManager();
109
110 virtual WebBrowser* webBrowser() const;
111
112 SilentNetworkAccessManager* networkManager() const;
113
114 int totalDownloads() const;
115 int activeDownloads() const;
116 int downloadProgress() const;
117
118 RemovePolicy removePolicy() const;
119 void setRemovePolicy(RemovePolicy policy);
120
121 void setDownloadDirectory(const QString& directory);
122 QString downloadDirectory();
123
124 static QString timeString(double time_remaining);
125 static QString dataString(qint64 size);
126
127 public slots:
128 void download(const QNetworkRequest& request,
129 const QString& preferred_file_name = {},
130 const std::function<void(DownloadItem*)>& run_on_finish = {});
131 void download(const QUrl& url);
132 void cleanup();
133
134 private slots:
135 void save() const;
136 void load();
137
138 void updateRow(DownloadItem* item);
139 void updateRow();
140 void itemProgress();
141 void itemFinished();
142
143 signals:
144 void removePolicyChanged();
145 void downloadProgressed(int progress, const QString& description);
146 void downloadFinished();
147
148 private:
149 void handleUnsupportedContent(QNetworkReply* reply,
150 const QString& preferred_file_name,
151 const std::function<void(DownloadItem*)>& run_on_finish);
152 void addItem(DownloadItem* item);
153
154 QScopedPointer<Ui::DownloadManager> m_ui;
155 AutoSaver* m_autoSaver;
156 DownloadModel* m_model;
157 SilentNetworkAccessManager* m_networkManager;
158 QScopedPointer<QFileIconProvider> m_iconProvider;
159 QList<DownloadItem*> m_downloads;
160 RemovePolicy m_removePolicy;
161 QString m_downloadDirectory;
162 };
163
164 inline WebBrowser* DownloadManager::webBrowser() const {
165 return nullptr;
166 }
167
168 class DownloadModel : public QAbstractListModel {
169 Q_OBJECT
170
171 friend class DownloadManager;
172
173 public:
174 explicit DownloadModel(DownloadManager* download_manager, QObject* parent = nullptr);
175
176 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
177 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
178 virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
179 virtual Qt::ItemFlags flags(const QModelIndex& index) const;
180 virtual QMimeData* mimeData(const QModelIndexList& indexes) const;
181
182 private:
183 DownloadManager* m_downloadManager;
184 };
185
186 #endif // DOWNLOADMANAGER_H