Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

modeminfo.cpp

Go to the documentation of this file.
00001 /*
00002  *            kPPP: A front end for pppd for the KDE project
00003  *
00004  * $Id: modeminfo.cpp,v 1.5 2004/02/21 18:32:38 ar Exp $
00005  *
00006  * Copyright (C) 1997 Bernd Johannes Wuebben
00007  *                    wuebben@math.cornell.edu
00008  *
00009  * This file contributed by: Markus Wuebben, mwuebben@fiwi02.wiwi.uni-tuebingen.de
00010  *
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Library General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Library General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Library General Public
00023  * License along with this program; if not, write to the Free
00024  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  */
00026 
00027 #include "modeminfo.h"
00028 #include "modem.h"
00029 
00030 /* OPIE */
00031 #include <qpe/qpeapplication.h>
00032 
00033 /* QT */
00034 #include <qregexp.h>
00035 #include <qlayout.h>
00036 #include <qmessagebox.h>
00037 #include <qapplication.h>
00038 
00039 /* STD */
00040 #include <unistd.h>
00041 
00042 ModemTransfer::ModemTransfer(Modem *mo, QWidget *parent, const char *name)
00043         : QDialog(parent, name,TRUE, WStyle_Customize|WStyle_NormalBorder),
00044         _modem(mo)
00045 {
00046     setCaption(QObject::tr("ATI Query"));
00047     //  KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
00048 
00049     QVBoxLayout *tl = new QVBoxLayout(this, 10, 10);
00050 
00051     progressBar = new QProgressBar(this, "bar");
00052     progressBar->setTotalSteps(8);
00053 
00054     statusBar = new QLabel(this,"sBar");
00055     statusBar->setFrameStyle(QFrame::Panel|QFrame::Sunken);
00056     statusBar->setAlignment(AlignCenter);
00057 
00058     // This is a rather complicated case. Since we do not know which
00059     // message is the widest in the national language, we'd to
00060     // search all these messages. This is a little overkill, so I take
00061     // the longest english message, translate it and give it additional
00062     // 20 percent space. Hope this is enough.
00063     statusBar->setText(QObject::tr("Unable to create modem lock file."));
00064     statusBar->setFixedWidth((statusBar->sizeHint().width() * 12) / 10);
00065     statusBar->setFixedHeight(statusBar->sizeHint().height() + 4);
00066 
00067     // set original text
00068     statusBar->setText(QObject::tr("Looking for modem..."));
00069     progressBar->setFixedHeight(statusBar->minimumSize().height());
00070     tl->addWidget(progressBar);
00071     tl->addWidget(statusBar);
00072 
00073     cancel = new QPushButton(QObject::tr("Cancel"), this);
00074     cancel->setFocus();
00075     connect(cancel, SIGNAL(clicked()), SLOT(cancelbutton()));
00076 
00077     QHBoxLayout *l1 = new QHBoxLayout;
00078     tl->addLayout(l1);
00079     l1->addStretch(1);
00080     l1->addWidget(cancel);
00081 
00082     setFixedSize(sizeHint());
00083 
00084     step = 0;
00085 
00087 
00088     timeout_timer = new QTimer(this);
00089     connect(timeout_timer, SIGNAL(timeout()), SLOT(time_out_slot()));
00090 
00091     scripttimer = new QTimer(this);
00092     connect(scripttimer, SIGNAL(timeout()), SLOT(do_script()));
00093 
00094     timeout_timer->start(15000,TRUE); // 15 secs single shot
00095     QTimer::singleShot(500, this, SLOT(init()));
00096 
00097 }
00098 
00099 
00100 void ModemTransfer::ati_done()
00101 {
00102     scripttimer->stop();
00103     timeout_timer->stop();
00104     _modem->closetty();
00105     _modem->unlockdevice();
00106     hide();
00107 
00108     // open the result window
00109     ModemInfo *mi = new ModemInfo(this);
00110     for(int i = 0; i < NUM_OF_ATI; i++)
00111         mi->setAtiString(i, ati_query_strings[i]);
00112 
00113     QPEApplication::execDialog( mi );
00114     delete mi;
00115 
00116     accept();
00117 }
00118 
00119 
00120 void ModemTransfer::time_out_slot()
00121 {
00122     timeout_timer->stop();
00123     scripttimer->stop();
00124 
00125     QMessageBox::warning(this, tr("Error"), QObject::tr("Modem Query timed out."));
00126     reject();
00127 }
00128 
00129 
00130 void ModemTransfer::init()
00131 {
00132 
00133     qApp->processEvents();
00134 
00135     int lock = _modem->lockdevice();
00136     if (lock == 1)
00137     {
00138 
00139         statusBar->setText(QObject::tr("Modem device is locked."));
00140         return;
00141     }
00142 
00143     if (lock == -1)
00144     {
00145 
00146         statusBar->setText(QObject::tr("Unable to create modem lock file."));
00147         return;
00148     }
00149 
00150 
00151     if(_modem->opentty())
00152     {
00153         if(_modem->hangup())
00154         {
00155             usleep(100000);  // wait 0.1 secs
00156             _modem->writeLine("ATE0Q1V1"); // E0 don't echo the commands I send ...
00157 
00158             statusBar->setText(QObject::tr("Modem Ready"));
00159             qApp->processEvents();
00160             usleep(100000);  // wait 0.1 secs
00161             qApp->processEvents();
00162             scripttimer->start(1000);           // this one does the ati query
00163 
00164             // clear modem buffer
00165             _modem->flush();
00166 
00167             _modem->notify(this, SLOT(readChar(unsigned char)));
00168             return;
00169         }
00170     }
00171 
00172     // opentty() or hangup() failed
00173     statusBar->setText(_modem->modemMessage());
00174     step = 99; // wait until cancel is pressed
00175     _modem->unlockdevice();
00176 }
00177 
00178 
00179 void ModemTransfer::do_script()
00180 {
00181     QString msg;
00182     QString query;
00183 
00184     switch(step)
00185     {
00186     case 0:
00187         readtty();
00188         statusBar->setText("ATI...");
00189         progressBar->setProgress( progressBar->progress() + 1);
00190         _modem->writeLine("ATI\n");
00191         break;
00192 
00193     case 1:
00194     case 2:
00195     case 3:
00196     case 4:
00197     case 5:
00198     case 6:
00199     case 7:
00200         readtty();
00201         msg.sprintf("ATI %d ...", step);
00202         query.sprintf("ATI%d\n", step);
00203         statusBar->setText(msg);
00204         progressBar->setProgress( progressBar->progress() + 1);
00205         _modem->writeLine(query.local8Bit());
00206         break;
00207 
00208     default:
00209         readtty();
00210         ati_done();
00211     }
00212     step++;
00213 }
00214 
00215 void ModemTransfer::readChar(unsigned char c)
00216 {
00217     if(readbuffer.length() < 255)
00218         readbuffer += c;
00219 }
00220 
00221 void ModemTransfer::readtty()
00222 {
00223 
00224     if (step == 0)
00225         return;
00226 
00227     readbuffer.replace(QRegExp("[\n\r]")," ");         // remove stray \n and \r
00228     readbuffer = readbuffer.stripWhiteSpace(); // strip of leading or trailing white
00229     // space
00230 
00231     if(step <= NUM_OF_ATI)
00232         ati_query_strings[step-1] = readbuffer.copy();
00233 
00234     readbuffer = "";
00235 }
00236 
00237 
00238 void ModemTransfer::cancelbutton()
00239 {
00240     scripttimer->stop();
00241     _modem->stop();
00242     timeout_timer->stop();
00243 
00244     statusBar->setText(QObject::tr("One moment please..."));
00245     qApp->processEvents();
00246 
00247     _modem->hangup();
00248 
00249     _modem->closetty();
00250     _modem->unlockdevice();
00251     reject();
00252 }
00253 
00254 
00255 void ModemTransfer::closeEvent( QCloseEvent *e )
00256 {
00257     cancelbutton();
00258     e->accept();
00259 }
00260 
00261 
00262 ModemInfo::ModemInfo(QWidget *parent, const char* name)
00263         : QDialog(parent, name, TRUE, WStyle_Customize|WStyle_NormalBorder)
00264 {
00265     QString label_text;
00266 
00267     setCaption(QObject::tr("Modem Query Results"));
00268     // KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
00269 
00270     QVBoxLayout *tl = new QVBoxLayout(this, 10, 10);
00271 
00272     QGridLayout *l1 = new QGridLayout(NUM_OF_ATI, 2, 5);
00273     tl->addLayout(l1, 1);
00274     for(int  i = 0 ; i < NUM_OF_ATI ; i++)
00275     {
00276 
00277         label_text = "";
00278         if ( i == 0)
00279             label_text.sprintf("ATI :");
00280         else
00281             label_text.sprintf("ATI %d:", i );
00282 
00283         ati_label[i] = new QLabel(label_text, this);
00284         l1->addWidget(ati_label[i], i, 0);
00285 
00286         ati_label_result[i] =  new QLineEdit(this);
00287         ati_label_result[i]->setMinimumWidth(fontMetrics().width('H') * 24);
00288         l1->addWidget(ati_label_result[i], i, 1);
00289     }
00290     //tl->addSpacing(1);
00291 
00292     QHBoxLayout *l2 = new QHBoxLayout;
00293     QPushButton *ok = new QPushButton(QObject::tr("Close"), this);
00294     ok->setDefault(TRUE);
00295     ok->setFocus();
00296 
00297     tl->addLayout(l2);
00298     l2->addStretch(1);
00299 
00300     connect(ok, SIGNAL(clicked()), SLOT(accept()));
00301     l2->addWidget(ok);
00302 
00303     setMinimumSize(sizeHint());
00304 }
00305 
00306 
00307 void ModemInfo::setAtiString(int i, QString s)
00308 {
00309     if(i < NUM_OF_ATI)
00310         ati_label_result[i]->setText(s);
00311 }
00312 
00313 //#include "modeminfo.moc"

Generated on Sat Nov 5 16:17:51 2005 for OPIE by  doxygen 1.4.2