/* * * (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. * */ #region Import using ASC.CRM.Resources; using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Reflection; #endregion namespace ASC.CRM.Classes { public static class EnumExtension { public static String ToLocalizedString(this Enum value) { return LocalizedEnumConverter.ConvertToString(value); } public static bool TryParse(string value, out T result) where T : struct // error CS0702: Constraint cannot be special class 'System.Enum' { return TryParse(value, false, out result); } public static bool TryParse(string value, bool ignoreCase, out T result) where T : struct // error CS0702: Constraint cannot be special class 'System.Enum' { if (value == null) value = String.Empty; result = default(T); try { result = (T)Enum.Parse(typeof(T), value, ignoreCase); return true; } catch { } return false; } } public class LocalizedEnumConverter : EnumConverter { private class LookupTable : Dictionary { } private Dictionary _lookupTables = new Dictionary(); private System.Resources.ResourceManager _resourceManager; private bool _isFlagEnum = false; private Array _flagValues; /// /// GetList the lookup table for the given culture (creating if necessary) /// /// /// private LookupTable GetLookupTable(CultureInfo culture) { LookupTable result = null; if (culture == null) culture = CultureInfo.CurrentCulture; if (!_lookupTables.TryGetValue(culture, out result)) { result = new LookupTable(); foreach (object value in GetStandardValues()) { string 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) { Type type = value.GetType(); string resourceName = string.Format("{0}_{1}", type.Name, value.ToString()); string 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) { switch (value) { case 0: return false; case 1: return true; } return ((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 (object flagValue in _flagValues) { ulong lFlagValue = Convert.ToUInt32(flagValue); if (IsSingleBitValue(lFlagValue)) { if ((lFlagValue & lValue) == lFlagValue) { string valueText = GetValueText(culture, flagValue); if (result == null) { result = valueText; } else { result = string.Format("{0}, {1}", 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) { LookupTable lookupTable = GetLookupTable(culture); object result = null; lookupTable.TryGetValue(text, out result); return result; } private object GetFlagValue(CultureInfo culture, string text) { LookupTable lookupTable = GetLookupTable(culture); string[] textValues = text.Split(','); ulong result = 0; foreach (string textValue in textValues) { object value = null; string trimmedTextValue = textValue.Trim(); if (!lookupTable.TryGetValue(trimmedTextValue, out value)) { return null; } result |= Convert.ToUInt32(value); } return Enum.ToObject(EnumType, result); } public LocalizedEnumConverter(Type type) : base(type) { _resourceManager = CRMEnumResource.ResourceManager; object[] flagAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), true); _isFlagEnum = flagAttributes.Length > 0; if (_isFlagEnum) { _flagValues = Enum.GetValues(type); } } public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { object result = (_isFlagEnum) ? GetFlagValue(culture, (string)value) : GetValue(culture, (string)value); if (result == null) { result = base.ConvertFrom(context, culture, value); } return result; } return base.ConvertFrom(context, culture, value); } 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); } } public static string ConvertToString(Enum value) { TypeConverter converter = TypeDescriptor.GetConverter(value.GetType()); return converter.ConvertToString(value); } public static List> GetValues(Type enumType, CultureInfo culture) { List> result = new List>(); TypeConverter converter = TypeDescriptor.GetConverter(enumType); foreach (Enum value in Enum.GetValues(enumType)) { KeyValuePair pair = new KeyValuePair(value, converter.ConvertToString(null, culture, value)); result.Add(pair); } return result; } public static List> GetValues(Type enumType) { return GetValues(enumType, CultureInfo.CurrentUICulture); } public static List GetLocalizedValues(Type enumType) { var converter = TypeDescriptor.GetConverter(enumType); return (from Enum value in Enum.GetValues(enumType) select converter.ConvertToString(null, CultureInfo.CurrentUICulture, value)).ToList(); } } }