Wrappers/WatchedWindow.cs
author Thomas
Thu, 06 Sep 2018 09:29:19 +0200
branchOUT-497
changeset 2359 0e6191d039e4
child 2360 f01523e581fa
permissions -rw-r--r--
Major restructuration: - Remove FormRegionPrivacyStatus - Create new WatchedWindow as base class for WatchedExplorers and WatchedInspectors - Move all logic from FormRegionPrivacy to WatchedWindow - Use Explorer.SelectionChanged and Inspectors.Newinspector events instead of FormRegionPrivacyStatus.FormRegionShowing to detect opening of mail item. - Rework of RibbonCustomizations. State of pEp button now stored in WatchedWindow instead of FormRegionPrivacyStatus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2359
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     1
´╗┐using pEp.UI;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     2
using pEpCOMServerAdapterLib;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     3
using System;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     4
using System.ComponentModel;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     5
using System.Windows.Forms;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     6
using Outlook = Microsoft.Office.Interop.Outlook;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     7
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     8
namespace pEp
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
     9
{
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    10
    /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    11
    /// Stores an Outlook Window with connected events.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    12
    /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    13
    public abstract class WatchedWindow : IDisposable
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    14
    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    15
        private CryptableMailItem               cryptableMailItem           = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    16
        private bool                            displayMirrorRequested      = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    17
        private HandshakeDialog                 handshakeDialog             = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    18
        private bool                            isEnabled                   = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    19
        private bool                            isStarted                   = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    20
        private bool                            processingOngoing           = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    21
        private bool                            refreshOngoing              = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    22
        private bool                            repeatProcessing            = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    23
        private int                             repeatCounter               = 0;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    24
        private const int                       maxRepeatCount              = 5;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    25
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    26
        private Outlook.MailItem                _CurrentMailItem            = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    27
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    28
        public enum WindowTypes
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    29
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    30
            Undefined,
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    31
            Inspector,
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    32
            Explorer
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    33
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    34
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    35
        #region Properties
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    36
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    37
        /// The rating of the currently selected message.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    38
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    39
        private pEpRating _Rating = pEpRating.pEpRatingUndefined;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    40
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    41
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    42
        /// The mail item that is connected to this Inspector/Explorer window.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    43
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    44
        public Outlook.MailItem CurrentMailItem
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    45
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    46
            get
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    47
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    48
                return this._CurrentMailItem;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    49
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    50
            protected set
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    51
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    52
                this._CurrentMailItem = value;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    53
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    54
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    55
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    56
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    57
        /// Gets or sets whether to disable the Force Protection option.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    58
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    59
        public bool DisableForceProtection { get; set; } = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    60
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    61
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    62
        /// Gets or sets whether to send this message forcefully protected.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    63
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    64
        public bool ForceProtection { get; set; } = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    65
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    66
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    67
        /// Gets or sets whether to send this message forcefully unencrypted.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    68
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    69
        public bool ForceUnencrypted { get; set; } = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    70
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    71
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    72
        /// Gets or sets whether the message is a draft message.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    73
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    74
        public bool IsDraft { get; set; } = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    75
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    76
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    77
        /// Gets or sets whether to always store this message as if on an untrusted server.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    78
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    79
        public bool NeverUnsecure { get; set; } = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    80
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    81
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    82
        /// Gets the rating of this message.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    83
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    84
        public pEpRating Rating
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    85
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    86
            get { return this._Rating; }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    87
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    88
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    89
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    90
        /// Gets the associated Explorer/Inspector window. To be overwritten by child class.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    91
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    92
        public abstract dynamic Window { get; }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    93
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    94
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    95
        /// Sets the rating of this message and updates the UI.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    96
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    97
        /// <param name="rating">The message rating.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    98
        public void SetRating(pEpRating rating)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
    99
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   100
            this._Rating = rating;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   101
            RibbonCustomizations.Invalidate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   102
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   103
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   104
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   105
        /// The timer that schedules the updates of this window.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   106
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   107
        public System.Windows.Forms.Timer TimerRefresh { get; set; } = new System.Windows.Forms.Timer();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   108
        #endregion
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   109
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   110
        #region Constructors / Destructors
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   111
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   112
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   113
        /// Destructor.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   114
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   115
        ~WatchedWindow()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   116
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   117
            this.Dispose(true);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   118
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   119
        #endregion
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   120
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   121
        #region Methods
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   122
        /**************************************************************
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   123
         * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   124
         * Methods
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   125
         * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   126
         *************************************************************/
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   127
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   128
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   129
        /// Releases all resources and disconnects internal events.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   130
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   131
        public void Dispose()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   132
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   133
            this.Dispose(true);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   134
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   135
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   136
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   137
        /// Clean up any resources being used.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   138
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   139
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   140
        protected virtual void Dispose(bool disposing)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   141
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   142
            if (disposing)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   143
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   144
                // Set Outlook objects to null
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   145
                this._CurrentMailItem = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   146
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   147
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   148
              
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   149
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   150
        /// Builds the latest state of the encryption status manager then shows the UI.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   151
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   152
        public void BuildAndShowManager()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   153
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   154
            /* Resolve all recipients -- this ensures the identities list is correctly populated
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   155
             * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   156
             * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   157
             * This is because the resolve process can modify the contents of the mail item which triggers an event.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   158
             * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   159
             * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   160
             * cannot be resolved (no address).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   161
             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   162
            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   163
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   164
                this.ResolveAllRecipients();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   165
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   166
            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   167
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   168
                Log.Verbose("BuildAndShowManager: Error resolving recipients. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   169
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   170
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   171
            // Build the dialog
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   172
            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   173
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   174
                // The PEPMessage to build the dialog with
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   175
                PEPMessage message = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   176
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   177
                // The own identity to build the dialog with
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   178
                PEPIdentity myself = this.cryptableMailItem?.Myself;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   179
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   180
                // If message is a draft, create it directly from the Outlook mail item
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   181
                if (this.IsDraft)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   182
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   183
                    Log.Verbose("BuildAndShowManager: Creating PEPMessage from draft.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   184
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   185
                    if (PEPMessage.Create(this._CurrentMailItem, out message) != Globals.ReturnStatus.Success)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   186
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   187
                        Log.Error("BuildAndShowManager: Error creating PEPMessage from draft.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   188
                        message = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   189
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   190
                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   191
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   192
                        // Calculate rating
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   193
                        message.Rating = message.GetOutgoingRating();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   194
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   195
                        // If Force Protection is set, assign a random GUID
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   196
                        if ((this.ForceProtection) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   197
                            (this.DisableForceProtection == false))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   198
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   199
                            message.ForceProtectionId = Guid.NewGuid().ToString();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   200
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   201
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   202
                        // If message is Force Unencrypted, assign it
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   203
                        message.ForceUnencrypted = this.ForceUnencrypted;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   204
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   205
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   206
                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   207
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   208
                    // Else, use either the cryptable mail item's associated mirror or message
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   209
                    message = this.cryptableMailItem?.Mirror ?? this.cryptableMailItem?.Message;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   210
                    Log.Verbose(string.Format("BuildAndShowManager: Message {0} retrieved from CryptableMailItem", ((message != null) ? "successfully" : "could not be")));
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   211
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   212
                    // As fallback, if we don't have a message yet, create it
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   213
                    if (message == null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   214
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   215
                        Log.Verbose("BuildAndShowManager: Using fallback method to get message.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   216
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   217
                        Outlook.MailItem mirror = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   218
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   219
                        try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   220
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   221
                            // For securely stored mails, try to look up mirror
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   222
                            if (this._CurrentMailItem?.GetIsSecurelyStored() == true)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   223
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   224
                                mirror = this._CurrentMailItem?.GetMirror();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   225
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   226
                                if (mirror != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   227
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   228
                                    // If mirror is found, use it to create PEPMessage
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   229
                                    Log.Verbose("BuildAndShowManager: Mirror found.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   230
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   231
                                    if (PEPMessage.Create(mirror, out message) != Globals.ReturnStatus.Success)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   232
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   233
                                        message = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   234
                                        Log.Error("BuildAndShowManager: Error creating PEPMessage.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   235
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   236
                                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   237
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   238
                                        message.Rating = AdapterExtensions.ReevaluateMessageRating(message);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   239
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   240
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   241
                                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   242
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   243
                                    // If no mirror is found, decrypt message and use decrypted one (do not 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   244
                                    // decrypt forcefully protected messages).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   245
                                    if ((PEPMessage.Create(this._CurrentMailItem, out message) == Globals.ReturnStatus.Success) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   246
                                        (string.IsNullOrEmpty(message.ForceProtectionId)))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   247
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   248
                                        var msgProcessor = new MsgProcessor();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   249
                                        PEPMessage decryptedMsg = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   250
                                        string[] keyList;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   251
                                        pEpDecryptFlags flags = pEpDecryptFlags.pEpDecryptFlagsNone;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   252
                                        pEpRating rating = msgProcessor.Decrypt(ref message, out decryptedMsg, out keyList, ref flags);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   253
                                        if (decryptedMsg != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   254
                                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   255
                                            message = decryptedMsg;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   256
                                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   257
                                        message.Rating = rating;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   258
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   259
                                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   260
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   261
                                        message = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   262
                                        Log.Error("BuildAndShowManager: Error creating PEPMessage from mirror.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   263
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   264
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   265
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   266
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   267
                            // If we don't have a PEPMessage yet, create it
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   268
                            if (message == null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   269
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   270
                                Log.Verbose("BuildAndShowManager: Creating PEPMessage.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   271
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   272
                                if (PEPMessage.Create(this._CurrentMailItem, out message) != Globals.ReturnStatus.Success)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   273
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   274
                                    message = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   275
                                    Log.Error("BuildAndShowManager: Error creating PEPMessage.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   276
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   277
                                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   278
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   279
                                    message.Rating = AdapterExtensions.ReevaluateMessageRating(message);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   280
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   281
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   282
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   283
                            // Get myself identiy if we don't have it yet
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   284
                            if (myself == null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   285
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   286
                                myself = this._CurrentMailItem.GetMyselfIdentity();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   287
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   288
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   289
                        catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   290
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   291
                            Log.Error("FormRegionPrivacyStatus.Message: Error converting message. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   292
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   293
                        finally
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   294
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   295
                            mirror = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   296
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   297
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   298
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   299
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   300
                // Build dialog
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   301
                if (message != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   302
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   303
                    handshakeDialog = new HandshakeDialog(message, myself, this.IsDraft);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   304
                    handshakeDialog.OnUpdateStatus += HandshakeDialog_Updated;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   305
                    handshakeDialog.Closed += HandshakeDialog_Closed;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   306
                    handshakeDialog.Show();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   307
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   308
                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   309
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   310
                    throw new Exception("Could not build handshake dialog. Message is null.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   311
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   312
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   313
            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   314
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   315
                Globals.StopAndSendCrashReport(ex);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   316
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   317
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   318
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   319
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   320
        /// Schedules for the pEp rating and UI (including displayed mirror) to be updated.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   321
        /// This can be called many times with no issue as the update is only run every n milliseconds.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   322
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   323
        public void RequestRatingAndUIUpdate()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   324
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   325
            if (this.isEnabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   326
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   327
                this.TimerRefresh.Enabled = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   328
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   329
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   330
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   331
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   332
        /// Immediately starts the update of UI rating based on the associated mail item.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   333
        /// This will start decryption as necessary and also will update the displayed mirror.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   334
        /// This method by-passes the refresh timer completely.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   335
        /// WARNING: This method assumes the message is fully downloaded already.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   336
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   337
        private void ImmediateRatingAndUIUpdate()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   338
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   339
            if ((this.isEnabled) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   340
                (this.cryptableMailItem != null))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   341
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   342
                Log.Verbose("ImmediateRatingAndUIUpdate: Starting processing.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   343
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   344
                // Start the rating calculation/decryption process
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   345
                this.cryptableMailItem.StartProcessing();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   346
                this.processingOngoing = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   347
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   348
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   349
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   350
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   351
        /// Resets the Enabled status of this form region and recalculates the rating if necessary.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   352
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   353
        /// <param name="enable">Whether to enable the form region.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   354
        public void UpdateFormRegion(bool enable)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   355
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   356
            this.SetIsEnabled(enable);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   357
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   358
            // Refresh the UI
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   359
            if (enable)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   360
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   361
                this.ResolveAllRecipients();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   362
                this.RequestRatingAndUIUpdate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   363
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   364
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   365
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   366
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   367
        /// Workaround method to update the current inspector window. This is done by moving the mail item
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   368
        /// to a temporary folder first and then back to the current folder. Both folders CANNOT be the same.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   369
        /// As a fallback, the mail item stays in the temporary folder if moving back to the current folder
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   370
        /// fails.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   371
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   372
        private void UpdateInspector()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   373
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   374
            Outlook.Application application = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   375
            Outlook.Folder currentFolder = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   376
            Outlook.Folder tempFolder = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   377
            Outlook.Inspector currentInspector = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   378
            Outlook.Inspector newInspector = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   379
            Outlook.MailItem tempMailItem = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   380
            Outlook.Store store = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   381
            Outlook.MailItem omi = this.CurrentMailItem;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   382
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   383
            if (omi != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   384
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   385
                try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   386
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   387
                    application = omi.Application;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   388
                    currentInspector = omi.GetInspector;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   389
                    currentFolder = omi.Parent as Outlook.Folder;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   390
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   391
                    if ((currentInspector != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   392
                        (application != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   393
                        (currentFolder != null))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   394
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   395
                        var left = currentInspector.Left;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   396
                        var top = currentInspector.Top;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   397
                        var width = currentInspector.Width;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   398
                        var height = currentInspector.Height;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   399
                        var windowState = currentInspector.WindowState;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   400
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   401
                        /* Check, if in trusted store. In that case, use the default drafts folder
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   402
                         * as temporary folder. If the store is untrusted, use the pEp drafts folder.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   403
                         */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   404
                        store = currentFolder.Store;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   405
                        if (store?.GetIsSecureStorageEnabled() ?? false)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   406
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   407
                            tempFolder = Globals.ThisAddIn.GetPEPStoreDraftsFolder();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   408
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   409
                        else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   410
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   411
                            tempFolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   412
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   413
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   414
                        if (tempFolder != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   415
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   416
                            tempMailItem = omi.Move(tempFolder) as Outlook.MailItem;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   417
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   418
                            if (tempMailItem != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   419
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   420
                                try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   421
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   422
                                    omi = tempMailItem.Move(currentFolder) as Outlook.MailItem;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   423
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   424
                                catch
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   425
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   426
                                    omi = tempMailItem.Copy();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   427
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   428
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   429
                                newInspector = application.Inspectors.Add(omi);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   430
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   431
                                if (windowState == Outlook.OlWindowState.olNormalWindow)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   432
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   433
                                    newInspector.Left = left;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   434
                                    newInspector.Top = top;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   435
                                    newInspector.Width = width;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   436
                                    newInspector.Height = height;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   437
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   438
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   439
                                newInspector.Display();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   440
                                newInspector.WindowState = windowState;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   441
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   442
                                repeatProcessing = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   443
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   444
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   445
                        else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   446
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   447
                            Log.Error("UpdateInspector: Cannot get temporary folder.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   448
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   449
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   450
                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   451
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   452
                        Log.Verbose("UpdateInspector: Error retrieving inspector window or application.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   453
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   454
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   455
                catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   456
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   457
                    Log.Verbose("UpdateInspector: Error updating inspector window. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   458
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   459
                finally
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   460
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   461
                    application = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   462
                    currentInspector = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   463
                    newInspector = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   464
                    omi = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   465
                    tempMailItem = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   466
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   467
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   468
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   469
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   470
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   471
        /// Clears the associated unencrypted preview and displays the given note (if any).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   472
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   473
        /// <param name="note">The note to display.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   474
        private void SetNote(string note = null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   475
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   476
            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   477
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   478
            if ((formRegions != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   479
                (string.IsNullOrEmpty(note) == false))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   480
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   481
                if ((formRegions.FormRegionPreviewUnencrypted != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   482
                    (formRegions.FormRegionPreviewUnencrypted.Visible))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   483
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   484
                    formRegions.FormRegionPreviewUnencrypted.DisplayState.SetNote(note);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   485
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   486
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   487
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   488
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   489
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   490
        /// Sets whether processing of the mail item is enabled.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   491
        /// This should commonly be set from the .GetIsPEPEnabled() value.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   492
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   493
        private void SetIsEnabled(bool enabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   494
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   495
            PEPIdentity currIdent;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   496
            Globals.ReturnStatus sts;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   497
            Outlook.Recipient currUser = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   498
            Outlook.Account currAccount = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   499
            Outlook.Account sendingAccount = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   500
            Outlook.NameSpace ns = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   501
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   502
            this.isEnabled = enabled;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   503
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   504
            if (enabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   505
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   506
                // Do not allow initialization more than once
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   507
                if (this.isStarted == false)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   508
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   509
                    /* It's possible for new draft MailItems to be created outside the context of an account.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   510
                     * In this situation the SendUsingAccount will always be null which of course breaks several pEp operations.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   511
                     * The operations themselves cannot make the assumption about what account information to use.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   512
                     * Therefore, the situation is detected here and for draft mail items a null SendUsingAccount will be 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   513
                     * set with the session's default account.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   514
                     */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   515
                    try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   516
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   517
                        ns = Globals.ThisAddIn.Application.Session;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   518
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   519
                        if (this.IsDraft)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   520
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   521
                            sendingAccount = this._CurrentMailItem.SendUsingAccount;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   522
                            currUser = ns.CurrentUser;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   523
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   524
                            if (sendingAccount == null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   525
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   526
                                sts = PEPIdentity.Create(currUser, out currIdent);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   527
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   528
                                if (sts == Globals.ReturnStatus.Success)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   529
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   530
                                    sendingAccount = AccountExtensions.GetDefaultAccount(Outlook.OlDefaultFolders.olFolderDrafts);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   531
                                    if (sendingAccount != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   532
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   533
                                        this._CurrentMailItem.SendUsingAccount = currAccount;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   534
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   535
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   536
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   537
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   538
                            // Check if account is already registered in pEp list and add it if necessary
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   539
                            var acctSettings = Globals.ThisAddIn.Settings.GetAccountSettings(sendingAccount?.SmtpAddress);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   540
                            if (acctSettings == null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   541
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   542
                                Log.Verbose("SetIsEnabled: New account detected. Creating pEp settings.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   543
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   544
                                // Create pEp settings for the new account
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   545
                                acctSettings = sendingAccount.CreatePEPSettings();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   546
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   547
                                // Add account to list
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   548
                                Globals.ThisAddIn.Settings.AccountsAddedWhileRunningList.Add(acctSettings?.SmtpAddress);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   549
                                Log.Verbose("SetIsEnabled: New account registered in pEp settings.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   550
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   551
                                // Generate key
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   552
                                Globals.ThisAddIn.RegisterMyself(acctSettings);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   553
                                Log.Verbose("SetIsEnabled: Myself registered.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   554
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   555
                                // Set rules and view filters
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   556
                                Globals.ThisAddIn.SetRulesAndViewFilters(sendingAccount);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   557
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   558
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   559
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   560
                    catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   561
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   562
                        Log.Error("SetIsEnabled: Error occured. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   563
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   564
                    finally
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   565
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   566
                        currAccount = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   567
                        currUser = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   568
                        ns = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   569
                        sendingAccount = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   570
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   571
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   572
                    // Connect refresh timer
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   573
                    this.TimerRefresh.Tick += TimerRefresh_Tick;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   574
                    this.isStarted = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   575
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   576
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   577
            else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   578
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   579
                // Stop and disconnect the refresh timer
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   580
                this.TimerRefresh.Stop();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   581
                this.TimerRefresh.Enabled = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   582
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   583
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   584
                this.isStarted = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   585
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   586
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   587
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   588
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   589
        /// Resolves all recipients of the Outlook mail item.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   590
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   591
        private void ResolveAllRecipients()
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   592
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   593
            if ((this.isEnabled) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   594
                (this.cryptableMailItem != null))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   595
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   596
                /*
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   597
                 * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   598
                 * This is because the resolve process can modify the contents of the mail item which triggers an event.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   599
                 * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   600
                 * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   601
                 * cannot be resolved(no address).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   602
                 */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   603
                try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   604
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   605
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   606
                    this.cryptableMailItem.ResolveAllRecipients();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   607
                    this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   608
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   609
                catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   610
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   611
                    Log.Error("Error resolving recipients. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   612
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   613
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   614
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   615
        #endregion
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   616
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   617
        #region Event Handling
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   618
        /**************************************************************
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   619
         * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   620
         * Event Handling
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   621
         * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   622
         *************************************************************/
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   623
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   624
        protected void InitializeMailItem(bool isInlineResponse = false)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   625
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   626
            bool enableFormRegion = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   627
            bool cancelOpenEvent = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   628
            bool isSecureAttachedMail = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   629
            string messageId = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   630
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   631
            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   632
            {      
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   633
                // Check if draft
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   634
                this.IsDraft = this._CurrentMailItem.GetIsDraft();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   635
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   636
                /* Set immediately a provisional rating in order to have a less flickery
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   637
                 * UI experience.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   638
                 * The provisional rating is either a stored rating or, in case we have a
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   639
                 * reply message, the rating of the original (the item we reply to).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   640
                 * This provisional rating will be replace with the real one once the full
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   641
                 * processing is complete.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   642
                 */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   643
                pEpRating provisionalRating = pEpRating.pEpRatingUndefined;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   644
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   645
                // Try to get an original rating (in case of reply messages)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   646
                if ((this.IsDraft) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   647
                    (this._CurrentMailItem?.Recipients?.Count > 0))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   648
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   649
                    string originalRatingString = this._CurrentMailItem.GetUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ORIGINAL_RATING) as string;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   650
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   651
                    // If we have an original rating, parse it and set it.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   652
                    if (string.IsNullOrEmpty(originalRatingString) == false)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   653
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   654
                        provisionalRating = AdapterExtensions.ParseRatingString(originalRatingString);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   655
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   656
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   657
                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   658
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   659
                    // Try to get rating from db
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   660
                    provisionalRating = PEPDatabase.GetRating(this._CurrentMailItem.EntryID);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   661
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   662
                    // If there is no rating in the db, use stored or default rating
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   663
                    if (provisionalRating == pEpRating.pEpRatingUndefined)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   664
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   665
                        provisionalRating = this._CurrentMailItem.GetStoredRating() ?? pEpRating.pEpRatingUndefined;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   666
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   667
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   668
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   669
                // Only set rating if one has been retrieved
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   670
                if (provisionalRating != pEpRating.pEpRatingUndefined)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   671
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   672
                    this.SetRating(provisionalRating);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   673
                    Log.Verbose("FormRegionPrivacyStatus_FormRegionShowing: Provisional rating {0} shown.", Enum.GetName(typeof(pEpRating), provisionalRating));
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   674
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   675
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   676
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   677
                // Do not process S/MIME messages
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   678
                if (this._CurrentMailItem.GetIsSMIMEEnabled())
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   679
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   680
                    Log.Verbose("FormRegionPrivacyStatus_FormRegionShowing: S/MIME message detected. Won't be processed.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   681
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   682
                    // Set unencrypted rating
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   683
                    this.SetRating(pEpRating.pEpRatingUnencrypted);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   684
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   685
                    // Set icon(s) if necessary     
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   686
                    if ((IsDraft == false) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   687
                        (this._CurrentMailItem.SetEncryptionIcons()))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   688
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   689
                        try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   690
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   691
                            this._CurrentMailItem.Save();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   692
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   693
                        catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   694
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   695
                            Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Error saving message after changing icon. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   696
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   697
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   698
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   699
                    return;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   700
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   701
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   702
                /* Check if item is attached mail. For performance reasons,
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   703
                 * only do the whole check if an item has been loaded to the
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   704
                 * cache of attached mails and if item might be secure.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   705
                 */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   706
                if ((PEPAttachment.AttachedMailsCache.Count > 0) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   707
                    (this._CurrentMailItem.Attachments?.Count == 2))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   708
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   709
                    try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   710
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   711
                        // Check if mail item is an attached mail
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   712
                        if (this._CurrentMailItem.GetIsAttachedMail(out messageId))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   713
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   714
                            Outlook.MailItem mirror = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   715
                            PEPMessage pEpMessage;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   716
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   717
                            // Try to get the mirror
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   718
                            mirror = this._CurrentMailItem.GetMirror(messageId);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   719
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   720
                            // If mirror was not found, decrypt and create mirror
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   721
                            if (mirror != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   722
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   723
                                isSecureAttachedMail = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   724
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   725
                            else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   726
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   727
                                if ((PEPMessage.Create(this._CurrentMailItem, out pEpMessage) == Globals.ReturnStatus.Success) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   728
                                    (pEpMessage.IsSecure))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   729
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   730
                                    PEPMessage decryptedMessage;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   731
                                    MsgProcessor msgProcessor = new MsgProcessor();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   732
                                    if (msgProcessor.Decrypt(pEpMessage, out decryptedMessage))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   733
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   734
                                        isSecureAttachedMail = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   735
                                        mirror = this._CurrentMailItem.CreateMirrorOMI(messageId);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   736
                                        decryptedMessage.ApplyTo(mirror, true, false);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   737
                                        mirror?.Save();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   738
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   739
                                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   740
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   741
                                        Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Decryption of attached mail was not successful.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   742
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   743
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   744
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   745
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   746
                            // Check if attachment is being opened or if only the preview is needed
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   747
                            if (CryptableMailItem.PreviewAttachedMailId?.Equals(messageId) != true)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   748
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   749
                                // Display mirror and cancel opening of original
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   750
                                cancelOpenEvent = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   751
                                mirror?.Display();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   752
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   753
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   754
                            // Not needed anymore after this point
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   755
                            CryptableMailItem.PreviewAttachedMailId = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   756
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   757
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   758
                    catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   759
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   760
                        messageId = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   761
                        Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Error checking for attached mail. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   762
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   763
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   764
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   765
                this.cryptableMailItem = new CryptableMailItem(this._CurrentMailItem, provisionalRating);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   766
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   767
                // Set inline response property
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   768
                this.cryptableMailItem.IsInlineResponse = isInlineResponse;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   769
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   770
                // Set properties for encrypted attached mail
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   771
                if (isSecureAttachedMail)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   772
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   773
                    this.cryptableMailItem.MessageId = messageId;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   774
                    this.cryptableMailItem.IsSecureAttachedMail = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   775
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   776
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   777
                // Connect cryptable mail item events
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   778
                if (this.cryptableMailItem != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   779
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   780
                    // If we don't open the original, set property and return
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   781
                    if (cancelOpenEvent)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   782
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   783
                        this.cryptableMailItem.Open += MailItem_Open;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   784
                        this.cryptableMailItem.CancelOpen = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   785
                        return;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   786
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   787
                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   788
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   789
                        try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   790
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   791
                            this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   792
                            this.cryptableMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   793
                            this.cryptableMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   794
                            this.cryptableMailItem.Send += MailItem_Send;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   795
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   796
                            if (this.cryptableMailItem.IsSecurelyStored)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   797
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   798
                                this.cryptableMailItem.Open += MailItem_Open;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   799
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   800
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   801
                        catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   802
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   803
                            Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Error occured. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   804
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   805
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   806
                        if (this._CurrentMailItem.GetIsPEPEnabled())
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   807
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   808
                            // If pEp is enabled, show the form region
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   809
                            enableFormRegion = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   810
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   811
                        else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   812
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   813
                            /* If pEp is disabled, process item and show form region in the following cases:
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   814
                             * 1. Incoming items: if decrypt always option is set
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   815
                             * 2. Outgoing items: if EnableProtection option is set
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   816
                             *                    Note: if this property isn't set, subscribe to originally 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   817
                             *                          encrypted status update event handler in case the 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   818
                             *                          current code runs before CryptableMailItem.MailItem_Reply
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   819
                             *                          or CryptableMailItem.MailItem_Forward.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   820
                             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   821
                            if (this.IsDraft)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   822
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   823
                                enableFormRegion = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   824
                                this.cryptableMailItem.OriginallyEncryptedStatusUpdated += CryptableMailItem_OriginallyEncryptedStatusUpdated;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   825
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   826
                            else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   827
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   828
                                enableFormRegion = this._CurrentMailItem.GetIsDecryptAlwaysEnabled();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   829
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   830
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   831
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   832
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   833
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   834
                // Update pEp enabled status
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   835
                this.SetIsEnabled(enableFormRegion);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   836
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   837
                if (this.isEnabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   838
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   839
                    // If forcefully protected, run dedicated decryption
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   840
                    if (this._CurrentMailItem.GetIsForcefullyProtected())
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   841
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   842
                        FPPMessage fppMessage = new FPPMessage(this._CurrentMailItem);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   843
                        if ((fppMessage?.GetMessageType() != null) ||
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   844
                            (fppMessage?.CurrentMessage?.IsSecure == true))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   845
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   846
                            fppMessage.ProcessIncoming(true);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   847
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   848
                        else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   849
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   850
                            this.TimerRefresh_Tick(null, new EventArgs());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   851
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   852
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   853
                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   854
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   855
                        // Call the timer tick method manually to refresh data with no delay
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   856
                        this.TimerRefresh_Tick(null, new EventArgs());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   857
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   858
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   859
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   860
            catch (Exception e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   861
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   862
                Log.Error("Window_SelectionChange: Error. " + e.Message);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   863
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   864
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   865
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   866
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   867
        /// Event handler for when the form region is closed.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   868
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   869
        protected void FormRegionPrivacyStatus_FormRegionClosed(object sender, EventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   870
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   871
            // Disconnect cryptable mail item
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   872
            if (this.cryptableMailItem != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   873
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   874
                try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   875
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   876
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   877
                    this.cryptableMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   878
                    this.cryptableMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   879
                    this.cryptableMailItem.Open -= MailItem_Open;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   880
                    this.cryptableMailItem.Send -= MailItem_Send;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   881
                    this.cryptableMailItem.OriginallyEncryptedStatusUpdated -= CryptableMailItem_OriginallyEncryptedStatusUpdated;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   882
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   883
                catch { }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   884
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   885
                this.cryptableMailItem.Dispose();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   886
                this.cryptableMailItem = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   887
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   888
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   889
            // Disconnect other
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   890
            this.SetIsEnabled(false);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   891
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   892
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   893
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   894
        /// Event handler for when the processing is completed in the associated mail item.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   895
        /// This will then update the form region UI and the privacy status window as needed.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   896
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   897
        private void MailItem_ProcessingCompleted(object sender, MsgProcessor.ProcessingCompletedEventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   898
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   899
            Log.Verbose("MailItem_ProcessingComplete: Decryption completed.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   900
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   901
            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   902
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   903
                // Marshal code back to UI thread as necessary
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   904
                KeySyncWizard.Wizard.Dispatcher.Invoke(new Action(() =>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   905
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   906
                    if (this.isEnabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   907
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   908
                        try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   909
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   910
                            this.SetRating(e.ProcessedRating);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   911
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   912
                            // Set MAPI properties if needed
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   913
                            if (e.PropertiesToSet?.Count > 0)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   914
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   915
                                this._CurrentMailItem?.SetMAPIProperties(e.PropertiesToSet);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   916
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   917
                            Log.Verbose("MailItem_ProcessingComplete: Status bar updated with rating " + Enum.GetName(typeof(pEpRating), e.ProcessedRating));
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   918
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   919
                        catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   920
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   921
                            Log.Verbose("MailItem_ProcessingComplete: Error. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   922
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   923
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   924
                        if (repeatProcessing &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   925
                            (repeatCounter++ < maxRepeatCount))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   926
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   927
                            repeatProcessing = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   928
                            this.RequestRatingAndUIUpdate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   929
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   930
                        else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   931
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   932
                            /* Check if the mail item is in Outbox and update the inspector window if possible.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   933
                             * This is the case when a submitted, but not yet sent email is opened again when working 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   934
                             * offline or without internet connection. Without this update, Outlook removes the message class 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   935
                             * "IPM.Note.SMIME.MultipartSigned" at the next send event and the message gets invalid and can't be
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   936
                             * opened again (i.e. is lost).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   937
                             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   938
                            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   939
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   940
                                if ((this._CurrentMailItem?.GetIsSubmitted() == true) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   941
                                    (this.IsDraft))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   942
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   943
                                    UpdateInspector();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   944
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   945
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   946
                            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   947
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   948
                                Log.Verbose("MailItem_ProcessingComplete: Error while checking if mail item is in outbox or updating inspector. " + ex.Message);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   949
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   950
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   951
                            /* Create the unencrypted preview if the mail item is encrypted and
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   952
                             * it is in an encrypted (untrusted) store
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   953
                             * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   954
                             * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   955
                             * it also is initialized after FormRegionPreviewUnencrypted.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   956
                             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   957
                            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   958
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   959
                                if (this.cryptableMailItem.IsSecurelyStored || this.cryptableMailItem.IsSecureAttachedMail)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   960
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   961
                                    Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   962
                                    this.cryptableMailItem.StartGetMirror(this.cryptableMailItem.MessageId);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   963
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   964
                                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   965
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   966
                                    this.SetNote();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   967
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   968
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   969
                            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   970
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   971
                                // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   972
                                // While rare, just log the issue and stop the process
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   973
                                Log.Warning("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   974
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   975
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   976
                            /* OUT-470: Workaround until this is implemented in the engine:
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   977
                             * Only enable Force Protection if all recipients are grey
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   978
                             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   979
                            if (this.IsDraft)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   980
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   981
                                bool disableForceProtection = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   982
                                Outlook.Recipients recipients = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   983
                                Outlook.Recipient recipient = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   984
                                PEPIdentity pEpIdentity = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   985
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   986
                                try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   987
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   988
                                    recipients = this._CurrentMailItem?.Recipients;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   989
                                    if (recipients?.Count > 0)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   990
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   991
                                        for (int i = 1; i <= recipients.Count; i++)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   992
                                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   993
                                            recipient = recipients[i];
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   994
                                            if ((PEPIdentity.Create(recipient, out pEpIdentity, false) == Globals.ReturnStatus.Success) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   995
                                                ((PEPIdentity.GetIsOwnIdentity(pEpIdentity.Address) ||
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   996
                                                 (ThisAddIn.PEPEngine.IdentityRating(pEpIdentity.ToCOMType()) >= pEpRating.pEpRatingReliable))))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   997
                                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   998
                                                disableForceProtection = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
   999
                                                Log.Verbose("ToggleButtonForceProtection_GetEnabled: Secure recipient found. Disabling force protection.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1000
                                                break;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1001
                                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1002
                                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1003
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1004
                                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1005
                                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1006
                                        // If there aren't any recipients (anymore), reset values
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1007
                                        this.DisableForceProtection = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1008
                                        this.ForceProtection = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1009
                                        this.NeverUnsecure = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1010
                                        this.ForceUnencrypted = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1011
                                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1012
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1013
                                catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1014
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1015
                                    Log.Error("ToggleButtonForceProtection_GetEnabled: Error occured. " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1016
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1017
                                finally
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1018
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1019
                                    recipient = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1020
                                    recipients = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1021
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1022
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1023
                                this.DisableForceProtection = disableForceProtection;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1024
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1025
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1026
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1027
                }));
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1028
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1029
            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1030
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1031
                Log.Error("MailItem_ProcessingComplete: Error setting UI state, " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1032
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1033
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1034
            this.processingOngoing = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1035
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1036
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1037
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1038
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1039
        /// This will then update the unencrypted preview in the UI.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1040
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1041
        private void MailItem_GetMirrorCompleted(object sender, CryptableMailItem.GetMirrorCompletedEventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1042
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1043
            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1044
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1045
                // Marshal code back to UI thread as necessary
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1046
                KeySyncWizard.Wizard.Dispatcher.Invoke(new Action(() =>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1047
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1048
                    if (this.isEnabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1049
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1050
                        WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1051
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1052
                        // If the message was forcefully protected, there is no mirror and we show the actual message
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1053
                        if ((e.Mirror == null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1054
                            (this._CurrentMailItem?.GetIsForcefullyProtected() == true))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1055
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1056
                            PEPMessage mirror = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1057
                            if (PEPMessage.Create(this._CurrentMailItem, out mirror) == Globals.ReturnStatus.Success)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1058
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1059
                                e.Mirror = mirror;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1060
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1061
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1062
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1063
                        if ((e.Mirror == null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1064
                            (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.Failure))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1065
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1066
                            this.SetNote(Properties.Resources.Message_OpenError);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1067
                            Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1068
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1069
                        else if ((e.Mirror == null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1070
                                 (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.FailureNoConnection))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1071
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1072
                            this.SetNote(Properties.Resources.Message_DecryptionNoConnection);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1073
                            Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1074
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1075
                        else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1076
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1077
                            if (formRegions != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1078
                            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1079
                                if ((formRegions.FormRegionPreviewUnencrypted != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1080
                                    (formRegions.FormRegionPreviewUnencrypted.Visible))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1081
                                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1082
                                    formRegions.FormRegionPreviewUnencrypted.DisplayState.OriginalEntryId = this._CurrentMailItem?.EntryID;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1083
                                    formRegions.FormRegionPreviewUnencrypted.DisplayState.SetMessage(e.Mirror);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1084
                                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1085
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1086
                                Log.Verbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1087
                            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1088
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1089
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1090
                        // Display the mirror if necessary
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1091
                        if ((this.cryptableMailItem != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1092
                            (this.displayMirrorRequested))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1093
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1094
                            this.cryptableMailItem.DisplayMirror();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1095
                            this.displayMirrorRequested = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1096
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1097
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1098
                }));
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1099
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1100
            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1101
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1102
                Log.Error("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1103
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1104
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1105
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1106
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1107
        /// Event handler for the mail item originally encrypted status updated.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1108
        /// This event is fired after the status has been updated with parent information following a
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1109
        /// forward or reply mail item event (on a different, likely parent, mail item).
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1110
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1111
        protected void CryptableMailItem_OriginallyEncryptedStatusUpdated(object sender, EventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1112
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1113
            // Process the mail item now that the cache is updated
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1114
            if (this.cryptableMailItem.IsOriginallyEncrypted)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1115
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1116
                this.cryptableMailItem.StartProcessing();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1117
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1118
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1119
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1120
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1121
        /// Event handler for when a mail item is being opened in an inspector.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1122
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1123
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1124
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1125
        /// If the event procedure sets this argument to True, the open operation is not completed 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1126
        /// and the inspector is not displayed.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1127
        protected void MailItem_Open(ref bool cancel)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1128
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1129
            bool result;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1130
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1131
            if ((this.isEnabled) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1132
                (this.cryptableMailItem != null))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1133
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1134
                // If cryptable mail item is not to be opened, cancel opening
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1135
                if (this.cryptableMailItem.CancelOpen)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1136
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1137
                    cancel = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1138
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1139
                else if ((this.cryptableMailItem.IsSecurelyStored) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1140
                         (this.cryptableMailItem.IsIncoming || this.cryptableMailItem.IsOriginallyEncrypted || !this.cryptableMailItem.IsDraft))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1141
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1142
                    // Try to open the mirror
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1143
                    result = this.cryptableMailItem.DisplayMirror();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1144
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1145
                    if (result == false)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1146
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1147
                        // Set flag to open after decryption/mirror location
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1148
                        this.displayMirrorRequested = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1149
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1150
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1151
                    // Always cancel opening the original
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1152
                    cancel = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1153
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1154
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1155
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1156
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1157
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1158
        /// Event handler for when a mail item is sent.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1159
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1160
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1161
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1162
        /// If the event procedure sets this argument to True, the send operation is not completed 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1163
        /// and the inspector is left open.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1164
        protected void MailItem_Send(ref bool cancel)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1165
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1166
            DialogResult result;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1167
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1168
            if (this.isEnabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1169
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1170
                if ((this.cryptableMailItem.IsBeingProcessed) ||
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1171
                    (this.processingOngoing))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1172
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1173
                    Log.Verbose("MailItem_Send cancelled. Mail item still being processed.");
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1174
                    cancel = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1175
                    return;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1176
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1177
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1178
                // Show warning message if needed
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1179
                if ((Globals.ThisAddIn.Settings.IsSecurityLossWarningEnabled) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1180
                    (this.Rating < pEpRating.pEpRatingUnreliable) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1181
                    (this.cryptableMailItem.IsOriginallyEncrypted))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1182
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1183
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1184
#if READER_RELEASE_MODE
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1185
                    FormReaderSplash warningMessage = new FormReaderSplash(true);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1186
                    result = warningMessage.ShowDialog();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1187
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1188
                    if (result != DialogResult.OK)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1189
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1190
                        // Cancel sending
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1191
                        cancel = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1192
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1193
#else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1194
                    result = MessageBox.Show(Properties.Resources.Message_WarningSecurityLoss,
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1195
                                             Properties.Resources.Message_TitleConfirmOperation,
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1196
                                             MessageBoxButtons.YesNo,
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1197
                                             MessageBoxIcon.Warning);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1198
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1199
                    if (result == DialogResult.No)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1200
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1201
                        // Cancel sending
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1202
                        cancel = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1203
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1204
#endif
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1205
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1206
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1207
                if (cancel == false)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1208
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1209
                    // Set pEp options if needed
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1210
                    if ((this.ForceProtection) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1211
                        (this.DisableForceProtection == false))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1212
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1213
                        this._CurrentMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, Guid.NewGuid().ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1214
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1215
                    else if (this.ForceUnencrypted)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1216
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1217
                        this._CurrentMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.ForceUnencrypted, true);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1218
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1219
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1220
                    if (this.NeverUnsecure)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1221
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1222
                        this._CurrentMailItem?.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, true);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1223
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1224
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1225
                    // Stop and disconnect the refresh timer
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1226
                    // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1227
                    this.TimerRefresh.Stop();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1228
                    this.TimerRefresh.Enabled = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1229
                    this.TimerRefresh.Tick -= TimerRefresh_Tick;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1230
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1231
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1232
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1233
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1234
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1235
        /// Event handler for when a mail item property is changed.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1236
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1237
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1238
        /// <param name="propertyName">The name of the property that was changed.</param>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1239
        protected void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1240
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1241
            switch (e.PropertyName.ToUpperInvariant())
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1242
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1243
                case "BCC":
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1244
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1245
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1246
                        break;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1247
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1248
                case "CC":
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1249
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1250
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1251
                        break;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1252
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1253
                case "SENTONBEHALFOFNAME":
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1254
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1255
                        // Always fired with "SENDUSINGACCOUNT" so is ignored
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1256
                        break;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1257
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1258
                case "SENDUSINGACCOUNT":
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1259
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1260
                        // Update pEp enabled status
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1261
                        this.isStarted = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1262
                        this.SetIsEnabled((this.CurrentMailItem)?.GetEnableFormRegion() ?? false);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1263
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1264
                        // Refresh the UI
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1265
                        this.ResolveAllRecipients();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1266
                        this.RequestRatingAndUIUpdate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1267
                        RibbonCustomizations.Invalidate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1268
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1269
                        break;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1270
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1271
                case "TO":
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1272
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1273
                        if (this.isEnabled)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1274
                        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1275
                            this.RequestRatingAndUIUpdate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1276
                        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1277
                        break;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1278
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1279
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1280
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1281
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1282
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1283
        /// Event handler for when a handshake dialog was updated.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1284
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1285
        protected void HandshakeDialog_Updated(object sender, EventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1286
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1287
            // Update current form region
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1288
            this.RequestRatingAndUIUpdate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1289
            
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1290
            /* If a handshake is performed while having the same message open both in an inspector
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1291
             * and an Window window, the one that didn't trigger the handshake won't get updated
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1292
             * automatically. Therefore, after a handshake, we also update all other open windows.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1293
             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1294
            try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1295
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1296
                Globals.ThisAddIn.RecalculateAllWindows(this);                
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1297
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1298
            catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1299
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1300
                Log.Error("HandshakeDialog_Updated: Error updating other windows. " + ex.Message);
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1301
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1302
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1303
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1304
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1305
        /// Event handler for when a handshake dialog was closed.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1306
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1307
        protected void HandshakeDialog_Closed(object sender, EventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1308
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1309
            if (this.handshakeDialog != null)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1310
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1311
                this.handshakeDialog.OnUpdateStatus -= HandshakeDialog_Updated;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1312
                this.handshakeDialog.Closed -= HandshakeDialog_Closed;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1313
                this.handshakeDialog = null;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1314
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1315
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1316
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1317
        /// <summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1318
        /// Event handler called after the refresh timer has elapsed.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1319
        /// </summary>
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1320
        private void TimerRefresh_Tick(object sender, EventArgs e)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1321
        {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1322
            bool tryAgain = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1323
#pragma warning disable 219
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1324
            bool markForDownload = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1325
#pragma warning restore 219
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1326
            this.TimerRefresh.Enabled = false; // Only once
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1327
            Outlook.OlDownloadState dlState;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1328
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1329
            /* The Refresh/UI_Update process:
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1330
             * There are the following components:
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1331
             *   1. TimerRefresh_Tick
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1332
             *        This is the main timer tick event handler called any time
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1333
             *        a refresh was requested (RequestRatingAndUIUpdate) and hasn't been run yet. 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1334
             *        A refresh is requested either at initialization or when a property changes
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1335
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1336
             *        property change events could occur rapidy, but only one refresh should
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1337
             *        occur for performance reasons.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1338
             * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1339
             *   2. ImmediateRatingAndUIUpdate
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1340
             *        This will re-calculate the mail item's rating.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1341
             *        This internally just calls CryptableMailItem.StartProcessing.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1342
             * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1343
             *   3. MailItem_ProcessingCompleted
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1344
             *        When processing of the mail item is complete, this even handler will be called.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1345
             *        This will update the UI with the latest rating then call StartGetMirror.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1346
             *        The CopyStateToUI method is used which means any open privacy status form will also
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1347
             *        be updated.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1348
             * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1349
             *   4. MailItem_GetMirrorComplete
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1350
             *        This is the final step in updating the UI, after a mirror is located, it's contents
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1351
             *        will be shown in the unencrypted preview.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1352
             * 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1353
             * The general calling sequence is as shown above 1->4 with each component calling the next.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1354
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1355
             */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1356
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1357
            // Ensure the tick method is not called more than once
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1358
            if ((this.isEnabled) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1359
                (refreshOngoing == false))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1360
            {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1361
                this.refreshOngoing = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1362
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1363
                if ((this.cryptableMailItem != null) &&
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1364
                    (this.cryptableMailItem.IsBeingProcessed == false))
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1365
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1366
                    // Attempt to get the download state
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1367
                    try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1368
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1369
                        dlState = this.cryptableMailItem.DownloadState;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1370
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1371
                    catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1372
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1373
                        Log.Warning("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1374
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1375
                        // Assume everything is downloaded, but try to download again as well
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1376
                        dlState = Outlook.OlDownloadState.olFullItem;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1377
                        markForDownload = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1378
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1379
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1380
                    if (dlState == Outlook.OlDownloadState.olFullItem)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1381
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1382
                        this.ImmediateRatingAndUIUpdate();
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1383
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1384
                    else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1385
                    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1386
                        markForDownload = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1387
                    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1388
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1389
                    /* 12/16/2016: It could be verified via testing that setting the MarkForDownload property
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1390
                     * can be a way to crash Outlook under certain circumstances (e.g. with IMAP on Outlook 2010 or on Windows 7). 
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1391
                     * This then is not caught by a try/catch block and therefore has to be considered an Outlook bug.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1392
                     * It seems that in newer versions of Outlook/Windows, they fixed it, causing exceptions, if at all.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1393
                     * For now, the following is commented out to prevent a crash. If at any point we see that header-only
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1394
                     * messages cause bigger issues, this decision has to be reevaluated.
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1395
                     */
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1396
                    //if (markForDownload)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1397
                    //{
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1398
                    //    // Try to mark the message for full download
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1399
                    //    try
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1400
                    //    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1401
                    //        this.cryptableMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1402
                    //        tryAgain = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1403
                    //    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1404
                    //    catch (Exception ex)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1405
                    //    {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1406
                    //        Log.Warning("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1407
                    //    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1408
                    //}
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1409
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1410
                else
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1411
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1412
                    repeatProcessing = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1413
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1414
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1415
                // Set the timer to refresh again later automatically
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1416
                if (tryAgain)
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1417
                {
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1418
                    this.TimerRefresh.Interval = 100;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1419
                    this.TimerRefresh.Enabled = true;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1420
                }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1421
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1422
                this.refreshOngoing = false;
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1423
            }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1424
        }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1425
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1426
        #endregion
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1427
    }
0e6191d039e4 Major restructuration:
Thomas
parents:
diff changeset
  1428
}