UI/FormRegionPrivacyStatus.cs
author Dean
Thu, 28 Jul 2016 11:53:32 +0200
changeset 1096 dd83e670e336
parent 1094 6866b9e8bd9a
child 1106 1ad65967bd9f
permissions -rw-r--r--
Initial implementation of KeepConfidential.
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@384
   333
            PEPIdentity myIdentity;
Dean@259
   334
            _pEp_color identityPartnerColor;
Dean@259
   335
            SelectionItem item;
Dean@1069
   336
            FormControlManagePrivacyStatus.State managerState = new FormControlManagePrivacyStatus.State();
Dean@1069
   337
Dean@1069
   338
            // Load all images from resources
Dean@1069
   339
            imageForceUnencOn = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImageForceUnencOn.png", UriKind.RelativeOrAbsolute));
Dean@1069
   340
            imageGreen = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusGreen.png", UriKind.RelativeOrAbsolute));
Dean@1069
   341
            imageNoColor = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusNoColor.png", UriKind.RelativeOrAbsolute));
Dean@1069
   342
            imageRed = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusRed.png", UriKind.RelativeOrAbsolute));
Dean@1069
   343
            imageYellow = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusYellow.png", UriKind.RelativeOrAbsolute));
Dean@259
   344
Dean@769
   345
            /* Resolve all recipients -- this ensures the identities list is correctly populated
Dean@769
   346
             *
Dean@769
   347
             * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
Dean@769
   348
             * This is because the resolve process can modify the contents of the mail item which triggers an event.
Dean@769
   349
             * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
Dean@769
   350
             * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that 
Dean@769
   351
             * cannot be resolved (no address).
Dean@769
   352
             */
Dean@769
   353
            if (this.associatedMailItem != null)
Dean@769
   354
            {
Dean@769
   355
                this.associatedMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@769
   356
                this.associatedMailItem.ResolveAllRecipients();
Dean@769
   357
                this.associatedMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@769
   358
            }
Dean@332
   359
Dean@1086
   360
            managerState.ColorRating = this.FormControlPrivacyStatusChild.DisplayState.ColorRating;
Dean@279
   361
            managerState.IsIncoming = this.associatedMailItem.IsIncoming;
Dean@259
   362
Dean@653
   363
            if (managerState.IsIncoming)
Dean@259
   364
            {
Dean@384
   365
                identities = new List<PEPIdentity>();
Dean@259
   366
Dean@259
   367
                // Add only one identity
Dean@638
   368
                if (this.associatedMailItem.From != null)
Dean@638
   369
                {
Dean@764
   370
                    identities.Add(this.associatedMailItem.From);
Dean@638
   371
                }
Dean@259
   372
            }
Dean@259
   373
            else
Dean@259
   374
            {
Dean@395
   375
                identities = this.associatedMailItem.Recipients;
Dean@259
   376
            }
Dean@259
   377
Dean@366
   378
            // Get my identity
Dean@633
   379
            myIdentity = this.associatedMailItem.Myself;
Dean@366
   380
Dean@279
   381
            // Add identities
Dean@394
   382
            identities = PEPIdentity.ToFlatList(identities);
Dean@279
   383
            managerState.Identities.Clear();
Dean@384
   384
            foreach (PEPIdentity ident in identities)
