UI/FormRegionPrivacyStatus.cs
author Dean
Tue, 16 Aug 2016 06:16:36 -0700
changeset 1138 979b777a088b
parent 1136 5db697976b5e
child 1145 dfabd4047d8d
permissions -rw-r--r--
OUT-39: Implement the ability to disable pEp for certain accounts.
Also includes a renaming of 'encrypted' to 'secure'.
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@1138
    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@1069
   494
                            item.ExpandedText = Properties.Resources.PrivacyStatus_TrustwordDesc + "\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@680
   697
            // Do not allow initialization more than once
Dean@680
   698
            if (initialized == false)
Dean@680
   699
            {
Dean@680
   700
                this.SetAssociatedMailItem();
Dean@173
   701
Dean@1136
   702
                // Set reader upgrade link visibility
Dean@1138
   703
                this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = (Globals.RELEASE_MODE == Globals.ReleaseMode.Reader);
Dean@1136
   704
Dean@680
   705
                // Set background color for Office 2013
Dean@159
   706
                try
Dean@159
   707
                {
Dean@680
   708
                    if (Globals.ThisAddIn.Application.Version.Substring(0, 2) == "15")
Dean@159
   709
                    {
Dean@680
   710
                        if (this.IsWithinInspector())
Dean@680
   711
                        {
Dean@1074
   712
                            this.BackColor = System.Drawing.SystemColors.ButtonFace;
Dean@680
   713
                        }
Dean@680
   714
                        else
Dean@680
   715
                        {
Dean@1074
   716
                            this.BackColor = System.Drawing.SystemColors.Window;
Dean@680
   717
                        }
Dean@159
   718
                    }
Dean@159
   719
                }
Dean@159
   720
                catch { }
Dean@680
   721
Dean@680
   722
                // Connect cryptable mail item events
Dean@680
   723
                if (this.associatedMailItem != null)
Dean@680
   724
                {
Dean@680
   725
                    try
Dean@680
   726
                    {
Dean@680
   727
                        this.associatedMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@1096
   728
                        this.associatedMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
Dean@1096
   729
                        this.associatedMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
Dean@682
   730
                        this.associatedMailItem.Send += MailItem_Send;
Dean@680
   731
Dean@1096
   732
                        if (this.associatedMailItem.IsStoredEncrypted)
Dean@680
   733
                        {
Dean@680
   734
                            this.associatedMailItem.Open += MailItem_Open;
Dean@680
   735
                        }
Dean@680
   736
                    }
Dean@680
   737
                    catch { }
Dean@680
   738
                }
Dean@680
   739
Dean@1086
   740
                // Connect events
Dean@807
   741
                CryptableMailItem.EncryptedConversationCacheUpdated += CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1086
   742
                this.FormControlPrivacyStatusChild.PrivacyViewClick += FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@807
   743
Dean@683
   744
                this.TimerRefresh.Tick += TimerRefresh_Tick;
Dean@680
   745
                this.initialized = true;
Dean@179
   746
            }
Dean@159
   747
Dean@683
   748
            // Call the timer tick method manually to refresh data with no delay
Dean@199
   749
            this.TimerRefresh_Tick(null, new EventArgs());
Dean@179
   750
Dean@179
   751
            return;
Dean@159
   752
        }
Dean@159
   753
Dean@159
   754
        /// <summary>
Dean@159
   755
        /// Event handler for when the form region is closed.
Dean@159
   756
        /// </summary>
Dean@312
   757
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   758
        {
Dean@199
   759
            // Disconnect cryptable mail item events
Dean@190
   760
            if (this.associatedMailItem != null)
Dean@179
   761
            {
Dean@179
   762
                try
Dean@179
   763
                {
Dean@445
   764
                    this.associatedMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@1096
   765
                    this.associatedMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
Dean@1096
   766
                    this.associatedMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
markus@201
   767
                    this.associatedMailItem.Open -= MailItem_Open;
Dean@682
   768
                    this.associatedMailItem.Send -= MailItem_Send;
Dean@179
   769
                }
Dean@179
   770
                catch { }
Dean@179
   771
            }
Dean@179
   772
Dean@1086
   773
            // Disconnect events
Dean@807
   774
            CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1086
   775
            this.FormControlPrivacyStatusChild.PrivacyViewClick -= FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@807
   776
Dean@682
   777
            // Stop and disconnect the refresh timer
Dean@682
   778
            this.TimerRefresh.Stop();
Dean@682
   779
            this.TimerRefresh.Enabled = false;
Dean@682
   780
            this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@682
   781
Dean@179
   782
            return;
Dean@179
   783
        }
