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

bluebase.cpp

Go to the documentation of this file.
00001 /*
00002  * bluebase.cpp *
00003  * ---------------------
00004  *
00005  * copyright   : (c) 2002 by Maximilian Reiß
00006  * email       : max.reiss@gmx.de
00007  *
00008  */
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "bluebase.h"
00019 #include "scandialog.h"
00020 #include "hciconfwrapper.h"
00021 #include "devicehandler.h"
00022 #include "btconnectionitem.h"
00023 #include "rfcommassigndialogimpl.h"
00024 
00025 /* OPIE */
00026 #include <qpe/qpeapplication.h>
00027 #include <qpe/resource.h>
00028 #include <qpe/config.h>
00029 #include <opie2/odebug.h>
00030 using namespace Opie::Core;
00031 
00032 /* QT */
00033 #include <qframe.h>
00034 #include <qlabel.h>
00035 #include <qpushbutton.h>
00036 #include <qlayout.h>
00037 #include <qvariant.h>
00038 #include <qimage.h>
00039 #include <qpixmap.h>
00040 #include <qtabwidget.h>
00041 #include <qscrollview.h>
00042 #include <qvbox.h>
00043 #include <qmessagebox.h>
00044 #include <qcheckbox.h>
00045 #include <qlineedit.h>
00046 #include <qlistview.h>
00047 #include <qdir.h>
00048 #include <qpopupmenu.h>
00049 #include <qtimer.h>
00050 #include <qlist.h>
00051 
00052 /* STD */
00053 #include <remotedevice.h>
00054 #include <services.h>
00055 #include <stdlib.h>
00056 
00057 using namespace OpieTooth;
00058 
00059 BlueBase::BlueBase( QWidget* parent,  const char* name, WFlags fl )
00060         : BluetoothBase( parent, name, fl )
00061 {
00062     m_localDevice = new Manager( "hci0" );
00063 
00064     connect( PushButton2,  SIGNAL( clicked() ), this, SLOT(startScan() ) );
00065     connect( configApplyButton, SIGNAL(clicked() ), this, SLOT(applyConfigChanges() ) );
00066 
00067     connect( rfcommBindButton,  SIGNAL( clicked() ),  this,  SLOT( rfcommDialog() ) );
00068 
00069     connect( devicesView, SIGNAL( clicked(QListViewItem*)),
00070              this, SLOT( startServiceActionClicked(QListViewItem*) ) );
00071     connect( devicesView, SIGNAL( rightButtonClicked(QListViewItem*,const QPoint&,int) ),
00072              this,  SLOT(startServiceActionHold(QListViewItem*,const QPoint&,int) ) );
00073     connect( m_localDevice , SIGNAL( foundServices(const QString&,Services::ValueList) ),
00074              this, SLOT( addServicesToDevice(const QString&,Services::ValueList) ) );
00075     connect( m_localDevice, SIGNAL( available(const QString&,bool) ),
00076              this, SLOT( deviceActive(const QString&,bool) ) );
00077     connect( m_localDevice, SIGNAL( connections(ConnectionState::ValueList) ),
00078              this, SLOT( addConnectedDevices(ConnectionState::ValueList) ) );
00079     connect( m_localDevice, SIGNAL( signalStrength(const QString&,const QString&) ),
00080              this, SLOT( addSignalStrength(const QString&,const QString&) ) );
00081 
00082     // let hold be rightButtonClicked()
00083     QPEApplication::setStylusOperation( devicesView->viewport(), QPEApplication::RightOnHold);
00084     QPEApplication::setStylusOperation( connectionsView->viewport(), QPEApplication::RightOnHold);
00085 
00086     //Load all icons needed
00087     m_offPix = Resource::loadPixmap( "opietooth/notconnected" );
00088     m_onPix = Resource::loadPixmap( "opietooth/connected" );
00089     m_findPix = Resource::loadPixmap( "opietooth/find" );
00090 
00091     QPalette pal = this->palette();
00092     QColor col = pal.color( QPalette::Active, QColorGroup::Background );
00093     pal.setColor( QPalette::Active, QColorGroup::Button, col );
00094     pal.setColor( QPalette::Inactive, QColorGroup::Button, col );
00095     pal.setColor( QPalette::Normal, QColorGroup::Button, col );
00096     pal.setColor( QPalette::Disabled, QColorGroup::Button, col );
00097     this->setPalette( pal );
00098 
00099     setCaption( tr( "Bluetooth Manager" ) );
00100 
00101     readConfig();
00102     initGui();
00103 
00104     devicesView->setRootIsDecorated(true);
00105     m_iconLoader = new BTIconLoader();
00106     writeToHciConfig();
00107     addConnectedDevices();
00108     readSavedDevices();
00109     addServicesToDevices();
00110     QTimer::singleShot( 3000, this, SLOT( addServicesToDevices() ) );
00111 }
00112 
00116 void BlueBase::readConfig()
00117 {
00118 
00119     Config cfg( "bluetoothmanager" );
00120     cfg.setGroup( "bluezsettings" );
00121 
00122     m_deviceName = cfg.readEntry( "name" , "No name" ); // name the device should identify with
00123     m_defaultPasskey = cfg.readEntryCrypt( "passkey" , "" ); // <- hmm, look up how good the trolls did that, maybe too weak
00124     m_useEncryption = cfg.readBoolEntry( "useEncryption" , TRUE );
00125     m_enableAuthentification = cfg.readBoolEntry( "enableAuthentification" , TRUE );
00126     m_enablePagescan = cfg.readBoolEntry( "enablePagescan" , TRUE );
00127     m_enableInquiryscan = cfg.readBoolEntry( "enableInquiryscan" , TRUE );
00128 }
00129 
00133 void BlueBase::writeConfig()
00134 {
00135 
00136     Config cfg( "bluetoothmanager" );
00137     cfg.setGroup( "bluezsettings" );
00138 
00139     cfg.writeEntry( "name" , m_deviceName );
00140     cfg.writeEntryCrypt( "passkey" , m_defaultPasskey );
00141     cfg.writeEntry( "useEncryption" , m_useEncryption );
00142     cfg.writeEntry( "enableAuthentification" , m_enableAuthentification );
00143     cfg.writeEntry( "enablePagescan" , m_enablePagescan );
00144     cfg.writeEntry( "enableInquiryscan" , m_enableInquiryscan );
00145 
00146     writeToHciConfig();
00147 }
00148 
00152 void BlueBase::writeToHciConfig()
00153 {
00154     owarn << "writeToHciConfig" << oendl;
00155     HciConfWrapper hciconf ( "/etc/bluetooth/hcid.conf" );
00156     hciconf.load();
00157     hciconf.setPinHelper( QPEApplication::qpeDir() + "bin/bluepin" );
00158     hciconf.setName( m_deviceName );
00159     hciconf.setEncrypt( m_useEncryption );
00160     hciconf.setAuth( m_enableAuthentification );
00161     hciconf.setPscan( m_enablePagescan );
00162     hciconf.setIscan( m_enableInquiryscan );
00163     hciconf.save();
00164 }
00165 
00166 
00170 void BlueBase::readSavedDevices()
00171 {
00172 
00173     QValueList<RemoteDevice> loadedDevices;
00174     DeviceHandler handler;
00175     loadedDevices = handler.load();
00176 
00177     addSearchedDevices( loadedDevices );
00178 }
00179 
00180 
00184 void BlueBase::writeSavedDevices()
00185 {
00186     QListViewItemIterator it( devicesView );
00187     BTListItem* item;
00188     BTDeviceItem* device;
00189     RemoteDevice::ValueList list;
00190     for ( ; it.current(); ++it )
00191     {
00192         item = (BTListItem*)it.current();
00193         if(item->typeId() != BTListItem::Device )
00194             continue;
00195         device = (BTDeviceItem*)item;
00196 
00197         list.append( device->remoteDevice() );
00198     }
00199     /*
00200      * if not empty save the List through DeviceHandler
00201      */
00202     if ( list.isEmpty() )
00203         return;
00204     DeviceHandler handler;
00205     handler.save( list );
00206 }
00207 
00208 
00212 void BlueBase::initGui()
00213 {
00214     StatusLabel->setText( status() ); // maybe move it to getStatus()
00215     cryptCheckBox->setChecked( m_useEncryption );
00216     authCheckBox->setChecked( m_enableAuthentification );
00217     pagescanCheckBox->setChecked( m_enablePagescan );
00218     inquiryscanCheckBox->setChecked( m_enableInquiryscan );
00219     deviceNameLine->setText( m_deviceName );
00220     passkeyLine->setText( m_defaultPasskey );
00221     // set info tab
00222     setInfo();
00223 }
00224 
00225 
00230 QString BlueBase::status()const
00231 {
00232     QString infoString = tr( "<b>Device name : </b> Ipaq" );
00233     infoString += QString( "<br><b>" + tr( "MAC adress: " ) +"</b> No idea" );
00234     infoString += QString(  "<br><b>" + tr( "Class" ) + "</b> PDA" );
00235 
00236     return (infoString);
00237 }
00238 
00239 
00243 void BlueBase::applyConfigChanges()
00244 {
00245     m_deviceName =  deviceNameLine->text();
00246     m_defaultPasskey = passkeyLine->text();
00247     m_useEncryption = cryptCheckBox->isChecked();
00248     m_enableAuthentification = authCheckBox->isChecked();
00249     m_enablePagescan = pagescanCheckBox->isChecked();
00250     m_enableInquiryscan = inquiryscanCheckBox->isChecked();
00251 
00252     writeConfig();
00253 
00254     QMessageBox::information( this, tr("Test") , tr("Changes were applied.") );
00255 }
00256 
00261 void BlueBase::rfcommDialog()
00262 {
00263     RfcommAssignDialog rfcommAssign ( this, "RfcommAssignDialog", true, WStyle_ContextHelp  );
00264 
00265     if ( QPEApplication::execDialog( &rfcommAssign ) == QDialog::Accepted )
00266     {
00267         rfcommAssign.saveConfig();
00268     }
00269 }
00270 
00275 void BlueBase::addSearchedDevices( const QValueList<RemoteDevice> &newDevices )
00276 {
00277     BTDeviceItem * deviceItem;
00278     QValueList<RemoteDevice>::ConstIterator it;
00279 
00280     for( it = newDevices.begin(); it != newDevices.end() ; ++it )
00281     {
00282 
00283         if (find( (*it)  )) // is already inserted
00284             continue;
00285 
00286         deviceItem = new BTDeviceItem( devicesView , (*it) );
00287         deviceItem->setPixmap( 1, m_findPix );
00288         deviceItem->setExpandable ( true );
00289 
00290         // look if device is avail. atm, async
00291         deviceActive( (*it) );
00292     }
00293 }
00294 
00295 
00299 void BlueBase::startServiceActionClicked( QListViewItem */*item*/ )
00300 {}
00301 
00302 
00306 void BlueBase::startServiceActionHold( QListViewItem * item, const QPoint & point, int /*column*/ )
00307 {
00308     if (!item )
00309         return;
00310 
00311     QPopupMenu *menu = new QPopupMenu();
00312 
00313     if ( static_cast<BTListItem*>( item )->type() == "device")
00314     {
00315         QPopupMenu *groups = new QPopupMenu();
00316 
00317         menu->insertItem( static_cast<BTDeviceItem*>( item )->name(), 0 );
00318         menu->insertSeparator( 1 );
00319         menu->insertItem( tr( "&Rescan services" ), 2);
00320         // menu->insertItem( tr( "&Add to group" ), groups, 3);
00321         menu->insertItem( tr( "&Delete"),  4);
00322         int ret = menu->exec( point, 0);
00323 
00324         switch(ret)
00325         {
00326             case -1:
00327                 break;
00328             case 2:
00329                 addServicesToDevice( static_cast<BTDeviceItem*>( item ) );
00330                 break;
00331 
00332             case 4:
00333                 // deletes childs too
00334                 delete item;
00335                 break;
00336         }
00337         // delete groups;
00338     }
00339 
00340     /*
00341      * We got service sensitive PopupMenus in our factory
00342      * We will create one through the factory and will insert
00343      * our Separator + ShowInfo into the menu or create a new
00344      * one if the factory returns 0
00345      * PopupMenu deletion is kind of weird.
00346      * If escaped( -1 ) or any of our items were chosen we'll
00347      * delete the PopupMenu otherwise it's the responsibility of
00348      * the PopupMenu to delete itself
00349      *
00350      */
00351     else if ( ((BTListItem*)item)->type() == "service")
00352     {
00353         BTServiceItem* service = (BTServiceItem*)item;
00354         QMap<int, QString> list = service->services().classIdList();
00355         QMap<int, QString>::Iterator it = list.begin();
00356         QPopupMenu *popup =0l;
00357         if ( it != list.end() )
00358         {
00359             owarn << "Searching id " << it.key() << " " << it.data().latin1() << "" << oendl;
00360             popup = m_popHelper.find( it.key(),
00361                                       service->services(),
00362                                       (BTDeviceItem*)service->parent() );
00363         }
00364         else
00365         {
00366             owarn << "Empty" << oendl;
00367         }
00368 
00369         if ( popup == 0l )
00370         {
00371             owarn << "factory returned 0l" << oendl;
00372             popup = new QPopupMenu();
00373         }
00374         int test1 = popup->insertItem( tr("Test1:"),  2);
00375 
00376         int ret = popup->exec( point );
00377         owarn << "returned from exec() " << oendl;
00378         if ( ret == -1 )
00379         {
00380             ;
00381         }
00382         else if ( ret == test1 )
00383         {
00384             ;
00385         }
00386         delete popup;
00387     }
00388     delete menu;
00389 }
00390 
00391 
00392 void BlueBase::addServicesToDevices()
00393 {
00394     odebug << "BlueBase::addServicesToDevices()" << oendl;
00395     BTDeviceItem* item = (BTDeviceItem*) devicesView->firstChild();
00396     while ( item )
00397     {
00398         addServicesToDevice( item );
00399         item = (BTDeviceItem*) static_cast<QListViewItem*>( item )->nextSibling();
00400     }
00401 }
00402 
00407 void BlueBase::addServicesToDevice( BTDeviceItem * item )
00408 {
00409     odebug << "BlueBase::addServicesToDevice" << oendl;
00410     // row of mac adress text(3)
00411     RemoteDevice device = item->remoteDevice();
00412     m_deviceList.insert( item->mac() , item );
00413     // and some time later I get a signal foundServices( const QString& device, Services::ValueList ); back
00414     m_localDevice->searchServices( device );
00415 }
00416 
00417 
00422 void BlueBase::addServicesToDevice( const QString& device, Services::ValueList servicesList )
00423 {
00424     odebug << "BlueBase::fill services list" << oendl;
00425 
00426     QMap<QString,BTDeviceItem*>::Iterator it;
00427     BTDeviceItem* deviceItem = 0;
00428 
00429     // get the right devices which requested the search
00430     it = m_deviceList.find( device );
00431     if( it == m_deviceList.end() )
00432         return;
00433     deviceItem = it.data();
00434 
00435     // remove previous entries
00436     QList<QListViewItem> tempList;
00437     tempList.setAutoDelete( true );
00438     QListViewItem * child = deviceItem->firstChild();
00439     while( child )
00440     {
00441         tempList.append( child );
00442         child = child->nextSibling();
00443     }
00444     tempList.clear();
00445 
00446     QValueList<OpieTooth::Services>::Iterator it2;
00447     BTServiceItem* serviceItem;
00448 
00449 
00450     if (!servicesList.isEmpty() )
00451     {
00452         // add services
00453         QMap<int, QString> list;
00454         QMap<int, QString>::Iterator classIt;
00455         for( it2 = servicesList.begin(); it2 != servicesList.end(); ++it2 )
00456         {
00457             serviceItem = new BTServiceItem( deviceItem, (*it2)  );
00458             list = (*it2).classIdList();
00459             classIt = list.begin();
00460             int classId=0;
00461             if ( classIt != list.end() )
00462             {
00463                 classId = classIt.key();
00464             }
00465 
00466             serviceItem->setPixmap( 0, m_iconLoader->serviceIcon( classId ) );
00467         }
00468     }
00469     else
00470     {
00471         Services s1;
00472         s1.setServiceName( tr("no services found") );
00473         serviceItem = new BTServiceItem( deviceItem, s1 );
00474     }
00475     // now remove them from the  list
00476     m_deviceList.remove( it );
00477 }
00478 
00479 void BlueBase::addSignalStrength()
00480 {
00481 
00482     QListViewItemIterator it( connectionsView );
00483     for ( ; it.current(); ++it )
00484     {
00485         m_localDevice->signalStrength( ((BTConnectionItem*)it.current() )->connection().mac() );
00486     }
00487 
00488     QTimer::singleShot( 5000, this, SLOT( addSignalStrength() ) );
00489 }
00490 
00491 void BlueBase::addSignalStrength( const QString& mac, const QString& strength )
00492 {
00493 
00494     QListViewItemIterator it( connectionsView );
00495     for ( ; it.current(); ++it )
00496     {
00497         if( ((BTConnectionItem*)it.current())->connection().mac() == mac )
00498         {
00499             ((BTConnectionItem*)it.current() )->setSignalStrength( strength );
00500         }
00501     }
00502 }
00503 
00508 void BlueBase::addConnectedDevices()
00509 {
00510     m_localDevice->searchConnections();
00511     QTimer::singleShot( 5000, this, SLOT( addSignalStrength() ) );
00512 }
00513 
00518 void BlueBase::addConnectedDevices( ConnectionState::ValueList connectionList )
00519 {
00520 
00521     QValueList<OpieTooth::ConnectionState>::Iterator it;
00522     BTConnectionItem * connectionItem;
00523 
00524     if ( !connectionList.isEmpty() )
00525     {
00526 
00527         for (it = connectionList.begin(); it != connectionList.end(); ++it)
00528         {
00529 
00530             QListViewItemIterator it2( connectionsView );
00531             bool found = false;
00532             for ( ; it2.current(); ++it2 )
00533             {
00534                 if( ( (BTConnectionItem*)it2.current())->connection().mac() == (*it).mac() )
00535                 {
00536                     found = true;
00537                 }
00538             }
00539 
00540             if ( found == false )
00541             {
00542                 connectionItem = new BTConnectionItem( connectionsView, (*it) );
00543 
00544                 if( m_deviceList.find((*it).mac()).data() )
00545                 {
00546                     connectionItem->setName( m_deviceList.find( (*it).mac()).data()->name() );
00547                 }
00548             }
00549 
00550         }
00551 
00552         QListViewItemIterator it2( connectionsView );
00553         for ( ; it2.current(); ++it2 )
00554         {
00555             bool found = false;
00556             for (it = connectionList.begin(); it != connectionList.end(); ++it)
00557             {
00558                 if( ( ((BTConnectionItem*)it2.current())->connection().mac() ) == (*it).mac() )
00559                 {
00560                     found = true;
00561                 }
00562             }
00563 
00564             if ( !found )
00565             {
00566                 delete it2.current();
00567             }
00568 
00569         }
00570 
00571 
00572     }
00573     else
00574     {
00575         connectionsView->clear();
00576         ConnectionState con;
00577         con.setMac( tr("No connections found") );
00578         connectionItem = new BTConnectionItem( connectionsView , con );
00579     }
00580 
00581     //  recall connection search after some time
00582     QTimer::singleShot( 15000, this, SLOT( addConnectedDevices() ) );
00583 }
00584 
00585 
00590 void BlueBase::deviceActive( const RemoteDevice &device )
00591 {
00592     // search by mac, async, gets a signal back
00593     // We should have a BTDeviceItem there or where does it get added to the map -zecke
00594     m_localDevice->isAvailable( device.mac() );
00595 }
00596 
00597 
00603 void BlueBase::deviceActive( const QString& device, bool connected  )
00604 {
00605     odebug << "deviceActive slot" << oendl;
00606 
00607     QMap<QString,BTDeviceItem*>::Iterator it;
00608 
00609     it = m_deviceList.find( device );
00610     if( it == m_deviceList.end() )
00611         return;
00612 
00613     BTDeviceItem* deviceItem = it.data();
00614 
00615     if ( connected )
00616     {
00617         deviceItem->setPixmap( 1, m_onPix );
00618     }
00619     else
00620     {
00621         deviceItem->setPixmap( 1, m_offPix );
00622     }
00623     m_deviceList.remove( it );
00624 }
00625 
00626 
00630 void BlueBase::startScan()
00631 {
00632     ScanDialog *scan = new ScanDialog( this, "ScanDialog",
00633                                        true, WDestructiveClose );
00634     QObject::connect( scan, SIGNAL( selectedDevices(const QValueList<RemoteDevice>&) ),
00635                       this, SLOT( addSearchedDevices(const QValueList<RemoteDevice>&) ) );
00636 
00637     QPEApplication::showDialog( scan );
00638 }
00639 
00640 
00644 void BlueBase::setInfo()
00645 {
00646     StatusLabel->setText( status() );
00647 }
00648 
00649 
00653 BlueBase::~BlueBase()
00654 {
00655     writeSavedDevices();
00656     delete m_iconLoader;
00657 }
00658 
00659 
00666 bool BlueBase::find( const RemoteDevice& rem )
00667 {
00668     QListViewItemIterator it( devicesView );
00669     BTListItem* item;
00670     BTDeviceItem* device;
00671     for (; it.current(); ++it )
00672     {
00673         item = (BTListItem*) it.current();
00674         if ( item->typeId() != BTListItem::Device )
00675             continue;
00676 
00677         device = (BTDeviceItem*)item;
00678         if ( rem.equals( device->remoteDevice() ) )
00679             return true;
00680     }
00681     return false; // not found
00682 }

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