UI/FormRegionPrivacyStatus.cs
author Dean Looyengoed
Tue, 21 Jun 2016 18:51:00 +0200
changeset 1021 f6abdba046f0
parent 1018 4d85cb40cc4b
child 1047 f004ad571864
permissions -rw-r--r--
Remove reference to FormManagePrivacyStatus when it's closed.
Dean@159
     1
´╗┐using pEpCOMServerAdapterLib;
Dean@159
     2
using System;
vb@133
     3
using System.Collections.Generic;
Dean@445
     4
using System.ComponentModel;
Dean@859
     5
using System.Diagnostics;
Dean@438
     6
using System.Drawing;
Dean@854
     7
using System.Globalization;
Dean@159
     8
using System.Runtime.InteropServices;
vb@133
     9
using System.Windows.Forms;
vb@133
    10
using Outlook = Microsoft.Office.Interop.Outlook;
vb@133
    11
vb@133
    12
namespace pEp
vb@133
    13
{
Dean@159
    14
    /// <summary>
Dean@312
    15
    /// Partial class for the privacy status form region that is displayed below every outlook message.
Dean@159
    16
    /// </summary>
Dean@910
    17
    internal partial class FormRegionPrivacyStatus
vb@133
    18
    {
vb@133
    19
        #region Form Region Factory
vb@133
    20
vb@133
    21
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
Dean@312
    22
        [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegionPrivacyStatus")]
Dean@312
    23
        public partial class FormRegionPrivacyStatusFactory
vb@133
    24
        {
vb@133
    25
            // Occurs before the form region is initialized.
vb@133
    26
            // To prevent the form region from appearing, set e.Cancel to true.
vb@133
    27
            // Use e.OutlookItem to get a reference to the current Outlook item.
Dean@312
    28
            private void FormRegionPrivacyStatus_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
vb@133
    29
            {
Dean@985
    30
                //WindowFormRegionCollection formRegions;
Dean@327
    31
Dean@327
    32
                /* There is a Microsoft bug at least in Outlook 2013 and Windows 8.1
Dean@327
    33
                 * This bug causes multiple PrivacyStatus form regions to appear stacked on top of each other.
Dean@327
    34
                 * To trigger this bug, on an unencrypted server, click reply to compose an in-line response.
Dean@327
    35
                 * Then click to another tab such as People or Tasks. Then click back on the mail tab to view 
Dean@327
    36
                 * the original email again. Two form regions will be visible: 
Dean@327
    37
                 * (1) for the status during the in-line reply and 
Dean@327
    38
                 * (2) for the status of only the received message.
Dean@327
    39
                 * 
Dean@327
    40
                 * To fix this bug, any existing form regions are found and closed when initializing a new privacy status
Dean@327
    41
                 * form region.
Dean@327
    42
                 */
Dean@985
    43
                /*
Dean@327
    44
                try
Dean@327
    45
                {
Dean@987
    46
                    formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@327
    47
                    if (formRegions.FormRegionPrivacyStatus != null)
Dean@327
    48
                    {
Dean@985
    49
                        // Note: there seems to be no way to actually close the form region.
Dean@985
    50
                        // Therefore, this is as close as possible to actually closing it.
Dean@985
    51
                        // The actual form regions will be cleaned up as soon as another email is selected.
Dean@327
    52
                        formRegions.FormRegionPrivacyStatus.OutlookFormRegion.Visible = false;
Dean@327
    53
                    }
Dean@327
    54
                }
Dean@327
    55
                catch { }
Dean@985
    56
                */
Dean@327
    57
                return;
vb@133
    58
            }
vb@133
    59
        }
vb@133
    60
vb@133
    61
        #endregion
vb@133
    62
Dean@159
    63
        /* Notes:
Dean@179
    64
         * 
Dean@159
    65
         * Use this.OutlookItem to get a reference to the current Outlook item.
Dean@159
    66
         * Use this.OutlookFormRegion to get a reference to the form region.
Dean@179
    67
         * 
Dean@179
    68
         * UI State Managment:
Dean@179
    69
         * 
Dean@179
    70
         * The UI state is almost entirely set from the associated mail item data.
Dean@179
    71
         * However, a separate state class is maintained to represent the UI as some separation is needed.
Dean@179
    72
         * This logical separation MUST be maintained throughout the code.
Dean@179
    73
         * Specific cases are noted where possible.
Dean@179
    74
         * 
Dean@356
    75
         * The separate privacy status manager form state is also managed here.
Dean@259
    76
         * 
Dean@159
    77
         */
Dean@159
    78
Dean@758
    79
        private CryptableMailItem       associatedMailItem     = null;
Dean@758
    80
        private bool                    eventsAreConnected     = false;
Dean@758
    81
        private bool                    initialized            = false;
Dean@758
    82
        private FormManagePrivacyStatus managerForm            = null;
Dean@758
    83
        private bool                    refreshOngoing         = false;
Dean@758
    84
        private bool                    displayMirrorRequested = false;
Dean@259
    85
Dean@250
    86
        // Data or controls not stored in the UI
Dean@179
    87
        private _pEp_color stateUIColorRating; // WARNING: Do NOT use this directly
vb@133
    88
Dean@159
    89
        /**************************************************************
Dean@159
    90
         * 
Dean@159
    91
         * Methods
Dean@159
    92
         * 
Dean@159
    93
         *************************************************************/
Dean@159
    94
Dean@171
    95
        /// <summary>
Dean@438
    96
        /// Determines if this form region is running in an inspector window.
Dean@438
    97
        /// If not true, it is assumed to be within an explorer window.
Dean@438
    98
        /// </summary>
Dean@438
    99
        /// <returns>True if an inspector window, false if within an explorer.</returns>
Dean@438
   100
        private bool IsWithinInspector()
Dean@438
   101
        {
Dean@438
   102
            bool isWithinInspector = false;
Dean@895
   103
            Outlook.Inspector insp = null;
Dean@438
   104
Dean@438
   105
            try
Dean@438
   106
            {
Dean@895
   107
                /* There are two potential methods to determine if the item is within an inspector.
Dean@895
   108
                 * (1) Use this.OutlookFormRegion.Parent which will contain either the Explorer or an Inspector
Dean@895
   109
                 *     A cast could be tried to an Explorer of this parent and if it fails, it's assumed to be an inspector
Dean@895
   110
                 * (2) Use this.OutlookFormRegion.Inspector which will contain the Inspector if it exists.
Dean@895
   111
                 *     This will fail or return null if this form region is not running within it's own Inspector window.
Dean@895
   112
                 */
Dean@895
   113
                insp = this.OutlookFormRegion.Inspector;
Dean@895
   114
Dean@895
   115
                if (insp != null)
Dean@895
   116
                {
Dean@895
   117
                    isWithinInspector = true;
Dean@895
   118
                }
Dean@438
   119
            }
Dean@438
   120
            catch
Dean@438
   121
            {
Dean@895
   122
                isWithinInspector = false;
Dean@438
   123
            }
Dean@438
   124
            finally
Dean@438
   125
            {
Dean@895
   126
                if (insp != null)
Dean@438
   127
                {
Dean@895
   128
                    Marshal.ReleaseComObject(insp);
Dean@895
   129
                    insp = null;
Dean@438
   130
                }
Dean@438
   131
            }
Dean@438
   132
Dean@438
   133
            return (isWithinInspector);
Dean@438
   134
        }
Dean@438
   135
Dean@438
   136
        /// <summary>
Dean@170
   137
        /// Sets the cryptable mail item associated with this encryption status panel.
Dean@170
   138
        /// The associated mail item can then be accessed through its local variable.
Dean@170
   139
        /// </summary>
Dean@170
   140
        private void SetAssociatedMailItem()
Dean@170
   141
        {
Dean@170
   142
            bool errorOccurred = false;
Dean@170
   143
            Outlook.MailItem omi = null;
Dean@170
   144
Dean@170
   145
            // Null check
Dean@170
   146
            if (!errorOccurred)
Dean@170
   147
            {
Dean@170
   148
                try
Dean@170
   149
                {
Dean@170
   150
                    if (this.OutlookItem == null)
Dean@170
   151
                    {
Dean@170
   152
                        errorOccurred = true;
Dean@170
   153
                    }
Dean@170
   154
                }
Dean@170
   155
                catch (COMException)
Dean@170
   156
                {
Dean@170
   157
                    errorOccurred = true;
Dean@170
   158
                }
Dean@170
   159
            }
Dean@170
   160
Dean@170
   161
            // Attempt to get and cast the outlook mail item
Dean@170
   162
            if (!errorOccurred)
Dean@170
   163
            {
Dean@170
   164
                try
Dean@170
   165
                {
Dean@170
   166
                    omi = (Outlook.MailItem)this.OutlookItem;
Dean@170
   167
                }
Dean@170
   168
                catch
Dean@170
   169
                {
Dean@170
   170
                    errorOccurred = true;
Dean@170
   171
                }
Dean@170
   172
            }
Dean@170
   173
Dean@170
   174
            // Finally set the associated mail item
Dean@170
   175
            if ((errorOccurred) ||
Dean@170
   176
                (omi == null))
Dean@170
   177
            {
Dean@170
   178
                this.associatedMailItem = null;
Dean@170
   179
            }
Dean@170
   180
            else
Dean@170
   181
            {
Dean@170
   182
                // Check if the associated mail item has already been set
Dean@170
   183
                if (this.associatedMailItem != null)
Dean@170
   184
                {
Dean@170
   185
                    // Only re-set the mail item if the EntryID has changed
Dean@170
   186
                    if (this.associatedMailItem.EntryID != omi.EntryID)
Dean@170
   187
                    {
Dean@170
   188
                        this.associatedMailItem = new CryptableMailItem(omi);
Dean@170
   189
                    }
Dean@170
   190
                }
Dean@170
   191
                else
Dean@170
   192
                {
Dean@170
   193
                    this.associatedMailItem = new CryptableMailItem(omi);
Dean@170
   194
                }
Dean@170
   195
            }
Dean@170
   196
Dean@170
   197
            return;
Dean@170
   198
        }
Dean@170
   199
Dean@179
   200
        /// <summary>
Dean@672
   201
        /// Completes the handshake process when the identity of a partner was previously marked as mistrusted.
Dean@281
   202
        /// </summary>
Dean@646
   203
        /// <param name="myself">The personal identity to complete the handshake with.</param>
Dean@646
   204
        /// <param name="partner">The identity of the partner to complete the handshake with.</param>
Dean@672
   205
        private void DoHandshakeForMistrustedKey(PEPIdentity myself,
Dean@672
   206
                                                 PEPIdentity partner)
Dean@281
   207
        {
Dean@281
   208
            DialogResult result;
Dean@281
   209
Dean@281
   210
            result = MessageBox.Show(this.ParentForm,
Dean@672
   211
                                     pEp.Properties.Resources.Message_WarningMistrustedKey,
Dean@378
   212
                                     pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@423
   213
                                     MessageBoxButtons.YesNo);
Dean@281
   214
Dean@423
   215
            if (result == DialogResult.Yes)
Dean@281
   216
            {
Dean@646
   217
                this.DoHandshake(myself, partner);
Dean@281
   218
            }
Dean@281
   219
Dean@281
   220
            return;
Dean@281
   221
        }
Dean@281
   222
Dean@281
   223
        /// <summary>
Dean@281
   224
        /// Completes the handshake process where the identity of a partner is confirmed.
Dean@179
   225
        /// </summary>
Dean@646
   226
        /// <param name="myself">The personal identity to complete the handshake with.</param>
Dean@646
   227
        /// <param name="partner">The identity of the partner to complete the handshake with.</param>
Dean@646
   228
        private void DoHandshake(PEPIdentity myself,
Dean@646
   229
                                 PEPIdentity partner)
vb@133
   230
        {
Dean@179
   231
            DialogResult result;
Dean@155
   232
            FormHandshake handshakeDialog;
Dean@155
   233
            FormHandshake.State state = new FormHandshake.State();
markus@201
   234
Dean@860
   235
            Globals.LogVerbose("DoHandshake: Handshake started.");
vb@139
   236
Dean@155
   237
            // Create and show handshake dialog
Dean@914
   238
            state = new FormHandshake.State(myself, partner);
Dean@155
   239
            handshakeDialog = new FormHandshake(state);
Dean@179
   240
            handshakeDialog.StartPosition = FormStartPosition.CenterScreen;
Dean@179
   241
            result = handshakeDialog.ShowDialog(this);
Dean@155
   242
Dean@646
   243
            this.ProcessDoHandshakeResult(result, partner);
Dean@356
   244
Dean@356
   245
            return;
Dean@356
   246
        }
Dean@356
   247
Dean@356
   248
        /// <summary>
Dean@356
   249
        /// Processes the result of the do handshake dialog after a user makes a selection.
Dean@356
   250
        /// </summary>
Dean@356
   251
        /// <param name="result">The result of the handshake dialog selection.</param>
Dean@356
   252
        /// <param name="partner">The identity of the partner to handshake with.</param>
Dean@356
   253
        private void ProcessDoHandshakeResult(DialogResult result,
Dean@384
   254
                                              PEPIdentity partner)
Dean@356
   255
        {
Dean@384
   256
            pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(partner.ToCOMType());
vb@133
   257
vb@133
   258
            switch (result)
vb@133
   259
            {
vb@133
   260
                case DialogResult.Yes:
Dean@279
   261
                    {
Dean@672
   262
                        // Check if key was previously mistrusted -- warning to user must be displayed earlier
Dean@672
   263
                        if (identityPartner.comm_type == _pEp_comm_type.pEp_ct_mistrusted)
Dean@281
   264
                        {
Dean@281
   265
                            ThisAddIn.pEp.key_reset_trust(ref identityPartner);
Dean@281
   266
                        }
Dean@281
   267
Dean@279
   268
                        identityPartner = ThisAddIn.pEp.trust_personal_key(ref identityPartner);
Dean@279
   269
Dean@1001
   270
                        this.ImmediateColorRatingAndUIUpdate();
Dean@279
   271
Dean@279
   272
                        // Update the manager form state
Dean@279
   273
                        if (this.managerForm != null)
Dean@279
   274
                        {
Dean@279
   275
                            this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   276
                        }
Dean@279
   277
Dean@279
   278
                        break;
Dean@279
   279
                    }
vb@133
   280
                case DialogResult.No:
Dean@279
   281
                    {
Dean@279
   282
                        ThisAddIn.pEp.key_compromized(ref identityPartner);
Dean@279
   283
Dean@1001
   284
                        this.ImmediateColorRatingAndUIUpdate();
Dean@279
   285
Dean@279
   286
                        // Update the manager form state
Dean@279
   287
                        if (this.managerForm != null)
Dean@279
   288
                        {
Dean@279
   289
                            this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   290
                        }
Dean@279
   291
Dean@279
   292
                        break;
Dean@279
   293
                    }
vb@133
   294
            }
Dean@279
   295
Dean@860
   296
            Globals.LogVerbose("ProcessDoHandshakeResult: Handshake complete.");
Dean@740
   297
Dean@279
   298
            return;
vb@133
   299
        }
vb@133
   300
Dean@179
   301
        /// <summary>
Dean@179
   302
        /// Reverses any past handshake confirmation by unconfirming the given identity partner.
Dean@179
   303
        /// </summary>
Dean@384
   304
        /// <param name="partner">The identity of the partner to unconfirm.</param>
Dean@384
   305
        private void UndoHandshake(PEPIdentity partner)
vb@133
   306
        {
Dean@384
   307
            pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(partner.ToCOMType());
Dean@280
   308
            ThisAddIn.pEp.key_reset_trust(ref identityPartner);
Dean@279
   309
Dean@1001
   310
            this.ImmediateColorRatingAndUIUpdate();
Dean@279
   311
Dean@279
   312
            // Update the manager form state
Dean@279
   313
            if (this.managerForm != null)
Dean@279
   314
            {
Dean@279
   315
                this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   316
            }
Dean@279
   317
Dean@279
   318
            return;
vb@133
   319
        }
vb@133
   320
Dean@161
   321
        /// <summary>
Dean@279
   322
        /// Builds a new manager form state using this encryption state/mail item current state.
Dean@279
   323
        /// </summary>
Dean@279
   324
        /// <returns>A new manager form state.</returns>
Dean@313
   325
        private FormManagePrivacyStatus.State GetManagerState()
Dean@279
   326
        {
Dean@737
   327
            int currIndex = 0;
Dean@527
   328
            bool isMyself;
Dean@364
   329
            string trustwordsShort;
Dean@364
   330
            string trustwordsFull;
Dean@384
   331
            List<PEPIdentity> identities;
Dean@384
   332
            PEPIdentity myIdentity;
Dean@259
   333
            _pEp_color identityPartnerColor;
Dean@259
   334
            SelectionItem item;
Dean@312
   335
            PrivacyState state = this.CopyUIToState();
Dean@313
   336
            FormManagePrivacyStatus.State managerState = new FormManagePrivacyStatus.State();
Dean@259
   337
Dean@769
   338
            /* Resolve all recipients -- this ensures the identities list is correctly populated
Dean@769
   339
             *
Dean@769
   340
             * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
Dean@769
   341
             * This is because the resolve process can modify the contents of the mail item which triggers an event.
Dean@769
   342
             * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
Dean@769
   343
             * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that 
Dean@769
   344
             * cannot be resolved (no address).
Dean@769
   345
             */
Dean@769
   346
            if (this.associatedMailItem != null)
Dean@769
   347
            {
Dean@769
   348
                this.associatedMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@769
   349
                this.associatedMailItem.ResolveAllRecipients();
Dean@769
   350
                this.associatedMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@769
   351
            }
Dean@332
   352
Dean@279
   353
            managerState.VisualState = state.Copy();
Dean@279
   354
            managerState.IsIncoming = this.associatedMailItem.IsIncoming;
Dean@259
   355
Dean@653
   356
            if (managerState.IsIncoming)
Dean@259
   357
            {
Dean@384
   358
                identities = new List<PEPIdentity>();
Dean@259
   359
Dean@259
   360
                // Add only one identity
Dean@638
   361
                if (this.associatedMailItem.From != null)
Dean@638
   362
                {
Dean@764
   363
                    identities.Add(this.associatedMailItem.From);
Dean@638
   364
                }
Dean@259
   365
            }
Dean@259
   366
            else
Dean@259
   367
            {
Dean@395
   368
                identities = this.associatedMailItem.Recipients;
Dean@259
   369
            }
Dean@259
   370
Dean@366
   371
            // Get my identity
Dean@633
   372
            myIdentity = this.associatedMailItem.Myself;
Dean@366
   373
Dean@279
   374
            // Add identities
Dean@394
   375
            identities = PEPIdentity.ToFlatList(identities);
Dean@279
   376
            managerState.Identities.Clear();
Dean@384
   377
            foreach (PEPIdentity ident in identities)
Dean@259
   378
            {
Dean@737
   379
                // Needed so index can be used within anonymous methods without getting into the 'outer variable trap' of currIndex
Dean@737
   380
                int index = currIndex;
Dean@737
   381
Dean@865
   382
                if (ident.IsAddressValid)
Dean@485
   383
                {
Dean@774
   384
                    // Update the partner identity to get a fingerprint, preserve IsForceUnencrypted
Dean@485
   385
                    pEp_identity_s partnerIdentity_s = ThisAddIn.pEp.update_identity(ident.ToCOMType());
Dean@485
   386
                    PEPIdentity partnerIdentity = new PEPIdentity(partnerIdentity_s);
Dean@774
   387
                    partnerIdentity.IsForceUnencrypted = ident.IsForceUnencrypted;
Dean@774
   388
Dean@1018
   389
                    try
Dean@1018
   390
                    {
Dean@1018
   391
                        identityPartnerColor = ThisAddIn.pEp.identity_color(partnerIdentity_s);
Dean@1018
   392
                    }
Dean@1018
   393
                    catch (COMException ex)
Dean@1018
   394
                    {
Dean@1018
   395
                        identityPartnerColor = _pEp_color.pEp_rating_undefined;
Dean@1018
   396
                        Globals.Log("GetManagerState: Failed to get identity color, " + ex.ToString());
Dean@1018
   397
                    }
Dean@1018
   398
Dean@910
   399
                    isMyself = PEPIdentity.GetIsOwnIdentity(partnerIdentity.Address);
Dean@724
   400
Dean@880
   401
                    // Calculate trustwords
Dean@880
   402
                    Globals.ThisAddIn.CalcTrustwords(myIdentity,
Dean@880
   403
                                                     partnerIdentity,
Dean@880
   404
                                                     out trustwordsShort,
Dean@880
   405
                                                     out trustwordsFull,
Dean@880
   406
                                                     Globals.CULTURE_CODE_DEFAULT);
Dean@880
   407
Dean@485
   408
                    item = new SelectionItem();
Dean@485
   409
                    item.TextLine1 = partnerIdentity.Username;
Dean@485
   410
                    item.TextLine2 = partnerIdentity.Address;
Dean@894
   411
                    item.Tag = (string.IsNullOrWhiteSpace(partnerIdentity.Fingerprint) ? null : trustwordsShort);
Dean@259
   412
Dean@485
   413
                    // Don't show both the user name and address if they are the same
Dean@485
   414
                    if ((item.TextLine1 != null) &&
Dean@485
   415
                        (item.TextLine2 != null) &&
Dean@485
   416
                        (item.TextLine1 == item.TextLine2))
Dean@485
   417
                    {
Dean@485
   418
                        item.IsTwoTextLinesVisible = false;
Dean@485
   419
                    }
Dean@332
   420
Dean@485
   421
                    // Set image
Dean@774
   422
                    if ((partnerIdentity.IsForceUnencryptedBool) &&
Dean@653
   423
                        (managerState.IsIncoming == false))
Dean@485
   424
                    {
Dean@641
   425
                        // Show the force unencrypted image indicating this is set by the user not the engine
Dean@642
   426
                        item.ItemImage = pEp.Properties.Resources.ImageForceUnencOn;
Dean@641
   427
                    }
Dean@641
   428
                    else
Dean@641
   429
                    {
Dean@641
   430
                        switch (PrivacyState.ConvertRatingToPrivacyColor(identityPartnerColor))
Dean@641
   431
                        {
Dean@641
   432
                            case PrivacyState.PrivacyColor.Green:
Dean@641
   433
                                item.ItemImage = pEp.Properties.Resources.ImagePrivacyStatusGreen;
Dean@641
   434
                                break;
Dean@641
   435
                            case PrivacyState.PrivacyColor.Yellow:
Dean@641
   436
                                item.ItemImage = pEp.Properties.Resources.ImagePrivacyStatusYellow;
Dean@641
   437
                                break;
Dean@641
   438
                            case PrivacyState.PrivacyColor.Red:
Dean@641
   439
                                item.ItemImage = pEp.Properties.Resources.ImagePrivacyStatusRed;
Dean@641
   440
                                break;
Dean@641
   441
                            case PrivacyState.PrivacyColor.NoColor:
Dean@641
   442
                                item.ItemImage = pEp.Properties.Resources.ImagePrivacyStatusNoColor;
Dean@641
   443
                                break;
Dean@641
   444
                            default:
Dean@641
   445
                                item.ItemImage = null;
Dean@641
   446
                                break;
Dean@641
   447
                        }
Dean@485
   448
                    }
Dean@259
   449
Dean@485
   450
                    // Set button
Dean@653
   451
                    if ((isMyself) ||
Dean@774
   452
                        ((partnerIdentity.IsForceUnencryptedBool) &&
Dean@653
   453
                         (managerState.IsIncoming == false)))
Dean@653
   454
                    {
Dean@653
   455
                        item.IsButtonVisible = false;
Dean@653
   456
                        item.IsExpandable = false;
Dean@653
   457
                    }
Dean@653
   458
                    else
Dean@366
   459
                    {
Dean@485
   460
                        if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@485
   461
                        {
Dean@485
   462
                            // Undo handshake
Dean@485
   463
                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_StopTrusting;
Dean@485
   464
                            item.IsButtonVisible = true;
Dean@646
   465
                            item.ButtonOnClick = (x, y) => { this.UndoHandshake(partnerIdentity.Copy()); };
Dean@356
   466
Dean@485
   467
                            item.IsExpandable = false;
Dean@485
   468
                        }
Dean@485
   469
                        else if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_unconfirmed_encryption &&
Dean@485
   470
                                 partnerIdentity.CommunicationType < _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@485
   471
                        {
Dean@485
   472
                            // Do handshake
Dean@737
   473
                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@485
   474
                            item.IsButtonVisible = true;
Dean@646
   475
                            item.ButtonOnClick = (x, y) => { this.DoHandshake(myIdentity.Copy(), partnerIdentity.Copy()); };
Dean@356
   476
Dean@485
   477
                            item.IsExpandable = true;
Dean@485
   478
                            item.ExpandedText = Properties.Resources.PrivacyStatus_TrustwordDesc + "\n\n" +
Dean@485
   479
                                                trustwordsShort;
Dean@447
   480
Dean@485
   481
                            item.ExpandedButton1Text = Properties.Resources.PrivacyStatus_SelectTrust;
Dean@646
   482
                            item.ExpandedButton1OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.Yes, partnerIdentity.Copy()); };
Dean@485
   483
                            item.ExpandedButton1BackColor = PrivacyState.COLOR_GREEN;
Dean@485
   484
                            item.ExpandedButton1ForeColor = Color.White;
Dean@447
   485
Dean@737
   486
                            item.ExpandedButton2Text = Properties.Resources.PrivacyStatus_SelectCancel;
Dean@737
   487
                            item.ExpandedButton2OnClick = (x, y) =>
Dean@737
   488
                                {
Dean@737
   489
                                    FormManagePrivacyStatus.State activeManagerState;
Dean@737
   490
Dean@737
   491
                                    if (this.managerForm != null)
Dean@737
   492
                                    {
Dean@737
   493
                                        activeManagerState = this.managerForm.CopyUIToState();
Dean@758
   494
Dean@737
   495
                                        if ((index >= 0) &&
Dean@737
   496
                                            (index < activeManagerState.Identities.Count))
Dean@737
   497
                                        {
Dean@737
   498
                                            item = activeManagerState.Identities[index];
Dean@758
   499
                                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@758
   500
                                            item.IsExpanded = false;
Dean@737
   501
                                            item.IsExpandable = false;
Dean@880
   502
Dean@880
   503
                                            this.managerForm.UpdateDisplayedTrustwords();
Dean@737
   504
                                        }
Dean@737
   505
                                    }
Dean@737
   506
                                };
Dean@737
   507
Dean@737
   508
                            item.ExpandedButton3Text = Properties.Resources.PrivacyStatus_SelectMistrust;
Dean@737
   509
                            item.ExpandedButton3OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.No, partnerIdentity.Copy()); };
Dean@737
   510
                            item.ExpandedButton3BackColor = PrivacyState.COLOR_RED;
Dean@737
   511
                            item.ExpandedButton3ForeColor = Color.White;
Dean@485
   512
                        }
Dean@672
   513
                        else if (partnerIdentity.CommunicationType == _pEp_comm_type.pEp_ct_mistrusted)
Dean@485
   514
                        {
Dean@485
   515
                            // Redo handshake with confirmation
Dean@485
   516
                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@485
   517
                            item.IsButtonVisible = true;
Dean@672
   518
                            item.ButtonOnClick = (x, y) => { this.DoHandshakeForMistrustedKey(myIdentity.Copy(), partnerIdentity.Copy()); };
Dean@641
   519
Dean@641
   520
                            item.IsExpandable = false;
Dean@485
   521
                        }
Dean@485
   522
                        else
Dean@485
   523
                        {
Dean@485
   524
                            item.IsButtonVisible = false;
Dean@641
   525
                            item.IsExpandable = false;
Dean@485
   526
                        }
Dean@366
   527
                    }
Dean@281
   528
                }
Dean@485
   529
                else // Invalid identity
Dean@259
   530
                {
Dean@485
   531
                    item = new SelectionItem();
Dean@485
   532
                    item.TextLine1 = ident.Username;
Dean@485
   533
                    item.TextLine2 = ident.Address;
Dean@485
   534
                    item.ItemImage = null;
Dean@485
   535
                    item.IsTwoTextLinesVisible = true;
Dean@259
   536
                    item.IsButtonVisible = false;
Dean@259
   537
                }
Dean@259
   538
Dean@279
   539
                managerState.Identities.Add(item);
Dean@737
   540
                currIndex++;
Dean@259
   541
            }
