Small reordering of initialization of preview unencrypted form region
authorThomas
Mon, 18 Mar 2019 12:24:49 +0100
changeset 2602 4d1dec983a8b
parent 2598 9ec2fef7baea
child 2603 65f867d4c864
child 2605 86a03f2ff37f
Small reordering of initialization of preview unencrypted form region
UI/FormRegionPreviewUnencrypted.cs
--- a/UI/FormRegionPreviewUnencrypted.cs	Wed Mar 13 13:50:02 2019 +0100
+++ b/UI/FormRegionPreviewUnencrypted.cs	Mon Mar 18 12:24:49 2019 +0100
@@ -21,44 +21,41 @@
             // Use e.OutlookItem to get a reference to the current Outlook item.
             private void FormRegionPreviewUnencryptedFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
             {
-                Outlook.MailItem omi;
-
-                try
+                if (e?.OutlookItem is Outlook.MailItem omi)
                 {
-                    omi = (Outlook.MailItem)e.OutlookItem;
+                    // Do not load if the mail item itself is not stored encrypted or pEp is disabled
+                    if ((omi.GetIsSecurelyStored() == false) ||
+                        (omi.GetIsPEPEnabled() == false) && (omi.GetIsDecryptAlwaysEnabled() == false))
+                    {
+                        e.Cancel = true;
+                    }
+                    else if (string.IsNullOrEmpty(CryptableMailItem.PreviewAttachedMailId) == false)
+                    {
+                        HeaderList headers = omi.GetParsedTransportMessageHeaders();
+                        string messageId = null;
+
+                        try
+                        {
+                            messageId = headers[HeaderId.MessageId];
+                        }
+                        catch (Exception ex)
+                        {
+                            Log.Verbose("FormRegionPreviewUnencryptedFactory_FormRegionInitializing: Error getting MessageId from item. " + ex.ToString());
+                        }
+
+                        e.Cancel = (messageId?.Equals(CryptableMailItem.PreviewAttachedMailId) != true);
+                    }
+                    else
+                    {
+                        e.Cancel = false;
+                    }
+
+                    omi = null;
                 }
-                catch
+                else
                 {
                     // Never load if it's not a MailItem
                     e.Cancel = true;
-                    return;
-                }
-
-                // Do not load if the mail item itself is not stored encrypted or pEp is disabled
-                if ((omi.GetIsSecurelyStored() == false) ||
-                    (omi.GetIsPEPEnabled() == false) && (omi.GetIsDecryptAlwaysEnabled() == false))
-                {
-                    e.Cancel = true;
-                }
-                else if (string.IsNullOrEmpty(CryptableMailItem.PreviewAttachedMailId) == false)
-                {
-                    HeaderList headers = omi.GetParsedTransportMessageHeaders();
-                    string messageId = null;
-
-                    try
-                    {
-                        messageId = headers[HeaderId.MessageId];
-                    }
-                    catch (Exception ex)
-                    {
-                        Log.Verbose("FormRegionPreviewUnencryptedFactory_FormRegionInitializing: Error getting MessageId from item. " + ex.ToString());
-                    }
-
-                    e.Cancel = (messageId?.Equals(CryptableMailItem.PreviewAttachedMailId) != true);
-                }
-                else
-                {
-                    e.Cancel = false;
                 }
 
                 Log.Verbose("FormRegionPreviewUnencryptedFactory_FormRegionInitializing: e.Cancel = " + e.Cancel.ToString());