Bug 477961

Summary: Cannot save as JPEG after updating to KDE 6 beta
Product: [Applications] Spectacle Reporter: alexmateescu
Component: GeneralAssignee: Noah Davis <noahadvs>
Status: RESOLVED UPSTREAM    
Severity: normal CC: faure, kde, kde, mike, nate, nicolas.fella, putr4.s
Priority: NOR Keywords: qt6
Version First Reported In: 24.01.80   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description alexmateescu 2023-12-03 00:34:51 UTC
SUMMARY
***
Cannot save screen capture as JPEG after updating to KDE 6 beta
***


STEPS TO REPRODUCE
1. Take a screenshot
2. Select "Save As..."
3. Change file type to JPEG
4. Hit "Save"

OBSERVED RESULT
An error says QImageWriter (or something like that) cannot write the requested format. Still leaves a 0 byte file behind though.

EXPECTED RESULT
Image is saved as a JPEG.


Operating System: Arch Linux 
KDE Plasma Version: 5.90.0
KDE Frameworks Version: 5.246.0
Qt Version: 6.6.1
Kernel Version: 6.6.3-arch1-1 (64-bit)
Graphics Platform: X11
Processors: 16 × 12th Gen Intel® Core™ i5-12600K
Memory: 31.1 GiB of RAM
Graphics Processor: NVIDIA GeForce GTX 1060 6GB/PCIe/SSE2
Manufacturer: ASUS

ADDITIONAL INFORMATION
The problem is, after the update, the only registered file name extension for JPEG is JFIF and, for some reason QImageWriter cannot handle that. I am not sure whether JFIF being the only registered file name extension is a bug on its own, but replacing it with the proper JPEG extension in the file associations panel fixes the problem - QImageWriter is able to write JPEGs again.
NB Other file types may also be affected, I haven't tried them all.
Comment 1 Prajna Sariputra 2023-12-03 06:47:11 UTC
This doesn't seem to be a problem specific to Spectacle, I tried using Kolourpaint (from kdesrc-build) to save to a JPEG file and it too showed the same behaviour with showing JFIF as the file extension, and naturally saving doesn't actually work like that either.
Comment 2 Nicolas Fella 2023-12-03 15:11:04 UTC
The culprit seems to be shared-mime-info 2.4. It works fine with 2.2
Comment 3 Nicolas Fella 2023-12-03 15:41:14 UTC
https://gitlab.freedesktop.org/xdg/shared-mime-info/-/commit/35eca7eccc1a56a1d195af43ff37c8a7f7fbd67b added .jfif as an additional extension to image/jpeg.

When saving the image we use QMimeDataBase/QMimeType to get the extension for image/jpeg: https://invent.kde.org/frameworks/kio/-/blob/master/src/filewidgets/kfilewidget.cpp#L2257

mime.preferredSuffix() gives us jfif, presumably because that comes first in the alphabet and there is no other preference specified in shared-mime-info. I'd expect either *.jpeg or *.jpg to be the preferred one since those are the commonly used ones. David, something to fix in shared-mime-info?

mime.globPatterns() gives ("*.jfif") only, I'd expect that to return all globs specified in shared-mime-info. A bug in QMimeDataBase?

The fact that QImageReader doesn't know jfif is probably another bug in itself
Comment 4 David Faure 2023-12-04 15:50:51 UTC
@Nicolas I'm very surprised.
tst_QMimeDatabase::suffixes_data() makes sure that db.mimeTypeForName("image/jpeg") returns a QMimeType that says preferredSuffix() == "jpg", when using (only) the shared-mime-info mimetype definitions. Would you happen to have anything else in /usr/share/mime/packages or ~/.local/share/mime/packages?
For investigation you can use
grep image/jpeg /usr/share/mime/globs2 
grep image/jpeg ~/.local/share/mime/globs2
Here I get the expected 4 lines from the first grep, and nothing from the second grep.
And /usr/share/mime/image/jpeg.xml has all 4, with *.jpg first (which is what makes it the preferred suffix).
Do you (or anyone else here) get different results?

*.jfif was added in shared-mime-info 2.4 (latest release), earlier releases are not relevant indeed.
But make sure you don't have two versions installed at the same time.
Comment 5 Prajna Sariputra 2023-12-04 22:57:10 UTC
The `grep image/jpeg /usr/share/mime/globs2` command returns:
50:image/jpeg:*.jfif
50:image/jpeg:*.jpeg
50:image/jpeg:*.jpe
50:image/jpeg:*.jpg

The `grep image/jpeg ~/.local/share/mime/globs2` command returns:
50:image/jpeg:*.jfif

I'm also on Arch Linux for what it's worth.
Comment 6 Prajna Sariputra 2023-12-04 23:04:52 UTC
Oh, so it seems that the thing adding .jfif in my case to ~/.local/share/mime/globs2 is `~/local/share/mime/packages/x-wine-extension-jfif.xml`, deleting that and running `update-mime-database ~/.local/share/mime` fixes the problem for me.

So, I guess something ends up using only the user's database instead of overlaying it on top of the system's database?
Comment 7 Nicolas Fella 2023-12-04 23:09:57 UTC
I too have .local/share/mime/packages/x-wine-extension-jfif.xml on the affected system

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="image/jpeg">
    <glob pattern="*.jfif"/>
    <comment>JPEG Image</comment>
  </mime-type>
</mime-info>
Comment 8 Nicolas Fella 2023-12-04 23:16:37 UTC
And on the same system Qt5-based Spectacle works as expected but Qt6-based Spectacle gives .jfif

This looks like a change in QMimeDataBase between Qt5 and Qt6?
Comment 9 David Faure 2023-12-05 12:09:36 UTC
Interestingly, adding *.jfif in shared-mime-info was done by a WINE contributor so that Wine wouldn't have to generate such local overwrites anymore. But yeah the old files are still around...

I confirm that qt5 and qt6 work differently about this. I'll have to investigate further. Sounds like a bug since there no glob-deleteall in there.
Comment 10 Kai Uwe Broulik 2023-12-05 20:59:58 UTC
fwiw I can reproduce the bug Qt 6.6 but not git dev. Wonder if 4e9944e6c8a456353d243ab268cb0f01ff006faa "QMimeDatabase: collect glob patterns from all locations" fixed it?
Comment 11 David Faure 2023-12-07 10:54:09 UTC
Kai Uwe: good find, thanks! Indeed, I forgot to add Pick-To: 6.6 to these commits.
Backports submitted now in https://codereview.qt-project.org/c/qt/qtbase/+/523541/1
Comment 12 Nicolas Fella 2023-12-20 18:14:15 UTC
*** Bug 478779 has been marked as a duplicate of this bug. ***