Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / net / proxyconfigurationmanager.cpp
blob915b3b554d054a3669e4c7647cbb1eaf2da454ed
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "proxyconfigurationmanager.h"
31 #define SETTINGS_KEY(name) (u"Network/Proxy/" name)
33 bool Net::operator==(const ProxyConfiguration &left, const ProxyConfiguration &right)
35 return (left.type == right.type)
36 && (left.ip == right.ip)
37 && (left.port == right.port)
38 && (left.username == right.username)
39 && (left.password == right.password);
42 bool Net::operator!=(const ProxyConfiguration &left, const ProxyConfiguration &right)
44 return !(left == right);
47 using namespace Net;
49 ProxyConfigurationManager *ProxyConfigurationManager::m_instance = nullptr;
51 ProxyConfigurationManager::ProxyConfigurationManager(QObject *parent)
52 : QObject {parent}
53 , m_storeProxyOnlyForTorrents {SETTINGS_KEY(u"OnlyForTorrents"_qs)}
54 , m_storeProxyType {SETTINGS_KEY(u"Type"_qs)}
55 , m_storeProxyIP {SETTINGS_KEY(u"IP"_qs)}
56 , m_storeProxyPort {SETTINGS_KEY(u"Port"_qs)}
57 , m_storeProxyUsername {SETTINGS_KEY(u"Username"_qs)}
58 , m_storeProxyPassword {SETTINGS_KEY(u"Password"_qs)}
60 m_config.type = m_storeProxyType.get(ProxyType::None);
61 if ((m_config.type < ProxyType::None) || (m_config.type > ProxyType::SOCKS4))
62 m_config.type = ProxyType::None;
63 m_config.ip = m_storeProxyIP.get(u"0.0.0.0"_qs);
64 m_config.port = m_storeProxyPort.get(8080);
65 m_config.username = m_storeProxyUsername;
66 m_config.password = m_storeProxyPassword;
67 configureProxy();
70 void ProxyConfigurationManager::initInstance()
72 if (!m_instance)
73 m_instance = new ProxyConfigurationManager;
76 void ProxyConfigurationManager::freeInstance()
78 delete m_instance;
79 m_instance = nullptr;
82 ProxyConfigurationManager *ProxyConfigurationManager::instance()
84 return m_instance;
87 ProxyConfiguration ProxyConfigurationManager::proxyConfiguration() const
89 return m_config;
92 void ProxyConfigurationManager::setProxyConfiguration(const ProxyConfiguration &config)
94 if (m_config != config)
96 m_config = config;
97 m_storeProxyType = config.type;
98 m_storeProxyIP = config.ip;
99 m_storeProxyPort = config.port;
100 m_storeProxyUsername = config.username;
101 m_storeProxyPassword = config.password;
102 configureProxy();
104 emit proxyConfigurationChanged();
108 bool ProxyConfigurationManager::isProxyOnlyForTorrents() const
110 return m_storeProxyOnlyForTorrents || (m_config.type == ProxyType::SOCKS4);
113 void ProxyConfigurationManager::setProxyOnlyForTorrents(const bool onlyForTorrents)
115 m_storeProxyOnlyForTorrents = onlyForTorrents;
118 bool ProxyConfigurationManager::isAuthenticationRequired() const
120 return m_config.type == ProxyType::SOCKS5_PW
121 || m_config.type == ProxyType::HTTP_PW;
124 void ProxyConfigurationManager::configureProxy()
126 // Define environment variables for urllib in search engine plugins
127 QString proxyStrHTTP, proxyStrSOCK;
128 if (!isProxyOnlyForTorrents())
130 switch (m_config.type)
132 case ProxyType::HTTP_PW:
133 proxyStrHTTP = u"http://%1:%2@%3:%4"_qs.arg(m_config.username
134 , m_config.password, m_config.ip, QString::number(m_config.port));
135 break;
136 case ProxyType::HTTP:
137 proxyStrHTTP = u"http://%1:%2"_qs.arg(m_config.ip, QString::number(m_config.port));
138 break;
139 case ProxyType::SOCKS5:
140 proxyStrSOCK = u"%1:%2"_qs.arg(m_config.ip, QString::number(m_config.port));
141 break;
142 case ProxyType::SOCKS5_PW:
143 proxyStrSOCK = u"%1:%2@%3:%4"_qs.arg(m_config.username
144 , m_config.password, m_config.ip, QString::number(m_config.port));
145 break;
146 default:
147 qDebug("Disabling HTTP communications proxy");
150 qDebug("HTTP communications proxy string: %s"
151 , qUtf8Printable((m_config.type == ProxyType::SOCKS5) || (m_config.type == ProxyType::SOCKS5_PW)
152 ? proxyStrSOCK : proxyStrHTTP));
155 qputenv("http_proxy", proxyStrHTTP.toLocal8Bit());
156 qputenv("https_proxy", proxyStrHTTP.toLocal8Bit());
157 qputenv("sock_proxy", proxyStrSOCK.toLocal8Bit());