1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2017 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #include "ProxyPropertiesDialog.h"
21 
22 #include "ui_ProxyPropertiesDialog.h"
23 
24 namespace Otter
25 {
26 
ProxyPropertiesDialog(const ProxyDefinition & proxy,QWidget * parent)27 ProxyPropertiesDialog::ProxyPropertiesDialog(const ProxyDefinition &proxy, QWidget *parent) : Dialog(parent),
28 	m_proxy(proxy),
29 	m_ui(new Ui::ProxyPropertiesDialog)
30 {
31 	m_ui->setupUi(this);
32 	m_ui->titleLineEditWidget->setText(proxy.getTitle());
33 
34 	if (proxy.type == ProxyDefinition::AutomaticProxy)
35 	{
36 		m_ui->automaticConfigurationCheckBox->setChecked(true);
37 		m_ui->automaticConfigurationFilePathWidget->setPath(proxy.path);
38 	}
39 	else
40 	{
41 		m_ui->manualConfigurationCheckBox->setChecked(true);
42 
43 		if (proxy.servers.contains(ProxyDefinition::AnyProtocol))
44 		{
45 			m_ui->allCheckBox->setChecked(true);
46 			m_ui->allServersLineEditWidget->setText(proxy.servers[ProxyDefinition::AnyProtocol].hostName);
47 			m_ui->allPortSpinBox->setValue(proxy.servers[ProxyDefinition::AnyProtocol].port);
48 		}
49 		else
50 		{
51 			if (proxy.servers.contains(ProxyDefinition::HttpProtocol))
52 			{
53 				m_ui->httpCheckBox->setChecked(true);
54 				m_ui->httpServersLineEditWidget->setText(proxy.servers[ProxyDefinition::HttpProtocol].hostName);
55 				m_ui->httpPortSpinBox->setValue(proxy.servers[ProxyDefinition::HttpProtocol].port);
56 			}
57 
58 			if (proxy.servers.contains(ProxyDefinition::HttpsProtocol))
59 			{
60 				m_ui->httpsCheckBox->setChecked(true);
61 				m_ui->httpsServersLineEditWidget->setText(proxy.servers[ProxyDefinition::HttpsProtocol].hostName);
62 				m_ui->httpsPortSpinBox->setValue(proxy.servers[ProxyDefinition::HttpsProtocol].port);
63 			}
64 
65 			if (proxy.servers.contains(ProxyDefinition::FtpProtocol))
66 			{
67 				m_ui->ftpCheckBox->setChecked(true);
68 				m_ui->ftpServersLineEditWidget->setText(proxy.servers[ProxyDefinition::FtpProtocol].hostName);
69 				m_ui->ftpPortSpinBox->setValue(proxy.servers[ProxyDefinition::FtpProtocol].port);
70 			}
71 
72 			if (proxy.servers.contains(ProxyDefinition::SocksProtocol))
73 			{
74 				m_ui->socksCheckBox->setChecked(true);
75 				m_ui->socksServersLineEditWidget->setText(proxy.servers[ProxyDefinition::SocksProtocol].hostName);
76 				m_ui->socksPortSpinBox->setValue(proxy.servers[ProxyDefinition::SocksProtocol].port);
77 			}
78 		}
79 	}
80 
81 	m_ui->usesSystemAuthenticationCheckBox->setChecked(proxy.usesSystemAuthentication);
82 
83 	QStandardItemModel *exceptionsModel(new QStandardItemModel(this));
84 
85 	for (int i = 0; i < proxy.exceptions.count(); ++i)
86 	{
87 		exceptionsModel->appendRow(new QStandardItem(proxy.exceptions.at(i)));
88 	}
89 
90 	m_ui->exceptionsItemViewWidget->setModel(exceptionsModel);
91 
92 	updateProxyType();
93 	setWindowTitle(proxy.isValid() ? tr ("Edit Proxy") : tr("Add Proxy"));
94 
95 	connect(m_ui->buttonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), this, &ProxyPropertiesDialog::updateProxyType);
96 	connect(m_ui->allCheckBox, &QCheckBox::clicked, this, &ProxyPropertiesDialog::updateProxyType);
97 	connect(m_ui->exceptionsItemViewWidget, &ItemViewWidget::needsActionsUpdate, this, &ProxyPropertiesDialog::updateExceptionsActions);
98 	connect(m_ui->addExceptionButton, &QPushButton::clicked, this, &ProxyPropertiesDialog::addException);
99 	connect(m_ui->editExceptionButton, &QPushButton::clicked, this, &ProxyPropertiesDialog::editException);
100 	connect(m_ui->removeExceptionButton, &QPushButton::clicked, this, &ProxyPropertiesDialog::removeException);
101 }
102 
~ProxyPropertiesDialog()103 ProxyPropertiesDialog::~ProxyPropertiesDialog()
104 {
105 	delete m_ui;
106 }
107 
changeEvent(QEvent * event)108 void ProxyPropertiesDialog::changeEvent(QEvent *event)
109 {
110 	QDialog::changeEvent(event);
111 
112 	if (event->type() == QEvent::LanguageChange)
113 	{
114 		m_ui->retranslateUi(this);
115 	}
116 }
117 
addException()118 void ProxyPropertiesDialog::addException()
119 {
120 	m_ui->exceptionsItemViewWidget->insertRow();
121 
122 	editException();
123 }
124 
editException()125 void ProxyPropertiesDialog::editException()
126 {
127 	m_ui->exceptionsItemViewWidget->edit(m_ui->exceptionsItemViewWidget->getIndex(m_ui->exceptionsItemViewWidget->getCurrentRow()));
128 }
129 
removeException()130 void ProxyPropertiesDialog::removeException()
131 {
132 	m_ui->exceptionsItemViewWidget->removeRow();
133 	m_ui->exceptionsItemViewWidget->setFocus();
134 
135 	updateExceptionsActions();
136 }
137 
updateExceptionsActions()138 void ProxyPropertiesDialog::updateExceptionsActions()
139 {
140 	const bool isEditable(m_ui->exceptionsItemViewWidget->getCurrentRow() >= 0);
141 
142 	m_ui->editExceptionButton->setEnabled(isEditable);
143 	m_ui->removeExceptionButton->setEnabled(isEditable);
144 }
145 
updateProxyType()146 void ProxyPropertiesDialog::updateProxyType()
147 {
148 	if (m_ui->manualConfigurationCheckBox->isChecked())
149 	{
150 		const bool usesSeparateServers(!m_ui->allCheckBox->isChecked());
151 
152 		m_ui->manualConfigurationWidget->setEnabled(true);
153 		m_ui->automaticConfigurationWidget->setEnabled(false);
154 		m_ui->httpCheckBox->setEnabled(usesSeparateServers);
155 		m_ui->httpServersLineEditWidget->setEnabled(usesSeparateServers);
156 		m_ui->httpPortSpinBox->setEnabled(usesSeparateServers);
157 		m_ui->httpsCheckBox->setEnabled(usesSeparateServers);
158 		m_ui->httpsServersLineEditWidget->setEnabled(usesSeparateServers);
159 		m_ui->httpsPortSpinBox->setEnabled(usesSeparateServers);
160 		m_ui->ftpCheckBox->setEnabled(usesSeparateServers);
161 		m_ui->ftpServersLineEditWidget->setEnabled(usesSeparateServers);
162 		m_ui->ftpPortSpinBox->setEnabled(usesSeparateServers);
163 		m_ui->socksCheckBox->setEnabled(usesSeparateServers);
164 		m_ui->socksServersLineEditWidget->setEnabled(usesSeparateServers);
165 		m_ui->socksPortSpinBox->setEnabled(usesSeparateServers);
166 	}
167 	else
168 	{
169 		m_ui->manualConfigurationWidget->setEnabled(false);
170 		m_ui->automaticConfigurationWidget->setEnabled(true);
171 	}
172 }
173 
getProxy() const174 ProxyDefinition ProxyPropertiesDialog::getProxy() const
175 {
176 	ProxyDefinition proxy(m_proxy);
177 	proxy.title = m_ui->titleLineEditWidget->text();
178 	proxy.usesSystemAuthentication = m_ui->usesSystemAuthenticationCheckBox->isChecked();
179 	proxy.exceptions.clear();
180 	proxy.servers.clear();
181 
182 	if (m_ui->automaticConfigurationCheckBox->isChecked())
183 	{
184 		proxy.type = ProxyDefinition::AutomaticProxy;
185 		proxy.path = m_ui->automaticConfigurationFilePathWidget->getPath();
186 	}
187 	else
188 	{
189 		proxy.type = ProxyDefinition::ManualProxy;
190 
191 		if (m_ui->allCheckBox->isChecked())
192 		{
193 			proxy.servers[ProxyDefinition::AnyProtocol] = ProxyDefinition::ProxyServer(m_ui->allServersLineEditWidget->text(), static_cast<quint16>(m_ui->allPortSpinBox->value()));
194 		}
195 		else
196 		{
197 			if (m_ui->httpCheckBox->isChecked())
198 			{
199 				proxy.servers[ProxyDefinition::HttpProtocol] = ProxyDefinition::ProxyServer(m_ui->httpServersLineEditWidget->text(), static_cast<quint16>(m_ui->httpPortSpinBox->value()));
200 			}
201 
202 			if (m_ui->httpsCheckBox->isChecked())
203 			{
204 				proxy.servers[ProxyDefinition::HttpsProtocol] = ProxyDefinition::ProxyServer(m_ui->httpsServersLineEditWidget->text(), static_cast<quint16>(m_ui->httpsPortSpinBox->value()));
205 			}
206 
207 			if (m_ui->ftpCheckBox->isChecked())
208 			{
209 				proxy.servers[ProxyDefinition::FtpProtocol] = ProxyDefinition::ProxyServer(m_ui->ftpServersLineEditWidget->text(), static_cast<quint16>(m_ui->ftpPortSpinBox->value()));
210 			}
211 
212 			if (m_ui->socksCheckBox->isChecked())
213 			{
214 				proxy.servers[ProxyDefinition::SocksProtocol] = ProxyDefinition::ProxyServer(m_ui->socksServersLineEditWidget->text(), static_cast<quint16>(m_ui->socksPortSpinBox->value()));
215 			}
216 		}
217 	}
218 
219 	for (int i = 0; i < m_ui->exceptionsItemViewWidget->getRowCount(); ++i)
220 	{
221 		const QString exception(m_ui->exceptionsItemViewWidget->getIndex(i).data(Qt::DisplayRole).toString().simplified());
222 
223 		if (!exception.isEmpty())
224 		{
225 			proxy.exceptions.append(exception);
226 		}
227 	}
228 
229 	return proxy;
230 }
231 
232 }
233