Use KIO::filesize_t to be able to handle big filesizes (now 4GB DVD image filesizes...
[kdenetwork.git] / kppp / modeminfo.cpp
blobcc36e8ed051d31bb7e9ebab10ecffc095986f350
1 /*
2 * kPPP: A front end for pppd for the KDE project
4 * Copyright (C) 1997 Bernd Johannes Wuebben
5 * wuebben@math.cornell.edu
7 * This file contributed by: Markus Wuebben, mwuebben@fiwi02.wiwi.uni-tuebingen.de
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.
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.
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.
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>
44 ModemTransfer::ModemTransfer(QWidget *parent, const char *name)
45 : QDialog(parent)
47 setObjectName(name);
48 setModal(true);
49 setWindowFlags(Qt::WStyle_Customize|Qt::WStyle_NormalBorder);
50 setWindowTitle(i18n("ATI Query"));
51 KWindowSystem::setIcons(winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
53 QVBoxLayout *tl = new QVBoxLayout(this);
54 tl->setSpacing(10);
55 tl->setMargin(10);
57 progressBar = new QProgressBar(this);
58 progressBar->setMaximum(8);
60 statusBar = new QLabel( this );
61 statusBar->setObjectName( "sBar" );
62 statusBar->setFrameStyle(QFrame::Panel|QFrame::Sunken);
63 statusBar->setAlignment(Qt::AlignCenter);
65 // This is a rather complicated case. Since we do not know which
66 // message is the widest in the national language, we'd to
67 // search all these messages. This is a little overkill, so I take
68 // the longest english message, translate it and give it additional
69 // 20 percent space. Hope this is enough.
70 statusBar->setText(i18n("Unable to create modem lock file."));
71 statusBar->setFixedWidth((statusBar->sizeHint().width() * 12) / 10);
72 statusBar->setFixedHeight(statusBar->sizeHint().height() + 4);
74 // set original text
75 statusBar->setText(i18n("Looking for modem..."));
76 progressBar->setFixedHeight(statusBar->minimumSize().height());
77 tl->addWidget(progressBar);
78 tl->addWidget(statusBar);
80 cancel = new KPushButton(KStandardGuiItem::cancel(), this);
81 cancel->setFocus();
82 connect(cancel, SIGNAL(clicked()), SLOT(cancelbutton()));
84 QHBoxLayout *l1 = new QHBoxLayout;
85 tl->addLayout(l1);
86 l1->addStretch(1);
87 l1->addWidget(cancel);
89 setFixedSize(sizeHint());
91 step = 0;
93 ////////////////////////////////////////////////
95 timeout_timer = new QTimer(this);
96 connect(timeout_timer, SIGNAL(timeout()), SLOT(time_out_slot()));
98 scripttimer = new QTimer(this);
99 connect(scripttimer, SIGNAL(timeout()), SLOT(do_script()));
101 timeout_timer->setSingleShot(true);
102 timeout_timer->start(15000); // 15 secs
103 QTimer::singleShot(500, this, SLOT(init()));
108 void ModemTransfer::ati_done() {
109 scripttimer->stop();
110 timeout_timer->stop();
111 Modem::modem->closetty();
112 Modem::modem->unlockdevice();
113 hide();
115 // open the result window
116 ModemInfo *mi = new ModemInfo(this);
117 for(int i = 0; i < NUM_OF_ATI; i++)
118 mi->setAtiString(i, ati_query_strings[i]);
119 mi->exec();
120 delete mi;
122 accept();
126 void ModemTransfer::time_out_slot() {
127 timeout_timer->stop();
128 scripttimer->stop();
130 KMessageBox::error(this, i18n("Modem query timed out."));
131 reject();
135 void ModemTransfer::init() {
137 qApp->processEvents();
139 int lock = Modem::modem->lockdevice();
140 if (lock == 1) {
142 statusBar->setText(i18n("Modem device is locked."));
143 return;
146 if (lock == -1) {
148 statusBar->setText(i18n("Unable to create modem lock file."));
149 return;
153 if(Modem::modem->opentty()) {
154 if(Modem::modem->hangup()) {
155 usleep(100000); // wait 0.1 secs
156 Modem::modem->writeLine("ATE0Q1V1"); // E0 don't echo the commands I send ...
158 statusBar->setText(i18n("Modem Ready"));
159 qApp->processEvents();
160 usleep(100000); // wait 0.1 secs
161 qApp->processEvents();
162 scripttimer->start(1000); // this one does the ati query
164 // clear modem buffer
165 Modem::modem->flush();
167 Modem::modem->notify(this, SLOT(readChar(unsigned char)));
168 return;
172 // opentty() or hangup() failed
173 statusBar->setText(Modem::modem->modemMessage());
174 step = 99; // wait until cancel is pressed
175 Modem::modem->unlockdevice();
179 void ModemTransfer::do_script() {
180 QString msg;
181 QString query;
183 switch(step) {
184 case 0:
185 readtty();
186 statusBar->setText("ATI...");
187 progressBar->setValue(progressBar->value() + 1);
188 Modem::modem->writeLine("ATI\n");
189 break;
191 case 1:
192 case 2:
193 case 3:
194 case 4:
195 case 5:
196 case 6:
197 case 7:
198 readtty();
199 msg.sprintf("ATI %d ...", step);
200 query.sprintf("ATI%d\n", step);
201 statusBar->setText(msg);
202 progressBar->setValue(progressBar->value() + 1);
203 Modem::modem->writeLine(query.toLocal8Bit());
204 break;
206 default:
207 readtty();
208 ati_done();
210 step++;
213 void ModemTransfer::readChar(unsigned char c) {
214 if(readbuffer.length() < 255)
215 readbuffer += c;
218 void ModemTransfer::readtty() {
220 if (step == 0)
221 return;
223 readbuffer.replace(QRegExp("[\n\r]")," "); // remove stray \n and \r
224 readbuffer = readbuffer.trimmed(); // strip of leading or trailing white
225 // space
227 if(step <= NUM_OF_ATI)
228 ati_query_strings[step-1] = readbuffer;
230 readbuffer = "";
234 void ModemTransfer::cancelbutton() {
235 scripttimer->stop();
236 Modem::modem->stop();
237 timeout_timer->stop();
239 statusBar->setText(i18n("One moment please..."));
240 qApp->processEvents();
242 Modem::modem->hangup();
244 Modem::modem->closetty();
245 Modem::modem->unlockdevice();
246 reject();
250 void ModemTransfer::closeEvent( QCloseEvent *e ) {
251 cancelbutton();
252 e->accept();
256 ModemInfo::ModemInfo(QWidget *parent, const char* name)
257 : QDialog(parent)
259 setObjectName(name);
260 setModal(true);
261 setWindowFlags(Qt::WStyle_Customize|Qt::WStyle_NormalBorder);
262 QString label_text;
264 setWindowTitle(i18n("Modem Query Results"));
265 KWindowSystem::setIcons(winId(), qApp->windowIcon().pixmap(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop)), qApp->windowIcon().pixmap(IconSize(KIconLoader::Small),IconSize(KIconLoader::Small)));
267 QVBoxLayout *tl = new QVBoxLayout(this);
268 tl->setSpacing(10);
269 tl->setMargin(10);
271 QGridLayout *l1 = new QGridLayout();
272 l1->setMargin( 5 );
273 tl->addLayout(l1, 1);
274 for(int i = 0 ; i < NUM_OF_ATI ; i++) {
276 label_text = "";
277 if ( i == 0)
278 label_text.sprintf("ATI :");
279 else
280 label_text.sprintf("ATI %d:", i );
282 ati_label[i] = new QLabel(label_text, this);
283 l1->addWidget(ati_label[i], i, 0);
285 ati_label_result[i] = new QLineEdit(this);
286 ati_label_result[i]->setMinimumWidth(fontMetrics().width('H') * 24);
287 l1->addWidget(ati_label_result[i], i, 1);
289 //tl->addSpacing(1);
291 QHBoxLayout *l2 = new QHBoxLayout;
292 QPushButton *ok = new KPushButton(KStandardGuiItem::close(), this);
293 ok->setDefault(true);
294 ok->setFocus();
296 tl->addLayout(l2);
297 l2->addStretch(1);
299 connect(ok, SIGNAL(clicked()), SLOT(accept()));
300 l2->addWidget(ok);
302 setMinimumSize(sizeHint());
306 void ModemInfo::setAtiString(int i, const QString &s) {
307 if(i < NUM_OF_ATI)
308 ati_label_result[i]->setText(s);
311 #include "modeminfo.moc"