PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop.ExtensionUtils/ConfigurationWizard/ViewModel/CertificatePageViewModel.cs

#
C# | 143 lines | 93 code | 19 blank | 31 comment | 6 complexity | 1dc46baef8ec2dd88a9841290aef2a3a MD5 | raw file
  1// <copyright file="BasicSettingsPageViewModel.cs" company="Microsoft Corporation">
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ServiceModel.Interop.ConfigurationWizard.ViewModel
  6{
  7    using System;
  8    using System.Collections.Generic;
  9    using System.Linq;
 10    using Microsoft.ServiceModel.Interop.ConfigurationWizard.Services;
 11
 12    /// <summary>
 13    /// View model for the certificate selection view page
 14    /// </summary>
 15    public class CertificatePageViewModel : PageViewModelBase
 16    {
 17        private string storeLocation;
 18        private string store;
 19        private string certificate;
 20        private ICertificateStoreService certificateStore;
 21        private IEnumerable<string> certificates;
 22
 23        /// <summary>
 24        /// Initializes a new instance of the CertificatePageViewModel class
 25        /// </summary>
 26        /// <param name="certificateStore">A valid instance of ICertificateStoreService for selecting an existing certificate</param>
 27        public CertificatePageViewModel(ICertificateStoreService certificateStore)
 28        {
 29            if (certificateStore == null)
 30            {
 31                throw new ArgumentNullException("certificateStore");
 32            }
 33
 34            this.certificateStore = certificateStore;
 35
 36            this.store = this.certificateStore.GetStores().First();
 37            this.storeLocation = this.certificateStore.GetStoreLocations().First();
 38            this.certificates = this.certificateStore.GetCertificates(this.storeLocation, this.store);
 39        }
 40
 41        /// <summary>
 42        /// Gets the view display name
 43        /// </summary>
 44        public override string DisplayName
 45        {
 46            get { return "Certificate Selection"; }
 47        }
 48
 49        /// <summary>
 50        /// Gets the list of certificate store locations
 51        /// </summary>
 52        public IEnumerable<string> StoreLocations
 53        {
 54            get { return this.certificateStore.GetStoreLocations(); }
 55        }
 56
 57        /// <summary>
 58        /// Gets or sets the selected certificate store location
 59        /// </summary>
 60        public string StoreLocation
 61        {
 62            get
 63            {
 64                return this.storeLocation;
 65            }
 66
 67            set
 68            {
 69                if (this.storeLocation != value)
 70                {
 71                    this.storeLocation = value;
 72                    this.OnStoreLocationChanged();
 73                }
 74            }
 75        }
 76
 77        /// <summary>
 78        /// Gets the list of certificate stores
 79        /// </summary>
 80        public IEnumerable<string> Stores
 81        {
 82            get { return this.certificateStore.GetStores(); }
 83        }
 84
 85        /// <summary>
 86        /// Gets or sets the selected certificate store
 87        /// </summary>
 88        public string Store
 89        {
 90            get
 91            {
 92                return this.store;
 93            }
 94
 95            set
 96            {
 97                if (this.store != value)
 98                {
 99                    this.store = value;
100                    this.OnStoreChanged();
101                }
102            }
103        }
104
105        /// <summary>
106        /// Gets the list of certificates
107        /// </summary>
108        public IEnumerable<string> Certificates
109        {
110            get { return this.certificates; }
111        }
112
113        /// <summary>
114        /// Gets or sets the selected certificate
115        /// </summary>
116        public string Certificate
117        {
118            get { return this.certificate; }
119            set { SetValue(ref this.certificate, value, () => this.Certificate); }
120        }
121
122        internal override bool IsValid()
123        {
124            return !string.IsNullOrEmpty(this.Certificate);
125        }
126
127        private void OnStoreChanged()
128        {
129            this.certificates = this.certificateStore.GetCertificates(this.StoreLocation, this.Store);
130
131            this.OnPropertyChanged(() => this.Store);
132            this.OnPropertyChanged(() => this.Certificates);
133        }
134
135        private void OnStoreLocationChanged()
136        {
137            this.certificates = this.certificateStore.GetCertificates(this.StoreLocation, this.Store);
138
139            this.OnPropertyChanged(() => this.StoreLocation);
140            this.OnPropertyChanged(() => this.Certificates);
141        }
142    }
143}