-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNetwork.cxx
More file actions
182 lines (140 loc) · 5.53 KB
/
Network.cxx
File metadata and controls
182 lines (140 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
This file is part of liquidshell.
SPDX-FileCopyrightText: 2017 - 2024 Martin Koller <martin@kollix.at>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <Network.hxx>
#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/WirelessDevice>
#include <NetworkManagerQt/VpnConnection>
#include <QIcon>
#include <QMouseEvent>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDebug>
#include <KLocalizedString>
#include <KIconLoader>
//--------------------------------------------------------------------------------
Network::Network(QWidget *parent)
: SysTrayItem(parent)
{
blinkTimer.setInterval(500);
connect(&blinkTimer, &QTimer::timeout, [this]() { blinkState = !blinkState; setPixmap(blinkState ? origPixmap : QPixmap()); });
checkState();
connect(NetworkManager::notifier(), &NetworkManager::Notifier::statusChanged, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::connectivityChanged, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::primaryConnectionChanged, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionRemoved, this, &Network::checkState);
connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionsChanged, this, &Network::checkState);
connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, &Network::checkState);
QDBusConnection::sessionBus().send(
QDBusMessage::createMethodCall("org.kde.kded5", "/modules/networkmanagement",
"org.kde.plasmanetworkmanagement", "init"));
}
//--------------------------------------------------------------------------------
void Network::checkState()
{
if ( NetworkManager::status() == NetworkManager::Unknown )
{
// if the system does not have NM running, hide the icon
hide();
return;
}
show();
if ( NetworkManager::status() == NetworkManager::Connecting )
blinkTimer.start();
else
blinkTimer.stop();
if ( !NetworkManager::primaryConnection() || !NetworkManager::primaryConnection()->connection() )
{
if ( QIcon::hasThemeIcon("network-offline") )
setPixmap(origPixmap = QIcon::fromTheme("network-offline").pixmap(size()));
else
setPixmap(origPixmap = QIcon::fromTheme("network-disconnect").pixmap(size()));
setToolTip(i18n("No Network Connection"));
return;
}
NetworkManager::ActiveConnection::Ptr conn(NetworkManager::primaryConnection());
//connect(conn.data(), &NetworkManager::ActiveConnection::vpnChanged, this, &Network::checkState);
QString tip;
if ( NetworkManager::connectivity() == NetworkManager::Full )
tip = i18n("Full Network Connectivity (%1)", conn->connection()->name());
else
tip = i18n("Limited Network Connectivity (%1)", conn->connection()->name());
NetworkManager::Device::Ptr device;
if ( conn->devices().count() )
{
device = NetworkManager::findNetworkInterface(conn->devices()[0]);
if ( device && device->ipV4Config().addresses().count() )
tip += "\n" + i18n("IPv4 Address: %1", device->ipV4Config().addresses()[0].ip().toString());
}
QPixmap pixmap;
if ( conn->type() == NetworkManager::ConnectionSettings::Wireless )
{
NetworkManager::WirelessDevice::Ptr dev = qobject_cast<NetworkManager::WirelessDevice::Ptr>(device);
if ( dev )
{
NetworkManager::AccessPoint::Ptr accessPoint = dev->activeAccessPoint();
int signalStrength = accessPoint.isNull() ? 0 : accessPoint->signalStrength();
int x = qRound(signalStrength / 25.0) * 25;
pixmap = QIcon::fromTheme(QString("network-wireless-connected-%1").arg(x)).pixmap(size());
if ( !accessPoint.isNull() )
tip += "\n" + i18n("SSID: %1", accessPoint->ssid());
tip += "\n" + i18n("Signal Strength: %1", signalStrength);
}
}
else
{
pixmap = QIcon::fromTheme("network-connect").pixmap(size());
}
//qDebug() << conn << "type" << conn->type() << "vpn" << conn->vpn();
bool vpnActive = false;
for (const NetworkManager::ActiveConnection::Ptr &ac : NetworkManager::activeConnections())
{
//qDebug() << ac << ac->id() << ac->type();
if ( ac->vpn() )
{
vpnActive = true;
break;
}
// search for tunnel device
if ( ac->type() == NetworkManager::ConnectionSettings::Tun )
{
vpnActive = true;
break;
}
}
if ( vpnActive )
{
pixmap = QIcon::fromTheme("security-high").pixmap(size());
tip += "\n" + i18n("VPN active");
}
setPixmap(origPixmap = pixmap);
setToolTip(tip);
}
//--------------------------------------------------------------------------------
QWidget *Network::getDetailsList()
{
if ( !networkList )
{
networkList = new NetworkList(this);
networkList->setAttribute(Qt::WA_DeleteOnClose);
connect(networkList.data(), &NetworkList::changed, this, &Network::showDetailsList); // reposition
connect(networkList.data(), &NetworkList::configureDialogClicked, this, &Network::openConfigureDialog);
}
return networkList.data();
}
//--------------------------------------------------------------------------------
void Network::openConfigureDialog()
{
if ( !dialog )
{
dialog = new KCMultiDialog(nullptr);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->addModule(KPluginMetaData("plasma/kcms/systemsettings_qwidgets/kcm_networkmanagement"));
}
dialog->show();
if ( networkList ) // should always exist
networkList->close();
}
//--------------------------------------------------------------------------------