ThisAddIn.cs
branchsync
changeset 2610 09fde2338362
parent 2594 b4351ec5c330
parent 2607 8a9eddf5347a
child 2627 47eb50143f2a
--- a/ThisAddIn.cs	Tue Mar 05 14:45:53 2019 +0100
+++ b/ThisAddIn.cs	Mon Mar 25 13:27:16 2019 +0100
@@ -1547,7 +1547,6 @@
         /// </summary>
         private void OpenPEPStoreRootFolder()
         {
-            string path;
             Outlook.Store pEpStore = null;
             Outlook.Store store = null;
             Outlook.Stores stores = null;
@@ -1555,9 +1554,18 @@
 
             try
             {
+                // The path to store the pEp store to
+                string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
+                
+                /* For Outlook installations from the Windows Trusted Store the AddStoreEx function to add a store seems to
+                 * convert "%LOCALAPPDATA%" to "%LOCALAPPDATA\Packages\Microsoft.Office.Desktop_8wekyb3d8bbwe\LocalCache\Local\".
+                 * This is also confirmed on several sites/blogs. No official documentation has been found so far, though.
+                 * In any case, we also use the above mentioned path to search for the pEp store.
+                 */
+                string winStorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages", "Microsoft.Office.Desktop_8wekyb3d8bbwe", "LocalCache", "Local", "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
+
                 ns = this.Application.Session;
                 stores = ns.Stores;
-                path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
 
                 // Try to find an existing store based on file name
                 for (int i = 1; i <= stores.Count; i++)
@@ -1576,7 +1584,8 @@
                         Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
                     }
 
-                    if (store?.FilePath == path)
+                    if ((store?.FilePath?.Equals(path) == true) ||
+                        (store?.FilePath?.Equals(winStorePath) == true))
                     {
                         pEpStore = store;
                         break;
@@ -1588,7 +1597,7 @@
                 // If no store was found, create new 
                 if (pEpStore == null)
                 {
-                    /* Will create the file and add it as a store, otherwise it will use the existing one.
+                    /* Create the file and add it as a store, otherwise use the existing one.
                      * This can throw an error if a user either modified the list of Outlook data stores
                      * or the pEp.pst file itself.
                      */
@@ -1616,12 +1625,15 @@
                             Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
                         }
 
-                        if (store?.FilePath == path)
+                        if ((store?.FilePath?.Equals(path) == true) ||
+                            (store?.FilePath?.Equals(winStorePath) == true))
                         {
                             pEpStore = store;
                             pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
                             break; // Break before releasing
                         }
+
+                        store = null;
                     }
                 }
 
@@ -1632,14 +1644,14 @@
                 else
                 {
                     // Try again using fallback solution for file paths with prefixes
-                    Log.Warning("OpenPEPStoreRootFolder: No pEp store found. Trying fallback. \npEp path is " + path);
+                    Log.Warning("OpenPEPStoreRootFolder: No pEp store found. Trying fallback. pEp path is " + path);
 
                     for (int i = 1; i <= stores.Count; i++)
                     {
                         try
                         {
                             store = stores[i];
-                            Log.Warning("OpenPEPStoreRootFolder: file path of store " + i + ": " + store?.FilePath ?? "<null>");
+                            Log.Info("OpenPEPStoreRootFolder: file path of store " + i + " (" + (store?.DisplayName ?? "<null>") + "): " + store?.FilePath ?? "<null>");
                         }
                         catch (Exception ex)
                         {
@@ -1647,18 +1659,21 @@
                         }
 
                         // Custom comparison of file paths independently of their (known) prefixes
-                        if (Comparisons.FilePathsAreEqual(store?.FilePath, path))
+                        if (Comparisons.FilePathsAreEqual(store?.FilePath, path) ||
+                            Comparisons.FilePathsAreEqual(store?.FilePath, winStorePath))
                         {
                             pEpStore = store;
                             pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
                             break;
                         }
+
+                        store = null;
                     }
 
                     // If pEp store was found, use it. Else throw error.
                     if (pEpStore != null)
                     {
-                        this._PEPStoreRootFolder = (Outlook.Folder)pEpStore.GetRootFolder();
+                        this._PEPStoreRootFolder = pEpStore.GetRootFolder() as Outlook.Folder;
                     }
                     else
                     {