// // Association.cs - explicitly associates accessor with data format and/or object model // // Author: // Michael Becker // // Copyright (c) 2011-2020 Mike Becker's Software // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using System.Collections.Generic; using System.Text; namespace UniversalEditor { /// /// Associates s, s, and other related Universal Editor objects. /// public class Association : IComparable { /// /// Defines a collection of s. /// public class AssociationCollection : System.Collections.ObjectModel.Collection { } private static List _associations = new List(); /// /// Registers the specified if it is not already registered. /// /// true if the is not already registered and has been added; false otherwise. /// The to register. public static bool Register(Association assoc) { if (_associations.Contains(assoc)) return false; _associations.Add(assoc); return true; } /// /// Unregisters the specified if it has been registered. /// /// true if the given has been unregistered; false if the given has not been registered. /// The to unregister. public static bool Unregister(Association assoc) { if (!_associations.Contains(assoc)) return false; _associations.Remove(assoc); return true; } /// /// Returns an array of all known s. /// /// The all associations. public static Association[] GetAllAssociations() { return _associations.ToArray(); } /// /// Gets an array of s that match the given or . /// /// An array of s that match the given or . /// The to compare. /// The to compare. public static Association[] FromObjectModelOrDataFormat(ObjectModelReference objectModel = null, DataFormatReference dataFormat = null) { Association[] _associations = Association.GetAllAssociations(); List associations = new List(); for (int i = 0; i < _associations.Length; i++) { if ((objectModel != null && _associations[i].ObjectModels.Contains(objectModel)) || (dataFormat != null && _associations[i].DataFormats.Contains(dataFormat))) { associations.Add(_associations[i]); } } return associations.ToArray(); } /// /// Gets an array of s that match the given or file name filters. /// /// An array of s that match the given or file name filters. /// The to compare. /// Not implemented. public static Association[] FromAccessor(Accessor accessor = null, string fileNameFilter = null) { Association[] assocs = Association.GetAllAssociations(); List associations = new List(); // FIXME: the fileNameFilter parameter is not referenced in this method body // stopwatch diagnostics determined a nested for loop is 0.0547024 ms faster than foreach for (int i = 0; i < assocs.Length; i++) { for (int j = 0; j < assocs[i].Filters.Count; j++) { if (assocs[i].Filters[j].Matches(accessor)) { associations.Add(assocs[i]); } } } return associations.ToArray(); } private string mvarTitle = String.Empty; /// /// The title of this ; for example, "JPEG images". /// public string Title { get { return mvarTitle; } set { mvarTitle = value; } } private DataFormatFilter.DataFormatFilterCollection mvarFilters = new DataFormatFilter.DataFormatFilterCollection(); /// /// The filters that are used to determine which documents can be handled by this . /// public DataFormatFilter.DataFormatFilterCollection Filters { get { return mvarFilters; } } private string mvarExternalCommandLine = String.Empty; /// /// The command line of an external application to launch when a file handled by this association is opened. /// public string ExternalCommandLine { get { return mvarExternalCommandLine; } set { mvarExternalCommandLine = value; } } private ObjectModelReference.ObjectModelReferenceCollection mvarObjectModels = new ObjectModelReference.ObjectModelReferenceCollection(); /// /// The s which refer to s that are included in this . /// public ObjectModelReference.ObjectModelReferenceCollection ObjectModels { get { return mvarObjectModels; } } private DataFormatReference.DataFormatReferenceCollection mvarDataFormats = new DataFormatReference.DataFormatReferenceCollection(); /// /// The s which refer to s that are included in this . /// public DataFormatReference.DataFormatReferenceCollection DataFormats { get { return mvarDataFormats; } } /// /// Returns a that represents the current . /// /// A that represents the current . public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append('['); for (int i = 0; i < mvarFilters.Count; i++) { sb.Append(mvarFilters[i].Title); if (i < mvarFilters.Count - 1) sb.Append(", "); } sb.Append(']'); sb.Append(' '); sb.Append('{'); for (int i = 0; i < mvarObjectModels.Count; i++) { sb.Append(mvarObjectModels[i].TypeName); if (i < mvarObjectModels.Count - 1) sb.Append(", "); } sb.Append('}'); sb.Append(' '); sb.Append('('); for (int i = 0; i < mvarDataFormats.Count; i++) { sb.Append(mvarDataFormats[i].TypeName); if (i < mvarDataFormats.Count - 1) sb.Append(", "); } sb.Append(')'); return sb.ToString(); } /// /// Returns an array of s that satisfy the given . /// /// An array of s that match the specified criteria. /// The that define the criteria to match when searching for s. public static Association[] FromCriteria(AssociationCriteria ac) { List associations = new List(); Association[] _associations = GetAllAssociations(); foreach (Association assoc in _associations) { if (ac.ObjectModel != null) { if (assoc.ObjectModels.Contains(ac.ObjectModel)) { if (ac.DataFormat != null) { if (assoc.DataFormats.Contains(ac.DataFormat)) { associations.Add(assoc); continue; } } else { associations.Add(assoc); } } } if (ac.DataFormat != null) { if (assoc.DataFormats.Contains(ac.DataFormat)) { associations.Add(assoc); continue; } } if (ac.Accessor != null) { bool found = false; foreach (DataFormatFilter filter in assoc.Filters) { if (filter.Matches(ac.Accessor)) { associations.Add(assoc); found = true; break; } } if (found) continue; } } return associations.ToArray(); } public int CompareTo(Association other) { int nFileFormatFilters = 0, nMagicBytes = 0; foreach (DataFormatFilter filter in Filters) { nFileFormatFilters += filter.FileNameFilters.Count; nMagicBytes += filter.MagicBytes.Count; } int nFileFormatFiltersOther = 0, nMagicBytesOther = 0; foreach (DataFormatFilter filter in other.Filters) { nFileFormatFiltersOther += filter.FileNameFilters.Count; nMagicBytesOther += filter.MagicBytes.Count; } return (nFileFormatFilters + nMagicBytes).CompareTo(nFileFormatFiltersOther + nMagicBytesOther); } } }