OutlookOptions.cs
author Thomas
Mon, 16 Jul 2018 12:25:29 +0200
changeset 2295 e64498e9af92
parent 2073 7580679c7322
child 3241 7308e78e3fd3
permissions -rw-r--r--
OUT-477: Check in Registry if S/MIME is enabled.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     1
using Microsoft.Win32;
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     2
using System;
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
     3
using System.IO;
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     4
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     5
namespace pEp
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     6
{
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     7
    /// <summary>
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     8
    /// Class to store the Outlook options set in the current instance.
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
     9
    /// </summary>
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    10
    internal class OutlookOptions
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    11
    {
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    12
        /// <summary>
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    13
        /// The Office version as string to be used in Registry paths.
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    14
        /// 
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    15
        /// Office 2010 => "14.0"
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    16
        /// Office 2013 => "15.0"
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    17
        /// Office 2016 => "16.0"
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    18
        /// </summary>
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    19
        private readonly string officeVersion = Globals.ThisAddIn.Application.Version.Substring(0, 2) + ".0";
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    20
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    21
        /// <summary>
2295
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    22
        /// Gets whether S/MIME encryption or signatures are enabled.
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    23
        /// Note: The registry value that is set when enabling S/MIME won't get unset or changed
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    24
        ///       again after disabling it. So this might yield a false positive.
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    25
        /// </summary>
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    26
        public bool IsSMIMEEnabled { get; set; } = false;
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    27
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    28
        /// <summary>
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    29
        /// Read email messages in plain text
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    30
        /// See: https://support.office.com/en-us/article/Read-email-messages-in-plain-text-2443DA66-4AB8-451E-98A8-489A58D00A40
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    31
        /// </summary>
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    32
        public bool ReadAsPlain { get; set; } = false;
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    33
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    34
        /// <summary>
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    35
        /// Reads all Outlook options from registry and sets the respective values.
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    36
        /// </summary>
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    37
        public void ReadOptionsFromRegistry()
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    38
        {
2295
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    39
            // Path to Outlook
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    40
            string registryPathOutlook = Path.Combine("Software", "Microsoft", "Office", officeVersion, "Outlook");
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    41
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    42
            // Path to Security settings
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    43
            string registryPathSecurity = Path.Combine(registryPathOutlook, "Security");
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    44
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    45
            // Path to Outlook options
2295
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    46
            string registryPathOptions = Path.Combine(registryPathOutlook, "Options");
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    47
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    48
            // Path to Mail options
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    49
            string registryPathMail = Path.Combine(registryPathOptions, "Mail");
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    50
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    51
            // Get value for Read as Plain Text option
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    52
            try
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    53
            {
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    54
                using (RegistryKey mailOptions = Registry.CurrentUser.OpenSubKey(registryPathMail))
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    55
                {
2295
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    56
                    this.ReadAsPlain = ((mailOptions?.GetValue(nameof(ReadAsPlain)) as int?) == 1);
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    57
                }
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    58
            }
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    59
            catch (Exception ex)
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    60
            {
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    61
                this.ReadAsPlain = false;
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    62
                Log.Error("OutlookOptions.ReadOptionsFromRegistry: Error getting ReadAsPlain value. " + ex.ToString());
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    63
            }
2295
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    64
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    65
            // Get whether an S/MIME option is enabled
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    66
            try
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    67
            {
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    68
                using (RegistryKey securityOptions = Registry.CurrentUser.OpenSubKey(registryPathSecurity))
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    69
                {
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    70
                    /* If S/MIME encryption or signature is enabled by default through the Outlook UI,
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    71
                     * the Registry values "InitEncrypt" and/or InitSign" are being added to 
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    72
                     * HKCU\Software\Microsoft\Office\<version no.>\Outlook\Security and set to 2 (enabled).
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    73
                     * However, if they are being disabled again through the UI, those values remain with
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    74
                     * the same value (at least in some environments). The following condition is therefore prone
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    75
                     * to yield false positives. Until a better solution is found, we will use this approximation.
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    76
                     */ 
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    77
                    this.IsSMIMEEnabled = (((securityOptions?.GetValue("InitEncrypt") as int?) == 2) ||
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    78
                                           ((securityOptions?.GetValue("InitSign") as int?) == 2));
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    79
                }
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    80
            }
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    81
            catch (Exception ex)
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    82
            {
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    83
                this.IsSMIMEEnabled = false;
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    84
                Log.Error("OutlookOptions.ReadOptionsFromRegistry: Error getting IsSMIMEEnabled value. " + ex.ToString());
e64498e9af92 OUT-477: Check in Registry if S/MIME is enabled.
Thomas
parents: 2073
diff changeset
    85
            }
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
    86
        }
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    87
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
    88
        /// <summary>
2073
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    89
        /// Registers the FormRegionPrivacyStatus if necessary.
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    90
        /// </summary>
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    91
        private void RegisterFormRegion()
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    92
        {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    93
            string formRegionsPath = Path.Combine("Software", "Microsoft", "Office", "Outlook", "FormRegions", Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note);
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    94
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    95
            // Check if FormRegionPrivacyStatus is registered in LocalMachine scope
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    96
            bool isRegisteredInLM = false;
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    97
            try
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    98
            {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
    99
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(formRegionsPath))
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   100
                {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   101
                    isRegisteredInLM = (key?.GetValue("pEp.FormRegionPrivacyStatus") != null);
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   102
                }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   103
            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   104
            catch (Exception ex)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   105
            {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   106
                Log.Error("RegisterFormRegions: Error during form region check. " + ex.ToString());
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   107
            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   108
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   109
            // If registered in in LocalMachine scope, delete in CurrentUser scope if available
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   110
            if (isRegisteredInLM)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   111
            {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   112
                try
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   113
                {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   114
                    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(formRegionsPath, true))
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   115
                    {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   116
                        if (key != null)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   117
                        {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   118
                            try
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   119
                            {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   120
                                key.DeleteValue("pEp.FormRegionPrivacyStatus");
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   121
                                Log.Info("RegisterFormRegions: Deleted FormRegionPrivacyStatus form region entry in HKCU scope.");
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   122
                            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   123
                            catch (Exception ex)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   124
                            {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   125
                                Log.Error("RegisterFormRegions: Error deleting form region key. " + ex.ToString());
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   126
                            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   127
                        }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   128
                    }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   129
                }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   130
                catch (Exception ex)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   131
                {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   132
                    Log.Error("RegisterFormRegions: Error during form region check. " + ex.ToString());
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   133
                }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   134
            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   135
        }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   136
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   137
        /// <summary>
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   138
        /// In order to block JavaScript in unencrypted preview (WPF WebBrowser) set "Local machine zone lockdown" for Outlook to 1
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   139
        /// https://social.msdn.microsoft.com/Forums/vstudio/en-US/f73bb9fa-3252-4c7e-b8e4-6cdad59fd825/wpf-webbrowser-control-allow-blocked-content-setting?forum=wpf
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   140
        /// https://blogs.msdn.microsoft.com/ieinternals/2011/03/23/understanding-local-machine-zone-lockdown/
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   141
        /// </summary>
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   142
        private void SetLocalMachineLockdown()
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   143
        {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   144
            const string  outlookValue = "outlook.exe";
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   145
            string localMachineLockdownPath = Path.Combine("Software", "Microsoft", "Internet Explorer", "Main", "FeatureControl", "FEATURE_LOCALMACHINE_LOCKDOWN");
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   146
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   147
            try
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   148
            {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   149
                using (RegistryKey localMachineLockdownKey = Registry.CurrentUser.CreateSubKey(localMachineLockdownPath))
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   150
                {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   151
                    // Set value unless it already exists     
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   152
                    var value = localMachineLockdownKey?.GetValue(outlookValue);
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   153
                    if (value == null)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   154
                    {
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   155
                        localMachineLockdownKey.SetValue(outlookValue, 1, RegistryValueKind.DWord);
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   156
                        Log.Verbose("OutlookOptions.SetLocalMachineLockdown: Error getting LOCALMACHINE_LOCKDOWN Outlook value.");
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   157
                    }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   158
                }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   159
            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   160
            catch (Exception ex)
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   161
            {                
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   162
                Log.Error("OutlookOptions.SetLocalMachineLockdown: Error getting LOCALMACHINE_LOCKDOWN Outlook value. " + ex.ToString());
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   163
            }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   164
        }
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   165
7580679c7322 Implement warning message, reroute link clicks, some simplification
Thomas
parents: 2067
diff changeset
   166
        /// <summary>
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   167
        /// Sets all Registry values that are needed by pEp.
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   168
        /// </summary>
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   169
        public void SetRegistryValues()
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   170
        {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   171
            // Set the default codepage
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   172
            this.SetDefaultCodepageOut();
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   173
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   174
            // Set search in all mailboxes
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   175
            this.SetSearchInAllMailboxes();
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   176
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   177
            // Register the FormRegionPrivacyStatus
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   178
            this.RegisterFormRegion();
2066
e7d76f8725b5 OUT-369: Set LOCALMACHINE_LOCKDOWN for Outlook in Registry
nikolaj
parents: 1986
diff changeset
   179
e7d76f8725b5 OUT-369: Set LOCALMACHINE_LOCKDOWN for Outlook in Registry
nikolaj
parents: 1986
diff changeset
   180
            // set LOCALMACHINE_LOCKDOWN for Outlook
e7d76f8725b5 OUT-369: Set LOCALMACHINE_LOCKDOWN for Outlook in Registry
nikolaj
parents: 1986
diff changeset
   181
            this.SetLocalMachineLockdown();
e7d76f8725b5 OUT-369: Set LOCALMACHINE_LOCKDOWN for Outlook in Registry
nikolaj
parents: 1986
diff changeset
   182
        }
e7d76f8725b5 OUT-369: Set LOCALMACHINE_LOCKDOWN for Outlook in Registry
nikolaj
parents: 1986
diff changeset
   183
e7d76f8725b5 OUT-369: Set LOCALMACHINE_LOCKDOWN for Outlook in Registry
nikolaj
parents: 1986
diff changeset
   184
        /// <summary>
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   185
        /// Set Outlook to search in all mailboxes if no user setting exists.
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   186
        /// This is used so a quick search finds mirror mail items stored in the
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   187
        /// pEp store.
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   188
        /// </summary>
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   189
        private void SetSearchInAllMailboxes()
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   190
        {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   191
            /* The Registry value that defines the scope in which Outlook performs
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   192
             * searches is named DefaultSearchScope and can have values of:
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   193
             *      • 0 => Default behavior
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   194
             *      • 1 => Search in all mailboxes
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   195
             *      • 2 => Search in current folder
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   196
             *      • 3 => Search in current mailbox
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   197
             *   
1986
a023129358a0 OUT-374: Adjust registry path
Thomas
parents: 1985
diff changeset
   198
             * This value is stored in HKCU\SOFTWARE\Microsoft\Office\[version_no]\Outlook\Search
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   199
             *      
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   200
             * Cf.  https://getadmx.com/?Category=Office2016&Policy=outlk16.Office.Microsoft.Policies.Windows::L_SetDefaultSearchScope
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   201
             *      https://social.technet.microsoft.com/Forums/ie/en-US/3cbf6123-9880-4e68-ac27-f692a2844857/outlook-2103-modify-search-options-via-registry?forum=outlook
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   202
             */
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   203
            const string defaultSearchScope = "DefaultSearchScope";
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   204
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   205
            // Set Registry path according to Outlook version
1986
a023129358a0 OUT-374: Adjust registry path
Thomas
parents: 1985
diff changeset
   206
            string outlookSearchOptionsPath = Path.Combine("Software", "Microsoft", "Office", officeVersion, "Outlook", "Search");
1985
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   207
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   208
            // Check if DefaultSearchScope entry already exists
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   209
            bool setValue = true;
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   210
            try
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   211
            {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   212
                using (RegistryKey searchOptionsKey = Registry.CurrentUser.OpenSubKey(outlookSearchOptionsPath))
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   213
                {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   214
                    // Set value unless it already exists
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   215
                    var searchScopeValue = searchOptionsKey?.GetValue(defaultSearchScope);
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   216
                    if (searchScopeValue != null)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   217
                    {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   218
                        setValue = false;
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   219
                        Log.Verbose("OutlookOptions.SetSearchInAllMailboxes: DefaultSearchScope already exists and is set to " + searchScopeValue.ToString());
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   220
                    }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   221
                }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   222
            }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   223
            catch (Exception ex)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   224
            {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   225
                setValue = true;
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   226
                Log.Error("OutlookOptions.SetSearchInAllMailboxes: Error getting DefaultSearchScope value. " + ex.ToString());
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   227
            }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   228
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   229
            // If entry doesn't exist, set it to 1 (Search in all mailboxes)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   230
            if (setValue)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   231
            {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   232
                try
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   233
                {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   234
                    using (RegistryKey searchOptionsKey = Registry.CurrentUser.CreateSubKey(outlookSearchOptionsPath))
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   235
                    {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   236
                        searchOptionsKey.SetValue(defaultSearchScope, 1, RegistryValueKind.DWord);
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   237
                        Log.Verbose("OutlookOptions.SetSearchInAllMailboxes: DefaultSearchScope successfully set to 1.");
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   238
                    }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   239
                }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   240
                catch (Exception ex)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   241
                {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   242
                    Log.Error("OutlookOptions.SetSearchInAllMailboxes: Error setting DefaultSearchScope. " + ex.ToString());
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   243
                }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   244
            }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   245
        }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   246
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   247
        /// <summary>
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   248
        /// Sets the default codepage for outgoing mails to UTF-8.
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   249
        /// </summary>
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   250
        private void SetDefaultCodepageOut()
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   251
        {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   252
            int codePageUtf8 = 65001;  // UTF-8
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   253
            bool setValue = true;
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   254
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   255
            try
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   256
            {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   257
                string path = Path.Combine("Software", "Microsoft", "Office", officeVersion, "Outlook", "Options", "MSHTML", "International");
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   258
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   259
                // Check if value exists and set it if necessary
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   260
                using (RegistryKey codePageKey = Registry.CurrentUser.OpenSubKey(path))
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   261
                {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   262
                    // Only set it if not already set to UTF-8
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   263
                    var codePage = codePageKey?.GetValue(PEPSettings.REG_NAME_DEFAULT_CODEPAGE_OUT);
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   264
                    setValue = ((codePage as int?) != codePageUtf8);
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   265
                }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   266
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   267
                if (setValue)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   268
                {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   269
                    using (RegistryKey codePageKey = Registry.CurrentUser.CreateSubKey(path))
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   270
                    {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   271
                        codePageKey?.SetValue(PEPSettings.REG_NAME_DEFAULT_CODEPAGE_OUT, codePageUtf8);
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   272
                    }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   273
                }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   274
            }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   275
            catch (Exception ex)
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   276
            {
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   277
                Log.Error("SetDefaultCodepageOut: Failed to set default codepage. " + ex.ToString());
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   278
            }
e91899e7b19f OUT-374: Set search in all mailboxes by default. And some rearrangement.
Thomas
parents: 1948
diff changeset
   279
        }
1948
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
   280
    }
be0dd58ec451 OUT-370: Add OutlookOptions and read them from registry.
Thomas
parents:
diff changeset
   281
}