Bug 427469 - NotShowIn/OnlyShowIn overrides the user-selected apps saved in mimeapps.list
Summary: NotShowIn/OnlyShowIn overrides the user-selected apps saved in mimeapps.list
Status: RESOLVED FIXED
Alias: None
Product: frameworks-kservice
Classification: Frameworks and Libraries
Component: general (show other bugs)
Version: 5.74.0
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: David Faure
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-09 00:56 UTC by Martin
Modified: 2020-10-28 19:10 UTC (History)
5 users (show)

See Also:
Latest Commit:
Version Fixed In: 5.76


Attachments
~/.config/mimeapps.list (1.67 KB, text/plain)
2020-10-09 00:56 UTC, Martin
Details
Working entry - /usr/share/applications/kindlecomicconverter.desktop (323 bytes, application/x-desktop)
2020-10-09 00:57 UTC, Martin
Details
Broken entry - /usr/share/applications/org.gnome.FileRoller.desktop (15.70 KB, application/x-desktop)
2020-10-09 00:58 UTC, Martin
Details
unittest (3.82 KB, patch)
2020-10-22 23:35 UTC, David Faure
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Martin 2020-10-09 00:56:07 UTC
Created attachment 132225 [details]
~/.config/mimeapps.list

SUMMARY
If I open a .7z archive in an app like Chromium, it will consult xdg-open, which will check that the environment is KDE, which will result it in taking a kde-open5 path, which will use ktraderclient5, which will ignore entries that have a corresponding .desktop entry with NotShowIn=KDE if current environment is KDE;

I think the correct behavior here would be mimeapps.list overriding the .desktop NotShowIn rule.

STEPS TO REPRODUCE
1. Set file-roller to open 7z(application/x-7z-compressed) files in mimeapps.list
2. Set NotShowIn=KDE; in file-roller's .desktop entry(/usr/share/applications/org.gnome.FileRoller.desktop), which is already the default on Arch
3. Test what .7z is going to be opened with, either via Chromium, or this handy shell command:
  % XDG_CURRENT_DESKTOP="KDE" /usr/bin/ktraderclient5 --mimetype application/x-7z-compressed | grep EntryPath | head -n1 
DesktopEntryPath : '/usr/share/applications/kindlecomicconverter.desktop'

  % XDG_CURRENT_DESKTOP="notKDE" /usr/bin/ktraderclient5 --mimetype application/x-7z-compressed | grep EntryPath | head -n1
DesktopEntryPath : '/usr/share/applications/org.gnome.FileRoller.desktop'

OBSERVED RESULT
Downloaded .7z archives opened via Chromium are opened by an app that's not the system default

EXPECTED RESULT
Downloaded .7z archives opened via Chromium are opened in file-roller, which is defined as the default app in mimeapps.list


SOFTWARE/OS VERSIONS
Operating System: Arch Linux
KDE Plasma Version: 5.19.5
KDE Frameworks Version: 5.74.0
Qt Version: 5.15.1
Kernel Version: 5.8.13-arch1-1
OS Type: 64-bit
Processors: 12 × AMD Ryzen 5 3600 6-Core Processor
Memory: 31.4 GiB of RAM
Graphics Processor: GeForce GTX 1080 Ti/PCIe/SSE2

ADDITIONAL INFORMATION
https://bugs.kde.org/show_bug.cgi?id=416950
Comment 1 Martin 2020-10-09 00:57:51 UTC
Created attachment 132226 [details]
Working entry - /usr/share/applications/kindlecomicconverter.desktop
Comment 2 Martin 2020-10-09 00:58:54 UTC
Created attachment 132227 [details]
Broken entry - /usr/share/applications/org.gnome.FileRoller.desktop
Comment 3 David Faure 2020-10-22 23:35:13 UTC
Very good bug report, thanks. Detailed, and down to command-line calls.

That makes me want to fix it :-)

It's tricky though. We read both mimeapps.list and desktop files, write out an ordered list into the cache (ksycoca), then when it's time to find an app for a mimetype, we read that list from the cache and filter out services that shouldn't be shown in the current desktop (applyFilter() in kapplicationtrader.cpp). At that point we don't really know if the app is in the list because of mimeapps.list or because it was just a .desktop file being there (but not specifically chosen by the user) when we created the cache.

And we can't filter out based on current desktop at the time of creating the cache, because, well, technically the current desktop could change between the time of creating the cache and the time of using it.

The easy way out would be to say: NotShowIn is *only* about whether the app should be displayed in the K menu, it shouldn't prevent an application from being selected (even without user intervention) when clicking on a file. But I believe this would introduce unwanted behavior in some cases (let's say, like FileRoller being started for directories in Plasma, by default).

OK, so it's tricky indeed. Well, at least tonight I wrote the unittest for this (which also breaks if blindly removing the showInCurrentDesktop() check in applyFilter, i.e. it checks that nobody took the "easy way out" from the previous paragraph). TDD methodology: unittest written, next step is to come up with a fix.
Comment 4 David Faure 2020-10-22 23:35:36 UTC
Created attachment 132645 [details]
unittest
Comment 5 David Faure 2020-10-23 00:20:37 UTC
OK, actually, if I read NotShowIn/OnlyShowIn at cache creation time (but ignore it for things listed in mimeapps.list)... then I have a hard time coming up with a buggy corner case related to switching desktop environments. 
One would have to install an app with OnlyShowIn=KDE while in Gnome, run some KDE code there (so ksycoca gets updated while the current desktop isn't Plasma), switch to Plasma, do nothing that touches the mime configuration, and the KDE app is still ignored for the purpose of file associations.
And if that ever happens, there are easy workarounds (running kbuildsycoca5, installing/removing any other app, changing any file associations...).
Comment 6 Bug Janitor Service 2020-10-23 00:27:36 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/kservice/-/merge_requests/19
Comment 7 David Faure 2020-10-28 19:10:39 UTC
Git commit ade2064b971cbfdb76476d2ca8807dca2da29cb1 by David Faure.
Committed on 28/10/2020 at 19:07.
Pushed by dfaure into branch 'master'.

Allow NotShowIn=KDE apps, listed in mimeapps.list, to be used

It was unexpected for users that selecting e.g. Gnome FileRoller in
mimeapps.list for a given mimetype would still not make it associated
to that mimetype when in Plasma. NotShowIn is documented to be about
"displaying" applications (in the K menu and context menus), not about
completely forbidding users from associating such apps with a given
mimetype.

Note that if one wants different file associations per desktop, that's
supported with gnome-mimeapps.list and kde-mimeapps.list.

The fix is to check showInCurrentDesktop() at cache creation time
when collecting .desktop files, but to not check it for mimeapps.list entries.

The trick to make this work was that we still need to process ONE
servicetype for such "hidden" desktop files: the "Application"
servicetype (internal detail which can hopefully go away in KF6).
FIXED-IN: 5.76

M  +31   -0    autotests/kmimeassociationstest.cpp
M  +1    -1    src/services/kapplicationtrader.cpp
M  +2    -2    src/services/kservicefactory.cpp
M  +10   -0    src/services/kserviceoffer.cpp
M  +2    -0    src/services/kserviceoffer.h
M  +6    -0    src/sycoca/kbuildservicefactory.cpp

https://invent.kde.org/frameworks/kservice/commit/ade2064b971cbfdb76476d2ca8807dca2da29cb1