UI/FormRegionPrivacyStatus.cs
author Thomas
Mon, 21 Aug 2017 16:36:12 +0200
branchOUT-294
changeset 1795 27ab64b7bafc
parent 1790 314f3162286c
child 1797 804480ce0270
permissions -rw-r--r--
OUT-297: Implement Enable protection button
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
            bool setByCache = false;
Dean@1390
   418
            PEPIdentity currIdent;
Dean@1390
   419
            Globals.ReturnStatus sts;
Dean@1390
   420
            Outlook.MailItem omi = null;
Dean@1390
   421
            Outlook.Recipient currUser = null;
Dean@1390
   422
            Outlook.Account currAccount = null;
Dean@1390
   423
            Outlook.Account sendingAccount = null;
Dean@1390
   424
            Outlook.NameSpace ns = null;
Dean@1390
   425
Dean@1390
   426
            this.OutlookFormRegion.Visible = enabled;
Dean@1390
   427
            this.isEnabled = enabled;
Dean@1390
   428
Dean@1390
   429
            if (enabled)
Dean@1390
   430
            {
Dean@1390
   431
                // Do not allow initialization more than once
Dean@1390
   432
                if (this.isStarted == false)
Dean@1390
   433
                {
Dean@1390
   434
                    /* It's possible for new draft MailItems to be created outside the context of an account.
Dean@1390
   435
                     * In this situation the SendUsingAccount will always be null which of course breaks several pEp operations.
Dean@1390
   436
                     * The operations themselves cannot make the assumption about what account information to use.
Dean@1390
   437
                     * Therefore, the situation is detected here and for draft mail items a null SendUsingAccount will be 
Dean@1390
   438
                     * set with the session's default account.
Dean@1390
   439
                     */
Dean@1390
   440
                    try
Dean@1390
   441
                    {
Dean@1390
   442
                        ns = Globals.ThisAddIn.Application.Session;
Dean@1390
   443
Dean@1390
   444
                        if ((this.OutlookItem is Outlook.MailItem) &&
Dean@1390
   445
                            ((Outlook.MailItem)this.OutlookItem).GetIsDraft())
Dean@1390
   446
                        {
Dean@1390
   447
                            omi = this.OutlookItem as Outlook.MailItem;
Dean@1390
   448
                            sendingAccount = omi.SendUsingAccount;
Dean@1390
   449
                            currUser = ns.CurrentUser;
Dean@1390
   450
Dean@1390
   451
                            if (sendingAccount == null)
Dean@1390
   452
                            {
Dean@1390
   453
                                sts = PEPIdentity.Create(currUser, out currIdent);
Dean@1390
   454
Dean@1390
   455
                                if (sts == Globals.ReturnStatus.Success)
Dean@1390
   456
                                {
Dean@1390
   457
                                    currAccount = PEPIdentity.GetOwnAccount(currIdent.Address);
Dean@1390
   458
                                    omi.SendUsingAccount = currAccount;
Dean@1390
   459
                                }
Dean@1390
   460
                            }
Dean@1390
   461
                        }
Dean@1390
   462
                    }
Dean@1390
   463
                    catch { }
Dean@1390
   464
                    finally
Dean@1390
   465
                    {
Dean@1390
   466
                        if (omi != null)
Dean@1390
   467
                        {
vb@1516
   468
                            // Marshal.ReleaseComObject(omi);
Dean@1390
   469
                            omi = null;
Dean@1390
   470
                        }
Dean@1390
   471
Dean@1390
   472
                        if (currUser != null)
Dean@1390
   473
                        {
vb@1507
   474
                            //Marshal.ReleaseComObject(currUser);
Dean@1390
   475
                            currUser = null;
Dean@1390
   476
                        }
Dean@1390
   477
Dean@1390
   478
                        if (currAccount != null)
Dean@1390
   479
                        {
vb@1507
   480
                            //Marshal.ReleaseComObject(currAccount);
Dean@1390
   481
                            currAccount = null;
Dean@1390
   482
                        }
Dean@1390
   483
Dean@1390
   484
                        if (sendingAccount != null)
Dean@1390
   485
                        {
vb@1507
   486
                            //Marshal.ReleaseComObject(sendingAccount);
Dean@1390
   487
                            sendingAccount = null;
Dean@1390
   488
                        }
Dean@1390
   489
Dean@1390
   490
                        if (ns != null)
Dean@1390
   491
                        {
vb@1507
   492
                            //Marshal.ReleaseComObject(ns);
Dean@1390
   493
                            ns = null;
Dean@1390
   494
                        }
Dean@1390
   495
                    }
Dean@1390
   496
Dean@1390
   497
                    // Set reader upgrade link visibility
Dean@1390
   498
                    if ((Globals.RELEASE_MODE == Globals.ReleaseMode.Reader) &&
Dean@1390
   499
                        (this.cryptableMailItem != null) &&
Dean@1390
   500
                        (this.cryptableMailItem.IsDraft))
Dean@1390
   501
                    {
Dean@1390
   502
                        this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = true;
Dean@1390
   503
                    }
Dean@1390
   504
                    else
Dean@1390
   505
                    {
Dean@1390
   506
                        this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = false;
Dean@1390
   507
                    }
Dean@1390
   508
Dean@1390
   509
                    /* For a MailItem being forwarded or replied to it's import to check against the encrypted conversation cache.
Dean@1390
   510
                     * This is normally done within CryptableMailItem_EncryptedConversationCacheUpdated; however,
Dean@1390
   511
                     * for trusted servers that use an in-line reponse, the EncryptedConversationCacheUpdated event occurs
Dean@1390
   512
                     * BEFORE the new mail item is created. This means it never sees the event.
Dean@1390
   513
                     * To get around this issue, when the form region is first shown, also check if the mail item
Dean@1390
   514
                     * exists in the encrypted conversation cache and should be marked as formerly encrypted.
Dean@1390
   515
                     * If this check passes, never event connect the CryptableMailItem_EncryptedConversationCacheUpdated event.
Dean@1390
   516
                     */
Dean@1390
   517
                    if (this.cryptableMailItem != null)
Dean@1390
   518
                    {
Dean@1390
   519
                        setByCache = this.cryptableMailItem.SetIsOriginallyEncryptedByCache();
Dean@1390
   520
                    }
Dean@1390
   521
Dean@1390
   522
                    // Connect events
Dean@1390
   523
                    if (setByCache == false) { CryptableMailItem.EncryptedConversationCacheUpdated += CryptableMailItem_EncryptedConversationCacheUpdated; }
Dean@1390
   524
                    this.FormControlPrivacyStatusChild.PrivacyViewClick += FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@1390
   525
                    this.Expanded += FormRegionPrivacyStatus_Expanded;
Dean@1390
   526
Dean@1390
   527
                    this.TimerRefresh.Tick += TimerRefresh_Tick;
Dean@1390
   528
                    this.isStarted = true;
Dean@1390
   529
                }
Dean@1390
   530
            }