Dean@259
   542
Dean@737
   543
            // Attempt to select and expand the first identity requiring a handshake
Dean@375
   544
            for (int i = 0; i < managerState.Identities.Count; i++)
Dean@375
   545
            {
Dean@737
   546
                // Determine if it's a handshake identity by checking if it's expandable
Dean@375
   547
                if (managerState.Identities[i].IsExpandable)
Dean@375
   548
                {
Dean@737
   549
                    managerState.Identities[i].TextButton = Properties.Resources.PrivacyStatus_HandshakeAdvanced;
Dean@737
   550
                    managerState.Identities[i].IsExpanded = true;
Dean@375
   551
                    managerState.SelectedIdentityIndex = i;
Dean@375
   552
                    break;
Dean@375
   553
                }
Dean@375
   554
            }
Dean@375
   555
Dean@279
   556
            return (managerState);
Dean@279
   557
        }
Dean@279
   558
Dean@279
   559
        /// <summary>
Dean@279
   560
        /// Builds the latest state of the encryption status manager then shows the UI.
Dean@279
   561
        /// </summary>
Dean@279
   562
        private void BuildAndShowManager()
Dean@279
   563
        {
Dean@279
   564
            DialogResult result;
Dean@313
   565
            FormManagePrivacyStatus form;
Dean@313
   566
            FormManagePrivacyStatus.State stateOut;
Dean@279
   567
Dean@483
   568
            try
Dean@483
   569
            {
Dean@483
   570
                // Show the form
Dean@483
   571
                form = new FormManagePrivacyStatus();
Dean@483
   572
                form.StartPosition = FormStartPosition.CenterScreen;
Dean@1021
   573
                form.FormClosed += ManagerForm_FormClosed;
Dean@259
   574
Dean@483
   575
                this.managerForm = form;
Dean@483
   576
                result = form.ShowDialog(this.ParentForm, this.GetManagerState(), out stateOut); // Must show as dialog to block code
Dean@483
   577
            }
Dean@483
   578
            catch (Exception ex)
Dean@483
   579
            {
Dean@860
   580
                Globals.StopAndSendCrashReport(ex);
Dean@483
   581
            }
Dean@259
   582
Dean@259
   583
            return;
Dean@259
   584
        }
