Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on wrong file using Palette Tool #146

Merged
merged 4 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions Pinta.Core/Classes/Palette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;

using System.Linq;
using Cairo;
using Gtk;
using Mono.Unix;

namespace Pinta.Core
{
Expand Down Expand Up @@ -159,15 +161,46 @@ public void LoadDefault ()

public void Load (string fileName)
{
var loader = PintaCore.System.PaletteFormats.GetLoaderByFilename (fileName);
colors = loader.Load (fileName);
colors.TrimExcess ();
OnPaletteChanged ();
try
{
var loader = PintaCore.System.PaletteFormats.GetLoaderByFilename (fileName);

if (loader == null)
throw new FormatException();

colors = loader.Load (fileName);
colors.TrimExcess ();
OnPaletteChanged ();
}
catch (FormatException e)
{
var parent = PintaCore.Chrome.MainWindow;
ShowUnsupportedFormatDialog(parent, fileName, "Unsupported palette format", e.ToString());
}
}

public void Save (string fileName, IPaletteSaver saver)
{
saver.Save (colors, fileName);
}

private void ShowUnsupportedFormatDialog(Window parent, string filename, string primaryText, string details)
{
string markup = "<span weight=\"bold\" size=\"larger\">{0}</span>\n\n{1}";

string secondaryText = string.Format(Catalog.GetString("Could not open file: {0}"), filename);
secondaryText += string.Format(Catalog.GetString($"{Environment.NewLine}{Environment.NewLine}Pinta supports the following palette formats:{Environment.NewLine}"));
var extensions = from format in PintaCore.System.PaletteFormats.Formats
where format.Loader != null
from extension in format.Extensions
where char.IsLower(extension.FirstOrDefault())
orderby extension
select extension;

secondaryText += String.Join(", ", extensions);

string message = string.Format(markup, primaryText, secondaryText);
PintaCore.Chrome.ShowUnsupportedFormatDialog(parent, message, details);
}
}
}
4 changes: 1 addition & 3 deletions Pinta.Core/Managers/WorkspaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,7 @@ where char.IsLower(extension.FirstOrDefault())
orderby extension
select extension;

foreach (var extension in extensions)
secondaryText += extension + ", ";
secondaryText = secondaryText.Substring(0, secondaryText.Length - 2);
secondaryText += String.Join(", ", extensions);

string message = string.Format (markup, primaryText, secondaryText);
PintaCore.Chrome.ShowUnsupportedFormatDialog(parent, message, details);
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/PaletteFormats/PaletteFormats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public IPaletteLoader GetLoaderByFilename (string fileName)
{
string extension = System.IO.Path.GetExtension (fileName);
extension = NormalizeExtension (extension);
return formats.Where (p => p.Extensions.Contains (extension)).FirstOrDefault ().Loader;
return formats.Where (p => p.Extensions.Contains (extension)).FirstOrDefault ()?.Loader;
}

private static string NormalizeExtension (string extension)
Expand Down