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 <QNetworkAccessManager>
39 #include "base/path.h"
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);
59 uint
qHash(const ServiceID
&serviceID
, uint 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
;
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 DownloadManager
: public QObject
129 Q_DISABLE_COPY_MOVE(DownloadManager
)
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
);
152 void ignoreSslErrors(QNetworkReply
*, const QList
<QSslError
> &);
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
);