Bug 497399 - FileChooser handles mixed types of filters inconsistently
Summary: FileChooser handles mixed types of filters inconsistently
Status: CONFIRMED
Alias: None
Product: xdg-desktop-portal-kde
Classification: Plasma
Component: general (other bugs)
Version First Reported In: 6.2.4
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Plasma Bugs List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-12-13 07:56 UTC by Alfred Wingate
Modified: 2024-12-15 15:57 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments
plasma/xdg-desktop-portal-kde: Don't ignore current_filter parameter with multiple filter strings (1.23 KB, patch)
2024-12-14 14:11 UTC, Alfred Wingate
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alfred Wingate 2024-12-13 07:56:22 UTC
SUMMARY
FileChooser mangles filters if mimetypes and globs are mixed. Even ignoring current_filter in some cases. 

I'll be using gdbus and GVariant format out of convenience. I initially encountered this behaviour in a GTK program after porting FileChooserDialog to FileChooserNative. GTK dialog worked as expected.


SCENARIO 1
Two filters both using globs.

$ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.FileChooser.OpenFile "" "Please choose a file" "{ 'filters': <[ ( 'Windows Executable/MSI Installer', [ (@u 0, '*.[mM][sS][iI]'), (@u 0, '*.[eE][xX][eE]') ]), ( 'Any file', [ (@u 0, '*') ]) ]> }"

{
    'filters': <[
        (
            'Windows Executable/MSI Installer',
            [
                (@u 0, '*.[mM][sS][iI]'),
                (@u 0, '*.[eE][xX][eE]')
            ]
        ),
        (
            'Any file',
            [
                (@u 0, '*')
            ]
        )
    ]>
}

RESULT
Works as expected with "Windows Executable/MSI Installer" as the default filter.


SCENARIO 2
Filter with one mimetype and a filter with one glob

$ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.FileChooser.OpenFile "" "Please choose a file" "{ 'filters': <[ ( 'Windows Executable', [ (@u 1, 'application/x-ms-dos-executable') ]), ( 'Any file', [ (@u 0, '*') ]) ]> }"

{
    'filters': <[
        (
            'Windows Executable',
            [
                (@u 1, 'application/x-ms-dos-executable')
            ]
        ),
        (
            'Any file',
            [
                (@u 0, '*')
            ]
        )
    ]>
}

RESULT
An additional filter "Windows Executable, Any file" is shown and selected. It only shows files of type application/x-ms-dos-executable.


SCENARIO 3
Filter with two mimetypes and a filter with one glob

$ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.FileChooser.OpenFile "" "Please choose a file" "{ 'multiple': <false>, 'filters': <[ ( 'Windows Executable/MSI Installer', [ (@u 1, 'application/x-ms-dos-executable'), (@u 1, 'application/x-msi') ]), ( 'Any file', [ (@u 0, '*') ]) ]> }"

{
    'multiple': <false>,
    'filters': <[
        (
            'Windows Executable/MSI Installer',
            [
                (@u 1, 'application/x-ms-dos-executable'),
                (@u 1, 'application/x-msi')
            ]
        ),
        (
            'Any file',
            [
                (@u 0, '*')
            ]
        )
    ]>
}

RESULT
An additional filter "Windows Executable/MSI Installer, Any file" is shown and selected. It shows nothing.


SCENARIO 4
Two filters both using globs, but with the second filter as the current_filter.

$ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.FileChooser.OpenFile "" "Please choose a file" "{ 'filters': <[ ( 'Windows Executable/MSI Installer', [ (@u 0, '*.[mM][sS][iI]'), (@u 0, '*.[eE][xX][eE]') ]), ( 'Any file', [ (@u 0, '*') ]) ]>, 'current_filter': <( 'Any file', [ (@u 0, '*') ])> }"

{
    'filters': <[
        (
            'Windows Executable/MSI Installer',
            [
                (@u 0, '*.[mM][sS][iI]'),
                (@u 0, '*.[eE][xX][eE]')
            ]
        ),
        (
            'Any file',
            [
                (@u 0, '*')
            ]
        )
    ]>,
    'current_filter': <(
        'Any file',
        [
            (@u 0, '*')
        ]
    )>
}

RESULT
Works as expected with "Any file" as the default filter.


SCENARIO 5
Filter with two mimetypes and a filter with one glob, but with the second filter as the current_filter.

$ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.FileChooser.OpenFile "" "Please choose a file" "{ 'filters': <[ ( 'Windows Executable/MSI Installer', [ (@u 1, 'application/x-ms-dos-executable'), (@u 1, 'application/x-msi') ]), ( 'Any file', [ (@u 0, '*') ]) ]>, 'current_filter': <( 'Any file', [ (@u 0, '*') ])> }"

{
    'filters': <[
        (
            'Windows Executable/MSI Installer',
            [
                (@u 1, 'application/x-ms-dos-executable'),
                (@u 1, 'application/x-msi')
            ]
        ),
        (
            'Any file',
            [
                (@u 0, '*')
            ]
        )
    ]>,
    'current_filter': <(
        'Any file',
        [
            (@u 0, '*')
        ]
    )>
}

RESULT
Works as expected with "Any file" as the default filter and no extra mangled filter.


SCENARIO 6
Filter with two mimetypes and a filter with one glob, but with the first filter as the current_filter.

