2 * kPPP: A pppd front end for the KDE project
6 * Copyright (C) 1997 Bernd Johannes Wuebben
7 * wuebben@math.cornell.edu
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this program; if not, write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <QPaintEvent>
32 #include <QGridLayout>
33 #include <QCloseEvent>
35 #include <QHBoxLayout>
36 #include <QVBoxLayout>
37 #include <QApplication>
38 #include <kwindowsystem.h>
39 #include <kiconloader.h>
43 #include <q3groupbox.h>
44 #include <qlineedit.h>
46 #include <kpushbutton.h>
47 #include <KStandardGuiItem>
50 #include "pppstatdlg.h"
54 extern PPPData gpppdata
;
56 PPPStatsDlg::PPPStatsDlg(QWidget
*parent
, const char *name
, QWidget
*,
66 setWindowTitle(i18n("kppp Statistics"));
67 KWindowSystem::setIcons(winId(), qApp
->windowIcon().pixmap(IconSize(KIconLoader::Desktop
),IconSize(KIconLoader::Desktop
)), qApp
->windowIcon().pixmap(IconSize(KIconLoader::Small
),IconSize(KIconLoader::Small
)));
69 QVBoxLayout
*tl
= new QVBoxLayout(this);
71 QGridLayout
*l1
= new QGridLayout();
73 box
= new Q3GroupBox(i18n("Statistics"), this);
74 l1
->addWidget(box
, 0, 0, 4, 4);
75 l1
->addItem(new QSpacerItem(0, fontMetrics().lineSpacing() - 10), 0, 0);
76 l1
->setRowStretch(1, 1);
77 l1
->setColumnStretch(1, 1);
79 // inner part of the grid
80 QVBoxLayout
*l11
= new QVBoxLayout
;
81 l1
->addLayout(l11
, 1, 1);
83 // modem pixmap and IP labels
84 QHBoxLayout
*l111
= new QHBoxLayout
;
87 big_modem_both_pixmap
= UserIcon("modemboth");
88 big_modem_left_pixmap
= UserIcon("modemleft");
89 big_modem_right_pixmap
= UserIcon("modemright");
90 big_modem_none_pixmap
= UserIcon("modemnone");
92 pixmap_l
= new QLabel(this);
93 pixmap_l
->setMinimumSize(big_modem_both_pixmap
.size());
94 l111
->addWidget(pixmap_l
, 1);
95 pixmap_l
->setAlignment(Qt::AlignVCenter
|Qt::AlignLeft
);
97 QGridLayout
*l1112
= new QGridLayout();
98 l111
->addLayout(l1112
);
100 ip_address_label1
= new QLabel(this);
101 ip_address_label1
->setText(i18n("Local Addr:"));
103 ip_address_label2
= new IPLineEdit(this);
104 ip_address_label2
->setFocusPolicy(Qt::NoFocus
);
106 ip_address_label3
= new QLabel(this);
107 ip_address_label3
->setText(i18n("Remote Addr:"));
109 ip_address_label4
= new IPLineEdit(this);
110 ip_address_label4
->setFocusPolicy(Qt::NoFocus
);
112 l1112
->addWidget(ip_address_label1
, 0, 0);
113 l1112
->addWidget(ip_address_label2
, 0, 1);
114 l1112
->addWidget(ip_address_label3
, 1, 0);
115 l1112
->addWidget(ip_address_label4
, 1, 1);
117 // consumes space on bottom
118 l1112
->setRowStretch(2, 1);
120 QGridLayout
*l112
= new QGridLayout();
121 l11
->addLayout(l112
);
122 for(i
=0 ; i
< 5; i
++) {
123 labela1
[i
] = new QLabel(this);
125 labela2
[i
] = new QLabel(this);
126 labela2
[i
]->setFrameStyle(QFrame::WinPanel
| QFrame::Sunken
);
128 labelb1
[i
] = new QLabel(this);
130 labelb2
[i
] = new QLabel(this);
131 labelb2
[i
]->setFrameStyle(QFrame::WinPanel
| QFrame::Sunken
);
134 labela1
[0]->setText(i18n("bytes in"));
135 labelb1
[0]->setText(i18n("bytes out"));
137 labela1
[1]->setText(i18n("packets in"));
138 labelb1
[1]->setText(i18n("packets out"));
140 labela1
[2]->setText(i18n("vjcomp in"));
141 labelb1
[2]->setText(i18n("vjcomp out"));
143 labela1
[3]->setText(i18n("vjunc in"));
144 labelb1
[3]->setText(i18n("vjunc out"));
146 labela1
[4]->setText(i18n("vjerr"));
147 labelb1
[4]->setText(i18n("non-vj"));
149 for(i
= 0; i
< 5; i
++) {
150 labela2
[i
]->setText("888888888"); // TODO: resize automatically
151 labelb2
[i
]->setText("888888888");
152 labela2
[i
]->setAlignment(Qt::AlignRight
);
153 labelb2
[i
]->setAlignment(Qt::AlignRight
);
154 labela2
[i
]->setFixedSize(labela2
[i
]->sizeHint());
155 labelb2
[i
]->setFixedSize(labelb2
[i
]->sizeHint());
156 labela2
[i
]->setText("");
157 labelb2
[i
]->setText("");
160 l112
->addWidget(labela1
[i
], i
, 0);
161 l112
->addWidget(labela2
[i
], i
, 1);
162 l112
->addWidget(labelb1
[i
], i
, 2);
163 l112
->addWidget(labelb2
[i
], i
, 3);
166 l112
->setColumnStretch(1, 1);
167 l112
->setColumnStretch(3, 1);
170 QHBoxLayout
*l12
= new QHBoxLayout
;
174 if(gpppdata
.graphingEnabled()) {
177 gpppdata
.graphingOptions(dummy
, bg
, text
, in
, out
);
179 graph
= new QLabel(this);
180 graph
->setFrameStyle(QFrame::Box
| QFrame::Sunken
);
181 l1
->addWidget(graph
, 2, 1, 1, 2);
182 graph
->setMinimumWidth(300);
183 graph
->setFixedHeight(76+4);
186 palette
.setColor(graph
->backgroundRole(), bg
);
187 graph
->setPalette(palette
);
190 cancelbutton
= new KPushButton(KStandardGuiItem::close(),this);
191 cancelbutton
->setFocus();
192 connect(cancelbutton
, SIGNAL(clicked()), this,SLOT(cancel()));
193 cancelbutton
->setFixedHeight(cancelbutton
->sizeHint().height());
194 cancelbutton
->setMinimumWidth(qMax(cancelbutton
->sizeHint().width(), 70));
195 l12
->addWidget(cancelbutton
);
197 if(gpppdata
.graphingEnabled()) {
198 graphTimer
= new QTimer(this);
199 connect(graphTimer
, SIGNAL(timeout()), SLOT(updateGraph()));
202 setFixedSize(sizeHint());
204 connect(stats
, SIGNAL(statsChanged(int)), SLOT(paintIcon(int)));
206 // read window position from config file
208 gpppdata
.winPosStatWin(p_x
, p_y
);
213 PPPStatsDlg::~PPPStatsDlg() {
217 // save window position when window was closed
218 bool PPPStatsDlg::event(QEvent
*e
) {
219 if (e
->type() == QEvent::Hide
)
221 gpppdata
.setWinPosStatWin(x(), y());
225 return QWidget::event(e
);
228 void PPPStatsDlg::cancel() {
233 void PPPStatsDlg::take_stats() {
235 bin_last
= stats
->ibytes
;
236 bout_last
= stats
->obytes
;
238 for(int i
= 0; i
< MAX_GRAPH_WIDTH
; i
++) {
246 if(gpppdata
.graphingEnabled())
247 graphTimer
->start(GRAPH_UPDATE_TIME
);
251 void PPPStatsDlg::stop_stats() {
253 if(gpppdata
.graphingEnabled())
257 void PPPStatsDlg::paintGraph() {
258 // why draw that stuff if not visible?
262 QPixmap
pm(graph
->width() - 4, graph
->height() - 4);
264 pm
.fill(graph
->palette().color(graph
->backgroundRole()));
268 int idx
= ringIdx
- pm
.width() + 1;
270 idx
+= MAX_GRAPH_WIDTH
;
272 // find good scaling factor
274 pm
.height() - (int)((float)bin
[idx
]/max
* (pm
.height() - 8))-1;
276 pm
.height() - (int)((float)bout
[idx
]/max
* (pm
.height() - 8))-1;
280 for(x
= 1; x
< pm
.width(); x
++) {
283 h_in
= pm
.height() - (int)((float)bin
[idx
]/max
* (pm
.height() - 8))-1;
284 h_out
= pm
.height() - (int)((float)bout
[idx
]/max
* (pm
.height() - 8))-1;
288 p
.drawLine(x
-1, last_h_out
, x
, h_out
);
291 p
.drawLine(x
-1, last_h_in
, x
, h_in
);
296 idx
= (idx
+ 1) % MAX_GRAPH_WIDTH
;
300 int last_max
= bin
[last_idx
]>bout
[last_idx
] ? bin
[last_idx
] : bout
[last_idx
];
304 p
.setFont(QFont(KGlobalSettings::fixedFont().family(), 8));
307 QString s
= i18n("%1 (max. %2) kb/sec",
308 KGlobal::locale()->formatNumber((float)last_max
/ 1024.0, 1),
309 KGlobal::locale()->formatNumber((float)max
/ 1024.0, 1));
310 p
.drawText(0, 0, pm
.width(), 2*8, Qt::AlignRight
|Qt::AlignVCenter
, s
, &r
);
311 p
.drawLine(0, 8, r
.left() - 8, 8);
314 graph
->setPixmap(pm
);
317 void PPPStatsDlg::updateGraph() {
318 bin
[ringIdx
] = stats
->ibytes
- bin_last
;
319 bout
[ringIdx
] = stats
->obytes
- bout_last
;
320 if(bin
[ringIdx
] > max
)
321 max
= ((bin
[ringIdx
] / 1024) + 1) * 1024;
323 if(bout
[ringIdx
] > max
)
324 max
= ((bout
[ringIdx
] / 1024) + 1) * 1024;
326 bin_last
= stats
->ibytes
;
327 bout_last
= stats
->obytes
;
328 ringIdx
= (ringIdx
+ 1) % MAX_GRAPH_WIDTH
;
332 void PPPStatsDlg::paintIcon(int status
) {
333 const QPixmap
*pixmap
;
337 case PPPStats::BytesIn
:
338 pixmap
= &big_modem_left_pixmap
;
340 case PPPStats::BytesOut
:
341 pixmap
= &big_modem_right_pixmap
;
343 case PPPStats::BytesBoth
:
344 pixmap
= &big_modem_both_pixmap
;
346 case PPPStats::BytesNone
:
348 pixmap
= &big_modem_none_pixmap
;
352 pixmap_l
->setPixmap(*pixmap
);
358 void PPPStatsDlg::timeclick() {
360 switch(gpppdata
.VolAcctEnabled()) {
361 case 0: // no accounting
365 stats
->totalbytes
= gpppdata
.totalBytes() + stats
->ibytes
;
369 stats
->totalbytes
= gpppdata
.totalBytes() + stats
->obytes
;
373 stats
->totalbytes
= gpppdata
.totalBytes() + stats
->ibytes
+ stats
->obytes
;
379 void PPPStatsDlg::closeEvent(QCloseEvent
*) {
384 void PPPStatsDlg::update_data() {
387 ibytes_string
= KGlobal::locale()->formatNumber(stats
->ibytes
, 0);
388 ipackets_string
.setNum(stats
->ipackets
);
389 compressedin_string
.setNum(stats
->compressedin
);
390 uncompressedin_string
.setNum(stats
->uncompressedin
);
391 errorin_string
.setNum(stats
->errorin
);
392 obytes_string
= KGlobal::locale()->formatNumber(stats
->obytes
, 0);
393 opackets_string
.setNum(stats
->opackets
);
394 compressed_string
.setNum(stats
->compressed
);
395 packetsunc_string
.setNum(stats
->packetsunc
);
396 packetsoutunc_string
.setNum(stats
->packetsoutunc
);
398 labela2
[0]->setText(ibytes_string
);
399 labela2
[1]->setText(ipackets_string
);
400 labela2
[2]->setText(compressedin_string
);
401 labela2
[3]->setText(uncompressedin_string
);
402 labela2
[4]->setText(errorin_string
);
404 labelb2
[0]->setText(obytes_string
);
405 labelb2
[1]->setText(opackets_string
);
406 labelb2
[2]->setText(compressed_string
);
407 labelb2
[3]->setText(packetsunc_string
);
408 labelb2
[4]->setText(packetsoutunc_string
);
410 // if I don't resort to this trick it is imposible to
411 // copy/paste the ip out of the lineedits due to
412 // reset of cursor position on setText()
413 QString local_addr
= ( stats
->local_ip_address
.isEmpty() ?
414 i18n("unavailable") :
415 stats
->local_ip_address
);
417 if( ip_address_label2
->text() != local_addr
)
418 ip_address_label2
->setText(local_addr
);
420 QString remote_addr
= ( stats
->remote_ip_address
.isEmpty() ?
421 i18n("unavailable") :
422 stats
->remote_ip_address
);
424 if( ip_address_label4
->text() != remote_addr
)
425 ip_address_label4
->setText(remote_addr
);
429 void PPPStatsDlg::toCurrentDesktop() {
430 KWindowSystem::setOnDesktop(winId(), KWindowSystem::currentDesktop());
433 #include "pppstatdlg.moc"