1/*
2 * kPPP: A front end for pppd for the KDE project
3 *
4 * Copyright (C) 1997 Bernd Johannes Wuebben
5 * wuebben@math.cornell.edu
6 *
7 * This file contributed by: Markus Wuebben, mwuebben@fiwi02.wiwi.uni-tuebingen.de
8 *
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this program; if not, write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25#include <unistd.h>
26#include <qregexp.h>
27#include <qlayout.h>
28//Added by qt3to4:
29#include <QLabel>
30#include <QVBoxLayout>
31#include <QFrame>
32#include <QHBoxLayout>
33#include <QGridLayout>
34#include <QCloseEvent>
35#include <QApplication>
36#include <kwindowsystem.h>
37#include <kmessagebox.h>
38#include <kpushbutton.h>
39#include "modeminfo.h"
40#include "modem.h"
41#include <klocale.h>
42#include <kiconloader.h>
43
44ModemTransfer::ModemTransfer(QWidget *parent, const char *name)
45 : QDialog(parent)
46{
47 setObjectName(name);
48 setModal(true);
49 setWindowTitle(i18n("ATI Query"));
50 KWindowSystem::setIcons(winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
51
52 QVBoxLayout *tl = new QVBoxLayout(this);
53 tl->setSpacing(10);
54 tl->setMargin(10);
55
56 progressBar = new QProgressBar(this);
57 progressBar->setMaximum(8);
58
59 statusBar = new QLabel( this );
60 statusBar->setObjectName( "sBar" );
61 statusBar->setFrameStyle(QFrame::Panel|QFrame::Sunken);
62 statusBar->setAlignment(Qt::AlignCenter);
63
64 // This is a rather complicated case. Since we do not know which
65 // message is the widest in the national language, we'd to
66 // search all these messages. This is a little overkill, so I take
67 // the longest english message, translate it and give it additional
68 // 20 percent space. Hope this is enough.
69 statusBar->setText(i18n("Unable to create modem lock file."));
70 statusBar->setFixedWidth((statusBar->sizeHint().width() * 12) / 10);
71 statusBar->setFixedHeight(statusBar->sizeHint().height() + 4);
72
73 // set original text
74 statusBar->setText(i18n("Looking for modem..."));
75 progressBar->setFixedHeight(statusBar->minimumSize().height());
76 tl->addWidget(progressBar);
77 tl->addWidget(statusBar);
78
79 cancel = new KPushButton(KStandardGuiItem::cancel(), this);
80 cancel->setFocus();
81 connect(cancel, SIGNAL(clicked()), SLOT(cancelbutton()));
82
83 QHBoxLayout *l1 = new QHBoxLayout;
84 tl->addLayout(l1);
85 l1->addStretch(1);
86 l1->addWidget(cancel);
87
88 setFixedSize(sizeHint());
89
90 step = 0;
91
92 ////////////////////////////////////////////////
93
94 timeout_timer = new QTimer(this);
95 connect(timeout_timer, SIGNAL(timeout()), SLOT(time_out_slot()));
96
97 scripttimer = new QTimer(this);
98 connect(scripttimer, SIGNAL(timeout()), SLOT(do_script()));
99
100 timeout_timer->setSingleShot(true);
101 timeout_timer->start(15000); // 15 secs
102 QTimer::singleShot(500, this, SLOT(init()));
103
104}
105
106
107void ModemTransfer::ati_done() {
108 scripttimer->stop();
109 timeout_timer->stop();
110 Modem::modem->closetty();
111 Modem::modem->unlockdevice();
112 hide();
113
114 // open the result window
115 ModemInfo *mi = new ModemInfo(this);
116 for(int i = 0; i < NUM_OF_ATI; i++)
117 mi->setAtiString(i, ati_query_strings[i]);
118 mi->exec();
119 delete mi;
120
121 accept();
122}
123
124
125void ModemTransfer::time_out_slot() {
126 timeout_timer->stop();
127 scripttimer->stop();
128
129 KMessageBox::error(this, i18n("Modem query timed out."));
130 reject();
131}
132
133
134void ModemTransfer::init() {
135
136 qApp->processEvents();
137
138 int lock = Modem::modem->lockdevice();
139 if (lock == 1) {
140
141 statusBar->setText(i18n("Modem device is locked."));
142 return;
143 }
144
145 if (lock == -1) {
146
147 statusBar->setText(i18n("Unable to create modem lock file."));
148 return;
149 }
150
151
152 if(Modem::modem->opentty()) {
153 if(Modem::modem->hangup()) {
154 usleep(100000); // wait 0.1 secs
155 Modem::modem->writeLine("ATE0Q1V1"); // E0 don't echo the commands I send ...
156
157 statusBar->setText(i18n("Modem Ready"));
158 qApp->processEvents();
159 usleep(100000); // wait 0.1 secs
160 qApp->processEvents();
161 scripttimer->start(1000); // this one does the ati query
162
163 // clear modem buffer
164 Modem::modem->flush();
165
166 Modem::modem->notify(this, SLOT(readChar(unsigned char)));
167 return;
168 }
169 }
170
171 // opentty() or hangup() failed
172 statusBar->setText(Modem::modem->modemMessage());
173 step = 99; // wait until cancel is pressed
174 Modem::modem->unlockdevice();
175}
176
177
178void ModemTransfer::do_script() {
179 QString msg;
180 QString query;
181
182 switch(step) {
183 case 0:
184 readtty();
185 statusBar->setText("ATI...");
186 progressBar->setValue(progressBar->value() + 1);
187 Modem::modem->writeLine("ATI\n");
188 break;
189
190 case 1:
191 case 2:
192 case 3:
193 case 4:
194 case 5:
195 case 6:
196 case 7:
197 readtty();
198 msg.sprintf("ATI %d ...", step);
199 query.sprintf("ATI%d\n", step);
200 statusBar->setText(msg);
201 progressBar->setValue(progressBar->value() + 1);
202 Modem::modem->writeLine(query.toLocal8Bit());
203 break;
204
205 default:
206 readtty();
207 ati_done();
208 }
209 step++;
210}
211
212void ModemTransfer::readChar(unsigned char c) {
213 if(readbuffer.length() < 255)
214 readbuffer += c;
215}
216
217void ModemTransfer::readtty() {
218
219 if (step == 0)
220 return;
221
222 readbuffer.replace(QRegExp("[\n\r]")," "); // remove stray \n and \r
223 readbuffer = readbuffer.trimmed(); // strip of leading or trailing white
224 // space
225
226 if(step <= NUM_OF_ATI)
227 ati_query_strings[step-1] = readbuffer;
228
229 readbuffer = "";
230}
231
232
233void ModemTransfer::cancelbutton() {
234 scripttimer->stop();
235 Modem::modem->stop();
236 timeout_timer->stop();
237
238 statusBar->setText(i18n("One moment please..."));
239 qApp->processEvents();
240
241 Modem::modem->hangup();
242
243 Modem::modem->closetty();
244 Modem::modem->unlockdevice();
245 reject();
246}
247
248
249void ModemTransfer::closeEvent( QCloseEvent *e ) {
250 cancelbutton();
251 e->accept();
252}
253
254
255ModemInfo::ModemInfo(QWidget *parent, const char* name)
256 : QDialog(parent)
257{
258 setObjectName(name);
259 setModal(true);
260 QString label_text;
261
262 setWindowTitle(i18n("Modem Query Results"));
263 KWindowSystem::setIcons(winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
264
265 QVBoxLayout *tl = new QVBoxLayout(this);
266 tl->setSpacing(10);
267 tl->setMargin(10);
268
269 QGridLayout *l1 = new QGridLayout();
270 l1->setMargin( 5 );
271 tl->addLayout(l1, 1);
272 for(int i = 0 ; i < NUM_OF_ATI ; i++) {
273
274 label_text = "";
275 if ( i == 0)
276 label_text.sprintf("ATI :");
277 else
278 label_text.sprintf("ATI %d:", i );
279
280 ati_label[i] = new QLabel(label_text, this);
281 l1->addWidget(ati_label[i], i, 0);
282
283 ati_label_result[i] = new QLineEdit(this);
284 ati_label_result[i]->setMinimumWidth(fontMetrics().width('H') * 24);
285 l1->addWidget(ati_label_result[i], i, 1);
286 }
287 //tl->addSpacing(1);
288
289 QHBoxLayout *l2 = new QHBoxLayout;
290 QPushButton *ok = new KPushButton(KStandardGuiItem::close(), this);
291 ok->setDefault(true);
292 ok->setFocus();
293
294 tl->addLayout(l2);
295 l2->addStretch(1);
296
297 connect(ok, SIGNAL(clicked()), SLOT(accept()));
298 l2->addWidget(ok);
299
300 setMinimumSize(sizeHint());
301}
302
303
304void ModemInfo::setAtiString(int i, const QString &s) {
305 if(i < NUM_OF_ATI)
306 ati_label_result[i]->setText(s);
307}
308
309#include "modeminfo.moc"
310