00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "bluezapplet.h"
00031
00032
00033 #include <opie2/otaskbarapplet.h>
00034 #include <opie2/odevice.h>
00035 #include <opie2/odebug.h>
00036 #include <opie2/oresource.h>
00037 #include <qpe/applnk.h>
00038 #include <qpe/qcopenvelope_qws.h>
00039 #include <qpe/config.h>
00040 using namespace Opie::Core;
00041
00042
00043 #include <qapplication.h>
00044 #include <qpoint.h>
00045 #include <qpainter.h>
00046 #include <qlayout.h>
00047 #include <qframe.h>
00048 #include <qpixmap.h>
00049 #include <qstring.h>
00050 #include <qtimer.h>
00051 #include <qpopupmenu.h>
00052
00053
00054 #include <device.h>
00055
00056 namespace OpieTooth {
00057
00058 BluezApplet::BluezApplet( QWidget *parent, const char *name ) : QWidget( parent, name ) {
00059 setFixedHeight( AppLnk::smallIconSize() );
00060 setFixedWidth( AppLnk::smallIconSize() );
00061 bluezOnPixmap = OResource::loadImage( "bluetoothapplet/bluezon", OResource::SmallIcon );
00062 bluezOffPixmap = OResource::loadImage( "bluetoothapplet/bluezoff", Opie::Core::OResource::SmallIcon );
00063 bluezDiscoveryOnPixmap = OResource::loadImage( "bluetoothapplet/bluezondiscovery", Opie::Core::OResource::SmallIcon );
00064 startTimer(4000);
00065 btDevice = 0;
00066 btManager = 0;
00067 bluezactive = false;
00068 bluezDiscoveryActive = false;
00069
00070
00071 QCopChannel* chan = new QCopChannel("QPE/Bluetooth", this );
00072 connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ),
00073 this, SLOT(slotMessage(const QCString&,const QByteArray&) ) );
00074 }
00075
00076 BluezApplet::~BluezApplet() {
00077 if ( btDevice ) {
00078 delete btDevice;
00079 }
00080 if ( btManager ) {
00081 delete btManager;
00082 }
00083 }
00084
00085 int BluezApplet::position()
00086 {
00087 return 6;
00088 }
00089
00090
00091 bool BluezApplet::checkBluezStatus() {
00092 if (btDevice) {
00093 if (btDevice->isLoaded() ) {
00094 return true;
00095 } else {
00096 return false;
00097 }
00098 } else {
00099 return false;
00100 }
00101 }
00102
00103 int BluezApplet::setBluezStatus(int c) {
00104
00105 if ( c == 1 ) {
00106 switch ( ODevice::inst()->model() ) {
00107 case Model_iPAQ_H39xx:
00108 btDevice = new Device( "/dev/tts/1", "bcsp", "921600" );
00109 break;
00110
00111 case Model_iPAQ_H5xxx:
00112 btDevice = new Device( "/dev/tts/1", "any", "921600" );
00113 break;
00114
00115 case Model_MyPal_716:
00116 btDevice = new Device( "/dev/ttyS1", "bcsp", "921600" );
00117 break;
00118
00119 default:
00120 btDevice = new Device( "/dev/ttySB0", "bcsp", "230400" );
00121 break;
00122 }
00123 } else {
00124 if ( btDevice ) {
00125 delete btDevice;
00126 btDevice = 0;
00127 }
00128 }
00129 return 0;
00130 }
00131
00132 int BluezApplet::checkBluezDiscoveryStatus() {
00133 return bluezDiscoveryActive;
00134 }
00135
00136 int BluezApplet::setBluezDiscoveryStatus(int d) {
00137 return bluezDiscoveryActive=d;
00138 }
00139
00140
00141
00142 void BluezApplet::slotMessage( const QCString& str, const QByteArray& )
00143 {
00144 if ( str == "enableBluetooth()") {
00145 if (!checkBluezStatus()) {
00146 setBluezStatus(1);
00147 }
00148 } else if ( str == "disableBluetooth()") {
00149 if (checkBluezStatus()) {
00150
00151 }
00152 } else if ( str == "listDevices()") {
00153 if (!btManager)
00154 {
00155 btManager = new Manager("hci0");
00156 connect( btManager, SIGNAL( foundDevices(const QString&,RemoteDevice::ValueList) ),
00157 this, SLOT( fillList(const QString&,RemoteDevice::ValueList) ) ) ;
00158 }
00159
00160 btManager->searchDevices();
00161 }
00162 }
00163
00164
00165 void BluezApplet::fillList(const QString&, RemoteDevice::ValueList deviceList)
00166 {
00167 QCopEnvelope e("QPE/BluetoothBack", "devices(QStringMap)");
00168
00169 QStringList list;
00170 QMap<QString, QString> btmap;
00171
00172 RemoteDevice::ValueList::Iterator it;
00173 for( it = deviceList.begin(); it != deviceList.end(); ++it )
00174 {
00175 btmap[(*it).name()] = (*it).mac();
00176 }
00177
00178 e << btmap;
00179 }
00180
00181 void BluezApplet::mousePressEvent( QMouseEvent *) {
00182
00183 QPopupMenu *menu = new QPopupMenu();
00184 QPopupMenu *signal = new QPopupMenu();
00185 int ret=0;
00186
00187
00188 timerEvent( 0 );
00189
00190
00191 if (bluezactive) {
00192 menu->insertItem( tr("Disable Bluetooth"), 0 );
00193 } else {
00194 menu->insertItem( tr("Enable Bluetooth"), 1 );
00195 }
00196
00197 menu->insertItem( tr("Launch manager"), 2 );
00198
00199 menu->insertSeparator(6);
00200
00201
00202
00203 if (bluezDiscoveryActive) {
00204 menu->insertItem( tr("Disable discovery"), 3 );
00205 } else {
00206 menu->insertItem( tr("Enable discovery"), 4 );
00207 }
00208
00209
00210 QPoint p = mapToGlobal( QPoint(1, -menu->sizeHint().height()-1) );
00211 ret = menu->exec(p, 0);
00212
00213 switch(ret) {
00214 case 0:
00215 setBluezStatus(0);
00216 timerEvent( 0 );
00217 break;
00218 case 1:
00219 setBluezStatus(1);
00220 timerEvent( 0 );
00221 break;
00222 case 2:
00223
00224 launchManager();
00225 timerEvent( 0 );
00226 break;
00227 case 3:
00228 setBluezDiscoveryStatus(0);
00229 timerEvent( 0 );
00230 break;
00231 case 4:
00232 setBluezDiscoveryStatus(1);
00233 timerEvent(0 );
00234 break;
00235
00236
00237 }
00238
00239 delete signal;
00240 delete menu;
00241 }
00242
00243
00247 void BluezApplet::launchManager() {
00248 QCopEnvelope e("QPE/System", "execute(QString)");
00249 e << QString("bluetooth-manager");
00250 }
00251
00256 void BluezApplet::timerEvent( QTimerEvent * ) {
00257 bool oldactive = bluezactive;
00258 int olddiscovery = bluezDiscoveryActive;
00259
00260 bluezactive = checkBluezStatus();
00261 bluezDiscoveryActive = checkBluezDiscoveryStatus();
00262
00263 if ((bluezactive != oldactive) || (bluezDiscoveryActive != olddiscovery)) {
00264 update();
00265 }
00266 }
00267
00272 void BluezApplet::paintEvent( QPaintEvent* ) {
00273 QPainter p(this);
00274 odebug << "paint bluetooth pixmap" << oendl;
00275
00276 if (bluezactive > 0) {
00277 p.drawPixmap( 0, 0, bluezOnPixmap );
00278 } else {
00279 p.drawPixmap( 0, 0, bluezOffPixmap );
00280 }
00281
00282 if (bluezDiscoveryActive > 0) {
00283 p.drawPixmap( 0, 0, bluezDiscoveryOnPixmap );
00284 }
00285 }
00286 };
00287
00288 EXPORT_OPIE_APPLET_v1( OpieTooth::BluezApplet )
00289