Make the DevicesModel::deviceAdded code more robust

If deviceAdded is reported even though it's already in the model, don't
add it twice.
This commit is contained in:
Aleix Pol 2015-03-14 03:40:01 +01:00
parent e592081094
commit b99cba1949
2 changed files with 22 additions and 3 deletions

View file

@ -65,11 +65,29 @@ DevicesModel::~DevicesModel()
{
}
int DevicesModel::rowForDeviceId(const QString& id) const
{
QVector<DeviceDbusInterface*>::const_iterator it = m_deviceList.constBegin(), itEnd = m_deviceList.constEnd();
for (int i = 0; it!=itEnd; ++it, ++i) {
if ((*it)->id() == id) {
return i;
}
}
return -1;
}
void DevicesModel::deviceAdded(const QString& id)
{
beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count());
m_deviceList.append(new DeviceDbusInterface(id, this));
endInsertRows();
//Find whether the device already exists. if so, just report it as modified
int row = rowForDeviceId(id);
if (row>=0) {
const QModelIndex idx = index(row, 0);
Q_EMIT dataChanged(idx, idx);
} else {
beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count());
m_deviceList.append(new DeviceDbusInterface(id, this));
endInsertRows();
}
}
void DevicesModel::deviceRemoved(const QString& id)

View file

@ -81,6 +81,7 @@ Q_SIGNALS:
private:
void clearDevices();
int rowForDeviceId(const QString& id) const;
DaemonDbusInterface* m_dbusInterface;
QVector<DeviceDbusInterface*> m_deviceList;