1/*
2 * kPPP: A pppd front end for the KDE project
3 *
4 * $Id$
5 *
6 * Copyright (C) 1997 Bernd Johannes Wuebben
7 * wuebben@math.cornell.edu
8 *
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.
13 *
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.
18 *
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.
22 */
23
24
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>
38
39extern PPPData gpppdata;
40
41ConWindow::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)
52{
53 setObjectName(name);
54 info1 = new QLabel(i18n("Connected at:"), this);
55 info2 = new QLabel("", this);
56
57 timelabel1 = new QLabel(i18n("Time connected:"), this);
58 timelabel2 = new QLabel("000:00:00", this);
59
60 vollabel = new QLabel(i18n("Volume:"), this);
61 volinfo = new QLabel("", this);
62
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);
68
69 setWindowTitle("kppp");
70
71 cancelbutton = new QPushButton(this);
72 cancelbutton->setText(i18n("&Disconnect"));
73 connect(cancelbutton, SIGNAL(clicked()), mainwidget, SLOT(disconnect()));
74
75 statsbutton = new QPushButton(this);
76 statsbutton->setText(i18n("De&tails"));
77 statsbutton->setFocus();
78 connect(statsbutton, SIGNAL(clicked()), mainwidget, SLOT(showStats()));
79
80 clocktimer = new QTimer(this);
81 connect(clocktimer, SIGNAL(timeout()), SLOT(timeclick()));
82
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);
87}
88
89ConWindow::~ConWindow() {
90 stopClock();
91}
92
93// save window position when window was closed
94bool ConWindow::event(QEvent *e) {
95 if (e->type() == QEvent::Hide)
96 {
97 gpppdata.setWinPosConWin(x(), y());
98 return true;
99 }
100 else
101 return QWidget::event(e);
102}
103
104QString ConWindow::prettyPrintVolume(unsigned int n) {
105 int idx = 0;
106 const QString quant[] = {i18n("Byte"), i18n("KiB"),
107 i18n("MiB"), i18n("GiB"), QString()};
108
109 float n1 = n;
110 while(n >= 1024 && !quant[idx].isNull()) {
111 idx++;
112 n /= 1024;
113 }
114
115 int i = idx;
116 while(i--)
117 n1 = n1 / 1024.0;
118
119 QString s = KGlobal::locale()->formatNumber( n1, idx==0 ? 0 : 1 );
120 s += ' ' + quant[idx];
121 return s;
122}
123
124void ConWindow::accounting(bool on) {
125 // cache accounting settings
126 accountingEnabled = on;
127 volumeAccountingEnabled = gpppdata.VolAcctEnabled();
128
129 // delete old layout
130 if(tl1 != 0)
131 delete tl1;
132
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;
142
143 int vol_lines = 0;
144 if(gpppdata.VolAcctEnabled())
145 vol_lines = 1;
146
147 l1 = new QGridLayout();
148 l1->setMargin( 5 );
149 tl->addLayout(l1);
150 l1->setColumnStretch(0, 0);
151 l1->setColumnStretch(1, 1);
152
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();
162
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);
172
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);
186
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();
195 }
196
197 } else {
198 session_bill_l->hide();
199 session_bill->hide();
200 total_bill_l->hide();
201 total_bill->hide();
202
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();
211 }
212 }
213
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);
221
222 l2->addStretch(1);
223
224 tl1->addSpacing(5);
225
226 setFixedSize(sizeHint());
227/*
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());
234*/
235}
236
237
238void ConWindow::dock() {
239 DockWidget::dock_widget->show();
240 hide();
241}
242
243
244void ConWindow::startClock() {
245 minutes = 0;
246 seconds = 0;
247 hours = 0;
248 QString title ;
249
250 title = gpppdata.accname();
251
252 if(gpppdata.get_show_clock_on_caption()){
253 title += " 00:00" ;
254 }
255 setWindowTitle(title);
256
257 timelabel2->setText("00:00:00");
258 clocktimer->start(1000);
259}
260
261
262void ConWindow::setConnectionSpeed(const QString &speed) {
263 info2->setText(speed);
264}
265
266
267void ConWindow::stopClock() {
268 clocktimer->stop();
269}
270
271
272void 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);
278
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) {
284
285 volinfo->setEnabled(true);
286 int bytes = gpppdata.totalBytes();
287 volinfo->setText(prettyPrintVolume(bytes));
288 }
289
290 seconds++;
291
292 if(seconds >= 60 ) {
293 minutes ++;
294 seconds = 0;
295 }
296
297 if (minutes >= 60){
298 minutes = 0;
299 hours ++;
300 }
301
302 if( hours >= 24){
303 days ++;
304 hours = 0;
305 }
306
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);
312
313 else
314 time_string2.sprintf("%02d:%02d:%02d",hours,minutes,seconds);
315
316 caption_string = gpppdata.accname();
317 caption_string += ' ';
318 caption_string += time_string;
319
320
321 timelabel2->setText(time_string2);
322
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);
327 }
328
329 DockWidget::dock_widget->setToolTip( tooltip);
330}
331
332
333void 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();
338
339 if(gpppdata.get_dock_into_panel())
340 dock();
341}
342
343
344void ConWindow::slotAccounting(const QString &total, const QString &session) {
345 total_bill->setText(total);
346 session_bill->setText(session);
347}
348
349#include "conwindow.moc"
350