UI/FormRegionPrivacyStatus.cs
author Thomas
Thu, 17 Nov 2016 16:57:18 +0100
changeset 1458 0bea66928f72
parent 1430 eee46238e0ee
child 1461 4f5d484a863c
permissions -rw-r--r--
OUT-155: Implement workaround to update inspector window when opening a message in the outbox.
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@985
    44
                //WindowFormRegionCollection formRegions;
Dean@327
    45
Dean@327
    46
                /* There is a Microsoft bug at least in Outlook 2013 and Windows 8.1
Dean@327
    47
                 * This bug causes multiple PrivacyStatus form regions to appear stacked on top of each other.
Dean@327
    48
                 * To trigger this bug, on an unencrypted server, click reply to compose an in-line response.
Dean@327
    49
                 * Then click to another tab such as People or Tasks. Then click back on the mail tab to view 
Dean@327
    50
                 * the original email again. Two form regions will be visible: 
Dean@327
    51
                 * (1) for the status during the in-line reply and 
Dean@327
    52
                 * (2) for the status of only the received message.
Dean@327
    53
                 * 
Dean@327
    54
                 * To fix this bug, any existing form regions are found and closed when initializing a new privacy status
Dean@327
    55
                 * form region.
Dean@327
    56
                 */
Dean@985
    57
                /*
Dean@327
    58
                try
Dean@327
    59
                {
Dean@987
    60
                    formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@327
    61
                    if (formRegions.FormRegionPrivacyStatus != null)
Dean@327
    62
                    {
Dean@985
    63
                        // Note: there seems to be no way to actually close the form region.
Dean@985
    64
                        // Therefore, this is as close as possible to actually closing it.
Dean@985
    65
                        // The actual form regions will be cleaned up as soon as another email is selected.
Dean@327
    66
                        formRegions.FormRegionPrivacyStatus.OutlookFormRegion.Visible = false;
Dean@327
    67
                    }
Dean@327
    68
                }
Dean@327
    69
                catch { }
Dean@985
    70
                */
Dean@1169
    71
Dean@327
    72
                return;
vb@133
    73
            }
vb@133
    74
        }
vb@133
    75
vb@133
    76
        #endregion
vb@133
    77
Dean@159
    78
        /* Notes:
Dean@179
    79
         * 
Dean@159
    80
         * Use this.OutlookItem to get a reference to the current Outlook item.
Dean@159
    81
         * Use this.OutlookFormRegion to get a reference to the form region.
Dean@179
    82
         * 
Dean@179
    83
         * UI State Managment:
Dean@179
    84
         * 
Dean@179
    85
         * The UI state is almost entirely set from the associated mail item data.
Dean@179
    86
         * However, a separate state class is maintained to represent the UI as some separation is needed.
Dean@179
    87
         * This logical separation MUST be maintained throughout the code.
Dean@179
    88
         * Specific cases are noted where possible.
Dean@179
    89
         * 
Dean@356
    90
         * The separate privacy status manager form state is also managed here.
Dean@159
    91
         */
Dean@159
    92
Dean@1384
    93
        /// <summary>
Dean@1384
    94
        /// Event for when the form region is expanded or collapsed.
Dean@1384
    95
        /// </summary>
Dean@1384
    96
        public event EventHandler Expanded;
Dean@1384
    97
Dean@1390
    98
        private CryptableMailItem       cryptableMailItem      = null;
Dean@1390
    99
        private bool                    displayMirrorRequested = false;
Dean@1390
   100
        private bool                    isEnabled              = true;
Dean@1384
   101
        private bool?                   isExpandedPrevious     = null;
Dean@1390
   102
        private bool                    isManagerFormEnabled   = true;
Dean@1390
   103
        private bool                    isStarted              = false;
Dean@758
   104
        private FormManagePrivacyStatus managerForm            = null;
Dean@1402
   105
        private bool                    refreshOngoing         = false;
Thomas@1419
   106
        private bool                    repeatProcessing       = false;
Dean@259
   107
Dean@159
   108
        /**************************************************************
Dean@159
   109
         * 
Dean@1375
   110
         * Property Accessors
Dean@1375
   111
         * 
Dean@1375
   112
         *************************************************************/
Dean@1375
   113
Dean@1375
   114
        /// <summary>
Dean@1377
   115
        /// Gets or sets the displayed state.
Dean@1375
   116
        /// </summary>
Dean@1377
   117
        public FormControlPrivacyStatus.State DisplayState
Dean@1375
   118
        {
Dean@1377
   119
            get { return (this.FormControlPrivacyStatusChild.DisplayState); }
Dean@1377
   120
            set { this.FormControlPrivacyStatusChild.DisplayState = value; }
Dean@1375
   121
        }
Dean@1375
   122
Dean@1375
   123
        /**************************************************************
Dean@1375
   124
         * 
Dean@159
   125
         * Methods
Dean@159
   126
         * 
Dean@159
   127
         *************************************************************/
Dean@159
   128
Dean@171
   129
        /// <summary>
Dean@438
   130
        /// Determines if this form region is running in an inspector window.
Dean@438
   131
        /// If not true, it is assumed to be within an explorer window.
Dean@438
   132
        /// </summary>
Dean@438
   133
        /// <returns>True if an inspector window, false if within an explorer.</returns>
Dean@438
   134
        private bool IsWithinInspector()
Dean@438
   135
        {
Dean@438
   136
            bool isWithinInspector = false;
Dean@895
   137
            Outlook.Inspector insp = null;
Dean@438
   138
Dean@438
   139
            try
Dean@438
   140
            {
Dean@895
   141
                /* There are two potential methods to determine if the item is within an inspector.
Dean@895
   142
                 * (1) Use this.OutlookFormRegion.Parent which will contain either the Explorer or an Inspector
Dean@895
   143
                 *     A cast could be tried to an Explorer of this parent and if it fails, it's assumed to be an inspector
Dean@895
   144
                 * (2) Use this.OutlookFormRegion.Inspector which will contain the Inspector if it exists.
Dean@895
   145
                 *     This will fail or return null if this form region is not running within it's own Inspector window.
Dean@895
   146
                 */
Dean@895
   147
                insp = this.OutlookFormRegion.Inspector;
Dean@895
   148
Dean@895
   149
                if (insp != null)
Dean@895
   150
                {
Dean@895
   151
                    isWithinInspector = true;
Dean@895
   152
                }
Dean@438
   153
            }
Dean@438
   154
            catch
Dean@438
   155
            {
Dean@895
   156
                isWithinInspector = false;
Dean@438
   157
            }
Dean@438
   158
            finally
Dean@438
   159
            {
Dean@895
   160
                if (insp != null)
Dean@438
   161
                {
Dean@895
   162
                    Marshal.ReleaseComObject(insp);
Dean@895
   163
                    insp = null;
Dean@438
   164
                }
Dean@438
   165
            }
Dean@438
   166
Dean@438
   167
            return (isWithinInspector);
Dean@438
   168
        }
Dean@438
   169
Dean@438
   170
        /// <summary>
Dean@672
   171
        /// Completes the handshake process when the identity of a partner was previously marked as mistrusted.
Dean@281
   172
        /// </summary>
Dean@646
   173
        /// <param name="myself">The personal identity to complete the handshake with.</param>
Dean@646
   174
        /// <param name="partner">The identity of the partner to complete the handshake with.</param>
Dean@672
   175
        private void DoHandshakeForMistrustedKey(PEPIdentity myself,
Dean@672
   176
                                                 PEPIdentity partner)
Dean@281
   177
        {
Dean@281
   178
            DialogResult result;
Dean@281
   179
Dean@1074
   180
            result = System.Windows.Forms.MessageBox.Show(this.ParentForm,
Dean@1074
   181
                                                          pEp.Properties.Resources.Message_WarningMistrustedKey,
Dean@1074
   182
                                                          pEp.Properties.Resources.Message_TitleConfirmOperation,
Thomas@1184
   183
                                                          MessageBoxButtons.YesNo,
Thomas@1184
   184
                                                          MessageBoxIcon.Warning);
Dean@281
   185
Dean@423
   186
            if (result == DialogResult.Yes)
Dean@281
   187
            {
Dean@646
   188
                this.DoHandshake(myself, partner);
Dean@281
   189
            }
Dean@281
   190
Dean@281
   191
            return;
Dean@281
   192
        }
Dean@281
   193
Dean@281
   194
        /// <summary>
Dean@281
   195
        /// Completes the handshake process where the identity of a partner is confirmed.
Dean@179
   196
        /// </summary>
Dean@646
   197
        /// <param name="myself">The personal identity to complete the handshake with.</param>
Dean@646
   198
        /// <param name="partner">The identity of the partner to complete the handshake with.</param>
Dean@646
   199
        private void DoHandshake(PEPIdentity myself,
Dean@646
   200
                                 PEPIdentity partner)
vb@133
   201
        {
Dean@179
   202
            DialogResult result;
Dean@155
   203
            FormHandshake handshakeDialog;
markus@201
   204
Dean@1249
   205
            Log.Verbose("DoHandshake: Handshake started.");
vb@139
   206
Dean@155
   207
            // Create and show handshake dialog
Dean@1075
   208
            handshakeDialog = new FormHandshake();
Dean@1082
   209
            handshakeDialog.StartPosition = FormStartPosition.CenterParent;
Dean@1377
   210
            handshakeDialog.DisplayState = new FormControlHandshake.State(myself, partner,
Dean@1377
   211
                                                                          Globals.ThisAddIn.Settings.TrustwordsCulture);
Dean@1086
   212
Dean@179
   213
            result = handshakeDialog.ShowDialog(this);
Dean@646
   214
            this.ProcessDoHandshakeResult(result, partner);
Dean@356
   215
Dean@356
   216
            return;
Dean@356
   217
        }
Dean@356
   218
Dean@356
   219
        /// <summary>
Dean@356
   220
        /// Processes the result of the do handshake dialog after a user makes a selection.
Dean@356
   221
        /// </summary>
Dean@356
   222
        /// <param name="result">The result of the handshake dialog selection.</param>
