fix compilation
[kdenetwork.git] / kppp / accounts.cpp
blob36ca487635a3343c09f02850d6e5379257f5a58f
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 * based on EzPPP:
10 * Copyright (C) 1997 Jay Painter
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <qdir.h>
28 //Added by qt3to4:
29 #include <QLabel>
30 #include <QVBoxLayout>
31 #include <QHBoxLayout>
32 #include <stdlib.h>
33 #include <qlayout.h>
34 #include <q3tabdialog.h>
36 #include <qmessagebox.h>
37 #include <QApplication>
38 #include <kmessagebox.h>
39 #include <klocale.h>
40 #include <kglobal.h>
41 #include <kwindowsystem.h>
42 #include <kpagedialog.h>
43 #include <KStandardGuiItem>
44 //#include <qvgroupbox.h>
45 #include <QBoxLayout>
46 #include <kiconloader.h>
47 #include "pppdata.h"
48 #include "accounts.h"
49 #include "accounting.h"
50 #include "providerdb.h"
51 #include "edit.h"
52 #include <errno.h>
53 void parseargs(char* buf, char** args);
55 AccountWidget::AccountWidget( QWidget *parent, const char *name )
56 : QWidget( parent )
58 setObjectName( name );
59 int min = 0;
60 QVBoxLayout *l1 = new QVBoxLayout(parent);
61 l1->setSpacing(10);
62 l1->setMargin(10);
64 // add a hbox
65 QHBoxLayout *l11 = new QHBoxLayout;
66 l1->addLayout(l11);
68 accountlist_l = new Q3ListBox(parent);
69 accountlist_l->setMinimumSize(160, 128);
70 connect(accountlist_l, SIGNAL(highlighted(int)),
71 this, SLOT(slotListBoxSelect(int)));
72 connect(accountlist_l, SIGNAL(selected(int)),
73 this, SLOT(editaccount()));
74 l11->addWidget(accountlist_l, 10);
76 QVBoxLayout *l111 = new QVBoxLayout;
77 l11->addLayout(l111, 1);
78 edit_b = new QPushButton(i18n("&Edit..."), parent);
79 connect(edit_b, SIGNAL(clicked()), SLOT(editaccount()));
80 edit_b->setWhatsThis( i18n("Allows you to modify the selected account"));
82 min = edit_b->sizeHint().width();
83 min = qMax(70,min);
84 edit_b->setMinimumWidth(min);
86 l111->addWidget(edit_b);
88 new_b = new QPushButton(i18n("&New..."), parent);
89 connect(new_b, SIGNAL(clicked()), SLOT(newaccount()));
90 l111->addWidget(new_b);
91 new_b->setWhatsThis( i18n("Create a new dialup connection\n"
92 "to the Internet"));
94 copy_b = new QPushButton(i18n("Co&py"), parent);
95 connect(copy_b, SIGNAL(clicked()), SLOT(copyaccount()));
96 l111->addWidget(copy_b);
97 copy_b->setWhatsThis(
98 i18n("Makes a copy of the selected account. All\n"
99 "settings of the selected account are copied\n"
100 "to a new account that you can modify to fit your\n"
101 "needs"));
103 delete_b = new QPushButton(i18n("De&lete"), parent);
104 connect(delete_b, SIGNAL(clicked()), SLOT(deleteaccount()));
105 l111->addWidget(delete_b);
106 delete_b->setWhatsThis(
107 i18n("<p>Deletes the selected account\n\n"
108 "<font color=\"red\"><b>Use with care!</b></font>"));
110 QHBoxLayout *l12 = new QHBoxLayout;
111 l1->addStretch(1);
112 l1->addLayout(l12);
114 QVBoxLayout *l121 = new QVBoxLayout;
115 l12->addLayout(l121);
116 l121->addStretch(1);
117 costlabel = new QLabel(i18n("Phone costs:"), parent);
118 costlabel->setEnabled(false);
119 l121->addWidget(costlabel);
121 costedit = new QLineEdit(parent);
122 costedit->setFocusPolicy(Qt::NoFocus);
123 costedit->setFixedHeight(costedit->sizeHint().height());
124 costedit->setEnabled(false);
125 l121->addWidget(costedit);
126 l121->addStretch(1);
127 QString tmp = i18n("<p>This shows the accumulated phone costs\n"
128 "for the selected account.\n"
129 "\n"
130 "<b>Important</b>: If you have more than one \n"
131 "account - beware, this is <b>NOT</b> the sum \n"
132 "of the phone costs of all your accounts!");
133 costlabel->setWhatsThis( tmp);
134 costedit->setWhatsThis( tmp);
136 vollabel = new QLabel(i18n("Volume:"), parent);
137 vollabel->setEnabled(false);
138 l121->addWidget(vollabel);
140 voledit = new QLineEdit(parent);
141 voledit->setObjectName( "voledit" );
142 voledit->setFocusPolicy(Qt::NoFocus);
143 voledit->setFixedHeight(voledit->sizeHint().height());
144 voledit->setEnabled(false);
145 l121->addWidget(voledit);
146 tmp = i18n("<p>This shows the number of bytes transferred\n"
147 "for the selected account (not for all of your\n"
148 "accounts. You can select what to display in\n"
149 "the accounting dialog.\n"
150 "\n"
151 "<a href=\"#volaccounting\">More on volume accounting</a>");
153 vollabel->setWhatsThis(tmp);
154 voledit->setWhatsThis( tmp);
156 QVBoxLayout *l122 = new QVBoxLayout;
157 l12->addStretch(1);
158 l12->addLayout(l122);
160 l122->addStretch(1);
161 reset = new QPushButton(i18n("&Reset..."), parent);
162 reset->setEnabled(false);
163 connect(reset, SIGNAL(clicked()),
164 this, SLOT(resetClicked()));
165 l122->addWidget(reset);
167 log = new QPushButton(i18n("&View Logs"), parent);
168 connect(log, SIGNAL(clicked()),
169 this, SLOT(viewLogClicked()));
170 l122->addWidget(log);
171 l122->addStretch(1);
173 //load up account list from gppdata to the list box
174 if(gpppdata.accountCount() > 0) {
175 for(int i=0; i <= gpppdata.accountCount()-1; i++) {
176 gpppdata.setAccountByIndex(i);
177 accountlist_l->insertItem(gpppdata.accname());
181 slotListBoxSelect(accountlist_l->currentItem());
183 l1->activate();
188 void AccountWidget::slotListBoxSelect(int idx) {
189 delete_b->setEnabled((bool)(idx != -1));
190 edit_b->setEnabled((bool)(idx != -1));
191 copy_b->setEnabled((bool)(idx != -1));
192 if(idx!=-1) {
193 QString account = gpppdata.accname();
194 gpppdata.setAccountByIndex(accountlist_l->currentItem());
195 reset->setEnabled(true);
196 costlabel->setEnabled(true);
197 costedit->setEnabled(true);
198 costedit->setText(AccountingBase::getCosts(accountlist_l->text(accountlist_l->currentItem())));
200 vollabel->setEnabled(true);
201 voledit->setEnabled(true);
202 int bytes = gpppdata.totalBytes();
203 voledit->setText(prettyPrintVolume(bytes));
204 gpppdata.setAccount(account);
205 } else{
206 reset->setEnabled(false);
207 costlabel->setEnabled(false);
208 costedit->setText("");
209 costedit->setEnabled(false);
210 vollabel->setEnabled(false);
211 voledit->setText("");
212 voledit->setEnabled(false);
217 void AccountWidget::viewLogClicked(){
219 QApplication::flush();
220 if(fork() == 0) {
221 if (setgid(getgid()) < 0 && errno != EPERM)
222 _exit(2);
223 setuid(getuid());
224 if( geteuid() != getuid() )
225 _exit(1);
226 // TODO: use execvp
227 system("kppplogview -kppp");
228 _exit(0);
233 void AccountWidget::resetClicked(){
234 if(accountlist_l->currentItem() == -1)
235 return;
237 QueryReset dlg(this);
238 int what = dlg.exec();
240 if((what & QueryReset::COSTS)) {
241 emit resetCosts(accountlist_l->text(accountlist_l->currentItem()));
242 costedit->setText("0");
245 if((what & QueryReset::VOLUME)) {
246 emit resetVolume(accountlist_l->text(accountlist_l->currentItem()));
247 voledit->setText(prettyPrintVolume(0));
252 void AccountWidget::editaccount() {
253 gpppdata.setAccount(accountlist_l->text(accountlist_l->currentItem()));
255 int result = doTab();
257 if(result == QDialog::Accepted) {
258 accountlist_l->changeItem(gpppdata.accname(),accountlist_l->currentItem());
259 emit resetaccounts();
260 gpppdata.save();
265 void AccountWidget::newaccount() {
266 if(accountlist_l->count() == MAX_ACCOUNTS) {
267 KMessageBox::sorry(this, i18n("Maximum number of accounts reached."));
268 return;
271 int result;
272 int query = KMessageBox::questionYesNoCancel(this,
273 i18n("Do you want to use the wizard to create the new account or the "
274 "standard, dialog-based setup?\n"
275 "The wizard is easier and sufficient in most cases. If you need "
276 "very special settings, you might want to try the standard, "
277 "dialog-based setup."),
278 i18n("Create New Account"),
279 KGuiItem(i18n("&Wizard")), KGuiItem(i18n("&Manual Setup")));
281 switch(query) {
282 case KMessageBox::Yes:
284 if (gpppdata.newaccount() == -1)
285 return;
286 ProviderDB pdb(this);
287 result = pdb.exec();
288 break;
290 case KMessageBox::No:
291 if (gpppdata.newaccount() == -1)
292 return;
293 result = doTab();
294 break;
295 default:
296 return;
299 if(result == QDialog::Accepted) {
300 accountlist_l->insertItem(gpppdata.accname());
301 accountlist_l->setSelected(accountlist_l->findItem(gpppdata.accname()),
302 true);
303 emit resetaccounts();
304 gpppdata.save();
305 } else
306 gpppdata.deleteAccount();
310 void AccountWidget::copyaccount() {
311 if(accountlist_l->count() == MAX_ACCOUNTS) {
312 KMessageBox::sorry(this, i18n("Maximum number of accounts reached."));
313 return;
316 if(accountlist_l->currentItem()<0) {
317 KMessageBox::sorry(this, i18n("No account selected."));
318 return;
321 gpppdata.copyaccount(accountlist_l->currentItem());
323 accountlist_l->insertItem(gpppdata.accname());
324 emit resetaccounts();
325 gpppdata.save();
329 void AccountWidget::deleteaccount() {
331 QString s = i18n("Are you sure you want to delete\nthe account \"%1\"?",
332 accountlist_l->text(accountlist_l->currentItem()));
334 if(KMessageBox::warningYesNo(this, s, i18n("Confirm"), KGuiItem(i18n("Delete"), "edit-delete"), KStandardGuiItem::cancel()) != KMessageBox::Yes)
335 return;
337 if(gpppdata.deleteAccount(accountlist_l->text(accountlist_l->currentItem())))
338 accountlist_l->removeItem(accountlist_l->currentItem());
340 emit resetaccounts();
341 gpppdata.save();
343 slotListBoxSelect(accountlist_l->currentItem());
348 int AccountWidget::doTab(){
349 tabWindow = new KPageDialog( this );
350 tabWindow->setFaceType( KPageDialog::Tabbed );
351 tabWindow->setButtons( KDialog::Ok|KDialog::Cancel );
352 tabWindow->setDefaultButton( KDialog::Ok );
353 tabWindow->setModal( true );
354 KWindowSystem::setIcons(tabWindow->winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
355 bool isnewaccount;
357 if(gpppdata.accname().isEmpty()) {
358 tabWindow->setCaption(i18n("New Account"));
359 isnewaccount = true;
360 } else {
361 QString tit = i18n("Edit Account: ");
362 tit += gpppdata.accname();
363 tabWindow->setCaption(tit);
364 isnewaccount = false;
367 QFrame * frame = new QFrame();
368 KPageWidgetItem *pageItem = new KPageWidgetItem( frame, i18n("Dial") );
369 pageItem->setHeader( i18n("Dial Setup") );
370 tabWindow->addPage( pageItem );
371 dial_w = new DialWidget(frame, isnewaccount);
374 frame = new QFrame();
375 pageItem = new KPageWidgetItem( frame, i18n("IP") );
376 pageItem->setHeader( i18n("IP Setup") );
377 tabWindow->addPage( pageItem );
378 ip_w = new IPWidget(frame, isnewaccount);
380 frame = new QFrame();
381 pageItem = new KPageWidgetItem( frame, i18n("Gateway") );
382 pageItem->setHeader( i18n("Gateway Setup") );
383 tabWindow->addPage( pageItem );
384 gateway_w = new GatewayWidget(frame, isnewaccount);
386 frame = new QFrame();
387 pageItem = new KPageWidgetItem( frame, i18n("DNS") );
388 pageItem->setHeader( i18n("DNS Servers") );
389 tabWindow->addPage( pageItem );
390 dns_w = new DNSWidget(frame, isnewaccount);
392 frame = new QFrame();
393 pageItem = new KPageWidgetItem( frame, i18n("Login Script") );
394 pageItem->setHeader( i18n("Edit Login Script") );
395 tabWindow->addPage( pageItem );
396 script_w = new ScriptWidget(frame, isnewaccount);
399 frame = new QFrame();
400 pageItem = new KPageWidgetItem( frame, i18n("Execute") );
401 pageItem->setHeader( i18n("Execute Programs") );
402 tabWindow->addPage( pageItem );
403 ExecWidget *exec_w = new ExecWidget(frame, isnewaccount);
405 frame = new QFrame();
406 pageItem = new KPageWidgetItem( frame, i18n("Accounting") );
407 tabWindow->addPage( pageItem );
409 acct = new AccountingSelector(frame, isnewaccount);
411 int result = 0;
412 bool ok = false;
413 while (!ok){
415 result = tabWindow->exec();
416 ok = true;
418 if(result == QDialog::Accepted) {
419 if (script_w->check()) {
420 if(dial_w->save()) {
421 ip_w->save();
422 dns_w->save();
423 gateway_w->save();
424 script_w->save();
425 exec_w->save();
426 acct->save();
427 } else {
428 // ### add: "and valid"
429 KMessageBox::error(this, i18n( "You must enter a unique\n"
430 "account name"));
431 ok = false;
433 } else {
434 KMessageBox::error(this, i18n("Login script has unbalanced "
435 "loop Start/End"));
436 ok = false;
441 delete tabWindow;
442 return result;
446 QString AccountWidget::prettyPrintVolume(unsigned int n) {
447 int idx = 0;
448 const QString quant[] = {i18n("Byte"), i18n("KiB"),
449 i18n("MiB"), i18n("GiB"), QString()};
451 float n1 = n;
452 while(n >= 1024 && !quant[idx].isNull()) {
453 idx++;
454 n /= 1024;
457 int i = idx;
458 while(i--)
459 n1 = n1 / 1024.0;
461 QString s = KGlobal::locale()->formatNumber( n1, idx==0 ? 0 : 1 );
462 s += ' ' + quant[idx];
463 return s;
467 /////////////////////////////////////////////////////////////////////////////
469 // Queries the user what to reset: costs, volume or both
471 /////////////////////////////////////////////////////////////////////////////
472 QueryReset::QueryReset(QWidget *parent) : KDialog(parent) {
473 KWindowSystem::setIcons(winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
474 setWindowTitle(i18n("Reset Accounting"));
476 QWidget *w = new QWidget(this);
477 setMainWidget(w);
478 QVBoxLayout *tl = new QVBoxLayout;
479 w->setLayout(tl);
480 tl->setSpacing(0);
481 tl->setMargin(0);
483 QGroupBox *f = new QGroupBox(i18n("What to Reset"));
484 tl->addWidget(f);
486 QVBoxLayout *l1 = new QVBoxLayout;
487 f->setLayout(l1);
489 costs = new QCheckBox(i18n("Reset the accumulated p&hone costs"));
490 costs->setChecked(true);
491 l1->addWidget(costs);
492 costs->setWhatsThis( i18n("Check this to set the phone costs\n"
493 "to zero. Typically you will want to\n"
494 "do this once a month."));
496 volume = new QCheckBox(i18n("Reset &volume accounting"), f);
497 volume->setChecked(true);
498 l1->addWidget(volume);
499 volume->setWhatsThis( i18n("Check this to set the volume accounting\n"
500 "to zero. Typically you will want to do this\n"
501 "once a month."));
503 l1->activate();
505 // this activates the f-layout and sets minimumSize()
506 f->show();
508 tl->addWidget(f);
510 setButtons(Ok|Cancel);
512 // TODO: activate if KGroupBox is fixed
513 // setFixedSize(sizeHint());
517 void QueryReset::accepted() {
518 int result = costs->isChecked() ? COSTS : 0;
519 result += volume->isChecked() ? VOLUME : 0;
521 done(result);
524 #include "accounts.moc"