OUT-573: Read Registry value pEp/UserFolder and use it if available OUT-573
authorThomas
Mon, 17 Jun 2019 14:09:26 +0200
branchOUT-573
changeset 26671a7d192f0d56
parent 2663 269595c41be5
child 2668 68b3a55e02a0
OUT-573: Read Registry value pEp/UserFolder and use it if available
Globals.cs
Log.cs
PEPDatabase.cs
PEPSettings.cs
ThisAddIn.cs
     1.1 --- a/Globals.cs	Wed Jun 12 15:54:47 2019 +0200
     1.2 +++ b/Globals.cs	Mon Jun 17 14:09:26 2019 +0200
     1.3 @@ -2,6 +2,7 @@
     1.4  using pEp.UI;
     1.5  using System;
     1.6  using System.Collections.Generic;
     1.7 +using System.Diagnostics;
     1.8  using System.IO;
     1.9  using System.Reflection;
    1.10  using System.Runtime.InteropServices;
    1.11 @@ -19,23 +20,25 @@
    1.12      /// </summary>
    1.13      internal sealed partial class Globals
    1.14      {
    1.15 -        public const string CUSTOM_SENT_FOLDER_NAME         = "Sent";
    1.16 -        public const string PEP_INTERNAL_CATEGORY_NAME      = "pEp Internal";
    1.17 -        public const string PEP_PROCESSING_CATEGORY_NAME    = "pEp Processing";
    1.18 -        public const string PEP_COPYRIGHT                   = "Copyright © 2014-2019 p≡p Security SA, Luxembourg";
    1.19 -        public const string PEP_DATA_FILE_NAME              = "pEp";                           // The file name of the pEp data store. This does NOT include extension (.pst)
    1.20 -        public const string PEP_DISPLAY_NAME                = "p≡p for Outlook";               // Display name to the user
    1.21 -        public const string PEP_DISPLAY_NAME_READER         = "p≡p Reader for Outlook";        // Display name to the user
    1.22 -        public const string PEP_DRAFTS_FOLDER_NAME          = "pEp Drafts";                    // Folder name for secure drafts
    1.23 +        public const string CUSTOM_SENT_FOLDER_NAME                 = "Sent";
    1.24 +        public const string PEP_REG_NAME_USER_FOLDER                = "UserFolder";
    1.25 +        public const string PEP_COPYRIGHT                           = "Copyright © 2014-2019 p≡p Security SA, Luxembourg";
    1.26 +        public const string PEP_DATA_FILE_NAME                      = "pEp";                           // The file name of the pEp data store. This does NOT include extension (.pst)
    1.27 +        public const string PEP_DISPLAY_NAME                        = "p≡p for Outlook";               // Display name to the user
    1.28 +        public const string PEP_DISPLAY_NAME_READER                 = "p≡p Reader for Outlook";        // Display name to the user
    1.29 +        public const string PEP_DRAFTS_FOLDER_NAME                  = "pEp Drafts";                    // Folder name for secure drafts
    1.30 +        public const string PEP_FOLDER_NAME                         = "pEp";
    1.31 +        public const string PEP_INTERNAL_CATEGORY_NAME              = "pEp Internal";
    1.32  #if DEBUG
    1.33 -        public const string PEP_NAME_ADDIN_DESC             = "pEp";                           // Description of the add-in within Outlook itself (shared with reader)
    1.34 +        public const string PEP_NAME_ADDIN_DESC                     = "pEp";                           // Description of the add-in within Outlook itself (shared with reader)
    1.35  #else
    1.36 -        public const string PEP_NAME_ADDIN_DESC             = "pEp for Outlook";               // Description of the add-in within Outlook itself (shared with reader)
    1.37 +        public const string PEP_NAME_ADDIN_DESC                     = "pEp for Outlook";               // Description of the add-in within Outlook itself (shared with reader)
    1.38  #endif
    1.39 -        public const string PEP_NAME_INSTALL                = "pEp for Outlook";               // Name for Windows installer, must sync with installation code (shared with reader)
    1.40 -        public const string PEP_WEBSITE_LINK                = "https://pEp.software";
    1.41 -        public const string PEP_WEBSITE_UPGRADE_LINK        = "https://pEp.software";
    1.42 -        public const string PEP_WEBSITE_READER_LINK         = "https://pEp.security/reader";
    1.43 +        public const string PEP_NAME_INSTALL                        = "pEp for Outlook";               // Name for Windows installer, must sync with installation code (shared with reader)
    1.44 +        public const string PEP_PROCESSING_CATEGORY_NAME            = "pEp Processing";
    1.45 +        public const string PEP_WEBSITE_LINK                        = "https://pEp.software";
    1.46 +        public const string PEP_WEBSITE_UPGRADE_LINK                = "https://pEp.software";
    1.47 +        public const string PEP_WEBSITE_READER_LINK                 = "https://pEp.security/reader";
    1.48  
    1.49  #if READER_RELEASE_MODE
    1.50          public const ReleaseMode RELEASE_MODE = ReleaseMode.Reader;
    1.51 @@ -94,10 +97,11 @@
    1.52              Outlook2016 = 16
    1.53          }
    1.54  
    1.55 -        private static bool                 eventsAreConnected    = false;
    1.56 -        private static ResourceDictionary   _ResourceDict         = null;
    1.57 -        private static Version              _OutlookVersion       = Version.Undefined;
    1.58 -        private static char                 _ListDelimiter        = char.MinValue;
    1.59 +        private static bool                 eventsAreConnected      = false;
    1.60 +        private static ResourceDictionary   _ResourceDict           = null;
    1.61 +        private static Version              _OutlookVersion         = Version.Undefined;
    1.62 +        private static char                 _ListDelimiter          = char.MinValue;
    1.63 +        private static string               _PEPUserFolder          = null;
    1.64  
    1.65          /**************************************************************
    1.66           * 
    1.67 @@ -157,6 +161,44 @@
    1.68              }
    1.69          }
    1.70  
    1.71 +        /// <summary>
    1.72 +        /// Gets the folder where pEp saves and loads user files to/from.
    1.73 +        /// </summary>
    1.74 +        public static string PEPUserFolder
    1.75 +        {
    1.76 +            get
    1.77 +            {
    1.78 +                if (Globals._PEPUserFolder == null)
    1.79 +                {
    1.80 +                    string customFolder = null;
    1.81 +                    string defaultFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Globals.PEP_FOLDER_NAME);
    1.82 +
    1.83 +                    try
    1.84 +                    {
    1.85 +                        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Path.Combine("Software", "pEp")))
    1.86 +                        {
    1.87 +                            if (key?.GetValue(Globals.PEP_REG_NAME_USER_FOLDER) is string userFolder)
    1.88 +                            {
    1.89 +                                customFolder = Environment.ExpandEnvironmentVariables(userFolder);
    1.90 +                            }
    1.91 +                        }
    1.92 +                    }
    1.93 +                    catch (Exception ex)
    1.94 +                    {
    1.95 +                        customFolder = null;
    1.96 +
    1.97 +                        // Note: At this point, the pEp log might not yet be available
    1.98 +                        Debug.WriteLine("PEPUserFolder: Error getting folder. " + ex.ToString());
    1.99 +                    }
   1.100 +
   1.101 +                    Globals._PEPUserFolder = customFolder ?? defaultFolder;
   1.102 +                    Log.Verbose("PEPUserFolder: pEp user folder is " + Globals._PEPUserFolder);
   1.103 +                }
   1.104 +
   1.105 +                return Globals._PEPUserFolder;
   1.106 +            }
   1.107 +        }
   1.108 +
   1.109          /**************************************************************
   1.110           * 
   1.111           * Methods
     2.1 --- a/Log.cs	Wed Jun 12 15:54:47 2019 +0200
     2.2 +++ b/Log.cs	Mon Jun 17 14:09:26 2019 +0200
     2.3 @@ -28,7 +28,7 @@
     2.4          /// <summary>
     2.5          /// Gets the path of the log file.
     2.6          /// </summary>
     2.7 -        private static readonly string logFilePath = Path.Combine(Globals.ThisAddIn.Settings.LogsPath, Log.LOG_FILE_NAME);
     2.8 +        private static readonly string logFilePath = Path.Combine(Globals.PEPUserFolder, Log.LOG_FILE_NAME);
     2.9  
    2.10          /// <summary>
    2.11          /// Adds the given info text to the pEp for Outlook log.
    2.12 @@ -291,13 +291,13 @@
    2.13              // Create archive file with current date and timestamp
    2.14              try
    2.15              {
    2.16 -                archiveFile = Path.Combine(Globals.ThisAddIn.Settings.LogsPath, LOG_FILE_ARCHIVE_PREFIX);
    2.17 +                archiveFile = Path.Combine(Globals.PEPUserFolder, LOG_FILE_ARCHIVE_PREFIX);
    2.18                  archiveFile += DateTime.Now.ToString(LOG_FILE_DATE_FORMAT);
    2.19                  archiveFile += ".txt";
    2.20  
    2.21                  while (File.Exists(archiveFile))
    2.22                  {
    2.23 -                    archiveFile = Path.Combine(Globals.ThisAddIn.Settings.LogsPath, LOG_FILE_ARCHIVE_PREFIX);
    2.24 +                    archiveFile = Path.Combine(Globals.PEPUserFolder, LOG_FILE_ARCHIVE_PREFIX);
    2.25                      archiveFile += DateTime.Now.ToString(LOG_FILE_DATE_FORMAT);
    2.26                      archiveFile += ".txt";
    2.27  
     3.1 --- a/PEPDatabase.cs	Wed Jun 12 15:54:47 2019 +0200
     3.2 +++ b/PEPDatabase.cs	Mon Jun 17 14:09:26 2019 +0200
     3.3 @@ -221,11 +221,12 @@
     3.4          /// <returns>An open SQLiteConnection or null if an error occured.</returns>
     3.5          private static SQLiteConnection OpenPEPDatabase()
     3.6          {
     3.7 -            string dbPath = Globals.ThisAddIn.Settings.PEPDbPath;
     3.8              SQLiteConnection dbConnection = null;
     3.9  
    3.10              try
    3.11              {
    3.12 +                string dbPath = Path.Combine(Globals.PEPUserFolder, PEPDatabase.PEP_DB_NAME);
    3.13 +                
    3.14                  // If database does not exist yet, create it
    3.15                  if (File.Exists(dbPath) == false)
    3.16                  {
     4.1 --- a/PEPSettings.cs	Wed Jun 12 15:54:47 2019 +0200
     4.2 +++ b/PEPSettings.cs	Mon Jun 17 14:09:26 2019 +0200
     4.3 @@ -53,10 +53,6 @@
     4.4          public const string REG_NAME_ACCOUNT_WHITELIST                          = "AccountWhitelist";
     4.5          public const string REG_NAME_CIPHER_SUITE                               = "CipherSuite";
     4.6          public const string REG_NAME_CRASH_REPORT_SEND_ADDRESS                  = "CrashReportSendAddress";
     4.7 -        public const string REG_NAME_CUSTOM_PEP_FOLDER_PATH                     = "CustomPEPFolderPath";
     4.8 -        public const string REG_NAME_CUSTOM_PEP_PATH_LOGS                       = "CustomPEPPathLogs";
     4.9 -        public const string REG_NAME_CUSTOM_PEP_PATH_PEP_DB                     = "CustomPEPPathPEPDb";
    4.10 -        public const string REG_NAME_CUSTOM_PEP_PATH_PEP_PST                    = "CustomPEPPathPEPPst";
    4.11          public const string REG_NAME_DEFAULT_CODEPAGE_OUT                       = "Default_CodePageOut"; // Name defined by Microsoft
    4.12          public const string REG_NAME_EXTRA_KEYS                                 = "ExtraKeys";
    4.13          public const string REG_NAME_HIDE_INTERNAL_MESSAGES                     = "HideInternalMessages";
    4.14 @@ -119,7 +115,6 @@
    4.15          public const bool                  IS_UNENCRYPTED_SUBJECT_ENABLED_DEFAULT             = true;
    4.16          public const bool                  IS_UX_IMPROVEMENT_ENABLED_DEFAULT                  = true;
    4.17          public const bool                  IS_VERBOSE_LOGGING_ENABLED_DEFAULT                 = false;
    4.18 -        public static string               PEP_FOLDER_PATH_DEFAULT                            = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp");
    4.19  
    4.20          private ObservableCollection<PEPAccountSettings> _AccountSettingsList;
    4.21          private string[]                                 _AccountWhitelist;
    4.22 @@ -145,9 +140,6 @@
    4.23          private bool                                     _IsUnencryptedSubjectEnabled;
    4.24          private bool                                     _IsUXImprovementEnabled;
    4.25          private bool                                     _IsVerboseLoggingEnabled;
    4.26 -        private string                                   _LogsPath;
    4.27 -        private string                                   _PEPDbPath;
    4.28 -        private string                                   _PEPPstPath;
    4.29          private CultureInfo                              _TrustwordsCulture;
    4.30  
    4.31          /**************************************************************
    4.32 @@ -529,56 +521,6 @@
    4.33          }
    4.34  
    4.35          /// <summary>
    4.36 -        /// Get or sets the path to the logs.
    4.37 -        /// </summary>
    4.38 -        public string LogsPath
    4.39 -        {
    4.40 -            get
    4.41 -            {
    4.42 -                return this._LogsPath ?? this.PEPFolderPath;
    4.43 -            }
    4.44 -            set
    4.45 -            {
    4.46 -                this._LogsPath = value;
    4.47 -            }
    4.48 -        }
    4.49 -
    4.50 -        /// <summary>
    4.51 -        /// Gets or sets the path to the pEp.db.
    4.52 -        /// </summary>
    4.53 -        public string PEPDbPath
    4.54 -        {
    4.55 -            get
    4.56 -            {
    4.57 -                return this.PEPDbPath ?? this.PEPFolderPath;
    4.58 -            }
    4.59 -            set
    4.60 -            {
    4.61 -                this._PEPDbPath = value;
    4.62 -            }
    4.63 -        }
    4.64 -
    4.65 -        /// <summary>
    4.66 -        /// Gets or sets the path to the pEp folder.
    4.67 -        /// </summary>
    4.68 -        public string PEPFolderPath { get; private set; }
    4.69 -
    4.70 -        /// <summary>
    4.71 -        /// Gets or sets the path to the pEp.pst.
    4.72 -        /// </summary>
    4.73 -        public string PEPPstPath
    4.74 -        {
    4.75 -            get
    4.76 -            {
    4.77 -                return this._PEPPstPath ?? this.PEPFolderPath;
    4.78 -            }
    4.79 -            set
    4.80 -            {
    4.81 -                this._PEPPstPath = value;
    4.82 -            }
    4.83 -        }
    4.84 -
    4.85 -        /// <summary>
    4.86          /// Gets or sets the culture to display the trustwords in.
    4.87          /// </summary>
    4.88          public CultureInfo TrustwordsCulture
    4.89 @@ -652,10 +594,6 @@
    4.90              this._IsUnencryptedSubjectEnabled = PEPSettings.IS_UNENCRYPTED_SUBJECT_ENABLED_DEFAULT;
    4.91              this._IsUXImprovementEnabled = PEPSettings.IS_UX_IMPROVEMENT_ENABLED_DEFAULT;
    4.92              this._IsVerboseLoggingEnabled = PEPSettings.IS_VERBOSE_LOGGING_ENABLED_DEFAULT;
    4.93 -            this._LogsPath = null;
    4.94 -            this._PEPDbPath = null;
    4.95 -            this.PEPFolderPath = PEPSettings.PEP_FOLDER_PATH_DEFAULT;
    4.96 -            this._PEPPstPath = null;
    4.97              this._TrustwordsCulture = null; // Allow application to decide since default is UI language
    4.98  
    4.99              // Connect events
   4.100 @@ -816,10 +754,6 @@
   4.101                  Comparisons.Equals(this.IsUnencryptedSubjectEnabled, obj.IsUnencryptedSubjectEnabled) &&
   4.102                  Comparisons.Equals(this.IsUXImprovementEnabled, obj.IsUXImprovementEnabled) &&
   4.103                  Comparisons.Equals(this.IsVerboseLoggingEnabled, obj.IsVerboseLoggingEnabled) &&
   4.104 -                Comparisons.Equals(this.LogsPath, obj.LogsPath) &&
   4.105 -                Comparisons.Equals(this.PEPDbPath, obj.PEPDbPath) &&
   4.106 -                Comparisons.Equals(this.PEPFolderPath, obj.PEPFolderPath) &&
   4.107 -                Comparisons.Equals(this.PEPPstPath, obj.PEPPstPath) &&
   4.108                  Comparisons.Equals(this.TrustwordsCulture, obj.TrustwordsCulture))
   4.109              {
   4.110                  // Check if the number of accounts matches
   4.111 @@ -871,12 +805,6 @@
   4.112               *   • IsSyncQueueEnabled
   4.113               *   • IsDisableProtectionForContactsEnabled
   4.114               *   • IsTNEFDisabled
   4.115 -             *   • KeysDbPath
   4.116 -             *   • LogsPath
   4.117 -             *   • ManagementDbPath
   4.118 -             *   • PEPDbPath
   4.119 -             *   • PEPFolderPath
   4.120 -             *   • PEPPstPath
   4.121               */
   4.122  
   4.123              string pEpForOutlookRegKey = Path.Combine(PEPSettings.REG_KEY_SOFTWARE, PEPSettings.REG_KEY_PEP, PEPSettings.REG_KEY_PEP_FOR_OUTLOOK);
   4.124 @@ -1947,62 +1875,6 @@
   4.125                              this.RaisePropertyChangedEvent(nameof(this.IsVerboseLoggingEnabled));
   4.126                          }
   4.127  
   4.128 -                        // Attempt to get CustomPEPFolderPath
   4.129 -                        try
   4.130 -                        {
   4.131 -                            if (pEpKey.GetValue(PEPSettings.REG_NAME_CUSTOM_PEP_FOLDER_PATH) is string pEpFolderPath)
   4.132 -                            {                                
   4.133 -                                this.PEPFolderPath = Environment.ExpandEnvironmentVariables(pEpFolderPath);
   4.134 -                            }
   4.135 -                        }
   4.136 -                        catch (Exception ex)
   4.137 -                        {
   4.138 -                            this.PEPFolderPath = PEPSettings.PEP_FOLDER_PATH_DEFAULT;
   4.139 -                            Log.Warning("LoadFromRegistry: Error getting pEp folder path. " + ex.ToString());
   4.140 -                        }
   4.141 -
   4.142 -                        // Attempt to get CustomPEPPathLogs
   4.143 -                        try
   4.144 -                        {
   4.145 -                            if (pEpKey.GetValue(PEPSettings.REG_NAME_CUSTOM_PEP_PATH_LOGS) is string logsPath)
   4.146 -                            {
   4.147 -                                this.LogsPath = Environment.ExpandEnvironmentVariables(logsPath); 
   4.148 -                            }
   4.149 -                        }
   4.150 -                        catch (Exception ex)
   4.151 -                        {
   4.152 -                            this.LogsPath = null;
   4.153 -                            Log.Warning("LoadFromRegistry: Error getting logs path. " + ex.ToString());
   4.154 -                        }
   4.155 -
   4.156 -                        // Attempt to get CustomPEPPathPEPDb
   4.157 -                        try
   4.158 -                        {
   4.159 -                            if (pEpKey.GetValue(PEPSettings.REG_NAME_CUSTOM_PEP_PATH_PEP_DB) is string pEpDbPath)
   4.160 -                            {
   4.161 -                                this.PEPDbPath = Environment.ExpandEnvironmentVariables(pEpDbPath);
   4.162 -                            }
   4.163 -                        }
   4.164 -                        catch (Exception ex)
   4.165 -                        {
   4.166 -                            this.PEPDbPath = null;
   4.167 -                            Log.Warning("LoadFromRegistry: Error getting pEp.db path. " + ex.ToString());
   4.168 -                        }
   4.169 -
   4.170 -                        // Attempt to get CustomPEPPathPEPPst
   4.171 -                        try
   4.172 -                        {
   4.173 -                            if (pEpKey.GetValue(PEPSettings.REG_NAME_CUSTOM_PEP_PATH_PEP_PST) is string pEpPstPath)
   4.174 -                            {
   4.175 -                                this.PEPPstPath = Environment.ExpandEnvironmentVariables(pEpPstPath);
   4.176 -                            }
   4.177 -                        }
   4.178 -                        catch (Exception ex)
   4.179 -                        {
   4.180 -                            this.PEPPstPath = null;
   4.181 -                            Log.Warning("LoadFromRegistry: Error getting pEp.pst path. " + ex.ToString());
   4.182 -                        }
   4.183 -
   4.184                          // Attempt to get TrustwordsCulture value
   4.185                          isPropertySet = false;
   4.186                          try
     5.1 --- a/ThisAddIn.cs	Wed Jun 12 15:54:47 2019 +0200
     5.2 +++ b/ThisAddIn.cs	Mon Jun 17 14:09:26 2019 +0200
     5.3 @@ -1559,7 +1559,7 @@
     5.4              try
     5.5              {
     5.6                  // The path to store the pEp store to
     5.7 -                string path = Path.Combine(this.Settings.PEPPstPath, (Globals.PEP_DATA_FILE_NAME + ".pst"));
     5.8 +                string path = Path.Combine(Globals.PEPUserFolder, (Globals.PEP_DATA_FILE_NAME + ".pst"));
     5.9  
    5.10                  /* For Outlook installations from the Windows Trusted Store the AddStoreEx function to add a store seems to
    5.11                   * convert "%LOCALAPPDATA%" to "%LOCALAPPDATA\Packages\Microsoft.Office.Desktop_8wekyb3d8bbwe\LocalCache\Local\".
    5.12 @@ -2684,10 +2684,6 @@
    5.13          {
    5.14              CultureInfo culture;
    5.15  
    5.16 -            // Load settings from Registry. This has to be done first.
    5.17 -            this._Settings = new PEPSettings();
    5.18 -            this._Settings.LoadFromRegistry();
    5.19 -
    5.20              // Disable GPGOL
    5.21              Log.Info("ThisAddIn_Startup: Disable GPGOL");
    5.22              this.DisableGpgOL();
    5.23 @@ -2704,6 +2700,8 @@
    5.24               * to be able to set the not for sync flag if necessary.
    5.25               */
    5.26              Log.Info("ThisAddIn_Startup: Loading settings");
    5.27 +            this._Settings = new PEPSettings();
    5.28 +            this._Settings.LoadFromRegistry();
    5.29              this.SyncAccountsList();
    5.30  
    5.31  #if !READER_RELEASE_MODE