2013-06-27 01:13:16 +01:00
|
|
|
/*
|
|
|
|
* <one line to give the library's name and an idea of what it does.>
|
|
|
|
* Copyright 2013 <copyright holder> <email>
|
|
|
|
*
|
|
|
|
* 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-07-02 00:50:32 +01:00
|
|
|
#include <ksharedconfig.h>
|
2013-06-27 01:13:16 +01:00
|
|
|
|
2013-07-02 00:50:32 +01:00
|
|
|
#include <QDebug>
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <qdbusinterface.h>
|
2013-07-02 00:50:32 +01:00
|
|
|
#include <KConfigGroup>
|
2013-07-03 02:52:44 +01:00
|
|
|
#include <kicon.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-06-27 01:13:16 +01:00
|
|
|
{
|
2013-07-03 02:52:44 +01:00
|
|
|
QList<QString> deviceIds = m_dbusInterface->devices();
|
|
|
|
Q_FOREACH(const QString& id, deviceIds) {
|
|
|
|
deviceAdded(id);
|
|
|
|
}
|
2013-06-27 01:13:16 +01:00
|
|
|
|
2013-08-13 22:23:32 +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-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-07 17:14:41 +01:00
|
|
|
/*
|
2013-07-03 02:52:44 +01:00
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1);
|
|
|
|
m_deviceList.append(new DeviceDbusInterface(id,this));
|
|
|
|
endInsertRows();
|
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)
|
|
|
|
{
|
|
|
|
Q_UNUSED(id);
|
|
|
|
refreshDeviceList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DevicesModel::deviceStatusChanged(const QString& id)
|
|
|
|
{
|
|
|
|
Q_UNUSED(id);
|
|
|
|
emit dataChanged(index(0),index(rowCount()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::refreshDeviceList()
|
|
|
|
{
|
|
|
|
|
2013-08-08 18:09:42 +01:00
|
|
|
if (m_deviceList.count() > 0) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_deviceList.count() - 1);
|
|
|
|
m_deviceList.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2013-08-07 17:14:41 +01:00
|
|
|
QList<QString> deviceIds = m_dbusInterface->devices();
|
|
|
|
Q_FOREACH(const QString& id, deviceIds) {
|
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount() + 1);
|
|
|
|
m_deviceList.append(new DeviceDbusInterface(id,this));
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
/*
|
|
|
|
bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent)
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-07-03 02:52:44 +01:00
|
|
|
if (row < 0 || row > m_deviceList.count() || count < 1) {
|
|
|
|
return false;
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
beginInsertRows(parent, row, row + count - 1);
|
|
|
|
for (int i = row; i < row + count; ++i) {
|
|
|
|
m_deviceList.insert(i, new Device());
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
endInsertRows();
|
|
|
|
return true;
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent)
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-07-03 02:52:44 +01:00
|
|
|
if (row < 0 || row > m_deviceList.count() || count < 1) {
|
2013-06-27 01:13:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
beginRemoveRows(parent, row, row + count - 1);
|
|
|
|
for (int i = row; i < row + count; ++i) {
|
|
|
|
m_deviceList.removeAt(row);
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
endRemoveRows();
|
2013-06-27 01:13:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-03 02:52:44 +01:00
|
|
|
*/
|
|
|
|
QVariant DevicesModel::data(const QModelIndex &index, int role) const
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-08-07 17:14:41 +01:00
|
|
|
|
|
|
|
if (!m_dbusInterface->isValid()) {
|
|
|
|
switch (role) {
|
|
|
|
case IconModelRole:
|
|
|
|
return KIcon("dialog-close").pixmap(32, 32);
|
|
|
|
case NameModelRole:
|
|
|
|
return QString("KDED not running");
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_deviceList.count()) {
|
|
|
|
return QVariant();
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
2013-08-07 17:14:41 +01:00
|
|
|
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
//FIXME: This function gets called lots of times per second, producing lots of dbus calls
|
|
|
|
switch (role) {
|
|
|
|
case IconModelRole: {
|
|
|
|
bool paired = m_deviceList[index.row()]->paired();
|
|
|
|
bool reachable = m_deviceList[index.row()]->reachable();
|
|
|
|
QString icon = reachable? (paired? "user-online" : "user-busy") : "user-offline";
|
|
|
|
return KIcon(icon).pixmap(32, 32);
|
|
|
|
}
|
|
|
|
case IdModelRole:
|
|
|
|
return QString(m_deviceList[index.row()]->id());
|
|
|
|
case NameModelRole:
|
|
|
|
return QString(m_deviceList[index.row()]->name());
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
DeviceDbusInterface* DevicesModel::getDevice(const QModelIndex& index)
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-07-03 02:52:44 +01:00
|
|
|
return m_deviceList[index.row()];
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DevicesModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
|
|
|
return m_deviceList.count();
|
|
|
|
}
|
|
|
|
|