~ubuntu-branches/ubuntu/maverick/bluedevil/maverick-proposed

« back to all changes in this revision

Viewing changes to src/wizard/pages/discoverpage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2010-08-07 09:04:19 UTC
  • Revision ID: james.westby@ubuntu.com-20100807090419-68k54ucso2htcf5z
Tags: upstream-1.0~rc2
ImportĀ upstreamĀ versionĀ 1.0~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2010 Alex Fiestas <alex@eyeos.org>
 
3
    Copyright (C) 2010 UFO Coders <info@ufocoders.com>
 
4
 
 
5
    This program is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation, either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
 
 
20
#include "discoverpage.h"
 
21
#include "ui_discover.h"
 
22
#include "../bluewizard.h"
 
23
 
 
24
#include <QListWidgetItem>
 
25
#include <QListView>
 
26
#include <QLabel>
 
27
#include <QTimer>
 
28
 
 
29
#include <KDebug>
 
30
 
 
31
#include <bluedevil/bluedevil.h>
 
32
 
 
33
using namespace BlueDevil;
 
34
 
 
35
DiscoverPage::DiscoverPage(QWidget* parent): QWizardPage(parent), m_counter(0), m_wizard(0)
 
36
{
 
37
    setTitle("Discover Devices");
 
38
    setupUi(this);
 
39
 
 
40
    m_timer = new QTimer();
 
41
    m_timer->setInterval(100);
 
42
 
 
43
    connect(m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
 
44
    connect(scanBtn, SIGNAL(clicked()), this, SLOT(startScan()));
 
45
 
 
46
    connect(deviceList, SIGNAL(itemActivated(QListWidgetItem*)), this,
 
47
            SLOT(itemSelected(QListWidgetItem*)));
 
48
}
 
49
 
 
50
DiscoverPage::~DiscoverPage()
 
51
{
 
52
    delete m_timer;
 
53
}
 
54
 
 
55
void DiscoverPage::initializePage()
 
56
{
 
57
    kDebug() << "Initialize Page";
 
58
    if (!m_wizard) {
 
59
        kDebug() << "First time in the page";
 
60
        m_wizard = static_cast<BlueWizard* >(wizard());
 
61
        connect(Manager::self()->defaultAdapter(), SIGNAL(deviceFound(Device*)), this,
 
62
            SLOT(deviceFound(Device*)));
 
63
    }
 
64
 
 
65
    connect(m_wizard, SIGNAL(currentIdChanged(int)), this, SLOT(leavePage(int)));
 
66
    startScan();
 
67
}
 
68
 
 
69
void DiscoverPage::leavePage(int id)
 
70
{
 
71
    if (id == 2) {
 
72
        progressBar->setValue(0);
 
73
        cleanupPage();
 
74
    }
 
75
}
 
76
 
 
77
void DiscoverPage::cleanupPage()
 
78
{
 
79
    stopScan();
 
80
}
 
81
 
 
82
bool DiscoverPage::isComplete() const
 
83
{
 
84
    if (m_wizard->deviceAddress().isEmpty()) {
 
85
        return false;
 
86
    }
 
87
    return true;
 
88
}
 
89
 
 
90
void DiscoverPage::startScan()
 
91
{
 
92
    m_counter = 0;
 
93
    progressBar->setValue(0);
 
94
    deviceList->clear();
 
95
    stopScan();
 
96
 
 
97
    Manager::self()->defaultAdapter()->startDiscovery();
 
98
    m_timer->start();
 
99
}
 
100
 
 
101
void DiscoverPage::stopScan()
 
102
{
 
103
    m_counter = 0;
 
104
    m_timer->stop();
 
105
    Manager::self()->defaultAdapter()->stopDiscovery();
 
106
}
 
107
 
 
108
void DiscoverPage::deviceFound(Device* device)
 
109
{
 
110
    QString name = device->alias();
 
111
    if (device->alias() != device->name() && !device->name().isEmpty()) {
 
112
        name.append(" ("+device->name()+")");
 
113
    }
 
114
 
 
115
    QString icon = device->icon();
 
116
    if (icon.isEmpty()) {
 
117
        icon.append("preferences-system-bluetooth");
 
118
    }
 
119
 
 
120
    QListWidgetItem *item = new QListWidgetItem(KIcon(icon), name, deviceList);
 
121
 
 
122
    item->setData(Qt::UserRole, device->address());
 
123
    deviceList->addItem(item);
 
124
}
 
125
 
 
126
void DiscoverPage::timeout()
 
127
{
 
128
    m_counter ++;
 
129
    progressBar->setValue(m_counter);
 
130
 
 
131
    if (m_counter == 100) {
 
132
        stopScan();
 
133
    }
 
134
}
 
135
 
 
136
void DiscoverPage::itemSelected(QListWidgetItem* item)
 
137
{
 
138
    m_wizard->setDeviceAddress(item->data(Qt::UserRole).toByteArray());
 
139
    emit completeChanged();
 
140
}
 
141
 
 
142
int DiscoverPage::nextId() const
 
143
{
 
144
    if (m_wizard) {
 
145
        if (!m_wizard->deviceAddress().isEmpty()) {
 
146
            Device *device = Manager::self()->defaultAdapter()->deviceForAddress(m_wizard->deviceAddress());
 
147
            if (device->isPaired()) {
 
148
                kDebug() << "Device is paired, jumping";
 
149
                return BlueWizard::Services;
 
150
            }
 
151
        }
 
152
    }
 
153
    return BlueWizard::Pin;
 
154
}