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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* This file is part of KNemo
Copyright (C) 2004, 2006 Percy Leonhardt <[email protected]>
KNemo is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
KNemo 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <qtimer.h>
#include <qlabel.h>
#include <qstring.h>
#include <qgroupbox.h>
#include <qdatetime.h>
#include <qtabwidget.h>
#include <kdebug.h>
#include <kconfig.h>
#include <klocale.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kactivelabel.h>
#include <kio/global.h>
#include "data.h"
#include "interface.h"
#include "interfacestatistics.h"
#include "interfacestatusdialog.h"
InterfaceStatusDialog::InterfaceStatusDialog( Interface* interface, QWidget* parent, const char* name )
: InterfaceStatusDlg( parent, name ),
mPosInitialized( false ),
mInterface( interface )
{
setIcon( SmallIcon( "knemo" ) );
setCaption( interface->getName() + " " + i18n( "Interface Status" ) );
updateDialog();
if ( interface->getData().available )
{
enableNetworkGroups( 0 );
}
else
{
disableNetworkGroups( 0 );
}
if ( !interface->getData().wirelessDevice )
{
QWidget* wirelessTab = tabWidget->page( 2 );
tabWidget->removePage( wirelessTab );
delete wirelessTab;
}
if ( !interface->getSettings().activateStatistics )
{
setStatisticsGroupEnabled( false );
}
// Restore window size and position.
KConfig* config = new KConfig( "knemorc", false );
if ( config->hasGroup( "Interface_" + mInterface->getName() ) )
{
config->setGroup( "Interface_" + mInterface->getName() );
if ( config->hasKey( "StatusX" ) && config->hasKey( "StatusY" ) )
{
mPos.setX( config->readNumEntry( "StatusX" ) );
mPos.setY( config->readNumEntry( "StatusY" ) );
mPosInitialized = true;
}
if ( config->hasKey( "StatusWidth" ) && config->hasKey( "StatusHeight" ) )
resize( config->readNumEntry( "StatusWidth" ),
config->readNumEntry( "StatusHeight" ) );
}
delete config;
statisticsChanged();
mTimer = new QTimer();
connect( mTimer, SIGNAL( timeout() ), this, SLOT( updateDialog() ) );
mTimer->start( 1000 );
}
InterfaceStatusDialog::~InterfaceStatusDialog()
{
mTimer->stop();
delete mTimer;
// Store window size and position.
KConfig* config = new KConfig( "knemorc", false );
if ( config->hasGroup( "Interface_" + mInterface->getName() ) )
{
config->setGroup( "Interface_" + mInterface->getName() );
config->writeEntry( "StatusX", x() );
config->writeEntry( "StatusY", y() );
config->writeEntry( "StatusWidth", width() );
config->writeEntry( "StatusHeight", height() );
config->sync();
}
delete config;
}
void InterfaceStatusDialog::hide()
{
mPos = pos();
mPosInitialized = true;
QDialog::hide();
}
void InterfaceStatusDialog::show()
{
QDialog::show();
/**
* mPosInitialized should always be true, except when
* starting KNemo for the very first time.
*/
if ( mPosInitialized )
move( mPos );
}
void InterfaceStatusDialog::setStatisticsGroupEnabled( bool enabled )
{
groupBoxStatistics->setEnabled( enabled );
}
void InterfaceStatusDialog::updateDialog()
{
InterfaceData& data = mInterface->getData();
InterfaceSettings& settings = mInterface->getSettings();
// connection tab
textLabelInterface->setText( mInterface->getName() );
textLabelAlias->setText( settings.alias );
if ( data.available )
{
textLabelStatus->setText( i18n( "Connection established." ) );
int upsecs = mInterface->getStartTime().secsTo( QDateTime::currentDateTime() );
int updays = upsecs / 86400; // don't use QDateTime::daysTo() because
// we only want complete days
QString uptime;
if ( updays == 1 )
uptime = "1 day, ";
else if ( updays > 1 )
uptime = QString( "%1 days, " ).arg( updays );
upsecs -= 86400 * updays; // we only want the seconds of today
int hrs = upsecs / 3600;
int mins = ( upsecs - hrs * 3600 ) / 60;
int secs = upsecs - hrs * 3600 - mins * 60;
QString time;
time.sprintf( "%02d:%02d:%02d", hrs, mins, secs );
uptime += time;
textLabelUptime->setText( uptime );
}
else if ( data.existing )
{
textLabelStatus->setText( i18n( "Not connected." ) );
textLabelUptime->setText( "00:00:00" );
}
else
{
textLabelStatus->setText( i18n( "Not existing." ) );
textLabelUptime->setText( "00:00:00" );
}
if ( data.available )
{
// ip tab
textLabelIP->setText( data.ipAddress );
textLabelSubnet->setText( data.subnetMask );
if ( mInterface->getType() == Interface::ETHERNET )
{
variableLabel1->setText( i18n( "Broadcast Address:" ) );
variableText1->setText( data.broadcastAddress );
variableLabel2->setText( i18n( "Default Gateway:" ) );
variableText2->setText( data.defaultGateway );
variableLabel3->setText( i18n( "HW-Address:" ) );
variableText3->setText( data.hwAddress );
}
else if ( mInterface->getType() == Interface::PPP )
{
variableLabel1->setText( i18n( "PtP-Address:" ) );
variableText1->setText( data.ptpAddress );
variableLabel2->setText( QString::null );
variableText2->setText( QString::null );
variableLabel3->setText( QString::null );
variableText3->setText( QString::null );
}
else
{
// shouldn't happen
variableLabel1->setText( QString::null );
variableText1->setText( QString::null );
variableLabel2->setText( QString::null );
variableText2->setText( QString::null );
variableLabel3->setText( QString::null );
variableText3->setText( QString::null );
}
// traffic tab
textLabelPacketsSend->setText( QString::number( data.txPackets ) );
textLabelPacketsReceived->setText( QString::number( data.rxPackets ) );
textLabelBytesSend->setText( data.txString );
textLabelBytesReceived->setText( data.rxString );
unsigned long bytesPerSecond = data.outgoingBytes / mInterface->getGeneralData().pollInterval;
textLabelSpeedSend->setText( KIO::convertSize( bytesPerSecond ) + i18n( "/s" ) );
bytesPerSecond = data.incomingBytes / mInterface->getGeneralData().pollInterval;
textLabelSpeedReceived->setText( KIO::convertSize( bytesPerSecond ) + i18n( "/s" ) );
if ( data.wirelessDevice )
{
WirelessData& wdata = mInterface->getWirelessData();
// wireless tab
textLabelESSID->setText( wdata.essid );
textLabelAccessPoint->setText( wdata.accessPoint );
textLabelNickName->setText( wdata.nickName );
textLabelMode->setText( wdata.mode );
textLabelFreqChannel->setText( wdata.frequency + " [" + wdata.channel + "]" );
textLabelBitRate->setText( wdata.bitRate );
textLabelLinkQuality->setText( wdata.linkQuality + "%" );
if ( wdata.encryption == true )
{
textLabelEncryption->setText( i18n( "active" ) );
}
else
{
textLabelEncryption->setText( i18n( "off" ) );
}
}
}
}
void InterfaceStatusDialog::enableNetworkGroups( int )
{
groupBoxIP->setEnabled( true );
groupBoxCurrentConnection->setEnabled( true );
}
void InterfaceStatusDialog::disableNetworkGroups( int )
{
groupBoxIP->setEnabled( false );
groupBoxCurrentConnection->setEnabled( false );
// clear IP group
textLabelIP->setText( QString::null );
textLabelSubnet->setText( QString::null );
variableText1->setText( QString::null );
variableText2->setText( QString::null );
variableText3->setText( QString::null );
// clear current connection group
textLabelPacketsSend->setText( QString::null );
textLabelPacketsReceived->setText( QString::null );
textLabelBytesSend->setText( QString::null );
textLabelBytesReceived->setText( QString::null );
textLabelSpeedSend->setText( QString::null );
textLabelSpeedReceived->setText( QString::null );
// clear wireless tab
if ( mInterface->getData().wirelessDevice )
{
textLabelESSID->setText( QString::null );
textLabelAccessPoint->setText( QString::null );
textLabelNickName->setText( QString::null );
textLabelMode->setText( QString::null );
textLabelFreqChannel->setText( QString::null );
textLabelBitRate->setText( QString::null );
textLabelLinkQuality->setText( QString::null );
textLabelEncryption->setText( QString::null );
}
}
void InterfaceStatusDialog::statisticsChanged()
{
InterfaceStatistics* statistics = mInterface->getStatistics();
if ( statistics == 0 )
{
return;
}
const StatisticEntry* entry = statistics->getCurrentDay();
textLabelTodaySent->setText( KIO::convertSize( entry->txBytes ) );
textLabelTodayReceived->setText( KIO::convertSize( entry->rxBytes ) );
textLabelTodayTotal->setText( KIO::convertSize( entry->txBytes + entry->rxBytes ) );
entry = statistics->getCurrentMonth();
textLabelMonthSent->setText( KIO::convertSize( entry->txBytes ) );
textLabelMonthReceived->setText( KIO::convertSize( entry->rxBytes ) );
textLabelMonthTotal->setText( KIO::convertSize( entry->txBytes + entry->rxBytes ) );
entry = statistics->getCurrentYear();
textLabelYearSent->setText( KIO::convertSize( entry->txBytes ) );
textLabelYearReceived->setText( KIO::convertSize( entry->rxBytes ) );
textLabelYearTotal->setText( KIO::convertSize( entry->txBytes + entry->rxBytes ) );
}
#include "interfacestatusdialog.moc"
|