Dean@259
   385
            {
Dean@737
   386
                // Needed so index can be used within anonymous methods without getting into the 'outer variable trap' of currIndex
Dean@737
   387
                int index = currIndex;
Dean@737
   388
Dean@865
   389
                if (ident.IsAddressValid)
Dean@485
   390
                {
Dean@774
   391
                    // Update the partner identity to get a fingerprint, preserve IsForceUnencrypted
Dean@485
   392
                    pEp_identity_s partnerIdentity_s = ThisAddIn.pEp.update_identity(ident.ToCOMType());
Dean@485
   393
                    PEPIdentity partnerIdentity = new PEPIdentity(partnerIdentity_s);
Dean@774
   394
                    partnerIdentity.IsForceUnencrypted = ident.IsForceUnencrypted;
Dean@774
   395
Dean@1018
   396
                    try
Dean@1018
   397
                    {
Dean@1018
   398
                        identityPartnerColor = ThisAddIn.pEp.identity_color(partnerIdentity_s);
Dean@1018
   399
                    }
Dean@1018
   400
                    catch (COMException ex)
Dean@1018
   401
                    {
Dean@1018
   402
                        identityPartnerColor = _pEp_color.pEp_rating_undefined;
Dean@1018
   403
                        Globals.Log("GetManagerState: Failed to get identity color, " + ex.ToString());
Dean@1018
   404
                    }
Dean@1018
   405
Dean@910
   406
                    isMyself = PEPIdentity.GetIsOwnIdentity(partnerIdentity.Address);
Dean@724
   407
Dean@880
   408
                    // Calculate trustwords
Dean@880
   409
                    Globals.ThisAddIn.CalcTrustwords(myIdentity,
Dean@880
   410
                                                     partnerIdentity,
Dean@880
   411
                                                     out trustwordsShort,
Dean@880
   412
                                                     out trustwordsFull,
Dean@1090
   413
                                                     PEPSettings.CULTURE_CODE_DEFAULT);
Dean@880
   414
Dean@485
   415
                    item = new SelectionItem();
Dean@485
   416
                    item.TextLine1 = partnerIdentity.Username;
Dean@485
   417
                    item.TextLine2 = partnerIdentity.Address;
Dean@894
   418
                    item.Tag = (string.IsNullOrWhiteSpace(partnerIdentity.Fingerprint) ? null : trustwordsShort);
Dean@259
   419
Dean@485
   420
                    // Don't show both the user name and address if they are the same
Dean@485
   421
                    if ((item.TextLine1 != null) &&
Dean@485
   422
                        (item.TextLine2 != null) &&
Dean@485
   423
                        (item.TextLine1 == item.TextLine2))
Dean@485
   424
                    {
Dean@485
   425
                        item.IsTwoTextLinesVisible = false;
Dean@485
   426
                    }
Dean@332
   427
Dean@485
   428
                    // Set image
Dean@774
   429
                    if ((partnerIdentity.IsForceUnencryptedBool) &&
Dean@653
   430
                        (managerState.IsIncoming == false))
Dean@485
   431
                    {
Dean@1069
   432
                        item.ItemImage = imageForceUnencOn;
Dean@641
   433
                    }
Dean@641
   434
                    else
Dean@641
   435
                    {
Dean@641
   436
                        switch (PrivacyState.ConvertRatingToPrivacyColor(identityPartnerColor))
Dean@641
   437
                        {
Dean@641
   438
                            case PrivacyState.PrivacyColor.Green:
Dean@1069
   439
                                item.ItemImage = imageGreen;
Dean@641
   440
                                break;
Dean@641
   441
                            case PrivacyState.PrivacyColor.Yellow:
Dean@1069
   442
                                item.ItemImage = imageYellow;
Dean@641
   443
                                break;
Dean@641
   444
                            case PrivacyState.PrivacyColor.Red:
Dean@1069
   445
                                item.ItemImage = imageRed;
Dean@641
   446
                                break;
Dean@641
   447
                            case PrivacyState.PrivacyColor.NoColor:
Dean@1069
   448
                                item.ItemImage = imageNoColor;
Dean@641
   449
                                break;
Dean@641
   450
                            default:
Dean@641
   451
                                item.ItemImage = null;
Dean@641
   452
                                break;
Dean@641
   453
                        }
Dean@485
   454
                    }
Dean@259
   455
Dean@485
   456
                    // Set button
Dean@653
   457
                    if ((isMyself) ||
Dean@774
   458
                        ((partnerIdentity.IsForceUnencryptedBool) &&
Dean@653
   459
                         (managerState.IsIncoming == false)))
Dean@653
   460
                    {
Dean@653
   461
                        item.IsButtonVisible = false;
Dean@653
   462
                        item.IsExpandable = false;
Dean@653
   463
                    }
Dean@653
   464
                    else
Dean@366
   465
                    {
Dean@485
   466
                        if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@485
   467
                        {
Dean@485
   468
                            // Undo handshake
Dean@485
   469
                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_StopTrusting;
Dean@485
   470
                            item.IsButtonVisible = true;
Dean@646
   471
                            item.ButtonOnClick = (x, y) => { this.UndoHandshake(partnerIdentity.Copy()); };
Dean@356
   472
Dean@485
   473
                            item.IsExpandable = false;
Dean@485
   474
                        }
Dean@485
   475
                        else if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_unconfirmed_encryption &&
Dean@485
   476
                                 partnerIdentity.CommunicationType < _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@485
   477
                        {
Dean@485
   478
                            // Do handshake
Dean@737
   479
                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@485
   480
                            item.IsButtonVisible = true;
Dean@646
   481
                            item.ButtonOnClick = (x, y) => { this.DoHandshake(myIdentity.Copy(), partnerIdentity.Copy()); };
Dean@356
   482
Dean@485
   483
                            item.IsExpandable = true;
Dean@1069
   484
                            item.ExpandedText = Properties.Resources.PrivacyStatus_TrustwordDesc + "\n\n" +
Dean@485
   485
                                                trustwordsShort;
Dean@447
   486
Dean@1075
   487
                            item.ExpandedButton1Text = pEp.Properties.Resources.Handshake_ConfirmText;
Dean@646
   488
                            item.ExpandedButton1OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.Yes, partnerIdentity.Copy()); };
Dean@1074
   489
                            item.ExpandedButton1Style = (Style)Globals.ResourceDict["StyleConfirmButton"];
Dean@447
   490
Dean@1075
   491
                            item.ExpandedButton2Text = pEp.Properties.Resources.Handshake_CancelText;
Dean@737
   492
                            item.ExpandedButton2OnClick = (x, y) =>
Dean@737
   493
                                {
Dean@1069
   494
                                    SelectionItem selItem;
Dean@1069
   495
                                    FormControlManagePrivacyStatus.State activeState;
Dean@737
   496
Dean@737
   497
                                    if (this.managerForm != null)
Dean@737
   498
                                    {
Dean@1072
   499
                                        activeState = this.managerForm.FormControl.DisplayState;
Dean@758
   500
Dean@737
   501
                                        if ((index >= 0) &&
Dean@1069
   502
                                            (index < activeState.Identities.Count))
Dean@737
   503
                                        {
Dean@1069
   504
                                            selItem = activeState.Identities[index];
Dean@1069
   505
                                            selItem.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@1069
   506
                                            selItem.IsExpanded = false;
Dean@1069
   507
                                            selItem.IsExpandable = false;
Dean@737
   508
                                        }
Dean@737
   509
                                    }
Dean@737
   510
                                };
Dean@1074
   511
                            item.ExpandedButton2Style = (Style)Globals.ResourceDict["StyleCancelButton"];
Dean@737
   512
Dean@1075
   513
                            item.ExpandedButton3Text = pEp.Properties.Resources.Handshake_WrongText;
Dean@737
   514
                            item.ExpandedButton3OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.No, partnerIdentity.Copy()); };
