diff --git a/CHANGELOG.md b/CHANGELOG.md index e144a0924b..3ec4ff9f17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Thanks to the following contributors who worked on this release: - Improved support for `.ora` files - Hidden layers are now round-tripped correctly for `.ora` files ([#1377566](https://bugs.launchpad.net/pinta/+bug/1377566)) - When saving a `.ora` file, a flattened image (`mergedimage.png`) is now included in the archive. This is required by the spec to accomodate viewer software ([#1377566](https://bugs.launchpad.net/pinta/+bug/1377566)) +- Image or palette files that have an unknown extension but have valid contents can now be loaded ([#1679570](https://bugs.launchpad.net/pinta/+bug/1679570)) ### Changed - Pinta now uses the standard GTK about dialog @@ -31,7 +32,6 @@ Thanks to the following contributors who worked on this release: - Improved the zoom tool's rectangle zoom when working with smaller images - Fixed a potential crash when opening / adding an image after actions in the layer list panel ([#1959598](https://bugs.launchpad.net/pinta/+bug/1959598)) - Fixed an issue where the "All Files" filter in the Open File dialog ignored files with no extension, and did not work in the macOS native file chooser ([#1958670](https://bugs.launchpad.net/pinta/+bug/1958670), [#1679570](https://bugs.launchpad.net/pinta/+bug/1679570)) -- Palette files that have an unknown extension but have valid contents can now be loaded ([#1679570](https://bugs.launchpad.net/pinta/+bug/1679570)) ## [2.0.2](https://github.com/PintaProject/Pinta/compare/2.0.2...HEAD) - 2022/01/13 diff --git a/Pinta.Core/Managers/WorkspaceManager.cs b/Pinta.Core/Managers/WorkspaceManager.cs index 3dbce79378..554614990a 100644 --- a/Pinta.Core/Managers/WorkspaceManager.cs +++ b/Pinta.Core/Managers/WorkspaceManager.cs @@ -27,6 +27,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using Cairo; using Gtk; @@ -221,10 +222,35 @@ public bool OpenFile (GLib.IFile file, Window? parent = null) try { // Open the image and add it to the layers IImageImporter? importer = PintaCore.System.ImageFormats.GetImporterByFile (file.GetDisplayName ()); - if (importer == null) - throw new FormatException (Translations.GetString ("Unsupported file format")); - - importer.Import (file, parent); + if (importer is not null) { + importer.Import (file, parent); + } else { + // Unknown extension, so try every loader. + var errors = new StringBuilder (); + bool loaded = false; + foreach (var format in PintaCore.System.ImageFormats.Formats.Where (f => !f.IsWriteOnly ())) { + try { + format.Importer!.Import (file, parent); + loaded = true; + break; + } catch (UnauthorizedAccessException) { + // If the file can't be accessed, don't retry for every format. + ShowFilePermissionErrorDialog (parent, file.ParsedName); + return false; + } catch (Exception e) { + // Record errors in case none of the formats work. + errors.AppendLine ($"Failed to load image as {format.Filter.Name}:"); + errors.Append (e.ToString ()); + errors.AppendLine (); + } + } + + if (!loaded) { + ShowUnsupportedFormatDialog (parent, file.ParsedName, + Translations.GetString ("Unsupported file format"), errors.ToString ()); + return false; + } + } PintaCore.Workspace.ActiveWorkspace.History.PushNewItem (new BaseHistoryItem (Resources.StandardIcons.DocumentOpen, Translations.GetString ("Open Image"))); PintaCore.Workspace.ActiveDocument.History.SetClean (); @@ -239,8 +265,6 @@ public bool OpenFile (GLib.IFile file, Window? parent = null) fileOpened = true; } catch (UnauthorizedAccessException) { ShowFilePermissionErrorDialog (parent, file.ParsedName); - } catch (FormatException e) { - ShowUnsupportedFormatDialog (parent, file.ParsedName, e.Message, e.ToString ()); } catch (Exception e) { ShowOpenFileErrorDialog (parent, file.ParsedName, e.Message, e.ToString ()); }