From 67021908118b063cf28742dd27fa235c3e47763b Mon Sep 17 00:00:00 2001 From: Mavroudis Chatzilazaridis Date: Sun, 19 May 2024 21:28:08 +0300 Subject: [PATCH] urlhandler: Fix devicePicker selection with --device When using 'Send a file/URL' in the indicator with multiple devices, the device the user selects for file sharing is not automatically selected in the URL handler dialog that follows. This happens because `model.rowForDevice(deviceId)` is called without waiting for the model to be populated, always returning -1. Resolving this exposed another bug, where the selected device was still not the one picked. This happens because `proxyModel` is used to fill the `QComboBox` but `model` is used to find the correct index. Since `DevicesPluginFilterProxyModel` sorts and filters the data from `model` the indices do not match. This commit addresses these issues by deferring the device selection until data is available and implementing `rowForDevice` for filtered/sorted data in `DevicesPluginFilterProxyModel`. --- interfaces/devicespluginfilterproxymodel.cpp | 10 ++++++++++ interfaces/devicespluginfilterproxymodel.h | 1 + urlhandler/kdeconnect-handler.cpp | 13 ++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/interfaces/devicespluginfilterproxymodel.cpp b/interfaces/devicespluginfilterproxymodel.cpp index 4c0602d97..960b7a312 100644 --- a/interfaces/devicespluginfilterproxymodel.cpp +++ b/interfaces/devicespluginfilterproxymodel.cpp @@ -23,4 +23,14 @@ QString DevicesPluginFilterProxyModel::pluginFilter() const return m_pluginFilter; } +int DevicesPluginFilterProxyModel::rowForDevice(const QString &id) const +{ + for (int i = 0, c = rowCount(); i < c; ++i) { + if (data(index(i, 0), DevicesModel::IdModelRole).value() == id) { + return i; + } + } + return -1; +} + #include "moc_devicespluginfilterproxymodel.cpp" diff --git a/interfaces/devicespluginfilterproxymodel.h b/interfaces/devicespluginfilterproxymodel.h index d0d5ddf97..006fa0ee2 100644 --- a/interfaces/devicespluginfilterproxymodel.h +++ b/interfaces/devicespluginfilterproxymodel.h @@ -19,6 +19,7 @@ public: bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; void setPluginFilter(const QString plugin); QString pluginFilter() const; + int rowForDevice(const QString &id) const; private: QString m_pluginFilter; diff --git a/urlhandler/kdeconnect-handler.cpp b/urlhandler/kdeconnect-handler.cpp index d356069a7..97fd8ed51 100644 --- a/urlhandler/kdeconnect-handler.cpp +++ b/urlhandler/kdeconnect-handler.cpp @@ -86,7 +86,18 @@ int main(int argc, char **argv) uidialog.devicePicker->setModel(&proxyModel); if (!deviceId.isEmpty()) { - uidialog.devicePicker->setCurrentIndex(model.rowForDevice(deviceId)); + // This is done on rowsInserted because the model isn't populated yet + QObject::connect(&model, &QAbstractItemModel::rowsInserted, &app, [&uidialog, &deviceId, &proxyModel](const QModelIndex &parent, int first) { + Q_UNUSED(parent); + /** + * We want to run this only once, but on startup the data is populated, removed then repopulated (rowsInserted() -> rowsRemoved() -> + * rowsInserted()). Thus by running only when there were no devices previously, it ensures that the user's selection doesn't change without them + * realising. + */ + if (first == 0) { + uidialog.devicePicker->setCurrentIndex(proxyModel.rowForDevice(deviceId)); + } + }); } uidialog.openOnPeerCheckBox->setChecked(open);