using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Lithnet.CredentialProvider.Interop; namespace Lithnet.CredentialProvider { /// /// Represents the base of all control types /// public abstract class ControlBase : INotifyPropertyChanged { private static uint fieldIdCounter; private FieldState state; private FieldInteractiveState interactiveState; private string label; private FieldOptions options; private protected ILogger logger; public event PropertyChangedEventHandler PropertyChanged; private protected ControlBase(ControlBase source) { this.Id = source.Id; this.Key = source.Key; this.Type = source.Type; this.FieldTypeGuid = source.FieldTypeGuid; this.logger = source.logger; this.state = source.State; this.interactiveState = source.InteractiveState; this.label = source.Label; this.options = source.options; } private protected ControlBase(string key, FieldType type) : this(key, null, type, Guid.Empty) { } private protected ControlBase(string key, string label, FieldType type) : this(key, label, type, Guid.Empty) { } private protected ControlBase(string key, string label, FieldType type, Guid guidType) { this.Id = fieldIdCounter++; this.Type = type; this.FieldTypeGuid = guidType; this.label = label; this.state = FieldState.DisplayInSelectedTile; this.interactiveState = FieldInteractiveState.None; this.options = FieldOptions.None; this.logger = TraceLoggerFactory.Instance.CreateLogger(this.GetType()); this.Key = key; } internal void AssignCredential(ICredentialProviderCredential credential) { this.Credential = credential; } internal void AssignEvents(ICredentialProviderCredentialEvents events) { this.Events = events; } internal void UnassignEvents() { this.Events = null; } internal ICredentialProviderCredentialEvents Events { get; private set; } internal ICredentialProviderCredential Credential { get; private set; } internal void SetLogger(ILoggerFactory loggerFactory) { this.logger = loggerFactory.CreateLogger(this.GetType()); } /// /// Gets the unique ID associated with this control /// public string Key { get; } /// /// Gets the internal numerical ID associated with this control /// public uint Id { get; } /// /// Gets or sets the label text for the control /// public string Label { get { return this.label; } set { if (this.label != value) { this.label = value; if (this.Type != FieldType.PasswordText && this.Type != FieldType.EditText) { this.Events?.SetFieldString(this.Credential, this.Id, Marshal.StringToCoTaskMemUni(this.label)); } this.RaisePropertyChanged(); } } } internal FieldType Type { get; } internal Guid FieldTypeGuid { get; } /// /// Gets or sets the options that control the rendering of this field /// public FieldOptions Options { get { return this.options; } set { if (this.options != value) { this.options = value; if (this.Events is ICredentialProviderCredentialEvents2 e) { e.SetFieldOptions(this.Credential, this.Id, this.options); } this.RaisePropertyChanged(); } } } /// /// Gets or sets the states in which this control should be displayed /// public FieldState State { get { return this.state; } set { if (this.state != value) { this.state = value; this.Events?.SetFieldState(this.Credential, this.Id, this.state); this.RaisePropertyChanged(); } } } /// /// Gets or sets the interactivity status of the control /// public FieldInteractiveState InteractiveState { get { return this.interactiveState; } set { if (this.interactiveState != value) { this.interactiveState = value; this.Events?.SetFieldInteractiveState(this.Credential, this.Id, this.interactiveState); this.RaisePropertyChanged(); } } } internal FieldDescriptor GetDescriptor() { return new FieldDescriptor { FieldType = this.Type, FieldID = this.Id, FieldTypeGuid = this.FieldTypeGuid, Label = this.Label }; } /// public override string ToString() { return $"{this.Id}:{this.Type}:{this.Label}"; } internal void RaisePropertyChanged([CallerMemberName] string name = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } internal abstract ControlBase Clone(); } }