MythTV master
mythdownloadmanager.h
Go to the documentation of this file.
1#ifndef MYTHDOWNLOADMANAGER_H
2#define MYTHDOWNLOADMANAGER_H
3
4#include <QtGlobal>
5#if QT_VERSION >= QT_VERSION_CHECK(6,5,0)
6#include <QtSystemDetection>
7#endif
8#include <QChar> // Fix Qt6 GCC SFINAE warning
9#include <QDateTime>
10#include <QHash>
11#include <QMutex>
12#include <QNetworkAccessManager>
13#include <QNetworkCookieJar>
14#include <QNetworkDiskCache>
15#include <QNetworkProxy>
16#include <QNetworkReply>
17#include <QRecursiveMutex>
18#include <QString>
19#include <QTimer>
20#include <QWaitCondition>
21
22#include "mythbaseexp.h"
23#include "mthread.h"
24
27
29
34class MBASE_PUBLIC MythCookieJar : public QNetworkCookieJar
35{
36 Q_OBJECT
37 public:
38 MythCookieJar() = default;
39 void copyAllCookies(MythCookieJar &old);
40 void load(const QString &filename);
41 void save(const QString &filename);
42};
43
44// TODO : Overlap/Clash with RequestType in libupnp/httprequest.h
45enum MRequestType : std::uint8_t {
49};
50
51using AuthCallback = void (*)(QNetworkReply*, QAuthenticator*, void*);
52
53class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
54{
55 Q_OBJECT
56
57 public:
62 : MThread("DownloadManager"),
63 m_infoLock(new QRecursiveMutex())
64 {}
65
66 ~MythDownloadManager() override;
67
68 // Methods for starting the queue manager thread
69 void setRunThread(void) { m_runThread = true; }
70 QThread *getQueueThread(void) { return m_queueThread; }
71 bool isRunning(void) const { return m_isRunning; }
72
73 // Methods to GET a URL
74 void preCache(const QString &url);
75 void queueDownload(const QString &url, const QString &dest,
76 QObject *caller, bool reload = false);
77 void queueDownload(QNetworkRequest *req, QByteArray *data,
78 QObject *caller);
79 bool download(const QString &url, const QString &dest,
80 bool reload = false);
81 bool download(const QString &url, QByteArray *data,
82 bool reload = false, QString *finalUrl = nullptr);
83 QNetworkReply *download(const QString &url, bool reload = false);
84 bool download(QNetworkRequest *req, QByteArray *data);
85 bool downloadAuth(const QString &url, const QString &dest,
86 bool reload = false,
87 AuthCallback authCallback = nullptr,
88 void *authArg = nullptr,
89 const QHash<QByteArray, QByteArray> *headers = nullptr);
90
91 // Methods to POST to a URL
92 void queuePost(const QString &url, QByteArray *data, QObject *caller);
93 void queuePost(QNetworkRequest *req, QByteArray *data, QObject *caller);
94 bool post(const QString &url, QByteArray *data);
95 bool post(QNetworkRequest *req, QByteArray *data);
96 bool postAuth(const QString &url, QByteArray *data,
97 AuthCallback authCallback, void *authArg,
98 const QHash<QByteArray, QByteArray> *headers = nullptr);
99
100 // Cancel a download
101 void cancelDownload(const QString &url, bool block = true);
102 void cancelDownload(const QStringList &urls, bool block = true);
103
104 // Generic helpers
105 void removeListener(QObject *caller);
106 QDateTime GetLastModified(const QString &url);
107
108 void loadCookieJar(const QString &filename);
109 void saveCookieJar(const QString &filename);
110 void setCookieJar(QNetworkCookieJar *cookieJar);
111
112 QNetworkCookieJar *copyCookieJar(void);
113 void refreshCookieJar(QNetworkCookieJar *jar);
114 void updateCookieJar(void);
115
116 QString getHeader(const QUrl &url, const QString &header);
117 static QString getHeader(const QNetworkCacheMetaData &cacheData, const QString &header);
118
119 protected:
120 void run(void) override; // MThread
121
122 private slots:
123 // QNetworkAccessManager signals
124 void downloadFinished(QNetworkReply* reply);
125 void authCallback(QNetworkReply *reply, QAuthenticator *authenticator);
126
127 // QNetworkReply signals
128 void downloadError(QNetworkReply::NetworkError errorCode);
129 void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
130
131 private:
132 // Notification from RemoteFile downloads
133 void downloadFinished(MythDownloadInfo *dlInfo);
134
135 // Helper methods for initializing and performing requests
136 void queueItem(const QString &url, QNetworkRequest *req,
137 const QString &dest, QByteArray *data, QObject *caller,
138 MRequestType reqType = kRequestGet,
139 bool reload = false);
140
141 bool processItem(const QString &url, QNetworkRequest *req,
142 const QString &dest, QByteArray *data,
143 MRequestType reqType = kRequestGet,
144 bool reload = false,
145 AuthCallback authCallback = nullptr,
146 void *authArg = nullptr,
147 const QHash<QByteArray, QByteArray> *headers = nullptr,
148 QString *finalUrl = nullptr);
149
151 void downloadQNetworkRequest(MythDownloadInfo *dlInfo);
152 bool downloadNow(MythDownloadInfo *dlInfo, bool deleteInfo = true);
153#ifndef Q_OS_WINDOWS
154 static bool downloadNowLinkLocal(MythDownloadInfo *dlInfo, bool deleteInfo);
155#endif
156 void downloadCanceled(void);
157
158 static QUrl redirectUrl(const QUrl& possibleRedirectUrl,
159 const QUrl& oldRedirectUrl) ;
160
161 static bool saveFile(const QString &outFile, const QByteArray &data,
162 bool append = false);
163
164 void updateCookieJar(QNetworkCookieJar *jar);
165
166 QNetworkAccessManager *m_manager {nullptr};
167 QNetworkDiskCache *m_diskCache {nullptr};
168 QNetworkProxy *m_proxy {nullptr};
169
170 QWaitCondition m_queueWaitCond;
172
173 QRecursiveMutex *m_infoLock {nullptr};
174 QMap <QString, MythDownloadInfo*> m_downloadInfos;
175 QMap <QNetworkReply*, MythDownloadInfo*> m_downloadReplies;
176 QList <MythDownloadInfo*> m_downloadQueue;
177 QList <MythDownloadInfo*> m_cancellationQueue;
178
179 QThread *m_queueThread {nullptr};
180
181 bool m_runThread {false};
182 bool m_isRunning {false};
183
184 QNetworkCookieJar *m_inCookieJar {nullptr};
186
188};
189
191
192#endif
193
194/* vim: set expandtab tabstop=4 shiftwidth=4: */
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
A subclassed QNetworkCookieJar that allows for reading and writing cookie files that contain raw form...
MythCookieJar()=default
QMap< QString, MythDownloadInfo * > m_downloadInfos
bool isRunning(void) const
QWaitCondition m_queueWaitCond
void updateCookieJar(QNetworkCookieJar *jar)
QList< MythDownloadInfo * > m_cancellationQueue
QMap< QNetworkReply *, MythDownloadInfo * > m_downloadReplies
QList< MythDownloadInfo * > m_downloadQueue
QThread * getQueueThread(void)
MythDownloadManager()
Constructor for MythDownloadManager.
#define MBASE_PUBLIC
Definition: mythbaseexp.h:8
MBASE_PUBLIC MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
void ShutdownMythDownloadManager(void)
Deletes the running MythDownloadManager at program exit.
MRequestType
@ kRequestPost
@ kRequestHead
@ kRequestGet
void(*)(QNetworkReply *, QAuthenticator *, void *) AuthCallback
void run(const QString &name, Class *object, void(Class::*fn)())
Definition: mconcurrent.h:137
static QString downloadRemoteFile(const QString &cmd, const QString &url, const QString &storageGroup, const QString &filename)