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