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:
39 
40  // Methods for starting the queue manager thread
41  void run(void) override; // MThread
42  void setRunThread(void) { m_runThread = true; }
43  QThread *getQueueThread(void) { return m_queueThread; }
44  bool isRunning(void) { return m_isRunning; }
45 
46  // Methods to GET a URL
47  void preCache(const QString &url);
48  void queueDownload(const QString &url, const QString &dest,
49  QObject *caller, const bool reload = false);
50  void queueDownload(QNetworkRequest *req, QByteArray *data,
51  QObject *caller);
52  bool download(const QString &url, const QString &dest,
53  const bool reload = false);
54  bool download(const QString &url, QByteArray *data,
55  const bool reload = false);
56  QNetworkReply *download(const QString &url, const bool reload = false);
57  bool download(QNetworkRequest *req, QByteArray *data);
58  bool downloadAuth(const QString &url, const QString &dest,
59  const bool reload = false,
60  AuthCallback authCallback = nullptr,
61  void *authArg = nullptr,
62  const QHash<QByteArray, QByteArray> *headers = nullptr);
63 
64  // Methods to POST to a URL
65  void queuePost(const QString &url, QByteArray *data, QObject *caller);
66  void queuePost(QNetworkRequest *req, QByteArray *data, QObject *caller);
67  bool post(const QString &url, QByteArray *data);
68  bool post(QNetworkRequest *req, QByteArray *data);
69  bool postAuth(const QString &url, QByteArray *data,
70  AuthCallback authCallback, void *authArg,
71  const QHash<QByteArray, QByteArray> *headers = nullptr);
72 
73  // Cancel a download
74  void cancelDownload(const QString &url, bool block = true);
75  void cancelDownload(const QStringList &urls, bool block = true);
76 
77  // Generic helpers
78  void removeListener(QObject *caller);
79  QDateTime GetLastModified(const QString &url);
80 
81  void loadCookieJar(const QString &filename);
82  void saveCookieJar(const QString &filename);
83  void setCookieJar(QNetworkCookieJar *cookieJar);
84 
85  QNetworkCookieJar *copyCookieJar(void);
86  void refreshCookieJar(QNetworkCookieJar *jar);
87  void updateCookieJar(void);
88 
89  QString getHeader(const QUrl &url, const QString &header);
90  QString getHeader(const QNetworkCacheMetaData &cacheData, const QString &header);
91 
92  private slots:
93  // QNetworkAccessManager signals
94  void downloadFinished(QNetworkReply* reply);
95  void authCallback(QNetworkReply *reply, QAuthenticator *authenticator);
96 
97  // QNetworkReply signals
98  void downloadError(QNetworkReply::NetworkError errorCode);
99  void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
100 
101  private:
102  // Notification from RemoteFile downloads
103  void downloadFinished(MythDownloadInfo *dlInfo);
104 
105  // Helper methods for initializing and performing requests
106  void queueItem(const QString &url, QNetworkRequest *req,
107  const QString &dest, QByteArray *data, QObject *caller,
108  const MRequestType reqType = kRequestGet,
109  const bool reload = false);
110 
111  bool processItem(const QString &url, QNetworkRequest *req,
112  const QString &dest, QByteArray *data,
113  const MRequestType reqType = kRequestGet,
114  const bool reload = false,
115  AuthCallback authCallback = nullptr,
116  void *authArg = nullptr,
117  const QHash<QByteArray, QByteArray> *headers = nullptr);
118 
119  void downloadRemoteFile(MythDownloadInfo *dlInfo);
120  void downloadQNetworkRequest(MythDownloadInfo *dlInfo);
121  bool downloadNow(MythDownloadInfo *dlInfo, bool deleteInfo = true);
122 #ifndef _WIN32
123  bool downloadNowLinkLocal(MythDownloadInfo *dlInfo, bool deleteInfo);
124 #endif
125  void downloadCanceled(void);
126 
127  QUrl redirectUrl(const QUrl& possibleRedirectUrl,
128  const QUrl& oldRedirectUrl) const;
129 
130  bool saveFile(const QString &outFile, const QByteArray &data,
131  const bool append = false);
132 
133  void updateCookieJar(QNetworkCookieJar *jar);
134 
135  QNetworkAccessManager *m_manager;
136  QNetworkDiskCache *m_diskCache;
137  QNetworkProxy *m_proxy;
138 
139  QWaitCondition m_queueWaitCond;
141 
142  QMutex *m_infoLock;
143  QMap <QString, MythDownloadInfo*> m_downloadInfos;
144  QMap <QNetworkReply*, MythDownloadInfo*> m_downloadReplies;
145  QList <MythDownloadInfo*> m_downloadQueue;
146  QList <MythDownloadInfo*> m_cancellationQueue;
147 
148  QThread *m_queueThread;
149 
152 
153  QNetworkCookieJar *m_inCookieJar;
154  QMutex m_cookieLock;
155 
157 };
158 
160 
161 #endif
162 
163 /* 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
QNetworkDiskCache * m_diskCache
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:319
void ShutdownMythDownloadManager(void)
Deletes the running MythDownloadManager at program exit.
QList< MythDownloadInfo * > m_downloadQueue
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
QNetworkAccessManager * m_manager
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.
QNetworkCookieJar * m_inCookieJar
void(* AuthCallback)(QNetworkReply *, QAuthenticator *, void *)
QMap< QNetworkReply *, MythDownloadInfo * > m_downloadReplies
QThread * getQueueThread(void)