1 /*
2 * Cantata
3 *
4 * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5 *
6 * ----
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "proxysettings.h"
25 #include "networkproxyfactory.h"
26 #include <QSettings>
27
ProxySettings(QWidget * parent)28 ProxySettings::ProxySettings(QWidget *parent)
29 : QWidget(parent)
30 {
31 setupUi(this);
32 proxyMode->addItem(tr("No proxy"), (int)NetworkProxyFactory::Mode_Direct);
33 proxyMode->addItem(tr("Use the system proxy settings"), (int)NetworkProxyFactory::Mode_System);
34 proxyMode->addItem(tr("Manual proxy configuration"), (int)NetworkProxyFactory::Mode_Manual);
35 connect(proxyMode, SIGNAL(currentIndexChanged(int)), SLOT(toggleMode()));
36 }
37
~ProxySettings()38 ProxySettings::~ProxySettings()
39 {
40 }
41
load()42 void ProxySettings::load()
43 {
44 QSettings s;
45 s.beginGroup(NetworkProxyFactory::constSettingsGroup);
46
47 int mode=s.value("mode", NetworkProxyFactory::Mode_System).toInt();
48 for (int i=0; i<proxyMode->count(); ++i) {
49 if (proxyMode->itemData(i).toInt()==mode) {
50 proxyMode->setCurrentIndex(i);
51 break;
52 }
53 }
54
55 proxyType->setCurrentIndex(QNetworkProxy::HttpProxy==s.value("type", QNetworkProxy::HttpProxy).toInt() ? 0 : 1);
56 proxyHost->setText(s.value("hostname").toString());
57 proxyPort->setValue(s.value("port", 8080).toInt());
58 proxyUsername->setText(s.value("username").toString());
59 proxyPassword->setText(s.value("password").toString());
60 s.endGroup();
61 toggleMode();
62 }
63
save()64 void ProxySettings::save()
65 {
66 QSettings s;
67 s.beginGroup(NetworkProxyFactory::constSettingsGroup);
68
69 s.setValue("mode", proxyMode->itemData(proxyMode->currentIndex()).toInt());
70 s.setValue("type", 0==proxyType->currentIndex() ? QNetworkProxy::HttpProxy : QNetworkProxy::Socks5Proxy);
71 s.setValue("hostname", proxyHost->text());
72 s.setValue("port", proxyPort->value());
73 s.setValue("username", proxyUsername->text());
74 s.setValue("password", proxyPassword->text());
75 s.endGroup();
76 NetworkProxyFactory::self()->reloadSettings();
77 }
78
toggleMode()79 void ProxySettings::toggleMode()
80 {
81 bool showManual=NetworkProxyFactory::Mode_Manual==proxyMode->itemData(proxyMode->currentIndex()).toInt();
82 proxyType->setVisible(showManual);
83 proxyTypeLabel->setVisible(showManual);
84 proxyHost->setVisible(showManual);
85 proxyHostLabel->setVisible(showManual);
86 proxyPort->setVisible(showManual);
87 proxyPortLabel->setVisible(showManual);
88 proxyUsername->setVisible(showManual);
89 proxyUsernameLabel->setVisible(showManual);
90 proxyPassword->setVisible(showManual);
91 proxyPasswordLabel->setVisible(showManual);
92 }
93
94 #include "moc_proxysettings.cpp"
95