18
18
Boston, MA 02110-1301, USA.
23
#include <KCalendarSystem>
24
#include <KColorScheme>
25
#include <KNotification>
23
26
#include <KWindowSystem>
27
#include <kio/global.h>
29
#include "backends/backendbase.h"
25
31
#include "interface.h"
26
32
#include "interfaceplotterdialog.h"
27
33
#include "interfacestatistics.h"
28
34
#include "interfacestatusdialog.h"
29
35
#include "interfacestatisticsdialog.h"
31
Interface::Interface( QString ifname,
32
const GeneralData& generalData,
33
const PlotterSettings& plotterSettings )
37
Interface::Interface( const QString &ifname,
38
const BackendData* data,
39
const GeneralData& generalData )
35
mType( UNKNOWN_TYPE ),
36
mState( UNKNOWN_STATE ),
41
mType( KNemoIface::UnknownType ),
42
mState( KNemoIface::UnknownState ),
43
mPreviousState( KNemoIface::UnknownState ),
47
mUptimeString( "00:00:00" ),
41
52
mStatusDialog( 0 ),
42
53
mStatisticsDialog( 0 ),
43
54
mPlotterDialog( 0 ),
44
mGeneralData( generalData ),
45
mPlotterSettings( plotterSettings )
56
mGeneralData( generalData )
47
mPlotterDialog = new InterfacePlotterDialog( mPlotterSettings, mName );
48
mPlotterTimer = new QTimer();
58
mPlotterDialog = new InterfacePlotterDialog( mName );
50
connect( mPlotterTimer, SIGNAL( timeout() ),
51
this, SLOT( updatePlotter() ) );
52
connect( &mMonitor, SIGNAL( statusChanged( int ) ),
53
&mIcon, SLOT( updateStatus( int ) ) );
54
connect( &mMonitor, SIGNAL( available( int ) ),
55
&mIcon, SLOT( updateTrayStatus( int ) ) );
56
connect( &mMonitor, SIGNAL( notAvailable( int ) ),
57
&mIcon, SLOT( updateTrayStatus( int ) ) );
58
connect( &mMonitor, SIGNAL( notExisting( int ) ),
59
&mIcon, SLOT( updateTrayStatus( int ) ) );
60
connect( &mMonitor, SIGNAL( updateDetails() ),
61
this, SLOT( updateDetails() ) );
62
connect( &mMonitor, SIGNAL( available( int ) ),
63
this, SLOT( setStartTime( int ) ) );
64
connect( &mMonitor, SIGNAL( statusChanged( int ) ),
65
this, SLOT( resetData( int ) ) );
66
60
connect( &mIcon, SIGNAL( statisticsSelected() ),
67
61
this, SLOT( showStatisticsDialog() ) );
70
64
Interface::~Interface()
72
if ( mStatusDialog != 0 )
76
if ( mPlotterDialog != 0 )
78
delete mPlotterDialog;
80
if ( mPlotterTimer != 0 )
82
mPlotterTimer->stop();
67
delete mPlotterDialog;
85
68
if ( mStatistics != 0 )
87
70
// this will also delete a possibly open statistics dialog
92
75
void Interface::configChanged()
94
// UNKNOWN_STATE to avoid notification
95
mIcon.updateTrayStatus( UNKNOWN_STATE );
96
// handle changed iconset by user
97
mIcon.updateStatus( mState );
100
if ( mPlotterDialog != 0L )
102
mPlotterDialog->configChanged();
77
KSharedConfigPtr config = KGlobal::config();
78
QString group( confg_interface );
80
KConfigGroup interfaceGroup( config, group );
82
mSettings.alias = interfaceGroup.readEntry( conf_alias ).trimmed();
83
mSettings.iconTheme = interfaceGroup.readEntry( conf_iconTheme, s.iconTheme );
84
QStringList themeNames;
85
QList<KNemoTheme> themes = findThemes();
86
// Let's check that it's available
87
foreach( KNemoTheme theme, themes )
88
themeNames << theme.internalName;
89
if ( !themeNames.contains( mSettings.iconTheme ) )
90
mSettings.iconTheme = TEXT_THEME;
91
mSettings.colorIncoming = interfaceGroup.readEntry( conf_colorIncoming, s.colorIncoming );
92
mSettings.colorOutgoing = interfaceGroup.readEntry( conf_colorOutgoing, s.colorOutgoing );
93
KColorScheme scheme(QPalette::Active, KColorScheme::View);
94
mSettings.colorDisabled = interfaceGroup.readEntry( conf_colorDisabled, scheme.foreground( KColorScheme::InactiveText ).color() );
95
mSettings.colorUnavailable = interfaceGroup.readEntry( conf_colorUnavailable, scheme.foreground( KColorScheme::InactiveText ).color() );
96
mSettings.customCommands = interfaceGroup.readEntry( conf_customCommands, s.customCommands );
97
mSettings.hideWhenDisconnected = interfaceGroup.readEntry( conf_hideWhenNotAvail, s.hideWhenDisconnected );
98
mSettings.hideWhenUnavailable = interfaceGroup.readEntry( conf_hideWhenNotExist, s.hideWhenUnavailable );
99
mSettings.activateStatistics = interfaceGroup.readEntry( conf_activateStatistics, s.activateStatistics );
100
mSettings.trafficThreshold = clamp<unsigned int>(interfaceGroup.readEntry( conf_trafficThreshold, s.trafficThreshold ), 0, 1000 );
101
mSettings.warnThreshold = clamp<double>(interfaceGroup.readEntry( conf_billingWarnThresh, s.warnThreshold ), 0.0, 9999.0 );
102
mSettings.warnTotalTraffic = interfaceGroup.readEntry( conf_billingWarnRxTx, s.warnTotalTraffic );
104
// TODO: Some of the calendars are a bit buggy, so default to Gregorian for now
105
//mSettings.calendar = interfaceGroup.readEntry( conf_calendar, KGlobal::locale()->calendarType() );
106
mSettings.calendar = interfaceGroup.readEntry( conf_calendar, "gregorian" );
108
mSettings.customBilling = interfaceGroup.readEntry( conf_customBilling, s.customBilling );
110
KCalendarSystem* calendar = KCalendarSystem::create( mSettings.calendar );
111
QDate startDate = QDate::currentDate().addDays( 1 - calendar->day( QDate::currentDate() ) );
112
mSettings.billingStart = interfaceGroup.readEntry( conf_billingStart, startDate );
113
// No future start period
114
if ( mSettings.billingStart > QDate::currentDate() )
115
mSettings.billingStart = startDate;
116
mSettings.billingMonths = clamp<int>(interfaceGroup.readEntry( conf_billingMonths, s.billingMonths ), 1, 6 );
117
if ( mSettings.customBilling == false )
119
mSettings.billingMonths = 1;
120
mSettings.billingStart = mSettings.billingStart.addDays( 1 - calendar->day( mSettings.billingStart ) );
122
mSettings.commands.clear();
123
if ( mSettings.customCommands )
125
int numCommands = interfaceGroup.readEntry( conf_numCommands, s.numCommands );
126
for ( int i = 0; i < numCommands; i++ )
129
InterfaceCommand cmd;
130
entry = QString( "%1%2" ).arg( conf_runAsRoot ).arg( i + 1 );
131
cmd.runAsRoot = interfaceGroup.readEntry( entry, false );
132
entry = QString( "%1%2" ).arg( conf_command ).arg( i + 1 );
133
cmd.command = interfaceGroup.readEntry( entry );
134
entry = QString( "%1%2" ).arg( conf_menuText ).arg( i + 1 );
135
cmd.menuText = interfaceGroup.readEntry( entry );
136
mSettings.commands.append( cmd );
140
// This prevents needless regeneration of icon when first shown in tray
141
if ( mState == KNemoIface::UnknownState )
143
mState = mBackendData->status;
144
mPreviousState = mState;
146
mIcon.configChanged();
105
148
if ( mStatistics != 0 )
117
160
// user turned off statistics
118
161
stopStatistics();
165
void Interface::processUpdate()
167
mPreviousState = mState;
168
unsigned int trafficThreshold = mSettings.trafficThreshold;
169
mState = mBackendData->status;
171
mRxRate = mBackendData->incomingBytes / mGeneralData.pollInterval;
172
mTxRate = mBackendData->outgoingBytes / mGeneralData.pollInterval;
174
QString title = mSettings.alias;
175
if ( title.isEmpty() )
178
if ( mState & KNemoIface::Connected )
180
// the interface is connected, look for traffic
181
if ( ( mBackendData->rxPackets - mBackendData->prevRxPackets ) > trafficThreshold )
182
mState |= KNemoIface::RxTraffic;
183
if ( ( mBackendData->txPackets - mBackendData->prevTxPackets ) > trafficThreshold )
184
mState |= KNemoIface::TxTraffic;
188
mStatistics->addIncomingData( mBackendData->incomingBytes );
189
mStatistics->addOutgoingData( mBackendData->outgoingBytes );
194
if ( mPreviousState < KNemoIface::Connected )
196
QString connectedStr;
197
if ( mBackendData->isWireless )
198
connectedStr = i18n( "%1 is connected to %2", title, mBackendData->essid );
200
connectedStr = i18n( "%1 is connected", title );
201
if ( mPreviousState != KNemoIface::UnknownState )
202
KNotification::event( "connected", connectedStr );
205
else if ( mState & KNemoIface::Available )
207
if ( mPreviousState & KNemoIface::Connected )
209
KNotification::event( "disconnected", i18n( "%1 has disconnected", title ) );
210
if ( mType == KNemoIface::PPP )
211
backend->clearTraffic( mName );
214
else if ( mPreviousState < KNemoIface::Available )
216
if ( mPreviousState != KNemoIface::UnknownState )
217
KNotification::event( "available", i18n( "%1 is available", title ) );
220
else if ( mState == KNemoIface::Unavailable &&
221
mPreviousState > KNemoIface::Unavailable )
223
KNotification::event( "unavailable", i18n( "%1 is unavailable", title ) );
224
if ( mType == KNemoIface::PPP )
225
backend->clearTraffic( mName );
229
if ( mPreviousState != mState )
230
mIcon.updateTrayStatus();
232
if ( mPlotterDialog )
233
mPlotterDialog->updatePlotter( mRxRate / 1024.0, mTxRate / 1024.0 );
235
mIcon.updateToolTip();
121
236
if ( mStatusDialog )
123
mStatusDialog->setStatisticsGroupEnabled( mSettings.activateStatistics );
127
void Interface::activateMonitor()
129
mMonitor.checkStatus( this );
132
void Interface::setStartTime( int )
237
mStatusDialog->updateDialog();
240
void Interface::resetUptime()
135
244
mUptimeString = "00:00:00";
136
if ( !mPlotterTimer->isActive() )
137
mPlotterTimer->start( 1000 );
140
void Interface::showStatusDialog()
249
void Interface::showStatusDialog( bool fromContextMenu )
142
251
// Toggle the status dialog.
143
252
// First click will show the status dialog, second will hide it.
144
253
if ( mStatusDialog == 0L )
146
255
mStatusDialog = new InterfaceStatusDialog( this );
147
connect( &mMonitor, SIGNAL( available( int ) ),
148
mStatusDialog, SLOT( enableNetworkGroups( int ) ) );
149
connect( &mMonitor, SIGNAL( notAvailable( int ) ),
150
mStatusDialog, SLOT( disableNetworkGroups( int ) ) );
151
connect( &mMonitor, SIGNAL( notExisting( int ) ),
152
mStatusDialog, SLOT( disableNetworkGroups( int ) ) );
153
256
if ( mStatistics != 0 )
155
258
connect( mStatistics, SIGNAL( currentEntryChanged() ),
156
259
mStatusDialog, SLOT( statisticsChanged() ) );
157
260
mStatusDialog->statisticsChanged();
159
activateOrHide( mStatusDialog, true );
163
// Toggle the status dialog.
164
activateOrHide( mStatusDialog );
264
activateOrHide( mStatusDialog, fromContextMenu );
168
void Interface::showSignalPlotter( bool wasMiddleButton )
267
void Interface::showSignalPlotter( bool fromContextMenu )
170
if ( wasMiddleButton )
172
// Toggle the signal plotter.
173
activateOrHide( mPlotterDialog );
177
// Called from the context menu, show the dialog.
178
activateOrHide( mPlotterDialog, true );
180
if ( mPlotterDialog->isVisible() && !mPlotterTimer->isActive() )
181
mPlotterTimer->start( 1000 );
269
// Toggle the signal plotter.
270
activateOrHide( mPlotterDialog, fromContextMenu );
184
273
void Interface::showStatisticsDialog()
194
283
connect( mStatistics, SIGNAL( dayStatisticsChanged() ),
195
284
mStatisticsDialog, SLOT( updateDays() ) );
196
connect( mStatistics, SIGNAL( monthStatisticsChanged() ),
197
mStatisticsDialog, SLOT( updateMonths() ) );
285
connect( mStatistics, SIGNAL( weekStatisticsChanged() ),
286
mStatisticsDialog, SLOT( updateWeeks() ) );
287
connect( mStatistics, SIGNAL( monthStatisticsChanged( bool ) ),
288
mStatisticsDialog, SLOT( updateMonths( bool ) ) );
198
289
connect( mStatistics, SIGNAL( yearStatisticsChanged() ),
199
290
mStatisticsDialog, SLOT( updateYears() ) );
200
291
connect( mStatistics, SIGNAL( currentEntryChanged() ),
201
292
mStatisticsDialog, SLOT( updateCurrentEntry() ) );
202
connect( mStatisticsDialog, SIGNAL( clearDailyStatisticsClicked() ),
203
mStatistics, SLOT( clearDayStatistics() ) );
204
connect( mStatisticsDialog, SIGNAL( clearMonthlyStatisticsClicked() ),
205
mStatistics, SLOT( clearMonthStatistics() ) );
206
connect( mStatisticsDialog, SIGNAL( clearYearlyStatisticsClicked() ),
207
mStatistics, SLOT( clearYearStatistics() ) );
293
connect( mStatisticsDialog, SIGNAL( clearStatistics() ),
294
mStatistics, SLOT( clearStatistics() ) );
209
296
/* We need to show the dialog before we update the stats. That way
210
297
* the viewport doesn't get messed up when we scroll to the most recent
212
299
mStatisticsDialog->show();
214
301
mStatisticsDialog->updateDays();
215
mStatisticsDialog->updateMonths();
302
mStatisticsDialog->updateWeeks();
303
mStatisticsDialog->updateMonths( false );
216
304
mStatisticsDialog->updateYears();
219
307
mStatisticsDialog->show();
222
void Interface::resetData( int state )
224
// For PPP interfaces we will reset all data to zero when the
225
// interface gets disconnected. If the driver also resets its data
226
// (like PPP seems to do) we will start from zero for every new
229
( state == NOT_AVAILABLE ||
230
state == NOT_EXISTING ) )
232
mData.prevTxBytes = mData.txBytes = 0;
233
mData.prevRxBytes = mData.rxBytes = 0;
234
mData.prevTxPackets = mData.txPackets = 0;
235
mData.prevRxPackets = mData.rxPackets = 0;
239
void Interface::updateDetails()
241
InterfaceData &data = getData();
243
mUptime += mGeneralData.pollInterval;
245
if ( data.available )
247
time_t upsecs = mUptime;
248
time_t updays = upsecs / 86400;
252
else if ( updays > 1 )
253
uptime = QString( "%1 days, " ).arg( updays );
255
upsecs -= 86400 * updays; // we only want the seconds of today
256
int hrs = upsecs / 3600;
257
int mins = ( upsecs - hrs * 3600 ) / 60;
258
int secs = upsecs - hrs * 3600 - mins * 60;
260
time.sprintf( "%02d:%02d:%02d", hrs, mins, secs );
263
mUptimeString = uptime;
265
mIcon.updateToolTip();
267
mStatusDialog->updateDialog();
270
void Interface::updatePlotter()
272
if ( mPlotterDialog )
274
double outgoingBytes = mData.outgoingBytes / 1024.0 / (double) mGeneralData.pollInterval;
275
double incomingBytes = mData.incomingBytes / 1024.0 / (double) mGeneralData.pollInterval;
276
mPlotterDialog->updatePlotter( incomingBytes, outgoingBytes );
310
void Interface::updateTime()
312
mRealSec += mGeneralData.pollInterval;
313
if ( mRealSec < 1.0 )
316
mUptime += trunc( mRealSec );
317
mRealSec -= trunc( mRealSec );
319
time_t updays = mUptime / 86400;
321
mUptimeString = i18np("1 day, ","%1 days, ",updays);
323
mUptime -= 86400 * updays; // we only want the seconds of today
324
int hrs = mUptime / 3600;
325
int mins = ( mUptime - hrs * 3600 ) / 60;
326
int secs = mUptime - hrs * 3600 - mins * 60;
328
time.sprintf( "%02d:%02d:%02d", hrs, mins, secs );
329
mUptimeString += time;
280
332
void Interface::startStatistics()
282
334
mStatistics = new InterfaceStatistics( this );
283
connect( &mMonitor, SIGNAL( incomingData( unsigned long ) ),
284
mStatistics, SLOT( addIncomingData( unsigned long ) ) );
285
connect( &mMonitor, SIGNAL( outgoingData( unsigned long ) ),
286
mStatistics, SLOT( addOutgoingData( unsigned long ) ) );
335
connect( mStatistics, SIGNAL( warnMonthlyTraffic( quint64 ) ),
336
this, SLOT( warnMonthlyTraffic( quint64 ) ) );
287
337
if ( mStatusDialog != 0 )
289
339
connect( mStatistics, SIGNAL( currentEntryChanged() ),
290
340
mStatusDialog, SLOT( statisticsChanged() ) );
291
341
mStatusDialog->statisticsChanged();
294
mStatistics->loadStatistics();
297
345
void Interface::stopStatistics()
299
if ( mStatisticsDialog != 0 )
301
// this will close an open statistics dialog
302
delete mStatisticsDialog;
303
mStatisticsDialog = 0;
347
// this will close an open statistics dialog
348
delete mStatisticsDialog;
349
mStatisticsDialog = 0;
306
351
mStatistics->saveStatistics();