Changeset View
Changeset View
Standalone View
Standalone View
libs/editor/settings/wireguardtabwidget.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Bruce Anderson <banderson19com@san.rr.com> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library 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 GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | #include "debug.h" | ||||
21 | #include "wireguardtabwidget.h" | ||||
22 | #include "wireguardpeerwidget.h" | ||||
23 | #include "ui_wireguardtabwidget.h" | ||||
24 | #include "ui_wireguardpeerwidget.h" | ||||
25 | #include "uiutils.h" | ||||
26 | #include "simpleipv4addressvalidator.h" | ||||
27 | #include "simpleiplistvalidator.h" | ||||
28 | #include "wireguardkeyvalidator.h" | ||||
29 | | ||||
30 | #include <QStandardItemModel> | ||||
31 | | ||||
32 | #include <NetworkManagerQt/Utils> | ||||
33 | #include <NetworkManagerQt/WireguardSetting> | ||||
34 | #include <NetworkManagerQt/Ipv4Setting> | ||||
35 | #include <NetworkManagerQt/Ipv6Setting> | ||||
36 | #include <KColorScheme> | ||||
37 | #include <KConfig> | ||||
38 | #include <KConfigGroup> | ||||
39 | | ||||
40 | // Keys for the NetworkManager configuration | ||||
41 | #define PNM_SETTING_WIREGUARD_SETTING_NAME "wireguard" | ||||
42 | | ||||
43 | #define PNM_WG_KEY_PEERS "peers" | ||||
44 | #define PNM_WG_KEY_MTU "mtu" | ||||
45 | #define PNM_WG_KEY_PEER_ROUTES "peer-routes" | ||||
46 | #define PNM_WG_PEER_KEY_ALLOWED_IPS "allowed-ips" | ||||
47 | #define PNM_WG_PEER_KEY_ENDPOINT "endpoint" | ||||
48 | #define PNM_WG_PEER_KEY_PERSISTENT_KEEPALIVE "persistent-keepalive" | ||||
49 | #define PNM_WG_PEER_KEY_PRESHARED_KEY "preshared-key" | ||||
50 | #define PNM_WG_PEER_KEY_PRESHARED_KEY_FLAGS "preshared-key-flags" | ||||
51 | #define PNM_WG_PEER_KEY_PUBLIC_KEY "public-key" | ||||
52 | | ||||
53 | static WireGuardKeyValidator keyValidator(nullptr); | ||||
54 | static SimpleIpListValidator allowedIPsValidator(nullptr, SimpleIpListValidator::WithCidr, | ||||
55 | SimpleIpListValidator::Both); | ||||
56 | | ||||
57 | class WireGuardTabWidget::Private | ||||
58 | { | ||||
59 | public: | ||||
60 | Private(); | ||||
61 | ~Private(); | ||||
62 | | ||||
63 | Ui_WireGuardTabWidget ui; | ||||
64 | NetworkManager::WireGuardSetting::Ptr setting; | ||||
65 | KSharedConfigPtr config; | ||||
66 | NMVariantMapList peers; | ||||
67 | int currentIndex; | ||||
68 | bool currentPeerValid; | ||||
69 | bool otherPeersValid; | ||||
70 | }; | ||||
71 | | ||||
72 | WireGuardTabWidget::Private::Private(void) | ||||
73 | { | ||||
74 | } | ||||
75 | | ||||
76 | WireGuardTabWidget::Private::~Private() | ||||
77 | { | ||||
78 | } | ||||
79 | | ||||
80 | WireGuardTabWidget::WireGuardTabWidget(const NMVariantMapList& peerData, QWidget* parent, Qt::WindowFlags f) | ||||
81 | : QDialog(parent, f) | ||||
82 | , d(new Private) | ||||
83 | { | ||||
84 | d->ui.setupUi(this); | ||||
85 | | ||||
86 | d->config = KSharedConfig::openConfig(); | ||||
87 | setWindowTitle(i18nc("@title: window wireguard peers properties", | ||||
88 | "WireGuard peers properties")); | ||||
89 | connect(d->ui.btnAdd, &QPushButton::clicked, this, &WireGuardTabWidget::slotAddPeer); | ||||
90 | connect(d->ui.btnRemove, &QPushButton::clicked, this, &WireGuardTabWidget::slotRemovePeer); | ||||
91 | connect(d->ui.buttonBox, &QDialogButtonBox::accepted, this, &WireGuardTabWidget::accept); | ||||
92 | connect(d->ui.buttonBox, &QDialogButtonBox::rejected, this, &WireGuardTabWidget::reject); | ||||
93 | | ||||
94 | KAcceleratorManager::manage(this); | ||||
95 | | ||||
96 | loadConfig(peerData); | ||||
97 | | ||||
98 | if (peerData.isEmpty()) | ||||
99 | slotAddPeer(); | ||||
100 | } | ||||
101 | | ||||
102 | WireGuardTabWidget::~WireGuardTabWidget() | ||||
103 | { | ||||
104 | delete d; | ||||
105 | } | ||||
106 | | ||||
107 | void WireGuardTabWidget::loadConfig(const NMVariantMapList& peerData) | ||||
108 | { | ||||
109 | d->peers = peerData; | ||||
110 | int numIncomingPeers = d->peers.size(); | ||||
111 | | ||||
112 | // If there weren't any peers in the incoming setting, create | ||||
113 | // the required first element | ||||
114 | if (d->peers.isEmpty()) | ||||
115 | d->peers.append(*(new QVariantMap())); | ||||
116 | | ||||
117 | for (int i = 0; i < numIncomingPeers; i++) { | ||||
118 | slotAddPeerWithData(peerData[i]); | ||||
119 | } | ||||
120 | d->ui.tabWidget->setCurrentIndex(0); | ||||
121 | } | ||||
122 | | ||||
123 | NMVariantMapList WireGuardTabWidget::setting() const | ||||
124 | { | ||||
125 | d->peers.clear(); | ||||
126 | for (int i = 0; i < d->ui.tabWidget->count(); i++) | ||||
127 | d->peers.append(static_cast<WireGuardPeerWidget*>(d->ui.tabWidget->widget(i))->setting()); | ||||
128 | return d->peers; | ||||
129 | } | ||||
130 | | ||||
131 | void WireGuardTabWidget::slotAddPeer() | ||||
132 | { | ||||
133 | QVariantMap *newItem = new QVariantMap; | ||||
134 | int numPeers = d->ui.tabWidget->count() + 1; | ||||
135 | WireGuardPeerWidget *newTab = new WireGuardPeerWidget(*newItem); | ||||
136 | connect(newTab, &WireGuardPeerWidget::notifyValid, this, &WireGuardTabWidget::slotWidgetChanged); | ||||
137 | d->ui.tabWidget->addTab(newTab, QString("Peer %1").arg(QString::number(numPeers))); | ||||
138 | d->peers.append(*newItem); | ||||
139 | d->ui.tabWidget->setCurrentIndex(numPeers - 1); | ||||
140 | slotWidgetChanged(); | ||||
141 | } | ||||
142 | | ||||
143 | void WireGuardTabWidget::slotAddPeerWithData(QVariantMap peerData) | ||||
144 | { | ||||
145 | int numPeers = d->ui.tabWidget->count() + 1; | ||||
146 | WireGuardPeerWidget *newTab = new WireGuardPeerWidget(peerData); | ||||
147 | d->ui.tabWidget->addTab(newTab, QString("Peer %1").arg(QString::number(numPeers))); | ||||
148 | connect(newTab, &WireGuardPeerWidget::notifyValid, this, &WireGuardTabWidget::slotWidgetChanged); | ||||
149 | d->peers.append(peerData); | ||||
150 | d->ui.tabWidget->setCurrentIndex(numPeers - 1); | ||||
151 | slotWidgetChanged(); | ||||
152 | } | ||||
153 | | ||||
154 | void WireGuardTabWidget::slotRemovePeer() | ||||
155 | { | ||||
156 | int numPeers = d->ui.tabWidget->count() - 1; | ||||
157 | d->ui.tabWidget->removeTab(d->ui.tabWidget->currentIndex()); | ||||
158 | if (numPeers == 0) { | ||||
159 | slotAddPeer(); | ||||
160 | numPeers = 1; | ||||
161 | } | ||||
162 | | ||||
163 | // Retitle the tabs to reflect the current numbers | ||||
164 | for (int i = 0; i < numPeers; i++) | ||||
165 | d->ui.tabWidget->setTabText(i, QString("Peer %1").arg(QString::number(i + 1))); | ||||
166 | } | ||||
167 | | ||||
168 | void WireGuardTabWidget::slotWidgetChanged() | ||||
169 | { | ||||
170 | bool valid = true; | ||||
171 | for (int i = 0; i < d->ui.tabWidget->count(); i++) { | ||||
172 | if (!static_cast<WireGuardPeerWidget*>(d->ui.tabWidget->widget(i))->isValid()) { | ||||
173 | valid = false; | ||||
174 | break; | ||||
175 | } | ||||
176 | } | ||||
177 | d->ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); | ||||
178 | } |