Dean@356
   223
        /// <param name="partner">The identity of the partner to handshake with.</param>
Dean@356
   224
        private void ProcessDoHandshakeResult(DialogResult result,
Dean@384
   225
                                              PEPIdentity partner)
Dean@356
   226
        {
markus@1337
   227
            pEpRating partnerIdentityRating;
markus@1337
   228
            pEpColor partnerIdentityColor;
markus@1337
   229
            pEpIdentity identityPartner = ThisAddIn.PEPEngine.UpdateIdentity(partner.ToCOMType());
vb@133
   230
vb@133
   231
            switch (result)
vb@133
   232
            {
vb@133
   233
                case DialogResult.Yes:
Dean@279
   234
                    {
Dean@1310
   235
                        // Get the rating/color to determine if key was previously mistrusted -- warning to user must be displayed earlier
Dean@1295
   236
                        try
Dean@281
   237
                        {
markus@1337
   238
                            partnerIdentityRating = ThisAddIn.PEPEngine.IdentityRating(identityPartner);
Dean@1310
   239
                        }
Dean@1310
   240
                        catch (COMException ex)
Dean@1310
   241
                        {
markus@1337
   242
                            partnerIdentityRating = pEpRating.pEpRatingUndefined;
Dean@1310
   243
                            Log.Warning("ProcessDoHandshakeResult: Failed to get identity rating, " + ex.ToString());
Dean@1310
   244
                        }
Dean@1341
   245
                        partnerIdentityColor = partnerIdentityRating.ToColor();
Dean@1310
   246
Dean@1310
   247
                        // Process result
Dean@1310
   248
                        try
Dean@1310
   249
                        {
markus@1337
   250
                            if (partnerIdentityColor == pEpColor.pEpColorRed)
Dean@1295
   251
                            {
markus@1337
   252
                                ThisAddIn.PEPEngine.KeyResetTrust(ref identityPartner);
Dean@1295
   253
                            }
Dean@1295
   254
markus@1337
   255
                            identityPartner = ThisAddIn.PEPEngine.TrustPersonalKey(ref identityPartner);
Dean@281
   256
                        }
Dean@1295
   257
                        catch (Exception ex)
Dean@1295
   258
                        {
Dean@1295
   259
                            Log.Error("ProcessDoHandshakeResult: Error occured while trying to set trust: " + ex.ToString());
Dean@1295
   260
                        }
Dean@279
   261
Dean@1130
   262
                        this.ImmediateRatingAndUIUpdate();
Dean@1117
   263
                        this.UpdateManagePrivacyStatusForm();
Dean@279
   264
Dean@279
   265
                        break;
Dean@279
   266
                    }
vb@133
   267
                case DialogResult.No:
Dean@279
   268
                    {
Dean@1295
   269
                        try
Dean@1295
   270
                        {
markus@1337
   271
                            ThisAddIn.PEPEngine.KeyMistrusted(ref identityPartner);
Dean@1295
   272
                        }
Dean@1295
   273
                        catch (Exception ex)
Dean@1295
   274
                        {
Dean@1295
   275
                            Log.Error("ProcessDoHandshakeResult: Error occured while trying to mistrust key: " + ex.ToString());
Dean@1295
   276
                        }
Dean@279
   277
Dean@1130
   278
                        this.ImmediateRatingAndUIUpdate();
Dean@1117
   279
                        this.UpdateManagePrivacyStatusForm();
Dean@279
   280
Dean@279
   281
                        break;
Dean@279
   282
                    }
vb@133
   283
            }
Dean@279
   284
Dean@1249
   285
            Log.Verbose("ProcessDoHandshakeResult: Handshake complete.");
Dean@740
   286
Dean@279
   287
            return;
vb@133
   288
        }
vb@133
   289
Dean@179
   290
        /// <summary>
Dean@179
   291
        /// Reverses any past handshake confirmation by unconfirming the given identity partner.
Dean@179
   292
        /// </summary>
Dean@384
   293
        /// <param name="partner">The identity of the partner to unconfirm.</param>
Dean@384
   294
        private void UndoHandshake(PEPIdentity partner)
vb@133
   295
        {
markus@1337
   296
            pEpIdentity identityPartner = ThisAddIn.PEPEngine.UpdateIdentity(partner.ToCOMType());
markus@1337
   297
            ThisAddIn.PEPEngine.KeyResetTrust(ref identityPartner);
Dean@279
   298
Dean@1130
   299
            this.ImmediateRatingAndUIUpdate();
Dean@1117
   300
            this.UpdateManagePrivacyStatusForm();
Dean@279
   301
Dean@279
   302
            return;
vb@133
   303
        }
vb@133
   304
Dean@161
   305
        /// <summary>
Dean@279
   306
        /// Builds a new manager form state using this encryption state/mail item current state.
Dean@279
   307
        /// </summary>
Dean@279
   308
        /// <returns>A new manager form state.</returns>
Dean@1069
   309
        private FormControlManagePrivacyStatus.State GetManagerState()
