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