$ gdbus call --session --dest org.freedesktop.portal.Desktop --object-path /org/freedesktop/portal/desktop --method org.freedesktop.portal.FileChooser.OpenFile "" "Please choose a file" "{ 'filters': <[ ( 'Windows Executable/MSI Installer', [ (@u 1, 'application/x-ms-dos-executable'), (@u 1, 'application/x-msi') ]), ( 'Any file', [ (@u 0, '*') ]) ]>, 'current_filter': <( 'Windows Executable/MSI Installer', [ (@u 1, 'application/x-ms-dos-executable'), (@u 1, 'application/x-msi') ])> }"

{
    'filters': <[
        (
            'Windows Executable/MSI Installer',
            [
                (@u 1, 'application/x-ms-dos-executable'),
                (@u 1, 'application/x-msi')
            ]
        ),
        (
            'Any file',
            [
                (@u 0, '*')
            ]
        )
    ]>,
    'current_filter': <(
        'Windows Executable/MSI Installer',
            [
                (@u 1, 'application/x-ms-dos-executable'),
                (@u 1, 'application/x-msi')
            ]

    )>
}

RESULT
An additional filter "Windows Executable/MSI Installer, Any file" is shown and selected. It shows nothing


SOFTWARE/OS VERSIONS
Operating System: Gentoo Linux 2.17
KDE Plasma Version: 6.2.4
KDE Frameworks Version: 6.8.0
Qt Version: 6.8.1
Kernel Version: 6.12.4-xanmod1-dist (64-bit)
Graphics Platform: Wayland
Processors: 12 × AMD Ryzen 5 3600 6-Core Processor
Memory: 47.0 GiB of RAM
Graphics Processor: AMD Radeon RX 5700 XT
Comment 1 Nate Graham 2024-12-13 18:44:33 UTC
Can reproduce.
Comment 2 Nicolas Fella 2024-12-14 13:46:09 UTC
The problem seems to be that when KCoreDirLister is passed both MIME filters and name filters it treats them so that both must match for a file to be shown, which is not what we want here
Comment 3 Alfred Wingate 2024-12-14 14:11:05 UTC
Created attachment 176601 [details]
plasma/xdg-desktop-portal-kde: Don't ignore current_filter parameter with multiple filter strings

The current_filter one is a different issue (Scenario 5 vs 6), as filters with more than one string* are explicitly ignored. 

With this patch it works as expected on my end. (Scenario 6 gets me two working filters and it picks "Windows Executable/MSI Installer" as the default one even if I change order of the filters)

I'm not familiar with portal internals so I don't how much input validation is needed here, but I don't think its right reject this.

* I don't know what the correct terminology is. pattern? filter rule?
Comment 4 Bug Janitor Service 2024-12-14 14:39:08 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/kio/-/merge_requests/1769
Comment 5 Nicolas Fella 2024-12-14 15:20:24 UTC
(In reply to Alfred Wingate from comment #3)
> Created attachment 176601 [details]
> plasma/xdg-desktop-portal-kde: Don't ignore current_filter parameter with
> multiple filter strings
> 
> The current_filter one is a different issue (Scenario 5 vs 6), as filters
> with more than one string* are explicitly ignored. 
> 
> With this patch it works as expected on my end. (Scenario 6 gets me two
> working filters and it picks "Windows Executable/MSI Installer" as the
> default one even if I change order of the filters)
> 
> I'm not familiar with portal internals so I don't how much input validation
> is needed here, but I don't think its right reject this.
> 
> * I don't know what the correct terminology is. pattern? filter rule?

You're right, this seems wrong and is a leftover from previous refactoring. Can you make a merge request with your change?
Comment 6 Bug Janitor Service 2024-12-14 15:31:33 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/merge_requests/336
Comment 7 Nicolas Fella 2024-12-14 19:28:49 UTC
Git commit 4655b7e8cd0109ac6168c4dd824c179638049511 by Nicolas Fella.
Committed on 14/12/2024 at 14:38.
Pushed by cullmann into branch 'master'.

[kfilefiltercombo] Fix creating 'All supported types' filter

We would only pass the mime types and discard the name patterns

M  +19   -0    autotests/kfilefiltercombotest.cpp
M  +6    -4    src/filewidgets/kfilefiltercombo.cpp

https://invent.kde.org/frameworks/kio/-/commit/4655b7e8cd0109ac6168c4dd824c179638049511
Comment 8 Nicolas Fella 2024-12-15 15:48:32 UTC
Git commit f5b2d63442dd569d66737631e60c1fc956a9d13f by Nicolas Fella, on behalf of Alfred Wingate.
Committed on 15/12/2024 at 15:46.
Pushed by nicolasfella into branch 'master'.

[FileChooser] Don't ignore current_filter parameter with multiple name patterns
Signed-off-by: Alfred Wingate <parona@protonmail.com>

M  +2    -2    src/filechooser.cpp

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/f5b2d63442dd569d66737631e60c1fc956a9d13f
Comment 9 Nicolas Fella 2024-12-15 15:57:56 UTC
Git commit e92de218b2e1bd75167a4bede437fe52db082c80 by Nicolas Fella, on behalf of Alfred Wingate.
Committed on 15/12/2024 at 15:57.
Pushed by nicolasfella into branch 'Plasma/6.2'.

[FileChooser] Don't ignore current_filter parameter with multiple name patterns
Signed-off-by: Alfred Wingate <parona@protonmail.com>
(cherry picked from commit f5b2d63442dd569d66737631e60c1fc956a9d13f)

M  +2    -2    src/filechooser.cpp

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/e92de218b2e1bd75167a4bede437fe52db082c80