Dean@1390
   531
            else
Dean@1390
   532
            {
Dean@1390
   533
                // Disconnect events
Dean@1390
   534
                CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1390
   535
                this.FormControlPrivacyStatusChild.PrivacyViewClick -= FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@1390
   536
                this.Expanded -= FormRegionPrivacyStatus_Expanded;
Dean@1390
   537
Dean@1390
   538
                // Stop and disconnect the refresh timer
Dean@1390
   539
                this.TimerRefresh.Stop();
Dean@1390
   540
                this.TimerRefresh.Enabled = false;
Dean@1390
   541
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@1390
   542
Dean@1390
   543
                this.isStarted = false;
Dean@1390
   544
            }
Dean@1390
   545
Dean@1390
   546
            return;
Dean@1390
   547
        }
Dean@1390
   548
Dean@1390
   549
        /// <summary>
Dean@1390
   550
        /// Resolves all recipients of the Outlook mail item.
Dean@1390
   551
        /// </summary>
Dean@1390
   552
        private void ResolveAllRecipients()
Dean@1390
   553
        {
Dean@1390
   554
            if ((this.isEnabled) &&
Dean@1390
   555
                (this.cryptableMailItem != null))
Dean@1390
   556
            {
Dean@1390
   557
                /*
Dean@1390
   558
                 * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
Dean@1390
   559
                 * This is because the resolve process can modify the contents of the mail item which triggers an event.
Dean@1390
   560
                 * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
Dean@1390
   561
                 * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that
Dean@1390
   562
                 * cannot be resolved(no address).
Dean@1390
   563
                 */
Thomas@1551
   564
                try
Thomas@1551
   565
                {
Thomas@1551
   566
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
Thomas@1551
   567
                    this.cryptableMailItem.ResolveAllRecipients();
Thomas@1551
   568
                    this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
Thomas@1551
   569
                }
Thomas@1551
   570
                catch (Exception e)
Thomas@1551
   571
                {
Thomas@1551
   572
                    Log.Error("Error resolving recipients. " + e.Message);
Thomas@1551
   573
                }
Dean@1390
   574
            }
Dean@1390
   575
Dean@1390
   576
            return;
Dean@1390
   577
        }
Dean@1390
   578
Dean@159
   579
        /**************************************************************
Dean@159
   580
         * 
Dean@159
   581
         * Event Handling
Dean@159
   582
         * 
Dean@159
   583
         *************************************************************/
Dean@159
   584
Dean@159
   585
        /// <summary>
Dean@680
   586
        /// Event handler that is called when the form region is displayed.
Thomas@1795
   587
        /// This is called each time the form region loses then regains visibility 
Dean@680
   588
        /// (for example an other email is selected then back to this one).
Dean@159
   589
        /// </summary>
Dean@1461
   590
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, EventArgs e)
Dean@159
   591
        {
Dean@1390
   592
            // Set mail item
Dean@1390
   593
            if (this.OutlookItem is Outlook.MailItem)
Dean@1390
   594
            {
Dean@1390
   595
                this.cryptableMailItem = new CryptableMailItem((Outlook.MailItem)this.OutlookItem);
Dean@1390
   596
            }
Dean@1390
   597
            else
Dean@1390
   598
            {
Dean@1390
   599
                this.cryptableMailItem = null;
Dean@1390
   600
            }
Dean@1169
   601
Dean@1390
   602
            // Connect cryptable mail item events
Dean@1390
   603
            if (this.cryptableMailItem != null)
Dean@680
   604
            {
Dean@1197
   605
                try
Dean@1197
   606
                {
Dean@1390
   607
                    this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@1390
   608
                    this.cryptableMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
Dean@1390
   609
                    this.cryptableMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
Dean@1390
   610
                    this.cryptableMailItem.Send += MailItem_Send;
Thomas@1737
   611
                    this.cryptableMailItem.Open += MailItem_Open;
Dean@1197
   612
                }
Dean@1197
   613
                catch { }
Dean@179
   614
            }
Dean@159
   615
Dean@1390
   616
            // Update pEp enabled status
Thomas@1795
   617
            this.SetIsEnabled((this.OutlookItem as Outlook.MailItem)?.GetEnableFormRegion() ?? false);
Dean@1390
   618
            if (this.isEnabled)
Dean@1390
   619
            {
Dean@1390
   620
                // Call the timer tick method manually to refresh data with no delay
Dean@1390
   621
                this.TimerRefresh_Tick(null, new EventArgs());
Dean@1390
   622
            }
Dean@179
   623
Dean@179
   624
            return;
Dean@159
   625
        }
