Make it possible for the DevicesModel to react to device name changes

This commit is contained in:
Aleix Pol 2015-03-14 04:28:54 +01:00
parent bb97cc19b6
commit 6fe7a9b158
4 changed files with 32 additions and 4 deletions

View file

@ -255,7 +255,7 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link)
m_deviceLinks.append(link); m_deviceLinks.append(link);
//re-read the device name from the identityPackage because it could have changed //re-read the device name from the identityPackage because it could have changed
m_deviceName = identityPackage.get<QString>("deviceName"); setName(identityPackage.get<QString>("deviceName"));
m_deviceType = str2type(identityPackage.get<QString>("deviceType")); m_deviceType = str2type(identityPackage.get<QString>("deviceType"));
//Theoretically we will never add two links from the same provider (the provider should destroy //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(); return QString();
} }
void Device::setName(const QString &name)
{
if (m_deviceName != name) {
m_deviceName = name;
Q_EMIT nameChanged(name);
}
}

View file

@ -41,7 +41,7 @@ class KDECONNECTCORE_EXPORT Device
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device") Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device")
Q_PROPERTY(QString id READ id CONSTANT) Q_PROPERTY(QString id READ id CONSTANT)
Q_PROPERTY(QString iconName READ iconName 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) Q_PROPERTY(bool isReachable READ isReachable NOTIFY reachableStatusChanged)
enum PairStatus { enum PairStatus {
@ -121,8 +121,11 @@ Q_SIGNALS:
Q_SCRIPTABLE void pairingSuccesful(); Q_SCRIPTABLE void pairingSuccesful();
Q_SCRIPTABLE void pairingFailed(const QString& error); Q_SCRIPTABLE void pairingFailed(const QString& error);
Q_SCRIPTABLE void unpaired(); Q_SCRIPTABLE void unpaired();
Q_SCRIPTABLE void nameChanged(const QString& name);
private: private:
void setName(const QString &name);
const QString m_deviceId; const QString m_deviceId;
QString m_deviceName; QString m_deviceName;
DeviceType m_deviceType; DeviceType m_deviceType;

View file

@ -85,7 +85,7 @@ void DevicesModel::deviceAdded(const QString& id)
Q_EMIT dataChanged(idx, idx); Q_EMIT dataChanged(idx, idx);
} else { } else {
beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count()); beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count());
m_deviceList.append(new DeviceDbusInterface(id, this)); appendDevice(id);
endInsertRows(); endInsertRows();
} }
} }
@ -152,11 +152,26 @@ void DevicesModel::receivedDeviceList(QDBusPendingCallWatcher* watcher)
beginInsertRows(QModelIndex(), 0, deviceIds.count()-1); beginInsertRows(QModelIndex(), 0, deviceIds.count()-1);
Q_FOREACH(const QString& id, deviceIds) { Q_FOREACH(const QString& id, deviceIds) {
m_deviceList.append(new DeviceDbusInterface(id, this)); appendDevice(id);
} }
endInsertRows(); 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<DeviceDbusInterface*>(sender()));
Q_ASSERT(row>=0);
const QModelIndex idx = index(row, 0);
Q_EMIT dataChanged(idx, idx);
}
void DevicesModel::clearDevices() void DevicesModel::clearDevices()
{ {
if (!m_deviceList.isEmpty()) { if (!m_deviceList.isEmpty()) {

View file

@ -75,6 +75,7 @@ private Q_SLOTS:
void deviceRemoved(const QString& id); void deviceRemoved(const QString& id);
void refreshDeviceList(); void refreshDeviceList();
void receivedDeviceList(QDBusPendingCallWatcher* watcher); void receivedDeviceList(QDBusPendingCallWatcher* watcher);
void nameChanged(const QString& newName);
Q_SIGNALS: Q_SIGNALS:
void rowsChanged(); void rowsChanged();
@ -82,6 +83,7 @@ Q_SIGNALS:
private: private:
void clearDevices(); void clearDevices();
int rowForDeviceId(const QString& id) const; int rowForDeviceId(const QString& id) const;
void appendDevice(const QString& id);
DaemonDbusInterface* m_dbusInterface; DaemonDbusInterface* m_dbusInterface;
QVector<DeviceDbusInterface*> m_deviceList; QVector<DeviceDbusInterface*> m_deviceList;