/
kcm_nic.cpp
163 lines (135 loc) · 4.94 KB
/
kcm_nic.cpp
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
/*
* kcm_nic.cpp
*
* Copyright (C) 2010 David Hubner <hubnerd@ntlworld.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "kcm_nic.h"
//QT
#include <QLabel>
#include <QVBoxLayout>
#include <QScrollArea>
#include <QStatusBar>
//KDE
#include <KPluginFactory>
#include <KPluginLoader>
#include <KDebug>
#include <KLocale>
K_PLUGIN_FACTORY(KcmNicModuleFactory, registerPlugin<KcmNic>();)
K_EXPORT_PLUGIN(KcmNicModuleFactory("kcm_nic"))
KcmNic::KcmNic(QWidget *parent, const QVariantList &) :
KCModule(KcmNicModuleFactory::componentData(), parent),
m_parentWidget(parent),
m_statusLabel(NULL)
{
setContentsMargins(0,0,0,0);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
m_backend = new NicBackend();
if(m_backend->networkInterfacesCount() > 0)
{
createInterface();
updateStatusBarStatus(m_backend->isNetConnected());
connect(m_backend,SIGNAL(statusChanged(Solid::Networking::Status)),this,SLOT(networkingStatusChangedSlot(Solid::Networking::Status)));
}
else
{
createOppsMessage();
}
}
KcmNic::~KcmNic()
{
delete m_backend;
}
void KcmNic::createInterface()
{
QVBoxLayout *mainLay = new QVBoxLayout(this);
mainLay->setContentsMargins(0,0,0,0);
mainLay->setSpacing(5);
m_tabs = new KTabWidget(this);
m_tabs->setContentsMargins(0,0,0,0);
m_tabs->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
//All
QScrollArea *allTabScroll = new QScrollArea(m_tabs);
allTabScroll->setWidgetResizable(true);
allTab = new QWidget(allTabScroll);
createWidgetDisplay(allTab,NicBackend::ALL);
allTabScroll->setWidget(allTab);
allTabScroll->setWhatsThis(i18n("This tab shows all network interfaces"));
m_tabs->addTab(allTabScroll,i18nc("Show all interfaces tab label","All Interfaces"));
// Wired
QScrollArea *wiredTabScroll = new QScrollArea(m_tabs);
wiredTabScroll->setWidgetResizable(true);
wiredTab = new QWidget();
createWidgetDisplay(wiredTab,NicBackend::WIRED);
wiredTabScroll->setWidget(wiredTab);
wiredTabScroll->setWhatsThis(i18n("This tab shows only wired network interfaces"));
m_tabs->addTab(wiredTabScroll,i18nc("Show all wired interfaces tab label", "Wired Interfaces"));
//Wireless
QScrollArea *wirelessTabScroll = new QScrollArea(m_tabs);
wirelessTabScroll->setWidgetResizable(true);
wirelessTab = new QWidget(wirelessTabScroll);
createWidgetDisplay(wirelessTab,NicBackend::WIRELESS);
wirelessTabScroll->setWidget(wirelessTab);
wirelessTabScroll->setWhatsThis(i18n("This tab shows only wireless network interfaces"));
m_tabs->addTab(wirelessTabScroll,i18nc("Show all wireless interfaces tab label", "Wireless Interfaces"));
//StatusBar
QStatusBar *status = new QStatusBar();
status->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
status->setContentsMargins(0,0,0,0);
status->setSizeGripEnabled(false);
m_statusLabel = new QLabel();
//Adding
status->addPermanentWidget(m_statusLabel);
mainLay->addWidget(m_tabs);
mainLay->addWidget(status);
}
void KcmNic::createOppsMessage()
{
QVBoxLayout *mainLay = new QVBoxLayout(this);
mainLay->setContentsMargins(0,0,0,0);
mainLay->setSpacing(5);
QLabel *message = new QLabel(i18n("No Network Cards where detected.) ")
+ i18n("\nThis could be due to no network cards being found or that a Solid backend is not being used."));
message->setWordWrap(true);
message->setAlignment(Qt::AlignCenter);
mainLay->addWidget(message);
}
void KcmNic::createWidgetDisplay(QWidget *parent, enum NicBackend::InterfaceType type)
{
QVBoxLayout *widgetLayout = new QVBoxLayout(parent);
foreach (const NetworkInterfaceBase *device, m_backend->interfaces(type))
{
widgetLayout->addWidget(const_cast<NetworkInterfaceBase *>(device));
}
}
void KcmNic::updateStatusBarStatus(bool isConnected)
{
if(m_statusLabel == NULL) return;
QString message = (isConnected == 0 ? i18n("Not Connected") : i18n("Connected"));
m_statusLabel->setText(i18nc("%1 is connection status of connected or not connected","Connection Status: %1", message));
}
void KcmNic::networkingStatusChangedSlot(Solid::Networking::Status status)
{
if(status == Solid::Networking::Unconnected)
{
updateStatusBarStatus(false);
}
else if(status == Solid::Networking::Connected)
{
updateStatusBarStatus(true);
}
}