Dean@159
   626
Dean@159
   627
        /// <summary>
Dean@159
   628
        /// Event handler for when the form region is closed.
Dean@159
   629
        /// </summary>
Thomas@1795
   630
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, EventArgs e)
Dean@159
   631
        {
Dean@1390
   632
            // Disconnect cryptable mail item
Dean@1390
   633
            if (this.cryptableMailItem != null)
Dean@179
   634
            {
Dean@179
   635
                try
Dean@179
   636
                {
Dean@1390
   637
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@1390
   638
                    this.cryptableMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
Dean@1390
   639
                    this.cryptableMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
Dean@1390
   640
                    this.cryptableMailItem.Open -= MailItem_Open;
Dean@1390
   641
                    this.cryptableMailItem.Send -= MailItem_Send;
Dean@179
   642
                }
Dean@179
   643
                catch { }
Dean@1390
   644
Dean@1390
   645
                this.cryptableMailItem.Dispose();
Dean@1390
   646
                this.cryptableMailItem = null;
Dean@179
   647
            }
Dean@179
   648
Dean@1390
   649
            // Disconnect other
Dean@1390
   650
            this.SetIsEnabled(false);
Dean@682
   651
Dean@179
   652
            return;
Dean@179
   653
        }
Dean@179
   654
Dean@179
   655
        /// <summary>
Dean@1384
   656
        /// Event handler for when the form region is expanded or collapsed.
Dean@1384
   657
        /// </summary>
Dean@1384
   658
        private void FormRegionPrivacyStatus_Expanded(object sender, EventArgs e)
Dean@1384
   659
        {
Dean@1385
   660
            /* This Outlook form region has a bug where clicking the '+' button to expand will also
Dean@1385
   661
             * raise a click event on the privacy status button. Basically, Outlook would fire the click event 
Dean@1385
   662
             * (FormControlPrivacyStatusChild_PrivacyViewClick) immediately after the region is expanded even though at 
Dean@1385
   663
             * the time it was clicked it was collapsed and the click already captured by the expander '+' button.
Dean@1385
   664
             * This code here works around that issue by disabling the manager form when the form region is collapsed.
Dean@1385
   665
             * Since the click event occurs BEFORE the expanded event, this will disable opening the manage privacy status
Dean@1385
   666
             * form in this situation.
Dean@1385
   667
             */
Dean@1390
   668
            this.isManagerFormEnabled = this.OutlookFormRegion.IsExpanded;
Dean@1384
   669
Dean@1384
   670
            return;
Dean@1384
   671
        }
Dean@1384
   672
Dean@1384
   673
        /// <summary>
Dean@1384
   674
        /// Event handler called after the monitor timer has elapsed.
Dean@1384
   675
        /// This should be used only for quick polling of changes in Outlook or the UI state.
Dean@1384
   676
        /// </summary>
Dean@1384
   677
        private void TimerMonitor_Tick(object sender, EventArgs e)
Dean@1384
   678
        {
Dean@1384
   679
            bool isExpandedCurrent;
Dean@1384
   680
Dean@1384
   681
            // Check for IsExpanded changes
Dean@1384
   682
            isExpandedCurrent = this.OutlookFormRegion.IsExpanded;
Dean@1384
   683
            if ((this.isExpandedPrevious == null) ||
Dean@1384
   684
                ((bool)this.isExpandedPrevious != isExpandedCurrent))
Dean@1384
   685
            {
Dean@1384
   686
                this.Expanded?.Invoke(this, new EventArgs());
Dean@1384
   687
            }
Dean@1384
   688
            this.isExpandedPrevious = isExpandedCurrent;
Dean@1384
   689
Dean@1384
   690
            return;
Dean@1384
   691
        }
Dean@1384
   692
Dean@1384
   693
        /// <summary>
Dean@179
   694
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   695
        /// </summary>
