2013-08-16 05:23:54 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-06-27 01:13:16 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "devicesmodel.h"
|
|
|
|
|
2013-08-21 17:25:44 +01:00
|
|
|
#include <QDBusInterface>
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include <KSharedConfig>
|
2013-07-02 00:50:32 +01:00
|
|
|
#include <KConfigGroup>
|
2013-08-21 17:25:44 +01:00
|
|
|
#include <KIcon>
|
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "kdebugnamespace.h"
|
|
|
|
#include "modeltest.h"
|
|
|
|
|
2013-06-27 01:13:16 +01:00
|
|
|
DevicesModel::DevicesModel(QObject *parent)
|
2013-07-02 14:22:05 +01:00
|
|
|
: QAbstractListModel(parent)
|
2013-07-03 02:52:44 +01:00
|
|
|
, m_dbusInterface(new DaemonDbusInterface(this))
|
2013-08-22 03:38:58 +01:00
|
|
|
, m_displayFilter(DevicesModel::StatusUnknown)
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
//new ModelTest(this, this);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(rowsRemoved(QModelIndex, int, int)),
|
|
|
|
this, SIGNAL(rowsChanged()));
|
|
|
|
connect(this, SIGNAL(rowsInserted(QModelIndex, int, int)),
|
|
|
|
this, SIGNAL(rowsChanged()));
|
|
|
|
|
2013-08-15 21:45:33 +01:00
|
|
|
connect(m_dbusInterface, SIGNAL(deviceAdded(QString)),
|
|
|
|
this, SLOT(deviceAdded(QString)));
|
|
|
|
connect(m_dbusInterface, SIGNAL(deviceVisibilityChanged(QString,bool)),
|
|
|
|
this, SLOT(deviceStatusChanged(QString)));
|
|
|
|
connect(m_dbusInterface, SIGNAL(deviceRemoved(QString)),
|
|
|
|
this, SLOT(deviceRemoved(QString)));
|
2013-08-21 17:25:44 +01:00
|
|
|
|
|
|
|
refreshDeviceList();
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
//Role names for QML
|
|
|
|
QHash<int, QByteArray> names = roleNames();
|
|
|
|
names.insert(IdModelRole, "deviceId");
|
|
|
|
setRoleNames(names);
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 22:23:32 +01:00
|
|
|
DevicesModel::~DevicesModel()
|
2013-07-02 00:50:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void DevicesModel::deviceAdded(const QString& id)
|
2013-07-02 00:50:32 +01:00
|
|
|
{
|
2013-08-21 17:25:44 +01:00
|
|
|
//TODO: Actually add instead of refresh
|
2013-08-07 17:14:41 +01:00
|
|
|
Q_UNUSED(id);
|
2013-08-13 22:23:32 +01:00
|
|
|
refreshDeviceList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::deviceRemoved(const QString& id)
|
|
|
|
{
|
2013-08-21 17:25:44 +01:00
|
|
|
//TODO: Actually remove instead of refresh
|
2013-08-13 22:23:32 +01:00
|
|
|
Q_UNUSED(id);
|
|
|
|
refreshDeviceList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::deviceStatusChanged(const QString& id)
|
|
|
|
{
|
|
|
|
Q_UNUSED(id);
|
2013-08-15 23:28:07 +01:00
|
|
|
//FIXME: Emitting dataChanged does not invalidate the view, refreshDeviceList does.
|
2013-08-15 21:45:33 +01:00
|
|
|
//Q_EMIT dataChanged(index(0),index(rowCount()));
|
|
|
|
refreshDeviceList();
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
2014-06-14 12:31:02 +01:00
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
DevicesModel::StatusFlags DevicesModel::displayFilter() const
|
|
|
|
{
|
|
|
|
return m_displayFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::setDisplayFilter(int flags)
|
|
|
|
{
|
|
|
|
setDisplayFilter((StatusFlags)flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::setDisplayFilter(DevicesModel::StatusFlags flags)
|
|
|
|
{
|
|
|
|
m_displayFilter = flags;
|
|
|
|
refreshDeviceList();
|
|
|
|
}
|
2013-08-13 22:23:32 +01:00
|
|
|
|
|
|
|
void DevicesModel::refreshDeviceList()
|
|
|
|
{
|
2013-08-28 18:33:46 +01:00
|
|
|
if (!m_deviceList.isEmpty()) {
|
2013-08-21 17:25:44 +01:00
|
|
|
beginRemoveRows(QModelIndex(), 0, m_deviceList.size() - 1);
|
2013-08-08 18:09:42 +01:00
|
|
|
m_deviceList.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2013-08-15 21:45:33 +01:00
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
if (!m_dbusInterface->isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-16 11:14:05 +00:00
|
|
|
|
|
|
|
bool onlyPaired = (m_displayFilter & StatusPaired);
|
|
|
|
bool onlyReachable = (m_displayFilter & StatusReachable);
|
|
|
|
|
|
|
|
QDBusPendingReply<QStringList> pendingDeviceIds = m_dbusInterface->devices(onlyReachable, onlyPaired);
|
2013-08-28 18:33:46 +01:00
|
|
|
pendingDeviceIds.waitForFinished();
|
|
|
|
if (pendingDeviceIds.isError()) return;
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2014-01-16 11:14:05 +00:00
|
|
|
const QStringList& deviceIds = pendingDeviceIds.value();
|
2013-08-28 18:33:46 +01:00
|
|
|
Q_FOREACH(const QString& id, deviceIds) {
|
|
|
|
int firstRow = m_deviceList.size();
|
|
|
|
int lastRow = firstRow;
|
|
|
|
beginInsertRows(QModelIndex(), firstRow, lastRow);
|
2014-01-16 11:14:05 +00:00
|
|
|
m_deviceList.append(new DeviceDbusInterface(id,this));
|
2013-08-22 02:21:08 +01:00
|
|
|
endInsertRows();
|
2013-08-07 17:14:41 +01:00
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2013-08-28 18:33:46 +01:00
|
|
|
Q_EMIT dataChanged(index(0), index(m_deviceList.size()));
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
QVariant DevicesModel::data(const QModelIndex& index, int role) const
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-08-22 02:21:08 +01:00
|
|
|
if (!m_dbusInterface->isValid()
|
|
|
|
|| !index.isValid()
|
2013-08-15 21:22:11 +01:00
|
|
|
|| index.row() < 0
|
2013-08-28 18:33:46 +01:00
|
|
|
|| index.row() >= m_deviceList.size()
|
2013-08-15 21:22:11 +01:00
|
|
|
|| !m_deviceList[index.row()]->isValid())
|
|
|
|
{
|
2013-07-03 02:52:44 +01:00
|
|
|
return QVariant();
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
2013-08-07 17:14:41 +01:00
|
|
|
|
2013-08-15 21:22:11 +01:00
|
|
|
DeviceDbusInterface* device = m_deviceList[index.row()];
|
|
|
|
|
2013-08-18 17:22:54 +01:00
|
|
|
//FIXME: This function gets called lots of times, producing lots of dbus calls. Add a cache.
|
2013-07-03 02:52:44 +01:00
|
|
|
switch (role) {
|
|
|
|
case IconModelRole: {
|
2013-08-30 18:10:43 +01:00
|
|
|
bool paired = device->isPaired();
|
2013-08-31 12:04:00 +01:00
|
|
|
bool reachable = device->isReachable();
|
2013-07-03 02:52:44 +01:00
|
|
|
QString icon = reachable? (paired? "user-online" : "user-busy") : "user-offline";
|
|
|
|
return KIcon(icon).pixmap(32, 32);
|
|
|
|
}
|
|
|
|
case IdModelRole:
|
2013-08-15 21:22:11 +01:00
|
|
|
return QString(device->id());
|
2013-07-03 02:52:44 +01:00
|
|
|
case NameModelRole:
|
2013-08-15 21:22:11 +01:00
|
|
|
return QString(device->name());
|
2013-08-21 17:25:44 +01:00
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return QVariant(); //To implement
|
2013-08-15 21:22:11 +01:00
|
|
|
case StatusModelRole: {
|
|
|
|
int status = StatusUnknown;
|
2013-08-31 12:04:00 +01:00
|
|
|
if (device->isReachable()) {
|
2013-08-15 23:25:13 +01:00
|
|
|
status |= StatusReachable;
|
2013-08-30 18:10:43 +01:00
|
|
|
if (device->isPaired()) status |= StatusPaired;
|
2013-08-15 23:25:13 +01:00
|
|
|
}
|
2013-08-15 21:22:11 +01:00
|
|
|
return status;
|
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
default:
|
2013-08-07 17:14:41 +01:00
|
|
|
return QVariant();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
DeviceDbusInterface* DevicesModel::getDevice(const QModelIndex& index) const
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-08-18 17:22:54 +01:00
|
|
|
if (!index.isValid()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
if (row < 0 || row >= m_deviceList.size()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_deviceList[row];
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-14 12:31:02 +01:00
|
|
|
int DevicesModel::rowCount(const QModelIndex& parent) const
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-08-22 02:21:08 +01:00
|
|
|
if(parent.isValid()) {
|
|
|
|
//Return size 0 if we are a child because this is not a tree
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-27 01:13:16 +01:00
|
|
|
|
2013-08-28 18:33:46 +01:00
|
|
|
return m_deviceList.size();
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|