Diffstat (limited to 'noncore/settings/networksettings/ppp/accounts.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/accounts.cpp | 491 |
1 files changed, 491 insertions, 0 deletions
diff --git a/noncore/settings/networksettings/ppp/accounts.cpp b/noncore/settings/networksettings/ppp/accounts.cpp new file mode 100644 index 0000000..d902517 --- a/dev/null +++ b/noncore/settings/networksettings/ppp/accounts.cpp | |||
@@ -0,0 +1,491 @@ | |||
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 | * based on EzPPP: | ||
10 | * Copyright (C) 1997 Jay Painter | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU Library General Public | ||
14 | * License as published by the Free Software Foundation; either | ||
15 | * version 2 of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This program is distributed in the hope that it will be useful, | ||
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
20 | * Library General Public License for more details. | ||
21 | * | ||
22 | * You should have received a copy of the GNU Library General Public | ||
23 | * License along with this program; if not, write to the Free | ||
24 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
25 | */ | ||
26 | |||
27 | #include <qdir.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <qlayout.h> | ||
30 | #include <qtabwidget.h> | ||
31 | #include <qtabdialog.h> | ||
32 | #include <qwhatsthis.h> | ||
33 | #include <qmessagebox.h> | ||
34 | |||
35 | #include <qapplication.h> | ||
36 | #include <qbuttongroup.h> | ||
37 | #include <qmessagebox.h> | ||
38 | //#include <klocale.h> | ||
39 | #define i18n QObject::tr | ||
40 | //#include <kglobal.h> | ||
41 | //#include <kwin.h> | ||
42 | //#include <kdialogbase.h> | ||
43 | #include <qvgroupbox.h> | ||
44 | |||
45 | #include "pppdata.h" | ||
46 | #include "accounts.h" | ||
47 | //#include "accounting.h" | ||
48 | //#include "providerdb.h" | ||
49 | #include "edit.h" | ||
50 | |||
51 | void parseargs(char* buf, char** args); | ||
52 | |||
53 | AccountWidget::AccountWidget( QWidget *parent, const char *name ) | ||
54 | : QWidget( parent, name ) | ||
55 | { | ||
56 | int min = 0; | ||
57 | QVBoxLayout *l1 = new QVBoxLayout(this, 10, 10); | ||
58 | |||
59 | // add a hbox | ||
60 | // QHBoxLayout *l11 = new QHBoxLayout; | ||
61 | // l1->addLayout(l11); | ||
62 | |||
63 | accountlist_l = new QListBox(this); | ||
64 | accountlist_l->setMinimumSize(160, 128); | ||
65 | connect(accountlist_l, SIGNAL(highlighted(int)), | ||
66 | this, SLOT(slotListBoxSelect(int))); | ||
67 | connect(accountlist_l, SIGNAL(selected(int)), | ||
68 | this, SLOT(editaccount())); | ||
69 | l1->addWidget(accountlist_l, 10); | ||
70 | |||
71 | // QVBoxLayout *l111 = new QVBoxLayout(this); | ||
72 | // l11->addLayout(l111, 1); | ||
73 | edit_b = new QPushButton(i18n("&Edit..."), this); | ||
74 | connect(edit_b, SIGNAL(clicked()), SLOT(editaccount())); | ||
75 | QWhatsThis::add(edit_b, i18n("Allows you to modify the selected account")); | ||
76 | |||
77 | min = edit_b->sizeHint().width(); | ||
78 | min = QMAX(70,min); | ||
79 | edit_b->setMinimumWidth(min); | ||
80 | |||
81 | l1->addWidget(edit_b); | ||
82 | |||
83 | new_b = new QPushButton(i18n("&New..."), this); | ||
84 | connect(new_b, SIGNAL(clicked()), SLOT(newaccount())); | ||
85 | l1->addWidget(new_b); | ||
86 | QWhatsThis::add(new_b, i18n("Create a new dialup connection\n" | ||
87 | "to the Internet")); | ||
88 | |||
89 | copy_b = new QPushButton(i18n("Co&py"), this); | ||
90 | connect(copy_b, SIGNAL(clicked()), SLOT(copyaccount())); | ||
91 | l1->addWidget(copy_b); | ||
92 | QWhatsThis::add(copy_b, | ||
93 | i18n("Makes a copy of the selected account. All\n" | ||
94 | "settings of the selected account are copied\n" | ||
95 | "to a new account, that you can modify to fit your\n" | ||
96 | "needs")); | ||
97 | |||
98 | delete_b = new QPushButton(i18n("De&lete"), this); | ||
99 | connect(delete_b, SIGNAL(clicked()), SLOT(deleteaccount())); | ||
100 | l1->addWidget(delete_b); | ||
101 | QWhatsThis::add(delete_b, | ||
102 | i18n("<p>Deletes the selected account\n\n" | ||
103 | "<font color=\"red\"><b>Use with care!</b></font>")); | ||
104 | |||
105 | QHBoxLayout *l12 = new QHBoxLayout; | ||
106 | l1->addStretch(1); | ||
107 | l1->addLayout(l12); | ||
108 | |||
109 | // QVBoxLayout *l121 = new QVBoxLayout; | ||
110 | // l12->addLayout(l121); | ||
111 | // l121->addStretch(1); | ||
112 | // costlabel = new QLabel(i18n("Phone costs:"), parent); | ||
113 | // costlabel->setEnabled(FALSE); | ||
114 | // l121->addWidget(costlabel); | ||
115 | |||
116 | // costedit = new QLineEdit(parent); | ||
117 | // costedit->setFocusPolicy(QWidget::NoFocus); | ||
118 | // costedit->setFixedHeight(costedit->sizeHint().height()); | ||
119 | // costedit->setEnabled(FALSE); | ||
120 | // l121->addWidget(costedit); | ||
121 | // l121->addStretch(1); | ||
122 | // QString tmp = i18n("<p>This shows the accumulated phone costs\n" | ||
123 | // "for the selected account.\n" | ||
124 | // "\n" | ||
125 | // "<b>Important</b>: If you have more than one \n" | ||
126 | // "account - beware, this is <b>NOT</b> the sum \n" | ||
127 | // "of the phone costs of all your accounts!"); | ||
128 | // QWhatsThis::add(costlabel, tmp); | ||
129 | // QWhatsThis::add(costedit, tmp); | ||
130 | |||
131 | // vollabel = new QLabel(i18n("Volume:"), parent); | ||
132 | // vollabel->setEnabled(FALSE); | ||
133 | // l121->addWidget(vollabel); | ||
134 | |||
135 | // voledit = new QLineEdit(parent,"voledit"); | ||
136 | // voledit->setFocusPolicy(QWidget::NoFocus); | ||
137 | // voledit->setFixedHeight(voledit->sizeHint().height()); | ||
138 | // voledit->setEnabled(FALSE); | ||
139 | // l121->addWidget(voledit); | ||
140 | // tmp = i18n("<p>This shows the number of bytes transferred\n" | ||
141 | // "for the selected account (not for all of your\n" | ||
142 | // "accounts. You can select what to display in\n" | ||
143 | // "the accounting dialog.\n" | ||
144 | // "\n" | ||
145 | // "<a href=\"#volaccounting\">More on volume accounting</a>"); | ||
146 | |||
147 | // QWhatsThis::add(vollabel,tmp); | ||
148 | // QWhatsThis::add(voledit, tmp); | ||
149 | |||
150 | // QVBoxLayout *l122 = new QVBoxLayout; | ||
151 | // l12->addStretch(1); | ||
152 | // l12->addLayout(l122); | ||
153 | |||
154 | // l122->addStretch(1); | ||
155 | // reset = new QPushButton(i18n("&Reset..."), parent); | ||
156 | // reset->setEnabled(FALSE); | ||
157 | // connect(reset, SIGNAL(clicked()), | ||
158 | // this, SLOT(resetClicked())); | ||
159 | // l122->addWidget(reset); | ||
160 | |||
161 | // log = new QPushButton(i18n("&View Logs"), this); | ||
162 | // connect(log, SIGNAL(clicked()), | ||
163 | // this, SLOT(viewLogClicked())); | ||
164 | // l122->addWidget(log); | ||
165 | // l122->addStretch(1); | ||
166 | |||
167 | //load up account list from gppdata to the list box | ||
168 | if(gpppdata.count() > 0) { | ||
169 | for(int i=0; i <= gpppdata.count()-1; i++) { | ||
170 | gpppdata.setAccountbyIndex(i); | ||
171 | accountlist_l->insertItem(gpppdata.accname()); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | slotListBoxSelect(accountlist_l->currentItem()); | ||
176 | |||
177 | l1->activate(); | ||
178 | } | ||
179 | |||
180 | |||
181 | |||
182 | void AccountWidget::slotListBoxSelect(int idx) { | ||
183 | delete_b->setEnabled((bool)(idx != -1)); | ||
184 | edit_b->setEnabled((bool)(idx != -1)); | ||
185 | copy_b->setEnabled((bool)(idx != -1)); | ||
186 | if(idx!=-1) { | ||
187 | QString account = gpppdata.accname(); | ||
188 | gpppdata.setAccountbyIndex(accountlist_l->currentItem()); | ||
189 | // reset->setEnabled(TRUE); | ||
190 | // costlabel->setEnabled(TRUE); | ||
191 | // costedit->setEnabled(TRUE); | ||
192 | // costedit->setText(AccountingBase::getCosts(accountlist_l->text(accountlist_l->currentItem()))); | ||
193 | |||
194 | // vollabel->setEnabled(TRUE); | ||
195 | // voledit->setEnabled(TRUE); | ||
196 | int bytes = gpppdata.totalBytes(); | ||
197 | // voledit->setText(prettyPrintVolume(bytes)); | ||
198 | gpppdata.setAccount(account); | ||
199 | } else{ | ||
200 | // reset->setEnabled(FALSE); | ||
201 | // costlabel->setEnabled(FALSE); | ||
202 | // costedit->setText(""); | ||
203 | // costedit->setEnabled(FALSE); | ||
204 | // vollabel->setEnabled(FALSE); | ||
205 | // voledit->setText(""); | ||
206 | // voledit->setEnabled(FALSE); | ||
207 | } | ||
208 | } | ||
209 | |||
210 | |||
211 | // void AccountWidget::viewLogClicked(){ | ||
212 | |||
213 | // QApplication::flushX(); | ||
214 | // if(fork() == 0) { | ||
215 | // setgid(getgid()); | ||
216 | // setuid(getuid()); | ||
217 | // system("kppplogview -kppp"); | ||
218 | // _exit(0); | ||
219 | // } | ||
220 | // } | ||
221 | |||
222 | |||
223 | // void AccountWidget::resetClicked(){ | ||
224 | // if(accountlist_l->currentItem() == -1) | ||
225 | // return; | ||
226 | |||
227 | // // QueryReset dlg(this); | ||
228 | // // int what = dlg.exec(); | ||
229 | |||
230 | // // if(what && QueryReset::COSTS) { | ||
231 | // // emit resetCosts(accountlist_l->text(accountlist_l->currentItem())); | ||
232 | // // costedit->setText("0"); | ||
233 | // // } | ||
234 | |||
235 | // // if(what && QueryReset::VOLUME) { | ||
236 | // // emit resetVolume(accountlist_l->text(accountlist_l->currentItem())); | ||
237 | // // voledit->setText(prettyPrintVolume(0)); | ||
238 | // // } | ||
239 | // } | ||
240 | |||
241 | |||
242 | void AccountWidget::editaccount() { | ||
243 | gpppdata.setAccount(accountlist_l->text(accountlist_l->currentItem())); | ||
244 | |||
245 | int result = doTab(); | ||
246 | |||
247 | if(result == QDialog::Accepted) { | ||
248 | accountlist_l->changeItem(gpppdata.accname(),accountlist_l->currentItem()); | ||
249 | // emit resetaccounts(); | ||
250 | gpppdata.save(); | ||
251 | } | ||
252 | } | ||
253 | |||
254 | |||
255 | void AccountWidget::newaccount() { | ||
256 | if(accountlist_l->count() == MAX_ACCOUNTS) { | ||
257 | QMessageBox::information(this, "sorry", i18n("Maximum number of accounts reached.")); | ||
258 | return; | ||
259 | } | ||
260 | |||
261 | int result; | ||
262 | // int query = QMessageBox::information(this, | ||
263 | // i18n("Do you want to use the wizard to create the new account or the " | ||
264 | // "standard, dialog-based setup?\n" | ||
265 | // "The wizard is easier and sufficient in most cases. If you need " | ||
266 | // "very special settings, you might want to try the standard, " | ||
267 | // "dialog-based setup."), | ||
268 | // i18n("Create New Account"), | ||
269 | // i18n("Wizard"), i18n("Dialog Setup"), i18n("Cancel")); | ||
270 | |||
271 | // switch(query) { | ||
272 | // case QMessageBox::Yes: | ||
273 | // { | ||
274 | // if (gpppdata.newaccount() == -1) | ||
275 | // return; | ||
276 | // // ProviderDB pdb(this); | ||
277 | // // result = pdb.exec(); | ||
278 | // break; | ||
279 | // } | ||
280 | // case QMessageBox::No: | ||
281 | if (gpppdata.newaccount() == -1){ | ||
282 | qDebug("gpppdata.newaccount() == -1"); | ||
283 | return; | ||
284 | } | ||
285 | result = doTab(); | ||
286 | // break; | ||
287 | // default: | ||
288 | // return; | ||
289 | // } | ||
290 | |||
291 | if(result == QDialog::Accepted) { | ||
292 | accountlist_l->insertItem(gpppdata.accname()); | ||
293 | accountlist_l->setSelected(accountlist_l->findItem(gpppdata.accname()), | ||
294 | true); | ||
295 | // emit resetaccounts(); | ||
296 | gpppdata.save(); | ||
297 | } else | ||
298 | gpppdata.deleteAccount(); | ||
299 | } | ||
300 | |||
301 | |||
302 | void AccountWidget::copyaccount() { | ||
303 | if(accountlist_l->count() == MAX_ACCOUNTS) { | ||
304 | QMessageBox::information(this, "sorry", i18n("Maximum number of accounts reached.")); | ||
305 | return; | ||
306 | } | ||
307 | |||
308 | if(accountlist_l->currentItem()<0) { | ||
309 | QMessageBox::information(this, "sorry", i18n("No account selected.")); | ||
310 | return; | ||
311 | } | ||
312 | |||
313 | gpppdata.copyaccount(accountlist_l->currentItem()); | ||
314 | |||
315 | accountlist_l->insertItem(gpppdata.accname()); | ||
316 | // emit resetaccounts(); | ||
317 | gpppdata.save(); | ||
318 | } | ||
319 | |||
320 | |||
321 | void AccountWidget::deleteaccount() { | ||
322 | |||
323 | QString s = i18n("Are you sure you want to delete\nthe account \"%1\"?") | ||
324 | .arg(accountlist_l->text(accountlist_l->currentItem())); | ||
325 | |||
326 | if(QMessageBox::warning(this, s, i18n("Confirm")) != QMessageBox::Yes) | ||
327 | return; | ||
328 | |||
329 | if(gpppdata.deleteAccount(accountlist_l->text(accountlist_l->currentItem()))) | ||
330 | accountlist_l->removeItem(accountlist_l->currentItem()); | ||
331 | |||
332 | emit resetaccounts(); | ||
333 | gpppdata.save(); | ||
334 | |||
335 | slotListBoxSelect(accountlist_l->currentItem()); | ||
336 | |||
337 | } | ||
338 | |||
339 | |||
340 | int AccountWidget::doTab(){ | ||
341 | QDialog *dlg = new QDialog( this ); | ||
342 | tabWindow = new QTabWidget( dlg ); | ||
343 | // tabWindow = new KDialogBase( KDialogBase::Tabbed, QString::null, | ||
344 | // KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, | ||
345 | // 0, 0, true); | ||
346 | // KWin::setIcons(tabWindow->winId(), kapp->icon(), kapp->miniIcon()); | ||
347 | bool isnewaccount; | ||
348 | |||
349 | if(gpppdata.accname().isEmpty()) { | ||
350 | tabWindow->setCaption(i18n("New Account")); | ||
351 | isnewaccount = true; | ||
352 | } else { | ||
353 | QString tit = i18n("Edit Account: "); | ||
354 | tit += gpppdata.accname(); | ||
355 | tabWindow->setCaption(tit); | ||
356 | isnewaccount = false; | ||
357 | } | ||
358 | |||
359 | dial_w = new DialWidget( tabWindow ); | ||
360 | tabWindow->addTab( dial_w, i18n("Dial") );//, i18n("Dial Setup")), isnewaccount); | ||
361 | ip_w = new IPWidget( tabWindow ); | ||
362 | tabWindow->addTab( ip_w, i18n("IP") );//, i18n("IP Setup")), isnewaccount); | ||
363 | gateway_w = new GatewayWidget( tabWindow ); | ||
364 | tabWindow->addTab( gateway_w, i18n("Gateway") );//, i18n("Gateway Setup")), isnewaccount); | ||
365 | dns_w = new DNSWidget( tabWindow ); | ||
366 | tabWindow->addTab( dns_w, i18n("DNS") );//, i18n("DNS Servers")), isnewaccount); | ||
367 | script_w = new ScriptWidget( tabWindow ); | ||
368 | tabWindow->addTab( script_w, i18n("Login Script") ); //, i18n("Edit Login Script")), isnewaccount); | ||
369 | ExecWidget *exec_w = new ExecWidget( tabWindow ); | ||
370 | tabWindow->addTab( exec_w, i18n("Execute") );//, i18n("Execute Programs")), isnewaccount); | ||
371 | // acct = new AccountingSelector( tabWindow, isnewaccount ); | ||
372 | // tabWindow->addTab( acct, i18n("Accounting")); | ||
373 | |||
374 | int result = 0; | ||
375 | bool ok = false; | ||
376 | while (!ok){ | ||
377 | |||
378 | result = dlg->exec(); | ||
379 | ok = true; | ||
380 | |||
381 | if(result == QDialog::Accepted) { | ||
382 | if (script_w->check()) { | ||
383 | if(dial_w->save()) { | ||
384 | ip_w->save(); | ||
385 | dns_w->save(); | ||
386 | gateway_w->save(); | ||
387 | script_w->save(); | ||
388 | exec_w->save(); | ||
389 | // acct->save(); | ||
390 | } else { | ||
391 | QMessageBox::critical(this, "error", i18n( "You must enter a unique\n" | ||
392 | "account name")); | ||
393 | ok = false; | ||
394 | } | ||
395 | } else { | ||
396 | QMessageBox::critical(this, "error", i18n("Login script has unbalanced " | ||
397 | "loop Start/End")); | ||
398 | ok = false; | ||
399 | } | ||
400 | } | ||
401 | } | ||
402 | |||
403 | delete tabWindow; | ||
404 | return result; | ||
405 | } | ||
406 | |||
407 | |||
408 | QString AccountWidget::prettyPrintVolume(unsigned int n) { | ||
409 | int idx = 0; | ||
410 | const QString quant[] = {i18n("Byte"), i18n("KB"), | ||
411 | i18n("MB"), i18n("GB"), QString::null}; | ||
412 | |||
413 | float n1 = n; | ||
414 | while(n >= 1024 && quant[idx] != QString::null) { | ||
415 | idx++; | ||
416 | n /= 1024; | ||
417 | } | ||
418 | |||
419 | int i = idx; | ||
420 | while(i--) | ||
421 | n1 = n1 / 1024.0; | ||
422 | |||
423 | QString s = QString::number( n1, 'f', idx==0 ? 0 : 1 ); | ||
424 | s += " " + quant[idx]; | ||
425 | return s; | ||
426 | } | ||
427 | |||
428 | |||
429 | ///////////////////////////////////////////////////////////////////////////// | ||
430 | // | ||
431 | // Queries the user what to reset: costs, volume or both | ||
432 | // | ||
433 | ///////////////////////////////////////////////////////////////////////////// | ||
434 | QueryReset::QueryReset(QWidget *parent) : QDialog(parent, 0, true) { | ||
435 | // KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon()); | ||
436 | setCaption(i18n("Reset Accounting")); | ||
437 | |||
438 | QVBoxLayout *tl = new QVBoxLayout(this, 10, 10); | ||
439 | QVGroupBox *f = new QVGroupBox(i18n("What to Reset"), this); | ||
440 | |||
441 | QVBoxLayout *l1 = new QVBoxLayout(this, 10, 10); | ||
442 | // costs = new QCheckBox(i18n("Reset the accumulated phone costs"), f); | ||
443 | // costs->setChecked(true); | ||
444 | // l1->addWidget(costs); | ||
445 | // QWhatsThis::add(costs, i18n("Check this to set the phone costs\n" | ||
446 | // "to zero. Typically you'll want to\n" | ||
447 | // "do this once a month.")); | ||
448 | |||
449 | // volume = new QCheckBox(i18n("Reset volume accounting"), f); | ||
450 | // volume->setChecked(true); | ||
451 | // l1->addWidget(volume); | ||
452 | // QWhatsThis::add(volume, i18n("Check this to set the volume accounting\n" | ||
453 | // "to zero. Typically you'll want to do this\n" | ||
454 | // "once a month.")); | ||
455 | |||
456 | l1->activate(); | ||
457 | |||
458 | // this activates the f-layout and sets minimumSize() | ||
459 | f->show(); | ||
460 | |||
461 | tl->addWidget(f); | ||
462 | |||
463 | QButtonGroup *bbox = new QButtonGroup(this); | ||
464 | // bbox->addStretch(1); | ||
465 | QPushButton *ok = new QPushButton( bbox, i18n("OK") ); | ||
466 | bbox->insert(ok); | ||
467 | ok->setDefault(true); | ||
468 | QPushButton *cancel = new QPushButton( bbox, i18n("Cancel") ); | ||
469 | bbox->insert(cancel); | ||
470 | |||
471 | connect(ok, SIGNAL(clicked()), | ||
472 | this, SLOT(accepted())); | ||
473 | connect(cancel, SIGNAL(clicked()), | ||
474 | this, SLOT(reject())); | ||
475 | |||
476 | bbox->layout(); | ||
477 | tl->addWidget(bbox); | ||
478 | |||
479 | // TODO: activate if KGroupBox is fixed | ||
480 | // setFixedSize(sizeHint()); | ||
481 | } | ||
482 | |||
483 | |||
484 | void QueryReset::accepted() { | ||
485 | int result = costs->isChecked() ? COSTS : 0; | ||
486 | result += volume->isChecked() ? VOLUME : 0; | ||
487 | |||
488 | done(result); | ||
489 | } | ||
490 | |||
491 | |||