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