Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / net / downloadmanager.h
blob32fcab1755ccbe01453869709b9e1d0a5a843bf7
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 <QNetworkAccessManager>
35 #include <QObject>
36 #include <QQueue>
37 #include <QSet>
39 #include "base/path.h"
41 class QNetworkCookie;
42 class QNetworkReply;
43 class QSslError;
44 class QUrl;
46 namespace Net
48 struct ServiceID
50 QString hostName;
51 int port;
53 static ServiceID fromURL(const QUrl &url);
56 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
57 std::size_t qHash(const ServiceID &serviceID, std::size_t seed = 0);
58 #else
59 uint qHash(const ServiceID &serviceID, uint seed = 0);
60 #endif
61 bool operator==(const ServiceID &lhs, const ServiceID &rhs);
63 enum class DownloadStatus
65 Success,
66 RedirectedToMagnet,
67 Failed
70 class DownloadRequest
72 public:
73 DownloadRequest(const QString &url);
74 DownloadRequest(const DownloadRequest &other) = default;
76 QString url() const;
77 DownloadRequest &url(const QString &value);
79 QString userAgent() const;
80 DownloadRequest &userAgent(const QString &value);
82 qint64 limit() const;
83 DownloadRequest &limit(qint64 value);
85 bool saveToFile() const;
86 DownloadRequest &saveToFile(bool value);
88 // if saveToFile is set, the file is saved in destFileName
89 // (deprecated) if destFileName is not provided, the file will be saved
90 // in a temporary file, the name of file is set in DownloadResult::filePath
91 Path destFileName() const;
92 DownloadRequest &destFileName(const Path &value);
94 private:
95 QString m_url;
96 QString m_userAgent;
97 qint64 m_limit = 0;
98 bool m_saveToFile = false;
99 Path m_destFileName;
102 struct DownloadResult
104 QString url;
105 DownloadStatus status;
106 QString errorString;
107 QByteArray data;
108 Path filePath;
109 QString magnet;
112 class DownloadHandler : public QObject
114 Q_OBJECT
115 Q_DISABLE_COPY_MOVE(DownloadHandler)
117 public:
118 using QObject::QObject;
120 virtual void cancel() = 0;
122 signals:
123 void finished(const DownloadResult &result);
126 class DownloadManager : public QObject
128 Q_OBJECT
129 Q_DISABLE_COPY_MOVE(DownloadManager)
131 public:
132 static void initInstance();
133 static void freeInstance();
134 static DownloadManager *instance();
136 DownloadHandler *download(const DownloadRequest &downloadRequest);
138 template <typename Context, typename Func>
139 void download(const DownloadRequest &downloadRequest, Context context, Func &&slot);
141 void registerSequentialService(const ServiceID &serviceID);
143 QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
144 bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
145 QList<QNetworkCookie> allCookies() const;
146 void setAllCookies(const QList<QNetworkCookie> &cookieList);
147 bool deleteCookie(const QNetworkCookie &cookie);
149 static bool hasSupportedScheme(const QString &url);
151 private slots:
152 void ignoreSslErrors(QNetworkReply *, const QList<QSslError> &);
154 private:
155 explicit DownloadManager(QObject *parent = nullptr);
157 void applyProxySettings();
158 void handleReplyFinished(const QNetworkReply *reply);
160 static DownloadManager *m_instance;
161 QNetworkAccessManager m_networkManager;
163 QSet<ServiceID> m_sequentialServices;
164 QSet<ServiceID> m_busyServices;
165 QHash<ServiceID, QQueue<DownloadHandler *>> m_waitingJobs;
168 template <typename Context, typename Func>
169 void DownloadManager::download(const DownloadRequest &downloadRequest, Context context, Func &&slot)
171 const DownloadHandler *handler = download(downloadRequest);
172 connect(handler, &DownloadHandler::finished, context, slot);