Contact cannot be a null ptr, and it is accessed before anyway. Disscussed with kedge.
[kdenetwork.git] / kppp / providerdb.cpp
blob76456160661d3459bab14b118bf578635a5f3208
1 //---------------------------------------------------------------------------
2 //
3 // kPPP: A pppd front end for the KDE project
4 //
5 //---------------------------------------------------------------------------
6 //
7 // (c) 1997-1998 Bernd Johannes Wuebben <wuebben@kde.org>
8 // (c) 1997-1999 Mario Weilguni <mweilguni@kde.org>
9 // (c) 1998-1999 Harri Porten <porten@kde.org>
11 // derived from Jay Painters "ezppp"
13 //---------------------------------------------------------------------------
15 // This program is free software; you can redistribute it and-or
16 // modify it under the terms of the GNU Library General Public
17 // License as published by the Free Software Foundation; either
18 // version 2 of the License, or (at your option) any later version.
20 // This program is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 // Library General Public License for more details.
25 // You should have received a copy of the GNU Library General Public
26 // License along with this program; if not, write to the Free
27 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 //---------------------------------------------------------------------------
32 #include <qdir.h>
33 #include <qlabel.h>
34 #include <qlayout.h>
35 #include <qregexp.h>
36 //Added by qt3to4:
37 #include <QGridLayout>
38 #include <QHBoxLayout>
39 #include <QVBoxLayout>
40 #include <klocale.h>
41 #include <kstandarddirs.h>
42 #include <kdebug.h>
43 #include "providerdb.h"
44 #include "newwidget.h"
45 #include "pppdata.h"
46 #include <q3listbox.h>
47 #include <qlineedit.h>
48 #include <kconfig.h>
49 #include <kdesktopfile.h>
51 #define UNENCODED_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
53 Q3Wizard* ProviderDB::wiz = 0L;
55 ProviderDB::ProviderDB(QWidget *parent) :
56 K3Wizard(parent, "", true),
57 cfg(0)
59 setWindowTitle(i18n("Create New Account"));
61 wiz = this;
63 page1 = new PDB_Intro(this);
64 addPage(page1, "");
65 setHelpEnabled(page1, false);
66 // TODO p1->w->setFocusPolicy(StrongFocus);
68 page2 = new PDB_Country(this);
69 addPage(page2, "");
70 setHelpEnabled(page2, false);
72 page3 = new PDB_Provider(this);
73 addPage(page3, "");
74 setHelpEnabled(page3, false);
76 page4 = new PDB_UserInfo(this);
77 addPage(page4, "");
78 setHelpEnabled(page4, false);
80 page5 = new PDB_DialPrefix(this);
81 addPage(page5, "");
82 setHelpEnabled(page5, false);
84 page9 = new PDB_Finished(this);
85 addPage(page9, "");
86 setHelpEnabled(page9, false);
87 setFinish(page9, true);
88 setFinishEnabled(page9, true);
90 connect((const QObject *)nextButton(), SIGNAL(clicked()),
91 this, SLOT(pageSelected()));
92 connect((const QObject *)backButton(), SIGNAL(clicked()),
93 this, SLOT(pageSelected()));
95 // resize(minimumSize());
96 adjustSize();
100 ProviderDB::~ProviderDB() {
101 delete cfg;
105 void ProviderDB::pageSelected() {
106 bool prev = true;
107 bool next = true;
109 QWidget *page = currentPage();
110 if(page == page2) {
111 next = page2->lb->currentItem() != -1;
112 } else if(page == page3) {
113 page3->setDir(page2->list->at(page2->lb->currentItem()));
114 next = page3->lb->currentItem() != -1;
115 } else if(page == page4) {
116 loadProviderInfo();
117 next = !page4->username().isEmpty() &&
118 !page4->password().isEmpty();
121 setBackEnabled(page, prev);
122 setNextEnabled(page, next);
126 void ProviderDB::loadProviderInfo() {
127 delete cfg;
129 QString loc = page2->list->at(page2->lb->currentItem());
130 QString provider = page3->lb->text(page3->lb->currentItem());
131 urlEncode(provider);
132 QString prov = "Provider/" + loc;
133 prov += '/' + provider;
134 QString fname = KStandardDirs::locate("appdata", prov);
135 kDebug(5002) << "Providerfile=" << fname;
137 cfg = new KConfig(fname, KConfig::SimpleConfig);
141 void ProviderDB::accept() {
142 QString usernamePlaceholder="%USERNAME%";
143 QString passwordPlaceholder="%PASSWORD%";
144 QString username=page4->username();
145 QString password=page4->password();
147 QMap <QString, QString> map=cfg->entryMap("<default>");
148 QMap <QString, QString>::Iterator it=map.begin();
149 while(it != map.end()) {
150 QString key = it.key();
151 QString value = *it;
152 value.replace(usernamePlaceholder,username);
153 value.replace(passwordPlaceholder,password);
155 gpppdata.writeConfig(gpppdata.currentAccountGroup(), key, value);
157 if(key == "Name")
158 gpppdata.setAccname(value);
159 ++it;
162 map=cfg->entryMap(MODEM_GRP);
163 for(it=map.begin(); it!=map.end(); ++it) {
164 gpppdata.writeConfig(gpppdata.currentModemGroup(), it.key(), *it);
167 gpppdata.writeConfig(gpppdata.currentAccountGroup(), "DialPrefix", page5->prefix());
168 done(Accepted);
172 /////////////////////////////////////////////////////////////////////////////
174 /////////////////////////////////////////////////////////////////////////////
175 PDB_Intro::PDB_Intro(QWidget *parent) : QWidget(parent) {
176 QLabel *l = new QLabel(i18n("You will be asked a few questions on information\n"
177 "which is needed to establish an Internet connection\n"
178 "with your Internet Service Provider (ISP).\n\n"
179 "Make sure you have the registration form from your\n"
180 "ISP handy. If you have any problems, try the online\n"
181 "help first. If any information is missing, contact\n"
182 "your ISP."),
183 this);
184 QVBoxLayout *tl = new QVBoxLayout(this);
185 tl->setSpacing(10);
186 tl->setMargin(10);
187 tl->addWidget(l);
188 tl->activate();
192 /////////////////////////////////////////////////////////////////////////////
194 /////////////////////////////////////////////////////////////////////////////
195 PDB_Country::PDB_Country(QWidget *parent) : QWidget(parent) {
196 //TODO when ported to MVC -- preselect country
198 QLabel *l = new QLabel(i18n("Select the location where you plan to use this\n"
199 "account from the list below. If your country or\n"
200 "location is not listed, you have to create the\n"
201 "account with the normal, dialog based setup.\n\n"
202 "If you click \"Cancel\", the dialog based setup\n"
203 "will start."),
204 this);
205 QVBoxLayout *tl = new QVBoxLayout(this);
206 tl->setSpacing(10);
207 tl->setMargin(10);
208 tl->addWidget(l);
210 QHBoxLayout *l1 = new QHBoxLayout;
211 tl->addLayout(l1);
212 l1->addStretch(1);
214 lb = new Q3ListBox(this);
215 connect(lb, SIGNAL(highlighted(int)),
216 this, SLOT(selectionChanged(int)));
217 lb->setMinimumSize(220, 100);
218 l1->addWidget(lb, 2);
219 l1->addStretch(1);
221 list = new QStringList;
223 // fill the listbox
224 // set up filter
225 QDir d(KGlobal::dirs()->findDirs("appdata", "Provider").first());
226 d.setFilter(QDir::Dirs);
227 d.setSorting(QDir::Name);
229 // read the list of files
230 const QFileInfoList flist = d.entryInfoList();
231 if(flist.size()) {
232 QFileInfoList::const_iterator it = flist.begin();
233 QFileInfoList::const_iterator itEnd = flist.end();
234 QFileInfo fi;
235 // traverse the flist and insert into a map for sorting
236 QMap<QString, QString> countries;
237 for(; it != itEnd; ++it) {
238 fi = *it;
239 if(fi.fileName() != "." && fi.fileName() != "..") {
240 QString dirFile(fi.absoluteFilePath()+"/.directory");
241 QString entryName;
242 if(QFile::exists(dirFile)){
243 KDesktopFile config(dirFile);
244 entryName = config.readName();
246 if (entryName.isNull()) entryName = fi.fileName();
247 countries.insert(entryName, fi.fileName());
250 // insert sorted entries into list box and string list
251 QMap<QString, QString>::const_iterator mit = countries.constBegin();
252 QMap<QString, QString>::const_iterator mend = countries.constEnd();
253 while(mit != mend) {
254 lb->insertItem(mit.key());
255 list->append(*mit);
256 ++mit;
260 tl->activate();
263 PDB_Country::~PDB_Country()
265 delete list;
268 void PDB_Country::selectionChanged(int idx) {
269 // QWizard *wizard = (QWizard *)parent(); Why doesn't this work ?
270 ProviderDB::wiz->setNextEnabled(this, (idx != -1));
274 /////////////////////////////////////////////////////////////////////////////
276 /////////////////////////////////////////////////////////////////////////////
277 PDB_Provider::PDB_Provider(QWidget *parent) : QWidget(parent) {
278 QVBoxLayout *tl = new QVBoxLayout(this);
279 tl->setSpacing(10);
280 tl->setMargin(10);
281 QLabel *l = new QLabel(i18n("Select your Internet Service Provider (ISP) from\n"
282 "the list below. If the ISP is not in this list,\n"
283 "you have to click on \"Cancel\" and create this\n"
284 "account using the normal, dialog-based setup.\n\n"
285 "Click on \"Next\" when you have finished your\n"
286 "selection."), this);
287 tl->addWidget(l);
289 QHBoxLayout *l1 = new QHBoxLayout;
290 tl->addLayout(l1);
291 l1->addStretch(1);
293 lb = new Q3ListBox(this);
294 connect(lb, SIGNAL(highlighted(int)),
295 this, SLOT(selectionChanged(int)));
296 lb->setMinimumSize(220, 100);
297 l1->addWidget(lb, 2);
298 l1->addStretch(1);
301 void PDB_Provider::selectionChanged(int idx) {
302 ProviderDB::wiz->setNextEnabled(this, idx != -1);
306 void PDB_Provider::setDir(const QString &_dir) {
307 if(dir != _dir) {
308 lb->clear();
310 // fill the listbox
311 // set up filter
312 dir = _dir;
314 QString dir1 = KGlobal::dirs()->findDirs("appdata", "Provider").first();
315 dir = dir.replace(' ', "_");
316 dir1 += dir;
318 QDir d(dir1);
319 d.setFilter(QDir::Files);
320 d.setSorting(QDir::Name);
322 // read the list of files
323 const QFileInfoList list = d.entryInfoList();
324 QFileInfoList::const_iterator it = list.begin();
325 QFileInfoList::const_iterator itEnd = list.end();
326 QFileInfo fi;
328 // traverse the list and insert into the widget
329 while(it != itEnd) {
330 fi = *it;
331 QString fname = fi.fileName();
332 if(fname.length() && fname[0] != '.') {
333 urlDecode(fname);
334 lb->insertItem(fname);
336 ++it;
339 // TODO: Qt 1.x needs this if list is empty
340 lb->update();
345 QString PDB_Provider::getDir() {
346 return dir;
351 /////////////////////////////////////////////////////////////////////////////
353 /////////////////////////////////////////////////////////////////////////////
354 PDB_UserInfo::PDB_UserInfo(QWidget *parent) : QWidget(parent) {
355 QVBoxLayout *tl = new QVBoxLayout(this);
356 tl->setSpacing(10);
357 tl->setMargin(10);
358 QLabel *l = new QLabel(i18n("To log on to your ISP, kppp needs the username\n"
359 "and the password you got from your ISP. Type\n"
360 "in this information in the fields below.\n\n"
361 "Word case is important here."),
362 this);
363 tl->addWidget(l);
365 QGridLayout *l1 = new QGridLayout();
366 tl->addLayout(l1);
367 l = new QLabel(i18n("Username:"), this);
368 l1->addWidget(l, 0, 0);
369 l = new QLabel(i18n("Password:"), this);
370 l1->addWidget(l, 1, 0);
371 _username = newLineEdit(24, this);
372 connect(_username, SIGNAL(textChanged(const QString &)),
373 this, SLOT(textChanged(const QString &)));
374 l1->addWidget(_username, 0, 1);
375 _password = newLineEdit(24, this);
376 _password->setEchoMode(QLineEdit::Password);
377 connect(_password, SIGNAL(textChanged(const QString &)),
378 this, SLOT(textChanged(const QString &)));
379 l1->addWidget(_password, 1, 1);
380 tl->activate();
384 void PDB_UserInfo::textChanged(const QString &) {
385 ProviderDB::wiz->setNextEnabled(this, !_password->text().isEmpty() &&
386 !_username->text().isEmpty());
390 QString PDB_UserInfo::username() {
391 QString s = _username->text();
392 return s;
396 QString PDB_UserInfo::password() {
397 QString s = _password->text();
398 return s;
402 void PDB_UserInfo::activate() {
403 _username->setFocus();
407 /////////////////////////////////////////////////////////////////////////////
409 /////////////////////////////////////////////////////////////////////////////
410 PDB_DialPrefix::PDB_DialPrefix(QWidget *parent) : QWidget(parent) {
411 QVBoxLayout *tl = new QVBoxLayout(this);
412 tl->setSpacing(10);
413 tl->setMargin(10);
414 QLabel *l = new QLabel(i18n("If you need a special dial prefix (e.g. if you\n"
415 "are using a telephone switch) you can specify\n"
416 "it here. This prefix is dialed just before the\n"
417 "phone number.\n\n"
418 "If you have a telephone switch, you probably need\n"
419 "to write \"0\" or \"0,\" here."),
420 this);
421 tl->addWidget(l);
423 QGridLayout *l1 = new QGridLayout();
424 tl->addLayout(l1);
425 l = new QLabel(i18n("Dial prefix:"), this);
426 l1->addWidget(l, 0, 0);
427 _prefix = newLineEdit(24, this);
428 l1->addWidget(_prefix, 0, 1);
429 tl->activate();
433 QString PDB_DialPrefix::prefix() {
434 QString s = _prefix->text();
436 return s;
440 void PDB_DialPrefix::activate() {
441 _prefix->setFocus();
445 /////////////////////////////////////////////////////////////////////////////
447 /////////////////////////////////////////////////////////////////////////////
448 PDB_Finished::PDB_Finished(QWidget *parent) : QWidget(parent) {
449 QVBoxLayout *tl = new QVBoxLayout(this);
450 tl->setSpacing(10);
451 tl->setMargin(10);
452 QLabel *l = new QLabel(i18n("Finished!\n\n"
453 "A new account has been created. Click \"Finish\" to\n"
454 "go back to the setup dialog. If you want to\n"
455 "check the settings of the newly created account,\n"
456 "you can use \"Edit\" in the setup dialog."),
457 this);
458 tl->addWidget(l);
459 tl->addStretch(1);
462 //BEGIN pseudo percentencoding routines
463 void urlDecode(QString &s) {
464 QString s1;
466 for(int i = 0; i < s.length(); i++) {
467 if(s[i] == '%') {
468 s1 += 100*s[i+1].digitValue() + 10*s[i+2].digitValue()
469 + s[i+3].digitValue();
470 i += 3;
471 } else {
472 s1 += s[i];
476 s = s1;
480 void urlEncode(QString &s) {
481 QString s1, tmp;
483 for(int i = 0; i < s.length(); i++) {
484 if(QString(UNENCODED_CHARS).indexOf(s[i]) >= 0)
485 s1 += s[i];
486 else {
487 tmp.sprintf("%%%03i", s[i].unicode());
488 s1 += tmp;
491 s = s1;
493 //END pseudo percentencoding routines
495 #include "providerdb.moc"