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