Dean@279
   310
        {
Dean@737
   311
            int currIndex = 0;
Dean@527
   312
            bool isMyself;
Dean@364
   313
            string trustwordsShort;
Dean@1069
   314
            BitmapImage imageForceUnencOn;
Dean@1069
   315
            BitmapImage imageGreen;
Dean@1069
   316
            BitmapImage imageNoColor;
Dean@1069
   317
            BitmapImage imageRed;
Dean@1069
   318
            BitmapImage imageYellow;
Dean@384
   319
            List<PEPIdentity> identities;
Dean@1106
   320
            PEPIdentity identity;
Dean@384
   321
            PEPIdentity myIdentity;
Dean@259
   322
            SelectionItem item;
Dean@1069
   323
            FormControlManagePrivacyStatus.State managerState = new FormControlManagePrivacyStatus.State();
Dean@1069
   324
Dean@1390
   325
            if (this.cryptableMailItem != null)
Dean@769
   326
            {
Dean@1298
   327
                // Load all images from resources
Dean@1298
   328
                imageForceUnencOn = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImageForceUnencOn.png", UriKind.RelativeOrAbsolute));
Dean@1298
   329
                imageGreen = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusGreen.png", UriKind.RelativeOrAbsolute));
Dean@1298
   330
                imageNoColor = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusNoColor.png", UriKind.RelativeOrAbsolute));
Dean@1298
   331
                imageRed = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusRed.png", UriKind.RelativeOrAbsolute));
Dean@1298
   332
                imageYellow = new BitmapImage(new Uri("pack://application:,,,/pEp;component/Resources/ImagePrivacyStatusYellow.png", UriKind.RelativeOrAbsolute));
Dean@1298
   333
Dean@1390
   334
                // Resolve all recipients -- this ensures the identities list is correctly populated
Dean@1390
   335
                this.ResolveAllRecipients();
Dean@332
   336
Dean@1298
   337
                managerState.Rating = this.FormControlPrivacyStatusChild.DisplayState.Rating;
Dean@1390
   338
                managerState.IsIncoming = this.cryptableMailItem.IsIncoming;
Dean@259
   339
Dean@1298
   340
                // Get identities
Dean@1390
   341
                identities = this.cryptableMailItem.Recipients;
Dean@1390
   342
                myIdentity = this.cryptableMailItem.Myself;
Dean@1106
   343
Dean@1402
   344
                try
Dean@1402
   345
                {
Dean@1402
   346
                    myIdentity.Rating = ThisAddIn.PEPEngine.IdentityRating(myIdentity.ToCOMType());
Dean@1402
   347
                }
Dean@1402
   348
                catch (COMException ex)
Dean@1402
   349
                {
Dean@1402
   350
                    myIdentity.Rating = pEpRating.pEpRatingUndefined;
Dean@1402
   351
                    Log.Warning("GetManagerState: Failed to get myself identity rating, " + ex.ToString());
Dean@1402
   352
                }
Dean@1402
   353
Dean@1298
   354
                // Include the from identity for incoming messages at the beginning
Dean@1390
   355
                identity = this.cryptableMailItem.From;
Dean@1390
   356
                if ((this.cryptableMailItem.IsIncoming) &&
Dean@1298
   357
                    (identity != null))
Dean@1298
   358
                {
Dean@1298
   359
                    identities.Insert(0, identity);
Dean@1298
   360
                }
Dean@259
   361
Dean@1298
   362
                // Remove own identity in the MailItem from the list
Dean@1298
   363
                if (myIdentity != null)
Dean@638
   364
                {
Dean@1298
   365
                    for (int i = (identities.Count - 1); i >= 0; i--)
Dean@1106
   366
                    {
Dean@1298
   367
                        if ((identities[i] != null) &&
Dean@1298
   368
                            (identities[i].EqualsByAddress(myIdentity)))
Dean@1298
   369
                        {
Dean@1298
   370
                            identities.RemoveAt(i);
Dean@1298
   371
                        }
Dean@1106
   372
                    }
Dean@638
   373
                }
Dean@366
   374
Dean@1298
   375
                // Add identities
Dean@1298
   376
                identities = PEPIdentity.ToFlatList(identities);
Dean@1298
   377
                managerState.Identities.Clear();
Dean@1298
   378
                foreach (PEPIdentity ident in identities)
Dean@1298
   379
                {
Dean@1298
   380
                    // Needed so index can be used within anonymous methods without getting into the 'outer variable trap' of currIndex
Dean@1298
   381
                    int index = currIndex;
Dean@737
   382
Dean@1298
   383
                    if (ident.IsAddressValid)
Dean@1298
   384
                    {
Dean@1298
   385
                        // Update the partner identity to get a fingerprint, preserve IsForceUnencrypted
markus@1337
   386
                        pEpIdentity partnerIdentity_s = ThisAddIn.PEPEngine.UpdateIdentity(ident.ToCOMType());
Dean@1298
   387
                        PEPIdentity partnerIdentity = new PEPIdentity(partnerIdentity_s);
Dean@1298
   388
                        partnerIdentity.IsForceUnencrypted = ident.IsForceUnencrypted;
Dean@774
   389
Dean@1298
   390
                        try
Dean@1298
   391
                        {
Dean@1362
   392
                            partnerIdentity.Rating = ThisAddIn.PEPEngine.IdentityRating(partnerIdentity_s);
Dean@1298
   393
                        }
Dean@1298
   394
                        catch (COMException ex)
Dean@1298
   395
                        {
Dean@1362
   396
                            partnerIdentity.Rating = pEpRating.pEpRatingUndefined;
Dean@1402
   397
                            Log.Warning("GetManagerState: Failed to get partner identity rating, " + ex.ToString());
Dean@1298
   398
                        }
Dean@1018
   399
Dean@1298
   400
                        isMyself = PEPIdentity.GetIsOwnIdentity(partnerIdentity.Address);
Dean@724
   401
Dean@1298
   402
                        // Calculate trustwords
Dean@1430
   403
                        trustwordsShort = AdapterExtensions.GetTrustwords(myIdentity,
Dean@1430
   404
                                                                          partnerIdentity,
Dean@1430
   405
                                                                          Globals.ThisAddIn.Settings.TrustwordsCulture?.TwoLetterISOLanguageName,
Dean@1430
   406
                                                                          false);
Dean@880
   407
Dean@1298
   408
                        item = new SelectionItem();
Dean@1348
   409
                        item.TextLine1 = partnerIdentity.UserName;
Dean@1298
   410
                        item.TextLine2 = partnerIdentity.Address;
Dean@1430
   411
                        item.Tag = trustwordsShort;
Dean@259
   412
Dean@1298
   413
                        // Don't show both the user name and address if they are the same
Dean@1298
   414
                        if ((item.TextLine1 != null) &&
Dean@1298
   415
                            (item.TextLine2 != null) &&
Dean@1298
   416
                            (item.TextLine1 == item.TextLine2))
Dean@1298
   417
                        {
Dean@1298
   418
                            item.IsTwoTextLinesVisible = false;
Dean@1298
   419
                        }
Dean@332
   420
Dean@1298
   421
                        // Set image
Dean@1298
   422
                        if ((partnerIdentity.IsForceUnencryptedBool) &&
Dean@1298
   423
                            (managerState.IsIncoming == false))
Dean@641
   424
                        {
Dean@1298
   425
                            item.ItemImage = imageForceUnencOn;
Dean@641
   426
                        }
Dean@1298
   427
                        else
Dean@1298
   428
                        {
Dean@1362
   429
                            switch (partnerIdentity.Rating.ToColor())
Dean@1298
   430
                            {
markus@1337
   431
                                case pEpColor.pEpColorGreen:
Dean@1298
   432
                                    item.ItemImage = imageGreen;
Dean@1298
   433
                                    break;
markus@1337
   434
                                case pEpColor.pEpColorYellow:
Dean@1298
   435
                                    item.ItemImage = imageYellow;
Dean@1298
   436
                                    break;
markus@1337
   437
                                case pEpColor.pEpColorRed:
Dean@1298
   438
                                    item.ItemImage = imageRed;
Dean@1298
   439
                                    break;
markus@1337
   440
                                case pEpColor.pEpColorNoColor:
Dean@1298
   441
                                    item.ItemImage = imageNoColor;
Dean@1298
   442
                                    break;
Dean@1298
   443
                                default:
Dean@1298
   444
                                    item.ItemImage = null;
Dean@1298
   445
                                    break;
Dean@1298
   446
                            }
Dean@1298
   447
                        }
Dean@259
   448
Dean@1298
   449
                        // Set button
Dean@1298
   450
                        if ((isMyself) ||
Dean@1298
   451
                            ((partnerIdentity.IsForceUnencryptedBool) &&
Dean@1298
   452
                             (managerState.IsIncoming == false)))
Dean@485
   453
                        {
Dean@1298
   454
                            item.IsButtonVisible = false;
Dean@641
   455
                            item.IsExpandable = false;
Dean@485
   456
                        }
Dean@485
   457
                        else
Dean@485
   458
                        {
Dean@1362
   459
                            switch (partnerIdentity.Rating.ToColor())
Dean@1298
   460
                            {
Dean@1362
   461
                                case pEpColor.pEpColorGreen:
Dean@1362
   462
                                    {
Dean@1362
   463
                                        // Undo handshake
Dean@1362
   464
                                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_StopTrusting;
Dean@1362
   465
                                        item.IsButtonVisible = true;
Dean@1362
   466
                                        item.ButtonOnClick = (x, y) => { this.UndoHandshake(partnerIdentity.Copy()); };
Dean@1298
   467
Dean@1362
   468
                                        item.IsExpandable = false;
Dean@1298
   469
Dean@1362
   470
                                        break;
Dean@1362
   471
                                    }
Dean@1362
   472
                                case pEpColor.pEpColorYellow:
Dean@1362
   473
                                    {
Dean@1362
   474
                                        // Do handshake
Dean@1362
   475
                                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@1362
   476
                                        item.IsButtonVisible = true;
Dean@1362
   477
                                        item.ButtonOnClick = (x, y) => { this.DoHandshake(myIdentity.Copy(), partnerIdentity.Copy()); };
Dean@1298
   478
Dean@1362
   479
                                        item.IsExpandable = true;
Dean@1362
   480
                                        item.ExpandedText = pEp.Properties.Resources.Handshake_ConfirmTrustwordsText + "\n\n" +
Dean@1362
   481
                                                            trustwordsShort;
Dean@1298
   482
Dean@1362
   483
                                        item.ExpandedButton1Text = pEp.Properties.Resources.Handshake_ConfirmText;
Dean@1362
   484
                                        item.ExpandedButton1OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.Yes, partnerIdentity.Copy()); };
Dean@1362
   485
                                        item.ExpandedButton1Style = (Style)Globals.ResourceDict["StyleConfirmButton"];
Dean@1362
   486
Dean@1362
   487
                                        item.ExpandedButton2Text = pEp.Properties.Resources.Handshake_CancelText;
Dean@1362
   488
                                        item.ExpandedButton2OnClick = (x, y) =>
Dean@1362
   489
                                        {
Dean@1362
   490
                                            SelectionItem selItem;
Dean@1362
   491
                                            FormControlManagePrivacyStatus.State activeState;
Dean@1362
   492
Dean@1362
   493
                                            if (this.managerForm != null)
Dean@1362
   494
                                            {
Dean@1377
   495
                                                activeState = this.managerForm.DisplayState;
Dean@1362
   496
Dean@1362
   497
                                                if ((index >= 0) &&
Dean@1362
   498
                                                    (index < activeState.Identities.Count))
Dean@1362
   499
                                                {
Dean@1362
   500
                                                    selItem = activeState.Identities[index];
Dean@1362
   501
                                                    selItem.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@1362
   502
                                                    selItem.IsExpanded = false;
Dean@1362
   503
                                                    selItem.IsExpandable = false;
Dean@1362
   504
                                                }
Dean@1362
   505
                                            }
Dean@1362
   506
                                        };
Dean@1362
   507
                                        item.ExpandedButton2Style = (Style)Globals.ResourceDict["StyleCancelButton"];
Dean@1362
   508
Dean@1362
   509
                                        item.ExpandedButton3Text = pEp.Properties.Resources.Handshake_WrongText;
Dean@1362
   510
                                        item.ExpandedButton3OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.No, partnerIdentity.Copy()); };
Dean@1362
   511
                                        item.ExpandedButton3Style = (Style)Globals.ResourceDict["StyleWrongButton"];
Dean@1362
   512
Dean@1362
   513
                                        break;
Dean@1362
   514
                                    }
Dean@1362
   515
                                case pEpColor.pEpColorRed:
Dean@1298
   516
                                    {
Dean@1362
   517
                                        // Redo handshake with confirmation
Dean@1362
   518
                                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@1362
   519
                                        item.IsButtonVisible = true;
Dean@1362
   520
                                        item.ButtonOnClick = (x, y) => { this.DoHandshakeForMistrustedKey(myIdentity.Copy(), partnerIdentity.Copy()); };
Dean@1298
   521
Dean@1362
   522
                                        item.IsExpandable = false;
Dean@1298
   523
Dean@1362
   524
                                        break;
Dean@1362
   525
                                    }
Dean@1362
   526
                                default:
Dean@1362
   527
                                    {
Dean@1362
   528
                                        item.IsButtonVisible = false;
Dean@1362
   529
                                        item.IsExpandable = false;
Dean@1362
   530
                                        break;
Dean@1362
   531
                                    }
Dean@1298
   532
                            }
Dean@485
   533
                        }
Dean@366
   534
                    }
Dean@1298
   535
                    else // Invalid identity
Dean@1298
   536
                    {
Dean@1298
   537
                        item = new SelectionItem();
Dean@1348
   538
                        item.TextLine1 = ident.UserName;
Dean@1298
   539
                        item.TextLine2 = ident.Address;
Dean@1298
   540
                        item.ItemImage = null;
Dean@1298
   541
                        item.IsTwoTextLinesVisible = true;
Dean@1298
   542
                        item.IsButtonVisible = false;
Dean@1298
   543
                    }
Dean@1298
   544
Dean@1298
   545
                    managerState.Identities.Add(item);
Dean@1298
   546
                    currIndex++;
Dean@259
   547
                }
Dean@259
   548
Dean@1298
   549
                // Attempt to select and expand the first identity requiring a handshake
Dean@1298
   550
                for (int i = 0; i < managerState.Identities.Count; i++)
