Skip to content

Commit

Permalink
Support loading images that don't have the right extension
Browse files Browse the repository at this point in the history
This is similar to the changes for palettes in aa85ced

Fixes: #1679570
  • Loading branch information
cameronwhite committed Feb 3, 2022
1 parent aa85ced commit 734f800
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
36 changes: 30 additions & 6 deletions Pinta.Core/Managers/WorkspaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cairo;
using Gtk;

Expand Down Expand Up @@ -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 ();
Expand All @@ -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 ());
}
Expand Down

0 comments on commit 734f800

Please sign in to comment.