Extend wrapper functions OUT-78
authorThomas
Fri, 15 Mar 2019 14:59:23 +0100
branchOUT-78
changeset 2601173103ca3542
parent 2600 0df6cdea7e12
child 2603 65f867d4c864
Extend wrapper functions
Extensions/MailItemExtensions.cs
ThisAddIn.cs
Wrappers/ExplorerWrapper.cs
Wrappers/InspectorWrapper.cs
Wrappers/MailItemWrapper.cs
Wrappers/WindowBaseWrapper.cs
     1.1 --- a/Extensions/MailItemExtensions.cs	Thu Mar 14 16:15:35 2019 +0100
     1.2 +++ b/Extensions/MailItemExtensions.cs	Fri Mar 15 14:59:23 2019 +0100
     1.3 @@ -1766,22 +1766,6 @@
     1.4          }
     1.5  
     1.6          /// <summary>
     1.7 -        /// Gets the personal identity of this mail item.
     1.8 -        /// Warning: This can return null.
     1.9 -        /// </summary>
    1.10 -        /// <param name="omi">The Outlook mail item to process with.</param>
    1.11 -        /// <returns>The myself identity or null if an error occured.</returns>
    1.12 -        public static PEPIdentity GetMyselfIdentity(this Outlook.MailItem omi)
    1.13 -        {
    1.14 -            if (PEPIdentity.GetOwnIdentity(omi, out PEPIdentity identity) != Globals.ReturnStatus.Success)
    1.15 -            {
    1.16 -                identity = null;
    1.17 -            }
    1.18 -
    1.19 -            return identity;
    1.20 -        }
    1.21 -
    1.22 -        /// <summary>
    1.23          /// Gets whether a given EntryID is in the list of copied items.
    1.24          /// </summary>
    1.25          /// <param name="entryId">The EntryID to check for.</param>
     2.1 --- a/ThisAddIn.cs	Thu Mar 14 16:15:35 2019 +0100
     2.2 +++ b/ThisAddIn.cs	Fri Mar 15 14:59:23 2019 +0100
     2.3 @@ -270,7 +270,7 @@
     2.4                  // First, check all watched explorers
     2.5                  foreach (var watchedExplorer in this.watchedExplorers)
     2.6                  {
     2.7 -                    if (watchedExplorer?.CurrentMailItem?.MAPIOBJECT?.Equals(omi?.MAPIOBJECT) == true)
     2.8 +                    if (watchedExplorer?.WrappedMailItem?.MailItem?.MAPIOBJECT?.Equals(omi?.MAPIOBJECT) == true)
     2.9                      {
    2.10                          return watchedExplorer;
    2.11                      }
    2.12 @@ -279,7 +279,7 @@
    2.13                  // If window hasn't been found yet, check inspectors
    2.14                  foreach (var watchedInspector in this.watchedInspectors)
    2.15                  {
    2.16 -                    if (watchedInspector?.CurrentMailItem?.MAPIOBJECT?.Equals(omi?.MAPIOBJECT) == true)
    2.17 +                    if (watchedInspector?.WrappedMailItem?.MailItem?.MAPIOBJECT?.Equals(omi?.MAPIOBJECT) == true)
    2.18                      {
    2.19                          return watchedInspector;
    2.20                      }
     3.1 --- a/Wrappers/ExplorerWrapper.cs	Thu Mar 14 16:15:35 2019 +0100
     3.2 +++ b/Wrappers/ExplorerWrapper.cs	Fri Mar 15 14:59:23 2019 +0100
     3.3 @@ -13,6 +13,7 @@
     3.4      {
     3.5          private string                  lastProcessedEntryId    = null;
     3.6          private MailItemWrapper         inlineResponseItem      = null;
     3.7 +        private MailItemWrapper         _WrappedMailItem        = null;
     3.8  
     3.9          /**************************************************************
    3.10           * 
    3.11 @@ -42,6 +43,14 @@
    3.12          public Outlook.Explorer Explorer { get; private set; } = null;
    3.13  
    3.14          /// <summary>
    3.15 +        /// Gets the window type of this window.
    3.16 +        /// </summary>
    3.17 +        public override WindowType Type
    3.18 +        {
    3.19 +            get { return WindowType.Explorer; }
    3.20 +        }
    3.21 +
    3.22 +        /// <summary>
    3.23          /// Gets the Explorer that is wrapped by this WatchedExplorer.
    3.24          /// </summary>
    3.25          public override dynamic Window
    3.26 @@ -50,11 +59,11 @@
    3.27          }
    3.28  
    3.29          /// <summary>
    3.30 -        /// Gets the window type of this window.
    3.31 +        /// Gets the wrapped mail item.
    3.32          /// </summary>
    3.33 -        public override WindowType Type
    3.34 +        internal override MailItemWrapper WrappedMailItem
    3.35          {
    3.36 -            get { return WindowType.Explorer; }
    3.37 +            get => this._WrappedMailItem;
    3.38          }
    3.39  
    3.40          #endregion
    3.41 @@ -295,6 +304,7 @@
    3.42          private void Explorer_SelectionChange()
    3.43          {
    3.44              Outlook.Selection selection = null;
    3.45 +
    3.46              try
    3.47              {
    3.48                  // Process the newly selected item
    3.49 @@ -305,9 +315,11 @@
    3.50                   */
    3.51                  if (selection.Count == 1)
    3.52                  {
    3.53 -                    this.CurrentMailItem = selection[1] as Outlook.MailItem;
    3.54 -                    if (this.CurrentMailItem != null)
    3.55 +                    if (selection[1] is Outlook.MailItem mailItem)
    3.56                      {
    3.57 +                        this._WrappedMailItem = new MailItemWrapper(mailItem);
    3.58 +                        mailItem = null;
    3.59 +
    3.60                          /* Check if inline response. Note: in Outlook 2010, there is no inline response.
    3.61                           * IMPORTANT: Never call Explorer.ActiveInlineResponse on Outlook 2010
    3.62                           *            as this might lead to a crash of Outlook (even inside a
    3.63 @@ -316,31 +328,24 @@
    3.64                          bool isInlineResponse = false;
    3.65                          if (Globals.OutlookVersion != Globals.Version.Outlook2010)
    3.66                          {
    3.67 -                            Outlook.MailItem inlineResponse = null;
    3.68                              try
    3.69                              {
    3.70 -                                inlineResponse = this.Explorer?.ActiveInlineResponse as Outlook.MailItem;
    3.71 -                                if (inlineResponse != null)
    3.72 +                                if (this.Explorer?.ActiveInlineResponse is Outlook.MailItem inlineResponse)
    3.73                                  {
    3.74 -                                    isInlineResponse = true;
    3.75 -                                    this.CurrentMailItem = inlineResponse;
    3.76 +                                    this._WrappedMailItem = new MailItemWrapper(inlineResponse);
    3.77                                  }
    3.78                              }
    3.79                              catch (Exception ex)
    3.80                              {
    3.81                                  Log.Error("Explorer_SelectionChange: Error determining whether it is an inline response. " + ex.ToString());
    3.82                              }
    3.83 -                            finally
    3.84 -                            {
    3.85 -                                inlineResponse = null;
    3.86 -                            }
    3.87  
    3.88                              /* If a different mail is clicked in an Window, the SelectionChange
    3.89                               * event is always called twice. To prevent a double processing, we only
    3.90                               * process a mail item if it is different from the last one that has been
    3.91                               * processed.
    3.92                               */
    3.93 -                            string entryId = isInlineResponse ? null : this.CurrentMailItem?.EntryID;
    3.94 +                            string entryId = isInlineResponse ? null : this.WrappedMailItem?.EntryId;
    3.95                              if (entryId?.Equals(lastProcessedEntryId, StringComparison.OrdinalIgnoreCase) == true)
    3.96                              {
    3.97                                  Log.Verbose("Explorer_SelectionChange: Same EntryId as last item. Skipped processing of item with EntryId " + entryId);
    3.98 @@ -353,7 +358,7 @@
    3.99                          }
   3.100  
   3.101                          // Initialize mail item
   3.102 -                        this.InitializeWindow(isInlineResponse);
   3.103 +                        this.InitializeWindow();
   3.104                      }                    
   3.105                      else
   3.106                      {
     4.1 --- a/Wrappers/InspectorWrapper.cs	Thu Mar 14 16:15:35 2019 +0100
     4.2 +++ b/Wrappers/InspectorWrapper.cs	Fri Mar 15 14:59:23 2019 +0100
     4.3 @@ -1,4 +1,5 @@
     4.4 -´╗┐using System;
     4.5 +´╗┐using pEp.Wrappers;
     4.6 +using System;
     4.7  using Outlook = Microsoft.Office.Interop.Outlook;
     4.8  
     4.9  namespace pEp
    4.10 @@ -8,6 +9,7 @@
    4.11      /// </summary>
    4.12      public class InspectorWrapper : WindowBaseWrapper
    4.13      {
    4.14 +        private MailItemWrapper _WrappedMailItem = null;
    4.15  
    4.16          /**************************************************************
    4.17           * 
    4.18 @@ -28,9 +30,9 @@
    4.19                  this.ConnectWatchedInspectorEvents();
    4.20  
    4.21                  // Set mail item
    4.22 -                this.CurrentMailItem = this.Inspector.CurrentItem as Outlook.MailItem;
    4.23 +                this._WrappedMailItem = new MailItemWrapper(this.Inspector.CurrentItem as Outlook.MailItem);
    4.24  
    4.25 -                if (this.CurrentMailItem != null)
    4.26 +                if (this._WrappedMailItem != null)
    4.27                  {
    4.28                      this.InitializeWindow();
    4.29                  }
    4.30 @@ -48,19 +50,27 @@
    4.31          public Outlook.Inspector Inspector { get; private set; } = null;
    4.32  
    4.33          /// <summary>
    4.34 +        /// Gets the window type of this window.
    4.35 +        /// </summary>
    4.36 +        public override WindowType Type
    4.37 +        {
    4.38 +            get => WindowType.Inspector; 
    4.39 +        }
    4.40 +
    4.41 +        /// <summary>
    4.42          /// Gets the Inspector that is wrapped by this WatchedInspector.
    4.43          /// </summary>
    4.44          public override dynamic Window
    4.45          {
    4.46 -            get { return this.Inspector; }
    4.47 +            get => this.Inspector; 
    4.48          }
    4.49  
    4.50          /// <summary>
    4.51 -        /// Gets the window type of this window.
    4.52 +        /// Gets the wrapped mail item.
    4.53          /// </summary>
    4.54 -        public override WindowType Type
    4.55 +        internal override MailItemWrapper WrappedMailItem
    4.56          {
    4.57 -            get { return WindowType.Inspector; }
    4.58 +            get => this._WrappedMailItem;
    4.59          }
    4.60          #endregion
    4.61  
     5.1 --- a/Wrappers/MailItemWrapper.cs	Thu Mar 14 16:15:35 2019 +0100
     5.2 +++ b/Wrappers/MailItemWrapper.cs	Fri Mar 15 14:59:23 2019 +0100
     5.3 @@ -101,7 +101,6 @@
     5.4          private bool                                        sent;
     5.5          private bool                                        isClosed;
     5.6  
     5.7 -        private Outlook.MailItem                            wrappedMailItem;
     5.8          private readonly BackgroundWorker                   mirrorLocator;
     5.9          private readonly BackgroundWorker                   processor;
    5.10  
    5.11 @@ -113,9 +112,16 @@
    5.12          private static readonly List<OriginalMailItem>      conversationCache       = new List<OriginalMailItem>();
    5.13          private static Dictionary<string, string>           decryptionList          = new Dictionary<string, string>();
    5.14  
    5.15 +        /// <summary>
    5.16 +        /// Stores the message id of an attached mail item if it is loaded into the preview.
    5.17 +        /// See comments in MailItem_BeforeAttachmentPreview event handler.
    5.18 +        /// </summary>
    5.19 +        public static string PreviewAttachedMailId = null;
    5.20 +
    5.21          // Property fields of the wrapped item
    5.22          private Outlook.OlAccountType?                      _AccountType            = null;
    5.23          private Outlook.OlDownloadState?                    _DownloadState          = null;
    5.24 +        private string                                      _EntryId                = null;
    5.25          private bool?                                       _IsDraft                = null;
    5.26          private bool?                                       _IsForcefullyProtected  = null;
    5.27          private bool?                                       _IsIncoming             = null;
    5.28 @@ -123,44 +129,10 @@
    5.29          private bool?                                       _IsInSentFolder         = null;
    5.30          private bool?                                       _IsMirror               = null;
    5.31          private bool?                                       _IsOriginallyEncrypted  = null;
    5.32 +        private bool?                                       _IsPEPEnabled           = null;
    5.33          private bool?                                       _IsSecure               = null;
    5.34          private bool?                                       _IsSubmitted            = null;
    5.35 -
    5.36 -        /// <summary>
    5.37 -        /// Stores the message id of an attached mail item if it is loaded into the preview.
    5.38 -        /// See comments below in MailItem_BeforeAttachmentPreview event handler.
    5.39 -        /// </summary>
    5.40 -        public static string PreviewAttachedMailId = null;
    5.41 -
    5.42 -        /// <summary>
    5.43 -        /// Whether to cancel the opening event of the internal mail item. Default is false.
    5.44 -        /// </summary>
    5.45 -        public bool CancelOpen { get; set; } = false;
    5.46 -
    5.47 -        /// <summary>
    5.48 -        /// The messageId of the internal mail item. Only needed for special cases like attached mails.
    5.49 -        /// </summary>
    5.50 -        public string MessageId { get; set; } = null;
    5.51 -
    5.52 -        /// <summary>
    5.53 -        /// Whether the internal mail item is an inline response
    5.54 -        /// </summary>
    5.55 -        public bool IsInlineResponse { get; set; } = false;
    5.56 -
    5.57 -        /// <summary>
    5.58 -        /// Whether the internal mail item is an attached mail.
    5.59 -        /// </summary>
    5.60 -        public bool IsSecureAttachedMail { get; set; } = false;
    5.61 -
    5.62 -        /// <summary>
    5.63 -        /// The mirror of the internal mail item stored as PEPMessage.
    5.64 -        /// </summary>
    5.65 -        public PEPMessage Mirror { get; set; } = null;
    5.66 -
    5.67 -        /// <summary>
    5.68 -        /// The PEPMessage of this mail item.
    5.69 -        /// </summary>
    5.70 -        public PEPMessage Message { get; set; } = null;
    5.71 +        private pEpRating?                                  _StoredRating           = null;
    5.72  
    5.73          /**************************************************************
    5.74           * 
    5.75 @@ -178,7 +150,7 @@
    5.76                                   pEpRating? defaultRating = null,
    5.77                                   bool decrementCounter = false)
    5.78          {
    5.79 -            this.wrappedMailItem = mailItem;
    5.80 +            this.MailItem = mailItem;
    5.81              this.ConnectInternalMailItemEvents(true);
    5.82  
    5.83              this.disposeAfterProcessing = false;
    5.84 @@ -214,6 +186,275 @@
    5.85  
    5.86          /**************************************************************
    5.87           * 
    5.88 +         * Property Accessors
    5.89 +         * 
    5.90 +         *************************************************************/
    5.91 +
    5.92 +        /// <summary>
    5.93 +        /// Gets the type of the account where the mail item is located in.
    5.94 +        /// </summary>
    5.95 +        public Outlook.OlAccountType AccountType
    5.96 +        {
    5.97 +            get => this._AccountType ?? ((Outlook.OlAccountType)(this._AccountType = this.MailItem.GetAccountType()));
    5.98 +        }
    5.99 +
   5.100 +        /// <summary>
   5.101 +        /// Gets the attachments of the wrapped mail item.
   5.102 +        /// </summary>
   5.103 +        public Outlook.Attachments Attachments
   5.104 +        {
   5.105 +            get => this.MailItem?.Attachments;
   5.106 +        }
   5.107 +
   5.108 +        /// <summary>
   5.109 +        /// Whether to cancel the opening event of the internal mail item. Default is false.
   5.110 +        /// </summary>
   5.111 +        public bool CancelOpen { get; set; } = false;
   5.112 +
   5.113 +        /// <summary>
   5.114 +        /// Returns a constant that belongs to the OlDownloadState enumeration indicating the download state of the item.
   5.115 +        /// This forwards the call to the internal mail item.
   5.116 +        /// See: https://msdn.microsoft.com/en-us/library/office/ff866978.aspx
   5.117 +        /// </summary>
   5.118 +        public Outlook.OlDownloadState DownloadState
   5.119 +        {
   5.120 +            get => this._DownloadState ?? ((Outlook.OlDownloadState)(this._DownloadState = this.MailItem.DownloadState));
   5.121 +        }
   5.122 +
   5.123 +        /// <summary>
   5.124 +        /// Gets the entry id of the wrapped mail item.
   5.125 +        /// </summary>
   5.126 +        public string EntryId
   5.127 +        {
   5.128 +            get => this._EntryId ?? (this._EntryId = this.MailItem.EntryID);
   5.129 +        }
   5.130 +
   5.131 +        /// <summary>
   5.132 +        /// Gets whether the mail item is currently being processed.
   5.133 +        /// </summary>
   5.134 +        public bool IsBeingProcessed
   5.135 +        {
   5.136 +            get => this.processor.IsBusy;
   5.137 +        }
   5.138 +
   5.139 +        /// <summary>
   5.140 +        /// Gets whether decryption is enabled for this mail item if pEp is disabled.
   5.141 +        /// </summary>
   5.142 +        public bool IsDecryptAlwaysEnabled
   5.143 +        {
   5.144 +            get => this.MailItem.GetIsDecryptAlwaysEnabled();
   5.145 +        }
   5.146 +
   5.147 +        /// <summary>
   5.148 +        /// Gets whether the mail item is marked as a draft (unsent).
   5.149 +        /// </summary>
   5.150 +        public bool IsDraft
   5.151 +        {
   5.152 +            get => this._IsDraft ?? ((bool)(this._IsDraft = this.MailItem.GetIsDraft()));
   5.153 +        }
   5.154 +
   5.155 +        /// <summary>
   5.156 +        /// Gets whether the mail item is forcefully protected.
   5.157 +        /// </summary>
   5.158 +        public bool IsForcefullyProtected
   5.159 +        {
   5.160 +            get => this._IsForcefullyProtected ?? ((bool)(this._IsForcefullyProtected = this.MailItem.GetIsForcefullyProtected()));
   5.161 +        }
   5.162 +
   5.163 +        /// <summary>
   5.164 +        /// Gets whether the mail item is marked as incoming (received mail, not sent).
   5.165 +        /// </summary>
   5.166 +        public bool IsIncoming
   5.167 +        {
   5.168 +            get => this._IsIncoming ?? ((bool)(this._IsIncoming = this.MailItem.GetIsIncoming()));
   5.169 +        }
   5.170 +
   5.171 +        /// <summary>
   5.172 +        /// Whether the internal mail item is an inline response
   5.173 +        /// </summary>
   5.174 +        public bool IsInlineResponse { get; set; } = false;
   5.175 +
   5.176 +        /// <summary>
   5.177 +        /// Gets whether the mail item is in a secure store (untrusted server).
   5.178 +        /// </summary>
   5.179 +        public bool IsInSecureStore
   5.180 +        {
   5.181 +            get => this._IsInSecureStore ?? ((bool)(this._IsInSecureStore = this.MailItem.GetIsInSecureStore()));
   5.182 +        }
   5.183 +
   5.184 +        /// <summary>
   5.185 +        /// Gets whether the mail item is in the Sent folder.
   5.186 +        /// </summary>
   5.187 +        public bool IsInSentFolder
   5.188 +        {
   5.189 +            get => this._IsInSentFolder ?? ((bool)(this._IsInSentFolder = this.MailItem.GetIsInSentFolder()));
   5.190 +        }
   5.191 +
   5.192 +        /// <summary>
   5.193 +        /// Gets whether the mail item is a mirror item.
   5.194 +        /// </summary>
   5.195 +        public bool IsMirror
   5.196 +        {
   5.197 +            get => this._IsMirror ?? ((bool)(this._IsMirror = this.MailItem.GetIsMirror()));
   5.198 +        }
   5.199 +
   5.200 +        /// <summary>
   5.201 +        /// Gets whether the wrapped mail item was marked as being originally encrypted.
   5.202 +        /// This should have been set by calling SetIsOriginallyEncryptedByCache() during the 
   5.203 +        /// EncryptedConversationCacheUpdated event.
   5.204 +        /// False is returned if the property is either false or null (doesn't exist).
   5.205 +        /// </summary>
   5.206 +        public bool IsOriginallyEncrypted
   5.207 +        {
   5.208 +            get => this._IsOriginallyEncrypted ?? ((bool)(this._IsOriginallyEncrypted = ((this.MailItem.GetUserProperty(MailItemWrapper.USER_PROPERTY_KEY_IS_ORIGINALLY_ENCRYPTED) as bool?) == true)));
   5.209 +        }
   5.210 +
   5.211 +        /// <summary>
   5.212 +        /// Gets whether the item is in a store that has pEp enabled.
   5.213 +        /// </summary>
   5.214 +        public bool IsPEPEnabled
   5.215 +        {
   5.216 +            get => this._IsPEPEnabled ?? ((bool)(this._IsPEPEnabled = this.MailItem.GetIsPEPEnabled()));
   5.217 +        }
   5.218 +
   5.219 +        /// <summary>
   5.220 +        /// Determines whether the mail item has been processed through pEp. This is done by checking if a pEp protocol
   5.221 +        /// version is available for the item.
   5.222 +        /// </summary>
   5.223 +        public bool IsPEPMessage
   5.224 +        {
   5.225 +            get => (MailItemExtensions.GetPEPProperty(MailItem, MailItemExtensions.PEPProperty.PEPProtocolVersion, out object version) && (version != null));
   5.226 +        }
   5.227 +
   5.228 +        /// <summary>
   5.229 +        /// Gets whether the mail item is secure (encrypted).
   5.230 +        /// </summary>
   5.231 +        public bool IsSecure
   5.232 +        {
   5.233 +            get => this._IsSecure ?? ((bool)(this._IsSecure = this.MailItem.GetIsSecure()));
   5.234 +        }
   5.235 +
   5.236 +        /// <summary>
   5.237 +        /// Whether the internal mail item is an attached mail.
   5.238 +        /// </summary>
   5.239 +        public bool IsSecureAttachedMail { get; set; } = false;
   5.240 +
   5.241 +        /// <summary>
   5.242 +        /// Gets whether the mail item is securely stored.
   5.243 +        /// </summary>
   5.244 +        public bool IsSecurelyStored
   5.245 +        {
   5.246 +            get => (((this.IsSecure || this.IsForcefullyProtected) && this.IsInSecureStore) || (this.NeverUnsecure && (this.IsMirror == false)));
   5.247 +        }
   5.248 +
   5.249 +        /// <summary>
   5.250 +        /// Gets whether the mail item has been submitted.
   5.251 +        /// </summary>
   5.252 +        public bool IsSubmitted
   5.253 +        {
   5.254 +            get => this._IsSubmitted ?? ((bool)(this._IsSubmitted = this.MailItem.GetIsSubmitted()));
   5.255 +        }
   5.256 +
   5.257 +        /// <summary>
   5.258 +        /// Gets the status after the last message processing.
   5.259 +        /// This should be used to determine if a failure occured.
   5.260 +        /// Success is the default if no processing has been completed.
   5.261 +        /// </summary>
   5.262 +        public Globals.ReturnStatus LastProcessedStatus
   5.263 +        {
   5.264 +            get { return (this._LastProcessedStatus); }
   5.265 +        }
   5.266 +
   5.267 +        /// <summary>
   5.268 +        /// The wrapped Outlook mail item.
   5.269 +        /// </summary>
   5.270 +        public Outlook.MailItem MailItem { get; private set; }
   5.271 +
   5.272 +        /// <summary>
   5.273 +        /// The PEPMessage of this mail item.
   5.274 +        /// </summary>
   5.275 +        public PEPMessage Message { get; set; } = null;
   5.276 +
   5.277 +        /// <summary>
   5.278 +        /// The messageId of the internal mail item. Only needed for special cases like attached mails.
   5.279 +        /// </summary>
   5.280 +        public string MessageId { get; set; } = null;
   5.281 +
   5.282 +        /// <summary>
   5.283 +        /// The mirror of the internal mail item stored as PEPMessage.
   5.284 +        /// </summary>
   5.285 +        public PEPMessage Mirror { get; set; } = null;
   5.286 +
   5.287 +        /// <summary>
   5.288 +        /// Gets the personal identity using the internal mail item.
   5.289 +        /// Warning: This can return null.
   5.290 +        /// </summary>
   5.291 +        public PEPIdentity Myself
   5.292 +        {
   5.293 +            get
   5.294 +            {
   5.295 +                lock (mutexMailItem)
   5.296 +                {
   5.297 +                    if (PEPIdentity.GetOwnIdentity(this.MailItem, out PEPIdentity ident) != Globals.ReturnStatus.Success)
   5.298 +                    {
   5.299 +                        ident = null;
   5.300 +                    }
   5.301 +
   5.302 +                    return ident;
   5.303 +                }
   5.304 +            }
   5.305 +        }
   5.306 +
   5.307 +        /// <summary>
   5.308 +        /// Gets or sets whether this cryptable mail item is marked to never be unsecure (always encrypted).
   5.309 +        /// This information is stored as a MAPI property of the wrapped mail item.
   5.310 +        /// Warning: This will call .Save() on the MailItem.
   5.311 +        /// </summary>
   5.312 +        public bool NeverUnsecure
   5.313 +        {
   5.314 +            get
   5.315 +            {
   5.316 +                object propValue;
   5.317 +
   5.318 +                lock (mutexMailItem)
   5.319 +                {
   5.320 +                    // Return status can be ignored here, using the auto default value is good enough
   5.321 +                    this.MailItem.GetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, out propValue);
   5.322 +                }
   5.323 +
   5.324 +                return ((bool)propValue);
   5.325 +            }
   5.326 +            set
   5.327 +            {
   5.328 +                lock (mutexMailItem)
   5.329 +                {
   5.330 +                    // Return status can be ignored
   5.331 +                    this.MailItem.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, value);
   5.332 +                    this.MailItem.Save();
   5.333 +                }
   5.334 +
   5.335 +                this.OnPropertyChanged();
   5.336 +            }
   5.337 +        }
   5.338 +
   5.339 +        /// <summary>
   5.340 +        /// Gets the recipients of the wrapped mail item.
   5.341 +        /// </summary>
   5.342 +        public Outlook.Recipients Recipients
   5.343 +        {
   5.344 +            get => this.MailItem?.Recipients;
   5.345 +        }
   5.346 +
   5.347 +        /// <summary>
   5.348 +        /// Gets the stored rating of the wrapped mail item.
   5.349 +        /// </summary>
   5.350 +        public pEpRating StoredRating
   5.351 +        {
   5.352 +            get => this._StoredRating ?? ((pEpRating)(this._StoredRating = this.MailItem.GetStoredRating() ?? pEpRating.pEpRatingUndefined));
   5.353 +        }
   5.354 +
   5.355 +        /**************************************************************
   5.356 +         * 
   5.357           * Event Handling
   5.358           * 
   5.359           *************************************************************/
   5.360 @@ -387,6 +628,11 @@
   5.361          private void MailItem_Write(ref bool cancel)
   5.362          {
   5.363  #if !READER_RELEASE_MODE
   5.364 +
   5.365 +            this.Write?.Invoke(ref cancel);
   5.366 +            return;
   5.367 +
   5.368 +
   5.369              /* Save drafts for untrusted servers in a secure location.
   5.370               * First we try to save it to a custom drafts folder in the pEp store
   5.371               * and make this store visible in the favorites.
   5.372 @@ -395,8 +641,8 @@
   5.373               */
   5.374              if ((this.sent == false) &&
   5.375                  (this.IsPEPMessage == false) &&
   5.376 -                (this.wrappedMailItem.GetIsDraft()) &&
   5.377 -                ((this.wrappedMailItem.GetIsInSecureStore() || this.wrappedMailItem.GetNeverUnsecure())))
   5.378 +                (this.MailItem.GetIsDraft()) &&
   5.379 +                ((this.MailItem.GetIsInSecureStore() || this.MailItem.GetNeverUnsecure())))
   5.380              {
   5.381                  bool useFallback = false;
   5.382                  Outlook.Folder parentFolder = null;
   5.383 @@ -480,7 +726,7 @@
   5.384                              if (string.IsNullOrEmpty(this.draftEntryId) == false)
   5.385                              {
   5.386                                  // Get all items in the pEp drafts folder with the same subject as the current draft
   5.387 -                                items = pEpDraftsFolder?.Items?.Restrict(string.Format("[Subject] = '{0}'", this.wrappedMailItem?.Subject));
   5.388 +                                items = pEpDraftsFolder?.Items?.Restrict(string.Format("[Subject] = '{0}'", this.MailItem?.Subject));
   5.389  
   5.390                                  for (int i = 1; i <= items?.Count; i++)
   5.391                                  {
   5.392 @@ -498,7 +744,7 @@
   5.393                                  items = null;
   5.394                              }
   5.395  
   5.396 -                            if (PEPMessage.Create(this.wrappedMailItem, out PEPMessage createdMessage) == Globals.ReturnStatus.Success)
   5.397 +                            if (PEPMessage.Create(this.MailItem, out PEPMessage createdMessage) == Globals.ReturnStatus.Success)
   5.398                              {
   5.399                                  if (draft == null)
   5.400                                  {
   5.401 @@ -532,14 +778,14 @@
   5.402                      else
   5.403                      {
   5.404                          // Get the mail item's parent folder
   5.405 -                        parentFolder = this.wrappedMailItem.Parent as Outlook.Folder;
   5.406 +                        parentFolder = this.MailItem.Parent as Outlook.Folder;
   5.407  
   5.408                          // Save to pEp folder if not already there
   5.409                          if (pEpDraftsFolder?.FullFolderPath?.Equals(parentFolder?.FullFolderPath) == false)
   5.410                          {
   5.411 -                            currentInspector = this.wrappedMailItem.GetInspector;
   5.412 +                            currentInspector = this.MailItem.GetInspector;
   5.413                              //omi = this.internalMailItem.Copy();
   5.414 -                            omi = this.wrappedMailItem.Move(pEpDraftsFolder);
   5.415 +                            omi = this.MailItem.Move(pEpDraftsFolder);
   5.416  
   5.417                              if (currentInspector != null)
   5.418                              {
   5.419 @@ -566,15 +812,15 @@
   5.420                                          (omi.SendUsingAccount == null))
   5.421                                      {
   5.422                                          // Try to add SendUsingAccount
   5.423 -                                        if (this.wrappedMailItem.SendUsingAccount != null)
   5.424 +                                        if (this.MailItem.SendUsingAccount != null)
   5.425                                          {
   5.426 -                                            omi.SendUsingAccount = this.wrappedMailItem.SendUsingAccount;
   5.427 +                                            omi.SendUsingAccount = this.MailItem.SendUsingAccount;
   5.428                                          }
   5.429  
   5.430                                          // Try to add the Sender directly
   5.431 -                                        if (this.wrappedMailItem.Sender != null)
   5.432 +                                        if (this.MailItem.Sender != null)
   5.433                                          {
   5.434 -                                            omi.Sender = this.wrappedMailItem.Sender;
   5.435 +                                            omi.Sender = this.MailItem.Sender;
   5.436                                          }
   5.437                                      }
   5.438                                  }
   5.439 @@ -698,13 +944,13 @@
   5.440                                  Filter = Properties.Resources.DraftProtection_MSG_Format + " (*.msg)|*.msg",
   5.441                                  FilterIndex = 1
   5.442                              };
   5.443 -                            if (string.IsNullOrEmpty(this.wrappedMailItem.Subject) == false)
   5.444 -                                sfd.FileName = this.wrappedMailItem.Subject;
   5.445 +                            if (string.IsNullOrEmpty(this.MailItem.Subject) == false)
   5.446 +                                sfd.FileName = this.MailItem.Subject;
   5.447                              var r = sfd.ShowDialog();
   5.448                              if (r == System.Windows.Forms.DialogResult.OK)
   5.449                              {
   5.450                                  draftFileName = sfd.FileName;
   5.451 -                                this.wrappedMailItem.SaveAs(draftFileName, Outlook.OlSaveAsType.olMSGUnicode);
   5.452 +                                this.MailItem.SaveAs(draftFileName, Outlook.OlSaveAsType.olMSGUnicode);
   5.453                              }
   5.454                          }
   5.455  
   5.456 @@ -712,7 +958,7 @@
   5.457                      }
   5.458                      else
   5.459                      {
   5.460 -                        this.wrappedMailItem.SaveAs(draftFileName, Outlook.OlSaveAsType.olMSGUnicode);
   5.461 +                        this.MailItem.SaveAs(draftFileName, Outlook.OlSaveAsType.olMSGUnicode);
   5.462                          cancel = true;
   5.463                      }
   5.464                  }
   5.465 @@ -723,192 +969,6 @@
   5.466  
   5.467          /**************************************************************
   5.468           * 
   5.469 -         * Property Accessors
   5.470 -         * 
   5.471 -         *************************************************************/
   5.472 -
   5.473 -        /// <summary>
   5.474 -        /// Gets the type of the account where the mail item is located in.
   5.475 -        /// </summary>
   5.476 -        public Outlook.OlAccountType AccountType
   5.477 -        {
   5.478 -            get => this._AccountType ?? ((Outlook.OlAccountType)(this._AccountType = this.wrappedMailItem.GetAccountType()));
   5.479 -        }
   5.480 -
   5.481 -        /// <summary>
   5.482 -        /// Returns a constant that belongs to the OlDownloadState enumeration indicating the download state of the item.
   5.483 -        /// This forwards the call to the internal mail item.
   5.484 -        /// See: https://msdn.microsoft.com/en-us/library/office/ff866978.aspx
   5.485 -        /// </summary>
   5.486 -        public Outlook.OlDownloadState DownloadState
   5.487 -        {
   5.488 -            get => this._DownloadState ?? ((Outlook.OlDownloadState)(this._DownloadState = this.wrappedMailItem.DownloadState));
   5.489 -        }
   5.490 -
   5.491 -        /// <summary>
   5.492 -        /// Gets whether the mail item is currently being processed.
   5.493 -        /// </summary>
   5.494 -        public bool IsBeingProcessed
   5.495 -        {
   5.496 -            get => this.processor.IsBusy;
   5.497 -        }
   5.498 -
   5.499 -        /// <summary>
   5.500 -        /// Gets whether the mail item is marked as a draft (unsent).
   5.501 -        /// </summary>
   5.502 -        public bool IsDraft
   5.503 -        {
   5.504 -            get => this._IsDraft ?? ((bool)(this._IsDraft = this.wrappedMailItem.GetIsDraft()));
   5.505 -        }
   5.506 -
   5.507 -        /// <summary>
   5.508 -        /// Gets whether the mail item is forcefully protected.
   5.509 -        /// </summary>
   5.510 -        public bool IsForcefullyProtected
   5.511 -        {
   5.512 -            get => this._IsForcefullyProtected ?? ((bool)(this._IsForcefullyProtected = this.wrappedMailItem.GetIsForcefullyProtected()));
   5.513 -        }
   5.514 -
   5.515 -        /// <summary>
   5.516 -        /// Gets whether the mail item is marked as incoming (received mail, not sent).
   5.517 -        /// </summary>
   5.518 -        public bool IsIncoming
   5.519 -        {
   5.520 -            get => this._IsIncoming ?? ((bool)(this._IsIncoming = this.wrappedMailItem.GetIsIncoming()));
   5.521 -        }
   5.522 -
   5.523 -        /// <summary>
   5.524 -        /// Gets whether the mail item is in a secure store (untrusted server).
   5.525 -        /// </summary>
   5.526 -        public bool IsInSecureStore
   5.527 -        {
   5.528 -            get => this._IsInSecureStore ?? ((bool)(this._IsInSecureStore = this.wrappedMailItem.GetIsInSecureStore()));
   5.529 -        }
   5.530 -        
   5.531 -        /// <summary>
   5.532 -        /// Gets whether the mail item is in the Sent folder.
   5.533 -        /// </summary>
   5.534 -        public bool IsInSentFolder
   5.535 -        {
   5.536 -            get => this._IsInSentFolder ?? ((bool)(this._IsInSentFolder = this.wrappedMailItem.GetIsInSentFolder()));
   5.537 -        }
   5.538 -
   5.539 -        /// <summary>
   5.540 -        /// Gets whether the mail item is a mirror item.
   5.541 -        /// </summary>
   5.542 -        public bool IsMirror
   5.543 -        {
   5.544 -            get => this._IsMirror ?? ((bool)(this._IsMirror = this.wrappedMailItem.GetIsMirror()));
   5.545 -        }
   5.546 -
   5.547 -        /// <summary>
   5.548 -        /// Gets whether the wrapped mail item was marked as being originally encrypted.
   5.549 -        /// This should have been set by calling SetIsOriginallyEncryptedByCache() during the 
   5.550 -        /// EncryptedConversationCacheUpdated event.
   5.551 -        /// False is returned if the property is either false or null (doesn't exist).
   5.552 -        /// </summary>
   5.553 -        public bool IsOriginallyEncrypted
   5.554 -        {
   5.555 -            get => this._IsOriginallyEncrypted ?? ((bool)(this._IsOriginallyEncrypted = ((this.wrappedMailItem.GetUserProperty(MailItemWrapper.USER_PROPERTY_KEY_IS_ORIGINALLY_ENCRYPTED) as bool?) == true)));
   5.556 -        }
   5.557 -
   5.558 -        /// <summary>
   5.559 -        /// Determines whether the mail item has been processed through pEp. This is done by checking if a pEp protocol
   5.560 -        /// version is available for the item.
   5.561 -        /// </summary>
   5.562 -        protected bool IsPEPMessage
   5.563 -        {
   5.564 -            get => (MailItemExtensions.GetPEPProperty(wrappedMailItem, MailItemExtensions.PEPProperty.PEPProtocolVersion, out object version) && (version != null));
   5.565 -        }
   5.566 -
   5.567 -        /// <summary>
   5.568 -        /// Gets whether the mail item is secure (encrypted).
   5.569 -        /// </summary>
   5.570 -        public bool IsSecure
   5.571 -        {
   5.572 -            get => this._IsSecure ?? ((bool)(this._IsSecure = this.wrappedMailItem.GetIsSecure()));
   5.573 -        }
   5.574 -
   5.575 -        /// <summary>
   5.576 -        /// Gets whether the mail item is securely stored.
   5.577 -        /// </summary>
   5.578 -        public bool IsSecurelyStored
   5.579 -        {
   5.580 -            get => (((this.IsSecure || this.IsForcefullyProtected) && this.IsInSecureStore) || (this.NeverUnsecure && (this.IsMirror == false)));
   5.581 -        }
   5.582 -
   5.583 -        /// <summary>
   5.584 -        /// Gets whether the mail item has been submitted.
   5.585 -        /// </summary>
   5.586 -        public bool IsSubmitted
   5.587 -        {
   5.588 -            get => this._IsSubmitted ?? ((bool)(this._IsSubmitted = this.wrappedMailItem.GetIsSubmitted()));
   5.589 -        }
   5.590 -
   5.591 -        /// <summary>
   5.592 -        /// Gets the status after the last message processing.
   5.593 -        /// This should be used to determine if a failure occured.
   5.594 -        /// Success is the default if no processing has been completed.
   5.595 -        /// </summary>
   5.596 -        public Globals.ReturnStatus LastProcessedStatus
   5.597 -        {
   5.598 -            get { return (this._LastProcessedStatus); }
   5.599 -        }
   5.600 -
   5.601 -        /// <summary>
   5.602 -        /// Gets the personal identity using the internal mail item.
   5.603 -        /// Warning: This can return null.
   5.604 -        /// </summary>
   5.605 -        public PEPIdentity Myself
   5.606 -        {
   5.607 -            get
   5.608 -            {
   5.609 -                lock (mutexMailItem)
   5.610 -                {
   5.611 -                    if (PEPIdentity.GetOwnIdentity(this.wrappedMailItem, out PEPIdentity ident) != Globals.ReturnStatus.Success)
   5.612 -                    {
   5.613 -                        ident = null;
   5.614 -                    }
   5.615 -
   5.616 -                    return ident;
   5.617 -                }
   5.618 -            }
   5.619 -        }
   5.620 -
   5.621 -        /// <summary>
   5.622 -        /// Gets or sets whether this cryptable mail item is marked to never be unsecure (always encrypted).
   5.623 -        /// This information is stored as a MAPI property of the wrapped mail item.
   5.624 -        /// Warning: This will call .Save() on the MailItem.
   5.625 -        /// </summary>
   5.626 -        public bool NeverUnsecure
   5.627 -        {
   5.628 -            get
   5.629 -            {
   5.630 -                object propValue;
   5.631 -
   5.632 -                lock (mutexMailItem)
   5.633 -                {
   5.634 -                    // Return status can be ignored here, using the auto default value is good enough
   5.635 -                    this.wrappedMailItem.GetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, out propValue);
   5.636 -                }
   5.637 -
   5.638 -                return ((bool)propValue);
   5.639 -            }
   5.640 -            set
   5.641 -            {
   5.642 -                lock (mutexMailItem)
   5.643 -                {
   5.644 -                    // Return status can be ignored
   5.645 -                    this.wrappedMailItem.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, value);
   5.646 -                    this.wrappedMailItem.Save();
   5.647 -                }
   5.648 -
   5.649 -                this.OnPropertyChanged();
   5.650 -            }
   5.651 -        }
   5.652 -
   5.653 -        /**************************************************************
   5.654 -         * 
   5.655           * Decryption
   5.656           * 
   5.657           *************************************************************/
   5.658 @@ -945,14 +1005,14 @@
   5.659                  // Log the download status which can influence later processing
   5.660                  try
   5.661                  {
   5.662 -                    Log.Verbose("ProcessAndGetRating: DownloadState=" + this.wrappedMailItem.DownloadState.ToString());
   5.663 +                    Log.Verbose("ProcessAndGetRating: DownloadState=" + this.MailItem.DownloadState.ToString());
   5.664                  }
   5.665                  catch { }
   5.666  
   5.667                  // Do not processes deleted messages on IMAP
   5.668                  if ((fullCalculation) &&
   5.669                      (this.AccountType == Outlook.OlAccountType.olImap) &&
   5.670 -                    (MapiHelper.GetProperty(this.wrappedMailItem, MapiProperty.PidLidImapMarkedForDeletion, "0").ToString() == "1"))
   5.671 +                    (MapiHelper.GetProperty(this.MailItem, MapiProperty.PidLidImapMarkedForDeletion, "0").ToString() == "1"))
   5.672                  {
   5.673                      Log.Verbose("ProcessAndGetRating: Deleted IMAP message detected, skipping processing");
   5.674                      result.Rating = pEpRating.pEpRatingUndefined;
   5.675 @@ -966,7 +1026,7 @@
   5.676                      // Get UI rating from mirror
   5.677                      try
   5.678                      {
   5.679 -                        if (PEPMessage.Create(this.wrappedMailItem, out mirror) == Globals.ReturnStatus.Success)
   5.680 +                        if (PEPMessage.Create(this.MailItem, out mirror) == Globals.ReturnStatus.Success)
   5.681                          {
   5.682                              result.Rating = AdapterExtensions.ReevaluateMessageRating(mirror);
   5.683                              this.Message = mirror;
   5.684 @@ -998,7 +1058,7 @@
   5.685                  {
   5.686                      Log.Verbose("ProcessAndGetRating: Draft detected, using outgoing rating.");
   5.687  
   5.688 -                    result.Rating = this.wrappedMailItem.GetOutgoingRating(true);
   5.689 +                    result.Rating = this.MailItem.GetOutgoingRating(true);
   5.690                      fullCalculation = false;
   5.691                  }
   5.692  
   5.693 @@ -1014,7 +1074,7 @@
   5.694                          messageId = this.MessageId;
   5.695                      }
   5.696  
   5.697 -                    mirrorMailItem = this.wrappedMailItem.GetMirror(messageId);
   5.698 +                    mirrorMailItem = this.MailItem.GetMirror(messageId);
   5.699                      if ((mirrorMailItem != null) &&
   5.700                          (PEPMessage.Create(mirrorMailItem, out mirror) == Globals.ReturnStatus.Success))
   5.701                      {
   5.702 @@ -1042,7 +1102,7 @@
   5.703                      // Check if message has autoconsume header and set pEp internal category if necessary
   5.704                      try
   5.705                      {
   5.706 -                        if (this.wrappedMailItem.GetIsAutoConsume())
   5.707 +                        if (this.MailItem.GetIsAutoConsume())
   5.708                          {
   5.709                              isPEPInternal = true;
   5.710  
   5.711 @@ -1056,8 +1116,8 @@
   5.712                                  Globals.ThisAddIn.CreatePEPCategories();
   5.713  
   5.714                                  // Only one category is needed to hide the MailItem -- delete any others
   5.715 -                                this.wrappedMailItem.Categories = Globals.PEP_INTERNAL_CATEGORY_NAME;
   5.716 -                                this.wrappedMailItem.UnRead = false;
   5.717 +                                this.MailItem.Categories = Globals.PEP_INTERNAL_CATEGORY_NAME;
   5.718 +                                this.MailItem.UnRead = false;
   5.719                                  preProcessingPropertySet = true;
   5.720                              }
   5.721                          }
   5.722 @@ -1079,7 +1139,7 @@
   5.723                      {
   5.724                          try
   5.725                          {
   5.726 -                            MapiHelper.SetProperty(this.wrappedMailItem, MapiProperty.PidTagMessageClass, MapiPropertyValue.PidTagMessageClassSecurePEP);
   5.727 +                            MapiHelper.SetProperty(this.MailItem, MapiProperty.PidTagMessageClass, MapiPropertyValue.PidTagMessageClassSecurePEP);
   5.728                              preProcessingPropertySet = true;
   5.729                          }
   5.730                          catch (Exception ex)
   5.731 @@ -1093,7 +1153,7 @@
   5.732                      {
   5.733                          try
   5.734                          {
   5.735 -                            this.wrappedMailItem.Save();
   5.736 +                            this.MailItem.Save();
   5.737                          }
   5.738                          catch (Exception ex)
   5.739                          {
   5.740 @@ -1102,7 +1162,7 @@
   5.741                      }
   5.742  
   5.743                      // Process mail item
   5.744 -                    sts1 = PEPMessage.Create(this.wrappedMailItem, out message);
   5.745 +                    sts1 = PEPMessage.Create(this.MailItem, out message);
   5.746  
   5.747                      // If Key Import Wizard is open, add mail to list to process later
   5.748                      if ((KeySyncWizard.Wizard?.IsVisible == true) &&
   5.749 @@ -1151,10 +1211,10 @@
   5.750                              // Delete the mail item from Outlook
   5.751                              try
   5.752                              {
   5.753 -                                Log.SensitiveData("ProcessAndGetRating: Processed msg with subject " + this.wrappedMailItem.Subject + " received on " + this.wrappedMailItem.ReceivedTime.ToString());
   5.754 +                                Log.SensitiveData("ProcessAndGetRating: Processed msg with subject " + this.MailItem.Subject + " received on " + this.MailItem.ReceivedTime.ToString());
   5.755                              }
   5.756                              catch { }
   5.757 -                            this.wrappedMailItem.PermanentlyDelete();
   5.758 +                            this.MailItem.PermanentlyDelete();
   5.759                              this.disposeAfterProcessing = true;
   5.760  
   5.761                              Log.Verbose("ProcessAndGetRating: Processed message consumed");
   5.762 @@ -1163,7 +1223,7 @@
   5.763                          {
   5.764                              try
   5.765                              {
   5.766 -                                Log.SensitiveData("ProcessAndGetRating: Processed msg with subject " + this.wrappedMailItem.Subject + " received on " + this.wrappedMailItem.ReceivedTime.ToString());
   5.767 +                                Log.SensitiveData("ProcessAndGetRating: Processed msg with subject " + this.MailItem.Subject + " received on " + this.MailItem.ReceivedTime.ToString());
   5.768                              }
   5.769                              catch { }
   5.770                              Log.Verbose("ProcessAndGetRating: Processed message ignored.");
   5.771 @@ -1195,7 +1255,7 @@
   5.772                                              messageId = this.MessageId;
   5.773                                          }
   5.774  
   5.775 -                                        mirrorMailItem = this.wrappedMailItem.CreateMirrorOMI(messageId);
   5.776 +                                        mirrorMailItem = this.MailItem.CreateMirrorOMI(messageId);
   5.777                                      }
   5.778  
   5.779                                      // Apply message to mirror and save
   5.780 @@ -1213,7 +1273,7 @@
   5.781                                      // If needed, set SmartNoAttach MAPI property to hide the attachments icon
   5.782                                      if ((this.AccountType != Outlook.OlAccountType.olImap) &&
   5.783                                          (processedMessage.Attachments?.Count == 0) &&
   5.784 -                                        (this.wrappedMailItem.Attachments?.Count > 0))
   5.785 +                                        (this.MailItem.Attachments?.Count > 0))
   5.786                                      {
   5.787                                          // Note: The documented MAPI property PidLidSmartNoAttach (0x8514000B) doesn't work for some reason
   5.788                                          result.PropertiesToSet.Add(MapiProperty.PidTagSmartNoAttach2, true);
   5.789 @@ -1224,7 +1284,7 @@
   5.790                                       */
   5.791                                      if (decryptionFlags.HasFlag(pEpDecryptFlags.pEpDecryptFlagSrcModified))
   5.792                                      {
   5.793 -                                        if (message.ApplyTo(this.wrappedMailItem, false, false, true, false) == Globals.ReturnStatus.Success)
   5.794 +                                        if (message.ApplyTo(this.MailItem, false, false, true, false) == Globals.ReturnStatus.Success)
   5.795                                          {
   5.796                                              saveInternalMailItem = true;
   5.797                                          }
   5.798 @@ -1237,7 +1297,7 @@
   5.799                                  else
   5.800                                  {
   5.801                                      // Trusted server: apply message and save
   5.802 -                                    processedMessage.ApplyTo(this.wrappedMailItem, true, false);
   5.803 +                                    processedMessage.ApplyTo(this.MailItem, true, false);
   5.804                                      saveInternalMailItem = true;
   5.805                                  }
   5.806                              }
   5.807 @@ -1264,15 +1324,15 @@
   5.808  
   5.809                      // Remove the pEp Processing category if needed
   5.810                      if (Globals.ThisAddIn.Settings.IsUXImprovementEnabled &&
   5.811 -                        this.wrappedMailItem.RemovePEPProcessingCategory())
   5.812 +                        this.MailItem.RemovePEPProcessingCategory())
   5.813                      {
   5.814                          if (message?.Direction != null)
   5.815                          {
   5.816 -                            this.wrappedMailItem.UnRead = (message.Direction == pEpMsgDirection.pEpDirIncoming);
   5.817 +                            this.MailItem.UnRead = (message.Direction == pEpMsgDirection.pEpDirIncoming);
   5.818                          }
   5.819                          else
   5.820                          {
   5.821 -                            this.wrappedMailItem.UnRead = (this.IsInSentFolder == false);
   5.822 +                            this.MailItem.UnRead = (this.IsInSentFolder == false);
   5.823                          }
   5.824  
   5.825                          saveInternalMailItem = true;
   5.826 @@ -1284,7 +1344,7 @@
   5.827                          bool saveError = false;
   5.828                          try
   5.829                          {
   5.830 -                            this.wrappedMailItem.Save();
   5.831 +                            this.MailItem.Save();
   5.832                          }
   5.833                          catch (Exception ex)
   5.834                          {
   5.835 @@ -1297,12 +1357,12 @@
   5.836                          {
   5.837                              try
   5.838                              {
   5.839 -                                int sts = Mapi.Save(this.wrappedMailItem, Mapi.SaveOption.KEEP_OPEN_READWRITE);
   5.840 +                                int sts = Mapi.Save(this.MailItem, Mapi.SaveOption.KEEP_OPEN_READWRITE);
   5.841                                  Log.Verbose("ProcessAndGetRating: Saving using backup method. Return status is " + sts.ToString("X"));
   5.842  
   5.843                                  if (sts != (uint)Mapi.HResult.S_OK)
   5.844                                  {
   5.845 -                                    sts = Mapi.Save(this.wrappedMailItem, Mapi.SaveOption.FORCE_SAVE);
   5.846 +                                    sts = Mapi.Save(this.MailItem, Mapi.SaveOption.FORCE_SAVE);
   5.847                                      Log.Verbose("ProcessAndGetRating: Saving with FORCE_SAVE flag returned " + sts.ToString("X"));
   5.848                                  }
   5.849                              }
   5.850 @@ -1406,8 +1466,8 @@
   5.851                           */
   5.852                          try
   5.853                          {
   5.854 -                            entryId = this.wrappedMailItem.EntryID;
   5.855 -                            isAutoConsume = this.wrappedMailItem.GetIsAutoConsume();
   5.856 +                            entryId = this.MailItem.EntryID;
   5.857 +                            isAutoConsume = this.MailItem.GetIsAutoConsume();
   5.858                          }
   5.859                          catch
   5.860                          {
   5.861 @@ -1552,9 +1612,6 @@
   5.862                  if (this.disposeAfterProcessing)
   5.863                  {
   5.864                      this.Dispose(false);
   5.865 -
   5.866 -                    // Must be called last!
   5.867 -                    this.DisposeInternalMailItem();
   5.868                  }
   5.869  
   5.870                  return;
   5.871 @@ -1574,20 +1631,14 @@
   5.872                  {
   5.873                      KeySyncWizard.Wizard.Dispatcher.Invoke(new Action(() =>
   5.874                      {
   5.875 -                        this.wrappedMailItem?.SetMAPIProperties(result.PropertiesToSet);
   5.876 +                        this.MailItem?.SetMAPIProperties(result.PropertiesToSet);
   5.877  
   5.878                          this.Dispose(false);
   5.879 -
   5.880 -                        // Must be called last!
   5.881 -                        this.DisposeInternalMailItem();
   5.882                      }));
   5.883                  }
   5.884                  else
   5.885                  {
   5.886                      this.Dispose(false);
   5.887 -
   5.888 -                    // Must be called last!
   5.889 -                    this.DisposeInternalMailItem();
   5.890                  }
   5.891              }
   5.892              else
   5.893 @@ -1652,7 +1703,7 @@
   5.894                      lock (mutexMailItem)
   5.895                      {
   5.896                          string id = e.Argument as string;
   5.897 -                        omi = this.wrappedMailItem.GetMirror(id);
   5.898 +                        omi = this.MailItem.GetMirror(id);
   5.899  
   5.900                          if (omi != null)
   5.901                          {
   5.902 @@ -1702,21 +1753,6 @@
   5.903          }
   5.904  
   5.905          /// <summary>
   5.906 -        /// Releases the internal Outlook.MailItem wrapped by this MailItemWrapper.
   5.907 -        /// In general, this shouldn't be used as the MailItems are managed externally.
   5.908 -        /// However, there are some cases where the MailItem needs to be disposed and the MailItemWrapper is the only
   5.909 -        /// object that maintains the reference.
   5.910 -        /// </summary>
   5.911 -        public void DisposeInternalMailItem()
   5.912 -        {
   5.913 -            if (this.wrappedMailItem != null)
   5.914 -            {
   5.915 -                // Marshal.ReleaseComObject(this.internalMailItem);
   5.916 -                this.wrappedMailItem = null;
   5.917 -            }
   5.918 -        }
   5.919 -
   5.920 -        /// <summary>
   5.921          /// Releases resources and disconnects internal events.
   5.922          /// This exists to make code-analysis not throw old warnings.
   5.923          /// </summary>
   5.924 @@ -1748,7 +1784,26 @@
   5.925                  }
   5.926              }
   5.927  
   5.928 -            this.wrappedMailItem = null;
   5.929 +            this.MailItem = null;
   5.930 +        }
   5.931 +
   5.932 +        /// <summary>
   5.933 +        /// Gets the processsing state of the wrapped mail item.
   5.934 +        /// </summary>
   5.935 +        /// <returns>The processing state. Can be null.</returns>
   5.936 +        public MailItemExtensions.ProcessingState? GetProcessingState()
   5.937 +        {
   5.938 +            return this.MailItem?.GetProcessingState();
   5.939 +        }
   5.940 +
   5.941 +        /// <summary>
   5.942 +        /// Gets the user propery value for the wrapped mail item.
   5.943 +        /// </summary>
   5.944 +        /// <param name="propertyName">The property to get its value for.</param>
   5.945 +        /// <returns>The property value. Can be null.</returns>
   5.946 +        public object GetUserProperty(string propertyName)
   5.947 +        {
   5.948 +            return this.MailItem?.GetUserProperty(propertyName);
   5.949          }
   5.950  
   5.951          /// <summary>
   5.952 @@ -1766,11 +1821,11 @@
   5.953                      /* Calling Save() for drafts might cause a crash, as this then interferes
   5.954                       * with the protected storing of drafts for untrusted servers.
   5.955                       */
   5.956 -                    if (this.wrappedMailItem.GetIsDraft() == false)
   5.957 +                    if (this.MailItem.GetIsDraft() == false)
   5.958                      {
   5.959 -                        this.wrappedMailItem.Save();
   5.960 +                        this.MailItem.Save();
   5.961                      }
   5.962 -                    bool result = this.wrappedMailItem.Recipients.ResolveAll();
   5.963 +                    bool result = this.MailItem.Recipients.ResolveAll();
   5.964                  }
   5.965                  catch (Exception ex)
   5.966                  {
   5.967 @@ -1800,6 +1855,29 @@
   5.968          }
   5.969  
   5.970          /// <summary>
   5.971 +        /// Sets a value to either a UserProperty or MAPI property in the wrapped Outlook mail item.
   5.972 +        /// NOTE: The MailItem will NOT be automatically saved, this MUST be done externally for the property to be persistent.
   5.973 +        /// Warning: The propertyName for a UserProperty or MAPI property must never be the same.
   5.974 +        /// </summary>
   5.975 +        /// <param name="property">The pEp defined property to set.</param>
   5.976 +        /// <param name="value">The value of the property to set (null is not supported).</param>
   5.977 +        /// <returns>True if successful, otherwise false.</returns>
   5.978 +        public bool SetPEPProperty(MailItemExtensions.PEPProperty property, object value)
   5.979 +        {
   5.980 +            return (this.MailItem?.SetPEPProperty(property, value) == true);
   5.981 +        }
   5.982 +
   5.983 +        /// <summary>
   5.984 +        /// Sets the processing state of the wrapped mail item.
   5.985 +        /// </summary>
   5.986 +        /// <param name="processingState">The processing state to set.</param>
   5.987 +        /// <returns>True if successfully set, otherwise false.</returns>
   5.988 +        public bool SetProcessingState(MailItemExtensions.ProcessingState processingState)
   5.989 +        {
   5.990 +            return (this.MailItem?.SetProcessingState(processingState) == true);
   5.991 +        }
   5.992 +
   5.993 +        /// <summary>
   5.994          /// Displays the unencrypted mirror mail item (if it exists) associated with the wrapped mail item.
   5.995          /// </summary>
   5.996          /// <returns>True if successful, otherwise false.</returns>
   5.997 @@ -1813,7 +1891,7 @@
   5.998              }
   5.999              else
  5.1000              {
  5.1001 -                Outlook.MailItem omi = this.wrappedMailItem.GetMirror();
  5.1002 +                Outlook.MailItem omi = this.MailItem.GetMirror();
  5.1003  
  5.1004                  if (omi == null)
  5.1005                  {
  5.1006 @@ -1850,7 +1928,7 @@
  5.1007          /// <param name="connect">Whether to connect or disconnect events.</param>
  5.1008          private void ConnectInternalMailItemEvents(bool connect)
  5.1009          {
  5.1010 -            if (this.wrappedMailItem != null)
  5.1011 +            if (this.MailItem != null)
  5.1012              {
  5.1013                  /* OUT-285: This should probably never fail, but during dis/connection
  5.1014                   * of an account, this method raised an InvalidCastException, so we will 
  5.1015 @@ -1861,27 +1939,27 @@
  5.1016                      // Connect or disconnect events
  5.1017                      if (connect)
  5.1018                      {
  5.1019 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).BeforeAttachmentPreview += MailItem_BeforeAttachmentPreview;
  5.1020 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Close += MailItem_Close;
  5.1021 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Forward += MailItem_Forward;
  5.1022 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Open += MailItem_Open;
  5.1023 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).PropertyChange += MailItem_PropertyChange;
  5.1024 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Reply += MailItem_Reply;
  5.1025 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).ReplyAll += MailItem_ReplyAll;
  5.1026 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Send += MailItem_Send;
  5.1027 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Write += MailItem_Write;
  5.1028 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).BeforeAttachmentPreview += MailItem_BeforeAttachmentPreview;
  5.1029 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Close += MailItem_Close;
  5.1030 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Forward += MailItem_Forward;
  5.1031 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Open += MailItem_Open;
  5.1032 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).PropertyChange += MailItem_PropertyChange;
  5.1033 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Reply += MailItem_Reply;
  5.1034 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).ReplyAll += MailItem_ReplyAll;
  5.1035 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Send += MailItem_Send;
  5.1036 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Write += MailItem_Write;
  5.1037                      }
  5.1038                      else
  5.1039                      {
  5.1040 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).BeforeAttachmentPreview -= MailItem_BeforeAttachmentPreview;
  5.1041 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Close -= MailItem_Close;
  5.1042 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Forward -= MailItem_Forward;
  5.1043 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Open -= MailItem_Open;
  5.1044 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).PropertyChange -= MailItem_PropertyChange;
  5.1045 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Reply -= MailItem_Reply;
  5.1046 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).ReplyAll -= MailItem_ReplyAll;
  5.1047 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Send -= MailItem_Send;
  5.1048 -                        ((Outlook.ItemEvents_10_Event)this.wrappedMailItem).Write -= MailItem_Write;
  5.1049 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).BeforeAttachmentPreview -= MailItem_BeforeAttachmentPreview;
  5.1050 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Close -= MailItem_Close;
  5.1051 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Forward -= MailItem_Forward;
  5.1052 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Open -= MailItem_Open;
  5.1053 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).PropertyChange -= MailItem_PropertyChange;
  5.1054 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Reply -= MailItem_Reply;
  5.1055 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).ReplyAll -= MailItem_ReplyAll;
  5.1056 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Send -= MailItem_Send;
  5.1057 +                        ((Outlook.ItemEvents_10_Event)this.MailItem).Write -= MailItem_Write;
  5.1058                      }
  5.1059                  }
  5.1060                  catch (Exception ex)
     6.1 --- a/Wrappers/WindowBaseWrapper.cs	Thu Mar 14 16:15:35 2019 +0100
     6.2 +++ b/Wrappers/WindowBaseWrapper.cs	Fri Mar 15 14:59:23 2019 +0100
     6.3 @@ -22,7 +22,6 @@
     6.4          private bool                            refreshOngoing              = false;
     6.5          private bool                            repeatProcessing            = false;
     6.6          private int                             repeatCounter               = 0;
     6.7 -        private MailItemWrapper                 wrappedMailItem             = null;
     6.8  
     6.9          public enum WindowType
    6.10          {
    6.11 @@ -34,11 +33,6 @@
    6.12          #region Properties
    6.13  
    6.14          /// <summary>
    6.15 -        /// The mail item that is connected to this Inspector/Explorer window.
    6.16 -        /// </summary>
    6.17 -        public Outlook.MailItem CurrentMailItem { get; set; } = null;
    6.18 -
    6.19 -        /// <summary>
    6.20          /// Gets or sets whether to disable the Force Protection option.
    6.21          /// </summary>
    6.22          public bool DisableForceProtection { get; set; } = false;
    6.23 @@ -56,7 +50,7 @@
    6.24          /// <summary>
    6.25          /// Gets whether the wrapped mail item is a draft.
    6.26          /// </summary>
    6.27 -        public bool IsDraft { get => (this.wrappedMailItem?.IsDraft == true); }
    6.28 +        public bool IsDraft { get => (this.WrappedMailItem?.IsDraft == true); }
    6.29  
    6.30          /// <summary>
    6.31          /// Gets or sets whether to always store this message as if on an untrusted server.
    6.32 @@ -89,14 +83,19 @@
    6.33          public System.Windows.Forms.Timer TimerRefresh { get; set; } = new System.Windows.Forms.Timer();
    6.34  
    6.35          /// <summary>
    6.36 +        /// Gets the window type of this window. To be overwritten in child class.
    6.37 +        /// </summary>
    6.38 +        public abstract WindowType Type { get; }
    6.39 +
    6.40 +        /// <summary>
    6.41          /// Gets the associated Explorer/Inspector window. To be overwritten in child class.
    6.42          /// </summary>
    6.43          public abstract dynamic Window { get; }
    6.44  
    6.45          /// <summary>
    6.46 -        /// Gets the window type of this window. To be overwritten in child class.
    6.47 +        /// The mail item that is the current context of this window.
    6.48          /// </summary>
    6.49 -        public abstract WindowType Type { get; }
    6.50 +        internal abstract MailItemWrapper WrappedMailItem { get; }
    6.51  
    6.52          #endregion
    6.53  
    6.54 @@ -135,29 +134,26 @@
    6.55              if (disposing)
    6.56              {
    6.57                  // Disconnect cryptable mail item
    6.58 -                if (this.wrappedMailItem != null)
    6.59 +                if (this.WrappedMailItem != null)
    6.60                  {
    6.61                      try
    6.62                      {
    6.63 -                        this.wrappedMailItem.PropertyChanged -= MailItem_PropertyChanged;
    6.64 -                        this.wrappedMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
    6.65 -                        this.wrappedMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
    6.66 -                        this.wrappedMailItem.Open -= MailItem_Open;
    6.67 -                        this.wrappedMailItem.Send -= MailItem_Send;
    6.68 -                        this.wrappedMailItem.OriginallyEncryptedStatusUpdated -= MailItemWrapper_OriginallyEncryptedStatusUpdated;
    6.69 +                        this.WrappedMailItem.PropertyChanged -= MailItem_PropertyChanged;
    6.70 +                        this.WrappedMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
    6.71 +                        this.WrappedMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
    6.72 +                        this.WrappedMailItem.Open -= MailItem_Open;
    6.73 +                        this.WrappedMailItem.Send -= MailItem_Send;
    6.74 +                        this.WrappedMailItem.Write -= MailItem_Write;
    6.75 +                        this.WrappedMailItem.OriginallyEncryptedStatusUpdated -= MailItemWrapper_OriginallyEncryptedStatusUpdated;
    6.76                      }
    6.77                      catch { }
    6.78  
    6.79 -                    this.wrappedMailItem.Dispose();
    6.80 -                    this.wrappedMailItem = null;
    6.81 +                    this.WrappedMailItem.Dispose();
    6.82                  }
    6.83  
    6.84                  // Disconnect other
    6.85                  this.SetIsEnabled(false);
    6.86  
    6.87 -                // Set Outlook objects to null
    6.88 -                this.CurrentMailItem = null;
    6.89 -
    6.90                  // Dispose of timer
    6.91                  if (this.TimerRefresh != null)
    6.92                  {
    6.93 @@ -197,14 +193,14 @@
    6.94                  PEPMessage message = null;
    6.95  
    6.96                  // The own identity to build the dialog with
    6.97 -                PEPIdentity myself = this.wrappedMailItem?.Myself;
    6.98 +                PEPIdentity myself = this.WrappedMailItem?.Myself;
    6.99  
   6.100                  // If message is a draft, create it directly from the Outlook mail item
   6.101                  if (this.IsDraft)
   6.102                  {
   6.103                      Log.Verbose("BuildAndShowManager: Creating PEPMessage from draft.");
   6.104  
   6.105 -                    if (PEPMessage.Create(this.CurrentMailItem, out message, true) == Globals.ReturnStatus.Success)
   6.106 +                    if (PEPMessage.Create(this.WrappedMailItem.MailItem, out message, true) == Globals.ReturnStatus.Success)
   6.107                      {
   6.108                          // Calculate rating
   6.109                          message.Rating = message.GetOutgoingRating();
   6.110 @@ -228,8 +224,8 @@
   6.111                  else
   6.112                  {
   6.113                      // Else, use either the cryptable mail item's associated mirror or message
   6.114 -                    message = this.wrappedMailItem?.Mirror ?? this.wrappedMailItem?.Message;
   6.115 -                    Log.Verbose(string.Format("BuildAndShowManager: Message {0} retrieved from wrappedMailItem", ((message != null) ? "successfully" : "could not be")));
   6.116 +                    message = this.WrappedMailItem?.Mirror ?? this.WrappedMailItem?.Message;
   6.117 +                    Log.Verbose(string.Format("BuildAndShowManager: Message {0} retrieved from MailItem", ((message != null) ? "successfully" : "could not be")));
   6.118  
   6.119                      // As fallback, if we don't have a message yet, create it
   6.120                      if (message == null)
   6.121 @@ -241,9 +237,9 @@
   6.122                          try
   6.123                          {
   6.124                              // For securely stored mails, try to look up mirror
   6.125 -                            if (this.CurrentMailItem?.GetIsSecurelyStored() == true)
   6.126 +                            if (this.WrappedMailItem?.IsSecurelyStored == true)
   6.127                              {
   6.128 -                                mirror = this.CurrentMailItem?.GetMirror();
   6.129 +                                mirror = this.WrappedMailItem?.MailItem?.GetMirror();
   6.130  
   6.131                                  if (mirror != null)
   6.132                                  {
   6.133 @@ -264,7 +260,7 @@
   6.134                                  {
   6.135                                      // If no mirror is found, decrypt message and use decrypted one (do not 
   6.136                                      // decrypt forcefully protected messages).
   6.137 -                                    if ((PEPMessage.Create(this.CurrentMailItem, out message) == Globals.ReturnStatus.Success) &&
   6.138 +                                    if ((PEPMessage.Create(this.WrappedMailItem?.MailItem, out message) == Globals.ReturnStatus.Success) &&
   6.139                                          (string.IsNullOrEmpty(message.ForceProtectionId)))
   6.140                                      {
   6.141                                          var msgProcessor = new MsgProcessor();
   6.142 @@ -289,7 +285,7 @@
   6.143                              {
   6.144                                  Log.Verbose("BuildAndShowManager: Creating PEPMessage.");
   6.145  
   6.146 -                                if (PEPMessage.Create(this.CurrentMailItem, out message) != Globals.ReturnStatus.Success)
   6.147 +                                if (PEPMessage.Create(this.WrappedMailItem?.MailItem, out message) != Globals.ReturnStatus.Success)
   6.148                                  {
   6.149                                      message = null;
   6.150                                      Log.Error("BuildAndShowManager: Error creating PEPMessage.");
   6.151 @@ -303,7 +299,7 @@
   6.152                              // Get myself identiy if we don't have it yet
   6.153                              if (myself == null)
   6.154                              {
   6.155 -                                myself = this.CurrentMailItem.GetMyselfIdentity();
   6.156 +                                myself = this.WrappedMailItem.Myself;
   6.157                              }
   6.158                          }
   6.159                          catch (Exception ex)
   6.160 @@ -339,8 +335,7 @@
   6.161          /// <summary>
   6.162          /// Initializes the watched explorer or inspector window.
   6.163          /// </summary>
   6.164 -        /// <param name="isInlineResponse">Whether this window is an inline response.</param>
   6.165 -        protected void InitializeWindow(bool isInlineResponse = false)
   6.166 +        protected void InitializeWindow()
   6.167          {
   6.168              bool enableFormRegion = false;
   6.169              bool cancelOpenEvent = false;
   6.170 @@ -362,9 +357,9 @@
   6.171                  // Try to get an original rating (in case of reply messages)
   6.172                  if (this.IsDraft)
   6.173                  {
   6.174 -                    if (this.CurrentMailItem?.Recipients?.Count > 0)
   6.175 +                    if (this.WrappedMailItem?.MailItem?.Recipients?.Count > 0)
   6.176                      {
   6.177 -                        string originalRatingString = this.CurrentMailItem.GetUserProperty(MailItemWrapper.USER_PROPERTY_KEY_ORIGINAL_RATING) as string;
   6.178 +                        string originalRatingString = this.WrappedMailItem?.GetUserProperty(MailItemWrapper.USER_PROPERTY_KEY_ORIGINAL_RATING) as string;
   6.179  
   6.180                          // If we have an original rating, parse it and set it.
   6.181                          if (string.IsNullOrEmpty(originalRatingString) == false)
   6.182 @@ -376,7 +371,7 @@
   6.183                  else
   6.184                  {
   6.185                      // Try to get item from cache
   6.186 -                    cacheItem = PEPCache.GetItemFromCache(this.CurrentMailItem.EntryID);
   6.187 +                    cacheItem = PEPCache.GetItemFromCache(this.WrappedMailItem.EntryId);
   6.188  
   6.189                      if (cacheItem != null)
   6.190                      {
   6.191 @@ -386,12 +381,12 @@
   6.192                      else
   6.193                      {
   6.194                          // Get rating from db
   6.195 -                        provisionalRating = PEPDatabase.GetRating(this.CurrentMailItem.EntryID);
   6.196 +                        provisionalRating = PEPDatabase.GetRating(this.WrappedMailItem.EntryId);
   6.197  
   6.198                          // If there is no rating in the db, use stored or default rating
   6.199                          if (provisionalRating == pEpRating.pEpRatingUndefined)
   6.200                          {
   6.201 -                            provisionalRating = this.CurrentMailItem.GetStoredRating() ?? pEpRating.pEpRatingUndefined;
   6.202 +                            provisionalRating = this.WrappedMailItem.StoredRating;
   6.203                          }
   6.204                      }
   6.205                  }
   6.206 @@ -404,7 +399,7 @@
   6.207                  }
   6.208  
   6.209                  // Do not process S/MIME messages
   6.210 -                if (this.CurrentMailItem.GetIsSMIMEEnabled())
   6.211 +                if (this.WrappedMailItem?.MailItem?.GetIsSMIMEEnabled() == true)
   6.212                  {
   6.213                      Log.Verbose("InitializeWindow: S/MIME message detected. Won't be processed.");
   6.214  
   6.215 @@ -413,11 +408,11 @@
   6.216  
   6.217                      // Set icon(s) if necessary     
   6.218                      if ((IsDraft == false) &&
   6.219 -                        (this.CurrentMailItem.SetEncryptionIcons()))
   6.220 +                        (this.WrappedMailItem?.MailItem?.SetEncryptionIcons() == true))
   6.221                      {
   6.222                          try
   6.223                          {
   6.224 -                            this.CurrentMailItem.Save();
   6.225 +                            this.WrappedMailItem?.MailItem.Save();
   6.226                          }
   6.227                          catch (Exception ex)
   6.228                          {
   6.229 @@ -433,17 +428,17 @@
   6.230                   * cache of attached mails and if item might be secure.
   6.231                   */
   6.232                  if ((PEPAttachment.AttachedMailsCache.Count > 0) &&
   6.233 -                    (this.CurrentMailItem.Attachments?.Count == 2))
   6.234 +                    (this.WrappedMailItem?.Attachments?.Count == 2))
   6.235                  {
   6.236                      try
   6.237                      {
   6.238                          // Check if mail item is an attached mail
   6.239 -                        if (this.CurrentMailItem.GetIsAttachedMail(out messageId))
   6.240 +                        if (this.WrappedMailItem?.MailItem?.GetIsAttachedMail(out messageId) == true)
   6.241                          {
   6.242                              Outlook.MailItem mirror = null;
   6.243  
   6.244                              // Try to get the mirror
   6.245 -                            mirror = this.CurrentMailItem.GetMirror(messageId);
   6.246 +                            mirror = this.WrappedMailItem?.MailItem?.GetMirror(messageId);
   6.247  
   6.248                              // If mirror was not found, decrypt and create mirror
   6.249                              if (mirror != null)
   6.250 @@ -452,14 +447,14 @@
   6.251                              }
   6.252                              else
   6.253                              {
   6.254 -                                if ((PEPMessage.Create(this.CurrentMailItem, out PEPMessage pEpMessage) == Globals.ReturnStatus.Success) &&
   6.255 +                                if ((PEPMessage.Create(this.WrappedMailItem?.MailItem, out PEPMessage pEpMessage) == Globals.ReturnStatus.Success) &&
   6.256                                      (pEpMessage.IsSecure))
   6.257                                  {
   6.258                                      MsgProcessor msgProcessor = new MsgProcessor();
   6.259                                      if (msgProcessor.Decrypt(pEpMessage, out PEPMessage decryptedMessage))
   6.260                                      {
   6.261                                          isSecureAttachedMail = true;
   6.262 -                                        mirror = this.CurrentMailItem.CreateMirrorOMI(messageId);
   6.263 +                                        mirror = this.WrappedMailItem?.MailItem?.CreateMirrorOMI(messageId);
   6.264                                          decryptedMessage.ApplyTo(mirror, true, false);
   6.265                                          mirror?.Save();
   6.266                                      }
   6.267 @@ -492,7 +487,7 @@
   6.268                  // Check for item from cache and show it if possible
   6.269                  if ((this.IsDraft == false) &&
   6.270                      (cacheItem?.Mirror != null) &&
   6.271 -                    (this.CurrentMailItem.GetIsInSecureStore()))
   6.272 +                    (this.WrappedMailItem.IsInSecureStore))
   6.273                  {
   6.274                      WindowFormRegionCollection formRegions = null;
   6.275                      try
   6.276 @@ -507,41 +502,41 @@
   6.277                      this.GetFormRegionPreviewUnencrypted()?.DisplayState?.SetMessage(cacheItem.Mirror);
   6.278                  }
   6.279  
   6.280 -                this.wrappedMailItem = new MailItemWrapper(this.CurrentMailItem, provisionalRating)
   6.281 -                {
   6.282 -                    // Set inline response property
   6.283 -                    IsInlineResponse = isInlineResponse
   6.284 -                };
   6.285 -
   6.286                  // Set properties for encrypted attached mail
   6.287                  if (isSecureAttachedMail)
   6.288                  {
   6.289 -                    this.wrappedMailItem.MessageId = messageId;
   6.290 -                    this.wrappedMailItem.IsSecureAttachedMail = true;
   6.291 +                    this.WrappedMailItem.MessageId = messageId;
   6.292 +                    this.WrappedMailItem.IsSecureAttachedMail = true;
   6.293                  }
   6.294  
   6.295                  // Connect cryptable mail item events
   6.296 -                if (this.wrappedMailItem != null)
   6.297 +                if (this.WrappedMailItem != null)
   6.298                  {
   6.299                      // If we don't open the original, set property and return
   6.300                      if (cancelOpenEvent)
   6.301                      {
   6.302 -                        this.wrappedMailItem.Open += MailItem_Open;
   6.303 -                        this.wrappedMailItem.CancelOpen = true;
   6.304 +                        this.WrappedMailItem.Open += MailItem_Open;
   6.305 +                        this.WrappedMailItem.CancelOpen = true;
   6.306                          return;
   6.307                      }
   6.308                      else
   6.309                      {
   6.310                          try
   6.311                          {
   6.312 -                            this.wrappedMailItem.PropertyChanged += MailItem_PropertyChanged;
   6.313 -                            this.wrappedMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
   6.314 -                            this.wrappedMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
   6.315 -                            this.wrappedMailItem.Send += MailItem_Send;
   6.316 +                            this.WrappedMailItem.PropertyChanged += MailItem_PropertyChanged;
   6.317 +                            this.WrappedMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
   6.318 +                            this.WrappedMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
   6.319 +                            this.WrappedMailItem.Send += MailItem_Send;
   6.320  
   6.321 -                            if (this.wrappedMailItem.IsSecurelyStored)
   6.322 +                            if (this.WrappedMailItem.IsSecurelyStored)
   6.323                              {
   6.324 -                                this.wrappedMailItem.Open += MailItem_Open;
   6.325 +                                this.WrappedMailItem.Open += MailItem_Open;
   6.326 +                            }
   6.327 +
   6.328 +                            if (this.IsDraft &&
   6.329 +                                this.WrappedMailItem.IsInSecureStore)
   6.330 +                            {
   6.331 +                                this.WrappedMailItem.Write += MailItem_Write;
   6.332                              }
   6.333                          }
   6.334                          catch (Exception ex)
   6.335 @@ -549,7 +544,7 @@
   6.336                              Log.Error("BuildAndShowManager: Error occured. " + ex.ToString());
   6.337                          }
   6.338  
   6.339 -                        if (this.CurrentMailItem.GetIsPEPEnabled())
   6.340 +                        if (this.WrappedMailItem.IsPEPEnabled)
   6.341                          {
   6.342                              // If pEp is enabled, show the form region
   6.343                              enableFormRegion = true;
   6.344 @@ -567,11 +562,11 @@
   6.345                              if (this.IsDraft)
   6.346                              {
   6.347                                  enableFormRegion = true;
   6.348 -                                this.wrappedMailItem.OriginallyEncryptedStatusUpdated += MailItemWrapper_OriginallyEncryptedStatusUpdated;
   6.349 +                                this.WrappedMailItem.OriginallyEncryptedStatusUpdated += MailItemWrapper_OriginallyEncryptedStatusUpdated;
   6.350                              }
   6.351                              else
   6.352                              {
   6.353 -                                enableFormRegion = this.CurrentMailItem.GetIsDecryptAlwaysEnabled();
   6.354 +                                enableFormRegion = this.WrappedMailItem.IsDecryptAlwaysEnabled;
   6.355                              }
   6.356                          }
   6.357                      }
   6.358 @@ -583,11 +578,11 @@
   6.359                  if (this.isEnabled)
   6.360                  {
   6.361                      // If forcefully protected, run dedicated decryption
   6.362 -                    if (this.wrappedMailItem.IsForcefullyProtected)
   6.363 +                    if (this.WrappedMailItem.IsForcefullyProtected)
   6.364                      {
   6.365 -                        FPPMessage fppMessage = new FPPMessage(this.CurrentMailItem);
   6.366 -                        if ((fppMessage?.GetMessageType() != null) ||
   6.367 -                            (fppMessage?.CurrentMessage?.IsSecure == true))
   6.368 +                        FPPMessage fppMessage = new FPPMessage(this.WrappedMailItem.MailItem);
   6.369 +                        if ((fppMessage.GetMessageType() != null) ||
   6.370 +                            (fppMessage.CurrentMessage?.IsSecure == true))
   6.371                          {
   6.372                              fppMessage.ProcessIncoming(true);
   6.373                          }
   6.374 @@ -630,12 +625,12 @@
   6.375          private void ImmediateRatingAndUIUpdate()
   6.376          {
   6.377              if ((this.isEnabled) &&
   6.378 -                (this.wrappedMailItem != null))
   6.379 +                (this.WrappedMailItem != null))
   6.380              {
   6.381                  Log.Verbose("ImmediateRatingAndUIUpdate: Starting processing.");
   6.382  
   6.383                  // Start the rating calculation/decryption process
   6.384 -                this.wrappedMailItem.StartProcessing(this.IsDraft);
   6.385 +                this.WrappedMailItem.StartProcessing(this.IsDraft);
   6.386                  this.processingOngoing = true;
   6.387  
   6.388                  // If we have a draft message, fetch directly a preview outgoing rating
   6.389 @@ -662,7 +657,7 @@
   6.390  
   6.391              try
   6.392              {
   6.393 -                rating = this.CurrentMailItem?.GetOutgoingRating(false, true) ?? pEpRating.pEpRatingUndefined;
   6.394 +                rating = this.WrappedMailItem?.MailItem?.GetOutgoingRating(false, true) ?? pEpRating.pEpRatingUndefined;
   6.395              }
   6.396              catch (Exception ex)
   6.397              {
   6.398 @@ -787,7 +782,7 @@
   6.399              Outlook.Inspector newInspector = null;
   6.400              Outlook.MailItem tempMailItem = null;
   6.401              Outlook.Store store = null;
   6.402 -            Outlook.MailItem omi = this.CurrentMailItem;
   6.403 +            Outlook.MailItem omi = this.WrappedMailItem?.MailItem;
   6.404  
   6.405              if (omi != null)
   6.406              {
   6.407 @@ -924,7 +919,7 @@
   6.408                      {
   6.409                          try
   6.410                          {
   6.411 -                            sendingAccount = this.CurrentMailItem.SendUsingAccount;
   6.412 +                            sendingAccount = this.WrappedMailItem.MailItem.SendUsingAccount;
   6.413                              if (sendingAccount == null)
   6.414                              {
   6.415                                  ns = Globals.ThisAddIn.Application.Session;
   6.416 @@ -936,7 +931,7 @@
   6.417                                      sendingAccount = AccountExtensions.GetDefaultAccount(Outlook.OlDefaultFolders.olFolderDrafts);
   6.418                                      if (sendingAccount != null)
   6.419                                      {
   6.420 -                                        this.CurrentMailItem.SendUsingAccount = currAccount;
   6.421 +                                        this.WrappedMailItem.MailItem.SendUsingAccount = currAccount;
   6.422                                      }
   6.423                                  }
   6.424                              }
   6.425 @@ -1000,7 +995,7 @@
   6.426          private void ResolveAllRecipients()
   6.427          {
   6.428              if ((this.isEnabled) &&
   6.429 -                (this.wrappedMailItem != null))
   6.430 +                (this.WrappedMailItem != null))
   6.431              {
   6.432                  /*
   6.433                   * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
   6.434 @@ -1011,9 +1006,9 @@
   6.435                   */
   6.436                  try
   6.437                  {
   6.438 -                    this.wrappedMailItem.PropertyChanged -= MailItem_PropertyChanged;
   6.439 -                    this.wrappedMailItem.ResolveAllRecipients();
   6.440 -                    this.wrappedMailItem.PropertyChanged += MailItem_PropertyChanged;
   6.441 +                    this.WrappedMailItem.PropertyChanged -= MailItem_PropertyChanged;
   6.442 +                    this.WrappedMailItem.ResolveAllRecipients();
   6.443 +                    this.WrappedMailItem.PropertyChanged += MailItem_PropertyChanged;
   6.444                  }
   6.445                  catch (Exception ex)
   6.446                  {
   6.447 @@ -1052,8 +1047,9 @@
   6.448                              // Set MAPI properties if needed
   6.449                              if (e.PropertiesToSet?.Count > 0)
   6.450                              {
   6.451 -                                this.CurrentMailItem?.SetMAPIProperties(e.PropertiesToSet);
   6.452 +                                this.WrappedMailItem.MailItem.SetMAPIProperties(e.PropertiesToSet);
   6.453                              }
   6.454 +
   6.455                              Log.Verbose("MailItem_ProcessingComplete: Status bar updated with rating " + Enum.GetName(typeof(pEpRating), e.ProcessedRating));
   6.456                          }
   6.457                          catch (Exception ex)
   6.458 @@ -1077,8 +1073,8 @@
   6.459                               */
   6.460                              try
   6.461                              {
   6.462 -                                if ((this.CurrentMailItem?.GetIsSubmitted() == true) &&
   6.463 -                                    (this.IsDraft))
   6.464 +                                if (this.WrappedMailItem.IsSubmitted &&
   6.465 +                                    this.IsDraft)
   6.466                                  {
   6.467                                      UpdateInspector();
   6.468                                  }
   6.469 @@ -1096,10 +1092,10 @@
   6.470                               */
   6.471                              try
   6.472                              {
   6.473 -                                if (this.wrappedMailItem.IsSecurelyStored || this.wrappedMailItem.IsSecureAttachedMail)
   6.474 +                                if (this.WrappedMailItem.IsSecurelyStored || this.WrappedMailItem.IsSecureAttachedMail)
   6.475                                  {
   6.476                                      Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
   6.477 -                                    this.wrappedMailItem.StartGetMirror(this.wrappedMailItem.MessageId);
   6.478 +                                    this.WrappedMailItem.StartGetMirror(this.WrappedMailItem.MessageId);
   6.479                                  }
   6.480                                  else
   6.481                                  {
   6.482 @@ -1125,7 +1121,7 @@
   6.483  
   6.484                                  try
   6.485                                  {
   6.486 -                                    recipients = this.CurrentMailItem?.Recipients;
   6.487 +                                    recipients = this.WrappedMailItem?.Recipients;
   6.488                                      if (recipients?.Count > 0)
   6.489                                      {
   6.490                                          for (int i = 1; i <= recipients.Count; i++)
   6.491 @@ -1189,22 +1185,22 @@
   6.492                      {
   6.493                          // If the message was forcefully protected, there is no mirror and we show the actual message
   6.494                          if ((e.Mirror == null) &&
   6.495 -                            (this.CurrentMailItem?.GetIsForcefullyProtected() == true))
   6.496 +                            (this.WrappedMailItem.IsForcefullyProtected))
   6.497                          {
   6.498 -                            if (PEPMessage.Create(this.CurrentMailItem, out PEPMessage mirror) == Globals.ReturnStatus.Success)
   6.499 +                            if (PEPMessage.Create(this.WrappedMailItem.MailItem, out PEPMessage mirror) == Globals.ReturnStatus.Success)
   6.500                              {
   6.501                                  e.Mirror = mirror;
   6.502                              }
   6.503                          }
   6.504  
   6.505                          if ((e.Mirror == null) &&
   6.506 -                            (this.wrappedMailItem.LastProcessedStatus == Globals.ReturnStatus.Failure))
   6.507 +                            (this.WrappedMailItem.LastProcessedStatus == Globals.ReturnStatus.Failure))
   6.508                          {
   6.509                              this.SetNote(Properties.Resources.Message_OpenError);
   6.510                              Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
   6.511                          }
   6.512                          else if ((e.Mirror == null) &&
   6.513 -                                 (this.wrappedMailItem.LastProcessedStatus == Globals.ReturnStatus.FailureNoConnection))
   6.514 +                                 (this.WrappedMailItem.LastProcessedStatus == Globals.ReturnStatus.FailureNoConnection))
   6.515                          {
   6.516                              this.SetNote(Properties.Resources.Message_DecryptionNoConnection);
   6.517                              Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
   6.518 @@ -1216,7 +1212,7 @@
   6.519                              // If we have a FormRegionPreviewUnencrypted and it's visible, show preview
   6.520                              if (formRegionPreviewUnencrypted?.Visible == true)
   6.521                              {
   6.522 -                                formRegionPreviewUnencrypted.DisplayState.OriginalEntryId = this.CurrentMailItem?.EntryID;
   6.523 +                                formRegionPreviewUnencrypted.DisplayState.OriginalEntryId = this.WrappedMailItem?.EntryId;
   6.524                                  formRegionPreviewUnencrypted.DisplayState.SetMessage(e.Mirror);
   6.525  
   6.526                                  Log.Verbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
   6.527 @@ -1228,10 +1224,10 @@
   6.528                          }
   6.529  
   6.530                          // Display the mirror if necessary
   6.531 -                        if ((this.wrappedMailItem != null) &&
   6.532 +                        if ((this.WrappedMailItem != null) &&
   6.533                              (this.displayMirrorRequested))
   6.534                          {
   6.535 -                            this.wrappedMailItem.DisplayMirror();
   6.536 +                            this.WrappedMailItem.DisplayMirror();
   6.537                              this.displayMirrorRequested = false;
   6.538                          }
   6.539                      }
   6.540 @@ -1251,9 +1247,9 @@
   6.541          protected void MailItemWrapper_OriginallyEncryptedStatusUpdated(object sender, EventArgs e)
   6.542          {
   6.543              // Process the mail item now that the cache is updated
   6.544 -            if (this.wrappedMailItem.IsOriginallyEncrypted)
   6.545 +            if (this.WrappedMailItem.IsOriginallyEncrypted)
   6.546              {
   6.547 -                this.wrappedMailItem.StartProcessing(this.IsDraft);
   6.548 +                this.WrappedMailItem.StartProcessing(this.IsDraft);
   6.549              }
   6.550          }
   6.551  
   6.552 @@ -1267,18 +1263,18 @@
   6.553          protected void MailItem_Open(ref bool cancel)
   6.554          {
   6.555              if ((this.isEnabled) &&
   6.556 -                (this.wrappedMailItem != null))
   6.557 +                (this.WrappedMailItem != null))
   6.558              {
   6.559                  // If cryptable mail item is not to be opened, cancel opening
   6.560 -                if (this.wrappedMailItem.CancelOpen)
   6.561 +                if (this.WrappedMailItem.CancelOpen)
   6.562                  {
   6.563                      cancel = true;
   6.564                  }
   6.565 -                else if ((this.wrappedMailItem.IsSecurelyStored) &&
   6.566 -                         (this.wrappedMailItem.IsIncoming || this.wrappedMailItem.IsOriginallyEncrypted || !this.wrappedMailItem.IsDraft))
   6.567 +                else if ((this.WrappedMailItem.IsSecurelyStored) &&
   6.568 +                         (this.WrappedMailItem.IsIncoming || this.WrappedMailItem.IsOriginallyEncrypted || !this.WrappedMailItem.IsDraft))
   6.569                  {
   6.570                      // Try to open the mirror
   6.571 -                    cancel = this.wrappedMailItem.DisplayMirror();
   6.572 +                    cancel = this.WrappedMailItem.DisplayMirror();
   6.573                  }
   6.574              }
   6.575          }
   6.576 @@ -1296,7 +1292,7 @@
   6.577  
   6.578              if (this.isEnabled)
   6.579              {
   6.580 -                if ((this.wrappedMailItem.IsBeingProcessed) ||
   6.581 +                if ((this.WrappedMailItem.IsBeingProcessed) ||
   6.582                      (this.processingOngoing))
   6.583                  {
   6.584                      /* If the mail item is still being processed, this means that the message might go out with
   6.585 @@ -1322,7 +1318,7 @@
   6.586                  // Show warning message if needed
   6.587                  if ((Globals.ThisAddIn.Settings.IsSecurityLossWarningEnabled) &&
   6.588                      (this.Rating < pEpRating.pEpRatingUnreliable) &&
   6.589 -                    (this.wrappedMailItem.IsOriginallyEncrypted))
   6.590 +                    (this.WrappedMailItem.IsOriginallyEncrypted))
   6.591                  {
   6.592  
   6.593  #if READER_RELEASE_MODE
   6.594 @@ -1351,16 +1347,16 @@
   6.595                  if (cancel == false)
   6.596                  {
   6.597                      if ((this.Type == WindowType.Inspector) &&
   6.598 -                        (this.wrappedMailItem.IsInlineResponse == false) &&
   6.599 -                        (this.CurrentMailItem.GetProcessingState() == null))
   6.600 -                    {                        
   6.601 -                        IntPtr hWnd = NativeMethods.GetActiveWindow();    
   6.602 +                        (this.WrappedMailItem.IsInlineResponse == false) &&
   6.603 +                        (this.WrappedMailItem.GetProcessingState() == null))
   6.604 +                    {
   6.605 +                        IntPtr hWnd = NativeMethods.GetActiveWindow();
   6.606                          NativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SetWindowPosFlags.DoNotActivate |
   6.607                                                                                                  NativeMethods.SetWindowPosFlags.IgnoreMove |
   6.608                                                                                                  NativeMethods.SetWindowPosFlags.DoNotReposition |
   6.609                                                                                                  NativeMethods.SetWindowPosFlags.IgnoreResize |
   6.610                                                                                                  NativeMethods.SetWindowPosFlags.HideWindow);
   6.611 -                        this.CurrentMailItem.SetProcessingState(MailItemExtensions.ProcessingState.ProcessInBackground);
   6.612 +                        this.WrappedMailItem.SetProcessingState(MailItemExtensions.ProcessingState.ProcessInBackground);
   6.613                          Log.Verbose("MailItem_Send: Inspector window closed.");
   6.614                      }
   6.615  
   6.616 @@ -1368,16 +1364,16 @@
   6.617                      if ((this.ForceProtection) &&
   6.618                          (this.DisableForceProtection == false))
   6.619                      {
   6.620 -                        this.CurrentMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, Guid.NewGuid().ToString());
   6.621 +                        this.WrappedMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, Guid.NewGuid().ToString());
   6.622                      }
   6.623                      else if (this.ForceUnencrypted)
   6.624                      {
   6.625 -                        this.CurrentMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.ForceUnencrypted, true);
   6.626 +                        this.WrappedMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.ForceUnencrypted, true);
   6.627                      }
   6.628  
   6.629                      if (this.NeverUnsecure)
   6.630                      {
   6.631 -                        this.CurrentMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, true);
   6.632 +                        this.WrappedMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, true);
   6.633                      }
   6.634  
   6.635                      // Reset pEp options
   6.636 @@ -1396,6 +1392,15 @@
   6.637          }
   6.638  
   6.639          /// <summary>
   6.640 +        /// Event handler for when a mail item is being written.
   6.641 +        /// </summary>
   6.642 +        /// <param name="cancel">Whether to cancel the event.</param>
   6.643 +        protected void MailItem_Write(ref bool cancel)
   6.644 +        {
   6.645 +      
   6.646 +        }
   6.647 +
   6.648 +        /// <summary>
   6.649          /// Event handler for when a mail item property is changed.
   6.650          /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
   6.651          /// </summary>
   6.652 @@ -1423,7 +1428,7 @@
   6.653                      {
   6.654                          // Update pEp enabled status
   6.655                          this.isStarted = false;
   6.656 -                        this.SetIsEnabled((this.CurrentMailItem)?.GetEnableFormRegion() ?? false);
   6.657 +                        this.SetIsEnabled(this.WrappedMailItem?.MailItem?.GetEnableFormRegion() ?? false);
   6.658  
   6.659                          // Refresh the UI
   6.660                          this.ResolveAllRecipients();
   6.661 @@ -1524,13 +1529,13 @@
   6.662              {
   6.663                  this.refreshOngoing = true;
   6.664  
   6.665 -                if ((this.wrappedMailItem != null) &&
   6.666 -                    (this.wrappedMailItem.IsBeingProcessed == false))
   6.667 +                if ((this.WrappedMailItem != null) &&
   6.668 +                    (this.WrappedMailItem.IsBeingProcessed == false))
   6.669                  {
   6.670                      // Attempt to get the download state
   6.671                      try
   6.672                      {
   6.673 -                        dlState = this.wrappedMailItem.DownloadState;
   6.674 +                        dlState = this.WrappedMailItem.DownloadState;
   6.675                      }
   6.676                      catch (Exception ex)
   6.677                      {