Improve filtering of devices in the DevicesModel

We need to remove devices from the model if the filter doesn't match, to
prevent listing disconnected devices in the plasmoid.

REVIEW: 127610
This commit is contained in:
David Kahles 2016-04-01 15:01:42 +02:00
parent 337dd191e4
commit 3103bd0c5a
2 changed files with 31 additions and 14 deletions

View file

@ -102,10 +102,7 @@ void DevicesModel::deviceAdded(const QString& id)
DeviceDbusInterface* dev = new DeviceDbusInterface(id, this);
Q_ASSERT(dev->isValid());
bool onlyPaired = (m_displayFilter & StatusFilterFlag::Paired);
bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable);
if ((onlyReachable && !dev->isReachable()) || (onlyPaired && !dev->isPaired())) {
if (! passesFilter(dev)) {
delete dev;
return;
}
@ -127,22 +124,30 @@ void DevicesModel::deviceRemoved(const QString& id)
void DevicesModel::deviceUpdated(const QString& id, bool isVisible)
{
Q_UNUSED(isVisible);
int row = rowForDevice(id);
if (row < 0 && isVisible) {
if (row < 0) {
// FIXME: when m_dbusInterface is not valid refreshDeviceList() does
// nothing and we can miss some devices.
// Someone can reproduce this problem by restarting kdeconnectd while
// kdeconnect's plasmoid is still running.
qCDebug(KDECONNECT_INTERFACES) << "Adding missing device" << id;
// Another reason for this branch is that we removed the device previously
// because of the filter settings.
qCDebug(KDECONNECT_INTERFACES) << "Adding missing or previously removed device" << id;
deviceAdded(id);
row = rowForDevice(id);
}
if (row >= 0) {
} else {
DeviceDbusInterface *dev = getDevice(row);
if (! passesFilter(dev)) {
beginRemoveRows(QModelIndex(), row, row);
delete m_deviceList.takeAt(row);
endRemoveRows();
qCDebug(KDECONNECT_INTERFACES) << "Removed changed device " << id;
} else {
const QModelIndex idx = index(row);
Q_EMIT dataChanged(idx, idx);
}
}
}
int DevicesModel::displayFilter() const
@ -295,3 +300,11 @@ int DevicesModel::rowCount(const QModelIndex& parent) const
return m_deviceList.size();
}
bool DevicesModel::passesFilter(DeviceDbusInterface* dev) const
{
bool onlyPaired = (m_displayFilter & StatusFilterFlag::Paired);
bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable);
return !((onlyReachable && !dev->isReachable()) || (onlyPaired && !dev->isPaired()));
}

View file

@ -48,10 +48,13 @@ public:
DeviceRole
};
Q_ENUMS(ModelRoles);
// A device is always paired or reachable or both
// You can combine the Paired and Reachable flags
enum StatusFilterFlag {
NoFilter = 0x00,
Paired = 0x01,
Reachable = 0x02
Paired = 0x01, // show device only if it's paired
Reachable = 0x02 // show device only if it's reachable
};
Q_DECLARE_FLAGS(StatusFilterFlags, StatusFilterFlag)
Q_FLAGS(StatusFilterFlags)
@ -84,6 +87,7 @@ private:
int rowForDevice(const QString& id) const;
void clearDevices();
void appendDevice(DeviceDbusInterface* dev);
bool passesFilter(DeviceDbusInterface *dev) const;
DaemonDbusInterface* m_dbusInterface;
QVector<DeviceDbusInterface*> m_deviceList;