Improve GetIsSecureStorageEnabled() method
authorThomas
Thu, 11 Oct 2018 10:52:44 +0200
changeset 2403621ad3c9c3d0
parent 2402 b0701760bfae
child 2404 a1760ea7715b
Improve GetIsSecureStorageEnabled() method
Extensions/StoreExtensions.cs
     1.1 --- a/Extensions/StoreExtensions.cs	Thu Oct 11 09:33:59 2018 +0200
     1.2 +++ b/Extensions/StoreExtensions.cs	Thu Oct 11 10:52:44 2018 +0200
     1.3 @@ -250,12 +250,14 @@
     1.4          /// <returns>True if the outlook store is to store messages securely, otherwise false.</returns>
     1.5          public static bool GetIsSecureStorageEnabled(this Outlook.Store store)
     1.6          {
     1.7 +            bool accountFound = false;
     1.8              bool defaultResult = PEPSettings.ACCOUNT_SETTING_IS_SECURE_STORAGE_ENABLED_DEFAULT;
     1.9              bool result = defaultResult;
    1.10              Outlook.Store deliveryStore = null;
    1.11              Outlook.Account account = null;
    1.12              Outlook.Accounts accounts = null;
    1.13              Outlook.NameSpace ns = null;
    1.14 +            Outlook.Folder folder;
    1.15              PEPSettings.PEPAccountSettings acctSettings;
    1.16  
    1.17              if (Globals.ThisAddIn.Settings.IsEncryptAllAccountsEnabled)
    1.18 @@ -266,7 +268,7 @@
    1.19              try
    1.20              {
    1.21                  if (store != null)
    1.22 -                {
    1.23 +                {                  
    1.24                      ns = Globals.ThisAddIn.Application.Session;
    1.25                      accounts = ns.Accounts;
    1.26  
    1.27 @@ -286,32 +288,36 @@
    1.28                              Log.Warning("GetIsSecureStorageEnabled: Failure getting DeliveryStore.");
    1.29                          }
    1.30  
    1.31 -                        if ((deliveryStore != null) &&
    1.32 -                            (deliveryStore.StoreID == store.StoreID))
    1.33 +                        // If in account store, find account and check settings
    1.34 +                        if (deliveryStore?.StoreID?.Equals(store?.StoreID) == true)
    1.35                          {
    1.36                              acctSettings = Globals.ThisAddIn.Settings.GetAccountSettings(account.SmtpAddress);
    1.37                              if (acctSettings != null)
    1.38                              {
    1.39                                  result = acctSettings.IsSecureStorageEnabled;
    1.40 -                                break;
    1.41                              }
    1.42                              else
    1.43                              {
    1.44                                  result = defaultResult;
    1.45 -                                break;
    1.46                              }
    1.47 +
    1.48 +                            accountFound = true;
    1.49 +                            break;
    1.50                          }
    1.51  
    1.52 -                        if (account != null)
    1.53 +                        account = null;
    1.54 +                        deliveryStore = null;
    1.55 +                    }
    1.56 +
    1.57 +                    // If not in an account store, check if in pEp store
    1.58 +                    if (accountFound == false)
    1.59 +                    {
    1.60 +                        folder = store.GetRootFolder() as Outlook.Folder;
    1.61 +                        string folderPath = folder.FolderPath;
    1.62 +                        string pEpStoreRootFolderPath = Globals.ThisAddIn.PEPStoreRootFolder.FolderPath;
    1.63 +                        if (folderPath?.Equals(pEpStoreRootFolderPath) == true)
    1.64                          {
    1.65 -                            // Marshal.ReleaseComObject(account);
    1.66 -                            account = null;
    1.67 -                        }
    1.68 -
    1.69 -                        if (deliveryStore != null)
    1.70 -                        {
    1.71 -                            // Marshal.ReleaseComObject(deliveryStore);
    1.72 -                            deliveryStore = null;
    1.73 +                            result = false;
    1.74                          }
    1.75                      }
    1.76                  }
    1.77 @@ -329,32 +335,14 @@
    1.78              finally
    1.79              {
    1.80                  // Release objects
    1.81 -                if (deliveryStore != null)
    1.82 -                {
    1.83 -                    // Marshal.ReleaseComObject(deliveryStore);
    1.84 -                    deliveryStore = null;
    1.85 -                }
    1.86 -
    1.87 -                if (account != null)
    1.88 -                {
    1.89 -                    // Marshal.ReleaseComObject(account);
    1.90 -                    account = null;
    1.91 -                }
    1.92 -
    1.93 -                if (accounts != null)
    1.94 -                {
    1.95 -                    // Marshal.ReleaseComObject(accounts);
    1.96 -                    accounts = null;
    1.97 -                }
    1.98 -
    1.99 -                if (ns != null)
   1.100 -                {
   1.101 -                    // Marshal.ReleaseComObject(ns);
   1.102 -                    ns = null;
   1.103 -                }
   1.104 +                account = null;
   1.105 +                accounts = null;
   1.106 +                deliveryStore = null;
   1.107 +                folder = null;
   1.108 +                ns = null;
   1.109              }
   1.110  
   1.111 -            return (result);
   1.112 +            return result;
   1.113          }
   1.114  
   1.115          /// <summary>