From 6fe7a9b15890947a104378c88c142bc47485ddc0 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Sat, 14 Mar 2015 04:28:54 +0100 Subject: [PATCH] Make it possible for the DevicesModel to react to device name changes --- core/device.cpp | 10 +++++++++- core/device.h | 5 ++++- interfaces/devicesmodel.cpp | 19 +++++++++++++++++-- interfaces/devicesmodel.h | 2 ++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/device.cpp b/core/device.cpp index 780d9e0a6..85d41344a 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -255,7 +255,7 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link) m_deviceLinks.append(link); //re-read the device name from the identityPackage because it could have changed - m_deviceName = identityPackage.get("deviceName"); + setName(identityPackage.get("deviceName")); m_deviceType = str2type(identityPackage.get("deviceType")); //Theoretically we will never add two links from the same provider (the provider should destroy @@ -497,3 +497,11 @@ QString Device::iconName() const } return QString(); } + +void Device::setName(const QString &name) +{ + if (m_deviceName != name) { + m_deviceName = name; + Q_EMIT nameChanged(name); + } +} diff --git a/core/device.h b/core/device.h index 367c1980a..a0c97292e 100644 --- a/core/device.h +++ b/core/device.h @@ -41,7 +41,7 @@ class KDECONNECTCORE_EXPORT Device Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device") Q_PROPERTY(QString id READ id CONSTANT) Q_PROPERTY(QString iconName READ iconName CONSTANT) - Q_PROPERTY(QString name READ name) + Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(bool isReachable READ isReachable NOTIFY reachableStatusChanged) enum PairStatus { @@ -121,8 +121,11 @@ Q_SIGNALS: Q_SCRIPTABLE void pairingSuccesful(); Q_SCRIPTABLE void pairingFailed(const QString& error); Q_SCRIPTABLE void unpaired(); + Q_SCRIPTABLE void nameChanged(const QString& name); private: + void setName(const QString &name); + const QString m_deviceId; QString m_deviceName; DeviceType m_deviceType; diff --git a/interfaces/devicesmodel.cpp b/interfaces/devicesmodel.cpp index 940177464..028497e25 100644 --- a/interfaces/devicesmodel.cpp +++ b/interfaces/devicesmodel.cpp @@ -85,7 +85,7 @@ void DevicesModel::deviceAdded(const QString& id) Q_EMIT dataChanged(idx, idx); } else { beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count()); - m_deviceList.append(new DeviceDbusInterface(id, this)); + appendDevice(id); endInsertRows(); } } @@ -152,11 +152,26 @@ void DevicesModel::receivedDeviceList(QDBusPendingCallWatcher* watcher) beginInsertRows(QModelIndex(), 0, deviceIds.count()-1); Q_FOREACH(const QString& id, deviceIds) { - m_deviceList.append(new DeviceDbusInterface(id, this)); + appendDevice(id); } endInsertRows(); } +void DevicesModel::appendDevice(const QString& id) +{ + DeviceDbusInterface* dev = new DeviceDbusInterface(id, this); + m_deviceList.append(dev); + connect(dev, SIGNAL(nameChanged(QString)), SLOT(nameChanged(QString))); +} + +void DevicesModel::nameChanged(const QString& newName) +{ + int row = m_deviceList.indexOf(static_cast(sender())); + Q_ASSERT(row>=0); + const QModelIndex idx = index(row, 0); + Q_EMIT dataChanged(idx, idx); +} + void DevicesModel::clearDevices() { if (!m_deviceList.isEmpty()) { diff --git a/interfaces/devicesmodel.h b/interfaces/devicesmodel.h index 001bb4def..0923cf3b8 100644 --- a/interfaces/devicesmodel.h +++ b/interfaces/devicesmodel.h @@ -75,6 +75,7 @@ private Q_SLOTS: void deviceRemoved(const QString& id); void refreshDeviceList(); void receivedDeviceList(QDBusPendingCallWatcher* watcher); + void nameChanged(const QString& newName); Q_SIGNALS: void rowsChanged(); @@ -82,6 +83,7 @@ Q_SIGNALS: private: void clearDevices(); int rowForDeviceId(const QString& id) const; + void appendDevice(const QString& id); DaemonDbusInterface* m_dbusInterface; QVector m_deviceList;