context: //-----------------------------------------------------------------------------
  //
  // <copyright file="ManagedAPIClasses.cs" company="Microsoft">
  //    Copyright (C) Microsoft Corporation.  All rights reserved.
  // </copyright>
  //
  // Description:  Enums and classes needed for the MSIPC Managed API.
  //               
  //
  //
  //-----------------------------------------------------------------------------
  
  
  using System;
  using System.Collections.ObjectModel;
  using System.Globalization;
  using System.Runtime.InteropServices;
  using System.Windows.Interop;
  
  namespace Microsoft.InformationProtectionAndControl
  {
      public class InformationProtectionException : Exception
      {
          public InformationProtectionException(int hrError, string message)
              : base(string.Format("{0} HRESULT: 0x{1:X8}", message, hrError))
          {
              HResult = hrError;
          }
  
          public int ErrorCode
          {
              get { return HResult; }
          }
      };
  
      // API Mode values - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535236(v=vs.85).aspx
      public enum APIMode : int
      {
          Client = 1,
          Server = 2,
      };
  
      // IpcSerializeLicense() flags - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535269(v=vs.85).aspx
      [Flags]
      public enum SerializeLicenseFlags : int
      {
          KeyNoPersist = 2,
          KeyNoPersistOnDisk = 4,
          DeprecatedUnsignedLicenseTemplate = 8,
          KeyNoPersistInLicense = 16,
      };
  
      //  User Id types - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535284(v=vs.85).aspx
      public enum UserIdType : uint
      {
          Email = 1,
          IpcUser = 2,
      };
  
      // IPC-internal User IDs - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535284(v=vs.85).aspx
      public class IpcUserIDs
      {
          public const string UserIdEveryone = "ANYONE";
          public const string UserIdNull = "NULL";
          public const string UserIdOwner = "OWNER";
      }
  
      // Connection Info - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535274(v=vs.85).aspx
      public class ConnectionInfo
      {
          public ConnectionInfo(Uri extranetUrl, Uri intranetUrl, bool bConfigureForLicenseingOnlyClusters = false)
          {
              if (extranetUrl == null && intranetUrl == null)
              {
                  throw new ArgumentException("Either extranetUrl or intranetUrl should not be null");
              }
              m_extranetUrl = extranetUrl;
              m_intranetUrl = intranetUrl;
  
              OverrideServiceDiscoveryForLicensing = bConfigureForLicenseingOnlyClusters;
          }
  
          /// <summary>
          /// This flag only applies to IpcGetTemplateIssuerList and IpcGetTemplateList. When set, this flag configures
          /// MSIPC to work correctly with multiple AD RMS licensing-only clusters. When this flag is set, service
          /// discovery will use these URLs to locate the appropriate licensing server. When this flag is not set,
          /// service discovery will use these URLs to locate the certification cluster and then locate the licensing
          /// service off of that certification cluster.
          /// </summary>
          public bool OverrideServiceDiscoveryForLicensing { get; private set; }
  
          public Uri ExtranetUrl
          {
              get { return m_extranetUrl; }
          }
  
          public Uri IntranetUrl
          {
              get { return m_intranetUrl; }
          }
  
          private Uri m_extranetUrl = null;
          private Uri m_intranetUrl = null;
      }
  
      // Template Info - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535279(v=vs.85).aspx
      public class TemplateInfo
      {
          public string TemplateId
          {
              get { return m_templateId; }
              set { m_templateId = value; }
          }
  
          public CultureInfo CultureInfo
          {
              get { return m_cultureInfo; }
              set { m_cultureInfo = value; }
          }
  
          public string Description
          {
              get { return m_description; }
              set { m_description = value; }
          }
  
          public string Name
          {
              get { return m_name; }
              set { m_name = value; }
          }
  
          public string IssuerDisplayName
          {
              get { return m_issuerDisplayName; }
              set { m_issuerDisplayName = value; }
          }
  
          public bool FromTemplate
          {
              get { return m_fromTemplate; }
              set { m_fromTemplate = value; }
          }
  
          public TemplateInfo(string templateId, CultureInfo cultureInfo, string templateName, string templateDescription, string issuerDisplayName, bool fromTemplate)
          {
              m_templateId = templateId;
              m_cultureInfo = cultureInfo;
              m_name = templateName;
              m_description = templateDescription;
              m_issuerDisplayName = issuerDisplayName;
              m_fromTemplate = fromTemplate;
          }
          
          private string m_templateId = null;
          private CultureInfo m_cultureInfo = null;
          private string m_name = null;
          private string m_description = null;
          private string m_issuerDisplayName = null;
          private bool m_fromTemplate = false;
      }
  
      // Template Issuer - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535280(v=vs.85).aspx
      public class TemplateIssuer
      {
          public ConnectionInfo ConnectionInfo
          {
              get { return m_connectionInfo; }
          }
  
          public string DisplayName
          {
              get { return m_displayName; }
          }
  
          public bool AllowFromScratch
          {
              get { return m_allowFromScratch; }
          }
  
          public TemplateIssuer(ConnectionInfo connectionInfo, string displayName, bool allowFromScratch)
          {
              m_connectionInfo = connectionInfo;
              m_displayName = displayName;
              m_allowFromScratch = allowFromScratch;
          }
  
          private ConnectionInfo m_connectionInfo = null;
          private string m_displayName = null;
          private bool m_allowFromScratch = false;
      }
  
      // Term - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535282(v=vs.85).aspx
      public class Term
      {
          public DateTime From
          {
              get { return m_from; }
              set { m_from = value; }
          }
  
          public TimeSpan Duration
          {
              get { return m_duration; }
              set { m_duration = value; }
          }
  
          public static bool IsValid(Term validate)
          {
              DateTime termDisabled = DateTime.FromFileTime(0);
              return (validate != null && (validate.From > termDisabled || validate.Duration.Ticks > 0));
          }
  
          private DateTime m_from = new DateTime();
          private TimeSpan m_duration = new TimeSpan();
      }
  
      // Common Rights - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535295(v=vs.85).aspx
      public class CommonRights
      {
          public static string OwnerRight = "OWNER";
          public static string ViewRight = "VIEW";
          public static string EditRight = "DOCEDIT";
          public static string SaveRight = "EDIT";
          public static string ExtractRight = "EXTRACT";
          public static string ExportRight = "EXPORT";
          public static string PrintRight = "PRINT";
          public static string CommentRight = "COMMENT";
          public static string ViewRightsDataRight = "VIEWRIGHTSDATA";
          public static string EditRightsDataRight = "EDITRIGHTSDATA";
          public static string ForwardRight = "FORWARD";
          public static string ReplyRight = "REPLY";
          public static string ReplyAllRight = "REPLYALL";
      }
  
      // User Rights - http://msdn.microsoft.com/en-us/library/windows/desktop/hh535285(v=vs.85).aspx
      public class UserRights
      {
          public UserRights(UserIdType userIdType, string principalId, Collection<string> rights)
          {
              if ((userIdType != UserIdType.Email) &&
                  (userIdType != UserIdType.IpcUser))
              {
                  throw new ArgumentOutOfRangeException("principalIdType");
              }
  
              if (principalId == null)
              {
  
gt:                 throw new ArgumentNullException("principalId");