using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using Vanara.Extensions; using static Vanara.PInvoke.Ole32; using static Vanara.PInvoke.PropSys; namespace Vanara.Windows.Shell { /// Enumerate and retrieve individual property description details. Wraps the shell interface /// public class PropertyDescription : IDisposable { /// The IPropertyDescription object. protected IPropertyDescription iDesc; /// The IPropertyDescription2 object. protected IPropertyDescription2 iDesc2; /// The property key for this property. protected PROPERTYKEY key; /// Gets the type list. protected PropertyTypeList typeList; /// Initializes a new instance of the class. /// The property description. protected internal PropertyDescription(IPropertyDescription propertyDescription) { iDesc = propertyDescription; key = iDesc.GetPropertyKey(); } /// Creates a instance from a specified property key. /// The property key. /// An associated instance of or if the PROPERTYKEY does not exist in the schema subsystem cache. public static PropertyDescription Create(PROPERTYKEY propkey) => PSGetPropertyDescription(propkey, typeof(IPropertyDescription).GUID, out var ppv).Succeeded ? new PropertyDescription((IPropertyDescription)ppv) : null; /// Gets a value that describes how the property values are displayed when multiple items are selected in the UI. public PROPDESC_AGGREGATION_TYPE AggregationType => iDesc?.GetAggregationType() ?? 0; /// Gets the case-sensitive name by which a property is known to the system, regardless of its localized name. public string CanonicalName => iDesc?.GetCanonicalName(); /// Gets the column state flag, which describes how the property should be treated by interfaces or APIs that use this flag. public SHCOLSTATE ColumnState => iDesc?.GetColumnState() ?? 0; /// Gets the condition type and default condition operation to use when displaying the property in the query builder UI. This influences the list of predicate conditions (for example, equals, less than, and contains) that are shown for this property. public Tuple ConditionType { get { PROPDESC_CONDITION_TYPE ct = 0; CONDITION_OPERATION co = 0; iDesc?.GetConditionType(out ct, out co); return new Tuple(ct, co); } } /// Gets the default column width of the property in a list view. /// A pointer to the column width value, in characters. public uint DefaultColumnWidth => iDesc?.GetDefaultColumnWidth() ?? 0; /// Gets the display name of the property as it is shown in any UI. public string DisplayName => iDesc != null && iDesc.GetDisplayName(out var s).Succeeded ? s : null; /// Gets the current data type used to display the property. public PROPDESC_DISPLAYTYPE DisplayType { get { try { return iDesc?.GetDisplayType() ?? 0; } catch { return 0; } } } /// Gets the text used in edit controls hosted in various dialog boxes. public string EditInvitation => iDesc?.GetEditInvitation(); /// Gets the grouping method to be used when a view is grouped by a property, and retrieves the grouping type. public PROPDESC_GROUPING_RANGE GroupingRange => iDesc?.GetGroupingRange() ?? 0; /// Gets a structure that acts as a property's unique identifier. public PROPERTYKEY PropertyKey => key; /// Gets the variant type of the property. If the type cannot be determined, this property returns null. public Type PropertyType => PROPVARIANT.GetType(iDesc?.GetPropertyType() ?? VARTYPE.VT_EMPTY); /// Gets the relative description type for a property description. public PROPDESC_RELATIVEDESCRIPTION_TYPE RelativeDescriptionType => iDesc?.GetRelativeDescriptionType() ?? 0; /// Gets the current sort description flags for the property, which indicate the particular wordings of sort offerings. public PROPDESC_SORTDESCRIPTION SortDescription => iDesc?.GetSortDescription() ?? 0; /// Gets a set of flags that describe the uses and capabilities of the property. public PROPDESC_TYPE_FLAGS TypeFlags => iDesc?.GetTypeFlags(PROPDESC_TYPE_FLAGS.PDTF_MASK_ALL) ?? 0; // /// Gets an instance of an PropertyTypeList, which can be used to enumerate the possible values for a property. public PropertyTypeList TypeList => typeList ?? (typeList = new PropertyTypeList(iDesc?.GetEnumTypeList(typeof(IPropertyEnumTypeList).GUID))); /// Gets the current set of flags governing the property's view. /// When this method returns, contains a pointer to a value that includes one or more of the following flags. See PROPDESC_VIEW_FLAGS for valid values. public PROPDESC_VIEW_FLAGS ViewFlags => iDesc?.GetViewFlags() ?? 0; /// Coerces the value to the canonical value, according to the property description. /// On entry, contains a PROPVARIANT that contains the original value. When this method returns, contains the canonical value. public bool CoerceToCanonicalValue(PROPVARIANT propvar) => iDesc?.CoerceToCanonicalValue(propvar).Succeeded ?? false; /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public virtual void Dispose() { // typeList?.Dispose(); if (iDesc2 != null) { Marshal.ReleaseComObject(iDesc2); iDesc2 = null; } if (iDesc != null) { Marshal.ReleaseComObject(iDesc); iDesc = null; } } /// Gets a formatted string representation of a property value. /// A object that contains the type and value of the property. /// One or more of the PROPDESC_FORMAT_FLAGS flags, which are either bitwise or multiple values, that indicate the property string format. /// The formatted value. public string FormatForDisplay(object obj, PROPDESC_FORMAT_FLAGS pdfFlags = PROPDESC_FORMAT_FLAGS.PDFF_DEFAULT) => iDesc?.FormatForDisplay(new PROPVARIANT(obj), pdfFlags) ?? obj?.ToString(); /// Gets a formatted string representation of a property value. /// A object that contains the type and value of the property. /// One or more of the PROPDESC_FORMAT_FLAGS flags, which are either bitwise or multiple values, that indicate the property string format. /// The formatted value. internal string FormatForDisplay(PROPVARIANT pv, PROPDESC_FORMAT_FLAGS pdfFlags = PROPDESC_FORMAT_FLAGS.PDFF_DEFAULT) => iDesc?.FormatForDisplay(pv, pdfFlags) ?? pv?.ToString(); /// Gets the image location for a value. /// The value. /// An IconLocation for the image associated with the property value. public IconLocation GetImageLocationForValue(object obj) { if (iDesc2 == null) iDesc2 = iDesc as IPropertyDescription2; return iDesc2 != null && IconLocation.TryParse(iDesc2.GetImageReferenceForValue(new PROPVARIANT(obj), out var img).Succeeded ? img : null, out var loc) ? loc : new IconLocation(); } /// Compares two property values in the manner specified by the property description. Returns two display strings that describe how the two properties compare. /// An object for the first property. /// An object for the second property. public Tuple GetRelativeDescription(object obj1, object obj2) { string d1 = null, d2 = null; iDesc?.GetRelativeDescription(new PROPVARIANT(obj1), new PROPVARIANT(obj2), out d1, out d2); return new Tuple(d1, d2); } /// Gets the localized display string that describes the current sort order. /// TRUE if ppszDescription should reference the string "Z on top"; FALSE to reference the string "A on top". /// When this method returns, contains the address of a pointer to the sort description as a null-terminated Unicode string. public string GetSortDescriptionLabel(bool descending = false) => iDesc?.GetSortDescriptionLabel(descending); /// Gets a value that indicates whether a property is canonical according to the definition of the property description. /// A PROPVARIANT that contains the type and value of the property. public bool IsValueCanonical(PROPVARIANT propvar) => iDesc?.IsValueCanonical(propvar).Succeeded ?? false; /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() => CanonicalName; public IPropertyDescription Raw => iDesc; } /// Exposes methods that extract information from a collection of property descriptions presented as a list. /// /// public class PropertyDescriptionList : IReadOnlyList, IDisposable { /// The IPropertyDescriptionList instance. protected IPropertyDescriptionList iList; /// /// Initializes a new instance of the class from a string. /// /// The property list. See for the required format. public PropertyDescriptionList(string propList) { PSGetPropertyDescriptionListFromString(propList, typeof(IPropertyDescriptionList).GUID, out iList).ThrowIfFailed(); } /// Initializes a new instance of the class. /// The COM interface pointer. protected internal PropertyDescriptionList(IPropertyDescriptionList list) { iList = list; } /// public virtual int Count => (int)(iList?.GetCount() ?? 0); /// public virtual PropertyDescription this[int index] => new PropertyDescription(iList?.GetAt((uint)index, typeof(IPropertyDescription).GUID)); /// Gets the for the specified key. /// The . /// The PROPERTYKEY. /// The for the specified key. public virtual PropertyDescription this[PROPERTYKEY propkey] => PropertyDescription.Create(propkey); /// public virtual void Dispose() { if (iList != null) { Marshal.ReleaseComObject(iList); iList = null; } } /// public IEnumerator GetEnumerator() => Enum().GetEnumerator(); /// public override string ToString() => "prop:" + string.Join(";", this.Select(d => $"{GetPrefixForViewFlags(d.ViewFlags)}{d.CanonicalName}").ToArray()); /// IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// Enumerates through the items in this instance. /// An for this list. protected virtual IEnumerable Enum() { for (var i = 0; i < Count; i++) yield return this[i]; } // TODO: Incomplete. Needs to also include ?, < and & flags, but they are not documented. private static string GetPrefixForViewFlags(PROPDESC_VIEW_FLAGS flags) { var sb = new StringBuilder(); foreach (var e in flags.GetFlags()) { switch (e) { case PROPDESC_VIEW_FLAGS.PDVF_CENTERALIGN: sb.Append('|'); break; case PROPDESC_VIEW_FLAGS.PDVF_RIGHTALIGN: sb.Append('/'); break; case PROPDESC_VIEW_FLAGS.PDVF_BEGINNEWGROUP: sb.Append('^'); break; case PROPDESC_VIEW_FLAGS.PDVF_FILLAREA: sb.Append('#'); break; case PROPDESC_VIEW_FLAGS.PDVF_SORTDESCENDING: sb.Append('-'); break; case PROPDESC_VIEW_FLAGS.PDVF_SHOWONLYIFPRESENT: sb.Append('*'); break; case PROPDESC_VIEW_FLAGS.PDVF_HIDELABEL: sb.Append('~'); break; } } if (flags.IsFlagSet(PROPDESC_VIEW_FLAGS.PDVF_SHOWBYDEFAULT | PROPDESC_VIEW_FLAGS.PDVF_SHOWINPRIMARYLIST | PROPDESC_VIEW_FLAGS.PDVF_SHOWINSECONDARYLIST)) sb.Append('0'); else if (flags.IsFlagSet(PROPDESC_VIEW_FLAGS.PDVF_SHOWINPRIMARYLIST | PROPDESC_VIEW_FLAGS.PDVF_SHOWINSECONDARYLIST)) sb.Append('1'); if (flags.IsFlagSet(PROPDESC_VIEW_FLAGS.PDVF_SHOWINSECONDARYLIST)) sb.Append('2'); return sb.ToString(); } } /// Exposes methods that extract data from enumeration information. /// public class PropertyType : IDisposable { /// The IPropertyEnumType instance. protected IPropertyEnumType iType; /// The IPropertyEnumType2 instance. protected IPropertyEnumType2 iType2; /// Initializes a new instance of the class. /// The IPropertyEnumType object. protected internal PropertyType(IPropertyEnumType type) { iType = type; } /// Gets the display text. /// The display text. public string DisplayText { get { try { iType.GetDisplayText(out var s); return s; } catch { return null; } } } /// Gets an enumeration type. /// The enumeration type. public PROPENUMTYPE EnumType => iType?.GetEnumType() ?? 0; /// Gets the image reference. /// The image reference. public IconLocation ImageReference { get { if (iType2 == null) iType2 = iType as IPropertyEnumType2; string img = null; return IconLocation.TryParse(iType2?.GetImageReference(out img).Succeeded ?? false ? img : null, out var loc) ? loc : new IconLocation(); } } /// Gets a minimum value. /// The minimum value. public object RangeMinValue { get { try { var t = new PROPVARIANT(); iType.GetRangeMinValue(t); return t.Value; } catch { return null; } } } /// Gets a set value. /// The set value. public object RangeSetValue { get { try { var t = new PROPVARIANT(); iType.GetRangeSetValue(t); return t.Value; } catch { return null; } } } /// Gets a value. /// The value.EnumType != PROPENUMTYPE.PET_DEFAULTVALUE ? public object Value { get { try { var t = new PROPVARIANT(); iType.GetValue(t); return t.Value; } catch { return null; } } } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public virtual void Dispose() { if (iType2 != null) { Marshal.ReleaseComObject(iType2); iType2 = null; } if (iType != null) { Marshal.ReleaseComObject(iType); iType = null; } } /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() => DisplayText ?? ""; } /// Exposes methods that enumerate the possible values for a property. /// /// public class PropertyTypeList : IReadOnlyList, IDisposable { /// The IPropertyEnumTypeList object. protected IPropertyEnumTypeList iList; /// Initializes a new instance of the class. /// The IPropertyEnumTypeList object. protected internal PropertyTypeList(IPropertyEnumTypeList list) { iList = list; } /// Gets the number of elements in the collection. /// The number of elements in the collection. public virtual int Count => (int)(iList?.GetCount() ?? 0); /// Gets the at the specified index. /// The . /// The index. /// The at the specified index. public virtual PropertyType this[int index] => new PropertyType(iList?.GetAt((uint)index, typeof(IPropertyEnumType).GUID)); /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public virtual void Dispose() { if (iList != null) { Marshal.ReleaseComObject(iList); iList = null; } } /// Determines the index of a specific item in the list. /// The object to locate in the list. /// The index of item if found in the list; otherwise, -1. public virtual int IndexOf(object obj) => iList == null ? -1 : (iList.FindMatchingIndex(new PROPVARIANT(obj), out var idx).Succeeded ? (int)idx : -1); /// Returns an enumerator that iterates through the collection. /// /// A that can be used to iterate through the collection. /// public IEnumerator GetEnumerator() => Enum().GetEnumerator(); /// Returns an enumerator that iterates through a collection. /// /// An object that can be used to iterate through the collection. /// IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// Enumerates through the items in this instance. /// An for this list. protected virtual IEnumerable Enum() { for (var i = 0; i < Count; i++) yield return this[i]; } } }