SignatureResourceHelper.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / SignatureResourceHelper.cs / 1 / SignatureResourceHelper.cs

                            //---------------------------------------------------------------------------------------- 
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved. 
//
// Description: 
//    SignatureResourceHelper is a helper class used to get resources.
// 
// History: 
// 05/03/05 - [....] created
// 07/26/05 - [....] refactored to reduce code and complexity 
//                     added support for rendering Image from DrawingBrush
//---------------------------------------------------------------------------------------
using System;
using System.Collections; 
using Drawing = System.Drawing;
using System.Globalization; 
using System.IO; 
using System.Security.Cryptography.X509Certificates;
using System.Windows; 
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging; 
using System.Windows.TrustUI;
using System.Security.Permissions; 
using System.Security; 

using MS.Internal.Documents.Application; 

namespace MS.Internal.Documents
{
    internal static class SignatureResourceHelper 
    {
        #region Internal Methods 
        //------------------------------------------------------------------------------- 
        //  Internal Methods
        //------------------------------------------------------------------------------- 

        /// 
        /// This will return the resources based on the overall status of the document.
        ///  
        /// The overall status of the document.
        /// The document level resources. 
        internal static DocumentStatusResources GetDocumentLevelResources( 
            SignatureStatus status)
        { 
            DocumentStatusResources docSigStatusResources = new DocumentStatusResources();

            // Set the text representation for the status.
            switch (status) 
            {
                case SignatureStatus.Valid: 
                    docSigStatusResources.Text = SR.Get(SRID.DocumentSignatureManagerValid); 
                    docSigStatusResources.ToolTip = SR.Get(SRID.DocumentSignatureManagerAppliedToolTip);
                    break; 
                case SignatureStatus.Invalid:
                    docSigStatusResources.Text = SR.Get(SRID.DocumentSignatureManagerInvalid);
                    docSigStatusResources.ToolTip = SR.Get(SRID.DocumentSignatureManagerAppliedToolTip);
                    break; 
                case SignatureStatus.NotSigned:
                    docSigStatusResources.Text = String.Empty; 
                    docSigStatusResources.ToolTip = SR.Get(SRID.DocumentSignatureManagerDefaultToolTip); 
                    break;
                default: // SignatureStatus.Unknown or SignatureStatus.Undetermined 
                         // In this case signatures have been applied to the document, but
                         // the validity of the signatures is not yet known.
                    docSigStatusResources.Text = SR.Get(SRID.DocumentSignatureManagerUndetermined);
                    docSigStatusResources.ToolTip = SR.Get(SRID.DocumentSignatureManagerAppliedToolTip); 
                    break;
            } 
 
            // Set the image representation for the status.
            docSigStatusResources.Image = GetDrawingBrushFromStatus(status); 
            return docSigStatusResources;
        }

        ///  
        /// Get the Image icon for the status.
        ///  
        /// Requested signature status 
        /// Requested certificate status
        /// A Image on success (valid status, DrawingBrush found), null 
        /// otherwise.
        internal static Drawing.Image GetImageFromStatus(
            int height,
            int width, 
            SignatureStatus sigStatus,
            CertificatePriorityStatus certStatus) 
        { 
            //
 
            // If the signature is okay, but the certificate cannot be trusted then display
            // invalid signature image.
            if ((sigStatus == SignatureStatus.Valid) && (certStatus != CertificatePriorityStatus.Ok))
            { 
                sigStatus = SignatureStatus.Invalid;
            } 
 
            string resourceName = string.Format(
                CultureInfo.InvariantCulture, 
                @"{0}_{1}x{2}",
                sigStatus.ToString(),
                height,
                width); 
            return (Drawing.Image)Resources.ResourceManager.GetObject(resourceName);
        } 
 