Dean@1074
   515
                            item.ExpandedButton3Style = (Style)Globals.ResourceDict["StyleWrongButton"];
Dean@485
   516
                        }
Dean@672
   517
                        else if (partnerIdentity.CommunicationType == _pEp_comm_type.pEp_ct_mistrusted)
Dean@485
   518
                        {
Dean@485
   519
                            // Redo handshake with confirmation
Dean@485
   520
                            item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@485
   521
                            item.IsButtonVisible = true;
Dean@672
   522
                            item.ButtonOnClick = (x, y) => { this.DoHandshakeForMistrustedKey(myIdentity.Copy(), partnerIdentity.Copy()); };
Dean@641
   523
Dean@641
   524
                            item.IsExpandable = false;
Dean@485
   525
                        }
Dean@485
   526
                        else
Dean@485
   527
                        {
Dean@485
   528
                            item.IsButtonVisible = false;
Dean@641
   529
                            item.IsExpandable = false;
Dean@485
   530
                        }
Dean@366
   531
                    }
Dean@281
   532
                }
Dean@485
   533
                else // Invalid identity
Dean@259
   534
                {
Dean@485
   535
                    item = new SelectionItem();
Dean@485
   536
                    item.TextLine1 = ident.Username;
Dean@485
   537
                    item.TextLine2 = ident.Address;
Dean@485
   538
                    item.ItemImage = null;
Dean@485
   539
                    item.IsTwoTextLinesVisible = true;
Dean@259
   540
                    item.IsButtonVisible = false;
Dean@259
   541
                }
Dean@259
   542
Dean@279
   543
                managerState.Identities.Add(item);
Dean@737
   544
                currIndex++;
Dean@259
   545
            }
Dean@259
   546
Dean@737
   547
            // Attempt to select and expand the first identity requiring a handshake
Dean@375
   548
            for (int i = 0; i < managerState.Identities.Count; i++)
Dean@375
   549
            {
Dean@737
   550
                // Determine if it's a handshake identity by checking if it's expandable
Dean@375
   551
                if (managerState.Identities[i].IsExpandable)
Dean@375
   552
                {
Dean@737
   553
                    managerState.Identities[i].TextButton = Properties.Resources.PrivacyStatus_HandshakeAdvanced;
Dean@737
   554
                    managerState.Identities[i].IsExpanded = true;
Dean@375
   555
                    managerState.SelectedIdentityIndex = i;
Dean@375
   556
                    break;
Dean@375
   557
                }
Dean@375
   558
            }
Dean@375
   559
Dean@279
   560
            return (managerState);
Dean@279
   561
        }
Dean@279
   562
Dean@279
   563
        /// <summary>
Dean@279
   564
        /// Builds the latest state of the encryption status manager then shows the UI.
Dean@279
   565
        /// </summary>
Dean@279
   566
        private void BuildAndShowManager()
Dean@279
   567
        {
Dean@279
   568
            DialogResult result;
Dean@313
   569
            FormManagePrivacyStatus form;
Dean@279
   570
Dean@483
   571
            try
Dean@483
   572
            {
Dean@483
   573
                // Show the form
Dean@483
   574
                form = new FormManagePrivacyStatus();
Dean@1082
   575
                form.StartPosition = FormStartPosition.CenterParent;
Dean@1021
   576
                form.FormClosed += ManagerForm_FormClosed;
Dean@1073
   577
                form.FormControl.DisplayState = this.GetManagerState();
Dean@259
   578
Dean@483
   579
                this.managerForm = form;
Dean@1073
   580
                result = form.ShowDialog(this.ParentForm);
Dean@483
   581
            }
Dean@483
   582
            catch (Exception ex)
Dean@483
   583
            {
Dean@860
   584
                Globals.StopAndSendCrashReport(ex);
Dean@483
   585
            }
Dean@259
   586
Dean@259
   587
            return;
Dean@259
   588
        }