Dean@179
   784
Dean@179
   785
        /// <summary>
Dean@179
   786
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   787
        /// </summary>
Dean@179
   788
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   789
        {
Dean@199
   790
            bool tryAgain = false;
Dean@684
   791
            bool markForDownload = false;
Dean@179
   792
            this.TimerRefresh.Enabled = false; // Only once
Dean@684
   793
            Outlook.OlDownloadState dlState;
markus@201
   794
Dean@709
   795
            /* The Refresh/UI_Update process is a little more complicated here than other forms.
Dean@709
   796
             * There are the following components:
Dean@709
   797
             *   1. TimerRefresh_Tick
Dean@709
   798
             *        This is the main timer tick event handler called any time
Dean@1130
   799
             *        a refresh was requested (RequestRatingAndUIUpdate) and hasn't been run yet. 
Dean@1001
   800
             *        A refresh is requested either at initialization or when a property changes
Dean@709
   801
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
Dean@709
   802
             *        property change events could occur rapidy, but only one refresh should
Dean@709
   803
             *        occur for performance reasons.
Dean@709
   804
             * 
Dean@1130
   805
             *   2. ImmediateRatingAndUIUpdate
Dean@1130
   806
             *        This will re-calculate the mail item's rating and then update the UI.
Dean@709
   807
             *        However, if a mail item is not forcefully unencrypted, it will just call StartDecryption.
Dean@709
   808
             * 
Dean@1130
   809
             *   3. MailItem_ProcessingCompleted
Dean@709
   810
             *        When decryption of the mail item is complete, this even handler will be called.
Dean@1130
   811
             *        This will update the UI with the latest rating then call StartGetMirror.
Dean@709
   812
             *        The CopyStateToUI method is used which means any open privacy status form will also
Dean@709
   813
             *        be updated.
Dean@709
   814
             * 
Dean@709
   815
             *   4. MailItem_GetMirrorComplete
Dean@709
   816
             *        This is the final step in updating the UI, after a mirror is located, it's contents
Dean@709
   817
             *        will be shown to the unencrypted preview.
Dean@709
   818
             * 
Dean@709
   819
             * The general calling sequence is as shown above 1->4 with each component calling the next.
Dean@709
   820
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
Dean@709
   821
             */
Dean@709
   822
Dean@179
   823
            // Ensure the tick method is not called more than once
Dean@179
   824
            if (refreshOngoing == false)
Dean@179
   825
            {
Dean@179
   826
                this.refreshOngoing = true;
Dean@179
   827
Dean@179
   828
                if (this.associatedMailItem != null)
Dean@179
   829
                {
Dean@684
   830
                    // Attempt to get the download state
Dean@684
   831
                    try
Dean@684
   832
                    {
Dean@684
   833
                        dlState = this.associatedMailItem.DownloadState;
Dean@684
   834
                    }
Dean@684
   835
                    catch (Exception ex)
Dean@684
   836
                    {
Dean@860
   837
                        Globals.Log("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
Dean@684
   838
Dean@684
   839
                        // Assume everything is downloaded, but try to download again as well
Dean@684
   840
                        dlState = Outlook.OlDownloadState.olFullItem;
Dean@684
   841
                        markForDownload = true;
Dean@684
   842
                    }
Dean@684
   843
Dean@684
   844
                    if (dlState == Outlook.OlDownloadState.olFullItem)
Dean@179
   845
                    {
Dean@1130
   846
                        this.ImmediateRatingAndUIUpdate();
Dean@179
   847
                    }
Dean@179
   848
                    else
Dean@179
   849
                    {
Dean@684
   850
                        markForDownload = true;
Dean@684
   851
                    }
Dean@684
   852
Dean@684
   853
                    if (markForDownload)
Dean@684
   854
                    {
Dean@684
   855
                        // Try to mark the message for full download
Dean@684
   856
                        try
Dean@684
   857
                        {
Dean@684
   858
                            this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@684
   859
                            tryAgain = true;
Dean@684
   860
                        }
Dean@684
   861
                        catch (Exception ex)
Dean@684
   862
                        {
Dean@860
   863
                            Globals.Log("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
Dean@684
   864
                        }
Dean@179
   865
                    }
Dean@179
   866
                }
Dean@179
   867
Dean@199
   868
                // Set the timer to refresh again later automatically
Dean@199
   869
                if (tryAgain)
Dean@199
   870
                {
Dean@199
   871
                    this.TimerRefresh.Interval = 100;
Dean@199
   872
                    this.TimerRefresh.Enabled = true;
Dean@199
   873
                }
Dean@199
   874
Dean@179
   875
                this.refreshOngoing = false;
Dean@179
   876
            }
Dean@175
   877
Dean@159
   878
            return;
Dean@159
   879
        }
Dean@159
   880
Dean@169
   881
        /// <summary>
Dean@1096
   882
        /// Event handler for when the processing is completed in the associated mail item.
Dean@709
   883
        /// This will then update the form region UI and the privacy status window as needed.
Dean@680
   884
        /// </summary>
Dean@1096
   885
        private void MailItem_ProcessingCompleted(object sender, CryptableMailItem.ProcessingCompletedEventArgs e)
Dean@680
   886
        {
Dean@1096
   887
            Globals.LogVerbose("MailItem_ProcessingComplete: Decryption completed.");
Dean@740
   888
Dean@680
   889
            try
Dean@680
   890
            {
Dean@680
   891
                // Marshal code back to UI thread as necessary
Dean@680
   892
                this.Invoke(new Action(() =>
Dean@680
   893
                    {
Dean@1130
   894
                        this.FormControlPrivacyStatusChild.DisplayState.Rating = e.Rating;
Dean@1130
   895
                        this.UpdateManagePrivacyStatusForm(true); // Only update the rating
Dean@709
   896
Dean@709
   897
                        /* Create the unencrypted preview if the mail item is encrypted and
Dean@709
   898
                         * it is in an encrypted (untrusted) store
Dean@709
   899
                         * 
Dean@709
   900
                         * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Dean@709
   901
                         * it also is initialized after FormRegionPreviewUnencrypted.
Dean@709
   902
                         */
Dean@747
   903
                        try
Dean@709
   904
                        {
Dean@1096
   905
                            if (this.associatedMailItem.IsStoredEncrypted)
Dean@747
   906
                            {
Dean@1096
   907
                                Globals.LogVerbose("MailItem_ProcessingComplete: Starting mirror location.");
Dean@747
   908
                                this.associatedMailItem.StartGetMirror();
Dean@747
   909
                            }
Dean@747
   910
                            else
Dean@747
   911
                            {
Dean@747
   912
                                this.ClearPreview();
Dean@747
   913
                            }
Dean@709
   914
                        }
Dean@747
   915
                        catch (Exception ex)
Dean@719
   916
                        {
Dean@747
   917
                            // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
Dean@747
   918
                            // While rare, just log the issue and stop the process
Dean@1096
   919
                            Globals.Log("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
Dean@719
   920
                        }
Dean@680
   921
                    }));
Dean@680
   922
            }
Dean@680
   923
            catch (Exception ex)
Dean@680
   924
            {
Dean@1096
   925
                Globals.Log("MailItem_ProcessingComplete: Error setting UI state, " + ex.ToString());
Dean@680
   926
            }
Dean@680
   927
Dean@680
   928
            return;
Dean@680
   929
        }
Dean@680
   930
Dean@680
   931
        /// <summary>
Dean@709
   932
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
Dean@709
   933
        /// This will then update the unencrypted preview in the UI.
Dean@709
   934
        /// </summary>
Dean@1096
   935
        private void MailItem_GetMirrorCompleted(object sender, CryptableMailItem.GetMirrorCompletedEventArgs e)
Dean@709
   936
        {
Dean@709
   937
            try
Dean@709
   938
            {
Dean@709
   939
                // Marshal code back to UI thread as necessary
Dean@709
   940
                this.Invoke(new Action(() =>
Dean@709
   941
                    {
Dean@987
   942
                        WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   943
Dean@910
   944
                        if ((e.Mirror == null) &&
Dean@1096
   945
                            (this.associatedMailItem.LastProcessingStatus == Globals.ReturnStatus.Failure))
Dean@1014
   946
                        {
Dean@1014
   947
                            this.ClearPreview(Properties.Resources.Message_OpenError);
Dean@1014
   948
                            Globals.LogVerbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
Dean@1014
   949
                        }
Dean@1014
   950
                        else if ((e.Mirror == null) &&
Dean@1096
   951
                                 (this.associatedMailItem.LastProcessingStatus == Globals.ReturnStatus.FailureNoConnection))
Dean@709
   952
                        {
Dean@910
   953
                            this.ClearPreview(Properties.Resources.Message_DecryptionNoConnection);
Dean@910
   954
                            Globals.LogVerbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
Dean@910
   955
                        }
Dean@910
   956
                        else
Dean@910
   957
                        {
Dean@910
   958
                            if ((formRegions != null) &&
Dean@910
   959
                                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@910
   960
                                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@910
   961
                            {
Dean@910
   962
                                formRegions.FormRegionPreviewUnencrypted.SetMessage(e.Mirror);
Dean@910
   963
                                Globals.LogVerbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
Dean@910
   964
                            }
Dean@709
   965
                        }
Dean@758
   966
Dean@758
   967
                        // Display the mirror if necessary
Dean@758
   968
                        if ((this.associatedMailItem != null) &&
Dean@758
   969
                            (this.displayMirrorRequested))
Dean@758
   970
                        {
Dean@758
   971
                            this.associatedMailItem.DisplayMirror();
Dean@758
   972
                            this.displayMirrorRequested = false;
Dean@758
   973
                        }
Dean@709
   974
                    }));
Dean@709
   975
            }
Dean@709
   976
            catch (Exception ex)
Dean@709
   977
            {
Dean@860
   978
                Globals.Log("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
Dean@709
   979
            }
Dean@709
   980
Dean@709
   981
            return;
Dean@709
   982
        }
Dean@709
   983
Dean@709
   984
        /// <summary>
Dean@807
   985
        /// Event handler for the static mail item encrypted conversation cache updated.
Dean@807
   986
        /// This event is fired after the conversation cache has been updated with parent information following a
Dean@807
   987
        /// forward or reply mail item event (on a different, likely parent, mail item).
Dean@807
   988
        /// </summary>
Dean@807
   989
        private void CryptableMailItem_EncryptedConversationCacheUpdated(object sender, EventArgs e)
Dean@807
   990
        {
Dean@807
   991
            // Process the mail item now that the cache is updated
Dean@807
   992
            this.associatedMailItem.SetIsOriginallyEncryptedByCache();
Dean@807
   993
Dean@807
   994
            // Remove the event handler -- this can only be processed once because it's timing dependent
Dean@807
   995
            CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@807
   996
Dean@807
   997
            return;
Dean@807
   998
        }
Dean@807
   999
Dean@807
  1000
        /// <summary>
Dean@169
  1001
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
  1002
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
  1003
        /// </summary>
Dean@169
  1004
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
  1005
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
  1006
        /// and the inspector is not displayed.</param>
Dean@169
  1007
        private void MailItem_Open(ref bool cancel)
Dean@159
  1008
        {
Dean@758
  1009
            bool result;
Dean@758
  1010
Dean@758
  1011
            if (this.associatedMailItem != null &&
Dean@1096
  1012
                this.associatedMailItem.IsStoredEncrypted)
Dean@169
  1013
            {
Dean@758
  1014
                // Try to open the mirror
Dean@758
  1015
                result = this.associatedMailItem.DisplayMirror();
Dean@758
  1016
Dean@758
  1017
                if (result == false)
Dean@758
  1018
                {
Dean@758
  1019
                    // Set flag to open after decryption/mirror location
Dean@758
  1020
                    this.displayMirrorRequested = true;
Dean@758
  1021
                }
Dean@758
  1022
Dean@758
  1023
                // Always cancel opening the original
Dean@169
  1024
                cancel = true;
Dean@169
  1025
            }
Dean@169
  1026
Dean@169
  1027
            return;
Dean@159
  1028
        }
Dean@159
  1029
Dean@169
  1030
        /// <summary>
Dean@682
  1031
        /// Event handler for when a mail item is sent.
Dean@682
  1032
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
Dean@682
  1033
        /// </summary>
Dean@682
  1034
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@682
  1035
        /// If the event procedure sets this argument to True, the send operation is not completed 
Dean@682
  1036
        /// and the inspector is left open.</param>
Dean@682
  1037
        private void MailItem_Send(ref bool cancel)
Dean@682
  1038
        {
Dean@807
  1039
            DialogResult result;
Dean@807
  1040
Dean@807
  1041
            if ((this.associatedMailItem.IsOriginallyEncrypted) &&
Dean@1130
  1042
                (this.FormControlPrivacyStatusChild.DisplayState.Rating < _pEp_color.pEp_rating_unreliable))
Dean@807
  1043
            {
Dean@1074
  1044
                result = System.Windows.Forms.MessageBox.Show(this.ParentForm,
Dean@1074
  1045
                                                              pEp.Properties.Resources.Message_WarningPreviouslyEncrypted,
Dean@1074
  1046
                                                              pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@1074
  1047
                                                              MessageBoxButtons.YesNo);
Dean@807
  1048
Dean@807
  1049
                if (result == DialogResult.No)
Dean@807
  1050
                {
Dean@807
  1051
                    // Cancel sending
Dean@807
  1052
                    cancel = true;
Dean@807
  1053
                }
Dean@807
  1054
            }
Dean@807
  1055
Dean@682
  1056
            // Stop and disconnect the refresh timer
Dean@682
  1057
            // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
Dean@807
  1058
            if (cancel == false)
Dean@807
  1059
            {
Dean@807
  1060
                this.TimerRefresh.Stop();
Dean@807
  1061
                this.TimerRefresh.Enabled = false;
Dean@807
  1062
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@807
  1063
            }
Dean@682
  1064
Dean@682
  1065
            return;
Dean@682
  1066
        }
Dean@682
  1067
Dean@682
  1068
        /// <summary>
Dean@169
  1069
        /// Event handler for when a mail item property is changed.
Dean@169
  1070
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
  1071
        /// </summary>
Dean@169
  1072
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@445
  1073
        private void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
Dean@159
  1074
        {
Dean@445
  1075
            switch (e.PropertyName.ToUpper())
Dean@159
  1076
            {
Dean@179
  1077
                case "TO":
Dean@1130
  1078
                    this.RequestRatingAndUIUpdate();
Dean@159
  1079
                    break;
Dean@1106
  1080
                    // Outlook bug: there are always both events, so one is enough
Dean@1106
  1081
                    //case "CC":
Dean@1130
  1082
                    //    this.RequestRatingAndUIUpdate();
Dean@1106
  1083
                    //    break;
Dean@159
  1084
            }
Dean@169
  1085
Dean@169
  1086
            return;
Dean@159
  1087
        }
Dean@159
  1088
Dean@179
  1089
        /// <summary>
Dean@1021
  1090
        /// Event handler for when the manager form is closed.
Dean@1021
  1091
        /// </summary>
Dean@1021
  1092
        private void ManagerForm_FormClosed(object sender, FormClosedEventArgs e)
Dean@1021
  1093
        {
Dean@1021
  1094
            // Remove the reference held in the FormRegionPrivacyStatus so it stops getting updated
Dean@1021
  1095
            if (this.managerForm != null)
Dean@1021
  1096
            {
Dean@1021
  1097
                this.managerForm.FormClosed -= ManagerForm_FormClosed;
Dean@1021
  1098
                this.managerForm = null;
Dean@1021
  1099
            }
Dean@1021
  1100
Dean@1021
  1101
            return;
Dean@1021
  1102
        }
Dean@1021
  1103
Dean@1021
  1104
        /// <summary>
Dean@859
  1105
        /// Event handler for when the pEp website hyperlink is clicked.
Dean@859
  1106
        /// </summary>
Dean@859
  1107
        private void LinkLabelUpgrade_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Dean@859
  1108
        {
Dean@966
  1109
            try
Dean@966
  1110
            {
Dean@1007
  1111
                Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
Dean@966
  1112
            }
Dean@966
  1113
            catch (Exception ex)
Dean@966
  1114
            {
Dean@1007
  1115
                Globals.Log("LinkLabelUpgrade_LinkClicked: Unable to open website link, " + ex.ToString());
Dean@966
  1116
            }
Dean@966
  1117
Dean@859
  1118
            return;
Dean@859
  1119
        }
Dean@1047
  1120
Dean@1047
  1121
        /// <summary>
Dean@1086
  1122
        /// Event handler for when the privacy view button is clicked within the form control.
Dean@1047
  1123
        /// </summary>
Dean@1086
  1124
        private void FormControlPrivacyStatusChild_PrivacyViewClick(object sender, System.Windows.RoutedEventArgs e)
Dean@1047
  1125
        {
Dean@1130
  1126
            this.RequestRatingAndUIUpdate();
Dean@1047
  1127
            this.BuildAndShowManager();
Dean@1047
  1128
            return;
Dean@1047
  1129
        }
vb@133
  1130
    }
vb@133
  1131
}