Commit 5b9b8f19 authored by Michael Weghorn's avatar Michael Weghorn Committed by Jan Grulich
Browse files

FileChooser portal: Handle 'current_filter'

From FileChooser portal documentation [1]:

> current_filter (sa(us))
>
>     Request that this filter be set by default at dialog creation. If
>     the filters list is nonempty, it should match a filter in the list
>     to set the default filter from the list. Alternatively, it may be
>     specified when the list is empty to apply the filter
>     unconditionally.

The implemented approach adds the filter specified by current_filter
regardless of whether the passed filters list is empty or not.

Since KFileWidget always seems to select the first filter in the
list it was passed and there seems to be no way to explicitly select
a different one, this makes sure the the filter specified by
'current_filter' is the first one in the corresponding list.

[1] https://flatpak.github.io/xdg-desktop-portal/portal-docs.html#gdbus-org.freedesktop.impl.portal.FileChooser
parent cf1c33a9
......@@ -186,6 +186,25 @@ uint FileChooserPortal::OpenFile(const QDBusObjectPath &handle,
}
}
if (options.contains(QStringLiteral("current_filter"))) {
FilterList filterList = qdbus_cast<FilterList>(options.value(QStringLiteral("current_filter")));
if (filterList.filters.size() == 1) {
// make the relevant entry the first one in the list of filters,
// since that is the one that gets preselected by KFileWidget
Filter filterStruct = filterList.filters.at(0);
if (filterStruct.type == 0) {
QString nameFilter = QStringLiteral("%1|%2").arg(filterStruct.filterString).arg(filterList.userVisibleName);
nameFilters.removeAll(nameFilter);
nameFilters.push_front(nameFilter);
} else {
mimeTypeFilters.removeAll(filterStruct.filterString);
mimeTypeFilters.push_front(filterStruct.filterString);
}
} else {
qCDebug(XdgDesktopPortalKdeFileChooser) << "Ignoring 'current_filter' parameter with 0 or multiple filters specified.";
}
}
QScopedPointer<FileDialog, QScopedPointerDeleteLater> fileDialog(new FileDialog());
Utils::setParentWindow(fileDialog.data(), parent_window);
fileDialog->setWindowTitle(title);
......@@ -285,6 +304,25 @@ uint FileChooserPortal::SaveFile(const QDBusObjectPath &handle,
}
}
if (options.contains(QStringLiteral("current_filter"))) {
FilterList filterList = qdbus_cast<FilterList>(options.value(QStringLiteral("current_filter")));
if (filterList.filters.size() == 1) {
// make the relevant entry the first one in the list of filters,
// since that is the one that gets preselected by KFileWidget
Filter filterStruct = filterList.filters.at(0);
if (filterStruct.type == 0) {
QString nameFilter = QStringLiteral("%1|%2").arg(filterStruct.filterString).arg(filterList.userVisibleName);
nameFilters.removeAll(nameFilter);
nameFilters.push_front(nameFilter);
} else {
mimeTypeFilters.removeAll(filterStruct.filterString);
mimeTypeFilters.push_front(filterStruct.filterString);
}
} else {
qCDebug(XdgDesktopPortalKdeFileChooser) << "Ignoring 'current_filter' parameter with 0 or multiple filters specified.";
}
}
QScopedPointer<FileDialog, QScopedPointerDeleteLater> fileDialog(new FileDialog());
Utils::setParentWindow(fileDialog.data(), parent_window);
fileDialog->setWindowTitle(title);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment