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