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.
34 #include <QNetworkProxy>
39 #include "base/path.h"
41 class QNetworkAccessManager
;
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);
60 uint
qHash(const ServiceID
&serviceID
, uint seed
= 0);
62 bool operator==(const ServiceID
&lhs
, const ServiceID
&rhs
);
64 enum class DownloadStatus
74 DownloadRequest(const QString
&url
);
75 DownloadRequest(const DownloadRequest
&other
) = default;
78 DownloadRequest
&url(const QString
&value
);
80 QString
userAgent() const;
81 DownloadRequest
&userAgent(const QString
&value
);
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
);
99 bool m_saveToFile
= false;
103 struct DownloadResult
106 DownloadStatus status
= DownloadStatus::Failed
;
113 class DownloadHandler
: public QObject
116 Q_DISABLE_COPY_MOVE(DownloadHandler
)
119 using QObject::QObject
;
121 virtual void cancel() = 0;
124 void finished(const DownloadResult
&result
);
127 class DownloadHandlerImpl
;
129 class DownloadManager
: public QObject
132 Q_DISABLE_COPY_MOVE(DownloadManager
)
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
);
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
);