OUT-507: Improve setting of 'pEp Processing' category
authorThomas
Tue, 22 Jan 2019 10:44:46 +0100
changeset 2551 577313f9e962
parent 2550 80e3f982663e
child 2552 dd847ab3e96e
OUT-507: Improve setting of 'pEp Processing' category
Extensions/FolderExtensions.cs
Extensions/MailItemExtensions.cs
ThisAddIn.cs
--- a/Extensions/FolderExtensions.cs	Fri Jan 18 11:33:47 2019 +0100
+++ b/Extensions/FolderExtensions.cs	Tue Jan 22 10:44:46 2019 +0100
@@ -58,5 +58,36 @@
 
             return lastUsed;
         }
+
+        /// <summary>
+        /// Gets the type of the account where the folder is located.
+        /// </summary>
+        /// <param name="folder">The Outlook folder to process with.</param>
+        /// <returns>The mail item's parent account type or default (olOtherAccount) if an error occured.</returns>
+        public static Outlook.OlAccountType GetAccountType(this Outlook.Folder folder)
+        {
+            Outlook.OlAccountType accountType = Outlook.OlAccountType.olOtherAccount;
+            Outlook.Account account = null;
+            Outlook.Store store = null;
+
+            try
+            {
+                store = folder.Store;
+                account = store.GetAccount();
+                accountType = account.AccountType;
+            }
+            catch (Exception ex)
+            {
+                accountType = Outlook.OlAccountType.olOtherAccount;
+                Log.Error("GetAccountType: failure occured, returning default (other account). " + ex.ToString());
+            }
+            finally
+            {
+                account = null;
+                store = null;
+            }
+
+            return accountType;
+        }
     }
 }
--- a/Extensions/MailItemExtensions.cs	Fri Jan 18 11:33:47 2019 +0100
+++ b/Extensions/MailItemExtensions.cs	Tue Jan 22 10:44:46 2019 +0100
@@ -1,4 +1,5 @@
 using MimeKit;
+using pEp.Extensions;
 using pEpCOMServerAdapterLib;
 using System;
 using System.Collections.Generic;
@@ -606,16 +607,12 @@
         public static Outlook.OlAccountType GetAccountType(this Outlook.MailItem omi)
         {
             Outlook.OlAccountType accountType = Outlook.OlAccountType.olOtherAccount;
-            Outlook.Account account = null;
             Outlook.Folder folder = null;
-            Outlook.Store store = null;
 
             try
             {
-                folder = (Outlook.Folder)omi.Parent;
-                store = folder.Store;
-                account = store.GetAccount();
-                accountType = account.AccountType;
+                folder = omi.Parent as Outlook.Folder;                
+                accountType = folder.GetAccountType();
             }
             catch
             {
@@ -624,9 +621,7 @@
             }
             finally
             {
-                account = null;
                 folder = null;
-                store = null;
             }
 
             return accountType;
--- a/ThisAddIn.cs	Fri Jan 18 11:33:47 2019 +0100
+++ b/ThisAddIn.cs	Tue Jan 22 10:44:46 2019 +0100
@@ -1,4 +1,5 @@
-using pEp.UI;
+using pEp.Extensions;
+using pEp.UI;
 using pEpCOMServerAdapterLib;
 using System;
 using System.Collections.Generic;
@@ -3780,9 +3781,10 @@
             /// </summary>
             public event EventHandler Disposed;
 
-            private Outlook.Folder            folder;
-            private Outlook.OlDefaultFolders? defaultFolder;
-            private Outlook.Items             items;
+            private Outlook.OlAccountType       accountType;
+            private Outlook.OlDefaultFolders?   defaultFolder;
+            private Outlook.Folder              folder;
+            private Outlook.Items               items;
 
             /**************************************************************
              * 
@@ -3801,6 +3803,7 @@
                 this.folder = folder;
                 this.defaultFolder = defaultFolder;
                 this.items = this.folder?.Items;
+                this.accountType = folder.GetAccountType();
 
                 if (this.folder != null)
                 {
@@ -3871,6 +3874,7 @@
                 bool process = false;
                 object propValue = null;
                 Outlook.MailItem omi = null;
+                Outlook.Attachments attachments = null;
 
                 try
                 {
@@ -3922,13 +3926,27 @@
                              * the 'pEp Processing' category. As this means that in those environments, this category doesn't get removed correctly
                              * and the mail item remains hidden until a restart of Outlook. Therefore, don't hide messages on IMAP.
                              */
-                            if ((Globals.ThisAddIn.Settings.IsUXImprovementEnabled) &&
-                                (omi?.Attachments?.Count == 2) &&
-                                (omi.GetAccountType() != Outlook.OlAccountType.olImap))
+                            try
                             {
-                                omi.AddPEPProcessingCategory();
-                                omi.UnRead = false;
-                                omi.Save();
+                                if ((Globals.ThisAddIn.Settings.IsUXImprovementEnabled) &&
+                                    (this.accountType != Outlook.OlAccountType.olImap))
+                                {
+                                    attachments = omi.Attachments;
+                                    if (attachments.Count == 2)
+                                    {
+                                        omi.AddPEPProcessingCategory();
+                                        omi.UnRead = false;
+                                        omi.Save();
+                                    }
+                                }
+                            }
+                            catch (Exception ex)
+                            {
+                                Log.Error("Items_ItemAdd. Error adding pEp processing category. " + ex.ToString());
+                            }
+                            finally
+                            {
+                                attachments = null;
                             }
 
                             ThisAddIn.DecryptionStack.TryPush(entryId);