Backport OUT-89 fix to 1.0 series. Release_1_0_Series
authorDean Looyengoed
Thu, 08 Sep 2016 16:23:39 +0200
branchRelease_1_0_Series
changeset 1255d9e1668c0c40
parent 1087 204f5efdb983
child 1345 73d4e38b6c81
Backport OUT-89 fix to 1.0 series.
Globals.cs
ThisAddIn.cs
     1.1 --- a/Globals.cs	Wed Jul 20 14:13:10 2016 +0200
     1.2 +++ b/Globals.cs	Thu Sep 08 16:23:39 2016 +0200
     1.3 @@ -36,6 +36,7 @@
     1.4          public const string                LOG_FILE_NAME                                      = "log.txt";
     1.5  
     1.6          public const string PEP_COPYRIGHT              = "Copyright 2014-2016 p≡p Security SA, Luxembourg";
     1.7 +        public const string PEP_DATA_FILE_NAME         = "pEp";                           // The file name of the pEp data store. This does NOT include extension (.pst)
     1.8          public const string PEP_NAME                   = "p≡p for Outlook";               // Display name to the user
     1.9  #if DEBUG
    1.10          public const string PEP_NAME_ADDIN_DESC        = "pEp";                           // Description of the add-in within Outlook itself (shared with reader)
     2.1 --- a/ThisAddIn.cs	Wed Jul 20 14:13:10 2016 +0200
     2.2 +++ b/ThisAddIn.cs	Thu Sep 08 16:23:39 2016 +0200
     2.3 @@ -3,8 +3,8 @@
     2.4  using System;
     2.5  using System.Collections.Generic;
     2.6  using System.Globalization;
     2.7 +using System.IO;
     2.8  using System.Runtime.InteropServices;
     2.9 -using System.Security.Cryptography;
    2.10  using System.Text;
    2.11  using System.Threading;
    2.12  using System.Windows.Forms;
    2.13 @@ -1884,9 +1884,8 @@
    2.14          /// </summary>
    2.15          private void OpenTempFolder()
    2.16          {
    2.17 -            string dir;
    2.18              string path;
    2.19 -            Outlook.Store tempStore = null;
    2.20 +            Outlook.Store pEpStore = null;
    2.21              Outlook.Store curStore = null;
    2.22              Outlook.Stores stores = null;
    2.23              Outlook.NameSpace ns = null;
    2.24 @@ -1896,37 +1895,29 @@
    2.25                  ns = this.Application.Session;
    2.26                  stores = ns.Stores;
    2.27  
    2.28 -                // Try to find existing based on display name
    2.29 +                // Try to find an existing store based on display name
    2.30                  for (int i = 1; i <= stores.Count; i++)
    2.31                  {
    2.32 -                    curStore = stores[i];
    2.33 -
    2.34 -                    if (curStore.DisplayName == "pEp")
    2.35 -                    {
    2.36 -                        tempStore = curStore;
    2.37 -                        break;
    2.38 -                    }
    2.39 -
    2.40 -                    Marshal.ReleaseComObject(curStore);
    2.41 -                    curStore = null;
    2.42 -                }
    2.43 -
    2.44 -                // Create new
    2.45 -                if (tempStore == null)
    2.46 -                {
    2.47 -                    dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    2.48 -                    path = dir + "\\pEp\\pEp.pst";
    2.49 -
    2.50 -                    ns.AddStoreEx(path, Outlook.OlStoreType.olStoreUnicode);
    2.51 -
    2.52 -                    for (int i = 1; i <= stores.Count; i++)
    2.53 +                    /* Note: accessing the stores can fail if the data file is missing or is in use by another program.
    2.54 +                     * This is usually OK as long as it isn't the pEp file.
    2.55 +                     * However, here the assumption is made that any error is not the pEp file and execution will continue.
    2.56 +                     * If for some reason the error prevents detecting an existing pEp.pst file, this will be caught in the next step.
    2.57 +                     */
    2.58 +                    try
    2.59                      {
    2.60                          curStore = stores[i];
    2.61 -
    2.62 -                        if (curStore.FilePath == path)
    2.63 +                    }
    2.64 +                    catch (Exception ex1)
    2.65 +                    {
    2.66 +                        Globals.Log("OpenPEPStoreRootFolder: Failed to get store, " + ex1.ToString());
    2.67 +                    }
    2.68 +
    2.69 +                    if (curStore != null)
    2.70 +                    {
    2.71 +                        if ((curStore.DisplayName != null) &&
    2.72 +                            (curStore.DisplayName == Globals.PEP_DATA_FILE_NAME))
    2.73                          {
    2.74 -                            tempStore = curStore;
    2.75 -                            tempStore.GetRootFolder().Name = "pEp";
    2.76 +                            pEpStore = curStore;
    2.77                              break;
    2.78                          }
    2.79  
    2.80 @@ -1935,7 +1926,49 @@
    2.81                      }
    2.82                  }
    2.83  
    2.84 -                this._TempFolder = (Outlook.Folder)tempStore.GetRootFolder();
    2.85 +                // Create new
    2.86 +                if (pEpStore == null)
    2.87 +                {
    2.88 +                    path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
    2.89 +
    2.90 +                    // Will create the file and add it as a store, otherwise it will use the existing one
    2.91 +                    ns.AddStoreEx(path, Outlook.OlStoreType.olStoreUnicode);
    2.92 +
    2.93 +                    for (int i = 1; i <= stores.Count; i++)
    2.94 +                    {
    2.95 +                        /* Since a pEp store was just created or added, it's a reasonably safe assumption that any error
    2.96 +                         * is not the pEp.pst store. Therefore, just ignore errors and continue searching.
    2.97 +                         */
    2.98 +                        try
    2.99 +                        {
   2.100 +                            curStore = stores[i];
   2.101 +                        }
   2.102 +                        catch { }
   2.103 +
   2.104 +                        if (curStore != null)
   2.105 +                        {
   2.106 +                            if ((curStore.FilePath != null) &&
   2.107 +                                (curStore.FilePath == path))
   2.108 +                            {
   2.109 +                                pEpStore = curStore;
   2.110 +                                pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
   2.111 +                                break;
   2.112 +                            }
   2.113 +
   2.114 +                            Marshal.ReleaseComObject(curStore);
   2.115 +                            curStore = null;
   2.116 +                        }
   2.117 +                    }
   2.118 +                }
   2.119 +
   2.120 +                if (pEpStore != null)
   2.121 +                {
   2.122 +                    this._TempFolder = (Outlook.Folder)pEpStore.GetRootFolder();
   2.123 +                }
   2.124 +                else
   2.125 +                {
   2.126 +                    throw new Exception("Cannot open required pEp.pst file.");
   2.127 +                }
   2.128              }
   2.129              catch (Exception ex)
   2.130              {
   2.131 @@ -1962,10 +1995,10 @@
   2.132                      ns = null;
   2.133                  }
   2.134  
   2.135 -                if (tempStore != null)
   2.136 +                if (pEpStore != null)
   2.137                  {
   2.138 -                    Marshal.ReleaseComObject(tempStore);
   2.139 -                    tempStore = null;
   2.140 +                    Marshal.ReleaseComObject(pEpStore);
   2.141 +                    pEpStore = null;
   2.142                  }
   2.143              }
   2.144  
   2.145 @@ -2761,7 +2794,7 @@
   2.146                  }
   2.147              }
   2.148              catch { }
   2.149 -            
   2.150 +
   2.151              try
   2.152              {
   2.153                  /* Note: This is sensitive to time as pEp needs to start as fast as possible.