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
|
/*****************************************************************************
* This file is part of the BlueDevil project *
* *
* Copyright (C) 2010 Rafael Fernández López <ereslibre@kde.org> *
* Copyright (C) 2010 UFO Coders <info@ufocoders.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public License *
* along with this library; see the file COPYING.LIB. If not, write to *
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301, USA. *
*****************************************************************************/
#include "bluedevilmanager.h"
#include "bluedeviladapter.h"
#include "bluedevil/bluezmanager.h"
#include <QtCore/QHash>
namespace BlueDevil {
static Manager *instance = 0;
class Manager::Private
{
public:
Private(Manager *q);
~Private();
void _k_adapterAdded(const QDBusObjectPath &objectPath);
void _k_adapterRemoved(const QDBusObjectPath &objectPath);
void _k_defaultAdapterChanged(const QDBusObjectPath &objectPath);
void _k_propertyChanged(const QString &property, const QDBusVariant &value);
OrgBluezManagerInterface *m_bluezManagerInterface;
Adapter *m_defaultAdapter;
QHash<QString, Adapter*> m_adaptersHash;
Manager *const m_q;
};
Manager::Private::Private(Manager *q)
: m_defaultAdapter(0)
, m_q(q)
{
}
Manager::Private::~Private()
{
delete m_bluezManagerInterface;
}
void Manager::Private::_k_adapterAdded(const QDBusObjectPath &objectPath)
{
Adapter *const adapter = new Adapter(objectPath.path(), m_q);
m_adaptersHash.insert(objectPath.path(), adapter);
if (!m_defaultAdapter) {
m_defaultAdapter = adapter;
}
emit m_q->adapterAdded(adapter);
}
void Manager::Private::_k_adapterRemoved(const QDBusObjectPath &objectPath)
{
Adapter *const adapter = m_adaptersHash.take(objectPath.path()); // return and remove it from the hash
if (m_adaptersHash.isEmpty()) {
m_defaultAdapter = 0;
}
if (adapter) {
emit m_q->adapterRemoved(adapter);
delete adapter;
}
if (m_adaptersHash.isEmpty()) {
emit m_q->defaultAdapterChanged(0);
emit m_q->allAdaptersRemoved();
}
}
void Manager::Private::_k_defaultAdapterChanged(const QDBusObjectPath &objectPath)
{
Adapter *adapter = m_adaptersHash[objectPath.path()];
if (!adapter) {
adapter = new Adapter(objectPath.path(), m_q);
m_adaptersHash.insert(objectPath.path(), adapter);
}
m_defaultAdapter = adapter;
emit m_q->defaultAdapterChanged(adapter);
}
void Manager::Private::_k_propertyChanged(const QString &property, const QDBusVariant &value)
{
Q_UNUSED(property)
Q_UNUSED(value)
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Manager::Manager(QObject *parent)
: QObject(parent)
, d(new Private(this))
{
d->m_bluezManagerInterface = new OrgBluezManagerInterface("org.bluez", "/", QDBusConnection::systemBus(), this);
connect(d->m_bluezManagerInterface, SIGNAL(AdapterAdded(QDBusObjectPath)),
this, SLOT(_k_adapterAdded(QDBusObjectPath)));
connect(d->m_bluezManagerInterface, SIGNAL(AdapterRemoved(QDBusObjectPath)),
this, SLOT(_k_adapterRemoved(QDBusObjectPath)));
connect(d->m_bluezManagerInterface, SIGNAL(DefaultAdapterChanged(QDBusObjectPath)),
this, SLOT(_k_defaultAdapterChanged(QDBusObjectPath)));
connect(d->m_bluezManagerInterface, SIGNAL(PropertyChanged(QString,QDBusVariant)),
this, SLOT(_k_propertyChanged(QString,QDBusVariant)));
if (QDBusConnection::systemBus().isConnected()) {
const QString adapterPath = d->m_bluezManagerInterface->DefaultAdapter().value().path();
if (!adapterPath.isEmpty()) {
d->m_defaultAdapter = new Adapter(adapterPath, this);
d->m_adaptersHash.insert(adapterPath, d->m_defaultAdapter);
}
const QVariantMap properties = d->m_bluezManagerInterface->GetProperties().value();
const QList<QDBusObjectPath> adapters = qdbus_cast<QList<QDBusObjectPath> >(properties["Adapters"].value<QDBusArgument>());
Q_FOREACH (const QDBusObjectPath &path, adapters) {
Adapter *const adapter = new Adapter(path.path(), this);
d->m_adaptersHash.insert(path.path(), adapter);
}
}
}
Manager::~Manager()
{
delete d;
}
Manager* Manager::self()
{
if (!instance) {
instance = new Manager;
}
return instance;
}
void Manager::release()
{
delete instance;
instance = 0;
}
Adapter *Manager::defaultAdapter()
{
if (!QDBusConnection::systemBus().isConnected()) {
return 0;
}
if (!d->m_defaultAdapter) {
const QString adapterPath = d->m_bluezManagerInterface->DefaultAdapter().value().path();
if (!adapterPath.isEmpty()) {
d->m_defaultAdapter = new Adapter(adapterPath, this);
d->m_adaptersHash.insert(adapterPath, d->m_defaultAdapter);
}
}
return d->m_defaultAdapter;
}
QList<Adapter*> Manager::adapters() const
{
if (!QDBusConnection::systemBus().isConnected()) {
return QList<Adapter*>();
}
return d->m_adaptersHash.values();
}
bool Manager::isBluetoothOperational() const
{
return QDBusConnection::systemBus().isConnected() && d->m_defaultAdapter;
}
}
#include "bluedevilmanager.moc"
|