Contact cannot be a null ptr, and it is accessed before anyway. Disscussed with kedge.
[kdenetwork.git] / kppp / modems.cpp
blob2ecd40f33bed6ae8dea4aef0d385555ac0216506
1 /*
2 * kPPP: A pppd front end for the KDE project
7 * Copyright (C) 2004 Simone Gotti
8 * <simone.gotti@email.it>
10 * based on EzPPP:
11 * Copyright (C) 1997 Jay Painter
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public
24 * License along with this program; if not, write to the Free
25 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <qdir.h>
29 //Added by qt3to4:
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 <kiconloader.h>
45 #include "general.h"
46 #include "pppdata.h"
47 #include "modems.h"
48 #include "accounting.h"
49 #include "providerdb.h"
50 #include "edit.h"
52 void parseargs(char* buf, char** args);
54 ModemsWidget::ModemsWidget( QWidget *parent, const char *name )
55 : QWidget( parent )
57 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 modemlist_l = new Q3ListBox(parent);
69 modemlist_l->setMinimumSize(160, 128);
70 connect(modemlist_l, SIGNAL(highlighted(int)),
71 this, SLOT(slotListBoxSelect(int)));
72 connect(modemlist_l, SIGNAL(selected(int)),
73 this, SLOT(editmodem()));
74 l11->addWidget(modemlist_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(editmodem()));
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(newmodem()));
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(copymodem()));
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(deletemodem()));
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 //load up account list from gppdata to the list box
111 // but keep the current one selected in gpppdata
112 if(gpppdata.modemCount() > 0) {
113 const QString currentmodem = gpppdata.modname();
114 for(int i=0; i <= gpppdata.modemCount()-1; i++) {
115 gpppdata.setModemByIndex(i);
116 modemlist_l->insertItem(gpppdata.modname());
118 gpppdata.setModem(currentmodem);
121 slotListBoxSelect(modemlist_l->currentItem());
123 l1->activate();
128 void ModemsWidget::slotListBoxSelect(int idx) {
129 delete_b->setEnabled((bool)(idx != -1));
130 edit_b->setEnabled((bool)(idx != -1));
131 copy_b->setEnabled((bool)(idx != -1));
132 if(idx!=-1) {
133 QString modem = gpppdata.modname();
134 gpppdata.setModemByIndex(modemlist_l->currentItem());
135 gpppdata.setModem(modem);
139 void ModemsWidget::editmodem() {
140 gpppdata.setModem(modemlist_l->text(modemlist_l->currentItem()));
142 int result = doTab();
144 if(result == QDialog::Accepted) {
145 modemlist_l->changeItem(gpppdata.modname(),modemlist_l->currentItem());
146 emit resetmodems();
147 gpppdata.save();
152 void ModemsWidget::newmodem() {
153 if(modemlist_l->count() == MAX_MODEMS) {
154 KMessageBox::sorry(this, i18n("Maximum number of modems reached."));
155 return;
158 int result;
160 if (gpppdata.newmodem() == -1)
161 return;
162 result = doTab();
165 if(result == QDialog::Accepted) {
166 modemlist_l->insertItem(gpppdata.modname());
167 modemlist_l->setSelected(modemlist_l->findItem(gpppdata.modname()),
168 true);
169 emit resetmodems();
170 gpppdata.save();
171 } else
172 gpppdata.deleteModem();
176 void ModemsWidget::copymodem() {
177 if(modemlist_l->count() == MAX_MODEMS) {
178 KMessageBox::sorry(this, i18n("Maximum number of modems reached."));
179 return;
182 if(modemlist_l->currentItem()<0) {
183 KMessageBox::sorry(this, i18n("No modem selected."));
184 return;
187 gpppdata.copymodem(modemlist_l->currentItem());
189 modemlist_l->insertItem(gpppdata.modname());
190 emit resetmodems();
191 gpppdata.save();
195 void ModemsWidget::deletemodem() {
197 QString s = i18n("Are you sure you want to delete\nthe modem \"%1\"?",
198 modemlist_l->text(modemlist_l->currentItem()));
200 if(KMessageBox::warningContinueCancel(this, s, i18n("Confirm"), KStandardGuiItem::del()) != KMessageBox::Continue)
201 return;
203 if(gpppdata.deleteModem(modemlist_l->text(modemlist_l->currentItem())))
204 modemlist_l->removeItem(modemlist_l->currentItem());
206 emit resetmodems();
207 gpppdata.save();
209 slotListBoxSelect(modemlist_l->currentItem());
214 int ModemsWidget::doTab(){
215 tabWindow = new KPageDialog( this );
216 tabWindow->setModal( true );
217 tabWindow->setButtons( KDialog::Ok|KDialog::Cancel );
218 tabWindow->setDefaultButton( KDialog::Ok );
219 tabWindow->setFaceType( KPageDialog::Tabbed );
220 KWindowSystem::setIcons(tabWindow->winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
221 bool isnewmodem;
223 if(gpppdata.modname().isEmpty()) {
224 tabWindow->setCaption(i18n("New Modem"));
225 isnewmodem = true;
226 } else {
227 QString tit = i18n("Edit Modem: ");
228 tit += gpppdata.modname();
229 tabWindow->setCaption(tit);
230 isnewmodem = false;
232 QFrame *frame = new QFrame();
233 KPageWidgetItem *pageItem = new KPageWidgetItem( frame, i18n("&Device") );
234 pageItem->setHeader( i18n("Serial Device") );
235 tabWindow->addPage( pageItem );
236 modem1 = new ModemWidget(frame, isnewmodem );
238 frame = new QFrame();
239 pageItem = new KPageWidgetItem( frame, i18n("&Modem") );
240 pageItem->setHeader( i18n("Modem Settings") );
241 tabWindow->addPage( pageItem );
243 modem2 = new ModemWidget2(frame);
244 connect ( modem1->connectName(), SIGNAL(textChanged ( const QString & )), this, SLOT(modemNameChanged(const QString & )));
246 modemNameChanged(modem1->connectName()->text());
247 int result = 0;
248 bool ok = false;
249 while (!ok){
251 result = tabWindow->exec();
252 ok = true;
254 if(result == QDialog::Accepted) {
255 if(modem1->save()) {
256 modem2->save();
257 } else {
258 KMessageBox::error(this, i18n( "You must enter a unique\n"
259 "modem name"));
260 ok = false;
265 delete tabWindow;
266 return result;
269 void ModemsWidget::modemNameChanged(const QString & text)
271 tabWindow->enableButtonOk( !text.isEmpty() );
274 QString ModemsWidget::prettyPrintVolume(unsigned int n) {
275 int idx = 0;
276 const QString quant[] = {i18n("Byte"), i18n("KiB"),
277 i18n("MiB"), i18n("GiB"), QString()};
279 float n1 = n;
280 while(n >= 1024 && !quant[idx].isNull()) {
281 idx++;
282 n /= 1024;
285 int i = idx;
286 while(i--)
287 n1 = n1 / 1024.0;
289 QString s = KGlobal::locale()->formatNumber( n1, idx==0 ? 0 : 1 );
290 s += ' ' + quant[idx];
291 return s;
294 #include "modems.moc"