File: bluetooth.cpp

package info (click to toggle)
lomiri-system-settings 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,484 kB
  • sloc: cpp: 15,892; python: 5,994; xml: 362; javascript: 80; makefile: 46; sh: 5
file content (190 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2013-2015 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 * Charles Kerr <charles.kerr@canonical.com>
 *
 */

#include "bluetooth.h"

#include <QQmlEngine>

#include "agent.h"
#include "dbus-shared.h"

Bluetooth::Bluetooth(QObject *parent):
    Bluetooth(QDBusConnection::systemBus(), parent)
{
}

Bluetooth::Bluetooth(const QDBusConnection &dbus, QObject *parent):
    QObject(parent),
    m_dbus(dbus),
    m_devices(m_dbus),
    m_agent(m_dbus, m_devices)
{
    // export our Agent to handle pairing requests
    new BluezAgent1Adaptor(&m_agent);
    if(!m_dbus.registerObject(DBUS_ADAPTER_AGENT_PATH, &m_agent))
        qCritical() << "Couldn't register agent at" << DBUS_ADAPTER_AGENT_PATH;

    m_connectedDevices.filterOnTrusted(true);                                       
    m_connectedDevices.setSourceModel(&m_devices);

    m_disconnectedDevices.filterOnConnections(Device::Connection::Disconnected);
    m_disconnectedDevices.filterOnTrusted(false);
    m_disconnectedDevices.setSourceModel(&m_devices);

    QObject::connect(&m_devices, SIGNAL(poweredChanged(bool)),
                     this, SIGNAL(poweredChanged(bool)));

    QObject::connect(&m_devices, SIGNAL(discoveringChanged(bool)),
                     this, SIGNAL(discoveringChanged(bool)));

    QObject::connect(&m_devices, SIGNAL(discoverableChanged(bool)),
                     this, SIGNAL(discoverableChanged(bool)));

    QObject::connect(&m_devices, SIGNAL(devicePairingDone(Device*,bool)),
                     this, SIGNAL(devicePairingDone(Device*,bool)));

    QObject::connect(&m_devices, SIGNAL(adapterNameChanged()),
                     this, SIGNAL(adapterNameChanged()));

    QObject::connect(&m_devices, SIGNAL(adapterAddressChanged()),
                     this, SIGNAL(adapterAddressChanged()));
}

void Bluetooth::setSelectedDevice(const QString &address)
{
    if (!m_selectedDevice || (m_selectedDevice->getAddress() != address)) {
        m_selectedDevice = m_devices.getDeviceFromAddress(address);
        Q_EMIT(selectedDeviceChanged());
    }
}

void Bluetooth::resetSelectedDevice()
{
    m_selectedDevice.reset(0);
    Q_EMIT(selectedDeviceChanged());
}

void Bluetooth::trySetDiscoverable(bool discoverable)
{
    m_devices.trySetDiscoverable(discoverable);
}

void Bluetooth::startDiscovery()
{
    m_devices.startDiscovery();
}

void Bluetooth::stopDiscovery()
{
    m_devices.stopDiscovery();
}

void Bluetooth::toggleDiscovery()
{
    m_devices.toggleDiscovery();
}

void Bluetooth::blockDiscovery()
{
    m_devices.blockDiscovery();
}

void Bluetooth::unblockDiscovery()
{
    m_devices.unblockDiscovery();
}

/***
****
***/

Device * Bluetooth::getSelectedDevice()
{
    if (m_selectedDevice) {
        auto ret = m_selectedDevice.data();
        QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
        return ret;
    }

    return nullptr;
}

Agent * Bluetooth::getAgent()
{
    auto ret = &m_agent;
    QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
    return ret;
}

QAbstractItemModel * Bluetooth::getConnectedDevices()
{
    auto ret = &m_connectedDevices;
    QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
    return ret;
}

QAbstractItemModel * Bluetooth::getDisconnectedDevices()
{
    auto ret = &m_disconnectedDevices;
    QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
    return ret;
}

/***
****
***/

void Bluetooth::disconnectDevice()
{
    if (!m_selectedDevice)
        return;

    m_selectedDevice->disconnect();
}

void Bluetooth::connectDevice(const QString &address)
{
    auto device = m_devices.getDeviceFromAddress(address);

    if (!device) {
        qWarning() << "No device to connect.";
        return;
    }

    if (!device->isPaired()) {
        device->setConnectAfterPairing(true);
        device->pair();
    }
    else {
        device->connect();
    }
}

void Bluetooth::removeDevice()
{
    if (!m_selectedDevice) {
        qWarning() << "No selected device to remove.";
        return;
    }

    QString path = m_selectedDevice->getPath();
    m_devices.removeDevice(path);
}