/* * * (c) Copyright Ascensio System Limited 2010-2018 * * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights. * * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html * * You can contact Ascensio System SIA by email at sales@onlyoffice.com * * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3. * * Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains * relevant author attributions when distributing the software. If the display of the logo in its graphic * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" * in every copy of the program you distribute. * Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks. * */ namespace ASC.Web.Core.Helpers { /// /// Defines a type converter for enum values that converts enum values to /// and from string representations using resources /// /// /// This class makes localization of display values for enums in a project easy. Simply /// derive a class from this class and pass the ResourceManagerin the constructor. /// /// Then define the enum values in the resource editor. The names of /// the resources are simply the enum value prefixed by the enum type name with an /// underscore separator eg MyEnum_MyValue. You can then use the TypeConverter attribute /// to make the LocalizedEnumConverter the default TypeConverter for the enums in your /// project. /// public class ResourceEnumConverter : System.ComponentModel.EnumConverter { private class LookupTable : Dictionary { } private readonly Dictionary _lookupTables = new Dictionary(); private readonly System.Resources.ResourceManager _resourceManager; private readonly bool _isFlagEnum = false; private readonly Array _flagValues; /// /// Get the lookup table for the given culture (creating if necessary) /// /// /// private LookupTable GetLookupTable(CultureInfo culture) { if (culture == null) culture = CultureInfo.CurrentCulture; if (!_lookupTables.TryGetValue(culture, out var result)) { result = new LookupTable(); foreach (var value in GetStandardValues()) { var text = GetValueText(culture, value); if (text != null) { result.Add(text, value); } } _lookupTables.Add(culture, result); } return result; } /// /// Return the text to display for a simple value in the given culture /// /// The culture to get the text for /// The enum value to get the text for /// The localized text private string GetValueText(CultureInfo culture, object value) { var type = value.GetType(); var resourceName = $"{type.Name}_{value}"; var result = _resourceManager.GetString(resourceName, culture); if (result == null) result = resourceName; return result; } /// /// Return true if the given value is can be represented using a single bit /// /// /// private bool IsSingleBitValue(ulong value) { return value switch { 0 => false, 1 => true, _ => (value & (value - 1)) == 0, }; } /// /// Return the text to display for a flag value in the given culture /// /// The culture to get the text for /// The flag enum value to get the text for /// The localized text private string GetFlagValueText(CultureInfo culture, object value) { // if there is a standard value then use it // if (Enum.IsDefined(value.GetType(), value)) { return GetValueText(culture, value); } // otherwise find the combination of flag bit values // that makes up the value // ulong lValue = Convert.ToUInt32(value); string result = null; foreach (var flagValue in _flagValues) { ulong lFlagValue = Convert.ToUInt32(flagValue); if (IsSingleBitValue(lFlagValue)) { if ((lFlagValue & lValue) == lFlagValue) { var valueText = GetValueText(culture, flagValue); if (result == null) { result = valueText; } else { result = $"{result}, {valueText}"; } } } } return result; } /// /// Return the Enum value for a simple (non-flagged enum) /// /// The culture to convert using /// The text to convert /// The enum value private object GetValue(CultureInfo culture, string text) { var lookupTable = GetLookupTable(culture); lookupTable.TryGetValue(text, out var result); return result; } /// /// Return the Enum value for a flagged enum /// /// The culture to convert using /// The text to convert /// The enum value private object GetFlagValue(CultureInfo culture, string text) { var lookupTable = GetLookupTable(culture); var textValues = text.Split(','); ulong result = 0; foreach (var textValue in textValues) { var trimmedTextValue = textValue.Trim(); if (!lookupTable.TryGetValue(trimmedTextValue, out var value)) { return null; } result |= Convert.ToUInt32(value); } return Enum.ToObject(EnumType, result); } /// /// Create a new instance of the converter using translations from the given resource manager /// /// /// public ResourceEnumConverter(Type type, System.Resources.ResourceManager resourceManager) : base(type) { _resourceManager = resourceManager; var flagAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), true); _isFlagEnum = flagAttributes.Length > 0; if (_isFlagEnum) { _flagValues = Enum.GetValues(type); } } /// /// Convert string values to enum values /// /// /// /// /// public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string @string) { var result = _isFlagEnum ? GetFlagValue(culture, @string) : GetValue(culture, @string); if (result == null) { result = base.ConvertFrom(context, culture, value); } return result; } else { return base.ConvertFrom(context, culture, value); } } /// /// Convert the enum value to a string /// /// /// /// /// /// public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (value != null && destinationType == typeof(string)) { object result = _isFlagEnum ? GetFlagValueText(culture, value) : GetValueText(culture, value); return result; } else { return base.ConvertTo(context, culture, value, destinationType); } } /// /// Convert the given enum value to string using the registered type converter /// /// The enum value to convert to string /// The localized string value for the enum static public string ConvertToString(Enum value) { var converter = TypeDescriptor.GetConverter(value.GetType()); return converter.ConvertToString(value); } /// /// Return a list of the enum values and their associated display text for the given enum type /// /// The enum type to get the values for /// The culture to get the text for /// /// A list of KeyValuePairs where the key is the enum value and the value is the text to display /// /// /// This method can be used to provide localized binding to enums in ASP.NET applications. Unlike /// windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values /// to the displayed text. You can bind an ASP.NET control to the list returned by this method by setting /// the DataValueField to "Key" and theDataTextField to "Value". /// static public List> GetValues(Type enumType, CultureInfo culture) { var result = new List>(); var converter = TypeDescriptor.GetConverter(enumType); foreach (Enum value in Enum.GetValues(enumType)) { var pair = new KeyValuePair(value, converter.ConvertToString(null, culture, value)); result.Add(pair); } return result; } /// /// Return a list of the enum values and their associated display text for the given enum type in the current UI Culture /// /// The enum type to get the values for /// /// A list of KeyValuePairs where the key is the enum value and the value is the text to display /// /// /// This method can be used to provide localized binding to enums in ASP.NET applications. Unlike /// windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values /// to the displayed text. You can bind an ASP.NET control to the list returned by this method by setting /// the DataValueField to "Key" and theDataTextField to "Value". /// static public List> GetValues(Type enumType) { return GetValues(enumType, CultureInfo.CurrentUICulture); } } }