qBittorrent
Net::DownloadManager Class Reference

#include <downloadmanager.h>

Inheritance diagram for Net::DownloadManager:
Collaboration diagram for Net::DownloadManager:

Public Member Functions

DownloadHandlerdownload (const DownloadRequest &downloadRequest)
 
template<typename Context , typename Func >
void download (const DownloadRequest &downloadRequest, Context context, Func &&slot)
 
void registerSequentialService (const ServiceID &serviceID)
 
QList< QNetworkCookie > cookiesForUrl (const QUrl &url) const
 
bool setCookiesFromUrl (const QList< QNetworkCookie > &cookieList, const QUrl &url)
 
QList< QNetworkCookie > allCookies () const
 
void setAllCookies (const QList< QNetworkCookie > &cookieList)
 
bool deleteCookie (const QNetworkCookie &cookie)
 

Static Public Member Functions

static void initInstance ()
 
static void freeInstance ()
 
static DownloadManagerinstance ()
 
static bool hasSupportedScheme (const QString &url)
 

Private Slots

void ignoreSslErrors (QNetworkReply *, const QList< QSslError > &)
 

Private Member Functions

 DownloadManager (QObject *parent=nullptr)
 
void applyProxySettings ()
 
void handleReplyFinished (const QNetworkReply *reply)
 

Private Attributes

QNetworkAccessManager m_networkManager
 
QSet< ServiceIDm_sequentialServices
 
QSet< ServiceIDm_busyServices
 
QHash< ServiceID, QQueue< DownloadHandler * > > m_waitingJobs
 

Static Private Attributes

static DownloadManagerm_instance = nullptr
 

Detailed Description

Definition at line 119 of file downloadmanager.h.

Constructor & Destructor Documentation

◆ DownloadManager()

Net::DownloadManager::DownloadManager ( QObject *  parent = nullptr)
explicitprivate

Definition at line 137 of file downloadmanager.cpp.

138  : QObject(parent)
139 {
140  connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this, &Net::DownloadManager::ignoreSslErrors);
141  connect(&m_networkManager, &QNetworkAccessManager::finished, this, &DownloadManager::handleReplyFinished);
144  m_networkManager.setCookieJar(new NetworkCookieJar(this));
146 }
void handleReplyFinished(const QNetworkReply *reply)
void ignoreSslErrors(QNetworkReply *, const QList< QSslError > &)
QNetworkAccessManager m_networkManager
static ProxyConfigurationManager * instance()

References applyProxySettings(), handleReplyFinished(), ignoreSslErrors(), Net::ProxyConfigurationManager::instance(), m_networkManager, and Net::ProxyConfigurationManager::proxyConfigurationChanged().

Here is the call graph for this function:

Member Function Documentation

◆ allCookies()

QList< QNetworkCookie > Net::DownloadManager::allCookies ( ) const

Definition at line 209 of file downloadmanager.cpp.

210 {
211  return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->allCookies();
212 }
QList< QNetworkCookie > allCookies() const

◆ applyProxySettings()

void Net::DownloadManager::applyProxySettings ( )
private

Definition at line 233 of file downloadmanager.cpp.

234 {
235  const auto *proxyManager = ProxyConfigurationManager::instance();
236  const ProxyConfiguration proxyConfig = proxyManager->proxyConfiguration();
237  QNetworkProxy proxy;
238 
239  if (!proxyManager->isProxyOnlyForTorrents() && (proxyConfig.type != ProxyType::None))
240  {
241  // Proxy enabled
242  proxy.setHostName(proxyConfig.ip);
243  proxy.setPort(proxyConfig.port);
244  // Default proxy type is HTTP, we must change if it is SOCKS5
245  if ((proxyConfig.type == ProxyType::SOCKS5) || (proxyConfig.type == ProxyType::SOCKS5_PW))
246  {
247  qDebug() << Q_FUNC_INFO << "using SOCKS proxy";
248  proxy.setType(QNetworkProxy::Socks5Proxy);
249  }
250  else
251  {
252  qDebug() << Q_FUNC_INFO << "using HTTP proxy";
253  proxy.setType(QNetworkProxy::HttpProxy);
254  }
255  // Authentication?
256  if (proxyManager->isAuthenticationRequired())
257  {
258  qDebug("Proxy requires authentication, authenticating...");
259  proxy.setUser(proxyConfig.username);
260  proxy.setPassword(proxyConfig.password);
261  }
262  }
263  else
264  {
265  proxy.setType(QNetworkProxy::NoProxy);
266  }
267 
268  m_networkManager.setProxy(proxy);
269 }

