/* * GNU AFFERO GENERAL PUBLIC LICENSE * Version 3, 19 November 2007 * Copyright (C) 2007 Free Software Foundation, Inc. * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. */ using System; using System.Collections.Generic; namespace UVtools.Core.FileFormats { /// /// Represents a file extension for slicer file formats /// public sealed class FileExtension : IEquatable, IEquatable { #region Properties /// /// Stores a specific type that should be used to create with this FileExtension instance /// public Type FileFormatType { get; } /// /// Gets the extension name without the dot (.) /// public string Extension { get; } /// /// Gets the extension description /// public string Description { get; } /// /// Gets if the extension shows up on open file dialog filters /// public bool IsVisibleOnFileFilters { get; } /// /// Gets if the extension shows up on convert to menu /// public bool IsVisibleOnConvertMenu { get; } /// /// Gets a tag object /// public object Tag { get; } /// /// Gets the file filter for open and save dialogs /// public string Filter => $@"{Description} (*.{Extension})|*.{Extension}"; #endregion #region Constructor /// /// Constructor /// /// The exact type /// The extension name without the dot (.) /// The extension description /// True if this extension is visible on open file dialog filters /// True if this extension is visible on convert to menu /// Tag object public FileExtension(Type fileFormatType, string extension, string description, bool isVisibleOnFileFilters = true, bool isVisibleOnConvertMenu = true, object tag = null) { FileFormatType = fileFormatType; Extension = extension; Description = description; IsVisibleOnFileFilters = isVisibleOnFileFilters; IsVisibleOnConvertMenu = isVisibleOnConvertMenu; Tag = tag; } #endregion #region Overrides public override string ToString() { return $"{Description} ({Extension})"; } public bool Equals(FileExtension other) { return Extension.Equals(other.Extension, StringComparison.OrdinalIgnoreCase); } public bool Equals(string other) { return Extension.Equals(other, StringComparison.OrdinalIgnoreCase); } public override bool Equals(object obj) { return ReferenceEquals(this, obj) || obj is FileExtension other && Equals(other); } public override int GetHashCode() { return (Extension != null ? Extension.GetHashCode() : 0); } private sealed class ExtensionEqualityComparer : IEqualityComparer { public bool Equals(FileExtension x, FileExtension y) { if (ReferenceEquals(x, y)) return true; if (ReferenceEquals(x, null)) return false; if (ReferenceEquals(y, null)) return false; if (x.GetType() != y.GetType()) return false; return x.Extension == y.Extension; } public int GetHashCode(FileExtension obj) { return (obj.Extension != null ? obj.Extension.GetHashCode() : 0); } } public static IEqualityComparer ExtensionComparer { get; } = new ExtensionEqualityComparer(); #endregion #region Methods public FileFormat GetFileFormat(bool createNewInstance = false) => FileFormatType is null ? FileFormat.FindByExtensionOrFilePath(Extension, createNewInstance) : FileFormat.FindByType(FileFormatType, createNewInstance); public static FileExtension Find(string extension) => FileFormat.FindExtension(extension); #endregion } }