aboutsummaryrefslogtreecommitdiffstats
path: root/src/ivimedia/qivimediadevicediscoverymodel.cpp
blob: 240925e16b155c4e81765dd16be817c395bf2580 (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtIvi module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qivimediadevicediscoverymodel.h"
#include "qivimediadevicediscoverymodel_p.h"

#include <QtIviCore/QIviServiceObject>
#include <QtIviMedia/QIviMediaDevice>
#include <QtDebug>

QT_BEGIN_NAMESPACE

QIviMediaDeviceDiscoveryModelPrivate::QIviMediaDeviceDiscoveryModelPrivate(const QString &interface, QIviMediaDeviceDiscoveryModel *parent)
    : QIviAbstractFeatureListModelPrivate(interface, parent)
    , q_ptr(parent)
{
}

void QIviMediaDeviceDiscoveryModelPrivate::initialize()
{
    QIviAbstractFeatureListModelPrivate::initialize();
    Q_Q(QIviMediaDeviceDiscoveryModel);

    QObject::connect(q, &QAbstractListModel::rowsInserted,
                     q, &QIviMediaDeviceDiscoveryModel::countChanged);
    QObject::connect(q, &QAbstractListModel::rowsRemoved,
                     q, &QIviMediaDeviceDiscoveryModel::countChanged);
    QObject::connect(q, &QAbstractListModel::modelReset,
                     q, &QIviMediaDeviceDiscoveryModel::countChanged);
}

void QIviMediaDeviceDiscoveryModelPrivate::clearToDefaults()
{
    qDeleteAll(m_deviceList);
    m_deviceList.clear();
}

void QIviMediaDeviceDiscoveryModelPrivate::resetModel(const QList<QIviServiceObject*> &deviceList)
{
    Q_Q(QIviMediaDeviceDiscoveryModel);
    q->beginResetModel();
    qDeleteAll(m_deviceList);
    m_deviceList.clear();
    m_deviceList = deviceList;
    q->endResetModel();
}

void QIviMediaDeviceDiscoveryModelPrivate::onDeviceAdded(QIviServiceObject *device)
{
    QIviMediaDevice *mdevice = qobject_cast<QIviMediaDevice*>(device);
    if (!mdevice) {
        qWarning() << "Couldn't cast added Device to QIviMediaDevice. Aborting here.";
        return;
    }

    Q_Q(QIviMediaDeviceDiscoveryModel);
    q->beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count());
    m_deviceList += device;
    q->endInsertRows();

    emit q->deviceAdded(mdevice);
}

void QIviMediaDeviceDiscoveryModelPrivate::onDeviceRemoved(QIviServiceObject *device)
{
    QIviMediaDevice *mdevice = qobject_cast<QIviMediaDevice*>(device);
    if (!mdevice) {
        qWarning() << "Couldn't cast removed Device to QIviMediaDevice. Aborting here.";
        return;
    }

    Q_Q(QIviMediaDeviceDiscoveryModel);
    int index = m_deviceList.indexOf(device);
    if (index == -1) {
        qWarning() << "Failed to remove the Device. Couldn't find" << device << "in the list of available devices";
        return;
    }
    q->beginRemoveRows(QModelIndex(), index, index);
    m_deviceList.takeAt(index);
    q->endRemoveRows();

    emit q->deviceRemoved(mdevice);

    delete device;
}

QIviMediaDeviceDiscoveryModelBackendInterface *QIviMediaDeviceDiscoveryModelPrivate::discoveryBackend() const
{
    return backend<QIviMediaDeviceDiscoveryModelBackendInterface*>();
}

/*!
    \class QIviMediaDeviceDiscoveryModel
    \inmodule QtIviMedia
    \brief Provides a model for discovering media devices.

    The QIviMediaDeviceDiscoveryModel provides a way to query for available media devices and
    to get notified when new media devices get added or are removed.

    All devices listed here need to be a subclass of QIviMediaDevice.

    The QIviMediaDeviceDiscoveryModel expects a single backend to be available. It is recommended to use it
    with \l {QIviAbstractFeatureListModel::}{discoveryMode} set to \l QIviAbstractFeature::AutoDiscovery.
*/

/*!
    \qmltype MediaDeviceDiscoveryModel
    \instantiates QIviMediaDeviceDiscoveryModel
    \inqmlmodule QtIvi.Media
    \inherits AbstractFeatureListModel
    \brief Provides a model for discovering media devices.

    The MediaDeviceDiscoveryModel provides a way to query for available media devices and
    to get notified when new media devices get added or are removed.

    All devices listed here need to be a subclass of MediaDevice.

    The following roles are available in this model:

    \table
    \header
        \li Role name
        \li Type
        \li Description
    \row
        \li \c name
        \li string
        \li The name of the media device. E.g. The name of the connected USB-Thumbdrive/SDCard or a connected Ipod.
    \row
        \li \c type
        \li string
        \li The type of the media device. See \l SupportedMediaDevices for a detailed listing.
    \row
        \li \c serviceObject
        \li QIviMediaDevice
        \li The Media Device. This object be used as ServiceObject for other Features. E.g. The SearchAndBrowseModel.
    \endtable


    The MediaDeviceDiscoveryModel expects a single backend to be available. It is recommended to use it
    with \l {AbstractFeatureListModel::}{discoveryMode} set to AbstractFeature.AutoDiscovery.
*/

/*!
    \enum QIviMediaDeviceDiscoveryModel::Roles
    \value NameRole
          The name of the media device. E.g. The name of the connected USB-Thumbdrive/SDCard or a connected Ipod.
    \value TypeRole
          The type of the media device. See \l SupportedMediaDevices for a detailed listing.
    \value ServiceObjectRole
          A pointer to the media device itself. This pointer can be used as the ServiceObject for other Features. E.g. The QIviSearchAndBrowseModel.
*/

