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
22
24
29class 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
40enum MRequestType : std::uint8_t {
44};
45
46using AuthCallback = void (*)(QNetworkReply*, QAuthenticator*, void*);
47
48class 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
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};
179
181};
182
184
185#endif
186
187/* vim: set expandtab tabstop=4 shiftwidth=4: */
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
A subclassed QNetworkCookieJar that allows for reading and writing cookie files that contain raw form...
MythCookieJar()=default
QMap< QString, MythDownloadInfo * > m_downloadInfos
bool isRunning(void) const
QWaitCondition m_queueWaitCond
void updateCookieJar(QNetworkCookieJar *jar)
QList< MythDownloadInfo * > m_cancellationQueue
QMap< QNetworkReply *, MythDownloadInfo * > m_downloadReplies
QList< MythDownloadInfo * > m_downloadQueue
QThread * getQueueThread(void)
MythDownloadManager()
Constructor for MythDownloadManager.
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MBASE_PUBLIC MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
void ShutdownMythDownloadManager(void)
Deletes the running MythDownloadManager at program exit.
MRequestType
@ kRequestPost
@ kRequestHead
@ kRequestGet
void(*)(QNetworkReply *, QAuthenticator *, void *) AuthCallback
void run(const QString &name, Class *object, void(Class::*fn)())
Definition: mconcurrent.h:137
static QString downloadRemoteFile(const QString &cmd, const QString &url, const QString &storageGroup, const QString &filename)
Definition: remoteutil.cpp:588