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`.
This commit is contained in:
Mavroudis Chatzilazaridis 2024-05-19 21:28:08 +03:00 committed by Albert Vaca Cintora
parent a9a3661cf3
commit 6702190811
3 changed files with 23 additions and 1 deletions

View file

@ -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<QString>() == id) {
return i;
}
}
return -1;
}
#include "moc_devicespluginfilterproxymodel.cpp"

View file

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

View file

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