qBittorrent
downloadmanager.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2015, 2018 Vladimir Golovnev <glassez@yandex.ru>
4  * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL". If you
25  * modify file(s), you may extend this exception to your version of the file(s),
26  * but you are not obligated to do so. If you do not wish to do so, delete this
27  * exception statement from your version.
28  */
29 
30 #pragma once
31 
32 #include <QHash>
33 #include <QNetworkAccessManager>
34 #include <QObject>
35 #include <QQueue>
36 #include <QSet>
37 
38 class QNetworkCookie;
39 class QNetworkReply;
40 class QSslError;
41 class QUrl;
42 
43 namespace Net
44 {
45  struct ServiceID
46  {
47  QString hostName;
48  int port;
49 
50  static ServiceID fromURL(const QUrl &url);
51  };
52 
53  uint qHash(const ServiceID &serviceID, uint seed);
54  bool operator==(const ServiceID &lhs, const ServiceID &rhs);
55 
56  enum class DownloadStatus
57  {
58  Success,
60  Failed
61  };
62 
64  {
65  public:
66  DownloadRequest(const QString &url);
67  DownloadRequest(const DownloadRequest &other) = default;
68 
69  QString url() const;
70  DownloadRequest &url(const QString &value);
71 
72  QString userAgent() const;
73  DownloadRequest &userAgent(const QString &value);
74 
75  qint64 limit() const;
76  DownloadRequest &limit(qint64 value);
77 
78  bool saveToFile() const;
80 
81  // if saveToFile is set, the file is saved in destFileName
82  // (deprecated) if destFileName is not provided, the file will be saved
83  // in a temporary file, the name of file is set in DownloadResult::filePath
84  QString destFileName() const;
85  DownloadRequest &destFileName(const QString &value);
86 
87  private:
88  QString m_url;
89  QString m_userAgent;
90  qint64 m_limit = 0;
91  bool m_saveToFile = false;
92  QString m_destFileName;
93  };
94 
96  {
97  QString url;
99  QString errorString;
100  QByteArray data;
101  QString filePath;
102  QString magnet;
103  };
104 
105  class DownloadHandler : public QObject
106  {
107  Q_OBJECT
108  Q_DISABLE_COPY_MOVE(DownloadHandler)
109 
110  public:
111  using QObject::QObject;
112 
113  virtual void cancel() = 0;
114 
115  signals:
116  void finished(const DownloadResult &result);
117  };
118 
119  class DownloadManager : public QObject
120  {
121  Q_OBJECT
122  Q_DISABLE_COPY_MOVE(DownloadManager)
123 
124  public:
125  static void initInstance();
126  static void freeInstance();
127  static DownloadManager *instance();
128 
129  DownloadHandler *download(const DownloadRequest &downloadRequest);
130 
131  template <typename Context, typename Func>
132  void download(const DownloadRequest &downloadRequest, Context context, Func &&slot);
133 
134  void registerSequentialService(const ServiceID &serviceID);
135 
136  QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
137  bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
138  QList<QNetworkCookie> allCookies() const;
139  void setAllCookies(const QList<QNetworkCookie> &cookieList);
140  bool deleteCookie(const QNetworkCookie &cookie);
141 
142  static bool hasSupportedScheme(const QString &url);
143 
144  private slots:
145  void ignoreSslErrors(QNetworkReply *, const QList<QSslError> &);
146 
147  private:
148  explicit DownloadManager(QObject *parent = nullptr);
149 
150  void applyProxySettings();
151  void handleReplyFinished(const QNetworkReply *reply);
152 
154  QNetworkAccessManager m_networkManager;
155 
156  QSet<ServiceID> m_sequentialServices;
157  QSet<ServiceID> m_busyServices;
158  QHash<ServiceID, QQueue<DownloadHandler *>> m_waitingJobs;
159  };
160 
161  template <typename Context, typename Func>
162  void DownloadManager::download(const DownloadRequest &downloadRequest, Context context, Func &&slot)
163  {
164  const DownloadHandler *handler = download(downloadRequest);
165  connect(handler, &DownloadHandler::finished, context, slot);
166  }
167 }
virtual void cancel()=0
void finished(const DownloadResult &result)
DownloadManager(QObject *parent=nullptr)
void handleReplyFinished(const QNetworkReply *reply)
void ignoreSslErrors(QNetworkReply *, const QList< QSslError > &)
QSet< ServiceID > m_busyServices
void setAllCookies(const QList< QNetworkCookie > &cookieList)
bool setCookiesFromUrl(const QList< QNetworkCookie > &cookieList, const QUrl &url)
QList< QNetworkCookie > allCookies() const
void registerSequentialService(const ServiceID &serviceID)
QNetworkAccessManager m_networkManager
bool deleteCookie(const QNetworkCookie &cookie)
static void freeInstance()
static bool hasSupportedScheme(const QString &url)
QSet< ServiceID > m_sequentialServices
DownloadHandler * download(const DownloadRequest &downloadRequest)
static DownloadManager * instance()
QHash< ServiceID, QQueue< DownloadHandler * > > m_waitingJobs
QList< QNetworkCookie > cookiesForUrl(const QUrl &url) const
static void initInstance()
static DownloadManager * m_instance
DownloadRequest(const DownloadRequest &other)=default
QString userAgent() const
DownloadRequest(const QString &url)
QString destFileName() const
Definition: session.h:86
DownloadStatus
bool operator==(const ServiceID &lhs, const ServiceID &rhs)
uint qHash(const ServiceID &serviceID, uint seed)
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64
DownloadStatus status
static ServiceID fromURL(const QUrl &url)