Correctly use the selected file type after asking save path #18645
+185
−49
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Should resolve: #18321
Should resolve: #11615
Should resolve: #17563
Closes: #14365
Before, we determined the selected file type by looking at the path that the user entered in the file dialog. This is unreliable, since some OSs don't check if the entered filename extension matches the selected file type; if it didn't match, MuseScore would create a file of a different type from what was selected in the file dialog (in the dropdown).
Now, we will use the available APIs to check the actually selected file type. Then, we adjust the name based on the selected file type. It may happen that we end up writing to a different path from what was selected in the file dialog, for example when an extension needs to be added or removed.
In that case, the file dialog will not have had the ability to ask the user whether to replace the file that will actually be replaced, because the file dialog only knows what the user has entered, not what we will make of it. In this situation, to be on the safe side, we ourselves check if the file exists and ask the user about replacing it.
In the worst case, the user will see two "replace file?" dialogs, namely the one from the file dialog about what the user has entered there, and the one from us about what we will make of that, but that's still better than overwriting a file when the user didn't want that.
(Telling the file dialog that it should not show its own dialog is no option, because that needs to be told before the dialog is opened, and we only know whether we will show our own dialog after the dialog is closed. And on macOS, disabling the native "replace file?" dialog is not supported anyway.)
Testing checklist:
On macOS, there is a preference in Finder that influences the behaviour: Finder > Preferences (or "Settings", nowadays) > Advanced > Show all file extensions (topmost option)
I've already tested it on macOS, but not very systematically.