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