ThisAddIn.cs
branchsync
changeset 2610 09fde2338362
parent 2594 b4351ec5c330
parent 2607 8a9eddf5347a
child 2627 47eb50143f2a
     1.1 --- a/ThisAddIn.cs	Tue Mar 05 14:45:53 2019 +0100
     1.2 +++ b/ThisAddIn.cs	Mon Mar 25 13:27:16 2019 +0100
     1.3 @@ -1547,7 +1547,6 @@
     1.4          /// </summary>
     1.5          private void OpenPEPStoreRootFolder()
     1.6          {
     1.7 -            string path;
     1.8              Outlook.Store pEpStore = null;
     1.9              Outlook.Store store = null;
    1.10              Outlook.Stores stores = null;
    1.11 @@ -1555,9 +1554,18 @@
    1.12  
    1.13              try
    1.14              {
    1.15 +                // The path to store the pEp store to
    1.16 +                string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
    1.17 +                
    1.18 +                /* For Outlook installations from the Windows Trusted Store the AddStoreEx function to add a store seems to
    1.19 +                 * convert "%LOCALAPPDATA%" to "%LOCALAPPDATA\Packages\Microsoft.Office.Desktop_8wekyb3d8bbwe\LocalCache\Local\".
    1.20 +                 * This is also confirmed on several sites/blogs. No official documentation has been found so far, though.
    1.21 +                 * In any case, we also use the above mentioned path to search for the pEp store.
    1.22 +                 */
    1.23 +                string winStorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages", "Microsoft.Office.Desktop_8wekyb3d8bbwe", "LocalCache", "Local", "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
    1.24 +
    1.25                  ns = this.Application.Session;
    1.26                  stores = ns.Stores;
    1.27 -                path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
    1.28  
    1.29                  // Try to find an existing store based on file name
    1.30                  for (int i = 1; i <= stores.Count; i++)
    1.31 @@ -1576,7 +1584,8 @@
    1.32                          Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
    1.33                      }
    1.34  
    1.35 -                    if (store?.FilePath == path)
    1.36 +                    if ((store?.FilePath?.Equals(path) == true) ||
    1.37 +                        (store?.FilePath?.Equals(winStorePath) == true))
    1.38                      {
    1.39                          pEpStore = store;
    1.40                          break;
    1.41 @@ -1588,7 +1597,7 @@
    1.42                  // If no store was found, create new 
    1.43                  if (pEpStore == null)
    1.44                  {
    1.45 -                    /* Will create the file and add it as a store, otherwise it will use the existing one.
    1.46 +                    /* Create the file and add it as a store, otherwise use the existing one.
    1.47                       * This can throw an error if a user either modified the list of Outlook data stores
    1.48                       * or the pEp.pst file itself.
    1.49                       */
    1.50 @@ -1616,12 +1625,15 @@
    1.51                              Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
    1.52                          }
    1.53  
    1.54 -                        if (store?.FilePath == path)
    1.55 +                        if ((store?.FilePath?.Equals(path) == true) ||
    1.56 +                            (store?.FilePath?.Equals(winStorePath) == true))
    1.57                          {
    1.58                              pEpStore = store;
    1.59                              pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
    1.60                              break; // Break before releasing
    1.61                          }
    1.62 +
    1.63 +                        store = null;
    1.64                      }
    1.65                  }
    1.66  
    1.67 @@ -1632,14 +1644,14 @@
    1.68                  else
    1.69                  {
    1.70                      // Try again using fallback solution for file paths with prefixes
    1.71 -                    Log.Warning("OpenPEPStoreRootFolder: No pEp store found. Trying fallback. \npEp path is " + path);
    1.72 +                    Log.Warning("OpenPEPStoreRootFolder: No pEp store found. Trying fallback. pEp path is " + path);
    1.73  
    1.74                      for (int i = 1; i <= stores.Count; i++)
    1.75                      {
    1.76                          try
    1.77                          {
    1.78                              store = stores[i];
    1.79 -                            Log.Warning("OpenPEPStoreRootFolder: file path of store " + i + ": " + store?.FilePath ?? "<null>");
    1.80 +                            Log.Info("OpenPEPStoreRootFolder: file path of store " + i + " (" + (store?.DisplayName ?? "<null>") + "): " + store?.FilePath ?? "<null>");
    1.81                          }
    1.82                          catch (Exception ex)
    1.83                          {
    1.84 @@ -1647,18 +1659,21 @@
    1.85                          }
    1.86  
    1.87                          // Custom comparison of file paths independently of their (known) prefixes
    1.88 -                        if (Comparisons.FilePathsAreEqual(store?.FilePath, path))
    1.89 +                        if (Comparisons.FilePathsAreEqual(store?.FilePath, path) ||
    1.90 +                            Comparisons.FilePathsAreEqual(store?.FilePath, winStorePath))
    1.91                          {
    1.92                              pEpStore = store;
    1.93                              pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
    1.94                              break;
    1.95                          }
    1.96 +
    1.97 +                        store = null;
    1.98                      }
    1.99  
   1.100                      // If pEp store was found, use it. Else throw error.
   1.101                      if (pEpStore != null)
   1.102                      {
   1.103 -                        this._PEPStoreRootFolder = (Outlook.Folder)pEpStore.GetRootFolder();
   1.104 +                        this._PEPStoreRootFolder = pEpStore.GetRootFolder() as Outlook.Folder;
   1.105                      }
   1.106                      else
   1.107                      {