forked from KDE/liquidshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.cxx
146 lines (113 loc) · 4.82 KB
/
Network.cxx
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
/*
Copyright 2017 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see <http://www.gnu.org/licenses/>.
*/
#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(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::Connecting )
blinkTimer.start();
else
blinkTimer.stop();
if ( (NetworkManager::connectivity() != NetworkManager::Full) ||
!NetworkManager::primaryConnection() || !NetworkManager::primaryConnection()->connection() )
{
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 = i18n("Full 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 = std::round(signalStrength / 25.0) * 25;
pixmap = QIcon::fromTheme(QString("network-wireless-connected-%1").arg(x)).pixmap(size());
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())
{
if ( ac->vpn() )
{
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
}
return networkList.data();
}
//--------------------------------------------------------------------------------