Dean@179
   696
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   697
        {
Dean@1402
   698
            bool tryAgain = false;
Thomas@1547
   699
#pragma warning disable 219
Dean@1402
   700
            bool markForDownload = false;
Thomas@1547
   701
#pragma warning restore 219
Dean@179
   702
            this.TimerRefresh.Enabled = false; // Only once
Dean@1402
   703
            Outlook.OlDownloadState dlState;
markus@201
   704
Dean@709
   705
            /* The Refresh/UI_Update process is a little more complicated here than other forms.
Dean@709
   706
             * There are the following components:
Dean@709
   707
             *   1. TimerRefresh_Tick
Dean@709
   708
             *        This is the main timer tick event handler called any time
Dean@1130
   709
             *        a refresh was requested (RequestRatingAndUIUpdate) and hasn't been run yet. 
Dean@1001
   710
             *        A refresh is requested either at initialization or when a property changes
Dean@709
   711
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
Dean@709
   712
             *        property change events could occur rapidy, but only one refresh should
Dean@709
   713
             *        occur for performance reasons.
Dean@709
   714
             * 
Dean@1130
   715
             *   2. ImmediateRatingAndUIUpdate
Dean@1307
   716
             *        This will re-calculate the mail item's rating.
Dean@1307
   717
             *        This internally just calls CryptableMailItem.StartProcessing.
Dean@709
   718
             * 
Dean@1130
   719
             *   3. MailItem_ProcessingCompleted
Dean@1307
   720
             *        When processing of the mail item is complete, this even handler will be called.
Dean@1130
   721
             *        This will update the UI with the latest rating then call StartGetMirror.
Dean@709
   722
             *        The CopyStateToUI method is used which means any open privacy status form will also
Dean@709
   723
             *        be updated.
Dean@709
   724
             * 
Dean@709
   725
             *   4. MailItem_GetMirrorComplete
Dean@1402
   726
             *        This is the final step in updating the UI, after a mirror is located, it's contents
Dean@1307
   727
             *        will be shown in the unencrypted preview.
Dean@709
   728
             * 
Dean@709
   729
             * The general calling sequence is as shown above 1->4 with each component calling the next.
Dean@709
   730
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
Dean@709
   731
             */
Dean@709
   732
Dean@179
   733
            // Ensure the tick method is not called more than once
Dean@1390
   734
            if ((this.isEnabled) &&
Dean@1402
   735
                (refreshOngoing == false))
Dean@179
   736
            {
Dean@1402
   737
                this.refreshOngoing = true;
Dean@1402
   738
Thomas@1419
   739
                if ((this.cryptableMailItem != null) &&
Thomas@1419
   740
                    (this.cryptableMailItem.IsBeingProcessed == false))
Dean@1402
   741
                {
Dean@1402
   742
                    // Attempt to get the download state
Dean@1402
   743
                    try
Dean@1402
   744
                    {
Dean@1402
   745
                        dlState = this.cryptableMailItem.DownloadState;
Dean@1402
   746
                    }
Dean@1402
   747
                    catch (Exception ex)
Dean@1402
   748
                    {
Dean@1402
   749
                        Log.Warning("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
Dean@1402
   750
Dean@1402
   751
                        // Assume everything is downloaded, but try to download again as well
Dean@1402
   752
                        dlState = Outlook.OlDownloadState.olFullItem;
Dean@1402
   753
                        markForDownload = true;
Dean@1402
   754
                    }
Dean@1402
   755
Dean@1402
   756
                    if (dlState == Outlook.OlDownloadState.olFullItem)
Dean@1402
   757
                    {
Dean@1402
   758
                        this.ImmediateRatingAndUIUpdate();
Dean@1402
   759
                    }
Dean@1402
   760
                    else
Dean@1402
   761
                    {
Dean@1402
   762
                        markForDownload = true;
Dean@1402
   763
                    }
Dean@1402
   764
Thomas@1529
   765
                    /* 12/16/2016: It could be verified via testing that setting the MarkForDownload property
Thomas@1529
   766
                     * can be a way to crash Outlook under certain circumstances (e.g. with IMAP on Outlook 2010 or on Windows 7). 
Thomas@1529
   767
                     * This then is not caught by a try/catch block and therefore has to be considered an Outlook bug.
Thomas@1529
   768
                     * It seems that in newer versions of Outlook/Windows, they fixed it, causing exceptions, if at all.
Thomas@1529
   769
                     * For now, the following is commented out to prevent a crash. If at any point we see that header-only
Thomas@1529
   770
                     * messages cause bigger issues, this decision has to be reevaluated.
Thomas@1529
   771
                     */
Thomas@1529
   772
                    //if (markForDownload)
Thomas@1529
   773
                    //{
Thomas@1529
   774
                    //    // Try to mark the message for full download
Thomas@1529
   775
                    //    try
Thomas@1529
   776
                    //    {
Thomas@1529
   777
                    //        this.cryptableMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Thomas@1529
   778
                    //        tryAgain = true;
Thomas@1529
   779
                    //    }
Thomas@1529
   780
                    //    catch (Exception ex)
Thomas@1529
   781
                    //    {
Thomas@1529
   782
                    //        Log.Warning("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
Thomas@1529
   783
                    //    }
Thomas@1529
   784
                    //}
Dean@1402
   785
                }
Thomas@1419
   786
                else
Thomas@1419
   787
                {
Thomas@1419
   788
                    repeatProcessing = true;
Thomas@1419
   789
                }
Dean@1402
   790
Dean@1402
   791
                // Set the timer to refresh again later automatically
Dean@1402
   792
                if (tryAgain)
Dean@1402
   793
                {
Dean@1402
   794
                    this.TimerRefresh.Interval = 100;
Dean@1402
   795
                    this.TimerRefresh.Enabled = true;
Dean@1402
   796
                }
Dean@1402
   797
Dean@1402
   798
                this.refreshOngoing = false;
Dean@179
   799
            }
Dean@175
   800
Dean@159
   801
            return;
Dean@159
   802
        }
Dean@159
   803
Dean@169
   804
        /// <summary>
Dean@1096
   805
        /// Event handler for when the processing is completed in the associated mail item.
Dean@709
   806
        /// This will then update the form region UI and the privacy status window as needed.
Dean@680
   807
        /// </summary>
Dean@1461
   808
        private void MailItem_ProcessingCompleted(object sender, MsgProcessor.ProcessingCompletedEventArgs e)
Dean@680
   809
        {
Dean@1249
   810
            Log.Verbose("MailItem_ProcessingComplete: Decryption completed.");
Dean@740
   811
Dean@680
   812
            try
Dean@680
   813
            {
Dean@680
   814
                // Marshal code back to UI thread as necessary
Dean@680
   815
                this.Invoke(new Action(() =>
Dean@680
   816
                    {
Dean@1390
   817
                        if (this.isEnabled)
Dean@1390
   818
                        {
Thomas@1505
   819
                            try
Thomas@1505
   820
                            {
Thomas@1505
   821
                                this.FormControlPrivacyStatusChild.DisplayState.Rating = e.ProcessedRating;
Thomas@1505
   822
                                RibbonCustomizations.Invalidate();
Thomas@1583
   823
                                this.UpdateManagePrivacyStatusForm(); // Only update the rating
Thomas@1505
   824
                            }
Thomas@1505
   825
                            catch (Exception ex)
Thomas@1505
   826
                            {
Thomas@1505
   827
                                Log.Verbose("MailItem_ProcessingComplete: Error. " + ex.ToString());
Thomas@1505
   828
                            }
Dean@709
   829
Thomas@1547
   830
                            if (repeatProcessing &&
Thomas@1547
   831
                                (repeatCounter++ < maxRepeatCount))
Dean@747
   832
                            {
Thomas@1419
   833
                                repeatProcessing = false;
Thomas@1419
   834
                                this.RequestRatingAndUIUpdate();
Thomas@1419
   835
                            }
Thomas@1419
   836
                            else
Thomas@1419
   837
                            {
Thomas@1470
   838
                                /* Check if the mail item is in Outbox and update the inspector window if possible.
Thomas@1470
   839
                                 * This is the case when a submitted, but not yet sent email is opened again when working 
Thomas@1470
   840
                                 * offline or without internet connection. Without this update, Outlook removes the message class 
Thomas@1470
   841
                                 * "IPM.Note.SMIME.MultipartSigned" at the next send event and the message gets invalid and can't be
Thomas@1470
   842
                                 * opened again (i.e. is lost).
Thomas@1458
   843
                                 */
Thomas@1458
   844
                                try
Thomas@1458
   845
                                {
Thomas@1458
   846
                                    Outlook.MailItem omi = this.OutlookItem as Outlook.MailItem;
Thomas@1458
   847
Thomas@1458
   848
                                    if ((omi != null) &&
Thomas@1458
   849
                                        (omi.GetIsSubmitted()) &&
Thomas@1458
   850
                                        (omi.GetIsDraft()))
Thomas@1458
   851
                                    {
Thomas@1458
   852
                                        var insp = this.OutlookFormRegion.Inspector;
Thomas@1458
   853
Thomas@1458
   854
                                        if (insp != null)
Thomas@1458
   855
                                        {
Thomas@1547
   856
                                            UpdateInspector();
Thomas@1458
   857
                                        }
Thomas@1458
   858
                                    }
Thomas@1458
   859
                                }
Thomas@1458
   860
                                catch (Exception ex)
Thomas@1458
   861
                                {
Thomas@1505
   862
                                    Log.Verbose("MailItem_ProcessingComplete: Error while checking if mail item is in outbox or updating inspector. " + ex.Message);
Thomas@1458
   863
                                }
Thomas@1458
   864
Thomas@1419
   865
                                /* Create the unencrypted preview if the mail item is encrypted and
Thomas@1419
   866
                                 * it is in an encrypted (untrusted) store
Thomas@1419
   867
                                 * 
Thomas@1419
   868
                                 * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Thomas@1419
   869
                                 * it also is initialized after FormRegionPreviewUnencrypted.
Thomas@1419
   870
                                 */
Thomas@1419
   871
                                try
Dean@1390
   872
                                {
Thomas@1419
   873
                                    if (this.cryptableMailItem.IsSecurelyStored)
Thomas@1419
   874
                                    {
Thomas@1419
   875
                                        Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
Thomas@1419
   876
                                        this.cryptableMailItem.StartGetMirror();
Thomas@1419
   877
                                    }
Thomas@1419
   878
                                    else
Thomas@1419
   879
                                    {
Dean@1483
   880
                                        this.SetNote();
Thomas@1419
   881
                                    }
Dean@1390
   882
                                }
Thomas@1419
   883
                                catch (Exception ex)
Dean@1390
   884
                                {
Thomas@1419
   885
                                    // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
Thomas@1419
   886
                                    // While rare, just log the issue and stop the process
Thomas@1419
   887
                                    Log.Warning("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
Dean@1390
   888
                                }
Dean@747
   889
                            }
Dean@709
   890
                        }
Dean@680
   891
                    }));
Dean@680
   892
            }
Dean@680
   893
            catch (Exception ex)
Dean@680
   894
            {
Dean@1249
   895
                Log.Error("MailItem_ProcessingComplete: Error setting UI state, " + ex.ToString());
Dean@680
   896
            }
Dean@680
   897
Thomas@1505
   898
            this.processingOngoing = false;
Thomas@1505
   899
Dean@680
   900
            return;
Dean@680
   901
        }
Dean@680
   902
Dean@680
   903
        /// <summary>
Dean@709
   904
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
Dean@709
   905
        /// This will then update the unencrypted preview in the UI.
Dean@709
   906
        /// </summary>
Dean@1096
   907
        private void MailItem_GetMirrorCompleted(object sender, CryptableMailItem.GetMirrorCompletedEventArgs e)
Dean@709
   908
        {
Dean@709
   909
            try
Dean@709
   910
            {
Dean@709
   911
                // Marshal code back to UI thread as necessary
Dean@709
   912
                this.Invoke(new Action(() =>
Dean@709
   913
                    {
Dean@1390
   914
                        if (this.isEnabled)
Dean@1390
   915
                        {
Dean@1390
   916
                            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   917
Dean@1390
   918
                            if ((e.Mirror == null) &&
Dean@1390
   919
                                (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.Failure))
Dean@910
   920
                            {
Dean@1483
   921
                                this.SetNote(Properties.Resources.Message_OpenError);
Dean@1390
   922
                                Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
Dean@910
   923
                            }
Dean@1390
   924
                            else if ((e.Mirror == null) &&
Dean@1390
   925
                                     (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.FailureNoConnection))
Dean@1390
   926
                            {
Dean@1483
   927
                                this.SetNote(Properties.Resources.Message_DecryptionNoConnection);
Dean@1390
   928
                                Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
Dean@1390
   929
                            }
Dean@1390
   930
                            else
Dean@1390
   931
                            {
Dean@1476
   932
                                if (formRegions != null)
Dean@1390
   933
                                {
Dean@1476
   934
                                    if ((formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@1476
   935
                                        (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@1476
   936
                                    {
Dean@1483
   937
                                        formRegions.FormRegionPreviewUnencrypted.DisplayState.SetMessage(e.Mirror);
Dean@1476
   938
                                    }
Dean@1476
   939
Dean@1390
   940
                                    Log.Verbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
Dean@1390
   941
                                }
Dean@1390
   942
                            }
Dean@758
   943
Dean@1390
   944
                            // Display the mirror if necessary
Dean@1390
   945
                            if ((this.cryptableMailItem != null) &&
Dean@1390
   946
                                (this.displayMirrorRequested))
Dean@1390
   947
                            {
Dean@1390
   948
                                this.cryptableMailItem.DisplayMirror();
Dean@1390
   949
                                this.displayMirrorRequested = false;
Dean@1390
   950
                            }
Dean@758
   951
                        }
Dean@709
   952
                    }));
Dean@709
   953
            }
Dean@709
   954
            catch (Exception ex)
Dean@709
   955
            {
Dean@1249
   956
                Log.Error("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
Dean@709
   957
            }
Dean@709
   958
Dean@709
   959
            return;
Dean@709
   960
        }
Dean@709
   961
Dean@709
   962
        /// <summary>
Dean@807
   963
        /// Event handler for the static mail item encrypted conversation cache updated.
Dean@807
   964
        /// This event is fired after the conversation cache has been updated with parent information following a
Dean@807
   965
        /// forward or reply mail item event (on a different, likely parent, mail item).
Dean@807
   966
        /// </summary>
Dean@807
   967
        private void CryptableMailItem_EncryptedConversationCacheUpdated(object sender, EventArgs e)
Dean@807
   968
        {
Dean@1390
   969
            if (this.isEnabled)
Dean@1390
   970
            {
Dean@1390
   971
                // Process the mail item now that the cache is updated
Dean@1390
   972
                this.cryptableMailItem.SetIsOriginallyEncryptedByCache();
Dean@807
   973
Dean@1390
   974
                // Remove the event handler -- this can only be processed once because it's timing dependent
Dean@1390
   975
                CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1390
   976
            }
Dean@807
   977
Dean@807
   978
            return;
Dean@807
   979
        }
Dean@807
   980
Dean@807
   981
        /// <summary>
Dean@169
   982
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   983
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
   984
        /// </summary>
Dean@169
   985
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   986
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   987
        /// and the inspector is not displayed.</param>
Dean@169
   988
        private void MailItem_Open(ref bool cancel)
Dean@159
   989
        {
Dean@758
   990
            bool result;
Dean@758
   991
markus@1582
   992
            if ((this.isEnabled) 
markus@1582
   993
                && (this.cryptableMailItem != null)                 
markus@1582
   994
                && (this.cryptableMailItem.IsSecurelyStored)
markus@1582
   995
                && (this.cryptableMailItem.IsIncoming || this.cryptableMailItem.IsOriginallyEncrypted || !this.cryptableMailItem.IsDraft))
Dean@169
   996
            {
Dean@758
   997
                // Try to open the mirror
Dean@1390
   998
                result = this.cryptableMailItem.DisplayMirror();
Dean@758
   999
Dean@758
  1000
                if (result == false)
Dean@758
  1001
                {
Dean@758
  1002
                    // Set flag to open after decryption/mirror location
Dean@758
  1003
                    this.displayMirrorRequested = true;
Dean@758
  1004
                }
Dean@758
  1005
Dean@758
  1006
                // Always cancel opening the original
Dean@169
  1007
                cancel = true;
Dean@169
  1008
            }
Dean@169
  1009
Dean@169
  1010
            return;
Dean@159
  1011
        }
Dean@159
  1012
Dean@169
  1013
        /// <summary>
Dean@682
  1014
        /// Event handler for when a mail item is sent.
Dean@682
  1015
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
Dean@682
  1016
        /// </summary>
Dean@682
  1017
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@682
  1018
        /// If the event procedure sets this argument to True, the send operation is not completed 
Dean@682
  1019
        /// and the inspector is left open.</param>
Dean@682
  1020
        private void MailItem_Send(ref bool cancel)
Dean@682
  1021
        {
Dean@807
  1022
            DialogResult result;
Dean@807
  1023
Dean@1390
  1024
            if (this.isEnabled)
Dean@807
  1025
            {
Thomas@1505
  1026
                if ((this.cryptableMailItem.IsBeingProcessed) ||
Thomas@1505
  1027
                    (this.processingOngoing))
Thomas@1505
  1028
                {
Thomas@1505
  1029
                    Log.Verbose("MailItem_Send cancelled. Mail item still being processed.");
Thomas@1505
  1030
                    cancel = true;
Thomas@1505
  1031
                    return;
Thomas@1505
  1032
                }
Thomas@1505
  1033
Dean@1390
  1034
                if ((Globals.ThisAddIn.Settings.IsSecurityLossWarningEnabled) &&
Dean@1390
  1035
                    (this.cryptableMailItem.IsOriginallyEncrypted) &&
Dean@1390
  1036
                    (this.FormControlPrivacyStatusChild.DisplayState.Rating < pEpRating.pEpRatingUnreliable))
Dean@1390
  1037
                {
Thomas@1318
  1038
Thomas@1318
  1039
#if READER_RELEASE_MODE
Dean@1390
  1040
                    FormReaderSplash warningMessage = new FormReaderSplash(true);
Dean@1390
  1041
                    result = warningMessage.ShowDialog();
Thomas@1318
  1042
Dean@1390
  1043
                    if (result != DialogResult.OK)
Dean@1390
  1044
                    {
Dean@1390
  1045
                        // Cancel sending
Dean@1390
  1046
                        cancel = true;
Dean@1390
  1047
                    }
Dean@1390
  1048
#else
Dean@1390
  1049
                    result = System.Windows.Forms.MessageBox.Show(this.ParentForm,
Dean@1390
  1050
                                                                  pEp.Properties.Resources.Message_WarningSecurityLoss,
Dean@1390
  1051
                                                                  pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@1390
  1052
                                                                  MessageBoxButtons.YesNo,
Dean@1390
  1053
                                                                  MessageBoxIcon.Warning);
Dean@1390
  1054
Dean@1390
  1055
                    if (result == DialogResult.No)
Dean@1390
  1056
                    {
Dean@1390
  1057
                        // Cancel sending
Dean@1390
  1058
                        cancel = true;
Dean@1390
  1059
                    }
Dean@1390
  1060
#endif
Dean@1390
  1061
                }
Dean@1390
  1062
Dean@1390
  1063
                // Stop and disconnect the refresh timer
Dean@1390
  1064
                // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
Dean@1390
  1065
                if (cancel == false)
Thomas@1318
  1066
                {
Dean@1390
  1067
                    this.TimerRefresh.Stop();
Dean@1390
  1068
                    this.TimerRefresh.Enabled = false;
Dean@1390
  1069
                    this.TimerRefresh.Tick -= TimerRefresh_Tick;
Thomas@1318
  1070
                }
Dean@807
  1071
            }
Dean@682
  1072
Dean@682
  1073
            return;
Dean@682
  1074
        }
Dean@682
  1075
Dean@682
  1076
        /// <summary>
Dean@169
  1077
        /// Event handler for when a mail item property is changed.
Dean@169
  1078
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
  1079
        /// </summary>
Dean@169
  1080
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@445
  1081
        private void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
Dean@159
  1082
        {
Dean@1349
  1083
            switch (e.PropertyName.ToUpperInvariant())
Dean@159
  1084
            {
Dean@1390
  1085
                case "BCC":
Dean@1390
  1086
                    {
Dean@1390
  1087
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
Dean@1390
  1088
                        break;
Dean@1390
  1089
                    }
Dean@1390
  1090
                case "CC":
Dean@1390
  1091
                    {
Dean@1390
  1092
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
Dean@1390
  1093
                        break;
Dean@1390
  1094
                    }
Dean@1390
  1095
                case "SENTONBEHALFOFNAME":
Dean@1390
  1096
                    {
Dean@1390
  1097
                        // Always fired with "SENDUSINGACCOUNT" so is ignored
Dean@1390
  1098
                        break;
Dean@1390
  1099
                    }
Dean@1390
  1100
                case "SENDUSINGACCOUNT":
Dean@1390
  1101
                    {
Dean@1390
  1102
                        // Update pEp enabled status
Thomas@1795
  1103
                        this.SetIsEnabled((this.OutlookItem as Outlook.MailItem)?.GetEnableFormRegion() ?? false);
Dean@1390
  1104
Dean@1390
  1105
                        // Refresh the UI
Dean@1390
  1106
                        this.ResolveAllRecipients();
Dean@1390
  1107
                        this.RequestRatingAndUIUpdate();
Dean@1390
  1108
                        RibbonCustomizations.Invalidate();
Dean@1390
  1109
Dean@1390
  1110
                        break;
Dean@1390
  1111
                    }
Dean@179
  1112
                case "TO":
Dean@1390
  1113
                    {
Dean@1390
  1114
                        if (this.isEnabled)
Dean@1390
  1115
                        {
Dean@1390
  1116
                            this.RequestRatingAndUIUpdate();
Dean@1390
  1117
                        }
Dean@1390
  1118
                        break;
Dean@1390
  1119
                    }
Dean@159
  1120
            }
Dean@169
  1121
Dean@169
  1122
            return;
Dean@159
  1123
        }
Dean@159
  1124
Dean@179
  1125
        /// <summary>
Thomas@1587
  1126
        /// Event handler for when a handshake dialog was updated.
Thomas@1583
  1127
        /// </summary>
Thomas@1587
  1128
        private void HandshakeDialog_Updated(object sender, EventArgs e)
Thomas@1583
  1129
        {
Thomas@1633
  1130
            // Update current form region
Thomas@1587
  1131
            this.RequestRatingAndUIUpdate();
Thomas@1633
  1132
Thomas@1633
  1133
            /* If a handshake is performed while having the same message open both in an inspector
Thomas@1633
  1134
             * and an explorer window, the one that didn't trigger the handshake won't get updated
Thomas@1633
  1135
             * automatically. Therefore, after a handshake, we perform a search for all form regions
Thomas@1633
  1136
             * of type FormRegionPrivacyStatus and update the associated cryptable mail items.
Thomas@1633
  1137
             */
Thomas@1633
  1138
            try
Thomas@1633
  1139
            {
Thomas@1633
  1140
                // Get all form regions
Thomas@1633
  1141
                var formRegions = Globals.FormRegions;
Thomas@1633
  1142
Thomas@1633
  1143
                foreach (var formRegion in formRegions)
Thomas@1633
  1144
                {
Thomas@1633
  1145
                    var fr = formRegion as FormRegionPrivacyStatus;
Thomas@1633
  1146
Thomas@1633
  1147
                    if (fr != null)
Thomas@1633
  1148
                    {
Thomas@1633
  1149
                        // Only process the ones that differ from the one that triggered the update.
Thomas@1633
  1150
                        if (fr.cryptableMailItem.Equals(this.cryptableMailItem) == false)
Thomas@1633
  1151
                        {
Thomas@1633
  1152
                            fr.cryptableMailItem.StartProcessing();
Thomas@1633
  1153
                        }
Thomas@1633
  1154
                    }
Thomas@1633
  1155
                }
Thomas@1633
  1156
            }
Thomas@1633
  1157
            catch (Exception ex)
Thomas@1633
  1158
            {
Thomas@1633
  1159
                Log.Error("HandshakeDialog_Updated: Error updating form regions. " + ex.Message);
Thomas@1633
  1160
            }
Thomas@1587
  1161
        }
Thomas@1587
  1162
Thomas@1587
  1163
        /// <summary>
Thomas@1587
  1164
        /// Event handler for when a handshake dialog was closed.
Thomas@1587
  1165
        /// </summary>
Thomas@1587
  1166
        private void HandshakeDialog_Closed(object sender, EventArgs e)
Thomas@1587
  1167
        {
Thomas@1587
  1168
            if (this.handshakeDialog != null)
Thomas@1587
  1169
            {
Thomas@1587
  1170
                this.handshakeDialog.OnUpdateStatus -= HandshakeDialog_Updated;
Thomas@1587
  1171
                this.handshakeDialog.Closed -= HandshakeDialog_Closed;
Thomas@1587
  1172
                this.handshakeDialog = null;
Thomas@1587
  1173
            }
Thomas@1583
  1174
        }
Thomas@1583
  1175
Thomas@1583
  1176
        /// <summary>
Dean@1021
  1177
        /// Event handler for when the manager form is closed.
Dean@1021
  1178
        /// </summary>
Dean@1021
  1179
        private void ManagerForm_FormClosed(object sender, FormClosedEventArgs e)
Dean@1021
  1180
        {
Dean@1021
  1181
            // Remove the reference held in the FormRegionPrivacyStatus so it stops getting updated
Dean@1021
  1182
            if (this.managerForm != null)
Dean@1021
  1183
            {
Dean@1021
  1184
                this.managerForm.FormClosed -= ManagerForm_FormClosed;
Dean@1021
  1185
                this.managerForm = null;
Dean@1021
  1186
            }
Dean@1021
  1187
Dean@1021
  1188
            return;
Dean@1021
  1189
        }
Dean@1021
  1190
Dean@1021
  1191
        /// <summary>
Dean@859
  1192
        /// Event handler for when the pEp website hyperlink is clicked.
Dean@859
  1193
        /// </summary>
Dean@859
  1194
        private void LinkLabelUpgrade_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Dean@859
  1195
        {
Dean@966
  1196
            try
Dean@966
  1197
            {
Dean@1007
  1198
                Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
Dean@966
  1199
            }
Dean@966
  1200
            catch (Exception ex)
Dean@966
  1201
            {
Dean@1249
  1202
                Log.Error("LinkLabelUpgrade_LinkClicked: Unable to open website link, " + ex.ToString());
Dean@966
  1203
            }
Dean@966
  1204
Dean@859
  1205
            return;
Dean@859
  1206
        }
Dean@1047
  1207
Dean@1047
  1208
        /// <summary>
Dean@1086
  1209
        /// Event handler for when the privacy view button is clicked within the form control.
Dean@1047
  1210
        /// </summary>
Dean@1086
  1211
        private void FormControlPrivacyStatusChild_PrivacyViewClick(object sender, System.Windows.RoutedEventArgs e)
Dean@1047
  1212
        {
Dean@1390
  1213
            if (this.isManagerFormEnabled)
Dean@1384
  1214
            {
Dean@1384
  1215
                this.RequestRatingAndUIUpdate();
Dean@1384
  1216
                this.BuildAndShowManager();
Dean@1384
  1217
            }
Dean@1384
  1218
Dean@1047
  1219
            return;
Dean@1047
  1220
        }
vb@133
  1221
    }
vb@133
  1222
}