Skip to content

Commit

Permalink
Merge pull request PintaProject#216 from MrCarroll/MimeTypeFilter
Browse files Browse the repository at this point in the history
Use MIME filters for file dialogs on non-Windows systems
  • Loading branch information
cameronwhite authored Feb 9, 2022
2 parents ac3ff08 + 115a9ad commit d5c215c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Pinta.Core/Actions/LayerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ private void HandlePintaCoreActionsLayersImportFromFileActivated (object sender,
}
}

// On Unix-like systems, file extensions are often considered optional.
// Files can often also be identified by their MIME types.
// Windows does not understand MIME types natively.
// Adding a MIME filter on Windows would break the native file picker and force a GTK file picker instead.
if (SystemManager.GetOperatingSystem() != OS.Windows) {
foreach (var format in PintaCore.System.ImageFormats.Formats) {
foreach (var mime in format.Mimes) {
ff.AddMimeType (mime);
}
}
}

ff.Name = Translations.GetString ("Image files");
fcd.AddFilter (ff);

Expand Down
19 changes: 18 additions & 1 deletion Pinta.Core/ImageFormats/FormatDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public sealed class FormatDescriptor
/// </summary>
public string[] Extensions { get; private set; }

/// <summary>
/// A list of supported MIME types (for example, "image/jpg" and "image/png").
/// </summary>
public string[] Mimes { get; private set; }

/// <summary>
/// The importer for this file format. This may be null if only exporting
/// is supported for this format.
Expand All @@ -65,16 +70,18 @@ public sealed class FormatDescriptor
/// in the file dialog's filter.
/// </param>
/// <param name="extensions">A list of supported file extensions (for example, "jpeg" and "JPEG").</param>
/// <param name="mimes">A list of supported file MIME types (for example, "image/jpeg" and "image/png").</param>
/// <param name="importer">The importer for this file format, or null if importing is not supported.</param>
/// <param name="exporter">The exporter for this file format, or null if exporting is not supported.</param>
public FormatDescriptor (string displayPrefix, string[] extensions,
public FormatDescriptor (string displayPrefix, string[] extensions, string[] mimes,
IImageImporter? importer, IImageExporter? exporter)
{
if (extensions == null || (importer == null && exporter == null)) {
throw new ArgumentNullException ("Format descriptor is initialized incorrectly");
}

this.Extensions = extensions;
this.Mimes = mimes;
this.Importer = importer;
this.Exporter = exporter;

Expand All @@ -90,6 +97,16 @@ public FormatDescriptor (string displayPrefix, string[] extensions,
formatNames.Append (wildcard);
}

// On Unix-like systems, file extensions are often considered optional.
// Files can often also be identified by their MIME types.
// Windows does not understand MIME types natively.
// Adding a MIME filter on Windows would break the native file picker and force a GTK file picker instead.
if (SystemManager.GetOperatingSystem() != OS.Windows) {
foreach (string mime in mimes) {
ff.AddMimeType (mime);
}
}

ff.Name = string.Format (Translations.GetString ("{0} image ({1})"), displayPrefix, formatNames);
this.Filter = ff;
}
Expand Down
4 changes: 2 additions & 2 deletions Pinta.Core/Managers/ImageConverterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public ImageConverterManager ()
else
exporter = null;

RegisterFormat (new FormatDescriptor (formatNameUpperCase, extensions, importer, exporter));
RegisterFormat (new FormatDescriptor (formatNameUpperCase, extensions, format.MimeTypes, importer, exporter));
}

// Create all the formats we have our own importers/exporters for
OraFormat oraHandler = new OraFormat ();
RegisterFormat (new FormatDescriptor ("OpenRaster", new string[] { "ora", "ORA" }, oraHandler, oraHandler));
RegisterFormat (new FormatDescriptor ("OpenRaster", new string[] { "ora", "ORA" }, new string[] { "image/openraster" }, oraHandler, oraHandler));
}

public IEnumerable<FormatDescriptor> Formats { get { return formats; } }
Expand Down
12 changes: 12 additions & 0 deletions Pinta/Actions/File/OpenDocumentAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ private void Activated (object sender, EventArgs e)
}
}

// On Unix-like systems, file extensions are often considered optional.
// Files can often also be identified by their MIME types.
// Windows does not understand MIME types natively.
// Adding a MIME filter on Windows would break the native file picker and force a GTK file picker instead.
if (SystemManager.GetOperatingSystem() != OS.Windows) {
foreach (var format in PintaCore.System.ImageFormats.Formats) {
foreach (var mime in format.Mimes) {
ff.AddMimeType (mime);
}
}
}

fcd.AddFilter (ff);

var ff2 = new FileFilter {
Expand Down

0 comments on commit d5c215c

Please sign in to comment.