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"
|
2014-09-21 23:16:39 +01:00
|
|
|
#include "interfaces_debug.h"
|
2013-06-27 01:13:16 +01:00
|
|
|
|
2015-05-18 02:47:31 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2015-05-18 07:28:58 +01:00
|
|
|
#include <QString>
|
2013-08-21 17:25:44 +01:00
|
|
|
#include <QDBusInterface>
|
2015-05-18 07:28:58 +01:00
|
|
|
#include <QDBusPendingReply>
|
2014-09-13 00:04:48 +01:00
|
|
|
#include <QIcon>
|
2015-06-25 20:19:23 +01:00
|
|
|
#include <QDBusServiceWatcher>
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2014-07-01 00:25:30 +01:00
|
|
|
#include "dbusinterfaces.h"
|
2014-06-14 14:22:40 +01:00
|
|
|
// #include "modeltest.h"
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2014-09-21 23:16:39 +01:00
|
|
|
Q_LOGGING_CATEGORY(KDECONNECT_INTERFACES, "kdeconnect.interfaces");
|
|
|
|
|
2015-09-09 19:09:04 +01:00
|
|
|
static QString createId() { return QCoreApplication::instance()->applicationName()+QString::number(QCoreApplication::applicationPid()); }
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC_WITH_ARGS(QString, s_keyId, (createId()));
|
|
|
|
|
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))
|
2015-06-22 03:42:16 +01:00
|
|
|
, m_displayFilter(StatusFilterFlag::NoFilter)
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
//new ModelTest(this, this);
|
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(this, &QAbstractItemModel::rowsRemoved,
|
|
|
|
this, &DevicesModel::rowsChanged);
|
|
|
|
connect(this, &QAbstractItemModel::rowsInserted,
|
|
|
|
this, &DevicesModel::rowsChanged);
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2013-08-15 21:45:33 +01:00
|
|
|
connect(m_dbusInterface, SIGNAL(deviceAdded(QString)),
|
|
|
|
this, SLOT(deviceAdded(QString)));
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(m_dbusInterface, &OrgKdeKdeconnectDaemonInterface::deviceVisibilityChanged,
|
|
|
|
this, &DevicesModel::deviceUpdated);
|
|
|
|
connect(m_dbusInterface, &OrgKdeKdeconnectDaemonInterface::deviceRemoved,
|
|
|
|
this, &DevicesModel::deviceRemoved);
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2015-06-25 20:19:23 +01:00
|
|
|
QDBusServiceWatcher* watcher = new QDBusServiceWatcher(DaemonDbusInterface::activatedService(),
|
|
|
|
QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
|
|
|
|
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &DevicesModel::refreshDeviceList);
|
|
|
|
connect(watcher, &QDBusServiceWatcher::serviceUnregistered, this, &DevicesModel::clearDevices);
|
|
|
|
|
2015-09-09 19:09:04 +01:00
|
|
|
//refresh the view, acquireDiscoveryMode if necessary
|
|
|
|
setDisplayFilter(NoFilter);
|
2014-09-23 18:27:47 +01:00
|
|
|
}
|
2013-08-21 17:25:44 +01:00
|
|
|
|
2014-09-23 18:27:47 +01:00
|
|
|
QHash< int, QByteArray > DevicesModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
|
2013-08-22 02:21:08 +01:00
|
|
|
names.insert(IdModelRole, "deviceId");
|
2014-06-14 18:09:31 +01:00
|
|
|
names.insert(IconNameRole, "iconName");
|
2015-06-11 15:47:53 +01:00
|
|
|
names.insert(DeviceRole, "device");
|
2015-06-19 06:14:02 +01:00
|
|
|
names.insert(StatusModelRole, "status");
|
2014-09-23 18:27:47 +01:00
|
|
|
return 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
|
|
|
{
|
2015-09-09 19:09:04 +01:00
|
|
|
m_dbusInterface->releaseDiscoveryMode(*s_keyId);
|
2013-07-02 00:50:32 +01:00
|
|
|
}
|
|
|
|
|
2015-06-24 19:57:02 +01:00
|
|
|
int DevicesModel::rowForDevice(const QString& id) const
|
|
|
|
{
|
|
|
|
for (int i = 0, c=m_deviceList.size(); i<c; ++i) {
|
|
|
|
if (m_deviceList[i]->id() == id) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-22 03:42:16 +01:00
|
|
|
void DevicesModel::deviceAdded(const QString& id)
|
2015-03-14 02:40:01 +00:00
|
|
|
{
|
2015-06-24 19:57:02 +01:00
|
|
|
if (rowForDevice(id) >= 0) {
|
|
|
|
Q_ASSERT_X(false, "deviceAdded", "Trying to add a device twice");
|
2015-06-22 03:42:16 +01:00
|
|
|
return;
|
2015-03-14 02:40:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 03:42:16 +01:00
|
|
|
DeviceDbusInterface* dev = new DeviceDbusInterface(id, this);
|
2015-06-24 19:57:02 +01:00
|
|
|
Q_ASSERT(dev->isValid());
|
2015-06-22 03:42:16 +01:00
|
|
|
|
2016-04-01 14:01:42 +01:00
|
|
|
if (! passesFilter(dev)) {
|
2015-06-24 19:57:02 +01:00
|
|
|
delete dev;
|
2015-06-22 03:42:16 +01:00
|
|
|
return;
|
2015-03-14 02:40:01 +00:00
|
|
|
}
|
2015-06-22 03:42:16 +01:00
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), m_deviceList.size(), m_deviceList.size());
|
|
|
|
appendDevice(dev);
|
|
|
|
endInsertRows();
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::deviceRemoved(const QString& id)
|
|
|
|
{
|
2015-06-24 19:57:02 +01:00
|
|
|
int row = rowForDevice(id);
|
|
|
|
if (row>=0) {
|
2015-06-22 04:20:40 +01:00
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
delete m_deviceList.takeAt(row);
|
|
|
|
endRemoveRows();
|
2015-06-22 03:42:16 +01:00
|
|
|
}
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
|
|
|
|
2015-07-14 17:41:47 +01:00
|
|
|
void DevicesModel::deviceUpdated(const QString& id, bool isVisible)
|
2013-08-13 22:23:32 +01:00
|
|
|
{
|
2016-04-01 14:01:42 +01:00
|
|
|
Q_UNUSED(isVisible);
|
2015-06-24 19:57:02 +01:00
|
|
|
int row = rowForDevice(id);
|
2015-07-14 17:41:47 +01:00
|
|
|
|
2016-04-01 14:01:42 +01:00
|
|
|
if (row < 0) {
|
2015-07-14 17:41:47 +01:00
|
|
|
// FIXME: when m_dbusInterface is not valid refreshDeviceList() does
|
|
|
|
// nothing and we can miss some devices.
|
|
|
|
// Someone can reproduce this problem by restarting kdeconnectd while
|
|
|
|
// kdeconnect's plasmoid is still running.
|
2016-04-01 14:01:42 +01:00
|
|
|
// Another reason for this branch is that we removed the device previously
|
|
|
|
// because of the filter settings.
|
|
|
|
qCDebug(KDECONNECT_INTERFACES) << "Adding missing or previously removed device" << id;
|
2015-07-14 17:41:47 +01:00
|
|
|
deviceAdded(id);
|
2016-04-01 14:01:42 +01:00
|
|
|
} else {
|
|
|
|
DeviceDbusInterface *dev = getDevice(row);
|
|
|
|
if (! passesFilter(dev)) {
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
delete m_deviceList.takeAt(row);
|
|
|
|
endRemoveRows();
|
|
|
|
qCDebug(KDECONNECT_INTERFACES) << "Removed changed device " << id;
|
|
|
|
} else {
|
|
|
|
const QModelIndex idx = index(row);
|
|
|
|
Q_EMIT dataChanged(idx, idx);
|
|
|
|
}
|
2015-06-22 03:42:16 +01:00
|
|
|
}
|
2013-08-13 22:23:32 +01:00
|
|
|
}
|
2014-06-14 12:31:02 +01:00
|
|
|
|
2014-07-01 00:19:14 +01:00
|
|
|
int DevicesModel::displayFilter() const
|
2013-08-22 02:21:08 +01:00
|
|
|
{
|
|
|
|
return m_displayFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::setDisplayFilter(int flags)
|
|
|
|
{
|
2015-06-22 03:42:16 +01:00
|
|
|
m_displayFilter = (StatusFilterFlag)flags;
|
2015-09-09 19:09:04 +01:00
|
|
|
|
2016-06-05 21:49:32 +01:00
|
|
|
const bool reachableNeeded = (m_displayFilter & StatusFilterFlag::Reachable);
|
|
|
|
if (reachableNeeded)
|
2015-09-09 19:09:04 +01:00
|
|
|
m_dbusInterface->acquireDiscoveryMode(*s_keyId);
|
2016-06-05 21:49:32 +01:00
|
|
|
else
|
|
|
|
m_dbusInterface->releaseDiscoveryMode(*s_keyId);
|
2015-09-09 19:09:04 +01:00
|
|
|
|
2013-08-22 02:21:08 +01:00
|
|
|
refreshDeviceList();
|
|
|
|
}
|
2013-08-13 22:23:32 +01:00
|
|
|
|
|
|
|
void DevicesModel::refreshDeviceList()
|
|
|
|
{
|
2013-08-22 02:21:08 +01:00
|
|
|
if (!m_dbusInterface->isValid()) {
|
2015-03-14 01:30:35 +00:00
|
|
|
clearDevices();
|
2015-03-14 01:42:18 +00:00
|
|
|
qCWarning(KDECONNECT_INTERFACES) << "dbus interface not valid";
|
2013-08-22 02:21:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-22 03:42:16 +01:00
|
|
|
bool onlyPaired = (m_displayFilter & StatusFilterFlag::Paired);
|
|
|
|
bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable);
|
2014-01-16 11:14:05 +00:00
|
|
|
|
|
|
|
QDBusPendingReply<QStringList> pendingDeviceIds = m_dbusInterface->devices(onlyReachable, onlyPaired);
|
2015-03-14 01:30:35 +00:00
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingDeviceIds, this);
|
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
|
|
|
|
this, &DevicesModel::receivedDeviceList);
|
2015-03-14 01:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::receivedDeviceList(QDBusPendingCallWatcher* watcher)
|
|
|
|
{
|
2015-03-14 03:27:30 +00:00
|
|
|
watcher->deleteLater();
|
2015-03-14 01:30:35 +00:00
|
|
|
clearDevices();
|
|
|
|
QDBusPendingReply<QStringList> pendingDeviceIds = *watcher;
|
2014-07-01 00:25:30 +01:00
|
|
|
if (pendingDeviceIds.isError()) {
|
2015-03-14 01:42:18 +00:00
|
|
|
qCWarning(KDECONNECT_INTERFACES) << "error while refreshing device list" << pendingDeviceIds.error().message();
|
2014-07-01 00:25:30 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2015-03-14 01:30:35 +00:00
|
|
|
Q_ASSERT(m_deviceList.isEmpty());
|
|
|
|
const QStringList deviceIds = pendingDeviceIds.value();
|
2015-03-14 03:28:06 +00:00
|
|
|
|
|
|
|
if (deviceIds.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2015-03-14 02:32:53 +00:00
|
|
|
beginInsertRows(QModelIndex(), 0, deviceIds.count()-1);
|
2017-07-20 15:14:07 +01:00
|
|
|
for (const QString& id : deviceIds) {
|
2015-03-16 01:31:59 +00:00
|
|
|
appendDevice(new DeviceDbusInterface(id, this));
|
2013-08-07 17:14:41 +01:00
|
|
|
}
|
2015-03-14 01:30:35 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
2013-08-22 02:21:08 +01:00
|
|
|
|
2015-03-16 01:31:59 +00:00
|
|
|
void DevicesModel::appendDevice(DeviceDbusInterface* dev)
|
2015-03-14 03:28:54 +00:00
|
|
|
{
|
|
|
|
m_deviceList.append(dev);
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(dev, &OrgKdeKdeconnectDeviceInterface::nameChanged, this, &DevicesModel::nameChanged);
|
2015-03-14 03:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DevicesModel::nameChanged(const QString& newName)
|
|
|
|
{
|
2015-03-24 03:44:18 +00:00
|
|
|
Q_UNUSED(newName);
|
2015-06-22 03:42:16 +01:00
|
|
|
DeviceDbusInterface* device = static_cast<DeviceDbusInterface*>(sender());
|
2015-07-14 17:41:47 +01:00
|
|
|
|
|
|
|
Q_ASSERT(rowForDevice(device->id()) >= 0);
|
|
|
|
|
|
|
|
deviceUpdated(device->id(), true);
|
2015-03-14 03:28:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 01:30:35 +00:00
|
|
|
void DevicesModel::clearDevices()
|
|
|
|
{
|
|
|
|
if (!m_deviceList.isEmpty()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_deviceList.size() - 1);
|
|
|
|
qDeleteAll(m_deviceList);
|
|
|
|
m_deviceList.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
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
|
|
|
{
|
2015-06-24 19:57:02 +01:00
|
|
|
if (!index.isValid()
|
2013-08-15 21:22:11 +01:00
|
|
|
|| index.row() < 0
|
2015-06-24 19:57:02 +01:00
|
|
|
|| index.row() >= m_deviceList.size())
|
2013-08-15 21:22:11 +01:00
|
|
|
{
|
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
|
|
|
|
2015-06-24 19:57:02 +01:00
|
|
|
Q_ASSERT(m_dbusInterface->isValid());
|
|
|
|
|
2015-06-11 15:47:53 +01:00
|
|
|
DeviceDbusInterface* device = m_deviceList[index.row()];
|
2015-06-24 19:57:02 +01:00
|
|
|
Q_ASSERT(device->isValid());
|
2013-08-15 21:22:11 +01:00
|
|
|
|
2015-05-18 02:48:10 +01:00
|
|
|
//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) {
|
2016-11-28 15:27:28 +00:00
|
|
|
case Qt::SizeHintRole:
|
|
|
|
return QSize(0, 32);
|
2013-07-03 02:52:44 +01:00
|
|
|
case IconModelRole: {
|
2015-05-18 02:48:10 +01:00
|
|
|
QString icon = data(index, IconNameRole).toString();
|
2015-04-20 06:21:37 +01:00
|
|
|
return QIcon::fromTheme(icon);
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
|
|
|
case IdModelRole:
|
2015-03-14 01:20:25 +00:00
|
|
|
return device->id();
|
2013-07-03 02:52:44 +01:00
|
|
|
case NameModelRole:
|
2015-03-14 01:20:25 +00:00
|
|
|
return device->name();
|
2015-05-18 02:47:31 +01:00
|
|
|
case Qt::ToolTipRole: {
|
2015-12-01 18:45:14 +00:00
|
|
|
bool trusted = device->isTrusted();
|
2015-05-18 02:47:31 +01:00
|
|
|
bool reachable = device->isReachable();
|
2015-12-01 18:45:14 +00:00
|
|
|
QString status = reachable? (trusted? i18n("Device trusted and connected") : i18n("Device not trusted")) : i18n("Device disconnected");
|
2015-05-18 02:47:31 +01:00
|
|
|
return status;
|
|
|
|
}
|
2013-08-15 21:22:11 +01:00
|
|
|
case StatusModelRole: {
|
2015-06-22 03:42:16 +01:00
|
|
|
int status = StatusFilterFlag::NoFilter;
|
2013-08-31 12:04:00 +01:00
|
|
|
if (device->isReachable()) {
|
2015-06-22 03:42:16 +01:00
|
|
|
status |= StatusFilterFlag::Reachable;
|
2016-04-15 03:00:42 +01:00
|
|
|
}
|
|
|
|
if (device->isTrusted()) {
|
2016-04-01 14:18:14 +01:00
|
|
|
status |= StatusFilterFlag::Paired;
|
2013-08-15 23:25:13 +01:00
|
|
|
}
|
2013-08-15 21:22:11 +01:00
|
|
|
return status;
|
|
|
|
}
|
2014-06-14 18:09:31 +01:00
|
|
|
case IconNameRole:
|
2015-05-18 07:28:58 +01:00
|
|
|
return device->statusIconName();
|
2015-06-11 15:47:53 +01:00
|
|
|
case DeviceRole:
|
|
|
|
return QVariant::fromValue<QObject*>(device);
|
2013-07-03 02:52:44 +01:00
|
|
|
default:
|
2014-06-14 18:09:31 +01:00
|
|
|
return QVariant();
|
2013-07-03 02:52:44 +01:00
|
|
|
}
|
2013-06-27 01:13:16 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 12:00:23 +01:00
|
|
|
DeviceDbusInterface* DevicesModel::getDevice(int row) const
|
2013-06-27 01:13:16 +01:00
|
|
|
{
|
2013-08-18 17:22:54 +01:00
|
|
|
if (row < 0 || row >= m_deviceList.size()) {
|
2015-09-08 09:46:59 +01:00
|
|
|
return nullptr;
|
2013-08-18 17:22:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-04-01 14:01:42 +01:00
|
|
|
|
|
|
|
bool DevicesModel::passesFilter(DeviceDbusInterface* dev) const
|
|
|
|
{
|
|
|
|
bool onlyPaired = (m_displayFilter & StatusFilterFlag::Paired);
|
|
|
|
bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable);
|
|
|
|
|
2016-04-15 03:00:42 +01:00
|
|
|
return !((onlyReachable && !dev->isReachable()) || (onlyPaired && !dev->isTrusted()));
|
2016-04-01 14:01:42 +01:00
|
|
|
}
|