Diffstat (limited to 'noncore/settings/networksettings/ppp/conwindow.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/conwindow.cpp | 341 |
1 files changed, 341 insertions, 0 deletions
diff --git a/noncore/settings/networksettings/ppp/conwindow.cpp b/noncore/settings/networksettings/ppp/conwindow.cpp new file mode 100644 index 0000000..d6b3fbe --- a/dev/null +++ b/noncore/settings/networksettings/ppp/conwindow.cpp | |||
@@ -0,0 +1,341 @@ | |||
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., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
22 | */ | ||
23 | |||
24 | #include <qtooltip.h> | ||
25 | #include <qdialog.h> | ||
26 | #include "conwindow.h" | ||
27 | //#include "docking.h" | ||
28 | #include "pppdata.h" | ||
29 | // #include "pppstats.h" | ||
30 | // #include <klocale.h> | ||
31 | #define i18n QObject::tr | ||
32 | // #include <kglobal.h> | ||
33 | |||
34 | extern PPPData gpppdata; | ||
35 | |||
36 | ConWindow::ConWindow(QWidget *parent, const char *name, QDialog *mainwidget ) | ||
37 | // PPPStats *st) | ||
38 | : QWidget(parent, name, 0), | ||
39 | minutes(0), | ||
40 | seconds(0), | ||
41 | hours(0), | ||
42 | days(0), | ||
43 | tl1(0), | ||
44 | // stats(st), | ||
45 | accountingEnabled(false), | ||
46 | volumeAccountingEnabled(false) | ||
47 | { | ||
48 | info1 = new QLabel(i18n("Connected at:"), this); | ||
49 | info2 = new QLabel("", this); | ||
50 | |||
51 | timelabel1 = new QLabel(i18n("Time connected:"), this); | ||
52 | timelabel2 = new QLabel("000:00:00", this); | ||
53 | |||
54 | vollabel = new QLabel(i18n("Volume:"), this); | ||
55 | volinfo = new QLabel("", this); | ||
56 | |||
57 | // now the stuff for accounting | ||
58 | session_bill_l = new QLabel(i18n("Session Bill:"), this); | ||
59 | session_bill = new QLabel("", this); | ||
60 | total_bill_l = new QLabel(i18n("Total Bill:"), this); | ||
61 | total_bill = new QLabel("", this); | ||
62 | |||
63 | this->setCaption("kppp"); | ||
64 | |||
65 | cancelbutton = new QPushButton(this); | ||
66 | cancelbutton->setText(i18n("Disconnect")); | ||
67 | connect(cancelbutton, SIGNAL(clicked()), mainwidget, SLOT(disconnect())); | ||
68 | |||
69 | // statsbutton = new QPushButton(this); | ||
70 | // statsbutton->setText(i18n("Details")); | ||
71 | // statsbutton->setFocus(); | ||
72 | // connect(statsbutton, SIGNAL(clicked()), mainwidget, SLOT(showStats())); | ||
73 | |||
74 | clocktimer = new QTimer(this); | ||
75 | connect(clocktimer, SIGNAL(timeout()), SLOT(timeclick())); | ||
76 | |||
77 | // read window position from config file | ||
78 | int p_x, p_y; | ||
79 | gpppdata.winPosConWin(p_x, p_y); | ||
80 | setGeometry(p_x, p_y, 320, 110); | ||
81 | } | ||
82 | |||
83 | ConWindow::~ConWindow() { | ||
84 | stopClock(); | ||
85 | } | ||
86 | |||
87 | // save window position when window was closed | ||
88 | bool ConWindow::event(QEvent *e) { | ||
89 | if (e->type() == QEvent::Hide) | ||
90 | { | ||
91 | gpppdata.setWinPosConWin(x(), y()); | ||
92 | return true; | ||
93 | } | ||
94 | else | ||
95 | return QWidget::event(e); | ||
96 | } | ||
97 | |||
98 | QString ConWindow::prettyPrintVolume(unsigned int n) { | ||
99 | int idx = 0; | ||
100 | const QString quant[] = {i18n("Byte"), i18n("KB"), | ||
101 | i18n("MB"), i18n("GB"), QString::null}; | ||
102 | |||
103 | float n1 = n; | ||
104 | while(n >= 1024 && quant[idx] != QString::null) { | ||
105 | idx++; | ||
106 | n /= 1024; | ||
107 | } | ||
108 | |||
109 | int i = idx; | ||
110 | while(i--) | ||
111 | n1 = n1 / 1024.0; | ||
112 | |||
113 | QString s = QString::number( n1, 'f', idx==0 ? 0 : 1 ); //KGlobal::locale()->formatNumber( n1, idx==0 ? 0 : 1 ); | ||
114 | s += " " + quant[idx]; | ||
115 | return s; | ||
116 | } | ||
117 | |||
118 | void ConWindow::accounting(bool on) { | ||
119 | // cache accounting settings | ||
120 | accountingEnabled = on; | ||
121 | volumeAccountingEnabled = gpppdata.VolAcctEnabled(); | ||
122 | |||
123 | // delete old layout | ||
124 | if(tl1 != 0) | ||
125 | delete tl1; | ||
126 | |||
127 | // add layout now | ||
128 | tl1 = new QVBoxLayout(this, 10, 10); | ||
129 | tl1->addSpacing(5); | ||
130 | QHBoxLayout *tl = new QHBoxLayout; | ||
131 | tl1->addLayout(tl); | ||
132 | tl->addSpacing(20); | ||
133 | QGridLayout *l1; | ||
134 | |||
135 | int vol_lines = 0; | ||
136 | if(gpppdata.VolAcctEnabled()) | ||
137 | vol_lines = 1; | ||
138 | |||
139 | if(accountingEnabled) | ||
140 | l1 = new QGridLayout(4 + vol_lines, 2, 5); | ||
141 | else | ||
142 | l1 = new QGridLayout(2 + vol_lines, 2, 5); | ||
143 | tl->addLayout(l1); | ||
144 | l1->setColStretch(0, 0); | ||
145 | l1->setColStretch(1, 1); | ||
146 | |||
147 | info2->setAlignment(AlignRight|AlignVCenter); | ||
148 | timelabel2->setAlignment(AlignRight|AlignVCenter); | ||
149 | session_bill->setAlignment(AlignRight|AlignVCenter); | ||
150 | total_bill->setAlignment(AlignRight|AlignVCenter); | ||
151 | volinfo->setAlignment(AlignRight|AlignVCenter); | ||
152 | // make sure that there's enough space for the bills | ||
153 | QString s1 = session_bill->text(); | ||
154 | QString s2 = total_bill->text(); | ||
155 | QString s3 = volinfo->text(); | ||
156 | |||
157 | session_bill->setText("888888.88 XXX"); | ||
158 | total_bill->setText("888888.88 XXX"); | ||
159 | volinfo->setText("8888.8 MB"); | ||
160 | session_bill->setFixedSize(session_bill->sizeHint()); | ||
161 | total_bill->setFixedSize(total_bill->sizeHint()); | ||
162 | volinfo->setFixedSize(volinfo->sizeHint()); | ||
163 | session_bill->setText(s1); | ||
164 | total_bill->setText(s2); | ||
165 | volinfo->setText(s3); | ||
166 | |||
167 | l1->addWidget(info1, 0, 0); | ||
168 | l1->addWidget(info2, 0, 1); | ||
169 | l1->addWidget(timelabel1, 1, 0); | ||
170 | l1->addWidget(timelabel2, 1, 1); | ||
171 | if(accountingEnabled) { | ||
172 | session_bill_l->show(); | ||
173 | session_bill->show(); | ||
174 | total_bill_l->show(); | ||
175 | total_bill->show(); | ||
176 | l1->addWidget(session_bill_l, 2, 0); | ||
177 | l1->addWidget(session_bill, 2, 1); | ||
178 | l1->addWidget(total_bill_l, 3, 0); | ||
179 | l1->addWidget(total_bill, 3, 1); | ||
180 | |||
181 | if(volumeAccountingEnabled) { | ||
182 | vollabel->show(); | ||
183 | volinfo->show(); | ||
184 | l1->addWidget(vollabel, 4, 0); | ||
185 | l1->addWidget(volinfo, 4, 1); | ||
186 | } else { | ||
187 | vollabel->hide(); | ||
188 | volinfo->hide(); | ||
189 | } | ||
190 | |||
191 | } else { | ||
192 | session_bill_l->hide(); | ||
193 | session_bill->hide(); | ||
194 | total_bill_l->hide(); | ||
195 | total_bill->hide(); | ||
196 | |||
197 | if(volumeAccountingEnabled) { | ||
198 | vollabel->show(); | ||
199 | volinfo->show(); | ||
200 | l1->addWidget(vollabel, 2, 0); | ||
201 | l1->addWidget(volinfo, 2, 1); | ||
202 | } else { | ||
203 | vollabel->hide(); | ||
204 | volinfo->hide(); | ||
205 | } | ||
206 | } | ||
207 | |||
208 | tl->addSpacing(10); | ||
209 | QVBoxLayout *l2 = new QVBoxLayout(5); | ||
210 | tl->addLayout(l2); | ||
211 | l2->addStretch(1); | ||
212 | // l2->addWidget(statsbutton); | ||
213 | l2->addWidget(cancelbutton); | ||
214 | |||
215 | l2->addStretch(1); | ||
216 | |||
217 | tl1->addSpacing(5); | ||
218 | |||
219 | setFixedSize(sizeHint()); | ||
220 | /* | ||
221 | do not overwrite position read from config | ||
222 | setGeometry((QApplication::desktop()->width() - width()) / 2, | ||
223 | (QApplication::desktop()->height() - height())/2, | ||
224 | width(), | ||
225 | height()); | ||
226 | */ | ||
227 | } | ||
228 | |||
229 | |||
230 | void ConWindow::dock() { | ||
231 | // DockWidget::dock_widget->show(); | ||
232 | this->hide(); | ||
233 | } | ||
234 | |||
235 | |||
236 | void ConWindow::startClock() { | ||
237 | minutes = 0; | ||
238 | seconds = 0; | ||
239 | hours = 0; | ||
240 | QString title ; | ||
241 | |||
242 | title = gpppdata.accname(); | ||
243 | |||
244 | if(gpppdata.get_show_clock_on_caption()){ | ||
245 | title += " 00:00" ; | ||
246 | } | ||
247 | this->setCaption(title); | ||
248 | |||
249 | timelabel2->setText("00:00:00"); | ||
250 | clocktimer->start(1000); | ||
251 | } | ||
252 | |||
253 | |||
254 | void ConWindow::setConnectionSpeed(const QString &speed) { | ||
255 | info2->setText(speed); | ||
256 | } | ||
257 | |||
258 | |||
259 | void ConWindow::stopClock() { | ||
260 | clocktimer->stop(); | ||
261 | } | ||
262 | |||
263 | |||
264 | void ConWindow::timeclick() { | ||
265 | QString tooltip = i18n("Connection: %1\n" | ||
266 | "Connected at: %2\n" | ||
267 | "Time connected: %3") | ||
268 | .arg(gpppdata.accname()).arg(info2->text()) | ||
269 | .arg(time_string2); | ||
270 | |||
271 | if(accountingEnabled) | ||
272 | tooltip += i18n("\nSession Bill: %1\nTotal Bill: %2") | ||
273 | .arg(session_bill->text()).arg(total_bill->text()); | ||
274 | // volume accounting | ||
275 | if(volumeAccountingEnabled) { | ||
276 | |||
277 | volinfo->setEnabled(TRUE); | ||
278 | int bytes = gpppdata.totalBytes(); | ||
279 | volinfo->setText(prettyPrintVolume(bytes)); | ||
280 | } | ||
281 | |||
282 | seconds++; | ||
283 | |||
284 | if(seconds >= 60 ) { | ||
285 | minutes ++; | ||
286 | seconds = 0; | ||
287 | } | ||
288 | |||
289 | if (minutes >= 60){ | ||
290 | minutes = 0; | ||
291 | hours ++; | ||
292 | } | ||
293 | |||
294 | if( hours >= 24){ | ||
295 | days ++; | ||
296 | hours = 0; | ||
297 | } | ||
298 | |||
299 | time_string.sprintf("%02d:%02d",hours,minutes); | ||
300 | time_string2 = ""; | ||
301 | if (days) | ||
302 | time_string2.sprintf("%d d %02d:%02d:%02d", | ||
303 | days,hours,minutes,seconds); | ||
304 | |||
305 | else | ||
306 | time_string2.sprintf("%02d:%02d:%02d",hours,minutes,seconds); | ||
307 | |||
308 | caption_string = gpppdata.accname(); | ||
309 | caption_string += " "; | ||
310 | caption_string += time_string; | ||
311 | |||
312 | |||
313 | timelabel2->setText(time_string2); | ||
314 | |||
315 | if(gpppdata.get_show_clock_on_caption() && (seconds == 1)){ | ||
316 | // we update the Caption only once per minute not every second | ||
317 | // otherwise I get a flickering icon | ||
318 | this->setCaption(caption_string); | ||
319 | } | ||
320 | |||
321 | // QToolTip::add(DockWidget::dock_widget, tooltip); | ||
322 | } | ||
323 | |||
324 | |||
325 | void ConWindow::closeEvent( QCloseEvent *e ){ | ||
326 | // we don't want to lose the | ||
327 | // conwindow since this is our last connection kppp. | ||
328 | // if we lost it we could only kill the program by hand to get on with life. | ||
329 | e->ignore(); | ||
330 | |||
331 | if(gpppdata.get_dock_into_panel()) | ||
332 | dock(); | ||
333 | } | ||
334 | |||
335 | |||
336 | void ConWindow::slotAccounting(QString total, QString session) { | ||
337 | total_bill->setText(total); | ||
338 | session_bill->setText(session); | ||
339 | } | ||
340 | |||
341 | |||