Dean@259
   589
Dean@259
   590
        /// <summary>
Dean@1001
   591
        /// Schedules for the color rating and UI (including displayed mirror) to be updated.
Dean@1001
   592
        /// This can be called many times with no issue as the update is only run every n milliseconds.
Dean@179
   593
        /// </summary>
Dean@1001
   594
        public void RequestColorRatingAndUIUpdate()
Dean@1001
   595
        {
Dean@1001
   596
            this.TimerRefresh.Enabled = true;
Dean@1001
   597
            return;
Dean@1001
   598
        }
Dean@1001
   599
Dean@1001
   600
        /// <summary>
Dean@1001
   601
        /// Immediately starts the update of UI color rating based on the associated mail item.
Dean@1001
   602
        /// This will start decryption as necessary and also will update the displayed mirror.
Dean@1001
   603
        /// This method by-passes the refresh timer completely.
Dean@1001
   604
        /// WARNING: This method assumes the message is fully downloaded already.
Dean@1001
   605
        /// </summary>
Dean@1001
   606
        private void ImmediateColorRatingAndUIUpdate()
Dean@159
   607
        {
Dean@860
   608
            Globals.LogVerbose("UpdateUIFromMailItem: Started.");
Dean@740
   609
Dean@173
   610
            if (this.associatedMailItem != null)
Dean@159
   611
            {
Dean@1094
   612
                if (this.associatedMailItem.ForceUnencrypted == true)
Dean@159
   613
                {
Dean@342
   614
                    // Force unencrypted
Dean@1086
   615
                    this.FormControlPrivacyStatusChild.DisplayState.ColorRating = _pEp_color.pEp_rating_unencrypted;
Dean@740
   616
Dean@860
   617
                    Globals.LogVerbose("UpdateUIFromMailItem: Force unencrypted detected.");
Dean@159
   618
                }
Dean@159
   619
                else
Dean@159
   620
                {
Dean@860
   621
                    Globals.LogVerbose("UpdateUIFromMailItem: Starting decryption.");
Dean@740
   622
Dean@680
   623
                    // Start the color rating calculation/decryption process
Dean@1096
   624
                    this.associatedMailItem.StartProcessing();
Dean@159
   625
                }
Dean@159
   626
            }
Dean@159
   627
Dean@179
   628
            return;
Dean@159
   629
        }
Dean@159
   630
Dean@709
   631
        /// <summary>
Dean@709
   632
        /// Clears the associated unencrypted preview and displays the given note (if any).
Dean@709
   633
        /// </summary>
Dean@709
   634
        /// <param name="note">The note to diplsay.</param>
Dean@709
   635
        private void ClearPreview(string note = null)
