summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/bluebase.cpp
blob: b4b59c3e96317f2c5bd1c01067a935243cf0a068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * bluebase.cpp *
 * ---------------------
 *
 * copyright   : (c) 2002 by Maximilian Reiß
 * email       : max.reiss@gmx.de
 *
 */
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "bluebase.h"
#include "scandialog.h"
#include "hciconfwrapper.h"

#include <qframe.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qtabwidget.h>
#include <qscrollview.h>
#include <qvbox.h>
#include <qmessagebox.h>
#include <qcheckbox.h>
#include <qlineedit.h>
#include <qlistview.h>
#include <qdir.h>
#include <qpopupmenu.h>

#include <qpe/resource.h>
#include <qpe/config.h>

#include <remotedevice.h>


namespace OpieTooth {


    BlueBase::BlueBase( QWidget* parent,  const char* name, WFlags fl )
        : BluetoothBase( parent, name, fl ) {

        localDevice = new Manager( "hci0" );

        connect( PushButton2,  SIGNAL( clicked() ), this, SLOT(startScan() ) );
        connect( configApplyButton, SIGNAL(clicked() ), this, SLOT(applyConfigChanges() ) );
        // not good since lib is async
        //       connect( ListView2, SIGNAL( expanded ( QListViewItem* ) ),
        //                  this, SLOT( addServicesToDevice( QListViewItem * ) ) );
        connect( ListView2, SIGNAL( clicked( QListViewItem* )),
                          this, SLOT( startServiceActionClicked( QListViewItem* ) ) );
        // hehe, cast right later
        connect( ListView2, SIGNAL( rightButtonClicked( QListViewItem *, const QPoint &, int ) ),
                 this,  SLOT(startServiceActionHold( QListViewItem *, const QPoint &, int) ) );
        connect( localDevice , SIGNAL( foundServices( const QString& , Services::ValueList ) ),
                 this, SLOT( addServicesToDevice( const QString& , Services::ValueList ) ) );
        connect( localDevice, SIGNAL( available( const QString&, bool ) ),
                 this, SLOT( deviceActive( const QString& , bool ) ) );

        //Load all icons needed

        offPix = Resource::loadPixmap( "editdelete" );
        onPix = Resource::loadPixmap( "installed" );

        QPalette pal = this->palette();
        QColor col = pal.color( QPalette::Active, QColorGroup::Background );
        pal.setColor( QPalette::Active, QColorGroup::Button, col );
        pal.setColor( QPalette::Inactive, QColorGroup::Button, col );
        pal.setColor( QPalette::Normal, QColorGroup::Button, col );
        pal.setColor( QPalette::Disabled, QColorGroup::Button, col );
        this->setPalette( pal );

        setCaption( tr( "Bluetooth Manager" ) );

        readConfig();
        initGui();

        //TESTING
        ListView2->setRootIsDecorated(true);

        BTListItem *topLV2 = new BTListItem( ListView2, "Siemens S45", "", "device" );
        topLV2->setPixmap( 1, onPix );
        (void) new BTListItem( topLV2, "Serial" ,"", "service" );
        (void) new BTListItem( topLV2, "BlueNiC" , "", "service" );
        writeToHciConfig();
    }


    /**
     * Reads all options from the config file
     */
    void BlueBase::readConfig() {

        Config cfg( "bluetoothmanager" );
        cfg.setGroup( "bluezsettings" );


        deviceName = cfg.readEntry( "name" , "No name" ); // name the device should identify with
        defaultPasskey = cfg.readEntryCrypt( "passkey" , "" ); // <- hmm, look up how good the trolls did that, maybe too weak
        useEncryption = cfg.readNumEntry( "useEncryption" , 1 );
        enableAuthentification = cfg.readNumEntry( "enableAuthentification" , 1 );
        enablePagescan = cfg.readNumEntry( "enablePagescan" , 1 );
        enableInquiryscan = cfg.readNumEntry( "enableInquiryscan" , 1 );

    }

    /**
     * Writes all options to the config file
     */
    void BlueBase::writeConfig() {


        Config cfg( "bluetoothmanager" );
        cfg.setGroup( "bluezsettings" );

        cfg.writeEntry( "name" , deviceName );
        cfg.writeEntryCrypt( "passkey" , defaultPasskey );
        cfg.writeEntry( "useEncryption" , useEncryption );
        cfg.writeEntry( "enableAuthentification" , enableAuthentification );
        cfg.writeEntry( "enablePagescan" , enablePagescan );
        cfg.writeEntry( "enableInquiryscan" , enableInquiryscan );

        writeToHciConfig();
    }

