1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "declarativedevicesmodel.h"
10#include "declarativeadapter.h"
11#include "declarativebattery.h"
12#include "declarativedevice.h"
13#include "declarativemanager.h"
14#include "declarativemediaplayer.h"
15
16DeclarativeDevicesModel::DeclarativeDevicesModel(QObject *parent)
17 : QSortFilterProxyModel(parent)
18 , m_manager(nullptr)
19 , m_model(nullptr)
20{
21}
22
23DeclarativeManager *DeclarativeDevicesModel::manager() const
24{
25 return m_manager;
26}
27
28void DeclarativeDevicesModel::setManager(DeclarativeManager *manager)
29{
30 m_manager = manager;
31 m_model = new BluezQt::DevicesModel(m_manager, this);
32 setSourceModel(m_model);
33}
34
35QHash<int, QByteArray> DeclarativeDevicesModel::roleNames() const
36{
37 QHash<int, QByteArray> roles = QSortFilterProxyModel::roleNames();
38
39 roles[DeviceRole] = QByteArrayLiteral("Device");
40 roles[AdapterRole] = QByteArrayLiteral("Adapter");
41 roles[MediaPlayerRole] = QByteArrayLiteral("MediaPlayer");
42 roles[BatteryRole] = QByteArrayLiteral("Battery");
43
44 return roles;
45}
46
47QVariant DeclarativeDevicesModel::data(const QModelIndex &index, int role) const
48{
49 if (!m_model) {
50 return QSortFilterProxyModel::data(index, role);
51 }
52
53 BluezQt::DevicePtr dev = m_model->device(index: mapToSource(proxyIndex: index));
54 if (!dev) {
55 return QSortFilterProxyModel::data(index, role);
56 }
57
58 switch (role) {
59 case DeviceRole:
60 return QVariant::fromValue(value: m_manager->declarativeDeviceFromPtr(ptr: dev));
61 case AdapterRole:
62 return QVariant::fromValue(value: m_manager->declarativeAdapterFromPtr(ptr: dev->adapter()));
63 case MediaPlayerRole:
64 if (DeclarativeDevice *device = m_manager->declarativeDeviceFromPtr(ptr: dev)) {
65 return QVariant::fromValue(value: device->mediaPlayer());
66 }
67 break;
68 case BatteryRole:
69 if (DeclarativeDevice *device = m_manager->declarativeDeviceFromPtr(ptr: dev)) {
70 return QVariant::fromValue(value: device->battery());
71 }
72 break;
73 }
74
75 return QSortFilterProxyModel::data(index, role);
76}
77
78#include "moc_declarativedevicesmodel.cpp"
79

source code of bluez-qt/src/imports/declarativedevicesmodel.cpp