References Net::ProxyConfigurationManager::instance(), Net::ProxyConfiguration::ip, Net::None, Net::ProxyConfiguration::password, Net::ProxyConfiguration::port, Net::SOCKS5, Net::SOCKS5_PW, Net::ProxyConfiguration::type, and Net::ProxyConfiguration::username.

Referenced by DownloadManager().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cookiesForUrl()

QList< QNetworkCookie > Net::DownloadManager::cookiesForUrl ( const QUrl &  url) const

Definition at line 199 of file downloadmanager.cpp.

200 {
201  return m_networkManager.cookieJar()->cookiesForUrl(url);
202 }

◆ deleteCookie()

bool Net::DownloadManager::deleteCookie ( const QNetworkCookie &  cookie)

Definition at line 219 of file downloadmanager.cpp.

220 {
221  return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->deleteCookie(cookie);
222 }
bool deleteCookie(const QNetworkCookie &cookie)

◆ download() [1/2]

Net::DownloadHandler * Net::DownloadManager::download ( const DownloadRequest downloadRequest)

Definition at line 165 of file downloadmanager.cpp.

166 {
167  // Process download request
168  const QNetworkRequest request = createNetworkRequest(downloadRequest);
169  const ServiceID id = ServiceID::fromURL(request.url());
170  const bool isSequentialService = m_sequentialServices.contains(id);
171 
172  auto downloadHandler = new DownloadHandlerImpl {this, downloadRequest};
173  connect(downloadHandler, &DownloadHandler::finished, downloadHandler, &QObject::deleteLater);
174  connect(downloadHandler, &QObject::destroyed, this, [this, id, downloadHandler]()
175  {
176  m_waitingJobs[id].removeOne(downloadHandler);
177  });
178 
179  if (isSequentialService && m_busyServices.contains(id))
180  {
181  m_waitingJobs[id].enqueue(downloadHandler);
182  }
183  else
184  {
185  qDebug("Downloading %s...", qUtf8Printable(downloadRequest.url()));
186  if (isSequentialService)
187  m_busyServices.insert(id);
188  downloadHandler->assignNetworkReply(m_networkManager.get(request));
189  }
190 
191  return downloadHandler;
192 }
void finished(const DownloadResult &result)
QSet< ServiceID > m_busyServices
QSet< ServiceID > m_sequentialServices
QHash< ServiceID, QQueue< DownloadHandler * > > m_waitingJobs
QNetworkRequest createNetworkRequest(const Net::DownloadRequest &downloadRequest)
static ServiceID fromURL(const QUrl &url)

References anonymous_namespace{downloadmanager.cpp}::createNetworkRequest(), Net::DownloadHandler::finished(), Net::ServiceID::fromURL(), and Net::DownloadRequest::url().

Referenced by BitTorrent::Session::addTorrent(), ProgramUpdater::checkForUpdates(), Net::DNSUpdater::checkPublicIP(), download(), Net::GeoIPManager::downloadDatabaseFile(), TrackerFiltersList::downloadFavicon(), RSS::Feed::downloadIcon(), DownloadHandlerImpl::handleRedirection(), TrackersAdditionDialog::on_uTorrentListButton_clicked(), RSS::Feed::refresh(), AddNewTorrentDialog::show(), and Net::DNSUpdater::updateDNSService().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ download() [2/2]

template<typename Context , typename Func >
void Net::DownloadManager::download ( const DownloadRequest downloadRequest,
Context  context,
Func &&  slot 
)

Definition at line 162 of file downloadmanager.h.

163  {
164  const DownloadHandler *handler = download(downloadRequest);
165  connect(handler, &DownloadHandler::finished, context, slot);
166  }
DownloadHandler * download(const DownloadRequest &downloadRequest)

References download(), and Net::DownloadHandler::finished().

Here is the call graph for this function:

◆ freeInstance()

void Net::DownloadManager::freeInstance ( )
static

Definition at line 154 of file downloadmanager.cpp.

155 {
156  delete m_instance;
157  m_instance = nullptr;
158 }
static DownloadManager * m_instance

Referenced by Application::cleanup().

Here is the caller graph for this function:

◆ handleReplyFinished()

void Net::DownloadManager::handleReplyFinished ( const QNetworkReply *  reply)
private

Definition at line 271 of file downloadmanager.cpp.

