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