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