Allow to globally disable the use of proxy
[qBittorrent.git] / src / base / net / downloadmanager.h
blobaeb2b4887f289fb55a590cc0b62e5a1d4295ee64
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>
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.
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.
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.
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.
30 #pragma once
32 #include <QtGlobal>
33 #include <QHash>
34 #include <QNetworkProxy>
35 #include <QObject>
36 #include <QQueue>
37 #include <QSet>
39 #include "base/path.h"
41 class QNetworkAccessManager;
42 class QNetworkCookie;
43 class QNetworkReply;
44 class QSslError;
45 class QUrl;
47 namespace Net
49 struct ServiceID
51 QString hostName;
52 int port;
54 static ServiceID fromURL(const QUrl &url);
57 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
58 std::size_t qHash(const ServiceID &serviceID, std::size_t seed = 0);
59 #else
60 uint qHash(const ServiceID &serviceID, uint seed = 0);
61 #endif
62 bool operator==(const ServiceID &lhs, const ServiceID &rhs);
64 enum class DownloadStatus
66 Success,
67 RedirectedToMagnet,
68 Failed
71 class DownloadRequest
73 public:
74 DownloadRequest(const QString &url);
75 DownloadRequest(const DownloadRequest &other) = default;
77 QString url() const;
78 DownloadRequest &url(const QString &value);
80 QString userAgent() const;
81 DownloadRequest &userAgent(const QString &value);
83 qint64 limit() const;
84 DownloadRequest &limit(qint64 value);
86 bool saveToFile() const;
87 DownloadRequest &saveToFile(bool value);
89 // if saveToFile is set, the file is saved in destFileName
90 // (deprecated) if destFileName is not provided, the file will be saved
91 // in a temporary file, the name of file is set in DownloadResult::filePath
92 Path destFileName() const;
93 DownloadRequest &destFileName(const Path &value);
95 private:
96 QString m_url;
97 QString m_userAgent;
98 qint64 m_limit = 0;
99 bool m_saveToFile = false;
100 Path m_destFileName;
103 struct DownloadResult
105 QString url;
106 DownloadStatus status = DownloadStatus::Failed;
107 QString errorString;
108 QByteArray data;
109 Path filePath;
110 QString magnet;
113 class DownloadHandler : public QObject
115 Q_OBJECT
116 Q_DISABLE_COPY_MOVE(DownloadHandler)
118 public:
119 using QObject::QObject;
121 virtual void cancel() = 0;
123 signals:
124 void finished(const DownloadResult &result);
127 class DownloadHandlerImpl;
129 class DownloadManager : public QObject
131 Q_OBJECT
132 Q_DISABLE_COPY_MOVE(DownloadManager)
134 public:
135 static void initInstance();
136 static void freeInstance();
137 static DownloadManager *instance();
139 DownloadHandler *download(const DownloadRequest &downloadRequest, bool useProxy);
141 template <typename Context, typename Func>
142 void download(const DownloadRequest &downloadRequest, bool useProxy, Context context, Func &&slot);
144 void registerSequentialService(const ServiceID &serviceID);
146 QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
147 bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
148 QList<QNetworkCookie> allCookies() const;
149 void setAllCookies(const QList<QNetworkCookie> &cookieList);
150 bool deleteCookie(const QNetworkCookie &cookie);
152 static bool hasSupportedScheme(const QString &url);
154 private:
155 class NetworkCookieJar;
157 explicit DownloadManager(QObject *parent = nullptr);
159 void applyProxySettings();
160 void handleDownloadFinished(DownloadHandlerImpl *finishedHandler);
161 void processRequest(DownloadHandlerImpl *downloadHandler);
163 static DownloadManager *m_instance;
164 NetworkCookieJar *m_networkCookieJar = nullptr;
165 QNetworkAccessManager *m_networkManager = nullptr;
166 QNetworkProxy m_proxy;
168 QSet<ServiceID> m_sequentialServices;
169 QSet<ServiceID> m_busyServices;
170 QHash<ServiceID, QQueue<DownloadHandlerImpl *>> m_waitingJobs;
173 template <typename Context, typename Func>
174 void DownloadManager::download(const DownloadRequest &downloadRequest, bool useProxy, Context context, Func &&slot)
176 const DownloadHandler *handler = download(downloadRequest, useProxy);
177 connect(handler, &DownloadHandler::finished, context, slot);