/*!
    Constructs a QIviMediaDeviceDiscoveryModel.

    The \a parent argument is passed on to the \l QIviAbstractFeatureListModel base class.
*/
QIviMediaDeviceDiscoveryModel::QIviMediaDeviceDiscoveryModel(QObject *parent)
    : QIviAbstractFeatureListModel(*new QIviMediaDeviceDiscoveryModelPrivate(QLatin1String(QIviMediaDeviceDiscovery_iid), this), parent)
{
}

/*!
    \qmlproperty int MediaDeviceDiscoveryModel::count
    \brief Holds the current number of rows in this model.
*/
/*!
    \property QIviMediaDeviceDiscoveryModel::count
    \brief Holds the current number of rows in this model.
*/
int QIviMediaDeviceDiscoveryModel::rowCount(const QModelIndex &parent) const
{
    Q_D(const QIviMediaDeviceDiscoveryModel);
    if (parent.isValid())
        return 0;

    return d->m_deviceList.count();
}

/*!
    \reimp
*/
QVariant QIviMediaDeviceDiscoveryModel::data(const QModelIndex &index, int role) const
{
    Q_D(const QIviMediaDeviceDiscoveryModel);
    if (!index.isValid())
        return QVariant();

    int row = index.row();

    if (row >= d->m_deviceList.count() || row < 0)
        return QVariant();

    QIviMediaDevice *item = qobject_cast<QIviMediaDevice*>(d->m_deviceList.at(row));
    if (!item)
        return QVariant();

    switch (role) {
        case NameRole: return item->name();
        case TypeRole: return item->type();
        case ServiceObjectRole: return QVariant::fromValue(item);
    }

    return QVariant();
}

/*!
    \qmlmethod MediaDevice MediaDeviceDiscoveryModel::get(i)

    Returns the media devices at index \a i.

    \note The returned device is owned by the model and can be deleted at any time.
    If stored in a property or a var, this could lead to a dangling pointer.
*/
/*!
    Returns the media device at index \a i.

    \note The returned device is owned by the model and can be deleted at any time.
*/
QIviMediaDevice *QIviMediaDeviceDiscoveryModel::get(int i) const
{
    Q_D(const QIviMediaDeviceDiscoveryModel);
    if (i >= d->m_deviceList.count() || i < 0)
        return nullptr;

    QIviMediaDevice *item = qobject_cast<QIviMediaDevice*>(d->m_deviceList.at(i));
    return item;
}

/*!
    Returns the media device at index \a i.

    \note The returned device is owned by the model and can be deleted at any time.
*/
QIviMediaDevice *QIviMediaDeviceDiscoveryModel::at(int i) const
{
    return get(i);
}

/*!
    \reimp
*/
QHash<int, QByteArray> QIviMediaDeviceDiscoveryModel::roleNames() const
{
    static QHash<int, QByteArray> roles;
    if (roles.isEmpty()) {
        roles[NameRole] = "name";
        roles[TypeRole] = "type";
        roles[ServiceObjectRole] = "serviceObject";
    }
    return roles;
}

/*!
    \internal
*/
QIviMediaDeviceDiscoveryModel::QIviMediaDeviceDiscoveryModel(QIviMediaDeviceDiscoveryModelPrivate &dd, QObject *parent)
    : QIviAbstractFeatureListModel(dd, parent)
{
}

/*!
    \reimp
*/
void QIviMediaDeviceDiscoveryModel::connectToServiceObject(QIviServiceObject *serviceObject)
{
    Q_UNUSED(serviceObject)
    Q_D(QIviMediaDeviceDiscoveryModel);

    QIviMediaDeviceDiscoveryModelBackendInterface *backend = d->discoveryBackend();
    if (!backend)
        return;

    QObjectPrivate::connect(backend, &QIviMediaDeviceDiscoveryModelBackendInterface::availableDevices,
                            d, &QIviMediaDeviceDiscoveryModelPrivate::resetModel);
    QObjectPrivate::connect(backend, &QIviMediaDeviceDiscoveryModelBackendInterface::deviceAdded,
                            d, &QIviMediaDeviceDiscoveryModelPrivate::onDeviceAdded);
    QObjectPrivate::connect(backend, &QIviMediaDeviceDiscoveryModelBackendInterface::deviceRemoved,
                            d, &QIviMediaDeviceDiscoveryModelPrivate::onDeviceRemoved);

    QIviAbstractFeatureListModel::connectToServiceObject(serviceObject);

    backend->initialize();
}

/*!
    \reimp
*/
void QIviMediaDeviceDiscoveryModel::clearServiceObject()
{
    Q_D(QIviMediaDeviceDiscoveryModel);
    d->clearToDefaults();
}

/*!
    \fn void QIviMediaDeviceDiscoveryModel::deviceAdded(QIviMediaDevice *device)

    This signal is emitted whenever a new media device got added. The new media device is passed as \a device.
*/

/*!
    \qmlsignal MediaDeviceDiscoveryModel::deviceAdded(MediaDevice device)

    This signal is emitted whenever a new media device got added. The new media device is passed as \a device.
*/

/*!
    \fn void QIviMediaDeviceDiscoveryModel::deviceRemoved(QIviMediaDevice *device)

    This signal is emitted whenever a media device got removed. The device which got removed is passed as \a device.
    Afterwards the device will be deleted.
*/

/*!
    \qmlsignal MediaDeviceDiscoveryModel::deviceRemoved(MediaDevice device)

    This signal is emitted whenever a media device got removed. The device which got removed is passed as \a device.
    Afterwards the device will be deleted.
*/

QT_END_NAMESPACE

#include "moc_qivimediadevicediscoverymodel.cpp"