Dean@375
   551
                {
Dean@1298
   552
                    // Determine if it's a handshake identity by checking if it's expandable
Dean@1298
   553
                    if (managerState.Identities[i].IsExpandable)
Dean@1298
   554
                    {
Dean@1298
   555
                        managerState.Identities[i].TextButton = Properties.Resources.PrivacyStatus_HandshakeAdvanced;
Dean@1298
   556
                        managerState.Identities[i].IsExpanded = true;
Dean@1298
   557
                        managerState.SelectedIdentityIndex = i;
Dean@1298
   558
                        break;
Dean@1298
   559
                    }
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@1082
   578
                form.StartPosition = FormStartPosition.CenterParent;
Dean@1021
   579
                form.FormClosed += ManagerForm_FormClosed;
Dean@1377
   580
                form.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@1130
   594
        /// Schedules for the pEp 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@1130
   597
        public void RequestRatingAndUIUpdate()
Dean@1001
   598
        {
Dean@1390
   599
            if (this.isEnabled)
Dean@1390
   600
            {
Dean@1390
   601
                this.TimerRefresh.Enabled = true;
Dean@1390
   602
            }
Dean@1001
   603
            return;
Dean@1001
   604
        }
Dean@1001
   605
Dean@1001
   606
        /// <summary>
Dean@1130
   607
        /// Immediately starts the update of UI rating based on the associated mail item.
Dean@1001
   608
        /// This will start decryption as necessary and also will update the displayed mirror.
Dean@1001
   609
        /// This method by-passes the refresh timer completely.
Dean@1001
   610
        /// WARNING: This method assumes the message is fully downloaded already.
Dean@1001
   611
        /// </summary>
Dean@1130
   612
        private void ImmediateRatingAndUIUpdate()
Dean@159
   613
        {
Dean@1390
   614
            if ((this.isEnabled) &&
Dean@1390
   615
                (this.cryptableMailItem != null))
Dean@159
   616
            {
Dean@1301
   617
                Log.Verbose("ImmediateRatingAndUIUpdate: Starting processing.");
Dean@740
   618
Dean@1301
   619
                // Start the rating calculation/decryption process
Dean@1390
   620
                this.cryptableMailItem.StartProcessing();
Dean@159
   621
            }
Dean@159
   622
Dean@179
   623
            return;
Dean@159
   624
        }
Dean@159
   625
Dean@709
   626
        /// <summary>
Thomas@1458
   627
        /// Workaround method to update the current inspector window. This is done by moving the mail item
Thomas@1458
   628
        /// to a temporary folder first and then back to the current folder. Both folders CANNOT be the same.
Thomas@1458
   629
        /// As a fallback, the mail item stays in the temporary folder if moving back to the current folder
Thomas@1458
   630
        /// fails.
Thomas@1458
   631
        /// </summary>
Thomas@1458
   632
        private void UpdateInspector(Outlook.OlDefaultFolders currentFolder, 
Thomas@1458
   633
                                     Outlook.OlDefaultFolders tempFolder = Outlook.OlDefaultFolders.olFolderDrafts)
Thomas@1458
   634
        {
Thomas@1458
   635
            Outlook.Application application = null;
Thomas@1458
   636
            Outlook.Inspector currentInspector = null;
Thomas@1458
   637
            Outlook.Inspector newInspector = null;
Thomas@1458
   638
            Outlook.MailItem omi = null;
Thomas@1458
   639
            Outlook.MailItem tempMailItem = null;
Thomas@1458
   640
Thomas@1458
   641
            if (currentFolder == tempFolder)
Thomas@1458
   642
            {
Thomas@1458
   643
                throw new Exception("UpdateInspector: temporary folder and current folder cannot be the same.");
Thomas@1458
   644
            }
Thomas@1458
   645
Thomas@1458
   646
            omi = this.OutlookItem as Outlook.MailItem;
Thomas@1458
   647
Thomas@1458
   648
            if (omi != null)
Thomas@1458
   649
            {
Thomas@1458
   650
                try
Thomas@1458
   651
                {
Thomas@1458
   652
                    application = omi.Application;
Thomas@1458
   653
                    currentInspector = omi.GetInspector;
Thomas@1458
   654
Thomas@1458
   655
                    if ((currentInspector != null) &&
Thomas@1458
   656
                        (application != null))
Thomas@1458
   657
                    {
Thomas@1458
   658
                        var left = currentInspector.Left;
Thomas@1458
   659
                        var top = currentInspector.Top;
Thomas@1458
   660
                        var width = currentInspector.Width;
Thomas@1458
   661
                        var height = currentInspector.Height;
Thomas@1458
   662
                        var windowState = currentInspector.WindowState;
Thomas@1458
   663
Thomas@1458
   664
                        tempMailItem = omi.MoveToDefaultFolder(tempFolder) as Outlook.MailItem;
Thomas@1458
   665
Thomas@1458
   666
                        if (tempMailItem != null)
Thomas@1458
   667
                        {
Thomas@1458
   668
                            try
Thomas@1458
   669
                            {
Thomas@1458
   670
                                omi = tempMailItem.MoveToDefaultFolder(currentFolder) as Outlook.MailItem;
Thomas@1458
   671
                            }
Thomas@1458
   672
                            catch
Thomas@1458
   673
                            {
Thomas@1458
   674
                                omi = tempMailItem.Copy();
Thomas@1458
   675
                            }
Thomas@1458
   676
Thomas@1458
   677
                            newInspector = application.Inspectors.Add(omi);
Thomas@1458
   678
Thomas@1458
   679
                            if (windowState == Outlook.OlWindowState.olNormalWindow)
Thomas@1458
   680
                            {
Thomas@1458
   681
                                newInspector.Left = left;
Thomas@1458
   682
                                newInspector.Top = top;
Thomas@1458
   683
                                newInspector.Width = width;
Thomas@1458
   684
                                newInspector.Height = height;
Thomas@1458
   685
                            }
Thomas@1458
   686
Thomas@1458
   687
                            newInspector.Display();
Thomas@1458
   688
                            newInspector.WindowState = windowState;
Thomas@1458
   689
                        }
Thomas@1458
   690
                    }
Thomas@1458
   691
                    else
Thomas@1458
   692
                    {
Thomas@1458
   693
                        Log.Verbose("UpdateInspector: Error retrieving inspector window or application.");
Thomas@1458
   694
                    }
Thomas@1458
   695
                }
Thomas@1458
   696
                catch (Exception e)
Thomas@1458
   697
                {
Thomas@1458
   698
                    Log.Verbose("UpdateInspector: Error updating inspector window. " + e.Message);
Thomas@1458
   699
                }
Thomas@1458
   700
                finally
Thomas@1458
   701
                {
Thomas@1458
   702
                    if (application != null)
Thomas@1458
   703
                    {
Thomas@1458
   704
                        Marshal.ReleaseComObject(application);
Thomas@1458
   705
                        application = null;
Thomas@1458
   706
                    }
Thomas@1458
   707
Thomas@1458
   708
                    if (currentInspector != null)
Thomas@1458
   709
                    {
Thomas@1458
   710
                        Marshal.ReleaseComObject(currentInspector);
Thomas@1458
   711
                        currentInspector = null;
Thomas@1458
   712
                    }
Thomas@1458
   713
Thomas@1458
   714
                    if (newInspector != null)
Thomas@1458
   715
                    {
Thomas@1458
   716
                        Marshal.ReleaseComObject(newInspector);
Thomas@1458
   717
                        newInspector = null;
Thomas@1458
   718
                    }
Thomas@1458
   719
Thomas@1458
   720
                    if (tempMailItem != null)
Thomas@1458
   721
                    {
Thomas@1458
   722
                        Marshal.ReleaseComObject(tempMailItem);
Thomas@1458
   723
                        tempMailItem = null;
Thomas@1458
   724
                    }
Thomas@1458
   725
Thomas@1458
   726
                    if (omi != null)
Thomas@1458
   727
                    {
Thomas@1458
   728
                        Marshal.ReleaseComObject(omi);
Thomas@1458
   729
                        omi = null;
Thomas@1458
   730
                    }
Thomas@1458
   731
                }
Thomas@1458
   732
            }
Thomas@1458
   733
Thomas@1458
   734
            return;
Thomas@1458
   735
        }
Thomas@1458
   736
Thomas@1458
   737
        /// <summary>
Dean@1117
   738
        /// Immediately update the manage privacy status form display state.
Dean@1117
   739
        /// This by default will completely rebuild the display state.
Dean@1117
   740
        /// </summary>
Dean@1130
   741
        /// <param name="onlyRating">True to update only the rating, false to rebuild the entire display state.</param>
Dean@1130
   742
        private void UpdateManagePrivacyStatusForm(bool onlyRating = false)
Dean@1117
   743
        {
Dean@1117
   744
            if (this.managerForm != null)
Dean@1117
   745
            {
Dean@1130
   746
                if (onlyRating)
Dean@1117
   747
                {
Dean@1130
   748
                    // Only update the message rating
Dean@1377
   749
                    this.managerForm.DisplayState.Rating = this.FormControlPrivacyStatusChild.DisplayState.Rating;
Dean@1117
   750
                }
Dean@1117
   751
                else
Dean@1117
   752
                {
Dean@1117
   753
                    // Rebuild the entire display state which will update any identity changes
Dean@1377
   754
                    this.managerForm.DisplayState = this.GetManagerState();
Dean@1117
   755
                }
Dean@1117
   756
            }
Dean@1117
   757
Dean@1117
   758
            return;
Dean@1117
   759
        }
Dean@1117
   760
Dean@1117
   761
        /// <summary>
Dean@709
   762
        /// Clears the associated unencrypted preview and displays the given note (if any).
Dean@709
   763
        /// </summary>
Dean@709
   764
        /// <param name="note">The note to diplsay.</param>
Dean@709
   765
        private void ClearPreview(string note = null)
Dean@709
   766
        {
Dean@987
   767
            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
   768
Dean@709
   769
            if ((formRegions != null) &&
Dean@709
   770
                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@709
   771
                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@709
   772
            {
Dean@709
   773
                formRegions.FormRegionPreviewUnencrypted.ClearMessage();
Dean@709
   774
                formRegions.FormRegionPreviewUnencrypted.SetNote(note);
Dean@709
   775
            }
Dean@709
   776
Dean@709
   777
            return;
Dean@709
   778
        }
Dean@709
   779
Dean@1390
   780
        /// <summary>
Dean@1390
   781
        /// Sets whether processing of the mail item is enabled.
Dean@1390
   782
        /// This should commonly be set from the .GetIsPEPEnabled() value.
Dean@1390
   783
        /// </summary>
Dean@1390
   784
        private void SetIsEnabled(bool enabled)
Dean@1390
   785
        {
Dean@1390
   786
            bool setByCache = false;
Dean@1390
   787
            PEPIdentity currIdent;
Dean@1390
   788
            Globals.ReturnStatus sts;
Dean@1390
   789
            Outlook.MailItem omi = null;
Dean@1390
   790
            Outlook.Recipient currUser = null;
Dean@1390
   791
            Outlook.Account currAccount = null;
Dean@1390
   792
            Outlook.Account sendingAccount = null;
Dean@1390
   793
            Outlook.NameSpace ns = null;
Dean@1390
   794
Dean@1390
   795
            this.OutlookFormRegion.Visible = enabled;
Dean@1390
   796
            this.isEnabled = enabled;
Dean@1390
   797
Dean@1390
   798
            if (enabled)
Dean@1390
   799
            {
Dean@1390
   800
                // Do not allow initialization more than once
Dean@1390
   801
                if (this.isStarted == false)
Dean@1390
   802
                {
Dean@1390
   803
                    /* It's possible for new draft MailItems to be created outside the context of an account.
Dean@1390
   804
                     * In this situation the SendUsingAccount will always be null which of course breaks several pEp operations.
Dean@1390
   805
                     * The operations themselves cannot make the assumption about what account information to use.
Dean@1390
   806
                     * Therefore, the situation is detected here and for draft mail items a null SendUsingAccount will be 
Dean@1390
   807
                     * set with the session's default account.
Dean@1390
   808
                     */
Dean@1390
   809
                    try
Dean@1390
   810
                    {
Dean@1390
   811
                        ns = Globals.ThisAddIn.Application.Session;
Dean@1390
   812
Dean@1390
   813
                        if ((this.OutlookItem is Outlook.MailItem) &&
Dean@1390
   814
                            ((Outlook.MailItem)this.OutlookItem).GetIsDraft())
Dean@1390
   815
                        {
Dean@1390
   816
                            omi = this.OutlookItem as Outlook.MailItem;
Dean@1390
   817
                            sendingAccount = omi.SendUsingAccount;
Dean@1390
   818
                            currUser = ns.CurrentUser;
Dean@1390
   819
Dean@1390
   820
                            if (sendingAccount == null)
Dean@1390
   821
                            {
Dean@1390
   822
                                sts = PEPIdentity.Create(currUser, out currIdent);
Dean@1390
   823
Dean@1390
   824
                                if (sts == Globals.ReturnStatus.Success)
Dean@1390
   825
                                {
Dean@1390
   826
                                    currAccount = PEPIdentity.GetOwnAccount(currIdent.Address);
Dean@1390
   827
                                    omi.SendUsingAccount = currAccount;
Dean@1390
   828
                                }
Dean@1390
   829
                            }
Dean@1390
   830
                        }
Dean@1390
   831
                    }
Dean@1390
   832
                    catch { }
Dean@1390
   833
                    finally
Dean@1390
   834
                    {
Dean@1390
   835
                        if (omi != null)
Dean@1390
   836
                        {
Dean@1390
   837
                            Marshal.ReleaseComObject(omi);
Dean@1390
   838
                            omi = null;
Dean@1390
   839
                        }
Dean@1390
   840
Dean@1390
   841
                        if (currUser != null)
Dean@1390
   842
                        {
Dean@1390
   843
                            Marshal.ReleaseComObject(currUser);
Dean@1390
   844
                            currUser = null;
Dean@1390
   845
                        }
Dean@1390
   846
Dean@1390
   847
                        if (currAccount != null)
Dean@1390
   848
                        {
Dean@1390
   849
                            Marshal.ReleaseComObject(currAccount);
Dean@1390
   850
                            currAccount = null;
Dean@1390
   851
                        }
Dean@1390
   852
Dean@1390
   853
                        if (sendingAccount != null)
Dean@1390
   854
                        {
Dean@1390
   855
                            Marshal.ReleaseComObject(sendingAccount);
Dean@1390
   856
                            sendingAccount = null;
Dean@1390
   857
                        }
Dean@1390
   858
Dean@1390
   859
                        if (ns != null)
Dean@1390
   860
                        {
Dean@1390
   861
                            Marshal.ReleaseComObject(ns);
Dean@1390
   862
                            ns = null;
Dean@1390
   863
                        }
Dean@1390
   864
                    }
Dean@1390
   865
Dean@1390
   866
                    // Set reader upgrade link visibility
Dean@1390
   867
                    if ((Globals.RELEASE_MODE == Globals.ReleaseMode.Reader) &&
Dean@1390
   868
                        (this.cryptableMailItem != null) &&
Dean@1390
   869
                        (this.cryptableMailItem.IsDraft))
Dean@1390
   870
                    {
Dean@1390
   871
                        this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = true;
Dean@1390
   872
                    }
Dean@1390
   873
                    else
Dean@1390
   874
                    {
Dean@1390
   875
                        this.FormControlPrivacyStatusChild.DisplayState.UpgradeLinkIsVisible = false;
Dean@1390
   876
                    }
Dean@1390
   877
Dean@1390
   878
                    /* For a MailItem being forwarded or replied to it's import to check against the encrypted conversation cache.
Dean@1390
   879
                     * This is normally done within CryptableMailItem_EncryptedConversationCacheUpdated; however,
Dean@1390
   880
                     * for trusted servers that use an in-line reponse, the EncryptedConversationCacheUpdated event occurs
Dean@1390
   881
                     * BEFORE the new mail item is created. This means it never sees the event.
Dean@1390
   882
                     * To get around this issue, when the form region is first shown, also check if the mail item
Dean@1390
   883
                     * exists in the encrypted conversation cache and should be marked as formerly encrypted.
Dean@1390
   884
                     * If this check passes, never event connect the CryptableMailItem_EncryptedConversationCacheUpdated event.
Dean@1390
   885
                     */
Dean@1390
   886
                    if (this.cryptableMailItem != null)
Dean@1390
   887
                    {
Dean@1390
   888
                        setByCache = this.cryptableMailItem.SetIsOriginallyEncryptedByCache();
Dean@1390
   889
                    }
Dean@1390
   890
Dean@1390
   891
                    // Connect events
Dean@1390
   892
                    if (setByCache == false) { CryptableMailItem.EncryptedConversationCacheUpdated += CryptableMailItem_EncryptedConversationCacheUpdated; }
Dean@1390
   893
                    this.FormControlPrivacyStatusChild.PrivacyViewClick += FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@1390
   894
                    this.Expanded += FormRegionPrivacyStatus_Expanded;
Dean@1390
   895
Dean@1390
   896
                    this.TimerRefresh.Tick += TimerRefresh_Tick;
Dean@1390
   897
                    this.isStarted = true;
Dean@1390
   898
                }
Dean@1390
   899
            }
Dean@1390
   900
            else
Dean@1390
   901
            {
Dean@1390
   902
                // Disconnect events
Dean@1390
   903
                CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1390
   904
                this.FormControlPrivacyStatusChild.PrivacyViewClick -= FormControlPrivacyStatusChild_PrivacyViewClick;
Dean@1390
   905
                this.Expanded -= FormRegionPrivacyStatus_Expanded;
Dean@1390
   906
Dean@1390
   907
                // Stop and disconnect the refresh timer
Dean@1390
   908
                this.TimerRefresh.Stop();
Dean@1390
   909
                this.TimerRefresh.Enabled = false;
Dean@1390
   910
                this.TimerRefresh.Tick -= TimerRefresh_Tick;
Dean@1390
   911
Dean@1390
   912
                this.isStarted = false;
Dean@1390
   913
            }
Dean@1390
   914
Dean@1390
   915
            return;
Dean@1390
   916
        }
Dean@1390
   917
Dean@1390
   918
        /// <summary>
Dean@1390
   919
        /// Resolves all recipients of the Outlook mail item.
Dean@1390
   920
        /// </summary>
Dean@1390
   921
        private void ResolveAllRecipients()
Dean@1390
   922
        {
Dean@1390
   923
            if ((this.isEnabled) &&
Dean@1390
   924
                (this.cryptableMailItem != null))
Dean@1390
   925
            {
Dean@1390
   926
                /*
Dean@1390
   927
                 * Note: The PropertyChanged changed event must be disconnected before trying to resolve.
Dean@1390
   928
                 * This is because the resolve process can modify the contents of the mail item which triggers an event.
Dean@1390
   929
                 * The PropertyChanged event would then trigger a UI refresh cycle. However, because the GetManagerState itself 
Dean@1390
   930
                 * is called within the UI refresh, an infinite loop could occur trying to resolve a recipient that
Dean@1390
   931
                 * cannot be resolved(no address).
Dean@1390
   932
                 */
Dean@1390
   933
                this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@1390
   934
                this.cryptableMailItem.ResolveAllRecipients();
Dean@1390
   935
                this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@1390
   936
            }
Dean@1390
   937
Dean@1390
   938
            return;
Dean@1390
   939
        }
Dean@1390
   940
Dean@159
   941
        /**************************************************************
Dean@159
   942
         * 
Dean@159
   943
         * Event Handling
Dean@159
   944
         * 
Dean@159
   945
         *************************************************************/
Dean@159
   946
Dean@159
   947
        /// <summary>
Dean@680
   948
        /// Event handler that is called when the form region is displayed.
Dean@1211
   949
        /// This is called each time the form region looses then regains visibility 
Dean@680
   950
        /// (for example an other email is selected then back to this one).
Dean@159
   951
        /// </summary>
Dean@312
   952
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   953
        {
Dean@1390
   954
            // Set mail item
Dean@1390
   955
            if (this.OutlookItem is Outlook.MailItem)
Dean@1390
   956
            {
Dean@1390
   957
                this.cryptableMailItem = new CryptableMailItem((Outlook.MailItem)this.OutlookItem);
Dean@1390
   958
            }
Dean@1390
   959
            else
Dean@1390
   960
            {
Dean@1390
   961
                this.cryptableMailItem = null;
Dean@1390
   962
            }
Dean@1169
   963
Dean@1390
   964
            // Connect cryptable mail item events
Dean@1390
   965
            if (this.cryptableMailItem != null)
Dean@680
   966
            {
Dean@1197
   967
                try
Dean@1197
   968
                {
Dean@1390
   969
                    this.cryptableMailItem.PropertyChanged += MailItem_PropertyChanged;
Dean@1390
   970
                    this.cryptableMailItem.ProcessingCompleted += MailItem_ProcessingCompleted;
Dean@1390
   971
                    this.cryptableMailItem.GetMirrorCompleted += MailItem_GetMirrorCompleted;
Dean@1390
   972
                    this.cryptableMailItem.Send += MailItem_Send;
Dean@1390
   973
Dean@1390
   974
                    if (this.cryptableMailItem.IsSecurelyStored)
Dean@1197
   975
                    {
Dean@1390
   976
                        this.cryptableMailItem.Open += MailItem_Open;
Dean@1197
   977
                    }
Dean@1197
   978
                }
Dean@1197
   979
                catch { }
Dean@179
   980
            }
Dean@159
   981
Dean@1390
   982
            // Update pEp enabled status
Dean@1390
   983
            this.SetIsEnabled(((Outlook.MailItem)this.OutlookItem).GetIsPEPEnabled());
Dean@1390
   984
            if (this.isEnabled)
Dean@1390
   985
            {
Dean@1390
   986
                // Call the timer tick method manually to refresh data with no delay
Dean@1390
   987
                this.TimerRefresh_Tick(null, new EventArgs());
Dean@1390
   988
            }
Dean@179
   989
Dean@179
   990
            return;
Dean@159
   991
        }
Dean@159
   992
Dean@159
   993
        /// <summary>
Dean@159
   994
        /// Event handler for when the form region is closed.
Dean@159
   995
        /// </summary>
Dean@312
   996
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   997
        {
Dean@1390
   998
            // Disconnect cryptable mail item
Dean@1390
   999
            if (this.cryptableMailItem != null)
Dean@179
  1000
            {
Dean@179
  1001
                try
Dean@179
  1002
                {
Dean@1390
  1003
                    this.cryptableMailItem.PropertyChanged -= MailItem_PropertyChanged;
Dean@1390
  1004
                    this.cryptableMailItem.ProcessingCompleted -= MailItem_ProcessingCompleted;
Dean@1390
  1005
                    this.cryptableMailItem.GetMirrorCompleted -= MailItem_GetMirrorCompleted;
Dean@1390
  1006
                    this.cryptableMailItem.Open -= MailItem_Open;
Dean@1390
  1007
                    this.cryptableMailItem.Send -= MailItem_Send;
Dean@179
  1008
                }
Dean@179
  1009
                catch { }
Dean@1390
  1010
Dean@1390
  1011
                this.cryptableMailItem.Dispose();
Dean@1390
  1012
                this.cryptableMailItem = null;
Dean@179
  1013
            }
Dean@179
  1014
Dean@1390
  1015
            // Disconnect other
Dean@1390
  1016
            this.SetIsEnabled(false);
Dean@682
  1017
Dean@179
  1018
            return;
Dean@179
  1019
        }
Dean@179
  1020
Dean@179
  1021
        /// <summary>
Dean@1384
  1022
        /// Event handler for when the form region is expanded or collapsed.
Dean@1384
  1023
        /// </summary>
Dean@1384
  1024
        private void FormRegionPrivacyStatus_Expanded(object sender, EventArgs e)
Dean@1384
  1025
        {
Dean@1385
  1026
            /* This Outlook form region has a bug where clicking the '+' button to expand will also
Dean@1385
  1027
             * raise a click event on the privacy status button. Basically, Outlook would fire the click event 
Dean@1385
  1028
             * (FormControlPrivacyStatusChild_PrivacyViewClick) immediately after the region is expanded even though at 
Dean@1385
  1029
             * the time it was clicked it was collapsed and the click already captured by the expander '+' button.
Dean@1385
  1030
             * This code here works around that issue by disabling the manager form when the form region is collapsed.
Dean@1385
  1031
             * Since the click event occurs BEFORE the expanded event, this will disable opening the manage privacy status
Dean@1385
  1032
             * form in this situation.
Dean@1385
  1033
             */
Dean@1390
  1034
            this.isManagerFormEnabled = this.OutlookFormRegion.IsExpanded;
Dean@1384
  1035
Dean@1384
  1036
            return;
Dean@1384
  1037
        }
Dean@1384
  1038
Dean@1384
  1039
        /// <summary>
Dean@1384
  1040
        /// Event handler called after the monitor timer has elapsed.
Dean@1384
  1041
        /// This should be used only for quick polling of changes in Outlook or the UI state.
Dean@1384
  1042
        /// </summary>
Dean@1384
  1043
        private void TimerMonitor_Tick(object sender, EventArgs e)
Dean@1384
  1044
        {
Dean@1384
  1045
            bool isExpandedCurrent;
Dean@1384
  1046
Dean@1384
  1047
            // Check for IsExpanded changes
Dean@1384
  1048
            isExpandedCurrent = this.OutlookFormRegion.IsExpanded;
Dean@1384
  1049
            if ((this.isExpandedPrevious == null) ||
Dean@1384
  1050
                ((bool)this.isExpandedPrevious != isExpandedCurrent))
Dean@1384
  1051
            {
Dean@1384
  1052
                this.Expanded?.Invoke(this, new EventArgs());
Dean@1384
  1053
            }
Dean@1384
  1054
            this.isExpandedPrevious = isExpandedCurrent;
Dean@1384
  1055
Dean@1384
  1056
            return;
Dean@1384
  1057
        }
Dean@1384
  1058
Dean@1384
  1059
        /// <summary>
Dean@179
  1060
        /// Event handler called after the refresh timer has elapsed.
Dean@179
  1061
        /// </summary>
Dean@179
  1062
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
  1063
        {
Dean@1402
  1064
            bool tryAgain = false;
Dean@1402
  1065
            bool markForDownload = false;
Dean@179
  1066
            this.TimerRefresh.Enabled = false; // Only once
Dean@1402
  1067
            Outlook.OlDownloadState dlState;
markus@201
  1068
Dean@709
  1069
            /* The Refresh/UI_Update process is a little more complicated here than other forms.
Dean@709
  1070
             * There are the following components:
Dean@709
  1071
             *   1. TimerRefresh_Tick
Dean@709
  1072
             *        This is the main timer tick event handler called any time
Dean@1130
  1073
             *        a refresh was requested (RequestRatingAndUIUpdate) and hasn't been run yet. 
Dean@1001
  1074
             *        A refresh is requested either at initialization or when a property changes
Dean@709
  1075
             *        (such as MailItem_PropertyChanged). It is on a timer as many 
Dean@709
  1076
             *        property change events could occur rapidy, but only one refresh should
Dean@709
  1077
             *        occur for performance reasons.
Dean@709
  1078
             * 
Dean@1130
  1079
             *   2. ImmediateRatingAndUIUpdate
Dean@1307
  1080
             *        This will re-calculate the mail item's rating.
Dean@1307
  1081
             *        This internally just calls CryptableMailItem.StartProcessing.
Dean@709
  1082
             * 
Dean@1130
  1083
             *   3. MailItem_ProcessingCompleted
Dean@1307
  1084
             *        When processing of the mail item is complete, this even handler will be called.
Dean@1130
  1085
             *        This will update the UI with the latest rating then call StartGetMirror.
Dean@709
  1086
             *        The CopyStateToUI method is used which means any open privacy status form will also
Dean@709
  1087
             *        be updated.
Dean@709
  1088
             * 
Dean@709
  1089
             *   4. MailItem_GetMirrorComplete
Dean@1402
  1090
             *        This is the final step in updating the UI, after a mirror is located, it's contents
Dean@1307
  1091
             *        will be shown in the unencrypted preview.
Dean@709
  1092
             * 
Dean@709
  1093
             * The general calling sequence is as shown above 1->4 with each component calling the next.
Dean@709
  1094
             * However, for methods that update the mail item directly, commonly only 2->4 is needed.
Dean@709
  1095
             */
Dean@709
  1096
Dean@179
  1097
            // Ensure the tick method is not called more than once
Dean@1390
  1098
            if ((this.isEnabled) &&
Dean@1402
  1099
                (refreshOngoing == false))
Dean@179
  1100
            {
Dean@1402
  1101
                this.refreshOngoing = true;
Dean@1402
  1102
Thomas@1419
  1103
                if ((this.cryptableMailItem != null) &&
Thomas@1419
  1104
                    (this.cryptableMailItem.IsBeingProcessed == false))
Dean@1402
  1105
                {
Dean@1402
  1106
                    // Attempt to get the download state
Dean@1402
  1107
                    try
Dean@1402
  1108
                    {
Dean@1402
  1109
                        dlState = this.cryptableMailItem.DownloadState;
Dean@1402
  1110
                    }
Dean@1402
  1111
                    catch (Exception ex)
Dean@1402
  1112
                    {
Dean@1402
  1113
                        Log.Warning("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
Dean@1402
  1114
Dean@1402
  1115
                        // Assume everything is downloaded, but try to download again as well
Dean@1402
  1116
                        dlState = Outlook.OlDownloadState.olFullItem;
Dean@1402
  1117
                        markForDownload = true;
Dean@1402
  1118
                    }
Dean@1402
  1119
Dean@1402
  1120
                    if (dlState == Outlook.OlDownloadState.olFullItem)
Dean@1402
  1121
                    {
Dean@1402
  1122
                        this.ImmediateRatingAndUIUpdate();
Dean@1402
  1123
                    }
Dean@1402
  1124
                    else
Dean@1402
  1125
                    {
Dean@1402
  1126
                        markForDownload = true;
Dean@1402
  1127
                    }
Dean@1402
  1128
Dean@1402
  1129
                    if (markForDownload)
Dean@1402
  1130
                    {
Dean@1402
  1131
                        // Try to mark the message for full download
Dean@1402
  1132
                        try
Dean@1402
  1133
                        {
Dean@1402
  1134
                            this.cryptableMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@1402
  1135
                            tryAgain = true;
Dean@1402
  1136
                        }
Dean@1402
  1137
                        catch (Exception ex)
Dean@1402
  1138
                        {
Dean@1402
  1139
                            Log.Warning("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
Dean@1402
  1140
                        }
Dean@1402
  1141
                    }
Dean@1402
  1142
                }
Thomas@1419
  1143
                else
Thomas@1419
  1144
                {
Thomas@1419
  1145
                    repeatProcessing = true;
Thomas@1419
  1146
                }
Dean@1402
  1147
Dean@1402
  1148
                // Set the timer to refresh again later automatically
Dean@1402
  1149
                if (tryAgain)
Dean@1402
  1150
                {
Dean@1402
  1151
                    this.TimerRefresh.Interval = 100;
Dean@1402
  1152
                    this.TimerRefresh.Enabled = true;
Dean@1402
  1153
                }
Dean@1402
  1154
Dean@1402
  1155
                this.refreshOngoing = false;
Dean@179
  1156
            }
Dean@175
  1157
Dean@159
  1158
            return;
Dean@159
  1159
        }
Dean@159
  1160
Dean@169
  1161
        /// <summary>
Dean@1096
  1162
        /// Event handler for when the processing is completed in the associated mail item.
Dean@709
  1163
        /// This will then update the form region UI and the privacy status window as needed.
Dean@680
  1164
        /// </summary>
Dean@1096
  1165
        private void MailItem_ProcessingCompleted(object sender, CryptableMailItem.ProcessingCompletedEventArgs e)
Dean@680
  1166
        {
Dean@1249
  1167
            Log.Verbose("MailItem_ProcessingComplete: Decryption completed.");
Dean@740
  1168
Dean@680
  1169
            try
Dean@680
  1170
            {
Dean@680
  1171
                // Marshal code back to UI thread as necessary
Dean@680
  1172
                this.Invoke(new Action(() =>
Dean@680
  1173
                    {
Dean@1390
  1174
                        if (this.isEnabled)
Dean@1390
  1175
                        {
Dean@1390
  1176
                            this.FormControlPrivacyStatusChild.DisplayState.Rating = e.Rating;
Dean@1390
  1177
                            RibbonCustomizations.Invalidate();
Dean@1390
  1178
                            this.UpdateManagePrivacyStatusForm(true); // Only update the rating
Dean@709
  1179
Thomas@1419
  1180
                            if (repeatProcessing)
Dean@747
  1181
                            {
Thomas@1419
  1182
                                repeatProcessing = false;
Thomas@1419
  1183
                                this.RequestRatingAndUIUpdate();
Thomas@1419
  1184
                            }
Thomas@1419
  1185
                            else
Thomas@1419
  1186
                            {
Thomas@1458
  1187
                                /* Check if the mail item is in Outbox and update the inspector window
Thomas@1458
  1188
                                 * if possible
Thomas@1458
  1189
                                 */
Thomas@1458
  1190
                                try
Thomas@1458
  1191
                                {
Thomas@1458
  1192
                                    Outlook.MailItem omi = this.OutlookItem as Outlook.MailItem;
Thomas@1458
  1193
Thomas@1458
  1194
                                    if ((omi != null) &&
Thomas@1458
  1195
                                        (omi.GetIsSubmitted()) &&
Thomas@1458
  1196
                                        (omi.GetIsDraft()))
Thomas@1458
  1197
                                    {
Thomas@1458
  1198
                                        var insp = this.OutlookFormRegion.Inspector;
Thomas@1458
  1199
Thomas@1458
  1200
                                        if (insp != null)
Thomas@1458
  1201
                                        {
Thomas@1458
  1202
                                            UpdateInspector(Outlook.OlDefaultFolders.olFolderOutbox);
Thomas@1458
  1203
                                        }
Thomas@1458
  1204
                                    }
Thomas@1458
  1205
                                }
Thomas@1458
  1206
                                catch (Exception ex)
Thomas@1458
  1207
                                {
Thomas@1458
  1208
                                    Log.Verbose("Error while checking if mail item is in outbox or updating inspector. " + ex.Message);
Thomas@1458
  1209
                                }
Thomas@1458
  1210
Thomas@1419
  1211
                                /* Create the unencrypted preview if the mail item is encrypted and
Thomas@1419
  1212
                                 * it is in an encrypted (untrusted) store
Thomas@1419
  1213
                                 * 
Thomas@1419
  1214
                                 * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Thomas@1419
  1215
                                 * it also is initialized after FormRegionPreviewUnencrypted.
Thomas@1419
  1216
                                 */
Thomas@1419
  1217
                                try
Dean@1390
  1218
                                {
Thomas@1419
  1219
                                    if (this.cryptableMailItem.IsSecurelyStored)
Thomas@1419
  1220
                                    {
Thomas@1419
  1221
                                        Log.Verbose("MailItem_ProcessingComplete: Starting mirror location.");
Thomas@1419
  1222
                                        this.cryptableMailItem.StartGetMirror();
Thomas@1419
  1223
                                    }
Thomas@1419
  1224
                                    else
Thomas@1419
  1225
                                    {
Thomas@1419
  1226
                                        this.ClearPreview();
Thomas@1419
  1227
                                    }
Dean@1390
  1228
                                }
Thomas@1419
  1229
                                catch (Exception ex)
Dean@1390
  1230
                                {
Thomas@1419
  1231
                                    // Error is possible in some situations where the mail item was deleted or moved while decryption was ongoing.
Thomas@1419
  1232
                                    // While rare, just log the issue and stop the process
Thomas@1419
  1233
                                    Log.Warning("MailItem_ProcessingComplete: Failed to start mirror location, " + ex.ToString());
Dean@1390
  1234
                                }
Dean@747
  1235
                            }
Dean@709
  1236
                        }
Dean@680
  1237
                    }));
Dean@680
  1238
            }
Dean@680
  1239
            catch (Exception ex)
Dean@680
  1240
            {
Dean@1249
  1241
                Log.Error("MailItem_ProcessingComplete: Error setting UI state, " + ex.ToString());
Dean@680
  1242
            }
Dean@680
  1243
Dean@680
  1244
            return;
Dean@680
  1245
        }
Dean@680
  1246
Dean@680
  1247
        /// <summary>
Dean@709
  1248
        /// Event handler for when the get mirror locating process is complete for the associated mail item.
Dean@709
  1249
        /// This will then update the unencrypted preview in the UI.
Dean@709
  1250
        /// </summary>
Dean@1096
  1251
        private void MailItem_GetMirrorCompleted(object sender, CryptableMailItem.GetMirrorCompletedEventArgs e)
Dean@709
  1252
        {
Dean@709
  1253
            try
Dean@709
  1254
            {
Dean@709
  1255
                // Marshal code back to UI thread as necessary
Dean@709
  1256
                this.Invoke(new Action(() =>
Dean@709
  1257
                    {
Dean@1390
  1258
                        if (this.isEnabled)
Dean@1390
  1259
                        {
Dean@1390
  1260
                            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
Dean@709
  1261
Dean@1390
  1262
                            if ((e.Mirror == null) &&
Dean@1390
  1263
                                (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.Failure))
Dean@910
  1264
                            {
Dean@1390
  1265
                                this.ClearPreview(Properties.Resources.Message_OpenError);
Dean@1390
  1266
                                Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, failure during decryption.");
Dean@910
  1267
                            }
Dean@1390
  1268
                            else if ((e.Mirror == null) &&
Dean@1390
  1269
                                     (this.cryptableMailItem.LastProcessedStatus == Globals.ReturnStatus.FailureNoConnection))
Dean@1390
  1270
                            {
Dean@1390
  1271
                                this.ClearPreview(Properties.Resources.Message_DecryptionNoConnection);
Dean@1390
  1272
                                Log.Verbose("MailItem_GetMirrorComplete: Cannot display mirror, connection failure during decryption.");
Dean@1390
  1273
                            }
Dean@1390
  1274
                            else
Dean@1390
  1275
                            {
Dean@1390
  1276
                                if ((formRegions != null) &&
Dean@1390
  1277
                                    (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@1390
  1278
                                    (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@1390
  1279
                                {
Dean@1390
  1280
                                    formRegions.FormRegionPreviewUnencrypted.SetMessage(e.Mirror);
Dean@1390
  1281
                                    Log.Verbose("MailItem_GetMirrorComplete: Mirror found and displayed.");
Dean@1390
  1282
                                }
Dean@1390
  1283
                            }
Dean@758
  1284
Dean@1390
  1285
                            // Display the mirror if necessary
Dean@1390
  1286
                            if ((this.cryptableMailItem != null) &&
Dean@1390
  1287
                                (this.displayMirrorRequested))
Dean@1390
  1288
                            {
Dean@1390
  1289
                                this.cryptableMailItem.DisplayMirror();
Dean@1390
  1290
                                this.displayMirrorRequested = false;
Dean@1390
  1291
                            }
Dean@758
  1292
                        }
Dean@709
  1293
                    }));
Dean@709
  1294
            }
Dean@709
  1295
            catch (Exception ex)
Dean@709
  1296
            {
Dean@1249
  1297
                Log.Error("MailItem_GetMirrorComplete: Error displaying preview, " + ex.ToString());
Dean@709
  1298
            }
Dean@709
  1299
Dean@709
  1300
            return;
Dean@709
  1301
        }
Dean@709
  1302
Dean@709
  1303
        /// <summary>
Dean@807
  1304
        /// Event handler for the static mail item encrypted conversation cache updated.
Dean@807
  1305
        /// This event is fired after the conversation cache has been updated with parent information following a
Dean@807
  1306
        /// forward or reply mail item event (on a different, likely parent, mail item).
Dean@807
  1307
        /// </summary>
Dean@807
  1308
        private void CryptableMailItem_EncryptedConversationCacheUpdated(object sender, EventArgs e)
Dean@807
  1309
        {
Dean@1390
  1310
            if (this.isEnabled)
Dean@1390
  1311
            {
Dean@1390
  1312
                // Process the mail item now that the cache is updated
Dean@1390
  1313
                this.cryptableMailItem.SetIsOriginallyEncryptedByCache();
Dean@807
  1314
Dean@1390
  1315
                // Remove the event handler -- this can only be processed once because it's timing dependent
Dean@1390
  1316
                CryptableMailItem.EncryptedConversationCacheUpdated -= CryptableMailItem_EncryptedConversationCacheUpdated;
Dean@1390
  1317
            }
Dean@807
  1318
Dean@807
  1319
            return;
Dean@807
  1320
        }
Dean@807
  1321
Dean@807
  1322
        /// <summary>
Dean@169
  1323
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
  1324
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
  1325
        /// </summary>
Dean@169
  1326
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
  1327
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
  1328
        /// and the inspector is not displayed.</param>
Dean@169
  1329
        private void MailItem_Open(ref bool cancel)
Dean@159
  1330
        {
Dean@758
  1331
            bool result;
Dean@758
  1332
Dean@1390
  1333
            if ((this.isEnabled) &&
Dean@1390
  1334
                (this.cryptableMailItem != null) &&
Dean@1390
  1335
                (this.cryptableMailItem.IsSecurelyStored))
Dean@169
  1336
            {
Dean@758
  1337
                // Try to open the mirror
Dean@1390
  1338
                result = this.cryptableMailItem.DisplayMirror();
Dean@758
  1339
Dean@758
  1340
                if (result == false)
Dean@758
  1341
                {
Dean@758
  1342
                    // Set flag to open after decryption/mirror location
Dean@758
  1343
                    this.displayMirrorRequested = true;
Dean@758
  1344
                }
Dean@758
  1345
Dean@758
  1346
                // Always cancel opening the original
Dean@169
  1347
                cancel = true;
Dean@169
  1348
            }
Dean@169
  1349
Dean@169
  1350
            return;
Dean@159
  1351
        }
Dean@159
  1352
Dean@169
  1353
        /// <summary>
Dean@682
  1354
        /// Event handler for when a mail item is sent.
Dean@682
  1355
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865379.aspx
Dean@682
  1356
        /// </summary>
Dean@682
  1357
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@682
  1358
        /// If the event procedure sets this argument to True, the send operation is not completed 
Dean@682
  1359
        /// and the inspector is left open.</param>
Dean@682
  1360
        private void MailItem_Send(ref bool cancel)
Dean@682
  1361
        {
Dean@807
  1362
            DialogResult result;
Dean@807
  1363
Dean@1390
  1364
            if (this.isEnabled)
Dean@807
  1365
            {
Dean@1390
  1366
                if ((Globals.ThisAddIn.Settings.IsSecurityLossWarningEnabled) &&
Dean@1390
  1367
                    (this.cryptableMailItem.IsOriginallyEncrypted) &&
Dean@1390
  1368
                    (this.FormControlPrivacyStatusChild.DisplayState.Rating < pEpRating.pEpRatingUnreliable))
Dean@1390
  1369
                {
Thomas@1318
  1370
Thomas@1318
  1371
#if READER_RELEASE_MODE
Dean@1390
  1372
                    FormReaderSplash warningMessage = new FormReaderSplash(true);
Dean@1390
  1373
                    result = warningMessage.ShowDialog();
Thomas@1318
  1374
Dean@1390
  1375
                    if (result != DialogResult.OK)
Dean@1390
  1376
                    {
Dean@1390
  1377
                        // Cancel sending
Dean@1390
  1378
                        cancel = true;
Dean@1390
  1379
                    }
Dean@1390
  1380
#else
Dean@1390
  1381
                    result = System.Windows.Forms.MessageBox.Show(this.ParentForm,
Dean@1390
  1382
                                                                  pEp.Properties.Resources.Message_WarningSecurityLoss,
Dean@1390
  1383
                                                                  pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@1390
  1384
                                                                  MessageBoxButtons.YesNo,
Dean@1390
  1385
                                                                  MessageBoxIcon.Warning);
Dean@1390
  1386
Dean@1390
  1387
                    if (result == DialogResult.No)
Dean@1390
  1388
                    {
Dean@1390
  1389
                        // Cancel sending
Dean@1390
  1390
                        cancel = true;
Dean@1390
  1391
                    }
Dean@1390
  1392
#endif
Dean@1390
  1393
                }
Dean@1390
  1394
Dean@1390
  1395
                // Stop and disconnect the refresh timer
Dean@1390
  1396
                // This is necessary so an ongoing refresh doesn't try to access a mail item as it's being moved
Dean@1390
  1397
                if (cancel == false)
Thomas@1318
  1398
                {
Dean@1390
  1399
                    this.TimerRefresh.Stop();
Dean@1390
  1400
                    this.TimerRefresh.Enabled = false;
Dean@1390
  1401
                    this.TimerRefresh.Tick -= TimerRefresh_Tick;
Thomas@1318
  1402
                }
Dean@807
  1403
            }
Dean@682
  1404
Dean@682
  1405
            return;
Dean@682
  1406
        }
Dean@682
  1407
Dean@682
  1408
        /// <summary>
Dean@169
  1409
        /// Event handler for when a mail item property is changed.
Dean@169
  1410
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
  1411
        /// </summary>
Dean@169
  1412
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@445
  1413
        private void MailItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
Dean@159
  1414
        {
Dean@1349
  1415
            switch (e.PropertyName.ToUpperInvariant())
Dean@159
  1416
            {
Dean@1390
  1417
                case "BCC":
Dean@1390
  1418
                    {
Dean@1390
  1419
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
Dean@1390
  1420
                        break;
Dean@1390
  1421
                    }
Dean@1390
  1422
                case "CC":
Dean@1390
  1423
                    {
Dean@1390
  1424
                        // Outlook always fires Bcc, Cc and To together so only "TO" is used
Dean@1390
  1425
                        break;
Dean@1390
  1426
                    }
Dean@1390
  1427
                case "SENTONBEHALFOFNAME":
Dean@1390
  1428
                    {
Dean@1390
  1429
                        // Always fired with "SENDUSINGACCOUNT" so is ignored
Dean@1390
  1430
                        break;
Dean@1390
  1431
                    }
Dean@1390
  1432
                case "SENDUSINGACCOUNT":
Dean@1390
  1433
                    {
Dean@1390
  1434
                        // Update pEp enabled status
Dean@1390
  1435
                        this.SetIsEnabled(((Outlook.MailItem)this.OutlookItem).GetIsPEPEnabled());
Dean@1390
  1436
Dean@1390
  1437
                        // Refresh the UI
Dean@1390
  1438
                        this.ResolveAllRecipients();
Dean@1390
  1439
                        this.RequestRatingAndUIUpdate();
Dean@1390
  1440
                        RibbonCustomizations.Invalidate();
Dean@1390
  1441
Dean@1390
  1442
                        break;
Dean@1390
  1443
                    }
Dean@179
  1444
                case "TO":
Dean@1390
  1445
                    {
Dean@1390
  1446
                        if (this.isEnabled)
Dean@1390
  1447
                        {
Dean@1390
  1448
                            this.RequestRatingAndUIUpdate();
Dean@1390
  1449
                        }
Dean@1390
  1450
                        break;
Dean@1390
  1451
                    }
Dean@159
  1452
            }
Dean@169
  1453
Dean@169
  1454
            return;
Dean@159
  1455
        }
Dean@159
  1456
Dean@179
  1457
        /// <summary>
Dean@1021
  1458
        /// Event handler for when the manager form is closed.
Dean@1021
  1459
        /// </summary>
Dean@1021
  1460
        private void ManagerForm_FormClosed(object sender, FormClosedEventArgs e)
Dean@1021
  1461
        {
Dean@1021
  1462
            // Remove the reference held in the FormRegionPrivacyStatus so it stops getting updated
Dean@1021
  1463
            if (this.managerForm != null)
Dean@1021
  1464
            {
Dean@1021
  1465
                this.managerForm.FormClosed -= ManagerForm_FormClosed;
Dean@1021
  1466
                this.managerForm = null;
Dean@1021
  1467
            }
Dean@1021
  1468
Dean@1021
  1469
            return;
Dean@1021
  1470
        }
Dean@1021
  1471
Dean@1021
  1472
        /// <summary>
Dean@859
  1473
        /// Event handler for when the pEp website hyperlink is clicked.
Dean@859
  1474
        /// </summary>
Dean@859
  1475
        private void LinkLabelUpgrade_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
Dean@859
  1476
        {
Dean@966
  1477
            try
Dean@966
  1478
            {
Dean@1007
  1479
                Process.Start(Globals.PEP_WEBSITE_UPGRADE_LINK);
Dean@966
  1480
            }
Dean@966
  1481
            catch (Exception ex)
Dean@966
  1482
            {
Dean@1249
  1483
                Log.Error("LinkLabelUpgrade_LinkClicked: Unable to open website link, " + ex.ToString());
Dean@966
  1484
            }
Dean@966
  1485
Dean@859
  1486
            return;
Dean@859
  1487
        }
Dean@1047
  1488
Dean@1047
  1489
        /// <summary>
Dean@1086
  1490
        /// Event handler for when the privacy view button is clicked within the form control.
Dean@1047
  1491
        /// </summary>
Dean@1086
  1492
        private void FormControlPrivacyStatusChild_PrivacyViewClick(object sender, System.Windows.RoutedEventArgs e)
Dean@1047
  1493
        {
Dean@1390
  1494
            if (this.isManagerFormEnabled)
Dean@1384
  1495
            {
Dean@1384
  1496
                this.RequestRatingAndUIUpdate();
Dean@1384
  1497
                this.BuildAndShowManager();
Dean@1384
  1498
            }
Dean@1384
  1499
Dean@1047
  1500
            return;
Dean@1047
  1501
        }
vb@133
  1502
    }
vb@133
  1503
}