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> |
10 | // |
11 | // derived from Jay Painters "ezppp" |
12 | // |
13 | //--------------------------------------------------------------------------- |
14 | // |
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. |
19 | // |
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. |
24 | // |
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. |
28 | // |
29 | //--------------------------------------------------------------------------- |
30 | |
31 | |
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> |
50 | |
51 | #define UNENCODED_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" |
52 | |
53 | Q3Wizard* ProviderDB::wiz = 0L; |
54 | |
55 | ProviderDB::ProviderDB(QWidget *parent) : |
56 | K3Wizard(parent, "" , true), |
57 | cfg(0) |
58 | { |
59 | setWindowTitle(i18n("Create New Account" )); |
60 | |
61 | wiz = this; |
62 | |
63 | page1 = new PDB_Intro(this); |
64 | addPage(page1, "" ); |
65 | setHelpEnabled(page1, false); |
66 | // TODO p1->w->setFocusPolicy(StrongFocus); |
67 | |
68 | page2 = new PDB_Country(this); |
69 | addPage(page2, "" ); |
70 | setHelpEnabled(page2, false); |
71 | |
72 | page3 = new PDB_Provider(this); |
73 | addPage(page3, "" ); |
74 | setHelpEnabled(page3, false); |
75 | |
76 | page4 = new PDB_UserInfo(this); |
77 | addPage(page4, "" ); |
78 | setHelpEnabled(page4, false); |
79 | |
80 | page5 = new PDB_DialPrefix(this); |
81 | addPage(page5, "" ); |
82 | setHelpEnabled(page5, false); |
83 | |
84 | page9 = new PDB_Finished(this); |
85 | addPage(page9, "" ); |
86 | setHelpEnabled(page9, false); |
87 | setFinish(page9, true); |
88 | setFinishEnabled(page9, true); |
89 | |
90 | connect((const QObject *)nextButton(), SIGNAL(clicked()), |
91 | this, SLOT(pageSelected())); |
92 | connect((const QObject *)backButton(), SIGNAL(clicked()), |
93 | this, SLOT(pageSelected())); |
94 | |
95 | // resize(minimumSize()); |
96 | adjustSize(); |
97 | } |
98 | |
99 | |
100 | ProviderDB::~ProviderDB() { |
101 | delete cfg; |
102 | } |
103 | |
104 | |
105 | void ProviderDB::pageSelected() { |
106 | bool prev = true; |
107 | bool next = true; |
108 | |
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(); |
119 | } |
120 | |
121 | setBackEnabled(page, prev); |
122 | setNextEnabled(page, next); |
123 | } |
124 | |
125 | |
126 | void ProviderDB::loadProviderInfo() { |
127 | delete cfg; |
128 | |
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; |
136 | |
137 | cfg = new KConfig(fname, KConfig::SimpleConfig); |
138 | } |
139 | |
140 | |
141 | void ProviderDB::accept() { |
142 | QString usernamePlaceholder="%USERNAME%" ; |
143 | QString passwordPlaceholder="%PASSWORD%" ; |
144 | QString username=page4->username(); |
145 | QString password=page4->password(); |
146 | |
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); |
154 | |
155 | gpppdata.writeConfig(gpppdata.currentAccountGroup(), key, value); |
156 | |
157 | if(key == "Name" ) |
158 | gpppdata.setAccname(value); |
159 | ++it; |
160 | } |
161 | |
162 | map=cfg->entryMap(MODEM_GRP); |
163 | for(it=map.begin(); it!=map.end(); ++it) { |
164 | gpppdata.writeConfig(gpppdata.currentModemGroup(), it.key(), *it); |
165 | } |
166 | |
167 | gpppdata.writeConfig(gpppdata.currentAccountGroup(), "DialPrefix" , page5->prefix()); |
168 | done(Accepted); |
169 | } |
170 | |
171 | |
172 | ///////////////////////////////////////////////////////////////////////////// |
173 | // |
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(); |
189 | } |
190 | |
191 | |
192 | ///////////////////////////////////////////////////////////////////////////// |
193 | // |
194 | ///////////////////////////////////////////////////////////////////////////// |
195 | PDB_Country::PDB_Country(QWidget *parent) : QWidget(parent) { |
196 | //TODO when ported to MVC -- preselect country |
197 | |
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); |
209 | |
210 | QHBoxLayout *l1 = new QHBoxLayout; |
211 | tl->addLayout(l1); |
212 | l1->addStretch(1); |
213 | |
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); |
220 | |
221 | list = new QStringList; |
222 | |
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); |
228 | |
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(); |
245 | } |
246 | if (entryName.isNull()) entryName = fi.fileName(); |
247 | countries.insert(entryName, fi.fileName()); |
248 | } |
249 | } |
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; |
257 | } |
258 | } |
259 | |
260 | tl->activate(); |
261 | } |
262 | |
263 | PDB_Country::~PDB_Country() |
264 | { |
265 | delete list; |
266 | } |
267 | |
268 | void PDB_Country::selectionChanged(int idx) { |
269 | // QWizard *wizard = (QWizard *)parent(); Why doesn't this work ? |
270 | ProviderDB::wiz->setNextEnabled(this, (idx != -1)); |
271 | } |
272 | |
273 | |
274 | ///////////////////////////////////////////////////////////////////////////// |
275 | // |
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); |
288 | |
289 | QHBoxLayout *l1 = new QHBoxLayout; |
290 | tl->addLayout(l1); |
291 | l1->addStretch(1); |
292 | |
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); |
299 | } |
300 | |
301 | void PDB_Provider::selectionChanged(int idx) { |
302 | ProviderDB::wiz->setNextEnabled(this, idx != -1); |
303 | } |
304 | |
305 | |
306 | void PDB_Provider::setDir(const QString &_dir) { |
307 | if(dir != _dir) { |
308 | lb->clear(); |
309 | |
310 | // fill the listbox |
311 | // set up filter |
312 | dir = _dir; |
313 | |
314 | QString dir1 = KGlobal::dirs()->findDirs("appdata" , "Provider" ).first(); |
315 | dir = dir.replace(' ', "_" ); |
316 | dir1 += dir; |
317 | |
318 | QDir d(dir1); |
319 | d.setFilter(QDir::Files); |
320 | d.setSorting(QDir::Name); |
321 | |
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; |
327 | |
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); |
335 | } |
336 | ++it; |
337 | } |
338 | |
339 | // TODO: Qt 1.x needs this if list is empty |
340 | lb->update(); |
341 | } |
342 | } |
343 | |
344 | |
345 | QString PDB_Provider::getDir() { |
346 | return dir; |
347 | } |
348 | |
349 | |
350 | |
351 | ///////////////////////////////////////////////////////////////////////////// |
352 | // |
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); |
364 | |
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(QString)), |
373 | this, SLOT(textChanged(QString))); |
374 | l1->addWidget(_username, 0, 1); |
375 | _password = newLineEdit(24, this); |
376 | _password->setEchoMode(QLineEdit::Password); |
377 | connect(_password, SIGNAL(textChanged(QString)), |
378 | this, SLOT(textChanged(QString))); |
379 | l1->addWidget(_password, 1, 1); |
380 | tl->activate(); |
381 | } |
382 | |
383 | |
384 | void PDB_UserInfo::textChanged(const QString &) { |
385 | ProviderDB::wiz->setNextEnabled(this, !_password->text().isEmpty() && |
386 | !_username->text().isEmpty()); |
387 | } |
388 | |
389 | |
390 | QString PDB_UserInfo::username() { |
391 | QString s = _username->text(); |
392 | return s; |
393 | } |
394 | |
395 | |
396 | QString PDB_UserInfo::password() { |
397 | QString s = _password->text(); |
398 | return s; |
399 | } |
400 | |
401 | |
402 | void PDB_UserInfo::activate() { |
403 | _username->setFocus(); |
404 | } |
405 | |
406 | |
407 | ///////////////////////////////////////////////////////////////////////////// |
408 | // |
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); |
422 | |
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(); |
430 | } |
431 | |
432 | |
433 | QString PDB_DialPrefix::prefix() { |
434 | QString s = _prefix->text(); |
435 | |
436 | return s; |
437 | } |
438 | |
439 | |
440 | void PDB_DialPrefix::activate() { |
441 | _prefix->setFocus(); |
442 | } |
443 | |
444 | |
445 | ///////////////////////////////////////////////////////////////////////////// |
446 | // |
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); |
460 | } |
461 | |
462 | //BEGIN pseudo percentencoding routines |
463 | void urlDecode(QString &s) { |
464 | QString s1; |
465 | |
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]; |
473 | } |
474 | } |
475 | |
476 | s = s1; |
477 | } |
478 | |
479 | |
480 | void urlEncode(QString &s) { |
481 | QString s1, tmp; |
482 | |
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; |
489 | } |
490 | } |
491 | s = s1; |
492 | } |
493 | //END pseudo percentencoding routines |
494 | |
495 | #include "providerdb.moc" |
496 | |
497 | |