Move icon loading to device model
This commit is contained in:
parent
4148c289af
commit
4e599e2aba
@ -64,14 +64,7 @@ void DeviceInfo::InitFromDb(const DeviceDatabaseBackend::Device &dev) {
|
||||
size_ = dev.size_;
|
||||
transcode_mode_ = dev.transcode_mode_;
|
||||
transcode_format_ = dev.transcode_format_;
|
||||
|
||||
QStringList icon_names = dev.icon_name_.split(',');
|
||||
QVariantList icons;
|
||||
for (const QString &icon_name : icon_names) {
|
||||
icons << icon_name;
|
||||
}
|
||||
|
||||
LoadIcon(icons, friendly_name_);
|
||||
icon_name_ = dev.icon_name_;
|
||||
|
||||
QStringList unique_ids = dev.unique_id_.split(',');
|
||||
for (const QString &id : unique_ids) {
|
||||
|
@ -108,6 +108,8 @@ DeviceManager::DeviceManager(Application *app, QObject *parent)
|
||||
backend_->moveToThread(app_->database()->thread());
|
||||
backend_->Init(app_->database());
|
||||
|
||||
connect(this, SIGNAL(DeviceCreatedFromDB(DeviceInfo*)), SLOT(AddDeviceFromDB(DeviceInfo*)));
|
||||
|
||||
// This reads from the database and contents on the database mutex, which can be very slow on startup.
|
||||
ConcurrentRun::Run<void>(&thread_pool_, bind(&DeviceManager::LoadAllDevices, this));
|
||||
|
||||
@ -174,9 +176,34 @@ void DeviceManager::LoadAllDevices() {
|
||||
|
||||
DeviceDatabaseBackend::DeviceList devices = backend_->GetAllDevices();
|
||||
for (const DeviceDatabaseBackend::Device &device : devices) {
|
||||
beginInsertRows(ItemToIndex(root_), devices_.count(), devices_.count());
|
||||
DeviceInfo *info = new DeviceInfo(DeviceInfo::Type_Device, root_);
|
||||
info->InitFromDb(device);
|
||||
emit DeviceCreatedFromDB(info);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DeviceManager::AddDeviceFromDB(DeviceInfo *info) {
|
||||
|
||||
QStringList icon_names = info->icon_name_.split(',');
|
||||
QVariantList icons;
|
||||
for (const QString &icon_name : icon_names) {
|
||||
icons << icon_name;
|
||||
}
|
||||
info->LoadIcon(icons, info->friendly_name_);
|
||||
|
||||
DeviceInfo *existing = FindEquivalentDevice(info);
|
||||
if (existing) {
|
||||
qLog(Info) << "Found existing device: " << info->friendly_name_;
|
||||
existing->icon_name_ = info->icon_name_;
|
||||
existing->icon_ = info->icon_;
|
||||
QModelIndex idx = ItemToIndex(existing);
|
||||
if (idx.isValid()) emit dataChanged(idx, idx);
|
||||
delete info;
|
||||
}
|
||||
else {
|
||||
qLog(Info) << "Device added from database: " << info->friendly_name_;
|
||||
beginInsertRows(ItemToIndex(root_), devices_.count(), devices_.count());
|
||||
devices_ << info;
|
||||
endInsertRows();
|
||||
}
|
||||
@ -336,6 +363,16 @@ DeviceInfo *DeviceManager::FindDeviceByUrl(const QList<QUrl> &urls) const {
|
||||
|
||||
}
|
||||
|
||||
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());
|
||||
|
@ -97,6 +97,7 @@ class DeviceManager : public SimpleTreeModel<DeviceInfo> {
|
||||
DeviceInfo *FindDeviceById(const QString &id) const;
|
||||
DeviceInfo *FindDeviceByUrl(const QList<QUrl> &url) const;
|
||||
QString DeviceNameByID(QString unique_id);
|
||||
DeviceInfo *FindEquivalentDevice(DeviceInfo *info) const;
|
||||
|
||||
// Actions on devices
|
||||
std::shared_ptr<ConnectedDevice> Connect(DeviceInfo *info);
|
||||
@ -116,6 +117,7 @@ class DeviceManager : public SimpleTreeModel<DeviceInfo> {
|
||||
signals:
|
||||
void DeviceConnected(QModelIndex idx);
|
||||
void DeviceDisconnected(QModelIndex idx);
|
||||
void DeviceCreatedFromDB(DeviceInfo* info);
|
||||
|
||||
private slots:
|
||||
void PhysicalDeviceAdded(const QString &id);
|
||||
@ -126,6 +128,7 @@ class DeviceManager : public SimpleTreeModel<DeviceInfo> {
|
||||
void DeviceSongCountUpdated(int count);
|
||||
void LoadAllDevices();
|
||||
void DeviceConnectFinished(const QString &id, bool success);
|
||||
void AddDeviceFromDB(DeviceInfo *info);
|
||||
|
||||
protected:
|
||||
void LazyPopulate(DeviceInfo *item) { LazyPopulate(item, true); }
|
||||
|
@ -27,9 +27,8 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QAbstractItemModel>
|
||||
#include <QModelIndex>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "devicemanager.h"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user