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

opiestumbler.cpp

Go to the documentation of this file.
00001 #include <cstdlib>
00002 
00003 #include <qmenubar.h>
00004 #include <qaction.h>
00005 #include <qlistview.h>
00006 #include <qstring.h>
00007 //#include <qapplication.h>
00008 #include <qdatetime.h>
00009 #include <qpopupmenu.h>
00010 #include <qstatusbar.h>
00011 #include <qlayout.h>
00012 #include <qwhatsthis.h>
00013 #include <qtimer.h>
00014 #include <qprogressbar.h>
00015 
00016 #include <qpe/config.h>
00017 //#include <qpe/global.h>
00018 #include <qpe/qcopenvelope_qws.h>
00019 #include <qpe/qpeapplication.h>
00020 
00021 #include <opie2/odebug.h>
00022 #include <opie2/ostation.h>
00023 #include <opie2/omanufacturerdb.h>
00024 #include <opie2/onetwork.h>
00025 #include <opie2/oprocess.h>
00026 
00027 #include "stumbler.h"
00028 #include "opiestumbler.h"
00029 #include "stumblersettings.h"
00030 #include "stationviewitem.h"
00031 #include "stumblerstation.h"
00032 #include "stationinfo.h"
00033     
00034 
00035 using Opie::Net::OWirelessNetworkInterface;
00036 using Opie::Net::ONetwork;
00037 
00038 
00039 QString OpieStumbler::appCaption() {
00040     return QObject::tr("OpieStumbler");
00041 }
00042 
00043 OpieStumbler::OpieStumbler(QWidget *parent, const char *name, WFlags)
00044     :QMainWindow(parent, name, WStyle_ContextHelp),
00045     m_listCurrent(new QListView(this)), m_listHistory(new QListView(this)),
00046     m_stationsCurrent(new QList<Opie::Net::OStation>),
00047     m_popupCurrent(new QPopupMenu(this)),
00048     m_popupHistory(new QPopupMenu(this)),
00049     m_db(NULL), m_proc(NULL)
00050 {
00051     
00052     if ( QCopChannel::isRegistered("QPE/OpieStumbler") ) {
00053          QCopEnvelope e("QPE/OpieStumbler", "show()");
00054          exit(EXIT_SUCCESS);
00055     }
00056 
00057     QGridLayout *grid = new QGridLayout( this, 1, 1, 3, 0, "grid");
00058     QVBoxLayout *lay = new QVBoxLayout( NULL, 0, 5, "lay" );
00059     QSpacerItem *spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Fixed );
00060     lay->addItem(spacer);
00061     lay->addWidget(m_listCurrent);
00062     lay->addWidget(m_listHistory);
00063     grid->addLayout(lay, 0, 0);
00064 
00065     m_stationsCurrent->setAutoDelete(true);
00066     
00067     m_channel = new QCopChannel( "QPE/OpieStumbler", this );
00068     connect(m_channel, SIGNAL(received(const QCString &, const QByteArray &)), 
00069             this, SLOT(slotMessageReceived( const QCString &, const QByteArray &)) );
00070     
00071     //setCaption(appCaption());
00072     //setCentralWidget(grid);
00073     setToolBarsMovable(false);
00074     
00075     
00076     QPopupMenu *fileMenu = new QPopupMenu(this);
00077     QPopupMenu *configMenu = new QPopupMenu(this);
00078     QPopupMenu *scanMenu = new QPopupMenu(this);
00079     
00080     fileMenu->insertItem( tr("Exit"), this, SLOT(close()) );
00081     configMenu->insertItem( tr("Configure"), this, SLOT(slotConfigure()) );
00082     scanMenu->insertItem( tr("Start"), this, SLOT(slotStartScanning()) );
00083     scanMenu->insertItem( tr("Stop"), this, SLOT(slotStopScanning()) );
00084 
00085     m_popupCurrent->insertItem( tr("Show details"), this, SLOT(slotShowDetails()) );
00086     m_popupCurrent->insertItem( tr("Join Network"), this, SLOT(slotJoinNetwork()) );
00087     
00088     menuBar()->insertItem(tr("File"), fileMenu);
00089     menuBar()->insertItem(tr("Settings"), configMenu);
00090     menuBar()->insertItem(tr("Scanning"), scanMenu);
00091     
00092     QPEApplication::setStylusOperation(m_listCurrent->viewport(), QPEApplication::RightOnHold);
00093     QPEApplication::setStylusOperation(m_listHistory->viewport(), QPEApplication::RightOnHold);
00094     
00095     m_listCurrent->addColumn(tr("SSID"));
00096     m_listCurrent->addColumn(tr("Chan"));
00097     m_listCurrent->addColumn(tr("Signal"));
00098     m_listCurrent->addColumn(tr("Enc"));
00099     m_listCurrent->setSelectionMode( QListView::Extended );
00100 
00101     m_listHistory->addColumn(tr("SSID"));
00102     m_listHistory->addColumn(tr("Chan"));
00103     m_listHistory->addColumn(tr("Max Sig"));
00104     m_listHistory->addColumn(tr("Enc"));
00105     m_listHistory->addColumn(tr("Vendor"));
00106    
00107     connect(m_listCurrent, SIGNAL(mouseButtonPressed (int, QListViewItem*, const QPoint&, int)),
00108             this, SLOT(slotCurrentMousePressed (int, QListViewItem*, const QPoint&, int)));
00109 
00110     connect(m_listHistory, SIGNAL(mouseButtonPressed (int, QListViewItem*, const QPoint&, int)),
00111             this, SLOT(slotHistoryMousePressed (int, QListViewItem*, const QPoint&, int)));
00112     
00113     for(int i = CURCHAN; i <= CURENC; ++i) {
00114         m_listCurrent->setColumnAlignment( i, Qt::AlignHCenter );
00115         m_listHistory->setColumnAlignment( i, Qt::AlignHCenter );
00116     }
00117 
00118     loadConfig();
00119     m_stumbler = new Stumbler(m_interface, this);
00120     connect(m_stumbler, SIGNAL(newdata()), this, SLOT(slotUpdateStations()));
00121 
00122     QTimer::singleShot(1000, this, SLOT(slotLoadManufacturers()) );
00123     
00124     slotStartScanning();
00125 }
00126 
00127 void OpieStumbler::slotConfigure()
00128 {
00129     StumblerSettings settings(this, "Settings", true);
00130     if (settings.exec() == QDialog::Accepted)
00131     loadConfig();
00132 }
00133 
00134 void OpieStumbler::loadConfig()
00135 {
00136     Config cfg("OpieStumbler", Config::User);
00137     cfg.setGroup("General");
00138     m_interface = cfg.readEntry("interface", "wlan0");
00139 }
00140 
00141 void OpieStumbler::slotStartScanning()
00142 {
00143     setCaption(appCaption() + " (" + tr("Scanning") + ")");
00144     m_stumbler->start();
00145 }
00146 
00147 void OpieStumbler::slotStopScanning()
00148 {
00149     setCaption(appCaption());
00150     m_stumbler->stop();
00151 }
00152 
00153 void OpieStumbler::slotUpdateStations()
00154 {
00155     m_stationsCurrent->clear();
00156     
00157     m_stationsCurrent = m_stumbler->stations();
00158     if (m_stationsCurrent) {
00159         QListIterator<Opie::Net::OStation> it(*m_stationsCurrent);
00160         for(; it.current(); ++it) {
00161             Opie::Net::OStation *station = it.current();
00162             QListIterator<StumblerStation> itr(m_stationsHistory);
00163             for( ; itr.current(); ++itr) {
00164                 if (itr.current()->st->macAddress.toString() == station->macAddress.toString()) {
00165                     break;
00166                 }
00167             }
00168             if (!itr.current()) {
00169                 //We need to copy the date because m_statiosCurrent has autodelete enabled
00170                 m_stationsHistory.append(new StumblerStation(new Opie::Net::OStation, QDateTime::currentDateTime()));
00171                 *(m_stationsHistory.last()->st) = (*station);
00172             }
00173             else {
00174                 if ( itr.current()->st->level < station->level )
00175                     itr.current()->st->level = station->level;
00176                 
00177                 itr.current()->lastTimeSeen = QDateTime::currentDateTime();
00178             }   
00179         }
00180     }
00181     displayStations();
00182 }
00183 
00184 void OpieStumbler::displayStations()
00185 {
00186     m_listCurrent->clear();
00187     for(QListIterator<Opie::Net::OStation> it(*m_stationsCurrent); it.current(); ++it)
00188         new StationViewItem( m_listCurrent, it.current()->ssid, QString::number(it.current()->channel),
00189                 QString::number(it.current()->level), it.current()->encrypted ? "Y": "N", it.current()->macAddress.toString() );
00190 
00191     m_listHistory->clear();
00192     for(QListIterator<StumblerStation> it(m_stationsHistory); it.current(); ++it) 
00193         new StationViewItem( m_listHistory, it.current()->st->ssid, QString::number(it.current()->st->channel),
00194                 QString::number(it.current()->st->level), it.current()->st->encrypted ? "Y": "N",
00195                 manufacturer(it.current()->st->macAddress.toString()), it.current()->st->macAddress.toString() );
00196 }
00197 
00198 void OpieStumbler::slotMessageReceived( const QCString &message, const QByteArray &parameters)
00199 {
00200     Q_UNUSED(const_cast<QByteArray &>(parameters))
00201 
00202     if ( message == "show()" )
00203         show();
00204 }
00205 
00206 void OpieStumbler::slotCurrentMousePressed(int button, QListViewItem * item, const QPoint &point, int c)
00207 {
00208     Q_UNUSED(c)
00209         
00210     if ( 2 == button ) {
00211         m_mac = item->text(CURENC + 1);
00212         m_popupCurrent->popup(point);
00213     }
00214 }
00215 
00216 
00217 void OpieStumbler::slotHistoryMousePressed(int button, QListViewItem * item, const QPoint &point, int c)
00218 {
00219     Q_UNUSED(c)
00220     
00221     if ( 2 == button ) {
00222         m_mac = item->text(HISVENDOR + 1);
00223         m_popupHistory->popup(point);
00224     }
00225 }
00226 
00227 void OpieStumbler::slotShowDetails()
00228 {
00229     QListIterator<StumblerStation> it(m_stationsHistory);
00230     for(; it.current() && it.current()->st->macAddress.toString() != m_mac; ++it );
00231     
00232     if( it.current() ) {
00233         StationInfo info(it.current()->st->ssid, it.current()->st->type, QString::number(it.current()->st->channel),
00234                 QString::number(it.current()->st->rates.last()/1000000), QString::number(it.current()->st->level),
00235                 it.current()->st->encrypted ? "WEP": "No", 
00236                 it.current()->st->macAddress.toString(), manufacturer(it.current()->st->macAddress.toString(), true),
00237                     it.current()->lastTimeSeen.toString() ,this, "", true);
00238         info.exec();
00239     }
00240     
00241 } 
00242 
00243 void OpieStumbler::slotLoadManufacturers()
00244 {
00245     m_db = Opie::Net::OManufacturerDB::instance();
00246 }
00247 
00248 QString OpieStumbler::manufacturer( const QString &mac, bool extended )
00249 {
00250     QString retval;
00251     if ( m_db )
00252         if ( extended )
00253             retval = m_db->lookupExt(mac);
00254         else
00255             retval = m_db->lookup(mac);
00256 
00257     if ( retval.isEmpty() )
00258         retval = tr("Unknown");
00259 
00260     return retval;
00261 }
00262 
00263 void OpieStumbler::slotJoinNetwork()
00264 {
00265     slotStopScanning();
00266 
00267     OWirelessNetworkInterface *wiface = static_cast<OWirelessNetworkInterface*>(ONetwork::instance()->interface(m_interface)); 
00268     
00269     if( !wiface )
00270         return;
00271     
00272     QListIterator<StumblerStation> it(m_stationsHistory);
00273     
00274     for(; it.current() && it.current()->st->macAddress.toString() != m_mac; ++it );
00275 
00276     if( !it.current() )
00277         return;
00278     
00279     m_ssid = it.current()->st->ssid.left(it.current()->st->ssid.length()-1);
00280     m_splash = new QFrame( this, "splash", false, WStyle_StaysOnTop | WStyle_DialogBorder | WStyle_Customize );
00281     m_splash->setFrameStyle( QFrame::Panel | QFrame::Raised );
00282     m_splashBox = new QVBoxLayout( m_splash, 4, 4 );
00283     m_infoLabel = new QLabel( QString("<center><b>%1 %2</b></center>").arg(tr("Joining Network")).arg(m_ssid), m_splash );
00284     m_pbar = new QProgressBar( 3, m_splash );
00285     m_pbar->setCenterIndicator(true);
00286     m_splashBox->addWidget( m_infoLabel );
00287     m_splashBox->addWidget( m_pbar );
00288     int sw = m_splashBox->sizeHint().width() * 2;
00289     int sh = m_splashBox->sizeHint().height();
00290     m_splash->setGeometry((qApp->desktop()->width() - sw)/2, (qApp->desktop()->height() - sh)/2, sw, sh);
00291     m_splash->show();
00292     m_splash->raise();
00293 
00294     Opie::Net::OStation *station = it.current()->st;
00295     
00296     odebug << "Bringing interface down" << oendl;
00297     wiface->setUp(false);
00298     
00299     odebug << "Setting mode to " << (station->type == "adhoc" ? "adhoc" : "managed") << oendl;
00300     wiface->setMode(station->type == "adhoc" ? "adhoc" : "managed" );
00301     
00302     odebug << "Setting channel to " << station->channel << oendl;
00303     wiface->setChannel(station->channel);
00304     
00305     odebug << "Setting SSID to " << station->ssid << oendl;
00306     wiface->setSSID(station->ssid);
00307     
00308     wiface->commit();
00309     
00310     odebug << "Bringing interface up" << oendl;
00311     wiface->setUp(true);
00312     m_pbar->setProgress(1);
00313     //Wait 5 sec for association
00314     QTimer::singleShot(5000, this, SLOT(slotAssociated()));
00315 }
00316 
00317 void OpieStumbler::slotAssociated()
00318 {
00319     OWirelessNetworkInterface *wiface = static_cast<OWirelessNetworkInterface*>(ONetwork::instance()->interface(m_interface));
00320 
00321     if( !wiface ) {
00322         slotCleanSplash();
00323         return;
00324     }
00325 
00326     if (!wiface->isAssociated()) {
00327         Global::statusMessage(tr("Could not Join"));
00328         m_infoLabel->setText(tr("Could not Join"));
00329         QTimer::singleShot(5000, this, SLOT(slotCleanSplash()));
00330         return;
00331     }
00332     
00333     Global::statusMessage(tr("Joined"));
00334     m_pbar->setProgress(2);
00335     m_infoLabel->setText(QString("<center><b>%1 %2</b></center>").arg(tr("Joined Network")).arg(m_ssid));
00336    
00337     if(m_proc) {
00338         m_proc->kill();
00339         delete m_proc;
00340     }
00341 
00342     m_proc = new Opie::Core::OProcess(this);
00343         
00344     *m_proc << "udhcpc" << "-f" << "-n" << "-i" << m_interface;
00345     m_proc->start(Opie::Core::OProcess::DontCare);
00346     QTimer::singleShot(5000, this, SLOT(slotCheckDHCP()));
00347 }
00348 
00349 void OpieStumbler::slotCheckDHCP()
00350 {
00351     if(!m_proc->isRunning()) {
00352         Global::statusMessage(tr("Could not Obtain an Address"));
00353         m_infoLabel->setText(QString("<center><b>%1</b></center>").arg(tr("Could not Obtain an Address")));
00354         delete m_proc;
00355         m_proc = NULL;
00356         QTimer::singleShot(5000, this, SLOT(slotCleanSplash()));
00357         return;
00358     }
00359     m_listCurrent->show();
00360     m_pbar->setProgress(3);
00361 
00362     OWirelessNetworkInterface *wiface = static_cast<OWirelessNetworkInterface*>(ONetwork::instance()->interface(m_interface));
00363     QString ipv4 = wiface->ipV4Address().toString();
00364     m_infoLabel->setText(QString("<center><b>%1 %2</b></center>").arg(tr("Obtained IP")).arg(ipv4));
00365     Global::statusMessage(tr("Obtained IP") + " " + ipv4);
00366     QTimer::singleShot(5000, this, SLOT(slotCleanSplash()));
00367     
00368 }
00369 
00370 void OpieStumbler::slotCleanSplash()
00371 {
00372     delete m_pbar;
00373     m_pbar = 0;  
00374     
00375     delete m_infoLabel;
00376     m_infoLabel = 0;
00377     
00378     delete m_splashBox;
00379     m_splashBox = 0;
00380     
00381     delete m_splash;
00382     m_splash = 0;           
00383 }
00384 

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