flameshot-org / flameshot

Powerful yet simple to use screenshot software :desktop_computer: :camera_flash:
https://flameshot.org
GNU General Public License v3.0
24.02k stars 1.53k forks source link

Setting "Preferred save file Extension" does not set save extension (Debugged) #3398

Open LouisLambda opened 8 months ago

LouisLambda commented 8 months ago

Flameshot Version

v12.1.0

Installation Type

Linux, MacOS, or Windows Package manager (apt, pacman, eopkg, choco, brew, ...)

Operating System type and version

Fedora Linux 38

Description

Symptoms: Without fixed path, with "jpg" chosen as preferred extension, when choosing to save a dialog shows up with a filter type that is not jpg, and the default filename has no suffix, so by default it is saved in another format (on my computer, "avif", lexically the first entry in the file filter type.)

Analysis: Looking into the code, I think the issue is due to a logic error in ShowSaveFileDialog(). https://github.com/flameshot-org/flameshot/blob/fa29bcb4279b374ea7753fc4a514fd705499f7e7/src/utils/screenshotsaver.cpp#L77-L87

https://github.com/flameshot-org/flameshot/blob/fa29bcb4279b374ea7753fc4a514fd705499f7e7/src/utils/screenshotsaver.cpp#L93-L95

The code assumes that the system's default mime type for a suffix that .mimeTypeForFile() returns must exist in the master list returned by QImageWriter::supportedMimeTypes() - but this is not always true. And if the default mime type is not present in the list, the file type filter selected is just the first one on the list, which does not accept the suffix chosen with .setDefaultSuffix() and you get the behavior described above.

Additional Info/Workaround:

For me, the problem surfaced because I installed wine on my system, which shit-sprays the mime database with all kind of weird crap and breaks everything. As a result, the default mimetype for jpg is set to application/x-wine-extension-* for some reason.

Removing wine itself doesn't undo the damage. To clean up after wine, I followed this SO Answer which suggests:

#!/bin/bash
# Workaround for winemenubuilder bloating file associations by default

# Remove all existing associations
rm -f ~/.local/share/mime/packages/x-wine*
rm -f ~/.local/share/applications/wine-extension*
rm -f ~/.local/share/icons/hicolor/*/*/application-x-wine-extension*
rm -f ~/.local/share/mime/application/x-wine-extension*

# Disable winemenubuilder for all existing registries
# Replace ~/.bottles with your prefix/"bottle" directories
for sysreg in $(find ~/.wine ~/.bottles -name system.reg);
do
    sed -i 's/winemenubuilder.exe -a/winemenubuilder.exe/' $sysreg
done

and I followed up with the shell command update-mime-database ~/.local/share/mime/ to regenerate the mime database which expunges the now-deleted crap from it. After that, the problem cleared.

However, as shitty as this behavior by wine is, I think it's still fair to say that it's a bug in flameshot that causes the failure in this situation.

Thank you for flameshot, it is an excellent thing. And also thanks for this bug, which helped me figure out a solution to the wine-induced mime weirdness that's been annoying me for literally a decade, and which I never bothered to investigate prior.

Steps to reproduce

No response

Screenshots or screen recordings

No response

System Information

Wayland/Sway on Fedora 38