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);