Dean@709
   636
        {
Dean@987
   637
            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   638
Dean@709
   639
            if ((formRegions != null) &&
Dean@709
   640
                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@709
   641
                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@709
   642
            {
Dean@709
   643
                formRegions.FormRegionPreviewUnencrypted.ClearMessage();
Dean@709
   644
                formRegions.FormRegionPreviewUnencrypted.SetNote(note);
Dean@709
   645
            }
Dean@709
   646
Dean@709
   647
            return;
Dean@709
   648
        }
Dean@709
   649
Dean@159
   650
        /**************************************************************
Dean@159
   651
         * 
Dean@159
   652
         * Event Handling
Dean@159
   653
         * 
Dean@159
   654
         *************************************************************/
Dean@159
   655
Dean@159
   656
        /// <summary>
Dean@680
   657
        /// Event handler that is called when the form region is displayed.
Dean@680
   658
        /// This is called each time the form region looses then regains visiblity 
Dean@680
   659
        /// (for example an other email is selected then back to this one).
Dean@159
   660
        /// </summary>
Dean@312
   661
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   662
        {
Dean@680
   663
            // Do not allow initialization more than once
Dean@680
   664
            if (initialized == false)
Dean@680
   665
            {
Dean@680
   666
                this.SetAssociatedMailItem();
Dean@173
   667
Dean@680
   668
                // Set background color for Office 2013
Dean@159
   669
                try
Dean@159
   670
                {
Dean@680
   671
                    if (Globals.ThisAddIn.Application.Version.Substring(0, 2) == "15")
Dean@159
   672
                    {
Dean@680
   673
                        if (this.IsWithinInspector())
Dean@680
   674
                        {
Dean@1074
   675
                            this.BackColor = System.Drawing.SystemColors.ButtonFace;
Dean@680
   676
                        }
Dean@680
   677
                        else
Dean@680
   678
                        {
Dean@1074
   679
                            this.BackColor = System.Drawing.SystemColors.Window;
Dean@680
   680
                        }
Dean@159
   681
                    }
Dean@159
   682
                }
Dean@159
   683
                catch { }
Dean@680
   684
Dean@680
   685
                // Connect cryptable mail item events
Dean@680
   686
                if (this.associatedMailItem != null)
Dean@680
   687
                {
Dean@680
   688
                    try
Dean@680
   689
                    {
Dean@680
   690
                        this.associatedMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@1096
   691
                        this.associatedMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
Dean@1096
   692
                        this.associatedMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
Dean@682
   693
                        this.associatedMailItem.Send += MailItem_Send;
Dean@680
   694
Dean@1096
   695
                        if (this.associatedMailItem.IsStoredEncrypted)
Dean@680
   696
                        {
Dean@680
   697
                            this.associatedMailItem.Open += MailItem_Open;
Dean@680
   698
                        }
Dean@680
   699
                    }
Dean@680
   700
                    catch { }
Dean@680
   701
                }
Dean@680
   702
Dean@1086
   703
                // Connect events
Dean@807
   704
                CryptableMailItem.EncryptedConversationCacheUpdated += CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1086
   705
                this.FormControlPrivacyStatusChild.PrivacyViewClick += FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@807
   706
Dean@683
   707
                this.TimerRefresh.Tick += TimerRefresh_Tick;
Dean@680
   708
                this.initialized = true;
Dean@179
   709
            }
Dean@159
   710
Dean@683
   711
            // Call the timer tick method manually to refresh data with no delay
Dean@199
   712
            this.TimerRefresh_Tick(null, new EventArgs());
Dean@179
   713
Dean@179
   714
            return;
Dean@159
   715
        }
Dean@159
   716
Dean@159
   717
        /// <summary>
Dean@159
   718
        /// Event handler for when the form region is closed.
Dean@159
   719
        /// </summary>
Dean@312
   720
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   721
        {
Dean@199
   722
            // Disconnect cryptable mail item events
Dean@190
   723
            if (this.associatedMailItem != null)
Dean@179
   724
            {
Dean@179
   725
                try
Dean@179
   726
                {
Dean@445
   727
                    this.associatedMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@1096
   728
                    this.associatedMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
Dean@1096
   729
                    this.associatedMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
markus@201
   730
                    this.associatedMailItem.Open -= MailItem_Open;
Dean@682
   731
                    this.associatedMailItem.Send -= MailItem_Send;
Dean@179
   732
                }
Dean@179
   733
                catch { }
Dean@179
   734
            }
Dean@179
   735
Dean@1086
   736
            // Disconnect events
Dean@807
   737
            CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1086
   738
            this.FormControlPrivacyStatusChild.PrivacyViewClick -= FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@807
   739
Dean@682
   740
            // Stop and disconnect the refresh timer
Dean@682
   741
            this.TimerRefresh.Stop();
Dean@682
   742
            this.TimerRefresh.Enabled = false;
Dean@682
   743
            this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@682
   744
Dean@179
   745
            return;
Dean@179
   746
        }
Dean@179
   747
Dean@179
   748
        /// <summary>
Dean@179
   749
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   750
        /// </summary>
Dean@179
   751
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   752
        {
Dean@199
   753
            bool tryAgain = false;
Dean@684
   754
            bool markForDownload = false;
Dean@179
   755
            this.TimerRefresh.Enabled = false; // Only once
Dean@684
   756
            Outlook.OlDownloadState dlState;
markus@201
   757
Dean@709
   758
            /* The Refresh/UI_Update process is a little more complicated here than other forms.
Dean@709
   759
             * There are the following components:
Dean@709
   760
             *   1. TimerRefresh_Tick
Dean@709
   761
             *        This is the main timer tick event handler called any time
Dean@1001
   762
             *        a refresh was requested (RequestColorRatingAndUIUpdate) and hasn't been run yet. 
Dean@1001
   763
             *        A refresh is requested either at initialization or when a property changes
Dean@709
   764
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
Dean@709
   765
             *        property change events could occur rapidy, but only one refresh should
Dean@709
   766
             *        occur for performance reasons.
Dean@709
   767
             * 
Dean@1001
   768
             *   2. ImmediateColorRatingAndUIUpdate
Dean@709
   769
             *        This will re-calculate the mail item's color rating and then update the UI.
Dean@709
   770
             *        However, if a mail item is not forcefully unencrypted, it will just call StartDecryption.
Dean@709
   771
             * 
Dean@709
   772
             *   3. MailItem_DecryptionComplete
Dean@709
   773
             *        When decryption of the mail item is complete, this even handler will be called.
Dean@709
   774
             *        This will update the UI with the latest color rating then call StartGetMirror.
Dean@709
   775
             *        The CopyStateToUI method is used which means any open privacy status form will also
Dean@709
   776
             *        be updated.
Dean@709
   777
             * 
Dean@709
   778
             *   4. MailItem_GetMirrorComplete
Dean@709
   779
             *        This is the final step in updating the UI, after a mirror is located, it's contents
Dean@709
   780
             *        will be shown to the unencrypted preview.
Dean@709
   781
             * 
Dean@709
   782
             * The general calling sequence is as shown above 1->4 with each component calling the next.
Dean@709
   783
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
Dean@709
   784
             */
Dean@709
   785
Dean@179
   786
            // Ensure the tick method is not called more than once
Dean@179
   787
            if (refreshOngoing == false)
Dean@179
   788
            {
Dean@179
   789
                this.refreshOngoing = true;
Dean@179
   790
Dean@179
   791
                if (this.associatedMailItem != null)
Dean@179
   792
                {
Dean@684
   793
                    // Attempt to get the download state
Dean@684
   794
                    try
Dean@684
   795
                    {
Dean@684
   796
                        dlState = this.associatedMailItem.DownloadState;
Dean@684
   797
                    }
Dean@684
   798
                    catch (Exception ex)
Dean@684
   799
                    {
Dean@860
   800
                        Globals.Log("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
Dean@684
   801
Dean@684
   802
                        // Assume everything is downloaded, but try to download again as well
Dean@684
   803
                        dlState = Outlook.OlDownloadState.olFullItem;
Dean@684
   804
                        markForDownload = true;
Dean@684
   805
                    }
Dean@684
   806
Dean@684
   807
                    if (dlState == Outlook.OlDownloadState.olFullItem)
Dean@179
   808
                    {
Dean@1001
   809
                        this.ImmediateColorRatingAndUIUpdate();
Dean@179
   810
                    }
Dean@179
   811
                    else
Dean@179
   812
                    {
Dean@684
   813
                        markForDownload = true;
Dean@684
   814
                    }
Dean@684
   815
Dean@684
   816
                    if (markForDownload)
Dean@684
   817
                    {
Dean@684
   818
                        // Try to mark the message for full download
Dean@684
   819
                        try
Dean@684
   820
                        {
Dean@684
   821
                            this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@684
   822
                            tryAgain = true;
Dean@684
   823
                        }
Dean@684
   824
                        catch (Exception ex)
Dean@684
   825
                        {
Dean@860
   826
                            Globals.Log("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
Dean@684
   827
                        }
Dean@179
   828
                    }
Dean@179
   829
                }
Dean@179
   830
Dean@199
   831
                // Set the timer to refresh again later automatically
Dean@199
   832
                if (tryAgain)
Dean@199
   833
                {
Dean@199
   834
                    this.TimerRefresh.Interval = 100;
Dean@199
   835
                    this.TimerRefresh.Enabled = true;
Dean@199
   836
                }
Dean@199
   837
Dean@179
   838
                this.refreshOngoing = false;
Dean@179
   839
            }
Dean@175
   840
Dean@159
   841
            return;
Dean@159
   842
        }
Dean@159
   843
Dean@169
   844
        /// <summary>
Dean@1096
   845
        /// Event handler for when the processing is completed in the associated mail item.
Dean@709
   846
        /// This will then update the form region UI and the privacy status window as needed.
Dean@680
   847
        /// </summary>
Dean@1096
   848
        private void MailItem_ProcessingCompleted(object sender, CryptableMailItem.ProcessingCompletedEventArgs e)
Dean@680
   849
        {
Dean@1096
   850
            Globals.LogVerbose("MailItem_ProcessingComplete: Decryption completed.");
Dean@740
   851
Dean@680
   852
            try
Dean@680
   853
            {
Dean@680
   854
                // Marshal code back to UI thread as necessary
Dean@680
   855
                this.Invoke(new Action(() =>
Dean@680
   856
                    {
Dean@1086
   857
                        this.FormControlPrivacyStatusChild.DisplayState.ColorRating = e.ColorRating;
Dean@709
   858
Dean@709
   859
                        /* Create the unencrypted preview if the mail item is encrypted and
Dean@709
   860
                         * it is in an encrypted (untrusted) store
Dean@709
   861
                         * 
Dean@709
   862
                         * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Dean@709
   863
                         * it also is initialized after FormRegionPreviewUnencrypted.
Dean@709
   864
                         */
Dean@747
   865
                        try
Dean@709
   866
                        {
Dean@1096
   867
                            if (this.associatedMailItem.IsStoredEncrypted)
Dean@747
   868
                            {
Dean@1096
   869
                                Globals.LogVerbose("MailItem_ProcessingComplete: Starting mirror location.");
Dean@747
   870
                                this.associatedMailItem.StartGetMirror();
Dean@747
   871
                            }
Dean@747
   872
                            else
Dean@747
   873
                            {
Dean@747
   874
                                this.ClearPreview();
Dean@747
   875
                            }
Dean@709
   876
                        }
Dean@747
   877
                        catch (Exception ex)
Dean@719
   878
                        {
Dean@747
   879
                            // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
Dean@747
   880
                            // While rare, just log the issue and stop the process
Dean@1096
   881
                            Globals.Log("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
Dean@719
   882
                        }
Dean@680
   883
                    }));
Dean@680
   884
            }
Dean@680
   885
            catch (Exception ex)
Dean@680
   886
            {
Dean@1096
   887
                Globals.Log("MailItem_ProcessingComplete: Error setting UI state, " + ex.ToString());
Dean@680
   888
            }
Dean@680
   889
Dean@680
   890
            return;
Dean@680
   891
        }
Dean@680
   892
Dean@680
   893
        /// <summary>
Dean@709
   894
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
Dean@709
   895
        /// This will then update the unencrypted preview in the UI.
Dean@709
   896
        /// </summary>
Dean@1096
   897
        private void MailItem_GetMirrorCompleted(object sender, CryptableMailItem.GetMirrorCompletedEventArgs e)
Dean@709
   898
        {
Dean@709
   899
            try
Dean@709
   900
            {
Dean@709
   901
                // Marshal code back to UI thread as necessary
Dean@709
   902
                this.Invoke(new Action(() =>
Dean@709
   903
                    {
Dean@987
   904
                        WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   905
Dean@910
   906
                        if ((e.Mirror == null) &&
Dean@1096
   907
                            (this.associatedMailItem.LastProcessingStatus == Globals.ReturnStatus.Failure))
Dean@1014
   908
                        {
Dean@1014
   909
                            this.ClearPreview(Properties.Resources.Message_OpenError);
Dean@1014
   910
                            Globals.LogVerbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
Dean@1014
   911
                        }
Dean@1014
   912
                        else if ((e.Mirror == null) &&
Dean@1096
   913
                                 (this.associatedMailItem.LastProcessingStatus == Globals.ReturnStatus.FailureNoConnection))
Dean@709
   914
                        {
Dean@910
   915
                            this.ClearPreview(Properties.Resources.Message_DecryptionNoConnection);
Dean@910
   916
                            Globals.LogVerbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
Dean@910
   917
                        }
Dean@910
   918
                        else
Dean@910
   919
                        {
Dean@910
   920
                            if ((formRegions != null) &&
Dean@910
   921
                                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@910
   922
                                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@910
   923
                            {
Dean@910
   924
                                formRegions.FormRegionPreviewUnencrypted.SetMessage(e.Mirror);
Dean@910
   925
                                Globals.LogVerbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
Dean@910
   926
                            }
Dean@709
   927
                        }
Dean@758
   928
Dean@758
   929
                        // Display the mirror if necessary
Dean@758
   930
                        if ((this.associatedMailItem != null) &&
Dean@758
   931
                            (this.displayMirrorRequested))
Dean@758
   932
                        {
Dean@758
   933
                            this.associatedMailItem.DisplayMirror();
Dean@758
   934
                            this.displayMirrorRequested = false;
Dean@758
   935
                        }
Dean@709
   936
                    }));
Dean@709
   937
            }
Dean@709
   938
            catch (Exception ex)
Dean@709
   939
            {
Dean@860
   940
                Globals.Log("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
Dean@709
   941
            }
Dean@709
   942
Dean@709
   943
            return;
Dean@709
   944
        }
Dean@709
   945
Dean@709
   946
        /// <summary>
Dean@807
   947
        /// Event handler for the static mail item encrypted conversation cache updated.
Dean@807
   948
        /// This event is fired after the conversation cache has been updated with parent information following a
Dean@807
   949
        /// forward or reply mail item event (on a different, likely parent, mail item).
Dean@807
   950
        /// </summary>
Dean@807
   951
        private void CryptableMailItem_EncryptedConversationCacheUpdated(object sender, EventArgs e)
Dean@807
   952
        {
Dean@807
   953
            // Process the mail item now that the cache is updated
Dean@807
   954
            this.associatedMailItem.SetIsOriginallyEncryptedByCache();
Dean@807
   955
Dean@807
   956
            // Remove the event handler -- this can only be processed once because it's timing dependent
Dean@807
   957
            CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@807
   958
Dean@807
   959
            return;
Dean@807
   960
        }
Dean@807
   961
Dean@807
   962
        /// <summary>
Dean@169
   963
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   964
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
   965
        /// </summary>
Dean@169
   966
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   967
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   968
        /// and the inspector is not displayed.</param>
Dean@169
   969
        private void MailItem_Open(ref bool cancel)
Dean@159
   970
        {
Dean@758
   971
            bool result;
Dean@758
   972
Dean@758
   973
            if (this.associatedMailItem != null &&
Dean@1096
   974
                this.associatedMailItem.IsStoredEncrypted)
Dean@169
   975
            {
Dean@758
   976
                // Try to open the mirror
Dean@758
   977
                result = this.associatedMailItem.DisplayMirror();
Dean@758
   978
Dean@758
   979
                if (result == false)
Dean@758
   980
                {
Dean@758
   981
                    // Set flag to open after decryption/mirror location
Dean@758
   982
                    this.displayMirrorRequested = true;
Dean@758
   983
                }
Dean@758
   984
Dean@758
   985
                // Always cancel opening the original
Dean@169
   986
                cancel = true;
Dean@169
   987
            }
Dean@169
   988
Dean@169
   989
            return;
Dean@159
   990
        }
Dean@159
   991
Dean@169
   992
        /// <summary>
Dean@682
   993
        /// Event handler for when a mail item is sent.
Dean@682
   994
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
Dean@682
   995
        /// </summary>
Dean@682
   996
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@682
   997
        /// If the event procedure sets this argument to True, the send operation is not completed 
Dean@682
   998
        /// and the inspector is left open.</param>
Dean@682
   999
        private void MailItem_Send(ref bool cancel)
Dean@682
  1000
        {
Dean@807
  1001
            DialogResult result;
Dean@807
  1002
Dean@807
  1003
            if ((this.associatedMailItem.IsOriginallyEncrypted) &&
Dean@1086
  1004
                (this.FormControlPrivacyStatusChild.DisplayState.ColorRating < _pEp_color.pEp_rating_unreliable))
Dean@807
  1005
            {
Dean@1074
  1006
                result = System.Windows.Forms.MessageBox.Show(this.ParentForm,
Dean@1074
  1007
                                                              pEp.Properties.Resources.Message_WarningPreviouslyEncrypted,
Dean@1074
  1008
                                                              pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@1074
  1009
                                                              MessageBoxButtons.YesNo);
Dean@807
  1010
Dean@807
  1011
                if (result == DialogResult.No)
Dean@807
  1012
                {
Dean@807
  1013
                    // Cancel sending
Dean@807
  1014
                    cancel = true;
Dean@807
  1015
                }
Dean@807
  1016
            }
Dean@807
  1017
Dean@682
  1018
            // Stop and disconnect the refresh timer
Dean@682
  1019
            // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
Dean@807
  1020
            if (cancel == false)
Dean@807
  1021
            {
Dean@807
  1022
                this.TimerRefresh.Stop();
Dean@807
  1023
                this.TimerRefresh.Enabled = false;
Dean@807
  1024
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@807
  1025
            }
Dean@682
  1026
Dean@682
  1027
            return;
Dean@682
  1028
        }
Dean@682
  1029
Dean@682
  1030
        /// <summary>
Dean@169
  1031
        /// Event handler for when a mail item property is changed.
Dean@169
  1032
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
  1033
        /// </summary>
Dean@169
  1034
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@445
  1035
        private void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
Dean@159
  1036
        {
Dean@445
  1037
            switch (e.PropertyName.ToUpper())
Dean@159
  1038
            {
Dean@179
  1039
                case "TO":
Dean@1001
  1040
                    this.RequestColorRatingAndUIUpdate();
Dean@159
  1041
                    break;
Dean@159
  1042
                // Outlook bug: there are always both events, so one is enough
Dean@159
  1043
                //case "CC":
Dean@1001
  1044
                //    this.RequestColorRatingAndUIUpdate();
Dean@159
  1045
                //    break;
Dean@159
  1046
            }
Dean@169
  1047
Dean@169
  1048
            return;
Dean@159
  1049
        }
Dean@159
  1050
Dean@179
  1051
        /// <summary>
Dean@1021
  1052
        /// Event handler for when the manager form is closed.
Dean@1021
  1053
        /// </summary>
Dean@1021
  1054
        private void ManagerForm_FormClosed(object sender, FormClosedEventArgs e)
Dean@1021
  1055
        {
Dean@1021
  1056
            // Remove the reference held in the FormRegionPrivacyStatus so it stops getting updated
Dean@1021
  1057
            if (this.managerForm != null)
Dean@1021
  1058
            {
Dean@1021
  1059
                this.managerForm.FormClosed -= ManagerForm_FormClosed;
Dean@1021
  1060
                this.managerForm = null;
Dean@1021
  1061
            }
Dean@1021
  1062
Dean@1021
  1063
            return;
Dean@1021
  1064
        }
Dean@1021
  1065
Dean@1021
  1066
        /// <summary>
Dean@859
  1067
        /// Event handler for when the pEp website hyperlink is clicked.
Dean@859
  1068
        /// </summary>
Dean@859
  1069
        private void LinkLabelUpgrade_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Dean@859
  1070
        {
Dean@966
  1071
            try
Dean@966
  1072
            {
Dean@1007
  1073
                Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
Dean@966
  1074
            }
Dean@966
  1075
            catch (Exception ex)
Dean@966
  1076
            {
Dean@1007
  1077
                Globals.Log("LinkLabelUpgrade_LinkClicked: Unable to open website link, " + ex.ToString());
Dean@966
  1078
            }
Dean@966
  1079
Dean@859
  1080
            return;
Dean@859
  1081
        }
Dean@1047
  1082
Dean@1047
  1083
        /// <summary>
Dean@1086
  1084
        /// Event handler for when the privacy view button is clicked within the form control.
Dean@1047
  1085
        /// </summary>
Dean@1086
  1086
        private void FormControlPrivacyStatusChild_PrivacyViewClick(object sender, System.Windows.RoutedEventArgs e)
Dean@1047
  1087
        {
Dean@1047
  1088
            this.RequestColorRatingAndUIUpdate();
Dean@1047
  1089
            this.BuildAndShowManager();
Dean@1047
  1090
            return;
Dean@1047
  1091
        }
vb@133
  1092
    }
vb@133
  1093
}