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