* bump version number to 2.0 RC 1
[kdenetwork.git] / kppp / pppstatdlg.cpp
blobf4e2819c8bf3c0b1bcb7ae1243edc9043716bd9a
1 /*
2 * kPPP: A pppd front end for the KDE project
4 * $Id$
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.
26 #include <qlayout.h>
27 #include <qpainter.h>
28 #include <kguiitem.h>
29 //Added by qt3to4:
30 #include <QPixmap>
31 #include <QPaintEvent>
32 #include <QGridLayout>
33 #include <QCloseEvent>
34 #include <QEvent>
35 #include <QHBoxLayout>
36 #include <QVBoxLayout>
37 #include <QApplication>
38 #include <kwindowsystem.h>
39 #include <kiconloader.h>
40 #include <klocale.h>
41 #include <qlabel.h>
42 #include <q3frame.h>
43 #include <q3groupbox.h>
44 #include <qlineedit.h>
45 #include <qtimer.h>
46 #include <kpushbutton.h>
47 #include <KStandardGuiItem>
49 #include "pppdata.h"
50 #include "pppstatdlg.h"
51 #include "iplined.h"
52 #include "pppstats.h"
54 extern PPPData gpppdata;
56 PPPStatsDlg::PPPStatsDlg(QWidget *parent, const char *name, QWidget *,
57 PPPStats *st)
58 : QWidget(parent),
59 stats(st)
61 int i;
62 max = 1024;
64 setObjectName(name);
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);
70 tl->setSpacing(10);
71 QGridLayout *l1 = new QGridLayout();
72 tl->addLayout(l1, 1);
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;
85 l11->addLayout(l111);
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("");
159 // add to layout
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);
169 tl->addSpacing(5);
170 QHBoxLayout *l12 = new QHBoxLayout;
171 tl->addLayout(l12);
172 l12->addStretch(1);
174 if(gpppdata.graphingEnabled()) {
175 bool dummy;
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);
185 QPalette palette;
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
207 int p_x, p_y;
208 gpppdata.winPosStatWin(p_x, p_y);
209 move(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());
222 return true;
224 else
225 return QWidget::event(e);
228 void PPPStatsDlg::cancel() {
229 hide();
233 void PPPStatsDlg::take_stats() {
234 stats->initStats();
235 bin_last = stats->ibytes;
236 bout_last = stats->obytes;
237 ringIdx = 0;
238 for(int i = 0; i < MAX_GRAPH_WIDTH; i++) {
239 bin[i] = -1;
240 bout[i] = -1;
243 update_data();
245 stats->start();
246 if(gpppdata.graphingEnabled())
247 graphTimer->start(GRAPH_UPDATE_TIME);
251 void PPPStatsDlg::stop_stats() {
252 stats->stop();
253 if(gpppdata.graphingEnabled())
254 graphTimer->stop();
257 void PPPStatsDlg::paintGraph() {
258 // why draw that stuff if not visible?
259 if(!isVisible())
260 return;
262 QPixmap pm(graph->width() - 4, graph->height() - 4);
263 QPainter p;
264 pm.fill(graph->palette().color(graph->backgroundRole()));
265 p.begin(&pm);
267 int x;
268 int idx = ringIdx - pm.width() + 1;
269 if(idx < 0)
270 idx += MAX_GRAPH_WIDTH;
272 // find good scaling factor
273 int last_h_in =
274 pm.height() - (int)((float)bin[idx]/max * (pm.height() - 8))-1;
275 int last_h_out =
276 pm.height() - (int)((float)bout[idx]/max * (pm.height() - 8))-1;
278 // plot data
279 int last_idx = 0;
280 for(x = 1; x < pm.width(); x++) {
281 int h_in, h_out;
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;
286 p.setPen(out);
287 if(bout[idx]!=-1)
288 p.drawLine(x-1, last_h_out, x, h_out);
289 p.setPen(in);
290 if(bin[idx]!=-1)
291 p.drawLine(x-1, last_h_in, x, h_in);
292 last_h_in = h_in;
293 last_h_out = h_out;
295 last_idx = idx;
296 idx = (idx + 1) % MAX_GRAPH_WIDTH;
299 // take last value
300 int last_max = bin[last_idx]>bout[last_idx] ? bin[last_idx] : bout[last_idx];
302 // plot scale line
303 p.setPen(text);
304 p.setFont(QFont(KGlobalSettings::fixedFont().family(), 8));
306 QRect r;
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);
313 p.end();
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;
329 paintGraph();
332 void PPPStatsDlg::paintIcon(int status) {
333 const QPixmap *pixmap;
335 switch(status)
337 case PPPStats::BytesIn:
338 pixmap = &big_modem_left_pixmap;
339 break;
340 case PPPStats::BytesOut:
341 pixmap = &big_modem_right_pixmap;
342 break;
343 case PPPStats::BytesBoth:
344 pixmap = &big_modem_both_pixmap;
345 break;
346 case PPPStats::BytesNone:
347 default:
348 pixmap = &big_modem_none_pixmap;
349 break;
352 pixmap_l->setPixmap(*pixmap);
354 update_data();
358 void PPPStatsDlg::timeclick() {
359 // volume accounting
360 switch(gpppdata.VolAcctEnabled()) {
361 case 0: // no accounting
362 break;
364 case 1: // bytes in
365 stats->totalbytes = gpppdata.totalBytes() + stats->ibytes;
366 break;
368 case 2:
369 stats->totalbytes = gpppdata.totalBytes() + stats->obytes;
370 break;
372 case 3:
373 stats->totalbytes = gpppdata.totalBytes() + stats->ibytes + stats->obytes;
374 break;
379 void PPPStatsDlg::closeEvent(QCloseEvent *) {
380 emit cancel();
384 void PPPStatsDlg::update_data() {
385 timeclick();
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"