UI/FormRegionPrivacyStatus.cs
author Thomas
Tue, 22 Aug 2017 16:38:23 +0200
branchOUT-294
changeset 1797 804480ce0270
parent 1795 27ab64b7bafc
child 1799 caba7a4fb829
permissions -rw-r--r--
Detect originally encrypted item
Dean@1069
     1
´╗┐using pEp.UI;
Dean@1069
     2
using pEpCOMServerAdapterLib;
Dean@159
     3
using System;
Dean@445
     4
using System.ComponentModel;
Dean@859
     5
using System.Diagnostics;
vb@133
     6
using System.Windows.Forms;
vb@133
     7
using Outlook = Microsoft.Office.Interop.Outlook;
vb@133
     8
vb@133
     9
namespace pEp
vb@133
    10
{
Dean@159
    11
    /// <summary>
Dean@312
    12
    /// Partial class for the privacy status form region that is displayed below every outlook message.
Dean@159
    13
    /// </summary>
Dean@910
    14
    internal partial class FormRegionPrivacyStatus
vb@133
    15
    {
vb@133
    16
        #region Form Region Factory
vb@133
    17
vb@133
    18
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
Dean@312
    19
        [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegionPrivacyStatus")]
Dean@312
    20
        public partial class FormRegionPrivacyStatusFactory
vb@133
    21
        {
vb@133
    22
            // Occurs before the form region is initialized.
vb@133
    23
            // To prevent the form region from appearing, set e.Cancel to true.
vb@133
    24
            // Use e.OutlookItem to get a reference to the current Outlook item.
Dean@312
    25
            private void FormRegionPrivacyStatus_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
vb@133
    26
            {
Dean@1138
    27
                Outlook.MailItem omi;
Dean@1138
    28
Dean@1138
    29
                try
Dean@1138
    30
                {
Dean@1138
    31
                    omi = (Outlook.MailItem)e.OutlookItem;
Dean@1138
    32
                }
Dean@1138
    33
                catch
Dean@1138
    34
                {
Dean@1138
    35
                    // Never load if it's not a MailItem
Dean@1138
    36
                    e.Cancel = true;
Dean@1138
    37
                    return;
Dean@1138
    38
                }
Dean@1138
    39
Dean@985
    40
                //WindowFormRegionCollection formRegions;
Dean@327
    41
Dean@327
    42
                /* There is a Microsoft bug at least in Outlook 2013 and Windows 8.1
Dean@327
    43
                 * This bug causes multiple PrivacyStatus form regions to appear stacked on top of each other.
Dean@327
    44
                 * To trigger this bug, on an unencrypted server, click reply to compose an in-line response.
Dean@327
    45
                 * Then click to another tab such as People or Tasks. Then click back on the mail tab to view 
Dean@327
    46
                 * the original email again. Two form regions will be visible: 
Dean@327
    47
                 * (1) for the status during the in-line reply and 
Dean@327
    48
                 * (2) for the status of only the received message.
Dean@327
    49
                 * 
Dean@327
    50
                 * To fix this bug, any existing form regions are found and closed when initializing a new privacy status
Dean@327
    51
                 * form region.
Dean@327
    52
                 */
Dean@985
    53
                /*
Dean@327
    54
                try
Dean@327
    55
                {
Dean@987
    56
                    formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@327
    57
                    if (formRegions.FormRegionPrivacyStatus != null)
Dean@327
    58
                    {
Dean@985
    59
                        // Note: there seems to be no way to actually close the form region.
Dean@985
    60
                        // Therefore, this is as close as possible to actually closing it.
Dean@985
    61
                        // The actual form regions will be cleaned up as soon as another email is selected.
Dean@327
    62
                        formRegions.FormRegionPrivacyStatus.OutlookFormRegion.Visible = false;
Dean@327
    63
                    }
Dean@327
    64
                }
Dean@327
    65
                catch { }
Dean@985
    66
                */
Dean@1169
    67
Dean@327
    68
                return;
vb@133
    69
            }
vb@133
    70
        }
vb@133
    71
vb@133
    72
        #endregion
vb@133
    73
Dean@159
    74
        /* Notes:
Dean@179
    75
         * 
Dean@159
    76
         * Use this.OutlookItem to get a reference to the current Outlook item.
Dean@159
    77
         * Use this.OutlookFormRegion to get a reference to the form region.
Dean@179
    78
         * 
Dean@179
    79
         * UI State Managment:
Dean@179
    80
         * 
Dean@179
    81
         * The UI state is almost entirely set from the associated mail item data.
Dean@179
    82
         * However, a separate state class is maintained to represent the UI as some separation is needed.
Dean@179
    83
         * This logical separation MUST be maintained throughout the code.
Dean@179
    84
         * Specific cases are noted where possible.
Dean@179
    85
         * 
Dean@356
    86
         * The separate privacy status manager form state is also managed here.
Dean@159
    87
         */
Dean@159
    88
Dean@1384
    89
        /// <summary>
Dean@1384
    90
        /// Event for when the form region is expanded or collapsed.
Dean@1384
    91
        /// </summary>
Dean@1384
    92
        public event EventHandler Expanded;
Dean@1384
    93
Dean@1390
    94
        private CryptableMailItem       cryptableMailItem      = null;
Dean@1390
    95
        private bool                    displayMirrorRequested = false;
Thomas@1587
    96
        private HandshakeDialog         handshakeDialog        = null;
Dean@1390
    97
        private bool                    isEnabled              = true;
Dean@1384
    98
        private bool?                   isExpandedPrevious     = null;
Dean@1390
    99
        private bool                    isManagerFormEnabled   = true;
Dean@1390
   100
        private bool                    isStarted              = false;
Dean@758
   101
        private FormManagePrivacyStatus managerForm            = null;
Thomas@1505
   102
        private bool                    processingOngoing      = false;
Dean@1402
   103
        private bool                    refreshOngoing         = false;
Thomas@1419
   104
        private bool                    repeatProcessing       = false;
Thomas@1547
   105
        private int                     repeatCounter          = 0;
Thomas@1547
   106
        private const int               maxRepeatCount         = 5;
Dean@259
   107
Dean@159
   108
        /**************************************************************
Dean@159
   109
         * 
Dean@1375
   110
         * Property Accessors
Dean@1375
   111
         * 
Dean@1375
   112
         *************************************************************/
Dean@1375
   113
Dean@1375
   114
        /// <summary>
Dean@1377
   115
        /// Gets or sets the displayed state.
Dean@1375
   116
        /// </summary>
Dean@1377
   117
        public FormControlPrivacyStatus.State DisplayState
Dean@1375
   118
        {
Dean@1377
   119
            get { return (this.FormControlPrivacyStatusChild.DisplayState); }
Dean@1377
   120
            set { this.FormControlPrivacyStatusChild.DisplayState = value; }
Dean@1375
   121
        }
Dean@1375
   122
Dean@1375
   123
        /**************************************************************
Dean@1375
   124
         * 
Dean@159
   125
         * Methods
Dean@159
   126
         * 
Dean@159
   127
         *************************************************************/
Dean@159
   128
Dean@171
   129
        /// <summary>
Dean@438
   130
        /// Determines if this form region is running in an inspector window.
Dean@438
   131
        /// If not true, it is assumed to be within an explorer window.
Dean@438
   132
        /// </summary>
Dean@438
   133
        /// <returns>True if an inspector window, false if within an explorer.</returns>
Dean@438
   134
        private bool IsWithinInspector()
Dean@438
   135
        {
Dean@438
   136
            bool isWithinInspector = false;
Dean@895
   137
            Outlook.Inspector insp = null;
Dean@438
   138
Dean@438
   139
            try
Dean@438
   140
            {
Dean@895
   141
                /* There are two potential methods to determine if the item is within an inspector.
Dean@895
   142
                 * (1) Use this.OutlookFormRegion.Parent which will contain either the Explorer or an Inspector
Dean@895
   143
                 *     A cast could be tried to an Explorer of this parent and if it fails, it's assumed to be an inspector
Dean@895
   144
                 * (2) Use this.OutlookFormRegion.Inspector which will contain the Inspector if it exists.
Dean@895
   145
                 *     This will fail or return null if this form region is not running within it's own Inspector window.
Dean@895
   146
                 */
Dean@895
   147
                insp = this.OutlookFormRegion.Inspector;
Dean@895
   148
Dean@895
   149
                if (insp != null)
Dean@895
   150
                {
Dean@895
   151
                    isWithinInspector = true;
Dean@895
   152
                }
Dean@438
   153
            }
Dean@438
   154
            catch
Dean@438
   155
            {
Dean@895
   156
                isWithinInspector = false;
Dean@438
   157
            }
Dean@438
   158
            finally
Dean@438
   159
            {
Dean@895
   160
                if (insp != null)
Dean@438
   161
                {
vb@1516
   162
                    // Marshal.ReleaseComObject(insp);
Dean@895
   163
                    insp = null;
Dean@438
   164
                }
Dean@438
   165
            }
Dean@438
   166
Dean@438
   167
            return (isWithinInspector);
Dean@438
   168
        }
Dean@438
   169
Dean@438
   170
        /// <summary>
Dean@279
   171
        /// Builds the latest state of the encryption status manager then shows the UI.
Dean@279
   172
        /// </summary>
Dean@279
   173
        private void BuildAndShowManager()
Dean@279
   174
        {
Thomas@1583
   175
            /* Resolve all recipients -- this ensures the identities list is correctly populated
Thomas@1583
   176
             * 
Thomas@1583
   177
             * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
Thomas@1583
   178
             * This is because the resolve process can modify the contents of the mail item which triggers an event.
Thomas@1583
   179
             * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
Thomas@1583
   180
             * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that
Thomas@1583
   181
             * cannot be resolved (no address).
Thomas@1583
   182
             */
Dean@483
   183
            try
Dean@483
   184
            {
Thomas@1583
   185
                this.ResolveAllRecipients();
Dean@483
   186
            }
Thomas@1583
   187
            catch (Exception e)
Dean@483
   188
            {
Thomas@1583
   189
                Log.Verbose("BuildAndShowManager: Error resolving recipients. " + e.Message);
Dean@483
   190
            }
Dean@259
   191
Thomas@1587
   192
            // Build the dialog
Thomas@1583
   193
            try
Thomas@1583
   194
            {
Thomas@1587
   195
                handshakeDialog = new HandshakeDialog(this.cryptableMailItem.Myself,
Thomas@1594
   196
                                                      this.cryptableMailItem.From,
Thomas@1594
   197
                                                      this.cryptableMailItem.Recipients,
Thomas@1648
   198
                                                      this.cryptableMailItem.IsIncoming,
Thomas@1648
   199
                                                      this.cryptableMailItem.LastProcessedRating);
Thomas@1587
   200
                handshakeDialog.OnUpdateStatus += HandshakeDialog_Updated;
Thomas@1587
   201
                handshakeDialog.Closed += HandshakeDialog_Closed;
Thomas@1587
   202
                handshakeDialog.ShowDialog();
Thomas@1551
   203
            }
Thomas@1583
   204
            catch (Exception e)
Thomas@1551
   205
            {
Thomas@1583
   206
                Globals.StopAndSendCrashReport(e);
Thomas@1551
   207
            }
Dean@259
   208
        }
Dean@259
   209
Dean@259
   210
        /// <summary>
Dean@1130
   211
        /// Schedules for the pEp rating and UI (including displayed mirror) to be updated.
Dean@1001
   212
        /// This can be called many times with no issue as the update is only run every n milliseconds.
Dean@179
   213
        /// </summary>
Dean@1130
   214
        public void RequestRatingAndUIUpdate()
Dean@1001
   215
        {
Dean@1390
   216
            if (this.isEnabled)
Dean@1390
   217
            {
Dean@1390
   218
                this.TimerRefresh.Enabled = true;
Dean@1390
   219
            }
Dean@1001
   220
            return;
Dean@1001
   221
        }
Dean@1001
   222
Dean@1001
   223
        /// <summary>
Dean@1130
   224
        /// Immediately starts the update of UI rating based on the associated mail item.
Dean@1001
   225
        /// This will start decryption as necessary and also will update the displayed mirror.
Dean@1001
   226
        /// This method by-passes the refresh timer completely.
Dean@1001
   227
        /// WARNING: This method assumes the message is fully downloaded already.
Dean@1001
   228
        /// </summary>
Dean@1130
   229
        private void ImmediateRatingAndUIUpdate()
Dean@159
   230
        {
Dean@1390
   231
            if ((this.isEnabled) &&
Dean@1390
   232
                (this.cryptableMailItem != null))
Dean@159
   233
            {
Dean@1301
   234
                Log.Verbose("ImmediateRatingAndUIUpdate: Starting processing.");
Dean@740
   235
Dean@1301
   236
                // Start the rating calculation/decryption process
Dean@1390
   237
                this.cryptableMailItem.StartProcessing();
Thomas@1505
   238
                this.processingOngoing = true;
Dean@159
   239
            }
Dean@159
   240
Dean@179
   241
            return;
Dean@159
   242
        }
Dean@159
   243
Dean@709
   244
        /// <summary>
Thomas@1458
   245
        /// Workaround method to update the current inspector window. This is done by moving the mail item
Thomas@1458
   246
        /// to a temporary folder first and then back to the current folder. Both folders CANNOT be the same.
Thomas@1458
   247
        /// As a fallback, the mail item stays in the temporary folder if moving back to the current folder
Thomas@1458
   248
        /// fails.
Thomas@1458
   249
        /// </summary>
Thomas@1547
   250
        private void UpdateInspector()
Thomas@1458
   251
        {
Thomas@1458
   252
            Outlook.Application application = null;
Thomas@1547
   253
            Outlook.Folder currentFolder = null;
Thomas@1547
   254
            Outlook.Folder tempFolder = null;
Thomas@1458
   255
            Outlook.Inspector currentInspector = null;
Thomas@1458
   256
            Outlook.Inspector newInspector = null;
Thomas@1458
   257
            Outlook.MailItem omi = null;
Thomas@1458
   258
            Outlook.MailItem tempMailItem = null;
Thomas@1547
   259
            Outlook.Store store = null;
Thomas@1458
   260
Thomas@1458
   261
            omi = this.OutlookItem as Outlook.MailItem;
Thomas@1458
   262
Thomas@1458
   263
            if (omi != null)
Thomas@1458
   264
            {
Thomas@1458
   265
                try
Thomas@1458
   266
                {
Thomas@1458
   267
                    application = omi.Application;
Thomas@1458
   268
                    currentInspector = omi.GetInspector;
Thomas@1547
   269
                    currentFolder = omi.Parent as Outlook.Folder;
Thomas@1458
   270
Thomas@1458
   271
                    if ((currentInspector != null) &&
Thomas@1547
   272
                        (application != null) &&
Thomas@1547
   273
                        (currentFolder != null))
Thomas@1458
   274
                    {
Thomas@1458
   275
                        var left = currentInspector.Left;
Thomas@1458
   276
                        var top = currentInspector.Top;
Thomas@1458
   277
                        var width = currentInspector.Width;
Thomas@1458
   278
                        var height = currentInspector.Height;
Thomas@1458
   279
                        var windowState = currentInspector.WindowState;
Thomas@1458
   280
Thomas@1547
   281
                        /* Check, if in trusted store. In that case, use the default drafts folder
Thomas@1547
   282
                         * as temporary folder. If the store is untrusted, use the pEp drafts folder.
Thomas@1547
   283
                         */
Thomas@1547
   284
                        store = currentFolder.Store;
Thomas@1547
   285
                        if (store?.GetIsSecureStorageEnabled() ?? false)
Thomas@1547
   286
                        {
Thomas@1547
   287
                            tempFolder = Globals.ThisAddIn.GetPEPStoreDraftsFolder();
Thomas@1547
   288
                        }
Thomas@1547
   289
                        else
Thomas@1547
   290
                        {
Thomas@1547
   291
                            tempFolder = store.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderDrafts) as Outlook.Folder;
Thomas@1547
   292
                        }
Thomas@1458
   293
Thomas@1547
   294
                        if (tempFolder != null)
Thomas@1458
   295
                        {
Thomas@1547
   296
                            tempMailItem = omi.Move(tempFolder) as Outlook.MailItem;
Thomas@1547
   297
Thomas@1547
   298
                            if (tempMailItem != null)
Thomas@1458
   299
                            {
Thomas@1547
   300
                                try
Thomas@1547
   301
                                {
Thomas@1547
   302
                                    omi = tempMailItem.Move(currentFolder) as Outlook.MailItem;
Thomas@1547
   303
                                }
Thomas@1547
   304
                                catch
Thomas@1547
   305
                                {
Thomas@1547
   306
                                    omi = tempMailItem.Copy();
Thomas@1547
   307
                                }
Thomas@1547
   308
Thomas@1547
   309
                                newInspector = application.Inspectors.Add(omi);
Thomas@1547
   310
Thomas@1547
   311
                                if (windowState == Outlook.OlWindowState.olNormalWindow)
Thomas@1547
   312
                                {
Thomas@1547
   313
                                    newInspector.Left = left;
Thomas@1547
   314
                                    newInspector.Top = top;
Thomas@1547
   315
                                    newInspector.Width = width;
Thomas@1547
   316
                                    newInspector.Height = height;
Thomas@1547
   317
                                }
Thomas@1547
   318
Thomas@1547
   319
                                newInspector.Display();
Thomas@1547
   320
                                newInspector.WindowState = windowState;
Thomas@1547
   321
Thomas@1547
   322
                                repeatProcessing = false;
Thomas@1458
   323
                            }
Thomas@1547
   324
                        }
Thomas@1547
   325
                        else
Thomas@1547
   326
                        {
Thomas@1547
   327
                            Log.Error("UpdateInspector: Cannot get temporary folder.");
Thomas@1458
   328
                        }
Thomas@1458
   329
                    }
Thomas@1458
   330
                    else
Thomas@1458
   331
                    {
Thomas@1458
   332
                        Log.Verbose("UpdateInspector: Error retrieving inspector window or application.");
Thomas@1458
   333
                    }
Thomas@1458
   334
                }
Thomas@1458
   335
                catch (Exception e)
Thomas@1458
   336
                {
Thomas@1458
   337
                    Log.Verbose("UpdateInspector: Error updating inspector window. " + e.Message);
Thomas@1458
   338
                }
Thomas@1458
   339
                finally
Thomas@1458
   340
                {
Thomas@1458
   341
                    if (application != null)
Thomas@1458
   342
                    {
vb@1507
   343
                        //Marshal.ReleaseComObject(application);
Thomas@1458
   344
                        application = null;
Thomas@1458
   345
                    }
Thomas@1458
   346
Thomas@1458
   347
                    if (currentInspector != null)
Thomas@1458
   348
                    {
vb@1516
   349
                        // Marshal.ReleaseComObject(currentInspector);
Thomas@1458
   350
                        currentInspector = null;
Thomas@1458
   351
                    }
Thomas@1458
   352
Thomas@1458
   353
                    if (newInspector != null)
Thomas@1458
   354
                    {
vb@1516
   355
                        // Marshal.ReleaseComObject(newInspector);
Thomas@1458
   356
                        newInspector = null;
Thomas@1458
   357
                    }
Thomas@1458
   358
Thomas@1458
   359
                    if (tempMailItem != null)
Thomas@1458
   360
                    {
vb@1516
   361
                        // Marshal.ReleaseComObject(tempMailItem);
Thomas@1458
   362
                        tempMailItem = null;
Thomas@1458
   363
                    }
Thomas@1458
   364
Thomas@1458
   365
                    if (omi != null)
Thomas@1458
   366
                    {
vb@1516
   367
                        // Marshal.ReleaseComObject(omi);
Thomas@1458
   368
                        omi = null;
Thomas@1458
   369
                    }
Thomas@1458
   370
                }
Thomas@1458
   371
            }
Thomas@1458
   372
Thomas@1458
   373
            return;
Thomas@1458
   374
        }
Thomas@1458
   375
Thomas@1458
   376
        /// <summary>
Dean@1117
   377
        /// Immediately update the manage privacy status form display state.
Dean@1117
   378
        /// This by default will completely rebuild the display state.
Dean@1117
   379
        /// </summary>
Thomas@1583
   380
        private void UpdateManagePrivacyStatusForm()
Dean@1117
   381
        {
Dean@1117
   382
            if (this.managerForm != null)
Dean@1117
   383
            {
Thomas@1583
   384
                // Only update the message rating
Thomas@1583
   385
                this.managerForm.DisplayState.Rating = this.FormControlPrivacyStatusChild.DisplayState.Rating;
Dean@1117
   386
            }
Dean@1117
   387
Dean@1117
   388
            return;
Dean@1117
   389
        }
Dean@1117
   390
Dean@1117
   391
        /// <summary>
Dean@709
   392
        /// Clears the associated unencrypted preview and displays the given note (if any).
Dean@709
   393
        /// </summary>
Dean@709
   394
        /// <param name="note">The note to diplsay.</param>
Dean@1483
   395
        private void SetNote(string note = null)
Dean@709
   396
        {
Dean@987
   397
            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   398
Dean@1476
   399
            if (formRegions != null)
Dean@709
   400
            {
Dean@1476
   401
                if ((formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@1476
   402
                    (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@1476
   403
                {
Dean@1483
   404
                    formRegions.FormRegionPreviewUnencrypted.DisplayState.SetNote(note);
Dean@1476
   405
                }
Dean@709
   406
            }
Dean@709
   407
Dean@709
   408
            return;
Dean@709
   409
        }
Dean@709
   410
Dean@1390
   411
        /// <summary>
Dean@1390
   412
        /// Sets whether processing of the mail item is enabled.
Dean@1390
   413
        /// This should commonly be set from the .GetIsPEPEnabled() value.
Dean@1390
   414
        /// </summary>
Dean@1390
   415
        private void SetIsEnabled(bool enabled)
Dean@1390
   416
        {
Dean@1390
   417
            PEPIdentity currIdent;
Dean@1390
   418
            Globals.ReturnStatus sts;
Dean@1390
   419
            Outlook.MailItem omi = null;
Dean@1390
   420
            Outlook.Recipient currUser = null;
Dean@1390
   421
            Outlook.Account currAccount = null;
Dean@1390
   422
            Outlook.Account sendingAccount = null;
Dean@1390
   423
            Outlook.NameSpace ns = null;
Dean@1390
   424
Dean@1390
   425
            this.OutlookFormRegion.Visible = enabled;
Dean@1390
   426
            this.isEnabled = enabled;
Dean@1390
   427
Dean@1390
   428
            if (enabled)
Dean@1390
   429
            {
Dean@1390
   430
                // Do not allow initialization more than once
Dean@1390
   431
                if (this.isStarted == false)
Dean@1390
   432
                {
Dean@1390
   433
                    /* It's possible for new draft MailItems to be created outside the context of an account.
Dean@1390
   434
                     * In this situation the SendUsingAccount will always be null which of course breaks several pEp operations.
Dean@1390
   435
                     * The operations themselves cannot make the assumption about what account information to use.
Dean@1390
   436
                     * Therefore, the situation is detected here and for draft mail items a null SendUsingAccount will be 
Dean@1390
   437
                     * set with the session's default account.
Dean@1390
   438
                     */
Dean@1390
   439
                    try
Dean@1390
   440
                    {
Dean@1390
   441
                        ns = Globals.ThisAddIn.Application.Session;
Dean@1390
   442
Dean@1390
   443
                        if ((this.OutlookItem is Outlook.MailItem) &&
Dean@1390
   444
                            ((Outlook.MailItem)this.OutlookItem).GetIsDraft())
Dean@1390
   445
                        {
Dean@1390
   446
                            omi = this.OutlookItem as Outlook.MailItem;
Dean@1390
   447
                            sendingAccount = omi.SendUsingAccount;
Dean@1390
   448
                            currUser = ns.CurrentUser;
Dean@1390
   449
Dean@1390
   450
                            if (sendingAccount == null)
Dean@1390
   451
                            {
Dean@1390
   452
                                sts = PEPIdentity.Create(currUser, out currIdent);
Dean@1390
   453
Dean@1390
   454
                                if (sts == Globals.ReturnStatus.Success)
Dean@1390
   455
                                {
Dean@1390
   456
                                    currAccount = PEPIdentity.GetOwnAccount(currIdent.Address);
Dean@1390
   457
                                    omi.SendUsingAccount = currAccount;
Dean@1390
   458
                                }
Dean@1390
   459
                            }
Dean@1390
   460
                        }
Dean@1390
   461
                    }
Dean@1390
   462
                    catch { }
Dean@1390
   463
                    finally
Dean@1390
   464
                    {
Dean@1390
   465
                        if (omi != null)
Dean@1390
   466
                        {
vb@1516
   467
                            // Marshal.ReleaseComObject(omi);
Dean@1390
   468
                            omi = null;
Dean@1390
   469
                        }
Dean@1390
   470
Dean@1390
   471
                        if (currUser != null)
Dean@1390
   472
                        {
vb@1507
   473
                            //Marshal.ReleaseComObject(currUser);
Dean@1390
   474
                            currUser = null;
Dean@1390
   475
                        }
Dean@1390
   476
Dean@1390
   477
                        if (currAccount != null)
Dean@1390
   478
                        {
vb@1507
   479
                            //Marshal.ReleaseComObject(currAccount);
Dean@1390
   480
                            currAccount = null;
Dean@1390
   481
                        }
Dean@1390
   482
Dean@1390
   483
                        if (sendingAccount != null)
Dean@1390
   484
                        {
vb@1507
   485
                            //Marshal.ReleaseComObject(sendingAccount);
Dean@1390
   486
                            sendingAccount = null;
Dean@1390
   487
                        }
Dean@1390
   488
Dean@1390
   489
                        if (ns != null)
Dean@1390
   490
                        {
vb@1507
   491
                            //Marshal.ReleaseComObject(ns);
Dean@1390
   492
                            ns = null;
Dean@1390
   493
                        }
Dean@1390
   494
                    }
Dean@1390
   495
Dean@1390
   496
                    // Set reader upgrade link visibility
Dean@1390
   497
                    if ((Globals.RELEASE_MODE == Globals.ReleaseMode.Reader) &&
Dean@1390
   498
                        (this.cryptableMailItem != null) &&
Dean@1390
   499
                        (this.cryptableMailItem.IsDraft))
Dean@1390
   500
                    {
Dean@1390
   501
                        this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = true;
Dean@1390
   502
                    }
Dean@1390
   503
                    else
Dean@1390
   504
                    {
Dean@1390
   505
                        this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = false;
Dean@1390
   506
                    }
Dean@1390
   507
Dean@1390
   508
                    // Connect events
Dean@1390
   509
                    this.FormControlPrivacyStatusChild.PrivacyViewClick += FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@1390
   510
                    this.Expanded += FormRegionPrivacyStatus_Expanded;
Dean@1390
   511
Dean@1390
   512
                    this.TimerRefresh.Tick += TimerRefresh_Tick;
Dean@1390
   513
                    this.isStarted = true;
Dean@1390
   514
                }
Dean@1390
   515
            }
Dean@1390
   516
            else
Dean@1390
   517
            {
Dean@1390
   518
                // Disconnect events
Dean@1390
   519
                this.FormControlPrivacyStatusChild.PrivacyViewClick -= FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@1390
   520
                this.Expanded -= FormRegionPrivacyStatus_Expanded;
Dean@1390
   521
Dean@1390
   522
                // Stop and disconnect the refresh timer
Dean@1390
   523
                this.TimerRefresh.Stop();
Dean@1390
   524
                this.TimerRefresh.Enabled = false;
Dean@1390
   525
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@1390
   526
Dean@1390
   527
                this.isStarted = false;
Dean@1390
   528
            }
Dean@1390
   529
Dean@1390
   530
            return;
Dean@1390
   531
        }
Dean@1390
   532
Dean@1390
   533
        /// <summary>
Dean@1390
   534
        /// Resolves all recipients of the Outlook mail item.
Dean@1390
   535
        /// </summary>
Dean@1390
   536
        private void ResolveAllRecipients()
Dean@1390
   537
        {
Dean@1390
   538
            if ((this.isEnabled) &&
Dean@1390
   539
                (this.cryptableMailItem != null))
Dean@1390
   540
            {
Dean@1390
   541
                /*
Dean@1390
   542
                 * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
Dean@1390
   543
                 * This is because the resolve process can modify the contents of the mail item which triggers an event.
Dean@1390
   544
                 * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
Dean@1390
   545
                 * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that
Dean@1390
   546
                 * cannot be resolved(no address).
Dean@1390
   547
                 */
Thomas@1551
   548
                try
Thomas@1551
   549
                {
Thomas@1551
   550
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
Thomas@1551
   551
                    this.cryptableMailItem.ResolveAllRecipients();
Thomas@1551
   552
                    this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
Thomas@1551
   553
                }
Thomas@1551
   554
                catch (Exception e)
Thomas@1551
   555
                {
Thomas@1551
   556
                    Log.Error("Error resolving recipients. " + e.Message);
Thomas@1551
   557
                }
Dean@1390
   558
            }
Dean@1390
   559
Dean@1390
   560
            return;
Dean@1390
   561
        }
Dean@1390
   562
Dean@159
   563
        /**************************************************************
Dean@159
   564
         * 
Dean@159
   565
         * Event Handling
Dean@159
   566
         * 
Dean@159
   567
         *************************************************************/
Dean@159
   568
Dean@159
   569
        /// <summary>
Dean@680
   570
        /// Event handler that is called when the form region is displayed.
Thomas@1795
   571
        /// This is called each time the form region loses then regains visibility 
Dean@680
   572
        /// (for example an other email is selected then back to this one).
Dean@159
   573
        /// </summary>
Dean@1461
   574
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, EventArgs e)
Dean@159
   575
        {
Thomas@1797
   576
            bool enableFormRegion = false;
Thomas@1797
   577
            Outlook.MailItem omi = null;
Thomas@1797
   578
Dean@1390
   579
            // Set mail item
Thomas@1797
   580
            omi = this.OutlookItem as Outlook.MailItem;
Thomas@1797
   581
Thomas@1797
   582
            if (omi != null)
Dean@1390
   583
            {
Thomas@1797
   584
                this.cryptableMailItem = new CryptableMailItem(omi);
Thomas@1797
   585
Thomas@1797
   586
                // Connect cryptable mail item events
Thomas@1797
   587
                if (this.cryptableMailItem != null)
Thomas@1797
   588
                {
Thomas@1797
   589
                    try
Thomas@1797
   590
                    {
Thomas@1797
   591
                        this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
Thomas@1797
   592
                        this.cryptableMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
Thomas@1797
   593
                        this.cryptableMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
Thomas@1797
   594
                        this.cryptableMailItem.Send += MailItem_Send;
Thomas@1797
   595
                        this.cryptableMailItem.Open += MailItem_Open;
Thomas@1797
   596
                    }
Thomas@1797
   597
                    catch (Exception ex)
Thomas@1797
   598
                    {
Thomas@1797
   599
                        Log.Error("FormRegionPrivacyStatus_FormRegionShowing: Error occured. " + ex.ToString());
Thomas@1797
   600
                    }
Thomas@1797
   601
Thomas@1797
   602
                    // If pEp is enabled, show the form region
Thomas@1797
   603
                    if (omi.GetIsPEPEnabled())
Thomas@1797
   604
                    {
Thomas@1797
   605
                        enableFormRegion = true;
Thomas@1797
   606
                    }
Thomas@1797
   607
                    else
Thomas@1797
   608
                    {
Thomas@1797
   609
                        /* If pEp is disabled, process item and show form region in the following cases:
Thomas@1797
   610
                         * 1. Incoming items: if decrypt always option is set
Thomas@1797
   611
                         * 2. Outgoing items: if ForceProtection option is set
Thomas@1797
   612
                         *                    Note: if this property isn't set, subscribe to originally 
Thomas@1797
   613
                         *                          encrypted status update event handler in case the 
Thomas@1797
   614
                         *                          current code runs before CryptableMailItem.MailItem_Reply
Thomas@1797
   615
                         *                          or CryptableMailItem.MailItem_Forward.
Thomas@1797
   616
                         */
Thomas@1797
   617
                        if (omi.GetIsIncoming())
Thomas@1797
   618
                        {
Thomas@1797
   619
                            enableFormRegion = omi.GetIsDecryptAlwaysEnabled();
Thomas@1797
   620
                        }
Thomas@1797
   621
                        else
Thomas@1797
   622
                        {
Thomas@1797
   623
                            enableFormRegion = omi.GetForceProtection();
Thomas@1797
   624
Thomas@1797
   625
                            if (enableFormRegion == false)
Thomas@1797
   626
                            {
Thomas@1797
   627
                                this.cryptableMailItem.OriginallyEncryptedStatusUpdated += CryptableMailItem_OriginallyEncryptedStatusUpdated;
Thomas@1797
   628
                            }
Thomas@1797
   629
                        }
Thomas@1797
   630
                    }
Thomas@1797
   631
                }
Thomas@1797
   632
Thomas@1797
   633
                // Update pEp enabled status
Thomas@1797
   634
                this.SetIsEnabled(enableFormRegion);
Thomas@1797
   635
Thomas@1797
   636
                if (this.isEnabled)
Thomas@1797
   637
                {
Thomas@1797
   638
                    // Call the timer tick method manually to refresh data with no delay
Thomas@1797
   639
                    this.TimerRefresh_Tick(null, new EventArgs());
Thomas@1797
   640
                }
Dean@1390
   641
            }
Dean@159
   642
        }
Dean@159
   643
Dean@159
   644
        /// <summary>
Dean@159
   645
        /// Event handler for when the form region is closed.
Dean@159
   646
        /// </summary>
Thomas@1795
   647
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, EventArgs e)
Dean@159
   648
        {
Dean@1390
   649
            // Disconnect cryptable mail item
Dean@1390
   650
            if (this.cryptableMailItem != null)
Dean@179
   651
            {
Dean@179
   652
                try
Dean@179
   653
                {
Dean@1390
   654
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@1390
   655
                    this.cryptableMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
Dean@1390
   656
                    this.cryptableMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
Dean@1390
   657
                    this.cryptableMailItem.Open -= MailItem_Open;
Dean@1390
   658
                    this.cryptableMailItem.Send -= MailItem_Send;
Dean@179
   659
                }
Dean@179
   660
                catch { }
Dean@1390
   661
Dean@1390
   662
                this.cryptableMailItem.Dispose();
Dean@1390
   663
                this.cryptableMailItem = null;
Dean@179
   664
            }
Dean@179
   665
Dean@1390
   666
            // Disconnect other
Dean@1390
   667
            this.SetIsEnabled(false);
Dean@682
   668
Dean@179
   669
            return;
Dean@179
   670
        }
Dean@179
   671
Dean@179
   672
        /// <summary>
Dean@1384
   673
        /// Event handler for when the form region is expanded or collapsed.
Dean@1384
   674
        /// </summary>
Dean@1384
   675
        private void FormRegionPrivacyStatus_Expanded(object sender, EventArgs e)
Dean@1384
   676
        {
Dean@1385
   677
            /* This Outlook form region has a bug where clicking the '+' button to expand will also
Dean@1385
   678
             * raise a click event on the privacy status button. Basically, Outlook would fire the click event 
Dean@1385
   679
             * (FormControlPrivacyStatusChild_PrivacyViewClick) immediately after the region is expanded even though at 
Dean@1385
   680
             * the time it was clicked it was collapsed and the click already captured by the expander '+' button.
Dean@1385
   681
             * This code here works around that issue by disabling the manager form when the form region is collapsed.
Dean@1385
   682
             * Since the click event occurs BEFORE the expanded event, this will disable opening the manage privacy status
Dean@1385
   683
             * form in this situation.
Dean@1385
   684
             */
Dean@1390
   685
            this.isManagerFormEnabled = this.OutlookFormRegion.IsExpanded;
Dean@1384
   686
Dean@1384
   687
            return;
Dean@1384
   688
        }
Dean@1384
   689
Dean@1384
   690
        /// <summary>
Dean@1384
   691
        /// Event handler called after the monitor timer has elapsed.
Dean@1384
   692
        /// This should be used only for quick polling of changes in Outlook or the UI state.
Dean@1384
   693
        /// </summary>
Dean@1384
   694
        private void TimerMonitor_Tick(object sender, EventArgs e)
Dean@1384
   695
        {
Dean@1384
   696
            bool isExpandedCurrent;
Dean@1384
   697
Dean@1384
   698
            // Check for IsExpanded changes
Dean@1384
   699
            isExpandedCurrent = this.OutlookFormRegion.IsExpanded;
Dean@1384
   700
            if ((this.isExpandedPrevious == null) ||
Dean@1384
   701
                ((bool)this.isExpandedPrevious != isExpandedCurrent))
Dean@1384
   702
            {
Dean@1384
   703
                this.Expanded?.Invoke(this, new EventArgs());
Dean@1384
   704
            }
Dean@1384
   705
            this.isExpandedPrevious = isExpandedCurrent;
Dean@1384
   706
Dean@1384
   707
            return;
Dean@1384
   708
        }
Dean@1384
   709
Dean@1384
   710
        /// <summary>
Dean@179
   711
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   712
        /// </summary>
Dean@179
   713
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   714
        {
Dean@1402
   715
            bool tryAgain = false;
Thomas@1547
   716
#pragma warning disable 219
Dean@1402
   717
            bool markForDownload = false;
Thomas@1547
   718
#pragma warning restore 219
Dean@179
   719
            this.TimerRefresh.Enabled = false; // Only once
Dean@1402
   720
            Outlook.OlDownloadState dlState;
markus@201
   721
Dean@709
   722
            /* The Refresh/UI_Update process is a little more complicated here than other forms.
Dean@709
   723
             * There are the following components:
Dean@709
   724
             *   1. TimerRefresh_Tick
Dean@709
   725
             *        This is the main timer tick event handler called any time
Dean@1130
   726
             *        a refresh was requested (RequestRatingAndUIUpdate) and hasn't been run yet. 
Dean@1001
   727
             *        A refresh is requested either at initialization or when a property changes
Dean@709
   728
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
Dean@709
   729
             *        property change events could occur rapidy, but only one refresh should
Dean@709
   730
             *        occur for performance reasons.
Dean@709
   731
             * 
Dean@1130
   732
             *   2. ImmediateRatingAndUIUpdate
Dean@1307
   733
             *        This will re-calculate the mail item's rating.
Dean@1307
   734
             *        This internally just calls CryptableMailItem.StartProcessing.
Dean@709
   735
             * 
Dean@1130
   736
             *   3. MailItem_ProcessingCompleted
Dean@1307
   737
             *        When processing of the mail item is complete, this even handler will be called.
Dean@1130
   738
             *        This will update the UI with the latest rating then call StartGetMirror.
Dean@709
   739
             *        The CopyStateToUI method is used which means any open privacy status form will also
Dean@709
   740
             *        be updated.
Dean@709
   741
             * 
Dean@709
   742
             *   4. MailItem_GetMirrorComplete
Dean@1402
   743
             *        This is the final step in updating the UI, after a mirror is located, it's contents
Dean@1307
   744
             *        will be shown in the unencrypted preview.
Dean@709
   745
             * 
Dean@709
   746
             * The general calling sequence is as shown above 1->4 with each component calling the next.
Dean@709
   747
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
Dean@709
   748
             */
Dean@709
   749
Dean@179
   750
            // Ensure the tick method is not called more than once
Dean@1390
   751
            if ((this.isEnabled) &&
Dean@1402
   752
                (refreshOngoing == false))
Dean@179
   753
            {
Dean@1402
   754
                this.refreshOngoing = true;
Dean@1402
   755
Thomas@1419
   756
                if ((this.cryptableMailItem != null) &&
Thomas@1419
   757
                    (this.cryptableMailItem.IsBeingProcessed == false))
Dean@1402
   758
                {
Dean@1402
   759
                    // Attempt to get the download state
Dean@1402
   760
                    try
Dean@1402
   761
                    {
Dean@1402
   762
                        dlState = this.cryptableMailItem.DownloadState;
Dean@1402
   763
                    }
Dean@1402
   764
                    catch (Exception ex)
Dean@1402
   765
                    {
Dean@1402
   766
                        Log.Warning("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
Dean@1402
   767
Dean@1402
   768
                        // Assume everything is downloaded, but try to download again as well
Dean@1402
   769
                        dlState = Outlook.OlDownloadState.olFullItem;
Dean@1402
   770
                        markForDownload = true;
Dean@1402
   771
                    }
Dean@1402
   772
Dean@1402
   773
                    if (dlState == Outlook.OlDownloadState.olFullItem)
Dean@1402
   774
                    {
Dean@1402
   775
                        this.ImmediateRatingAndUIUpdate();
Dean@1402
   776
                    }
Dean@1402
   777
                    else
Dean@1402
   778
                    {
Dean@1402
   779
                        markForDownload = true;
Dean@1402
   780
                    }
Dean@1402
   781
Thomas@1529
   782
                    /* 12/16/2016: It could be verified via testing that setting the MarkForDownload property
Thomas@1529
   783
                     * can be a way to crash Outlook under certain circumstances (e.g. with IMAP on Outlook 2010 or on Windows 7). 
Thomas@1529
   784
                     * This then is not caught by a try/catch block and therefore has to be considered an Outlook bug.
Thomas@1529
   785
                     * It seems that in newer versions of Outlook/Windows, they fixed it, causing exceptions, if at all.
Thomas@1529
   786
                     * For now, the following is commented out to prevent a crash. If at any point we see that header-only
Thomas@1529
   787
                     * messages cause bigger issues, this decision has to be reevaluated.
Thomas@1529
   788
                     */
Thomas@1529
   789
                    //if (markForDownload)
Thomas@1529
   790
                    //{
Thomas@1529
   791
                    //    // Try to mark the message for full download
Thomas@1529
   792
                    //    try
Thomas@1529
   793
                    //    {
Thomas@1529
   794
                    //        this.cryptableMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Thomas@1529
   795
                    //        tryAgain = true;
Thomas@1529
   796
                    //    }
Thomas@1529
   797
                    //    catch (Exception ex)
Thomas@1529
   798
                    //    {
Thomas@1529
   799
                    //        Log.Warning("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
Thomas@1529
   800
                    //    }
Thomas@1529
   801
                    //}
Dean@1402
   802
                }
Thomas@1419
   803
                else
Thomas@1419
   804
                {
Thomas@1419
   805
                    repeatProcessing = true;
Thomas@1419
   806
                }
Dean@1402
   807
Dean@1402
   808
                // Set the timer to refresh again later automatically
Dean@1402
   809
                if (tryAgain)
Dean@1402
   810
                {
Dean@1402
   811
                    this.TimerRefresh.Interval = 100;
Dean@1402
   812
                    this.TimerRefresh.Enabled = true;
Dean@1402
   813
                }
Dean@1402
   814
Dean@1402
   815
                this.refreshOngoing = false;
Dean@179
   816
            }
Dean@175
   817
Dean@159
   818
            return;
Dean@159
   819
        }
Dean@159
   820
Dean@169
   821
        /// <summary>
Dean@1096
   822
        /// Event handler for when the processing is completed in the associated mail item.
Dean@709
   823
        /// This will then update the form region UI and the privacy status window as needed.
Dean@680
   824
        /// </summary>
Dean@1461
   825
        private void MailItem_ProcessingCompleted(object sender, MsgProcessor.ProcessingCompletedEventArgs e)
Dean@680
   826
        {
Dean@1249
   827
            Log.Verbose("MailItem_ProcessingComplete: Decryption completed.");
Dean@740
   828
Dean@680
   829
            try
Dean@680
   830
            {
Dean@680
   831
                // Marshal code back to UI thread as necessary
Dean@680
   832
                this.Invoke(new Action(() =>
Dean@680
   833
                    {
Dean@1390
   834
                        if (this.isEnabled)
Dean@1390
   835
                        {
Thomas@1505
   836
                            try
Thomas@1505
   837
                            {
Thomas@1505
   838
                                this.FormControlPrivacyStatusChild.DisplayState.Rating = e.ProcessedRating;
Thomas@1505
   839
                                RibbonCustomizations.Invalidate();
Thomas@1583
   840
                                this.UpdateManagePrivacyStatusForm(); // Only update the rating
Thomas@1505
   841
                            }
Thomas@1505
   842
                            catch (Exception ex)
Thomas@1505
   843
                            {
Thomas@1505
   844
                                Log.Verbose("MailItem_ProcessingComplete: Error. " + ex.ToString());
Thomas@1505
   845
                            }
Dean@709
   846
Thomas@1547
   847
                            if (repeatProcessing &&
Thomas@1547
   848
                                (repeatCounter++ < maxRepeatCount))
Dean@747
   849
                            {
Thomas@1419
   850
                                repeatProcessing = false;
Thomas@1419
   851
                                this.RequestRatingAndUIUpdate();
Thomas@1419
   852
                            }
Thomas@1419
   853
                            else
Thomas@1419
   854
                            {
Thomas@1470
   855
                                /* Check if the mail item is in Outbox and update the inspector window if possible.
Thomas@1470
   856
                                 * This is the case when a submitted, but not yet sent email is opened again when working 
Thomas@1470
   857
                                 * offline or without internet connection. Without this update, Outlook removes the message class 
Thomas@1470
   858
                                 * "IPM.Note.SMIME.MultipartSigned" at the next send event and the message gets invalid and can't be
Thomas@1470
   859
                                 * opened again (i.e. is lost).
Thomas@1458
   860
                                 */
Thomas@1458
   861
                                try
Thomas@1458
   862
                                {
Thomas@1458
   863
                                    Outlook.MailItem omi = this.OutlookItem as Outlook.MailItem;
Thomas@1458
   864
Thomas@1458
   865
                                    if ((omi != null) &&
Thomas@1458
   866
                                        (omi.GetIsSubmitted()) &&
Thomas@1458
   867
                                        (omi.GetIsDraft()))
Thomas@1458
   868
                                    {
Thomas@1458
   869
                                        var insp = this.OutlookFormRegion.Inspector;
Thomas@1458
   870
Thomas@1458
   871
                                        if (insp != null)
Thomas@1458
   872
                                        {
Thomas@1547
   873
                                            UpdateInspector();
Thomas@1458
   874
                                        }
Thomas@1458
   875
                                    }
Thomas@1458
   876
                                }
Thomas@1458
   877
                                catch (Exception ex)
Thomas@1458
   878
                                {
Thomas@1505
   879
                                    Log.Verbose("MailItem_ProcessingComplete: Error while checking if mail item is in outbox or updating inspector. " + ex.Message);
Thomas@1458
   880
                                }
Thomas@1458
   881
Thomas@1419
   882
                                /* Create the unencrypted preview if the mail item is encrypted and
Thomas@1419
   883
                                 * it is in an encrypted (untrusted) store
Thomas@1419
   884
                                 * 
Thomas@1419
   885
                                 * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Thomas@1419
   886
                                 * it also is initialized after FormRegionPreviewUnencrypted.
Thomas@1419
   887
                                 */
Thomas@1419
   888
                                try
Dean@1390
   889
                                {
Thomas@1419
   890
                                    if (this.cryptableMailItem.IsSecurelyStored)
Thomas@1419
   891
                                    {
Thomas@1419
   892
                                        Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
Thomas@1419
   893
                                        this.cryptableMailItem.StartGetMirror();
Thomas@1419
   894
                                    }
Thomas@1419
   895
                                    else
Thomas@1419
   896
                                    {
Dean@1483
   897
                                        this.SetNote();
Thomas@1419
   898
                                    }
Dean@1390
   899
                                }
Thomas@1419
   900
                                catch (Exception ex)
Dean@1390
   901
                                {
Thomas@1419
   902
                                    // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
Thomas@1419
   903
                                    // While rare, just log the issue and stop the process
Thomas@1419
   904
                                    Log.Warning("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
Dean@1390
   905
                                }
Dean@747
   906
                            }
Dean@709
   907
                        }
Dean@680
   908
                    }));
Dean@680
   909
            }
Dean@680
   910
            catch (Exception ex)
Dean@680
   911
            {
Dean@1249
   912
                Log.Error("MailItem_ProcessingComplete: Error setting UI state, " + ex.ToString());
Dean@680
   913
            }
Dean@680
   914
Thomas@1505
   915
            this.processingOngoing = false;
Thomas@1505
   916
Dean@680
   917
            return;
Dean@680
   918
        }
Dean@680
   919
Dean@680
   920
        /// <summary>
Dean@709
   921
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
Dean@709
   922
        /// This will then update the unencrypted preview in the UI.
Dean@709
   923
        /// </summary>
Dean@1096
   924
        private void MailItem_GetMirrorCompleted(object sender, CryptableMailItem.GetMirrorCompletedEventArgs e)
Dean@709
   925
        {
Dean@709
   926
            try
Dean@709
   927
            {
Dean@709
   928
                // Marshal code back to UI thread as necessary
Dean@709
   929
                this.Invoke(new Action(() =>
Dean@709
   930
                    {
Dean@1390
   931
                        if (this.isEnabled)
Dean@1390
   932
                        {
Dean@1390
   933
                            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   934
Dean@1390
   935
                            if ((e.Mirror == null) &&
Dean@1390
   936
                                (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.Failure))
Dean@910
   937
                            {
Dean@1483
   938
                                this.SetNote(Properties.Resources.Message_OpenError);
Dean@1390
   939
                                Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
Dean@910
   940
                            }
Dean@1390
   941
                            else if ((e.Mirror == null) &&
Dean@1390
   942
                                     (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.FailureNoConnection))
Dean@1390
   943
                            {
Dean@1483
   944
                                this.SetNote(Properties.Resources.Message_DecryptionNoConnection);
Dean@1390
   945
                                Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
Dean@1390
   946
                            }
Dean@1390
   947
                            else
Dean@1390
   948
                            {
Dean@1476
   949
                                if (formRegions != null)
Dean@1390
   950
                                {
Dean@1476
   951
                                    if ((formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@1476
   952
                                        (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@1476
   953
                                    {
Dean@1483
   954
                                        formRegions.FormRegionPreviewUnencrypted.DisplayState.SetMessage(e.Mirror);
Dean@1476
   955
                                    }
Dean@1476
   956
Dean@1390
   957
                                    Log.Verbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
Dean@1390
   958
                                }
Dean@1390
   959
                            }
Dean@758
   960
Dean@1390
   961
                            // Display the mirror if necessary
Dean@1390
   962
                            if ((this.cryptableMailItem != null) &&
Dean@1390
   963
                                (this.displayMirrorRequested))
Dean@1390
   964
                            {
Dean@1390
   965
                                this.cryptableMailItem.DisplayMirror();
Dean@1390
   966
                                this.displayMirrorRequested = false;
Dean@1390
   967
                            }
Dean@758
   968
                        }
Dean@709
   969
                    }));
Dean@709
   970
            }
Dean@709
   971
            catch (Exception ex)
Dean@709
   972
            {
Dean@1249
   973
                Log.Error("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
Dean@709
   974
            }
Dean@709
   975
Dean@709
   976
            return;
Dean@709
   977
        }
Dean@709
   978
Dean@709
   979
        /// <summary>
Thomas@1797
   980
        /// Event handler for the mail item originally encrypted status updated.
Thomas@1797
   981
        /// This event is fired after the status has been updated with parent information following a
Dean@807
   982
        /// forward or reply mail item event (on a different, likely parent, mail item).
Dean@807
   983
        /// </summary>
Thomas@1797
   984
        private void CryptableMailItem_OriginallyEncryptedStatusUpdated(object sender, EventArgs e)
Dean@807
   985
        {
Thomas@1797
   986
            // Process the mail item now that the cache is updated
Thomas@1797
   987
            if (this.isEnabled == false &&
Thomas@1797
   988
                this.cryptableMailItem.IsOriginallyEncrypted)
Dean@1390
   989
            {
Thomas@1797
   990
                this.isEnabled = true;
Thomas@1797
   991
                this.TimerRefresh_Tick(null, new EventArgs());
Dean@1390
   992
            }
Dean@807
   993
Dean@807
   994
            return;
Dean@807
   995
        }
Dean@807
   996
Dean@807
   997
        /// <summary>
Dean@169
   998
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   999
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
  1000
        /// </summary>
Dean@169
  1001
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
  1002
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
  1003
        /// and the inspector is not displayed.</param>
Dean@169
  1004
        private void MailItem_Open(ref bool cancel)
Dean@159
  1005
        {
Dean@758
  1006
            bool result;
Dean@758
  1007
Thomas@1797
  1008
            if ((this.isEnabled)
Thomas@1797
  1009
                && (this.cryptableMailItem != null)
markus@1582
  1010
                && (this.cryptableMailItem.IsSecurelyStored)
markus@1582
  1011
                && (this.cryptableMailItem.IsIncoming || this.cryptableMailItem.IsOriginallyEncrypted || !this.cryptableMailItem.IsDraft))
Dean@169
  1012
            {
Dean@758
  1013
                // Try to open the mirror
Dean@1390
  1014
                result = this.cryptableMailItem.DisplayMirror();
Dean@758
  1015
Dean@758
  1016
                if (result == false)
Dean@758
  1017
                {
Dean@758
  1018
                    // Set flag to open after decryption/mirror location
Dean@758
  1019
                    this.displayMirrorRequested = true;
Dean@758
  1020
                }
Dean@758
  1021
Dean@758
  1022
                // Always cancel opening the original
Dean@169
  1023
                cancel = true;
Dean@169
  1024
            }
Dean@169
  1025
Dean@169
  1026
            return;
Dean@159
  1027
        }
Dean@159
  1028
Dean@169
  1029
        /// <summary>
Dean@682
  1030
        /// Event handler for when a mail item is sent.
Dean@682
  1031
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
Dean@682
  1032
        /// </summary>
Dean@682
  1033
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@682
  1034
        /// If the event procedure sets this argument to True, the send operation is not completed 
Dean@682
  1035
        /// and the inspector is left open.</param>
Dean@682
  1036
        private void MailItem_Send(ref bool cancel)
Dean@682
  1037
        {
Dean@807
  1038
            DialogResult result;
Dean@807
  1039
Dean@1390
  1040
            if (this.isEnabled)
Dean@807
  1041
            {
Thomas@1505
  1042
                if ((this.cryptableMailItem.IsBeingProcessed) ||
Thomas@1505
  1043
                    (this.processingOngoing))
Thomas@1505
  1044
                {
Thomas@1505
  1045
                    Log.Verbose("MailItem_Send cancelled. Mail item still being processed.");
Thomas@1505
  1046
                    cancel = true;
Thomas@1505
  1047
                    return;
Thomas@1505
  1048
                }
Thomas@1505
  1049
Dean@1390
  1050
                if ((Globals.ThisAddIn.Settings.IsSecurityLossWarningEnabled) &&
Dean@1390
  1051
                    (this.cryptableMailItem.IsOriginallyEncrypted) &&
Dean@1390
  1052
                    (this.FormControlPrivacyStatusChild.DisplayState.Rating < pEpRating.pEpRatingUnreliable))
Dean@1390
  1053
                {
Thomas@1318
  1054
Thomas@1318
  1055
#if READER_RELEASE_MODE
Dean@1390
  1056
                    FormReaderSplash warningMessage = new FormReaderSplash(true);
Dean@1390
  1057
                    result = warningMessage.ShowDialog();
Thomas@1318
  1058
Dean@1390
  1059
                    if (result != DialogResult.OK)
Dean@1390
  1060
                    {
Dean@1390
  1061
                        // Cancel sending
Dean@1390
  1062
                        cancel = true;
Dean@1390
  1063
                    }
Dean@1390
  1064
#else
Dean@1390
  1065
                    result = System.Windows.Forms.MessageBox.Show(this.ParentForm,
Dean@1390
  1066
                                                                  pEp.Properties.Resources.Message_WarningSecurityLoss,
Dean@1390
  1067
                                                                  pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@1390
  1068
                                                                  MessageBoxButtons.YesNo,
Dean@1390
  1069
                                                                  MessageBoxIcon.Warning);
Dean@1390
  1070
Dean@1390
  1071
                    if (result == DialogResult.No)
Dean@1390
  1072
                    {
Dean@1390
  1073
                        // Cancel sending
Dean@1390
  1074
                        cancel = true;
Dean@1390
  1075
                    }
Dean@1390
  1076
#endif
Dean@1390
  1077
                }
Dean@1390
  1078
Dean@1390
  1079
                // Stop and disconnect the refresh timer
Dean@1390
  1080
                // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
Dean@1390
  1081
                if (cancel == false)
Thomas@1318
  1082
                {
Dean@1390
  1083
                    this.TimerRefresh.Stop();
Dean@1390
  1084
                    this.TimerRefresh.Enabled = false;
Dean@1390
  1085
                    this.TimerRefresh.Tick -= TimerRefresh_Tick;
Thomas@1318
  1086
                }
Dean@807
  1087
            }
Dean@682
  1088
Dean@682
  1089
            return;
Dean@682
  1090
        }
Dean@682
  1091
Dean@682
  1092
        /// <summary>
Dean@169
  1093
        /// Event handler for when a mail item property is changed.
Dean@169
  1094
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
  1095
        /// </summary>
Dean@169
  1096
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@445
  1097
        private void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
Dean@159
  1098
        {
Dean@1349
  1099
            switch (e.PropertyName.ToUpperInvariant())
Dean@159
  1100
            {
Dean@1390
  1101
                case "BCC":
Dean@1390
  1102
                    {
Dean@1390
  1103
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
Dean@1390
  1104
                        break;
Dean@1390
  1105
                    }
Dean@1390
  1106
                case "CC":
Dean@1390
  1107
                    {
Dean@1390
  1108
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
Dean@1390
  1109
                        break;
Dean@1390
  1110
                    }
Dean@1390
  1111
                case "SENTONBEHALFOFNAME":
Dean@1390
  1112
                    {
Dean@1390
  1113
                        // Always fired with "SENDUSINGACCOUNT" so is ignored
Dean@1390
  1114
                        break;
Dean@1390
  1115
                    }
Dean@1390
  1116
                case "SENDUSINGACCOUNT":
Dean@1390
  1117
                    {
Dean@1390
  1118
                        // Update pEp enabled status
Thomas@1795
  1119
                        this.SetIsEnabled((this.OutlookItem as Outlook.MailItem)?.GetEnableFormRegion() ?? false);
Dean@1390
  1120
Dean@1390
  1121
                        // Refresh the UI
Dean@1390
  1122
                        this.ResolveAllRecipients();
Dean@1390
  1123
                        this.RequestRatingAndUIUpdate();
Dean@1390
  1124
                        RibbonCustomizations.Invalidate();
Dean@1390
  1125
Dean@1390
  1126
                        break;
Dean@1390
  1127
                    }
Dean@179
  1128
                case "TO":
Dean@1390
  1129
                    {
Dean@1390
  1130
                        if (this.isEnabled)
Dean@1390
  1131
                        {
Dean@1390
  1132
                            this.RequestRatingAndUIUpdate();
Dean@1390
  1133
                        }
Dean@1390
  1134
                        break;
Dean@1390
  1135
                    }
Dean@159
  1136
            }
Dean@169
  1137
Dean@169
  1138
            return;
Dean@159
  1139
        }
Dean@159
  1140
Dean@179
  1141
        /// <summary>
Thomas@1587
  1142
        /// Event handler for when a handshake dialog was updated.
Thomas@1583
  1143
        /// </summary>
Thomas@1587
  1144
        private void HandshakeDialog_Updated(object sender, EventArgs e)
Thomas@1583
  1145
        {
Thomas@1633
  1146
            // Update current form region
Thomas@1587
  1147
            this.RequestRatingAndUIUpdate();
Thomas@1633
  1148
Thomas@1633
  1149
            /* If a handshake is performed while having the same message open both in an inspector
Thomas@1633
  1150
             * and an explorer window, the one that didn't trigger the handshake won't get updated
Thomas@1633
  1151
             * automatically. Therefore, after a handshake, we perform a search for all form regions
Thomas@1633
  1152
             * of type FormRegionPrivacyStatus and update the associated cryptable mail items.
Thomas@1633
  1153
             */
Thomas@1633
  1154
            try
Thomas@1633
  1155
            {
Thomas@1633
  1156
                // Get all form regions
Thomas@1633
  1157
                var formRegions = Globals.FormRegions;
Thomas@1633
  1158
Thomas@1633
  1159
                foreach (var formRegion in formRegions)
Thomas@1633
  1160
                {
Thomas@1633
  1161
                    var fr = formRegion as FormRegionPrivacyStatus;
Thomas@1633
  1162
Thomas@1633
  1163
                    if (fr != null)
Thomas@1633
  1164
                    {
Thomas@1633
  1165
                        // Only process the ones that differ from the one that triggered the update.
Thomas@1633
  1166
                        if (fr.cryptableMailItem.Equals(this.cryptableMailItem) == false)
Thomas@1633
  1167
                        {
Thomas@1633
  1168
                            fr.cryptableMailItem.StartProcessing();
Thomas@1633
  1169
                        }
Thomas@1633
  1170
                    }
Thomas@1633
  1171
                }
Thomas@1633
  1172
            }
Thomas@1633
  1173
            catch (Exception ex)
Thomas@1633
  1174
            {
Thomas@1633
  1175
                Log.Error("HandshakeDialog_Updated: Error updating form regions. " + ex.Message);
Thomas@1633
  1176
            }
Thomas@1587
  1177
        }
Thomas@1587
  1178
Thomas@1587
  1179
        /// <summary>
Thomas@1587
  1180
        /// Event handler for when a handshake dialog was closed.
Thomas@1587
  1181
        /// </summary>
Thomas@1587
  1182
        private void HandshakeDialog_Closed(object sender, EventArgs e)
Thomas@1587
  1183
        {
Thomas@1587
  1184
            if (this.handshakeDialog != null)
Thomas@1587
  1185
            {
Thomas@1587
  1186
                this.handshakeDialog.OnUpdateStatus -= HandshakeDialog_Updated;
Thomas@1587
  1187
                this.handshakeDialog.Closed -= HandshakeDialog_Closed;
Thomas@1587
  1188
                this.handshakeDialog = null;
Thomas@1587
  1189
            }
Thomas@1583
  1190
        }
Thomas@1583
  1191
Thomas@1583
  1192
        /// <summary>
Dean@1021
  1193
        /// Event handler for when the manager form is closed.
Dean@1021
  1194
        /// </summary>
Dean@1021
  1195
        private void ManagerForm_FormClosed(object sender, FormClosedEventArgs e)
Dean@1021
  1196
        {
Dean@1021
  1197
            // Remove the reference held in the FormRegionPrivacyStatus so it stops getting updated
Dean@1021
  1198
            if (this.managerForm != null)
Dean@1021
  1199
            {
Dean@1021
  1200
                this.managerForm.FormClosed -= ManagerForm_FormClosed;
Dean@1021
  1201
                this.managerForm = null;
Dean@1021
  1202
            }
Dean@1021
  1203
Dean@1021
  1204
            return;
Dean@1021
  1205
        }
Dean@1021
  1206
Dean@1021
  1207
        /// <summary>
Dean@859
  1208
        /// Event handler for when the pEp website hyperlink is clicked.
Dean@859
  1209
        /// </summary>
Dean@859
  1210
        private void LinkLabelUpgrade_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Dean@859
  1211
        {
Dean@966
  1212
            try
Dean@966
  1213
            {
Dean@1007
  1214
                Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
Dean@966
  1215
            }
Dean@966
  1216
            catch (Exception ex)
Dean@966
  1217
            {
Dean@1249
  1218
                Log.Error("LinkLabelUpgrade_LinkClicked: Unable to open website link, " + ex.ToString());
Dean@966
  1219
            }
Dean@966
  1220
Dean@859
  1221
            return;
Dean@859
  1222
        }
Dean@1047
  1223
Dean@1047
  1224
        /// <summary>
Dean@1086
  1225
        /// Event handler for when the privacy view button is clicked within the form control.
Dean@1047
  1226
        /// </summary>
Dean@1086
  1227
        private void FormControlPrivacyStatusChild_PrivacyViewClick(object sender, System.Windows.RoutedEventArgs e)
Dean@1047
  1228
        {
Dean@1390
  1229
            if (this.isManagerFormEnabled)
Dean@1384
  1230
            {
Dean@1384
  1231
                this.RequestRatingAndUIUpdate();
Dean@1384
  1232
                this.BuildAndShowManager();
Dean@1384
  1233
            }
Dean@1384
  1234
Dean@1047
  1235
            return;
Dean@1047
  1236
        }
vb@133
  1237
    }
vb@133
  1238
}