@@ -0,0 +1,140 @@
1+/****************************************************************************
2+**
3+** Copyright (C) 2015 Jolla.
4+** Contact: http://www.qt-project.org/legal
5+**
6+** This file is part of the QtSystems module of the Qt Toolkit.
7+**
8+** $QT_BEGIN_LICENSE:LGPL$
9+** Commercial License Usage
10+** Licensees holding valid commercial Qt licenses may use this file in
11+** accordance with the commercial license agreement provided with the
12+** Software or, alternatively, in accordance with the terms contained in
13+** a written agreement between you and Digia. For licensing terms and
14+** conditions see http://qt.digia.com/licensing. For further information
15+** use the contact form at http://qt.digia.com/contact-us.
16+**
17+** GNU Lesser General Public License Usage
18+** Alternatively, this file may be used under the terms of the GNU Lesser
19+** General Public License version 2.1 as published by the Free Software
20+** Foundation and appearing in the file LICENSE.LGPL included in the
21+** packaging of this file. Please review the following information to
22+** ensure the GNU Lesser General Public License version 2.1 requirements
23+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24+**
25+** In addition, as a special exception, Digia gives you certain additional
26+** rights. These rights are described in the Digia Qt LGPL Exception
27+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28+**
29+** GNU General Public License Usage
30+** Alternatively, this file may be used under the terms of the GNU
31+** General Public License version 3.0 as published by the Free Software
32+** Foundation and appearing in the file LICENSE.GPL included in the
33+** packaging of this file. Please review the following information to
34+** ensure the GNU General Public License version 3.0 requirements will be
35+** met: http://www.gnu.org/copyleft/gpl.html.
36+**
37+**
38+** $QT_END_LICENSE$
39+**
40+****************************************************************************/
41+#include "mockqdeclarativeinputdeviceinfo_p.h"
42+
43+QDeclarativeInputDeviceInfo::QDeclarativeInputDeviceInfo(QObject *parent) :
44+ QAbstractListModel(parent),
45+ deviceInfo(new QInputDeviceInfo)
46+{
47+}
48+
49+QDeclarativeInputDeviceInfo::~QDeclarativeInputDeviceInfo()
50+{
51+ delete deviceInfo;
52+}
53+
54+QVariant QDeclarativeInputDeviceInfo::data(const QModelIndex &index, int role) const
55+{
56+ switch (role) {
57+ case ServiceRole:
58+ return QVariant::fromValue(static_cast<QObject *>(inputDevices.value(index.row())));
59+ }
60+
61+ return QVariant();
62+}
63+
64+int QDeclarativeInputDeviceInfo::rowCount(const QModelIndex &parent) const
65+{
66+ Q_UNUSED(parent);
67+
68+ return inputDevices.count();
69+}
70+
71+int QDeclarativeInputDeviceInfo::indexOf(const QString &devicePath) const
72+{
73+ int idx(-1);
74+ Q_FOREACH (QInputDevice *device, inputDevices) {
75+ idx++;
76+ if (device->devicePath() == devicePath) return idx;
77+ }
78+
79+ return -1;
80+}
81+
82+QInputDevice *QDeclarativeInputDeviceInfo::get(int index) const
83+{
84+ if (index < 0 || index > inputDevices.count())
85+ return 0;
86+ return inputDevices.value(index);
87+}
88+
89+void QDeclarativeInputDeviceInfo::updateDeviceList()
90+{
91+ QVector <QInputDevice *> newDevices = deviceInfo->deviceList();
92+
93+ int numNew = newDevices.count();
94+
95+ for (int i = 0; i < numNew; i++) {
96+ int j = inputDevices.indexOf(newDevices.value(i));
97+ if (j == -1) {
98+ // not found -> remove from list
99+ beginInsertRows(QModelIndex(), i, i);
100+ inputDevices.insert(i, newDevices.value(i));
101+ endInsertRows();
102+ } else if (i != j) {
103+ // changed its position -> move it
104+ QInputDevice* device = inputDevices.value(j);
105+ beginMoveRows(QModelIndex(), j, j, QModelIndex(), i);
106+ inputDevices.remove(j);
107+ inputDevices.insert(i, device);
108+ endMoveRows();
109+ } else {
110+ QModelIndex changedIndex(this->index(j, 0, QModelIndex()));
111+ Q_EMIT dataChanged(changedIndex, changedIndex);
112+ }
113+ }
114+
115+ int numOld = inputDevices.count();
116+ if (numOld > numNew) {
117+ beginRemoveRows(QModelIndex(), numNew, numOld - 1);
118+ inputDevices.remove(numNew, numOld - numNew);
119+ endRemoveRows();
120+ }
121+}
122+
123+void QDeclarativeInputDeviceInfo::addedDevice(const QString &devicePath)
124+{
125+ updateDeviceList();
126+ Q_EMIT newDevice(devicePath);
127+}
128+
129+void QDeclarativeInputDeviceInfo::removedDevice(const QString &devicePath)
130+{
131+ updateDeviceList();
132+ Q_EMIT deviceRemoved(devicePath);
133+}
134+
135+QHash<int, QByteArray> QDeclarativeInputDeviceInfo::roleNames() const
136+{
137+ QHash<int, QByteArray> roles;
138+ roles.insert(ServiceRole, "service");
139+ return roles;
140+}