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// $Id$
16//
17//---------------------------------------------------------------------------
18//
19// This program is free software; you can redistribute it and-or
20// modify it under the terms of the GNU Library General Public
21// License as published by the Free Software Foundation; either
22// version 2 of the License, or (at your option) any later version.
23//
24// This program is distributed in the hope that it will be useful,
25// but WITHOUT ANY WARRANTY; without even the implied warranty of
26// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27// Library General Public License for more details.
28//
29// You should have received a copy of the GNU Library General Public
30// License along with this program; if not, write to the Free
31// Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32//
33//---------------------------------------------------------------------------
34
35#include <qlabel.h>
36//Added by qt3to4:
37#include <QVBoxLayout>
38#include <QHBoxLayout>
39#include <qlayout.h>
40#include "modemdb.h"
41#include <klocale.h>
42#include <qpushbutton.h>
43#include <q3listbox.h>
44#include <kconfig.h>
45#include <KStandardGuiItem>
46#include <kguiitem.h>
47#include <kdialog.h>
48
49ModemSelector::ModemSelector(QWidget *parent) : KDialog(parent) {
50 // set up widgets and such
51 setWindowTitle(i18n("Select Modem Type"));
52 QVBoxLayout *tl = new QVBoxLayout(this);
53 tl->setSpacing(10);
54 tl->setMargin(10);
55 QLabel *l1 = new QLabel(i18n("To set up your modem, first choose its vendor in the "
56 "list to the left, and then select the model from the "
57 "right list. If you do not know which modem you have, "
58 "you can try out one of the \"Generic\" modems."),
59 this);
60 l1->setAlignment(Qt::AlignLeft);
61 l1->setWordWrap( true );
62 l1->setFixedWidth(400);
63 l1->setMinimumHeight(50);
64 tl->addWidget(l1, 0);
65
66 tl->addSpacing(10);
67
68 QHBoxLayout *tl1 = new QHBoxLayout();
69 tl1->setSpacing( 10 );
70 tl->addLayout(tl1, 1);
71 vendor = new Q3ListBox(this);
72 model = new Q3ListBox(this);
73 vendor->setMinimumSize(200, 130);
74 model->setMinimumSize(200, 130);
75 tl1->addWidget(vendor, 2);
76 tl1->addWidget(model, 3);
77
78 setButtons(Ok|Cancel);
79 enableButton(Ok,false);
80 setFixedSize(sizeHint());
81
82 // set up modem database
83 db = new ModemDatabase();
84
85 // set up signal/slots
86 connect(vendor, SIGNAL(highlighted(int)),
87 this, SLOT(vendorSelected(int)));
88 connect(model, SIGNAL(highlighted(int)),
89 this, SLOT(modelSelected(int)));
90 connect(model, SIGNAL(selected(int)),
91 this, SLOT(selected(int)));
92
93 // fill vendor list with life
94 vendor->insertStringList(*db->vendors());
95
96 vendor->setCurrentItem(0);
97}
98
99
100ModemSelector::~ModemSelector() {
101 delete db;
102}
103
104
105void ModemSelector::vendorSelected(int idx) {
106 enableButton(Ok,false);
107
108 QString name = vendor->text(idx);
109 QStringList *models = db->models(name);
110 model->clear();
111 model->insertStringList(*models);
112
113 // FIXME: work around Qt bug
114 if(models->count() == 0)
115 model->update();
116 delete models;
117}
118
119
120void ModemSelector::modelSelected(int) {
121 enableButton(Ok,true);
122}
123
124void ModemSelector::selected(int) {
125 accept();
126}
127
128
129ModemDatabase::ModemDatabase() {
130 load();
131}
132
133
134ModemDatabase::~ModemDatabase() {
135 delete lvendors;
136 delete modemDB;
137}
138
139
140const QStringList *ModemDatabase::vendors() {
141 return lvendors;
142}
143
144
145QStringList *ModemDatabase::models(QString vendor) {
146 QStringList *sl = new QStringList;
147 QString s = i18n("<Generic>");
148 if(vendor == s)
149 vendor = i18n("<Generic>");
150
151 for(uint i = 0; i < modems.count(); i++) {
152 CharDict *dict = modems.at(i);
153 if(dict->find("Vendor") != 0) {
154 if(vendor == *(*dict)["Vendor"] && (*(*dict)["Name"]).at(0) != '!')
155 sl->append(*(*dict)["Name"]);
156 }
157 }
158 sl->sort();
159
160 return sl;
161}
162
163
164void ModemDatabase::loadModem(const QString &key, CharDict &dict) {
165 // KEntryIterator *it = modemDB->entryIterator(key);
166 // KEntryDictEntry *e;
167 QMap <QString, QString> map;
168 QMap <QString, QString>::Iterator it;
169 // KEntryMapConstIterator e;
170 map = modemDB->entryMap(key);
171 it = map.begin();
172
173 // remove parent attribute
174 dict.setAutoDelete(true);
175 dict.remove("Parent");
176
177 // e = it->current();
178 while(!it.key().isNull()) {
179 if(dict.find(it.key()) == 0) {
180 dict.insert(it.key(), new QString(it.value()));
181 }
182 it++;
183 }
184
185 // check name attribute
186 if(dict["Name"] == 0 || key[0]=='!') {
187 dict.replace("Name", new QString(key));
188 }
189
190 // check parent attribute
191 if(dict["Parent"] != 0)
192 loadModem(*dict["Parent"], dict);
193 else
194 // inherit common at last
195 if (key != "Common")
196 loadModem("Common", dict);
197
198}
199
200
201void ModemDatabase::load() {
202 modemDB = new KConfig("DB/modemDB.rc");
203 lvendors = new QStringList;
204 modems.setAutoDelete(true);
205
206 QStringList list = modemDB->groupList();
207 QStringList::Iterator it = list.begin();
208 while(it != list.end()) {
209 KConfigGroup cg ( modemDB, *it);
210 CharDict *c = new CharDict;
211 c->setAutoDelete(true);
212 loadModem(*it, *c);
213
214 // if(strcmp(it->latin1(), "Common") == 0) {
215 if(*it == "Common") {
216 QString s = i18n("Hayes(tm) compatible modem");
217 c->replace("Name", new QString (s));
218
219 s = i18n("<Generic>");
220 c->replace("Vendor", new QString(s));
221 }
222 modems.append(c);
223
224 if(cg.hasKey("Vendor")) {
225 QString vendor = cg.readEntry("Vendor");
226 if(lvendors->indexOf(vendor) == -1)
227 lvendors->append(vendor);
228 }
229 ++it;
230 }
231
232 lvendors->sort();
233
234 lvendors->insert(0, i18n("<Generic>"));
235}
236
237
238void ModemDatabase::save(KConfig *) {
239}
240
241#include "modemdb.moc"
242