summaryrefslogtreecommitdiffstats
path: root/kppp/modemdb.cpp
blob: 406a45c5222a80182a60ce1411623fd18f9c4b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//---------------------------------------------------------------------------
//
//             kPPP: A pppd front end for the KDE project
//
//---------------------------------------------------------------------------
//
// (c) 1997-1998 Bernd Johannes Wuebben <wuebben@kde.org>
// (c) 1997-1999 Mario Weilguni <mweilguni@kde.org>
// (c) 1998-1999 Harri Porten <porten@kde.org>
//
// derived from Jay Painters "ezppp"
//
//---------------------------------------------------------------------------
//
//  $Id$
//
//---------------------------------------------------------------------------
//
//  This program is free software; you can redistribute it and-or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Library General Public License for more details.
//
//  You should have received a copy of the GNU Library General Public
//  License along with this program; if not, write to the Free
//  Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//---------------------------------------------------------------------------

#include <tqlabel.h>
#include <kbuttonbox.h>
#include <tqlayout.h>
#include "modemdb.h"
#include <klocale.h>
#include <tqpushbutton.h>
#include <tqlistbox.h>
#include <kconfig.h>
#include <kstdguiitem.h>

ModemSelector::ModemSelector(TQWidget *parent) : TQDialog(parent, 0, true) {
  // set up widgets and such
  setCaption(i18n("Select Modem Type"));
  TQVBoxLayout *tl = new TQVBoxLayout(this, 10, 10);
  TQLabel *l1 = new TQLabel(i18n("To set up your modem, first choose its vendor in the "
			       "list to the left, and then select the model from the "
			       "right list. If you don't know which modem you have, "
			       "you can try out one of the \"Generic\" modems."),
			this);
  l1->setAlignment(AlignLeft | WordBreak);
  l1->setFixedWidth(400);
  l1->setMinimumHeight(50);
  tl->addWidget(l1, 0);

  tl->addSpacing(10);

  TQHBoxLayout *tl1 = new TQHBoxLayout(10);
  tl->addLayout(tl1, 1);
  vendor = new TQListBox(this);
  model  = new TQListBox(this);
  vendor->setMinimumSize(200, 130);
  model->setMinimumSize(200, 130);
  tl1->addWidget(vendor, 2);
  tl1->addWidget(model, 3);

  KButtonBox *bbox = new KButtonBox(this);
  bbox->addStretch(1);
  ok = bbox->addButton(KStdGuiItem::ok());
  ok->setDefault(true);
  ok->setEnabled(false);
  cancel =   bbox->addButton(KStdGuiItem::cancel());
  bbox->layout();
  tl->addWidget(bbox);
  setFixedSize(sizeHint());

  // set up modem database
  db = new ModemDatabase();

  // set up signal/slots
  connect(ok, TQT_SIGNAL(clicked()),
	  this, TQT_SLOT(reject()));
  connect(cancel, TQT_SIGNAL(clicked()),
	  this, TQT_SLOT(reject()));
  connect(vendor, TQT_SIGNAL(highlighted(int)),
	  this, TQT_SLOT(vendorSelected(int)));
  connect(model, TQT_SIGNAL(highlighted(int)),
	  this, TQT_SLOT(modelSelected(int)));
  connect(model, TQT_SIGNAL(selected(int)),
	  this, TQT_SLOT(selected(int)));

  // fill vendor list with life
  vendor->insertStringList(*db->vendors());

  vendor->setCurrentItem(0);
}


ModemSelector::~ModemSelector() {
  delete db;
}


void ModemSelector::vendorSelected(int idx) {
  ok->setEnabled(false);

  TQString name = vendor->text(idx);
  TQStringList *models = db->models(name);
  model->clear();
  model->insertStringList(*models);

  // FIXME: work around TQt bug
  if(models->count() == 0)
    model->update();
  delete models;
}


void ModemSelector::modelSelected(int) {
  ok->setEnabled(true);
}

void ModemSelector::selected(int) {
  accept();
}


ModemDatabase::ModemDatabase() {
  load();
}


ModemDatabase::~ModemDatabase() {
  delete lvendors;
  delete modemDB;
}


const TQStringList *ModemDatabase::vendors() {
  return lvendors;
}


TQStringList *ModemDatabase::models(TQString vendor)  {
  TQStringList *sl = new TQStringList;
  TQString s = i18n("<Generic>");
  if(vendor == s)
    vendor = i18n("<Generic>");

  for(uint i = 0; i < modems.count(); i++) {
    CharDict *dict = modems.at(i);
    if(dict->find("Vendor") != 0) {
      if(vendor == *(*dict)["Vendor"] && (*(*dict)["Name"]).at(0) != '!')
	sl->append(*(*dict)["Name"]);
    }
  }
  sl->sort();

  return sl;
}


void ModemDatabase::loadModem(const TQString &key, CharDict &dict) {
  //  KEntryIterator *it = modemDB->entryIterator(key);
  //  KEntryDictEntry *e;
  TQMap <TQString, TQString> map;
  TQMap <TQString, TQString>::Iterator it;
  //  KEntryMapConstIterator e;
  KEntry e;
  map = modemDB->entryMap(key);
  it = map.begin();

  // remove parent attribute
  dict.setAutoDelete(true);
  dict.remove("Parent");

  //  e = it->current();
  while(!it.key().isNull()) {
    if(dict.find(it.key()) == 0) {
      dict.insert(it.key(), new TQString(it.data()));
    }
    it++;
  }

  // check name attribute
  if(dict["Name"] == 0 || key[0]=='!') {
    dict.replace("Name", new TQString(key));
  }

  // check parent attribute
  if(dict["Parent"] != 0)
    loadModem(*dict["Parent"], dict);
  else
    // inherit common at last
    if (key != "Common")
      loadModem("Common", dict);

}


void ModemDatabase::load() {
  modemDB = new TDEConfig("DB/modemDB.rc", 0);
  lvendors = new TQStringList;
  modems.setAutoDelete(true);

  TQStringList list = modemDB->groupList();
  TQStringList::Iterator it = list.begin();
  while(it != list.end()) {
    modemDB->setGroup(*it);
    CharDict *c = new CharDict;
    c->setAutoDelete(true);
    loadModem(*it, *c);

    //    if(strcmp(it->latin1(), "Common") == 0) {
    if(*it == "Common") {
      TQString s = i18n("Hayes(tm) compatible modem");
      c->replace("Name", new TQString (s));

      s = i18n("<Generic>");
      c->replace("Vendor", new TQString(s));
    }
    modems.append(c);

    if(modemDB->hasKey("Vendor")) {
      TQString vendor = modemDB->readEntry("Vendor");
      if(lvendors->findIndex(vendor) == -1)
	lvendors->append(vendor);
    }
    ++it;
  }

  lvendors->sort();

  lvendors->insert(0, i18n("<Generic>"));
}


void ModemDatabase::save(TDEConfig *) {
}

#include "modemdb.moc"