Dean@259
   585
Dean@259
   586
        /// <summary>
Dean@1001
   587
        /// Schedules for the color rating and UI (including displayed mirror) to be updated.
Dean@1001
   588
        /// This can be called many times with no issue as the update is only run every n milliseconds.
Dean@179
   589
        /// </summary>
Dean@1001
   590
        public void RequestColorRatingAndUIUpdate()
Dean@1001
   591
        {
Dean@1001
   592
            this.TimerRefresh.Enabled = true;
Dean@1001
   593
            return;
Dean@1001
   594
        }
Dean@1001
   595
Dean@1001
   596
        /// <summary>
Dean@1001
   597
        /// Immediately starts the update of UI color rating based on the associated mail item.
Dean@1001
   598
        /// This will start decryption as necessary and also will update the displayed mirror.
Dean@1001
   599
        /// This method by-passes the refresh timer completely.
Dean@1001
   600
        /// WARNING: This method assumes the message is fully downloaded already.
Dean@1001
   601
        /// </summary>
Dean@1001
   602
        private void ImmediateColorRatingAndUIUpdate()
Dean@159
   603
        {
Dean@312
   604
            PrivacyState state;
Dean@159
   605
Dean@860
   606
            Globals.LogVerbose("UpdateUIFromMailItem: Started.");
Dean@740
   607
Dean@173
   608
            if (this.associatedMailItem != null)
Dean@159
   609
            {
Dean@859
   610
                if (this.associatedMailItem.ForceUnencryptedBool == true)
Dean@159
   611
                {
Dean@342
   612
                    // Force unencrypted
Dean@312
   613
                    state = new PrivacyState(_pEp_color.pEp_rating_unencrypted);
Dean@680
   614
                    this.CopyStateToUI(state);
Dean@740
   615
Dean@860
   616
                    Globals.LogVerbose("UpdateUIFromMailItem: Force unencrypted detected.");
Dean@159
   617
                }
Dean@159
   618
                else
Dean@159
   619
                {
Dean@860
   620
                    Globals.LogVerbose("UpdateUIFromMailItem: Starting decryption.");
Dean@740
   621
Dean@680
   622
                    // Start the color rating calculation/decryption process
Dean@680
   623
                    this.associatedMailItem.StartDecryption();
Dean@159
   624
                }
Dean@159
   625
            }
Dean@159
   626
Dean@179
   627
            return;
Dean@159
   628
        }
Dean@159
   629
Dean@709
   630
        /// <summary>
Dean@709
   631
        /// Clears the associated unencrypted preview and displays the given note (if any).
Dean@709
   632
        /// </summary>
Dean@709
   633
        /// <param name="note">The note to diplsay.</param>
Dean@709
   634
        private void ClearPreview(string note = null)
Dean@709
   635
        {
Dean@987
   636
            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   637
Dean@709
   638
            if ((formRegions != null) &&
Dean@709
   639
                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@709
   640
                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@709
   641
            {
Dean@709
   642
                formRegions.FormRegionPreviewUnencrypted.ClearMessage();
Dean@709
   643
                formRegions.FormRegionPreviewUnencrypted.SetNote(note);
Dean@709
   644
            }
Dean@709
   645
Dean@709
   646
            return;
Dean@709
   647
        }
Dean@709
   648
Dean@172
   649
        #region StateMethods
Dean@172
   650
Dean@172
   651
        /// <summary>
Dean@172
   652
        /// Connects or disconnects all control events from the UI.
Dean@172
   653
        /// </summary>
Dean@172
   654
        /// <param name="connect">True to connect events, false to disconnect.</param>
Dean@172
   655
        private void ConnectEvents(bool connect)
Dean@172
   656
        {
Dean@172
   657
            // Connect events only if not already connected
markus@201
   658
            if ((connect == true) &&
Dean@172
   659
                (this.eventsAreConnected == false))
Dean@172
   660
            {
Dean@312
   661
                this.ButtonPrivacyStatus.Click += this.ButtonPrivacyStatus_Click;
Dean@312
   662
                this.ButtonPrivacyStatus.MouseUp += this.ButtonPrivacyStatus_MouseUp;
Dean@859
   663
                this.LinkLabelUpgrade.LinkClicked += LinkLabelUpgrade_LinkClicked;
Dean@179
   664
Dean@172
   665
                this.eventsAreConnected = true;
Dean@172
   666
            }
Dean@172
   667
            // Always attempt to disconnect
Dean@172
   668
            else if (connect == false)
Dean@172
   669
            {
Dean@312
   670
                this.ButtonPrivacyStatus.Click -= this.ButtonPrivacyStatus_Click;
Dean@312
   671
                this.ButtonPrivacyStatus.MouseUp -= this.ButtonPrivacyStatus_MouseUp;
Dean@859
   672
                this.LinkLabelUpgrade.LinkClicked -= LinkLabelUpgrade_LinkClicked;
Dean@179
   673
Dean@172
   674
                this.eventsAreConnected = false;
Dean@172
   675
            }
Dean@172
   676
Dean@172
   677
            return;
Dean@172
   678
        }
Dean@172
   679
Dean@172
   680
        /// <summary>
Dean@172
   681
        /// Copies the given state to the UI.
Dean@172
   682
        /// Events are turned off until the process is complete.
Dean@172
   683
        /// </summary>
Dean@172
   684
        /// <param name="state">The state to set to the UI.</param>
Dean@312
   685
        private void CopyStateToUI(PrivacyState state)
Dean@172
   686
        {
Dean@172
   687
            this.ConnectEvents(false);
markus@201
   688
Dean@172
   689
            ///////////////////////////////////////////////////////////
Dean@172
   690
            // Set UI data
Dean@172
   691
            ///////////////////////////////////////////////////////////
Dean@172
   692
Dean@179
   693
            // Save UI state maintained outside of controls
Dean@312
   694
            this.stateUIColorRating = state.PrivacyStatus;
Dean@179
   695
Dean@312
   696
            this.ButtonPrivacyStatus.BackColor = state.BackgroundColor;
Dean@312
   697
            this.ButtonPrivacyStatus.ForeColor = state.ForegroundColor;
Dean@312
   698
            this.ButtonPrivacyStatus.Text = state.ShortText;
Dean@172
   699
Dean@670
   700
            // Update the manager form state
Dean@670
   701
            if (this.managerForm != null)
Dean@670
   702
            {
Dean@670
   703
                this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@670
   704
            }
Dean@670
   705
Dean@859
   706
            ///////////////////////////////////////////////////////////
Dean@859
   707
            // Set UI control visibility
Dean@859
   708
            ///////////////////////////////////////////////////////////
Dean@859
   709
Dean@894
   710
#if READER_RELEASE_MODE
Dean@894
   711
            this.LinkLabelUpgrade.Visible = true;
Dean@894
   712
#else
Dean@894
   713
            this.LinkLabelUpgrade.Visible = false;
Dean@894
   714
#endif
Dean@859
   715
Dean@172
   716
            this.ConnectEvents(true);
Dean@172
   717
Dean@172
   718
            return;
Dean@172
   719
        }
Dean@172
   720
Dean@172
   721
        /// <summary>
Dean@172
   722
        /// Copies the UI to a new state.
Dean@172
   723
        /// </summary>
Dean@172
   724
        /// <returns>The state of the UI.</returns>
Dean@312
   725
        private PrivacyState CopyUIToState()
Dean@172
   726
        {
Dean@312
   727
            return (new PrivacyState(this.stateUIColorRating));
Dean@172
   728
        }
Dean@172
   729
Dean@172
   730
        #endregion
Dean@172
   731
Dean@159
   732
        /**************************************************************
Dean@159
   733
         * 
Dean@159
   734
         * Event Handling
Dean@159
   735
         * 
Dean@159
   736
         *************************************************************/
Dean@159
   737
Dean@159
   738
        /// <summary>
Dean@680
   739
        /// Event handler that is called when the form region is displayed.
Dean@680
   740
        /// This is called each time the form region looses then regains visiblity 
Dean@680
   741
        /// (for example an other email is selected then back to this one).
Dean@159
   742
        /// </summary>
Dean@312
   743
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   744
        {
Dean@680
   745
            // Do not allow initialization more than once
Dean@680
   746
            if (initialized == false)
Dean@680
   747
            {
Dean@680
   748
                this.SetAssociatedMailItem();
Dean@173
   749
Dean@680
   750
                // Set background color for Office 2013
Dean@159
   751
                try
Dean@159
   752
                {
Dean@680
   753
                    if (Globals.ThisAddIn.Application.Version.Substring(0, 2) == "15")
Dean@159
   754
                    {
Dean@680
   755
                        if (this.IsWithinInspector())
Dean@680
   756
                        {
Dean@680
   757
                            this.BackColor = SystemColors.ButtonFace;
Dean@680
   758
                        }
Dean@680
   759
                        else
Dean@680
   760
                        {
Dean@680
   761
                            this.BackColor = SystemColors.Window;
Dean@680
   762
                        }
Dean@159
   763
                    }
Dean@159
   764
                }
Dean@159
   765
                catch { }
Dean@680
   766
Dean@680
   767
                // Connect cryptable mail item events
Dean@680
   768
                if (this.associatedMailItem != null)
Dean@680
   769
                {
Dean@680
   770
                    try
Dean@680
   771
                    {
Dean@680
   772
                        this.associatedMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@680
   773
                        this.associatedMailItem.DecryptionComplete += MailItem_DecryptionComplete;
Dean@709
   774
                        this.associatedMailItem.GetMirrorComplete += MailItem_GetMirrorComplete;
Dean@682
   775
                        this.associatedMailItem.Send += MailItem_Send;
Dean@680
   776
Dean@680
   777
                        if (this.associatedMailItem.IsInEncryptedStore &&
Dean@927
   778
                            this.associatedMailItem.IsEncrypted)
Dean@680
   779
                        {
Dean@680
   780
                            this.associatedMailItem.Open += MailItem_Open;
Dean@680
   781
                        }
Dean@680
   782
                    }
Dean@680
   783
                    catch { }
Dean@680
   784
                }
Dean@680
   785
Dean@807
   786
                // Connect static events
Dean@807
   787
                CryptableMailItem.EncryptedConversationCacheUpdated += CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@807
   788
Dean@680
   789
                this.CopyStateToUI(new PrivacyState());
Dean@683
   790
                this.TimerRefresh.Tick += TimerRefresh_Tick;
Dean@680
   791
                this.initialized = true;
Dean@179
   792
            }
Dean@159
   793
Dean@683
   794
            // Call the timer tick method manually to refresh data with no delay
Dean@199
   795
            this.TimerRefresh_Tick(null, new EventArgs());
Dean@179
   796
Dean@179
   797
            return;
Dean@159
   798
        }
Dean@159
   799
Dean@159
   800
        /// <summary>
Dean@159
   801
        /// Event handler for when the form region is closed.
Dean@159
   802
        /// </summary>
Dean@312
   803
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   804
        {
Dean@199
   805
            // Disconnect cryptable mail item events
Dean@190
   806
            if (this.associatedMailItem != null)
Dean@179
   807
            {
Dean@179
   808
                try
Dean@179
   809
                {
Dean@445
   810
                    this.associatedMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@680
   811
                    this.associatedMailItem.DecryptionComplete -= MailItem_DecryptionComplete;
Dean@709
   812
                    this.associatedMailItem.GetMirrorComplete -= MailItem_GetMirrorComplete;
markus@201
   813
                    this.associatedMailItem.Open -= MailItem_Open;
Dean@682
   814
                    this.associatedMailItem.Send -= MailItem_Send;
Dean@179
   815
                }
Dean@179
   816
                catch { }
Dean@179
   817
            }
Dean@179
   818
Dean@807
   819
            // Disconnect static events
Dean@807
   820
            CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@807
   821
Dean@682
   822
            // Stop and disconnect the refresh timer
Dean@682
   823
            this.TimerRefresh.Stop();
Dean@682
   824
            this.TimerRefresh.Enabled = false;
Dean@682
   825
            this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@682
   826
Dean@179
   827
            return;
Dean@179
   828
        }
Dean@179
   829
Dean@179
   830
        /// <summary>
Dean@179
   831
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   832
        /// </summary>
Dean@179
   833
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   834
        {
Dean@199
   835
            bool tryAgain = false;
Dean@684
   836
            bool markForDownload = false;
Dean@179
   837
            this.TimerRefresh.Enabled = false; // Only once
Dean@684
   838
            Outlook.OlDownloadState dlState;
markus@201
   839
Dean@709
   840
            /* The Refresh/UI_Update process is a little more complicated here than other forms.
Dean@709
   841
             * There are the following components:
Dean@709
   842
             *   1. TimerRefresh_Tick
Dean@709
   843
             *        This is the main timer tick event handler called any time
Dean@1001
   844
             *        a refresh was requested (RequestColorRatingAndUIUpdate) and hasn't been run yet. 
Dean@1001
   845
             *        A refresh is requested either at initialization or when a property changes
Dean@709
   846
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
Dean@709
   847
             *        property change events could occur rapidy, but only one refresh should
Dean@709
   848
             *        occur for performance reasons.
Dean@709
   849
             * 
Dean@1001
   850
             *   2. ImmediateColorRatingAndUIUpdate
Dean@709
   851
             *        This will re-calculate the mail item's color rating and then update the UI.
Dean@709
   852
             *        However, if a mail item is not forcefully unencrypted, it will just call StartDecryption.
Dean@709
   853
             * 
Dean@709
   854
             *   3. MailItem_DecryptionComplete
Dean@709
   855
             *        When decryption of the mail item is complete, this even handler will be called.
Dean@709
   856
             *        This will update the UI with the latest color rating then call StartGetMirror.
Dean@709
   857
             *        The CopyStateToUI method is used which means any open privacy status form will also
Dean@709
   858
             *        be updated.
Dean@709
   859
             * 
Dean@709
   860
             *   4. MailItem_GetMirrorComplete
Dean@709
   861
             *        This is the final step in updating the UI, after a mirror is located, it's contents
Dean@709
   862
             *        will be shown to the unencrypted preview.
Dean@709
   863
             * 
Dean@709
   864
             * The general calling sequence is as shown above 1->4 with each component calling the next.
Dean@709
   865
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
Dean@709
   866
             */
Dean@709
   867
Dean@179
   868
            // Ensure the tick method is not called more than once
Dean@179
   869
            if (refreshOngoing == false)
Dean@179
   870
            {
Dean@179
   871
                this.refreshOngoing = true;
Dean@179
   872
Dean@179
   873
                if (this.associatedMailItem != null)
Dean@179
   874
                {
Dean@684
   875
                    // Attempt to get the download state
Dean@684
   876
                    try
Dean@684
   877
                    {
Dean@684
   878
                        dlState = this.associatedMailItem.DownloadState;
Dean@684
   879
                    }
Dean@684
   880
                    catch (Exception ex)
Dean@684
   881
                    {
Dean@860
   882
                        Globals.Log("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
Dean@684
   883
Dean@684
   884
                        // Assume everything is downloaded, but try to download again as well
Dean@684
   885
                        dlState = Outlook.OlDownloadState.olFullItem;
Dean@684
   886
                        markForDownload = true;
Dean@684
   887
                    }
Dean@684
   888
Dean@684
   889
                    if (dlState == Outlook.OlDownloadState.olFullItem)
Dean@179
   890
                    {
Dean@1001
   891
                        this.ImmediateColorRatingAndUIUpdate();
Dean@179
   892
                    }
Dean@179
   893
                    else
Dean@179
   894
                    {
Dean@684
   895
                        markForDownload = true;
Dean@684
   896
                    }
Dean@684
   897
Dean@684
   898
                    if (markForDownload)
Dean@684
   899
                    {
Dean@684
   900
                        // Try to mark the message for full download
Dean@684
   901
                        try
Dean@684
   902
                        {
Dean@684
   903
                            this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@684
   904
                            tryAgain = true;
Dean@684
   905
                        }
Dean@684
   906
                        catch (Exception ex)
Dean@684
   907
                        {
Dean@860
   908
                            Globals.Log("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
Dean@684
   909
                        }
Dean@179
   910
                    }
Dean@179
   911
                }
Dean@179
   912
Dean@199
   913
                // Set the timer to refresh again later automatically
Dean@199
   914
                if (tryAgain)
Dean@199
   915
                {
Dean@199
   916
                    this.TimerRefresh.Interval = 100;
Dean@199
   917
                    this.TimerRefresh.Enabled = true;
Dean@199
   918
                }
Dean@199
   919
Dean@179
   920
                this.refreshOngoing = false;
Dean@179
   921
            }
Dean@175
   922
Dean@159
   923
            return;
Dean@159
   924
        }
Dean@159
   925
Dean@169
   926
        /// <summary>
Dean@680
   927
        /// Event handler for when the decryption process is completed in the associated mail item.
Dean@709
   928
        /// This will then update the form region UI and the privacy status window as needed.
Dean@680
   929
        /// </summary>
Dean@704
   930
        private void MailItem_DecryptionComplete(object sender, CryptableMailItem.DecryptionEventArgs e)
Dean@680
   931
        {
Dean@860
   932
            Globals.LogVerbose("MailItem_DecryptionComplete: Decryption completed.");
Dean@740
   933
Dean@680
   934
            try
Dean@680
   935
            {
Dean@680
   936
                // Marshal code back to UI thread as necessary
Dean@680
   937
                this.Invoke(new Action(() =>
Dean@680
   938
                    {
Dean@680
   939
                        this.CopyStateToUI(new PrivacyState(e.ColorRating));
Dean@709
   940
Dean@709
   941
                        /* Create the unencrypted preview if the mail item is encrypted and
Dean@709
   942
                         * it is in an encrypted (untrusted) store
Dean@709
   943
                         * 
Dean@709
   944
                         * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Dean@709
   945
                         * it also is initialized after FormRegionPreviewUnencrypted.
Dean@709
   946
                         */
Dean@747
   947
                        try
Dean@709
   948
                        {
Dean@747
   949
                            if (this.associatedMailItem.IsInEncryptedStore &&
Dean@927
   950
                                this.associatedMailItem.IsEncrypted)
Dean@747
   951
                            {
Dean@860
   952
                                Globals.LogVerbose("MailItem_DecryptionComplete: Starting mirror location.");
Dean@747
   953
                                this.associatedMailItem.StartGetMirror();
Dean@747
   954
                            }
Dean@747
   955
                            else
Dean@747
   956
                            {
Dean@747
   957
                                this.ClearPreview();
Dean@747
   958
                            }
Dean@709
   959
                        }
Dean@747
   960
                        catch (Exception ex)
Dean@719
   961
                        {
Dean@747
   962
                            // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
Dean@747
   963
                            // While rare, just log the issue and stop the process
Dean@860
   964
                            Globals.Log("MailItem_DecryptionComplete: Failed to start mirror location, " + ex.ToString());
Dean@719
   965
                        }
Dean@680
   966
                    }));
Dean@680
   967
            }
Dean@680
   968
            catch (Exception ex)
Dean@680
   969
            {
Dean@860
   970
                Globals.Log("MailItem_DecryptionComplete: Error setting UI state, " + ex.ToString());
Dean@680
   971
            }
Dean@680
   972
Dean@680
   973
            return;
Dean@680
   974
        }
Dean@680
   975
Dean@680
   976
        /// <summary>
Dean@709
   977
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
Dean@709
   978
        /// This will then update the unencrypted preview in the UI.
Dean@709
   979
        /// </summary>
Dean@709
   980
        private void MailItem_GetMirrorComplete(object sender, CryptableMailItem.GetMirrorEventArgs e)
Dean@709
   981
        {
Dean@709
   982
            try
Dean@709
   983
            {
Dean@709
   984
                // Marshal code back to UI thread as necessary
Dean@709
   985
                this.Invoke(new Action(() =>
Dean@709
   986
                    {
Dean@987
   987
                        WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   988
Dean@910
   989
                        if ((e.Mirror == null) &&
Dean@1014
   990
                            (this.associatedMailItem.LastDecryptionStatus == Globals.ReturnStatus.Failure))
Dean@1014
   991
                        {
Dean@1014
   992
                            this.ClearPreview(Properties.Resources.Message_OpenError);
Dean@1014
   993
                            Globals.LogVerbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
Dean@1014
   994
                        }
Dean@1014
   995
                        else if ((e.Mirror == null) &&
Dean@1014
   996
                                 (this.associatedMailItem.LastDecryptionStatus == Globals.ReturnStatus.FailureNoConnection))
Dean@709
   997
                        {
Dean@910
   998
                            this.ClearPreview(Properties.Resources.Message_DecryptionNoConnection);
Dean@910
   999
                            Globals.LogVerbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
Dean@910
  1000
                        }
Dean@910
  1001
                        else
Dean@910
  1002
                        {
Dean@910
  1003
                            if ((formRegions != null) &&
Dean@910
  1004
                                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@910
  1005
                                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@910
  1006
                            {
Dean@910
  1007
                                formRegions.FormRegionPreviewUnencrypted.SetMessage(e.Mirror);
Dean@910
  1008
                                Globals.LogVerbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
Dean@910
  1009
                            }
Dean@709
  1010
                        }
Dean@758
  1011
Dean@758
  1012
                        // Display the mirror if necessary
Dean@758
  1013
                        if ((this.associatedMailItem != null) &&
Dean@758
  1014
                            (this.displayMirrorRequested))
Dean@758
  1015
                        {
Dean@758
  1016
                            this.associatedMailItem.DisplayMirror();
Dean@758
  1017
                            this.displayMirrorRequested = false;
Dean@758
  1018
                        }
Dean@709
  1019
                    }));
Dean@709
  1020
            }
Dean@709
  1021
            catch (Exception ex)
Dean@709
  1022
            {
Dean@860
  1023
                Globals.Log("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
Dean@709
  1024
            }
Dean@709
  1025
Dean@709
  1026
            return;
Dean@709
  1027
        }
Dean@709
  1028
Dean@709
  1029
        /// <summary>
Dean@807
  1030
        /// Event handler for the static mail item encrypted conversation cache updated.
Dean@807
  1031
        /// This event is fired after the conversation cache has been updated with parent information following a
Dean@807
  1032
        /// forward or reply mail item event (on a different, likely parent, mail item).
Dean@807
  1033
        /// </summary>
Dean@807
  1034
        private void CryptableMailItem_EncryptedConversationCacheUpdated(object sender, EventArgs e)
Dean@807
  1035
        {
Dean@807
  1036
            // Process the mail item now that the cache is updated
Dean@807
  1037
            this.associatedMailItem.SetIsOriginallyEncryptedByCache();
Dean@807
  1038
Dean@807
  1039
            // Remove the event handler -- this can only be processed once because it's timing dependent
Dean@807
  1040
            CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@807
  1041
Dean@807
  1042
            return;
Dean@807
  1043
        }
Dean@807
  1044
Dean@807
  1045
        /// <summary>
Dean@169
  1046
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
  1047
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
  1048
        /// </summary>
Dean@169
  1049
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
  1050
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
  1051
        /// and the inspector is not displayed.</param>
Dean@169
  1052
        private void MailItem_Open(ref bool cancel)
Dean@159
  1053
        {
Dean@758
  1054
            bool result;
Dean@758
  1055
Dean@758
  1056
            if (this.associatedMailItem != null &&
Dean@901
  1057
                this.associatedMailItem.IsInEncryptedStore &&
Dean@927
  1058
                this.associatedMailItem.IsEncrypted)
Dean@169
  1059
            {
Dean@758
  1060
                // Try to open the mirror
Dean@758
  1061
                result = this.associatedMailItem.DisplayMirror();
Dean@758
  1062
Dean@758
  1063
                if (result == false)
Dean@758
  1064
                {
Dean@758
  1065
                    // Set flag to open after decryption/mirror location
Dean@758
  1066
                    this.displayMirrorRequested = true;
Dean@758
  1067
                }
Dean@758
  1068
Dean@758
  1069
                // Always cancel opening the original
Dean@169
  1070
                cancel = true;
Dean@169
  1071
            }
Dean@169
  1072
Dean@169
  1073
            return;
Dean@159
  1074
        }
Dean@159
  1075
Dean@169
  1076
        /// <summary>
Dean@682
  1077
        /// Event handler for when a mail item is sent.
Dean@682
  1078
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
Dean@682
  1079
        /// </summary>
Dean@682
  1080
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@682
  1081
        /// If the event procedure sets this argument to True, the send operation is not completed 
Dean@682
  1082
        /// and the inspector is left open.</param>
Dean@682
  1083
        private void MailItem_Send(ref bool cancel)
Dean@682
  1084
        {
Dean@807
  1085
            DialogResult result;
Dean@807
  1086
            PrivacyState state = this.CopyUIToState();
Dean@807
  1087
Dean@807
  1088
            if ((this.associatedMailItem.IsOriginallyEncrypted) &&
Dean@807
  1089
                (state.PrivacyStatus < _pEp_color.pEp_rating_unreliable))
Dean@807
  1090
            {
Dean@807
  1091
                result = MessageBox.Show(this.ParentForm,
Dean@807
  1092
                                         pEp.Properties.Resources.Message_WarningPreviouslyEncrypted,
Dean@807
  1093
                                         pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@807
  1094
                                         MessageBoxButtons.YesNo);
Dean@807
  1095
Dean@807
  1096
                if (result == DialogResult.No)
Dean@807
  1097
                {
Dean@807
  1098
                    // Cancel sending
Dean@807
  1099
                    cancel = true;
Dean@807
  1100
                }
Dean@807
  1101
            }
Dean@807
  1102
Dean@682
  1103
            // Stop and disconnect the refresh timer
Dean@682
  1104
            // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
Dean@807
  1105
            if (cancel == false)
Dean@807
  1106
            {
Dean@807
  1107
                this.TimerRefresh.Stop();
Dean@807
  1108
                this.TimerRefresh.Enabled = false;
Dean@807
  1109
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@807
  1110
            }
Dean@682
  1111
Dean@682
  1112
            return;
Dean@682
  1113
        }
Dean@682
  1114
Dean@682
  1115
        /// <summary>
Dean@169
  1116
        /// Event handler for when a mail item property is changed.
Dean@169
  1117
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
  1118
        /// </summary>
Dean@169
  1119
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@445
  1120
        private void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
Dean@159
  1121
        {
Dean@445
  1122
            switch (e.PropertyName.ToUpper())
Dean@159
  1123
            {
Dean@179
  1124
                case "TO":
Dean@1001
  1125
                    this.RequestColorRatingAndUIUpdate();
Dean@159
  1126
                    break;
Dean@159
  1127
                // Outlook bug: there are always both events, so one is enough
Dean@159
  1128
                //case "CC":
Dean@1001
  1129
                //    this.RequestColorRatingAndUIUpdate();
Dean@159
  1130
                //    break;
Dean@159
  1131
            }
Dean@169
  1132
Dean@169
  1133
            return;
Dean@159
  1134
        }
Dean@159
  1135
Dean@179
  1136
        /// <summary>
Dean@312
  1137
        /// Event handler for when the privacy status button is clicked.
Dean@179
  1138
        /// </summary>
Dean@312
  1139
        private void ButtonPrivacyStatus_Click(object sender, EventArgs e)
vb@133
  1140
        {
Dean@1002
  1141
            this.RequestColorRatingAndUIUpdate();
Dean@310
  1142
            this.BuildAndShowManager();
Dean@179
  1143
            return;
vb@133
  1144
        }
vb@133
  1145
Dean@179
  1146
        /// <summary>
Dean@312
  1147
        /// Event handler for when a mouse button is released over the privacy status button.
Dean@179
  1148
        /// </summary>
Dean@312
  1149
        private void ButtonPrivacyStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
  1150
        {
Dean@179
  1151
            return;
vb@133
  1152
        }
Dean@859
  1153
Dean@859
  1154
        /// <summary>
Dean@1021
  1155
        /// Event handler for when the manager form is closed.
Dean@1021
  1156
        /// </summary>
Dean@1021
  1157
        private void ManagerForm_FormClosed(object sender, FormClosedEventArgs e)
Dean@1021
  1158
        {
Dean@1021
  1159
            // Remove the reference held in the FormRegionPrivacyStatus so it stops getting updated
Dean@1021
  1160
            if (this.managerForm != null)
Dean@1021
  1161
            {
Dean@1021
  1162
                this.managerForm.FormClosed -= ManagerForm_FormClosed;
Dean@1021
  1163
                this.managerForm = null;
Dean@1021
  1164
            }
Dean@1021
  1165
Dean@1021
  1166
            return;
Dean@1021
  1167
        }
Dean@1021
  1168
Dean@1021
  1169
        /// <summary>
Dean@859
  1170
        /// Event handler for when the pEp website hyperlink is clicked.
Dean@859
  1171
        /// </summary>
Dean@859
  1172
        private void LinkLabelUpgrade_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Dean@859
  1173
        {
Dean@966
  1174
            try
Dean@966
  1175
            {
Dean@1007
  1176
                Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
Dean@966
  1177
            }
Dean@966
  1178
            catch (Exception ex)
Dean@966
  1179
            {
Dean@1007
  1180
                Globals.Log("LinkLabelUpgrade_LinkClicked: Unable to open website link, " + ex.ToString());
Dean@966
  1181
            }
Dean@966
  1182
Dean@859
  1183
            return;
Dean@859
  1184
        }
vb@133
  1185
    }
vb@133
  1186
}