ThisAddIn.cs
branchsync
changeset 2610 09fde2338362
parent 2594 b4351ec5c330
parent 2607 8a9eddf5347a
child 2627 47eb50143f2a
equal deleted inserted replaced
2594:b4351ec5c330 2610:09fde2338362
  1545         /// Opens the pEp data store root folder and saves the reference for further use.
  1545         /// Opens the pEp data store root folder and saves the reference for further use.
  1546         /// If none already exists, a new one is created.
  1546         /// If none already exists, a new one is created.
  1547         /// </summary>
  1547         /// </summary>
  1548         private void OpenPEPStoreRootFolder()
  1548         private void OpenPEPStoreRootFolder()
  1549         {
  1549         {
  1550             string path;
       
  1551             Outlook.Store pEpStore = null;
  1550             Outlook.Store pEpStore = null;
  1552             Outlook.Store store = null;
  1551             Outlook.Store store = null;
  1553             Outlook.Stores stores = null;
  1552             Outlook.Stores stores = null;
  1554             Outlook.NameSpace ns = null;
  1553             Outlook.NameSpace ns = null;
  1555 
  1554 
  1556             try
  1555             try
  1557             {
  1556             {
       
  1557                 // The path to store the pEp store to
       
  1558                 string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
       
  1559                 
       
  1560                 /* For Outlook installations from the Windows Trusted Store the AddStoreEx function to add a store seems to
       
  1561                  * convert "%LOCALAPPDATA%" to "%LOCALAPPDATA\Packages\Microsoft.Office.Desktop_8wekyb3d8bbwe\LocalCache\Local\".
       
  1562                  * This is also confirmed on several sites/blogs. No official documentation has been found so far, though.
       
  1563                  * In any case, we also use the above mentioned path to search for the pEp store.
       
  1564                  */
       
  1565                 string winStorePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages", "Microsoft.Office.Desktop_8wekyb3d8bbwe", "LocalCache", "Local", "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
       
  1566 
  1558                 ns = this.Application.Session;
  1567                 ns = this.Application.Session;
  1559                 stores = ns.Stores;
  1568                 stores = ns.Stores;
  1560                 path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", (Globals.PEP_DATA_FILE_NAME + ".pst"));
       
  1561 
  1569 
  1562                 // Try to find an existing store based on file name
  1570                 // Try to find an existing store based on file name
  1563                 for (int i = 1; i <= stores.Count; i++)
  1571                 for (int i = 1; i <= stores.Count; i++)
  1564                 {
  1572                 {
  1565                     /* Note: accessing the stores can fail if the data file is missing or is in use by another program. 
  1573                     /* Note: accessing the stores can fail if the data file is missing or is in use by another program. 
  1574                     catch (Exception ex)
  1582                     catch (Exception ex)
  1575                     {
  1583                     {
  1576                         Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
  1584                         Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
  1577                     }
  1585                     }
  1578 
  1586 
  1579                     if (store?.FilePath == path)
  1587                     if ((store?.FilePath?.Equals(path) == true) ||
       
  1588                         (store?.FilePath?.Equals(winStorePath) == true))
  1580                     {
  1589                     {
  1581                         pEpStore = store;
  1590                         pEpStore = store;
  1582                         break;
  1591                         break;
  1583                     }
  1592                     }
  1584 
  1593 
  1586                 }
  1595                 }
  1587 
  1596 
  1588                 // If no store was found, create new 
  1597                 // If no store was found, create new 
  1589                 if (pEpStore == null)
  1598                 if (pEpStore == null)
  1590                 {
  1599                 {
  1591                     /* Will create the file and add it as a store, otherwise it will use the existing one.
  1600                     /* Create the file and add it as a store, otherwise use the existing one.
  1592                      * This can throw an error if a user either modified the list of Outlook data stores
  1601                      * This can throw an error if a user either modified the list of Outlook data stores
  1593                      * or the pEp.pst file itself.
  1602                      * or the pEp.pst file itself.
  1594                      */
  1603                      */
  1595                     try
  1604                     try
  1596                     {
  1605                     {
  1614                         catch (Exception ex)
  1623                         catch (Exception ex)
  1615                         {
  1624                         {
  1616                             Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
  1625                             Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
  1617                         }
  1626                         }
  1618 
  1627 
  1619                         if (store?.FilePath == path)
  1628                         if ((store?.FilePath?.Equals(path) == true) ||
       
  1629                             (store?.FilePath?.Equals(winStorePath) == true))
  1620                         {
  1630                         {
  1621                             pEpStore = store;
  1631                             pEpStore = store;
  1622                             pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
  1632                             pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
  1623                             break; // Break before releasing
  1633                             break; // Break before releasing
  1624                         }
  1634                         }
       
  1635 
       
  1636                         store = null;
  1625                     }
  1637                     }
  1626                 }
  1638                 }
  1627 
  1639 
  1628                 if (pEpStore != null)
  1640                 if (pEpStore != null)
  1629                 {
  1641                 {
  1630                     this._PEPStoreRootFolder = (Outlook.Folder)pEpStore.GetRootFolder();
  1642                     this._PEPStoreRootFolder = (Outlook.Folder)pEpStore.GetRootFolder();
  1631                 }
  1643                 }
  1632                 else
  1644                 else
  1633                 {
  1645                 {
  1634                     // Try again using fallback solution for file paths with prefixes
  1646                     // Try again using fallback solution for file paths with prefixes
  1635                     Log.Warning("OpenPEPStoreRootFolder: No pEp store found. Trying fallback. \npEp path is " + path);
  1647                     Log.Warning("OpenPEPStoreRootFolder: No pEp store found. Trying fallback. pEp path is " + path);
  1636 
  1648 
  1637                     for (int i = 1; i <= stores.Count; i++)
  1649                     for (int i = 1; i <= stores.Count; i++)
  1638                     {
  1650                     {
  1639                         try
  1651                         try
  1640                         {
  1652                         {
  1641                             store = stores[i];
  1653                             store = stores[i];
  1642                             Log.Warning("OpenPEPStoreRootFolder: file path of store " + i + ": " + store?.FilePath ?? "<null>");
  1654                             Log.Info("OpenPEPStoreRootFolder: file path of store " + i + " (" + (store?.DisplayName ?? "<null>") + "): " + store?.FilePath ?? "<null>");
  1643                         }
  1655                         }
  1644                         catch (Exception ex)
  1656                         catch (Exception ex)
  1645                         {
  1657                         {
  1646                             Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
  1658                             Log.Warning("OpenPEPStoreRootFolder: Failed to get store, " + ex.ToString());
  1647                         }
  1659                         }
  1648 
  1660 
  1649                         // Custom comparison of file paths independently of their (known) prefixes
  1661                         // Custom comparison of file paths independently of their (known) prefixes
  1650                         if (Comparisons.FilePathsAreEqual(store?.FilePath, path))
  1662                         if (Comparisons.FilePathsAreEqual(store?.FilePath, path) ||
       
  1663                             Comparisons.FilePathsAreEqual(store?.FilePath, winStorePath))
  1651                         {
  1664                         {
  1652                             pEpStore = store;
  1665                             pEpStore = store;
  1653                             pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
  1666                             pEpStore.GetRootFolder().Name = Globals.PEP_DATA_FILE_NAME;
  1654                             break;
  1667                             break;
  1655                         }
  1668                         }
       
  1669 
       
  1670                         store = null;
  1656                     }
  1671                     }
  1657 
  1672 
  1658                     // If pEp store was found, use it. Else throw error.
  1673                     // If pEp store was found, use it. Else throw error.
  1659                     if (pEpStore != null)
  1674                     if (pEpStore != null)
  1660                     {
  1675                     {
  1661                         this._PEPStoreRootFolder = (Outlook.Folder)pEpStore.GetRootFolder();
  1676                         this._PEPStoreRootFolder = pEpStore.GetRootFolder() as Outlook.Folder;
  1662                     }
  1677                     }
  1663                     else
  1678                     else
  1664                     {
  1679                     {
  1665                         throw new Exception("Cannot open required pEp.pst file.");
  1680                         throw new Exception("Cannot open required pEp.pst file.");
  1666                     }
  1681                     }