Handle case where a lister adds a device before loaded from database.

There is a small chance that a device lister is able to discover and add a
previously known device before it is added by the database loader thread.
In this case, copy the data that is user-settable to the existing DeviceInfo
object and destroy the object created from the database query.

This adds and utilizes a new FindEquivalentDevice method that compares the
device unique IDs. This could probably be made more robust as the unique
IDs for some listers may change. However, this is a problem with the database
storage implementation in general.
This commit is contained in:
Jim Broadus 2019-02-23 23:42:31 -08:00
parent a62062127e
commit d041da18cc
2 changed files with 25 additions and 3 deletions

View File

@ -158,9 +158,22 @@ void DeviceManager::AddDeviceFromDb(DeviceInfo* info) {
}
info->LoadIcon(icons, info->friendly_name_);
beginInsertRows(ItemToIndex(root_), devices_.count(), devices_.count());
devices_ << info;
endInsertRows();
DeviceInfo* existing = FindEquivalentDevice(info);
if (existing) {
qLog(Info) << "Found existing device: " << info->friendly_name_;
// Update user configuration from the database.
existing->icon_name_ = info->icon_name_;
existing->icon_ = info->icon_;
QModelIndex idx = ItemToIndex(existing);
if (idx.isValid()) emit dataChanged(idx, idx);
// Discard the info loaded from the database.
delete info;
} else {
qLog(Info) << "Device added from database: " << info->friendly_name_;
beginInsertRows(ItemToIndex(root_), devices_.count(), devices_.count());
devices_ << info;
endInsertRows();
}
}
QVariant DeviceManager::data(const QModelIndex& idx, int role) const {
@ -328,6 +341,14 @@ DeviceInfo* DeviceManager::FindDeviceByUrl(const QList<QUrl>& urls) const {
return nullptr;
}
DeviceInfo* DeviceManager::FindEquivalentDevice(DeviceInfo* info) const {
for (const DeviceInfo::Backend& backend : info->backends_) {
DeviceInfo* match = FindDeviceById(backend.unique_id_);
if (match) return match;
}
return nullptr;
}
void DeviceManager::PhysicalDeviceAdded(const QString& id) {
DeviceLister* lister = qobject_cast<DeviceLister*>(sender());

View File

@ -79,6 +79,7 @@ class DeviceManager : public SimpleTreeModel<DeviceInfo> {
DeviceInfo* FindDeviceById(const QString& id) const;
DeviceInfo* FindDeviceByUrl(const QList<QUrl>& url) const;
DeviceInfo* FindEquivalentDevice(DeviceInfo* info) const;
// Actions on devices
std::shared_ptr<ConnectedDevice> Connect(DeviceInfo* info);