Commit 90dc9ac5 authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Device support: Fix yet another off-by-one error for device indices.



Prevent them from happening in the future by hiding the respective
functions in the device manager. No one should ever need to map an id to
an index directly on the device manager.

Change-Id: I41da16489b8aec67709ab3b4b115d915d4f29a4c
Reviewed-by: default avatarTobias Hunger <tobias.hunger@nokia.com>
parent 2dcec0ca
......@@ -70,6 +70,15 @@ const char DefaultDevicesKey[] = "DefaultDevices";
class DeviceManagerPrivate
{
public:
int indexForId(Core::Id id) const
{
for (int i = 0; i < devices.count(); ++i) {
if (devices.at(i)->id() == id)
return i;
}
return -1;
}
static DeviceManager *clonedInstance;
QList<IDevice::Ptr> devices;
QList<IDevice::Ptr> inactiveAutoDetectedDevices;
......@@ -223,7 +232,7 @@ void DeviceManager::addDevice(const IDevice::Ptr &_device)
QTC_ASSERT(this != instance() || device->isAutoDetected(), return);
QString name = device->displayName();
const int pos = indexForId(device->id());
const int pos = d->indexForId(device->id());
if (pos >= 0) {
device->setDisplayName(QString()); // For name uniquification to work.
d->devices[pos] = device;
......@@ -272,7 +281,7 @@ void DeviceManager::removeDevice(Core::Id id)
const bool wasDefault = d->defaultDevices.value(device->type()) == device->id();
const Core::Id deviceType = device->type();
d->devices.removeAt(indexForId(id));
d->devices.removeAt(d->indexForId(id));
emit deviceRemoved(device->id());
if (wasDefault) {
......@@ -342,7 +351,7 @@ IDevice::ConstPtr DeviceManager::deviceAt(int idx) const
IDevice::Ptr DeviceManager::mutableDevice(Core::Id id) const
{
const int index = indexForId(id);
const int index = d->indexForId(id);
return index == -1 ? IDevice::Ptr() : d->devices.at(index);
}
......@@ -357,7 +366,7 @@ bool DeviceManager::hasDevice(const QString &name) const
IDevice::ConstPtr DeviceManager::find(Core::Id id) const
{
const int index = indexForId(id);
const int index = d->indexForId(id);
return index == -1 ? IDevice::ConstPtr() : deviceAt(index);
}
......@@ -374,25 +383,11 @@ IDevice::ConstPtr DeviceManager::defaultDevice(Core::Id deviceType) const
return find(id);
}
int DeviceManager::indexForId(Core::Id id) const
{
for (int i = 0; i < d->devices.count(); ++i) {
if (deviceAt(i)->id() == id)
return i;
}
return -1;
}
Core::Id DeviceManager::deviceId(const IDevice::ConstPtr &device) const
{
return device ? device->id() : IDevice::invalidId();
}
int DeviceManager::indexOf(const IDevice::ConstPtr &device) const
{
return indexForId(device->id());
}
void DeviceManager::ensureOneDefaultDevicePerType()
{
foreach (const IDevice::Ptr &device, d->devices) {
......
......@@ -67,9 +67,6 @@ public:
bool hasDevice(const QString &name) const;
Core::Id deviceId(const IDevice::ConstPtr &device) const;
int indexForId(Core::Id id) const;
int indexOf(const IDevice::ConstPtr &device) const;
void addDevice(const IDevice::Ptr &device);
void removeDevice(Core::Id id);
......
......@@ -56,6 +56,7 @@ public:
IDevice::ConstPtr device(int pos) const;
Core::Id deviceId(int pos) const;
int indexOf(IDevice::ConstPtr dev) const;
int indexForId(Core::Id id) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
private slots:
......@@ -67,8 +68,6 @@ private slots:
private:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int indexForId(Core::Id id) const;
Internal::DeviceManagerModelPrivate * const d;
};
......
......@@ -271,7 +271,7 @@ void DeviceSettingsWidget::setDefaultDevice()
void DeviceSettingsWidget::handleDeviceUpdated(Id id)
{
const int index = m_deviceManager->indexForId(id);
const int index = m_deviceManagerModel->indexForId(id);
if (index == currentIndex())
currentDeviceChanged(index);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment