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