        /// 
        /// Will return the resources for the provided digital signature. 
        /// 
        /// The signature to get resources for.
        /// The signature resources.
        internal static SignatureResources GetResources(DigitalSignature signature, CertificatePriorityStatus certStatus) 
        {
            const int defaultHeight = 35; 
            const int defaultWidth = 35; 

            SignatureResources resources = new SignatureResources(); 
            string none = SR.Get(SRID.SignatureResourceHelperNone);

            resources._displayImage = GetImageFromStatus(
                defaultHeight, defaultWidth, signature.SignatureState, certStatus); 
            resources._location =
                string.IsNullOrEmpty(signature.Location) ? none : signature.Location; 
            resources._reason = 
                string.IsNullOrEmpty(signature.Reason) ? none : signature.Reason;
            resources._signBy = GetFormattedDate(signature.SignedOn); 
            resources._subjectName = signature.SubjectName;
            resources._summaryMessage = GetSummaryMessage(signature, certStatus);

            Trace.SafeWrite( 
                Trace.Rights,
                "Resources generated for {0} summary: {1}", 
                resources._subjectName, 
                resources._summaryMessage);
 
            return resources;
        }
        #endregion Internal Methods
 
        #region Private Methods
        ///  
        /// Get the DrawingBrush icon for the status. 
        /// 
        /// Requested status 
        /// A DrawingBrush on success (valid status, DrawingBrush found), null
        /// otherwise.
        private static DrawingBrush GetDrawingBrushFromStatus(SignatureStatus sigStatus)
        { 
            if (_brushResources == null)
            { 
                // Get the entire list of SignatureStatus values. 
                Array statusList = Enum.GetValues(typeof(SignatureStatus));
 
                // Construct the array to hold brush references.
                _brushResources = new DrawingBrush[statusList.Length];

                // To find the DrawingBrushes in the theme resources we need a 
                // FrameworkElement. TextBlock was used as it appears to have a very small
                // footprint, and won't take long to construct.  The actual 
                // FrameworkElement doesn't matter as long as we have an instance to one 
                _frameworkElement = new TextBlock();
            } 

            if ((_brushResources != null) && (_frameworkElement != null))
            {
                int index = (int)sigStatus; 

                // If there is no cached value of the requested DrawingBrush, then find 
                // it in the Resources. 
                if (_brushResources[index] == null)
                { 
                    // Determine resource name.
                    string resourceName = "PUISignatureStatus"
                        + Enum.GetName(typeof(SignatureStatus), sigStatus)
                        + "BrushKey"; 

                    // Acquire reference to the brush. 
                    object resource = _frameworkElement.FindResource( 
                        new ComponentResourceKey(
                            typeof(PresentationUIStyleResources), resourceName)); 

                    // Set cache value for the brush.
                    _brushResources[index] = resource as DrawingBrush;
                } 
                return _brushResources[index];
            } 
 
            return null;
        } 
        /// 
        /// Builds the summary message.
        /// 
        /// A DigitalSignature 
        /// A summary message.
        private static string GetSummaryMessage(DigitalSignature signature, CertificatePriorityStatus certStatus) 
        { 
            if (signature == null)
            { 
                return string.Empty;
            }

            // Setup the location text.  If not currently set, replace with the 
            // string "" to denote that no value was set.
            string location = (String.IsNullOrEmpty(signature.Location)) ? 
                SR.Get(SRID.SignatureResourceHelperNone) : signature.Location; 

            string result = String.Empty; 

            switch (signature.SignatureState)
            {
                case SignatureStatus.Valid: 
                case SignatureStatus.Invalid:
                case SignatureStatus.Unverifiable: 
                    // Verify that if the signature is valid, it has a certificate 
                    Invariant.Assert(
                        !(signature.SignatureState == SignatureStatus.Valid && signature.Certificate == null), 
                        SR.Get(SRID.SignatureResourceHelperMissingCertificate));

                    // Create the signature status message
                    string sigSummary = string.Format(CultureInfo.CurrentCulture, 
                        SR.Get(SRID.SignatureResourceHelperSummaryBreakLine),
                        GetSignatureSummaryMessage(signature.SignatureState, certStatus)); 
 
                    // Create the certificate status message (if required)
                    string certSummary = String.Empty; 
                    if (certStatus != CertificatePriorityStatus.Ok)
                    {
                        certSummary = string.Format(CultureInfo.CurrentCulture,
                            SR.Get(SRID.SignatureResourceHelperSummaryBreakLine), 
                            GetCertificateSummaryMessage(certStatus));
                    } 
 
                    // Create the summary message using the signature and certificate messages
                    // along with details from the current signature. 
                    result = string.Format(CultureInfo.CurrentCulture,
                        SR.Get(SRID.SignatureResourceHelperSummaryFormat),
                        sigSummary,
                        certSummary, 
                        signature.SubjectName,
                        signature.SignedOn, 
                        location); 

                    break; 

                case SignatureStatus.NotSigned:
                    // Create the summary message using signature information
                    result = string.Format(CultureInfo.CurrentCulture, 
                        SR.Get(SRID.SignatureResourceHelperValidSigSummaryPending),
                        signature.SubjectName, 
                        GetFormattedDate(signature.SignedOn), 
                        location);
                    break; 
            }

            return result;
        } 

