make it working with with all tested browsers
[kdenetwork.git] / kppp / providerdb.cpp
blobab0f29ff458fe7c66f21b3ac35db9ed93636a26c
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 QRegExp re_username("%USERNAME%");
143 QRegExp re_password("%PASSWORD%");
145 QMap <QString, QString> map(cfg->entryMap("<default>"));
146 QMap <QString, QString>::Iterator it(map.begin());
147 while(it != map.end()) {
148 QString key = it.key();
149 QString value = *it;
150 if(value.contains(re_username))
151 value.replace(re_username, page4->username());
153 if(value.contains(re_password))
154 value.replace(re_password, page4->password());
156 gpppdata.writeConfig(gpppdata.currentAccountGroup(), key, value);
158 if(key == "Name")
159 gpppdata.setAccname(value);
160 ++it;
163 gpppdata.writeConfig(gpppdata.currentAccountGroup(), "DialPrefix", page5->prefix());
164 done(Accepted);
168 /////////////////////////////////////////////////////////////////////////////
170 /////////////////////////////////////////////////////////////////////////////
171 PDB_Intro::PDB_Intro(QWidget *parent) : QWidget(parent) {
172 QLabel *l = new QLabel(i18n("You will be asked a few questions on information\n"
173 "which is needed to establish an Internet connection\n"
174 "with your Internet Service Provider (ISP).\n\n"
175 "Make sure you have the registration form from your\n"
176 "ISP handy. If you have any problems, try the online\n"
177 "help first. If any information is missing, contact\n"
178 "your ISP."),
179 this);
180 QVBoxLayout *tl = new QVBoxLayout(this);
181 tl->setSpacing(10);
182 tl->setMargin(10);
183 tl->addWidget(l);
184 tl->activate();
188 /////////////////////////////////////////////////////////////////////////////
190 /////////////////////////////////////////////////////////////////////////////
191 PDB_Country::PDB_Country(QWidget *parent) : QWidget(parent) {
192 QLabel *l = new QLabel(i18n("Select the location where you plan to use this\n"
193 "account from the list below. If your country or\n"
194 "location is not listed, you have to create the\n"
195 "account with the normal, dialog based setup.\n\n"
196 "If you click \"Cancel\", the dialog based setup\n"
197 "will start."),
198 this);
199 QVBoxLayout *tl = new QVBoxLayout(this);
200 tl->setSpacing(10);
201 tl->setMargin(10);
202 tl->addWidget(l);
204 QHBoxLayout *l1 = new QHBoxLayout;
205 tl->addLayout(l1);
206 l1->addStretch(1);
208 lb = new Q3ListBox(this);
209 connect(lb, SIGNAL(highlighted(int)),
210 this, SLOT(selectionChanged(int)));
211 lb->setMinimumSize(220, 100);
212 l1->addWidget(lb, 2);
213 l1->addStretch(1);
215 list = new QStringList;
217 // fill the listbox
218 // set up filter
219 QDir d(KGlobal::dirs()->findDirs("appdata", "Provider").first());
220 d.setFilter(QDir::Dirs);
221 d.setSorting(QDir::Name);
223 // read the list of files
224 const QFileInfoList flist = d.entryInfoList();
225 if(flist.size()) {
226 QFileInfoList::const_iterator it = flist.begin();
227 QFileInfoList::const_iterator itEnd = flist.end();
228 QFileInfo fi;
229 // traverse the flist and insert into a map for sorting
230 QMap<QString, QString> countries;
231 for(; it != itEnd; ++it) {
232 fi = *it;
233 if(fi.fileName() != "." && fi.fileName() != "..") {
234 QString dirFile(fi.absoluteFilePath()+"/.directory");
235 QString entryName;
236 if(QFile::exists(dirFile)){
237 KDesktopFile config(dirFile);
238 entryName = config.readName();
240 if (entryName.isNull()) entryName = fi.fileName();
241 countries.insert(entryName, fi.fileName());
244 // insert sorted entries into list box and string list
245 QMap<QString, QString>::const_iterator mit = countries.begin();
246 QMap<QString, QString>::const_iterator mend = countries.end();
247 while(mit != mend) {
248 lb->insertItem(mit.key());
249 list->append(*mit);
250 ++mit;
254 tl->activate();
257 PDB_Country::~PDB_Country()
259 delete list;
262 void PDB_Country::selectionChanged(int idx) {
263 // QWizard *wizard = (QWizard *)parent(); Why doesn't this work ?
264 ProviderDB::wiz->setNextEnabled(this, (idx != -1));
268 /////////////////////////////////////////////////////////////////////////////
270 /////////////////////////////////////////////////////////////////////////////
271 PDB_Provider::PDB_Provider(QWidget *parent) : QWidget(parent) {
272 QVBoxLayout *tl = new QVBoxLayout(this);
273 tl->setSpacing(10);
274 tl->setMargin(10);
275 QLabel *l = new QLabel(i18n("Select your Internet Service Provider (ISP) from\n"
276 "the list below. If the ISP is not in this list,\n"
277 "you have to click on \"Cancel\" and create this\n"
278 "account using the normal, dialog-based setup.\n\n"
279 "Click on \"Next\" when you have finished your\n"
280 "selection."), this);
281 tl->addWidget(l);
283 QHBoxLayout *l1 = new QHBoxLayout;
284 tl->addLayout(l1);
285 l1->addStretch(1);
287 lb = new Q3ListBox(this);
288 connect(lb, SIGNAL(highlighted(int)),
289 this, SLOT(selectionChanged(int)));
290 lb->setMinimumSize(220, 100);
291 l1->addWidget(lb, 2);
292 l1->addStretch(1);
295 void PDB_Provider::selectionChanged(int idx) {
296 ProviderDB::wiz->setNextEnabled(this, idx != -1);
300 void PDB_Provider::setDir(const QString &_dir) {
301 if(dir != _dir) {
302 lb->clear();
304 // fill the listbox
305 // set up filter
306 dir = _dir;
308 QString dir1 = KGlobal::dirs()->findDirs("appdata", "Provider").first();
309 QRegExp re1(" ");
310 dir = dir.replace(re1, "_");
311 dir1 += dir;
313 QDir d(dir1);
314 d.setFilter(QDir::Files);
315 d.setSorting(QDir::Name);
317 // read the list of files
318 const QFileInfoList list = d.entryInfoList();
319 QFileInfoList::const_iterator it = list.begin();
320 QFileInfoList::const_iterator itEnd = list.end();
321 QFileInfo fi;
323 // traverse the list and insert into the widget
324 QRegExp re("_");
325 while(it != itEnd) {
326 fi = *it;
327 QString fname = fi.fileName();
328 if(fname.length() && fname[0] != '.') {
329 urlDecode(fname);
330 lb->insertItem(fname);
332 ++it;
335 // TODO: Qt 1.x needs this if list is empty
336 lb->update();
341 QString PDB_Provider::getDir() {
342 return dir;
347 /////////////////////////////////////////////////////////////////////////////
349 /////////////////////////////////////////////////////////////////////////////
350 PDB_UserInfo::PDB_UserInfo(QWidget *parent) : QWidget(parent) {
351 QVBoxLayout *tl = new QVBoxLayout(this);
352 tl->setSpacing(10);
353 tl->setMargin(10);
354 QLabel *l = new QLabel(i18n("To log on to your ISP, kppp needs the username\n"
355 "and the password you got from your ISP. Type\n"
356 "in this information in the fields below.\n\n"
357 "Word case is important here."),
358 this);
359 tl->addWidget(l);
361 QGridLayout *l1 = new QGridLayout();
362 tl->addLayout(l1);
363 l = new QLabel(i18n("Username:"), this);
364 l1->addWidget(l, 0, 0);
365 l = new QLabel(i18n("Password:"), this);
366 l1->addWidget(l, 1, 0);
367 _username = newLineEdit(24, this);
368 connect(_username, SIGNAL(textChanged(const QString &)),
369 this, SLOT(textChanged(const QString &)));
370 l1->addWidget(_username, 0, 1);
371 _password = newLineEdit(24, this);
372 _password->setEchoMode(QLineEdit::Password);
373 connect(_password, SIGNAL(textChanged(const QString &)),
374 this, SLOT(textChanged(const QString &)));
375 l1->addWidget(_password, 1, 1);
376 tl->activate();
380 void PDB_UserInfo::textChanged(const QString &) {
381 ProviderDB::wiz->setNextEnabled(this, !_password->text().isEmpty() &&
382 !_username->text().isEmpty());
386 QString PDB_UserInfo::username() {
387 QString s = _username->text();
388 return s;
392 QString PDB_UserInfo::password() {
393 QString s = _password->text();
394 return s;
398 void PDB_UserInfo::activate() {
399 _username->setFocus();
403 /////////////////////////////////////////////////////////////////////////////
405 /////////////////////////////////////////////////////////////////////////////
406 PDB_DialPrefix::PDB_DialPrefix(QWidget *parent) : QWidget(parent) {
407 QVBoxLayout *tl = new QVBoxLayout(this);
408 tl->setSpacing(10);
409 tl->setMargin(10);
410 QLabel *l = new QLabel(i18n("If you need a special dial prefix (e.g. if you\n"
411 "are using a telephone switch) you can specify\n"
412 "it here. This prefix is dialed just before the\n"
413 "phone number.\n\n"
414 "If you have a telephone switch, you probably need\n"
415 "to write \"0\" or \"0,\" here."),
416 this);
417 tl->addWidget(l);
419 QGridLayout *l1 = new QGridLayout();
420 tl->addLayout(l1);
421 l = new QLabel(i18n("Dial prefix:"), this);
422 l1->addWidget(l, 0, 0);
423 _prefix = newLineEdit(24, this);
424 l1->addWidget(_prefix, 0, 1);
425 tl->activate();
429 QString PDB_DialPrefix::prefix() {
430 QString s = _prefix->text();
432 return s;
436 void PDB_DialPrefix::activate() {
437 _prefix->setFocus();
441 /////////////////////////////////////////////////////////////////////////////
443 /////////////////////////////////////////////////////////////////////////////
444 PDB_Finished::PDB_Finished(QWidget *parent) : QWidget(parent) {
445 QVBoxLayout *tl = new QVBoxLayout(this);
446 tl->setSpacing(10);
447 tl->setMargin(10);
448 QLabel *l = new QLabel(i18n("Finished!\n\n"
449 "A new account has been created. Click \"Finish\" to\n"
450 "go back to the setup dialog. If you want to\n"
451 "check the settings of the newly created account,\n"
452 "you can use \"Edit\" in the setup dialog."),
453 this);
454 tl->addWidget(l);
455 tl->addStretch(1);
459 void urlDecode(QString &s) {
460 QString s1;
462 for(int i = 0; i < s.length(); i++) {
463 if(s[i] == '%') {
464 s1 += 100*s[i+1].digitValue() + 10*s[i+2].digitValue()
465 + s[i+3].digitValue();
466 i += 3;
467 } else {
468 s1 += s[i];
472 s = s1;
476 void urlEncode(QString &s) {
477 QString s1, tmp;
479 for(int i = 0; i < s.length(); i++) {
480 if(QString(UNENCODED_CHARS).indexOf(s[i]) >= 0)
481 s1 += s[i];
482 else {
483 tmp.sprintf("%%%03i", s[i].unicode());
484 s1 += tmp;
487 s = s1;
491 #include "providerdb.moc"