    void BlueBase::writeToHciConfig() {

        HciConfWrapper *hciconf = new HciConfWrapper( "/tmp/hcid.conf" );
        hciconf->setPinHelper( "/bin/QtPalmtop/bin/blue-pin" );


        //    hciconf->setPinHelper( "/bin/QtPalmtop/bin/blue-pin" );

        hciconf->setName( deviceName );

        if ( useEncryption == 1) {
            hciconf->setEncrypt( true );
        } else {
            hciconf->setEncrypt( false );
        }


        if ( enableAuthentification == 1) {
            hciconf->setAuth( true );
        } else {
            hciconf->setAuth( false );
        }

        if ( enablePagescan  == 1) {
            hciconf->setPscan( true );
        } else {
            hciconf->setPscan( false );
        }

        if ( enableInquiryscan  == 1) {
            hciconf->setIscan( true );
        } else {
            hciconf->setIscan( false );
        }
    }


    /**
     * Read the list of allready known devices
     *
     */
    void BlueBase::readSavedDevices() {

        QList<RemoteDevice> *loadedDevices = new QList<RemoteDevice>;

        QDir deviceListSave( QDir::homeDirPath() + "/Settings/bluetooth/");
        // list of .conf files
        QStringList devicesFileList = deviceListSave.entryList();

        // cut .conf of to get the mac and also read the name entry in it.

        for ( QStringList::Iterator it = devicesFileList.begin(); it != devicesFileList.end(); ++it ) {

            QString name;
            QString mac;
            qDebug((*it).latin1() );
            Config conf((*it));
            conf.setGroup("Info");
            name = conf.readEntry("name", "Error");
            qDebug("MAC: " + mac);
            qDebug("NAME: " + name);
            RemoteDevice currentDevice = RemoteDevice( mac , name  );
            loadedDevices->append( &currentDevice );
        }
        addSearchedDevices( *loadedDevices );
    }

    /**
     * Write the list of allready known devices
     *
     */
    void BlueBase::writeSavedDevices() {

        QListViewItemIterator it( ListView2 );

        for ( ; it.current(); ++it ) {

            // seperate config file for each device, to store more information in future.
// TO FIX: BTLISTITEM!!!

            //  Config conf( QDir::homeDirPath() + "/Settings/bluetooth/" + (((BTListItem)it.current())->mac()) + ".conf", Config::File );
            // conf.setGroup( "Info" );
            //  conf.writeEntry( "name", it.current()->name() );
        }
    }


    /**
     * Set up the gui
     */
    void BlueBase::initGui() {

        StatusLabel->setText( getStatus() ); // maybe move it to getStatus()

        cryptCheckBox->setChecked( useEncryption );
        authCheckBox->setChecked( enableAuthentification );
        pagescanCheckBox->setChecked( enablePagescan );
        inquiryscanCheckBox->setChecked( enableInquiryscan );
        deviceNameLine->setText( deviceName );
        passkeyLine->setText( defaultPasskey );
        // set info tab
        setInfo();
    }


    /**
     * Get the status informations and returns it
     * @return QString the status informations gathered
     */
    QString BlueBase::getStatus(){

        QString infoString = tr( "<b>Device name : </b> Ipaq" );
        infoString += QString( "<br><b>" + tr( "MAC adress: " ) +"</b> No idea" );
        infoString += QString( "<br><b>" + tr( "Class" ) + "</b> PDA" );

        return (infoString);
    }


    /**
     * Read the current values from the gui and invoke writeConfig()
     */
    void BlueBase::applyConfigChanges() {

        deviceName =  deviceNameLine->text();
        defaultPasskey = passkeyLine->text();
        useEncryption = cryptCheckBox->isChecked();
        enableAuthentification = authCheckBox->isChecked();
        enablePagescan = pagescanCheckBox->isChecked();
        enableInquiryscan = inquiryscanCheckBox->isChecked();

        writeConfig();


        QMessageBox*  box = new QMessageBox( this, "Test" );
        box->setText( tr( "Changes applied" ) );
        box->show();
  // falls nötig hcid killhupen - die funktionalität adden
    }


    /**
     * Add fresh found devices from scan dialog to the listing
     *
     */
    void BlueBase::addSearchedDevices( QList<RemoteDevice> &newDevices ) {

        BTListItem * deviceItem;

        QListIterator<RemoteDevice> it( newDevices );

        for( ; it.current() ; ++it ) {


            RemoteDevice *dev = it.current();
            deviceItem = new BTListItem( ListView2 , dev->name(), dev->mac(), "device" );
            deviceItem->setExpandable ( true );

            // look if device is avail. atm, async
            deviceActive( dev );

            // move into the c'tor
            // deviceItem->setMac( dev->mac() );
            // what kind of entry is it.
            //deviceItem->setType( "device");

            // ggf auch hier?
            addServicesToDevice( deviceItem );
        }
    }


    /**
     * Action that is toggled on entrys on click
     */
    void BlueBase::startServiceActionClicked( QListViewItem *item ) {


    }

    /**
     * Action that are toggled on hold (mostly QPopups i guess)
     */
    void BlueBase::startServiceActionHold( QListViewItem * item, const QPoint & point, int column ) {

          QPopupMenu *menu = new QPopupMenu();

          int ret=0;


          //QSize s = menu->sizeHint ( );

          if ( ((BTListItem*)item)->type() == "device") {

              QPopupMenu *groups = new QPopupMenu();

              menu->insertItem( tr("rescan sevices:"),  0);
              menu->insertItem( tr("to group"), groups ,  1);
              menu->insertItem( tr("delete"),  2);


              ret = menu->exec( point  , 0);

              switch(ret) {
              case 0:
                  break;
              case 1:
                  break;
              case 2:
                  // delete childs too
                  delete item;
                  break;
              }
              delete groups;

          } else if ( ((BTListItem*)item)->type() == "service") {
              menu->insertItem( tr("Test1:"),  0);
              menu->insertItem( tr("connect"), 1);
              menu->insertItem( tr("delete"),  2);

              ret = menu->exec( point  , 0);

              switch(ret) {
              case 0:
                  break;
              case 1:
                  break;
              case 2:
                  // delete childs too
                  delete item;
                  break;
              }
          }

          delete menu;
    }

    /**
     * Search and display avail. services for a device (on expand from device listing)
     *
     */
    void BlueBase::addServicesToDevice( BTListItem * item ) {

        qDebug("addServicesToDevice");
        // row of mac adress text(3)
        RemoteDevice *device = new RemoteDevice( item->mac(),  item->name() );

        deviceList.insert( item->mac() ,  item );

        // and some time later I get a signal foundServices( const QString& device, Services::ValueList ); back
        localDevice->searchServices( *device );

        // delete
    }


    /**
     * Overloaded. This one it the one that is connected to the foundServices signal
     * @param device the mac address of the remote device
     * @param servicesList the list with the service the device has.
     */
    void BlueBase::addServicesToDevice( const QString& device, Services::ValueList servicesList ) {

        qDebug("fill services list");

        QMap<QString,BTListItem*>::Iterator it;

        BTListItem* deviceItem;

        // get the right devices which requested the search
        for( it = deviceList.begin(); it != deviceList.end(); ++it ) {
            if ( it.key() == device ) {
                deviceItem = it.data();
            }
        }


        // empty entries
        //  QListViewItem * myChild = deviceItem->firstChild();
        //QList<QListViewItem*> tmpList;
        //while( myChild ) {
        //    tmpList.append(myChild);
        //    myChild = myChild->nextSibling();
        // }


        QValueList<OpieTooth::Services>::Iterator it2;

        BTListItem * serviceItem;

        if (!servicesList.isEmpty() ) {
            // add services
            for( it2 = servicesList.begin(); it2 != servicesList.end(); ++it2 ) {
                serviceItem = new BTListItem( deviceItem  ,  (*it2).serviceName() , "" , "service"  );
                //serviceItem->setText(4, "service");
            }
        } else {
            serviceItem = new BTListItem( deviceItem  , tr("no services found"), "" , "service"  );
            //serviceItem->setText(4, "service");
        }
    }


    /**
     * Add the existing connections (pairs) to the connections tab.
     *
     */
    void BlueBase::addConnectedDevices() {


        //mac address

    }

    /**
     * Find out if a device can  currently be reached
     */
    void BlueBase::deviceActive( RemoteDevice *device ) {

        // search by mac, async, gets a signal back
        localDevice->isAvailable( device->mac() );
    }

    /**
     * The signal catcher. Set the avail. status on device.
     * @param device - the mac address
     * @param connected - if it is avail. or not
     */
    void BlueBase::deviceActive( const QString& device, bool connected  ) {

        qDebug("deviceActive slot");

        QMap<QString,BTListItem*>::Iterator it;

        BTListItem* deviceItem;

        // get the right devices which requested the search
        for( it = deviceList.begin(); it != deviceList.end(); ++it ) {
            if ( it.key() == device ) {
                deviceItem = it.data();
            }
        }

        if ( connected ) {
            deviceItem->setPixmap( 1, onPix );
        } else {
            deviceItem->setPixmap( 1, offPix );
        }

    }



    /**
     * Open the "scan for devices"  dialog
     */
    void BlueBase::startScan() {

        ScanDialog *scan = new ScanDialog( this, "", true);
        QObject::connect( scan, SIGNAL( selectedDevices( QList<RemoteDevice>& ) ),
                         this, SLOT( addSearchedDevices( QList<RemoteDevice>& ) ) );

        scan->showMaximized();
    }


    /**
     * Set the informations about the local device in information Tab
     */
    void BlueBase::setInfo() {
        StatusLabel->setText( getStatus() );
    }

    /**
     * Decontructor
     */
    BlueBase::~BlueBase() {
        writeSavedDevices();
    }
}