Contact cannot be a null ptr, and it is accessed before anyway. Disscussed with kedge.
[kdenetwork.git] / kppp / conwindow.cpp
blob0bdc6994defe0d99222cc365e0b5b53be483121b
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
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 //Added by qt3to4:
26 #include <QLabel>
27 #include <QVBoxLayout>
28 #include <QHBoxLayout>
29 #include <QGridLayout>
30 #include <QEvent>
31 #include <QCloseEvent>
32 #include "conwindow.h"
33 #include "docking.h"
34 #include "pppdata.h"
35 #include "pppstats.h"
36 #include <klocale.h>
37 #include <kglobal.h>
39 extern PPPData gpppdata;
41 ConWindow::ConWindow(QWidget *parent, const char *name, QWidget *mainwidget,
42 PPPStats *st)
43 : QWidget(parent),
44 minutes(0),
45 seconds(0),
46 hours(0),
47 days(0),
48 tl1(0),
49 stats(st),
50 accountingEnabled(false),
51 volumeAccountingEnabled(false)
53 setObjectName(name);
54 info1 = new QLabel(i18n("Connected at:"), this);
55 info2 = new QLabel("", this);
57 timelabel1 = new QLabel(i18n("Time connected:"), this);
58 timelabel2 = new QLabel("000:00:00", this);
60 vollabel = new QLabel(i18n("Volume:"), this);
61 volinfo = new QLabel("", this);
63 // now the stuff for accounting
64 session_bill_l = new QLabel(i18n("Session bill:"), this);
65 session_bill = new QLabel("", this);
66 total_bill_l = new QLabel(i18n("Total bill:"), this);
67 total_bill = new QLabel("", this);
69 setWindowTitle("kppp");
71 cancelbutton = new QPushButton(this);
72 cancelbutton->setText(i18n("&Disconnect"));
73 connect(cancelbutton, SIGNAL(clicked()), mainwidget, SLOT(disconnect()));
75 statsbutton = new QPushButton(this);
76 statsbutton->setText(i18n("De&tails"));
77 statsbutton->setFocus();
78 connect(statsbutton, SIGNAL(clicked()), mainwidget, SLOT(showStats()));
80 clocktimer = new QTimer(this);
81 connect(clocktimer, SIGNAL(timeout()), SLOT(timeclick()));
83 // read window position from config file
84 int p_x, p_y;
85 gpppdata.winPosConWin(p_x, p_y);
86 setGeometry(p_x, p_y, 320, 110);
89 ConWindow::~ConWindow() {
90 stopClock();
93 // save window position when window was closed
94 bool ConWindow::event(QEvent *e) {
95 if (e->type() == QEvent::Hide)
97 gpppdata.setWinPosConWin(x(), y());
98 return true;
100 else
101 return QWidget::event(e);
104 QString ConWindow::prettyPrintVolume(unsigned int n) {
105 int idx = 0;
106 const QString quant[] = {i18n("Byte"), i18n("KiB"),
107 i18n("MiB"), i18n("GiB"), QString()};
109 float n1 = n;
110 while(n >= 1024 && !quant[idx].isNull()) {
111 idx++;
112 n /= 1024;
115 int i = idx;
116 while(i--)
117 n1 = n1 / 1024.0;
119 QString s = KGlobal::locale()->formatNumber( n1, idx==0 ? 0 : 1 );
120 s += ' ' + quant[idx];
121 return s;
124 void ConWindow::accounting(bool on) {
125 // cache accounting settings
126 accountingEnabled = on;
127 volumeAccountingEnabled = gpppdata.VolAcctEnabled();
129 // delete old layout
130 if(tl1 != 0)
131 delete tl1;
133 // add layout now
134 tl1 = new QVBoxLayout(this);
135 tl1->setSpacing(10);
136 tl1->setMargin(10);
137 tl1->addSpacing(5);
138 QHBoxLayout *tl = new QHBoxLayout;
139 tl1->addLayout(tl);
140 tl->addSpacing(20);
141 QGridLayout *l1;
143 int vol_lines = 0;
144 if(gpppdata.VolAcctEnabled())
145 vol_lines = 1;
147 l1 = new QGridLayout();
148 l1->setMargin( 5 );
149 tl->addLayout(l1);
150 l1->setColumnStretch(0, 0);
151 l1->setColumnStretch(1, 1);
153 info2->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
154 timelabel2->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
155 session_bill->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
156 total_bill->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
157 volinfo->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
158 // make sure that there's enough space for the bills
159 QString s1 = session_bill->text();
160 QString s2 = total_bill->text();
161 QString s3 = volinfo->text();
163 session_bill->setText("888888.88 XXX");
164 total_bill->setText("888888.88 XXX");
165 volinfo->setText("8888.8 MB");
166 session_bill->setFixedSize(session_bill->sizeHint());
167 total_bill->setFixedSize(total_bill->sizeHint());
168 volinfo->setFixedSize(volinfo->sizeHint());
169 session_bill->setText(s1);
170 total_bill->setText(s2);
171 volinfo->setText(s3);
173 l1->addWidget(info1, 0, 0);
174 l1->addWidget(info2, 0, 1);
175 l1->addWidget(timelabel1, 1, 0);
176 l1->addWidget(timelabel2, 1, 1);
177 if(accountingEnabled) {
178 session_bill_l->show();
179 session_bill->show();
180 total_bill_l->show();
181 total_bill->show();
182 l1->addWidget(session_bill_l, 2, 0);
183 l1->addWidget(session_bill, 2, 1);
184 l1->addWidget(total_bill_l, 3, 0);
185 l1->addWidget(total_bill, 3, 1);
187 if(volumeAccountingEnabled) {
188 vollabel->show();
189 volinfo->show();
190 l1->addWidget(vollabel, 4, 0);
191 l1->addWidget(volinfo, 4, 1);
192 } else {
193 vollabel->hide();
194 volinfo->hide();
197 } else {
198 session_bill_l->hide();
199 session_bill->hide();
200 total_bill_l->hide();
201 total_bill->hide();
203 if(volumeAccountingEnabled) {
204 vollabel->show();
205 volinfo->show();
206 l1->addWidget(vollabel, 2, 0);
207 l1->addWidget(volinfo, 2, 1);
208 } else {
209 vollabel->hide();
210 volinfo->hide();
214 tl->addSpacing(10);
215 QVBoxLayout *l2 = new QVBoxLayout();
216 l2->setSpacing( 5 );
217 tl->addLayout(l2);
218 l2->addStretch(1);
219 l2->addWidget(statsbutton);
220 l2->addWidget(cancelbutton);
222 l2->addStretch(1);
224 tl1->addSpacing(5);
226 setFixedSize(sizeHint());
228 do not overwrite position read from config
229 // If this gets re-enabled, fix it for Xinerama before committing to CVS.
230 setGeometry((QApplication::desktop()->width() - width()) / 2,
231 (QApplication::desktop()->height() - height())/2,
232 width(),
233 height());
238 void ConWindow::dock() {
239 DockWidget::dock_widget->show();
240 hide();
244 void ConWindow::startClock() {
245 minutes = 0;
246 seconds = 0;
247 hours = 0;
248 QString title ;
250 title = gpppdata.accname();
252 if(gpppdata.get_show_clock_on_caption()){
253 title += " 00:00" ;
255 setWindowTitle(title);
257 timelabel2->setText("00:00:00");
258 clocktimer->start(1000);
262 void ConWindow::setConnectionSpeed(const QString &speed) {
263 info2->setText(speed);
267 void ConWindow::stopClock() {
268 clocktimer->stop();
272 void ConWindow::timeclick() {
273 QString tooltip = i18n("Connection: %1\n"
274 "Connected at: %2\n"
275 "Time connected: %3",
276 gpppdata.accname(), info2->text(),
277 time_string2);
279 if(accountingEnabled)
280 tooltip += i18n("\nSession Bill: %1\nTotal Bill: %2",
281 session_bill->text(), total_bill->text());
282 // volume accounting
283 if(volumeAccountingEnabled) {
285 volinfo->setEnabled(true);
286 int bytes = gpppdata.totalBytes();
287 volinfo->setText(prettyPrintVolume(bytes));
290 seconds++;
292 if(seconds >= 60 ) {
293 minutes ++;
294 seconds = 0;
297 if (minutes >= 60){
298 minutes = 0;
299 hours ++;
302 if( hours >= 24){
303 days ++;
304 hours = 0;
307 time_string.sprintf("%02d:%02d",hours,minutes);
308 time_string2 = "";
309 if (days)
310 time_string2.sprintf("%d d %02d:%02d:%02d",
311 days,hours,minutes,seconds);
313 else
314 time_string2.sprintf("%02d:%02d:%02d",hours,minutes,seconds);
316 caption_string = gpppdata.accname();
317 caption_string += ' ';
318 caption_string += time_string;
321 timelabel2->setText(time_string2);
323 if(gpppdata.get_show_clock_on_caption() && (seconds == 1)){
324 // we update the Caption only once per minute not every second
325 // otherwise I get a flickering icon
326 setWindowTitle(caption_string);
329 DockWidget::dock_widget->setToolTip( tooltip);
333 void ConWindow::closeEvent( QCloseEvent *e ){
334 // we don't want to lose the
335 // conwindow since this is our last connection kppp.
336 // if we lost it we could only kill the program by hand to get on with life.
337 e->ignore();
339 if(gpppdata.get_dock_into_panel())
340 dock();
344 void ConWindow::slotAccounting(const QString &total, const QString &session) {
345 total_bill->setText(total);
346 session_bill->setText(session);
349 #include "conwindow.moc"