1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  * Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
6  *
7  * Strawberry is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Strawberry is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "config.h"
23 
24 #include <QVariant>
25 #include <QSettings>
26 #include <QNetworkProxy>
27 #include <QComboBox>
28 #include <QGroupBox>
29 #include <QLineEdit>
30 #include <QRadioButton>
31 #include <QSpinBox>
32 
33 #include "core/iconloader.h"
34 #include "core/networkproxyfactory.h"
35 #include "networkproxysettingspage.h"
36 #include "settings/settingspage.h"
37 #include "ui_networkproxysettingspage.h"
38 
39 class SettingsDialog;
40 
41 const char *NetworkProxySettingsPage::kSettingsGroup = "NetworkProxy";
42 
43 NetworkProxySettingsPage::NetworkProxySettingsPage(SettingsDialog *dialog, QWidget *parent)
44     : SettingsPage(dialog, parent),
45       ui_(new Ui_NetworkProxySettingsPage) {
46 
47   ui_->setupUi(this);
48   setWindowIcon(IconLoader::Load("applications-internet"));
49 
50 }
51 
MoodbarSettingsPage(SettingsDialog * dialog,QWidget * parent)52 NetworkProxySettingsPage::~NetworkProxySettingsPage() { delete ui_; }
53 
54 void NetworkProxySettingsPage::Load() {
55 
56   QSettings s;
57 
58   s.beginGroup(NetworkProxyFactory::kSettingsGroup);
59   NetworkProxyFactory::Mode mode = NetworkProxyFactory::Mode(s.value("mode", NetworkProxyFactory::Mode_System).toInt());
60   switch (mode) {
61     case NetworkProxyFactory::Mode_Manual:
62       ui_->proxy_manual->setChecked(true);
63       break;
~MoodbarSettingsPage()64 
65     case NetworkProxyFactory::Mode_Direct:
66       ui_->proxy_direct->setChecked(true);
67       break;
68 
69     case NetworkProxyFactory::Mode_System:
70     default:
71       ui_->proxy_system->setChecked(true);
72       break;
73   }
74 
75   ui_->proxy_type->setCurrentIndex(s.value("type", QNetworkProxy::HttpProxy).toInt() == QNetworkProxy::HttpProxy ? 0 : 1);
76   ui_->proxy_hostname->setText(s.value("hostname").toString());
77   ui_->proxy_port->setValue(s.value("port").toInt());
78   ui_->proxy_auth->setChecked(s.value("use_authentication", false).toBool());
79   ui_->proxy_username->setText(s.value("username").toString());
80   ui_->proxy_password->setText(s.value("password").toString());
81   ui_->proxy_engine->setChecked(s.value("engine", true).toBool());
82   s.endGroup();
83 
Save()84   Init(ui_->layout_networkproxysettingspage->parentWidget());
85 
86   if (!QSettings().childGroups().contains(kSettingsGroup)) set_changed();
87 
88 }
89 
90 void NetworkProxySettingsPage::Save() {
91 
92   QSettings s;
93 
94   NetworkProxyFactory::Mode mode = NetworkProxyFactory::Mode_System;
Cancel()95   if (ui_->proxy_direct->isChecked()) mode = NetworkProxyFactory::Mode_Direct;
96   else if (ui_->proxy_system->isChecked()) mode = NetworkProxyFactory::Mode_System;
InitMoodbarPreviews()97   else if (ui_->proxy_manual->isChecked()) mode = NetworkProxyFactory::Mode_Manual;
98 
99   s.beginGroup(NetworkProxyFactory::kSettingsGroup);
100   s.setValue("mode", mode);
101   s.setValue("type", ui_->proxy_type->currentIndex() == 0 ? QNetworkProxy::HttpProxy : QNetworkProxy::Socks5Proxy);
102   s.setValue("hostname", ui_->proxy_hostname->text());
103   s.setValue("port", ui_->proxy_port->value());
104   s.setValue("use_authentication", ui_->proxy_auth->isChecked());
105   s.setValue("username", ui_->proxy_username->text());
106   s.setValue("password", ui_->proxy_password->text());
107   s.setValue("engine", ui_->proxy_engine->isChecked());
108   s.endGroup();
109 
110   NetworkProxyFactory::Instance()->ReloadSettings();
111 
112 }
113