Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / net / downloadmanager.h
blob8f74c2f9d135fe0fde6e6d0d6b8df3ed484219e4
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2024 Jonathan Ketchker
4 * Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
5 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #pragma once
33 #include <chrono>
35 #include <QtTypes>
36 #include <QHash>
37 #include <QNetworkProxy>
38 #include <QObject>
39 #include <QQueue>
40 #include <QSet>
42 #include "base/path.h"
44 class QNetworkAccessManager;
45 class QNetworkCookie;
46 class QNetworkReply;
47 class QSslError;
48 class QUrl;
50 namespace Net
52 struct ServiceID
54 QString hostName;
55 int port;
57 static ServiceID fromURL(const QUrl &url);
60 std::size_t qHash(const ServiceID &serviceID, std::size_t seed = 0);
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 = DownloadStatus::Failed;
106 QString errorString;
107 QByteArray data;
108 Path filePath;
109 QString magnetURI;
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 DownloadHandlerImpl;
128 class DownloadManager final : public QObject
130 Q_OBJECT
131 Q_DISABLE_COPY_MOVE(DownloadManager)
133 public:
134 static void initInstance();
135 static void freeInstance();
136 static DownloadManager *instance();
138 DownloadHandler *download(const DownloadRequest &downloadRequest, bool useProxy);
140 template <typename Context, typename Func>
141 void download(const DownloadRequest &downloadRequest, bool useProxy, Context context, Func &&slot);
143 void registerSequentialService(const ServiceID &serviceID, std::chrono::seconds delay = std::chrono::seconds(0));
145 QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
146 bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
147 QList<QNetworkCookie> allCookies() const;
148 void setAllCookies(const QList<QNetworkCookie> &cookieList);
149 bool deleteCookie(const QNetworkCookie &cookie);
151 static bool hasSupportedScheme(const QString &url);
153 private:
154 class NetworkCookieJar;
156 explicit DownloadManager(QObject *parent = nullptr);
158 void applyProxySettings();
159 void processWaitingJobs(const ServiceID &serviceID);
160 void processRequest(DownloadHandlerImpl *downloadHandler);
162 static DownloadManager *m_instance;
163 NetworkCookieJar *m_networkCookieJar = nullptr;
164 QNetworkAccessManager *m_networkManager = nullptr;
165 QNetworkProxy m_proxy;
167 // m_sequentialServices value is delay for same host requests
168 QHash<ServiceID, std::chrono::seconds> 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);