272 {
273  // QNetworkReply::url() may be different from that of the original request
274  // so we need QNetworkRequest::url() to properly process Sequential Services
275  // in the case when the redirection occurred.
276  const ServiceID id = ServiceID::fromURL(reply->request().url());
277  const auto waitingJobsIter = m_waitingJobs.find(id);
278  if ((waitingJobsIter == m_waitingJobs.end()) || waitingJobsIter.value().isEmpty())
279  {
280  // No more waiting jobs for given ServiceID
281  m_busyServices.remove(id);
282  return;
283  }
284 
285  auto handler = static_cast<DownloadHandlerImpl *>(waitingJobsIter.value().dequeue());
286  qDebug("Downloading %s...", qUtf8Printable(handler->url()));
287  handler->assignNetworkReply(m_networkManager.get(createNetworkRequest(handler->downloadRequest())));
288  handler->disconnect(this);
289 }

References anonymous_namespace{downloadmanager.cpp}::createNetworkRequest(), and Net::ServiceID::fromURL().

Referenced by DownloadManager().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasSupportedScheme()

bool Net::DownloadManager::hasSupportedScheme ( const QString &  url)
static

Definition at line 224 of file downloadmanager.cpp.

225 {
226  const QStringList schemes = instance()->m_networkManager.supportedSchemes();
227  return std::any_of(schemes.cbegin(), schemes.cend(), [&url](const QString &scheme)
228  {
229  return url.startsWith((scheme + QLatin1Char(':')), Qt::CaseInsensitive);
230  });
231 }
static DownloadManager * instance()

Referenced by BitTorrent::Session::addTorrent(), PluginSelectDialog::askForPluginUrl(), SearchPluginManager::installPlugin(), anonymous_namespace{mainwindow.cpp}::isTorrentLink(), RSSWidget::on_newFeedButton_clicked(), and AddNewTorrentDialog::show().

Here is the caller graph for this function:

◆ ignoreSslErrors

void Net::DownloadManager::ignoreSslErrors ( QNetworkReply *  reply,
const QList< QSslError > &  errors 
)
privateslot

Definition at line 291 of file downloadmanager.cpp.

292 {
293  QStringList errorList;
294  for (const QSslError &error : errors)
295  errorList += error.errorString();
296  LogMsg(tr("Ignoring SSL error, URL: \"%1\", errors: \"%2\"").arg(reply->url().toString(), errorList.join(". ")), Log::WARNING);
297 
298  // Ignore all SSL errors
299  reply->ignoreSslErrors();
300 }
void LogMsg(const QString &message, const Log::MsgType &type)
Definition: logger.cpp:125
@ WARNING
Definition: logger.h:47

References LogMsg(), and Log::WARNING.

Referenced by DownloadManager().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initInstance()

void Net::DownloadManager::initInstance ( )
static

Definition at line 148 of file downloadmanager.cpp.

149 {
150  if (!m_instance)
152 }
DownloadManager(QObject *parent=nullptr)

Referenced by Application::exec().

Here is the caller graph for this function:

◆ instance()

◆ registerSequentialService()

void Net::DownloadManager::registerSequentialService ( const ServiceID serviceID)

Definition at line 194 of file downloadmanager.cpp.

195 {
196  m_sequentialServices.insert(serviceID);
197 }

Referenced by RSS::Feed::Feed().

Here is the caller graph for this function:

◆ setAllCookies()

void Net::DownloadManager::setAllCookies ( const QList< QNetworkCookie > &  cookieList)

Definition at line 214 of file downloadmanager.cpp.

215 {
216  static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->setAllCookies(cookieList);
217 }
void setAllCookies(const QList< QNetworkCookie > &cookieList)

Referenced by CookiesDialog::accept().

Here is the caller graph for this function:

◆ setCookiesFromUrl()

bool Net::DownloadManager::setCookiesFromUrl ( const QList< QNetworkCookie > &  cookieList,
const QUrl &  url 
)

Definition at line 204 of file downloadmanager.cpp.

205 {
206  return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url);
207 }

Referenced by TorrentsController::addAction().

Here is the caller graph for this function:

Member Data Documentation

◆ m_busyServices

QSet<ServiceID> Net::DownloadManager::m_busyServices
private

Definition at line 157 of file downloadmanager.h.

◆ m_instance

Net::DownloadManager * Net::DownloadManager::m_instance = nullptr
staticprivate

Definition at line 153 of file downloadmanager.h.

◆ m_networkManager

QNetworkAccessManager Net::DownloadManager::m_networkManager
private

Definition at line 154 of file downloadmanager.h.

Referenced by DownloadManager().

◆ m_sequentialServices

QSet<ServiceID> Net::DownloadManager::m_sequentialServices
private

Definition at line 156 of file downloadmanager.h.

◆ m_waitingJobs

QHash<ServiceID, QQueue<DownloadHandler *> > Net::DownloadManager::m_waitingJobs
private

Definition at line 158 of file downloadmanager.h.


The documentation for this class was generated from the following files: