Some TODOs done :)
Also renamed the Status flags enum to make it more understandable
This commit is contained in:
parent
77256e1932
commit
fd65570487
2 changed files with 59 additions and 52 deletions
|
@ -23,7 +23,6 @@
|
||||||
|
|
||||||
#include <KLocalizedString>
|
#include <KLocalizedString>
|
||||||
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QDBusInterface>
|
#include <QDBusInterface>
|
||||||
#include <QDBusPendingReply>
|
#include <QDBusPendingReply>
|
||||||
|
@ -37,7 +36,7 @@ Q_LOGGING_CATEGORY(KDECONNECT_INTERFACES, "kdeconnect.interfaces");
|
||||||
DevicesModel::DevicesModel(QObject *parent)
|
DevicesModel::DevicesModel(QObject *parent)
|
||||||
: QAbstractListModel(parent)
|
: QAbstractListModel(parent)
|
||||||
, m_dbusInterface(new DaemonDbusInterface(this))
|
, m_dbusInterface(new DaemonDbusInterface(this))
|
||||||
, m_displayFilter(DevicesModel::StatusUnknown)
|
, m_displayFilter(StatusFilterFlag::NoFilter)
|
||||||
{
|
{
|
||||||
|
|
||||||
//new ModelTest(this, this);
|
//new ModelTest(this, this);
|
||||||
|
@ -71,47 +70,49 @@ 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)
|
void DevicesModel::deviceAdded(const QString& id)
|
||||||
{
|
{
|
||||||
//Find whether the device already exists. if so, just report it as modified
|
if (m_deviceIndexById.contains(id)) {
|
||||||
int row = rowForDeviceId(id);
|
Q_ASSERT(false); //This should only happen for new devices
|
||||||
if (row>=0) {
|
return;
|
||||||
const QModelIndex idx = index(row, 0);
|
|
||||||
Q_EMIT dataChanged(idx, idx);
|
|
||||||
} else {
|
|
||||||
DeviceDbusInterface* dev = new DeviceDbusInterface(id, this);
|
|
||||||
if (dev->isReachable() == bool(m_displayFilter & StatusReachable) && dev->isPaired() == bool(m_displayFilter & StatusPaired)) {
|
|
||||||
beginInsertRows(QModelIndex(), m_deviceList.count(), m_deviceList.count());
|
|
||||||
appendDevice(dev);
|
|
||||||
endInsertRows();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DeviceDbusInterface* dev = new DeviceDbusInterface(id, this);
|
||||||
|
|
||||||
|
bool onlyPaired = (m_displayFilter & StatusFilterFlag::Paired);
|
||||||
|
bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable);
|
||||||
|
|
||||||
|
if ((onlyReachable && !dev->isReachable()) || (onlyPaired && !dev->isPaired())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
beginInsertRows(QModelIndex(), m_deviceList.size(), m_deviceList.size());
|
||||||
|
appendDevice(dev);
|
||||||
|
endInsertRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicesModel::deviceRemoved(const QString& id)
|
void DevicesModel::deviceRemoved(const QString& id)
|
||||||
{
|
{
|
||||||
//TODO: Actually remove instead of refresh
|
if (!m_deviceIndexById.contains(id)) {
|
||||||
Q_UNUSED(id);
|
Q_ASSERT(false); //This should only be emited for existing devices
|
||||||
refreshDeviceList();
|
return;
|
||||||
|
}
|
||||||
|
const int row = m_deviceIndexById.take(id);
|
||||||
|
beginRemoveRows(QModelIndex(), row, row);
|
||||||
|
delete m_deviceList.takeAt(row);
|
||||||
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevicesModel::deviceStatusChanged(const QString& id)
|
void DevicesModel::deviceStatusChanged(const QString& id)
|
||||||
{
|
{
|
||||||
Q_UNUSED(id);
|
if (!m_deviceIndexById.contains(id)) {
|
||||||
//FIXME: Emitting dataChanged does not invalidate the view, refreshDeviceList does.
|
Q_ASSERT(false); //This should only be emited for existing devices
|
||||||
//Q_EMIT dataChanged(index(0),index(rowCount()));
|
return;
|
||||||
refreshDeviceList();
|
}
|
||||||
|
|
||||||
|
int row = m_deviceIndexById[id];
|
||||||
|
const QModelIndex idx = index(row);
|
||||||
|
Q_EMIT dataChanged(idx, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DevicesModel::displayFilter() const
|
int DevicesModel::displayFilter() const
|
||||||
|
@ -121,7 +122,7 @@ int DevicesModel::displayFilter() const
|
||||||
|
|
||||||
void DevicesModel::setDisplayFilter(int flags)
|
void DevicesModel::setDisplayFilter(int flags)
|
||||||
{
|
{
|
||||||
m_displayFilter = (StatusFlag)flags;
|
m_displayFilter = (StatusFilterFlag)flags;
|
||||||
refreshDeviceList();
|
refreshDeviceList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,8 +134,8 @@ void DevicesModel::refreshDeviceList()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool onlyPaired = (m_displayFilter & StatusPaired);
|
bool onlyPaired = (m_displayFilter & StatusFilterFlag::Paired);
|
||||||
bool onlyReachable = (m_displayFilter & StatusReachable);
|
bool onlyReachable = (m_displayFilter & StatusFilterFlag::Reachable);
|
||||||
|
|
||||||
QDBusPendingReply<QStringList> pendingDeviceIds = m_dbusInterface->devices(onlyReachable, onlyPaired);
|
QDBusPendingReply<QStringList> pendingDeviceIds = m_dbusInterface->devices(onlyReachable, onlyPaired);
|
||||||
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingDeviceIds, this);
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingDeviceIds, this);
|
||||||
|
@ -154,6 +155,7 @@ void DevicesModel::receivedDeviceList(QDBusPendingCallWatcher* watcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_ASSERT(m_deviceList.isEmpty());
|
Q_ASSERT(m_deviceList.isEmpty());
|
||||||
|
Q_ASSERT(m_deviceIndexById.isEmpty());
|
||||||
const QStringList deviceIds = pendingDeviceIds.value();
|
const QStringList deviceIds = pendingDeviceIds.value();
|
||||||
|
|
||||||
if (deviceIds.isEmpty())
|
if (deviceIds.isEmpty())
|
||||||
|
@ -168,6 +170,7 @@ void DevicesModel::receivedDeviceList(QDBusPendingCallWatcher* watcher)
|
||||||
|
|
||||||
void DevicesModel::appendDevice(DeviceDbusInterface* dev)
|
void DevicesModel::appendDevice(DeviceDbusInterface* dev)
|
||||||
{
|
{
|
||||||
|
m_deviceIndexById.insert(dev->id(), m_deviceList.size());
|
||||||
m_deviceList.append(dev);
|
m_deviceList.append(dev);
|
||||||
connect(dev, SIGNAL(nameChanged(QString)), SLOT(nameChanged(QString)));
|
connect(dev, SIGNAL(nameChanged(QString)), SLOT(nameChanged(QString)));
|
||||||
}
|
}
|
||||||
|
@ -175,9 +178,12 @@ void DevicesModel::appendDevice(DeviceDbusInterface* dev)
|
||||||
void DevicesModel::nameChanged(const QString& newName)
|
void DevicesModel::nameChanged(const QString& newName)
|
||||||
{
|
{
|
||||||
Q_UNUSED(newName);
|
Q_UNUSED(newName);
|
||||||
int row = m_deviceList.indexOf(static_cast<DeviceDbusInterface*>(sender()));
|
|
||||||
Q_ASSERT(row>=0);
|
DeviceDbusInterface* device = static_cast<DeviceDbusInterface*>(sender());
|
||||||
const QModelIndex idx = index(row, 0);
|
|
||||||
|
int row = m_deviceIndexById[device->id()];
|
||||||
|
Q_ASSERT(row >= 0);
|
||||||
|
const QModelIndex idx = index(row);
|
||||||
Q_EMIT dataChanged(idx, idx);
|
Q_EMIT dataChanged(idx, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,6 +193,7 @@ void DevicesModel::clearDevices()
|
||||||
beginRemoveRows(QModelIndex(), 0, m_deviceList.size() - 1);
|
beginRemoveRows(QModelIndex(), 0, m_deviceList.size() - 1);
|
||||||
qDeleteAll(m_deviceList);
|
qDeleteAll(m_deviceList);
|
||||||
m_deviceList.clear();
|
m_deviceList.clear();
|
||||||
|
m_deviceIndexById.clear();
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,10 +228,10 @@ QVariant DevicesModel::data(const QModelIndex& index, int role) const
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
case StatusModelRole: {
|
case StatusModelRole: {
|
||||||
int status = StatusUnknown;
|
int status = StatusFilterFlag::NoFilter;
|
||||||
if (device->isReachable()) {
|
if (device->isReachable()) {
|
||||||
status |= StatusReachable;
|
status |= StatusFilterFlag::Reachable;
|
||||||
if (device->isPaired()) status |= StatusPaired;
|
if (device->isPaired()) status |= StatusFilterFlag::Paired;
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,14 +48,14 @@ public:
|
||||||
DeviceRole
|
DeviceRole
|
||||||
};
|
};
|
||||||
Q_ENUMS(ModelRoles);
|
Q_ENUMS(ModelRoles);
|
||||||
enum StatusFlag {
|
enum StatusFilterFlag {
|
||||||
StatusUnknown = 0x00,
|
NoFilter = 0x00,
|
||||||
StatusPaired = 0x01,
|
Paired = 0x01,
|
||||||
StatusReachable = 0x02
|
Reachable = 0x02
|
||||||
};
|
};
|
||||||
Q_DECLARE_FLAGS(StatusFlags, StatusFlag)
|
Q_DECLARE_FLAGS(StatusFilterFlags, StatusFilterFlag)
|
||||||
Q_FLAGS(StatusFlags)
|
Q_FLAGS(StatusFilterFlags)
|
||||||
Q_ENUMS(StatusFlag)
|
Q_ENUMS(StatusFilterFlag)
|
||||||
|
|
||||||
DevicesModel(QObject *parent = 0);
|
DevicesModel(QObject *parent = 0);
|
||||||
virtual ~DevicesModel();
|
virtual ~DevicesModel();
|
||||||
|
@ -84,14 +84,14 @@ Q_SIGNALS:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void clearDevices();
|
void clearDevices();
|
||||||
int rowForDeviceId(const QString& id) const;
|
|
||||||
void appendDevice(DeviceDbusInterface* dev);
|
void appendDevice(DeviceDbusInterface* dev);
|
||||||
|
|
||||||
DaemonDbusInterface* m_dbusInterface;
|
DaemonDbusInterface* m_dbusInterface;
|
||||||
QVector<DeviceDbusInterface*> m_deviceList;
|
QList<DeviceDbusInterface*> m_deviceList;
|
||||||
StatusFlags m_displayFilter;
|
QMap<QString, int> m_deviceIndexById;
|
||||||
|
StatusFilterFlag m_displayFilter;
|
||||||
};
|
};
|
||||||
|
|
||||||
//Q_DECLARE_OPERATORS_FOR_FLAGS(DevicesModel::StatusFlags)
|
//Q_DECLARE_OPERATORS_FOR_FLAGS(DevicesModel::StatusFilterFlag)
|
||||||
|
|
||||||
#endif // DEVICESMODEL_H
|
#endif // DEVICESMODEL_H
|
||||||
|
|
Loading…
Reference in a new issue