2 * kPPP: A pppd front end for the KDE project
6 * Copyright (C) 1997 Bernd Johannes Wuebben
7 * wuebben@math.cornell.edu
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.
30 #include <QVBoxLayout>
31 #include <QHBoxLayout>
34 #include <q3tabdialog.h>
36 #include <qmessagebox.h>
37 #include <QApplication>
38 #include <kmessagebox.h>
41 #include <kwindowsystem.h>
42 #include <kpagedialog.h>
43 #include <KStandardGuiItem>
44 //#include <qvgroupbox.h>
46 #include <kiconloader.h>
49 #include "accounting.h"
50 #include "providerdb.h"
53 void parseargs(char* buf
, char** args
);
55 AccountWidget::AccountWidget( QWidget
*parent
, const char *name
)
58 setObjectName( name
);
60 QVBoxLayout
*l1
= new QVBoxLayout(parent
);
65 QHBoxLayout
*l11
= new QHBoxLayout
;
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();
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"
94 copy_b
= new QPushButton(i18n("Co&py"), parent
);
95 connect(copy_b
, SIGNAL(clicked()), SLOT(copyaccount()));
96 l111
->addWidget(copy_b
);
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"
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
;
114 QVBoxLayout
*l121
= new QVBoxLayout
;
115 l12
->addLayout(l121
);
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
);
127 QString tmp
= i18n("<p>This shows the accumulated phone costs\n"
128 "for the selected account.\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"
151 "<a href=\"#volaccounting\">More on volume accounting</a>");
153 vollabel
->setWhatsThis(tmp
);
154 voledit
->setWhatsThis( tmp
);
156 QVBoxLayout
*l122
= new QVBoxLayout
;
158 l12
->addLayout(l122
);
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
);
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());
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));
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
);
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();
221 if (setgid(getgid()) < 0 && errno
!= EPERM
)
224 if( geteuid() != getuid() )
227 system("kppplogview -kppp");
233 void AccountWidget::resetClicked(){
234 if(accountlist_l
->currentItem() == -1)
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();
265 void AccountWidget::newaccount() {
266 if(accountlist_l
->count() == MAX_ACCOUNTS
) {
267 KMessageBox::sorry(this, i18n("Maximum number of accounts reached."));
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")));
282 case KMessageBox::Yes
:
284 if (gpppdata
.newaccount() == -1)
286 ProviderDB
pdb(this);
290 case KMessageBox::No
:
291 if (gpppdata
.newaccount() == -1)
299 if(result
== QDialog::Accepted
) {
300 accountlist_l
->insertItem(gpppdata
.accname());
301 accountlist_l
->setSelected(accountlist_l
->findItem(gpppdata
.accname()),
303 emit
resetaccounts();
306 gpppdata
.deleteAccount();
310 void AccountWidget::copyaccount() {
311 if(accountlist_l
->count() == MAX_ACCOUNTS
) {
312 KMessageBox::sorry(this, i18n("Maximum number of accounts reached."));
316 if(accountlist_l
->currentItem()<0) {
317 KMessageBox::sorry(this, i18n("No account selected."));
321 gpppdata
.copyaccount(accountlist_l
->currentItem());
323 accountlist_l
->insertItem(gpppdata
.accname());
324 emit
resetaccounts();
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
)
337 if(gpppdata
.deleteAccount(accountlist_l
->text(accountlist_l
->currentItem())))
338 accountlist_l
->removeItem(accountlist_l
->currentItem());
340 emit
resetaccounts();
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
)));
357 if(gpppdata
.accname().isEmpty()) {
358 tabWindow
->setCaption(i18n("New Account"));
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
);
415 result
= tabWindow
->exec();
418 if(result
== QDialog::Accepted
) {
419 if (script_w
->check()) {
428 // ### add: "and valid"
429 KMessageBox::error(this, i18n( "You must enter a unique\n"
434 KMessageBox::error(this, i18n("Login script has unbalanced "
446 QString
AccountWidget::prettyPrintVolume(unsigned int n
) {
448 const QString quant
[] = {i18n("Byte"), i18n("KiB"),
449 i18n("MiB"), i18n("GiB"), QString()};
452 while(n
>= 1024 && !quant
[idx
].isNull()) {
461 QString s
= KGlobal::locale()->formatNumber( n1
, idx
==0 ? 0 : 1 );
462 s
+= ' ' + quant
[idx
];
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);
478 QVBoxLayout
*tl
= new QVBoxLayout
;
483 QGroupBox
*f
= new QGroupBox(i18n("What to Reset"));
486 QVBoxLayout
*l1
= new QVBoxLayout
;
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"
505 // this activates the f-layout and sets minimumSize()
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;
524 #include "accounts.moc"