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.
37 #include <QNetworkProxy>
42 #include "base/path.h"
44 class QNetworkAccessManager
;
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
73 DownloadRequest(const QString
&url
);
74 DownloadRequest(const DownloadRequest
&other
) = default;
77 DownloadRequest
&url(const QString
&value
);
79 QString
userAgent() const;
80 DownloadRequest
&userAgent(const QString
&value
);
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
);
98 bool m_saveToFile
= false;
102 struct DownloadResult
105 DownloadStatus status
= DownloadStatus::Failed
;
112 class DownloadHandler
: public QObject
115 Q_DISABLE_COPY_MOVE(DownloadHandler
)
118 using QObject::QObject
;
120 virtual void cancel() = 0;
123 void finished(const DownloadResult
&result
);
126 class DownloadHandlerImpl
;
128 class DownloadManager final
: public QObject
131 Q_DISABLE_COPY_MOVE(DownloadManager
)
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
);
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
);