        ///  
        /// Acquire the UI message associated with the certificate status 
        /// 
        /// The status to represent 
        /// The string associated with the status, otherwise String.Empty.
        private static string GetCertificateSummaryMessage(CertificatePriorityStatus certStatus)
        {
            string message = String.Empty; 
            switch (certStatus)
            { 
                case CertificatePriorityStatus.Ok : 
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusOk);
                    break; 
                case CertificatePriorityStatus.Corrupted :
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusCorrupted);
                    break;
                case CertificatePriorityStatus.CannotBeVerified : 
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusCannotBeVerified);
                    break; 
                case CertificatePriorityStatus.IssuerNotTrusted : 
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusIssuerNotTrusted);
                    break; 
                case CertificatePriorityStatus.Revoked :
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusRevoked);
                    break;
                case CertificatePriorityStatus.Expired : 
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusExpired);
                    break; 
                case CertificatePriorityStatus.NoCertificate : 
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusNoCertificate);
                    break; 
                case CertificatePriorityStatus.Verifying :
                    message = SR.Get(SRID.SignatureResourceHelperCertificateStatusVerifying);
                    break;
            } 

            return message; 
        } 

        ///  
        /// Acquire the UI message associated with the signature status
        /// 
        /// The signature status to represent
        /// The certificate status for the signature 
        /// The string associated with the status, otherwise String.Empty.
        private static string GetSignatureSummaryMessage(SignatureStatus sigStatus, CertificatePriorityStatus certStatus) 
        { 
            string message = String.Empty;
 
            if (sigStatus == SignatureStatus.Valid)
            {
                message = (certStatus == CertificatePriorityStatus.Ok) ?
                    SR.Get(SRID.SignatureResourceHelperSignatureStatusValid) : // Cert valid 
                    SR.Get(SRID.SignatureResourceHelperSignatureStatusValidCertInvalid); // Cert invalid
            } 
            else if (sigStatus == SignatureStatus.Unverifiable) 
            {
                message = SR.Get(SRID.SignatureResourceHelperSignatureStatusUnverifiable); 
            }
            else
            {
                message = SR.Get(SRID.SignatureResourceHelperSignatureStatusInvalid); 
            }
 
            return message; 
        }
 
        /// 
        /// Will format the date for display.
        /// 
        /// Either null or a valid date. 
        /// The short date or 'none' if the value was null.
        private static string GetFormattedDate(Nullable date) 
        { 
            string none = SR.Get(SRID.SignatureResourceHelperNone);
 
            return date == null ?
                none :
                String.Format(
                    CultureInfo.CurrentCulture, 
                    ((DateTime)date).ToShortDateString());
        } 
        #endregion Private Methods 

        #region Private Fields 
        /// 
        /// Caches DrawingBrushes loaded from styles.
        /// 
        private static DrawingBrush[]       _brushResources; 
        /// 
        /// Used to search resources. 
        ///  
        private static FrameworkElement _frameworkElement;
        #endregion 
    }

    /// 
    /// Internal representation of the document's signature status 
    /// 
    internal struct SignatureResources 
    { 
        public System.Drawing.Image _displayImage;
        public string _subjectName; 
        public string _summaryMessage;

        public string _reason;
        public string _location; 
        public string _signBy;
 
        ///  
        /// Provide a ToString implementation so that the ListItems in the
        /// SignatureSummary dialog provide text information for UIAutomation. 
        /// 
        /// 
        public override string ToString()
        { 
            return String.Format(
                CultureInfo.CurrentCulture, 
                SR.Get(SRID.SignatureResourcesFormatForAccessibility), 
                _summaryMessage,
                _subjectName, 
                _reason,
                _location,
                _signBy);
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK