summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/scandialog.cpp
blob: 160e8ddfde7faedcb280edc6d27667603a51d73b (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
/* main.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 "scandialog.h"

#include <qheader.h>
#include <qlistview.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qprogressbar.h>
#include <qlist.h>

#include <manager.h>
#include <device.h>

#include <opie2/odebug.h>
using namespace Opie::Core;


namespace OpieTooth {

#include <remotedevice.h>

/**
 */
    ScanDialog::ScanDialog( QWidget* parent,  const char* name, bool modal, WFlags fl )
        : QDialog( parent, name, modal, fl ) {

        setCaption( tr( "Scan for devices" ) );

        Layout11 = new QVBoxLayout( this );
        Layout11->setSpacing( 6 );
        Layout11->setMargin( 0 );

        progress = new QProgressBar( this, "progbar");
        progress->setTotalSteps(20);

        StartStopButton = new QPushButton( this, "StartButton" );
        StartStopButton->setText( tr( "Start scan" ) );

        serviceView = new QListView( this, "serviceView" );

        //serviceView->addColumn( tr( "Add" ) );
        serviceView->addColumn( tr( "Add Device" ) );
        //serviceView->addColumn( tr( "Type" ) );

        Layout11->addWidget( serviceView );
        Layout11->addWidget( progress );
        Layout11->addWidget( StartStopButton );

        localDevice = new Manager( "hci0" );

        connect( StartStopButton, SIGNAL( clicked() ), this, SLOT( startSearch() ) );
        connect( localDevice, SIGNAL( foundDevices(const QString&,RemoteDevice::ValueList) ),
                 this, SLOT( fillList(const QString&,RemoteDevice::ValueList) ) ) ;

        progressStat = 0;
        m_search = false;
    }

// hack, make cleaner later
    void ScanDialog::progressTimer() {

        progressStat++;
        if ( progressStat++ < 20 && m_search ) {
            QTimer::singleShot( 2000, this, SLOT( progressTimer() ) );
            progress->setProgress( progressStat++ );
        }
    }

    void ScanDialog::accept() {
        emitToManager();
        QDialog::accept();
    }


    void ScanDialog::startSearch() {
        if ( m_search ) {
            stopSearch();
            return;
        }
        m_search = true;
        progress->setProgress(0);
        progressStat = 0;

        // empty list before a new scan
        serviceView->clear();

        progressTimer();
        // when finished, it emmite foundDevices()
        // checken ob initialisiert , qcop ans applet.
        StartStopButton->setText( tr( "Stop scan" ) );

        localDevice->searchDevices();

    }

    void ScanDialog::stopSearch() {
        m_search = true;
    }

    void ScanDialog::fillList(const QString&, RemoteDevice::ValueList deviceList) {
        progress->setProgress(0);
        progressStat = 0;
        QCheckListItem * deviceItem;

        RemoteDevice::ValueList::Iterator it;
        for( it = deviceList.begin(); it != deviceList.end(); ++it ) {

            deviceItem = new QCheckListItem( serviceView, (*it).name(),  QCheckListItem::CheckBox );
            deviceItem->setText( 1, (*it).mac() );
        }
        m_search = false;
        StartStopButton->setText( tr( "Start scan" ) );
    }

/**
 * Iterates trough the items, and collects the checked items.
 * Then it emits it, so the manager can connect to the signal to fill the listing.
 */
    void ScanDialog::emitToManager() {

        if (!serviceView) {
            return;
        }

        QValueList<RemoteDevice> deviceList;

        QListViewItemIterator it( serviceView );
        for ( ; it.current(); ++it ) {
            if ( ( (QCheckListItem*)it.current() )->isOn() ) {
                RemoteDevice device(  it.current()->text(1), it.current()->text(0) );
                deviceList.append( device );
            }
        }
        emit selectedDevices( deviceList );
    }

/**
 * Cleanup
 */
    ScanDialog::~ScanDialog() {
        owarn << "delete scan dialog" << oendl; 
        delete localDevice;
    }
}