UI/FormRegionPrivacyStatus.cs
author Dean Looyengoed
Fri, 12 Feb 2016 15:01:01 +0100
changeset 423 713d5baee692
parent 417 d174d33eae87
child 430 c81da89afa45
permissions -rw-r--r--
Change warning dialog for handshake with mistrusted key to Yes/No.
Dean@159
     1
´╗┐using pEpCOMServerAdapterLib;
Dean@159
     2
using System;
vb@133
     3
using System.Collections.Generic;
Dean@159
     4
using System.Diagnostics;
vb@133
     5
using System.Linq;
Dean@159
     6
using System.Runtime.InteropServices;
vb@133
     7
using System.Windows.Forms;
Dean@159
     8
using Color = System.Drawing.Color;
vb@133
     9
using Outlook = Microsoft.Office.Interop.Outlook;
vb@133
    10
vb@133
    11
namespace pEp
vb@133
    12
{
Dean@159
    13
    /// <summary>
Dean@312
    14
    /// Partial class for the privacy status form region that is displayed below every outlook message.
Dean@159
    15
    /// </summary>
Dean@312
    16
    partial class FormRegionPrivacyStatus
vb@133
    17
    {
vb@133
    18
        #region Form Region Factory
vb@133
    19
vb@133
    20
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
Dean@312
    21
        [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegionPrivacyStatus")]
Dean@312
    22
        public partial class FormRegionPrivacyStatusFactory
vb@133
    23
        {
vb@133
    24
            // Occurs before the form region is initialized.
vb@133
    25
            // To prevent the form region from appearing, set e.Cancel to true.
vb@133
    26
            // Use e.OutlookItem to get a reference to the current Outlook item.
Dean@312
    27
            private void FormRegionPrivacyStatus_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
vb@133
    28
            {
Dean@327
    29
                WindowFormRegionCollection formRegions;
Dean@327
    30
Dean@327
    31
                /* There is a Microsoft bug at least in Outlook 2013 and Windows 8.1
Dean@327
    32
                 * This bug causes multiple PrivacyStatus form regions to appear stacked on top of each other.
Dean@327
    33
                 * To trigger this bug, on an unencrypted server, click reply to compose an in-line response.
Dean@327
    34
                 * Then click to another tab such as People or Tasks. Then click back on the mail tab to view 
Dean@327
    35
                 * the original email again. Two form regions will be visible: 
Dean@327
    36
                 * (1) for the status during the in-line reply and 
Dean@327
    37
                 * (2) for the status of only the received message.
Dean@327
    38
                 * 
Dean@327
    39
                 * To fix this bug, any existing form regions are found and closed when initializing a new privacy status
Dean@327
    40
                 * form region.
Dean@327
    41
                 */
Dean@327
    42
Dean@327
    43
                try
Dean@327
    44
                {
Dean@327
    45
                    formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
Dean@327
    46
                    if (formRegions.FormRegionPrivacyStatus != null)
Dean@327
    47
                    {
Dean@327
    48
                        /* Note: there seems to be no way to actually close the form region.
Dean@327
    49
                         * Therefore, this is as close as possible to actually closing it.
Dean@327
    50
                         * The actual form regions will be cleaned up as soon as another email is selected.
Dean@327
    51
                         */
Dean@327
    52
                        formRegions.FormRegionPrivacyStatus.OutlookFormRegion.Visible = false;
Dean@327
    53
                    }
Dean@327
    54
                }
Dean@327
    55
                catch { }
Dean@327
    56
Dean@327
    57
                return;
vb@133
    58
            }
vb@133
    59
        }
vb@133
    60
vb@133
    61
        #endregion
vb@133
    62
Dean@159
    63
        /* Notes:
Dean@179
    64
         * 
Dean@159
    65
         * Use this.OutlookItem to get a reference to the current Outlook item.
Dean@159
    66
         * Use this.OutlookFormRegion to get a reference to the form region.
Dean@179
    67
         * 
Dean@179
    68
         * UI State Managment:
Dean@179
    69
         * 
Dean@179
    70
         * The UI state is almost entirely set from the associated mail item data.
Dean@179
    71
         * However, a separate state class is maintained to represent the UI as some separation is needed.
Dean@179
    72
         * This logical separation MUST be maintained throughout the code.
Dean@179
    73
         * Specific cases are noted where possible.
Dean@179
    74
         * 
Dean@356
    75
         * The separate privacy status manager form state is also managed here.
Dean@259
    76
         * 
Dean@159
    77
         */
Dean@159
    78
Dean@170
    79
        private CryptableMailItem associatedMailItem = null;
Dean@179
    80
        private bool              refreshOngoing     = false;
Dean@179
    81
        private bool              eventsAreConnected = false;
Dean@170
    82
Dean@313
    83
        private FormManagePrivacyStatus managerForm = null;
Dean@259
    84
Dean@250
    85
        // Data or controls not stored in the UI
Dean@179
    86
        private _pEp_color stateUIColorRating; // WARNING: Do NOT use this directly
vb@133
    87
Dean@159
    88
        /**************************************************************
Dean@159
    89
         * 
Dean@340
    90
         * Property Accessors
Dean@340
    91
         * 
Dean@340
    92
         *************************************************************/
Dean@340
    93
Dean@340
    94
        /// <summary>
Dean@340
    95
        /// Gets or sets whether to send the associated cryptable mail item as unencrypted.
Dean@342
    96
        /// This forwards the call to the cryptable mail item but also refreshes the UI.
Dean@340
    97
        /// </summary>
Dean@342
    98
        public bool? SendUnencrypted
Dean@340
    99
        {
Dean@340
   100
            get
Dean@340
   101
            {
Dean@340
   102
                if (this.associatedMailItem != null)
Dean@340
   103
                {
Dean@340
   104
                    return (this.associatedMailItem.SendUnencrypted);
Dean@340
   105
                }
Dean@340
   106
                else
Dean@340
   107
                {
Dean@342
   108
                    return (null);
Dean@340
   109
                }
Dean@340
   110
            }
Dean@340
   111
            set
Dean@340
   112
            {
Dean@340
   113
                if (this.associatedMailItem != null)
Dean@340
   114
                {
Dean@340
   115
                    this.associatedMailItem.SendUnencrypted = value;
Dean@340
   116
                    this.UpdateUIFromMailItem();
Dean@340
   117
                }
Dean@340
   118
            }
Dean@340
   119
        }
Dean@340
   120
Dean@340
   121
        /**************************************************************
Dean@340
   122
         * 
Dean@159
   123
         * Methods
Dean@159
   124
         * 
Dean@159
   125
         *************************************************************/
Dean@159
   126
Dean@171
   127
        /// <summary>
Dean@170
   128
        /// Sets the cryptable mail item associated with this encryption status panel.
Dean@170
   129
        /// The associated mail item can then be accessed through its local variable.
Dean@170
   130
        /// </summary>
Dean@170
   131
        private void SetAssociatedMailItem()
Dean@170
   132
        {
Dean@170
   133
            bool errorOccurred = false;
Dean@170
   134
            Outlook.MailItem omi = null;
Dean@170
   135
Dean@170
   136
            // Null check
Dean@170
   137
            if (!errorOccurred)
Dean@170
   138
            {
Dean@170
   139
                try
Dean@170
   140
                {
Dean@170
   141
                    if (this.OutlookItem == null)
Dean@170
   142
                    {
Dean@170
   143
                        errorOccurred = true;
Dean@170
   144
                    }
Dean@170
   145
                }
Dean@170
   146
                catch (COMException)
Dean@170
   147
                {
Dean@170
   148
                    errorOccurred = true;
Dean@170
   149
                }
Dean@170
   150
            }
Dean@170
   151
Dean@170
   152
            // Attempt to get and cast the outlook mail item
Dean@170
   153
            if (!errorOccurred)
Dean@170
   154
            {
Dean@170
   155
                try
Dean@170
   156
                {
Dean@170
   157
                    omi = (Outlook.MailItem)this.OutlookItem;
Dean@170
   158
                }
Dean@170
   159
                catch
Dean@170
   160
                {
Dean@170
   161
                    errorOccurred = true;
Dean@170
   162
                }
Dean@170
   163
            }
Dean@170
   164
Dean@170
   165
            // Finally set the associated mail item
Dean@170
   166
            if ((errorOccurred) ||
Dean@170
   167
                (omi == null))
Dean@170
   168
            {
Dean@170
   169
                this.associatedMailItem = null;
Dean@170
   170
            }
Dean@170
   171
            else
Dean@170
   172
            {
Dean@170
   173
                // Check if the associated mail item has already been set
Dean@170
   174
                if (this.associatedMailItem != null)
Dean@170
   175
                {
Dean@170
   176
                    // Only re-set the mail item if the EntryID has changed
Dean@170
   177
                    if (this.associatedMailItem.EntryID != omi.EntryID)
Dean@170
   178
                    {
Dean@170
   179
                        this.associatedMailItem = new CryptableMailItem(omi);
Dean@170
   180
                    }
Dean@170
   181
                }
Dean@170
   182
                else
Dean@170
   183
                {
Dean@170
   184
                    this.associatedMailItem = new CryptableMailItem(omi);
Dean@170
   185
                }
Dean@170
   186
            }
Dean@170
   187
Dean@170
   188
            return;
Dean@170
   189
        }
Dean@170
   190
Dean@179
   191
        /// <summary>
Dean@290
   192
        /// Completes the handshake process when the identity of a partner was previously marked as compromised.
Dean@281
   193
        /// </summary>
Dean@281
   194
        /// <param name="identityPartner">The identity of the partner to complete the handshake with.</param>
Dean@384
   195
        private void DoHandshakeForCompromisedKey(PEPIdentity identityPartner)
Dean@281
   196
        {
Dean@281
   197
            DialogResult result;
Dean@281
   198
Dean@281
   199
            result = MessageBox.Show(this.ParentForm,
Dean@378
   200
                                     pEp.Properties.Resources.Message_WarningCompromisedKey,
Dean@378
   201
                                     pEp.Properties.Resources.Message_TitleConfirmOperation,
Dean@423
   202
                                     MessageBoxButtons.YesNo);
Dean@281
   203
Dean@423
   204
            if (result == DialogResult.Yes)
Dean@281
   205
            {
Dean@281
   206
                this.DoHandshake(identityPartner);
Dean@281
   207
            }
Dean@281
   208
Dean@281
   209
            return;
Dean@281
   210
        }
Dean@281
   211
Dean@281
   212
        /// <summary>
Dean@281
   213
        /// Completes the handshake process where the identity of a partner is confirmed.
Dean@179
   214
        /// </summary>
Dean@179
   215
        /// <param name="identityPartner">The identity of the partner to complete the handshake with.</param>
Dean@384
   216
        private void DoHandshake(PEPIdentity identityPartner)
vb@133
   217
        {
Dean@155
   218
            string ownFpr;
Dean@155
   219
            string partnerFpr;
Dean@363
   220
            string trustwordsShort;
Dean@363
   221
            string trustwordsFull;
Dean@155
   222
            string[] lines;
Dean@179
   223
            DialogResult result;
Dean@384
   224
            PEPIdentity me;
Dean@155
   225
            FormHandshake handshakeDialog;
Dean@155
   226
            FormHandshake.State state = new FormHandshake.State();
markus@201
   227
Dean@190
   228
            Globals.ThisAddIn.LogVerbose("doHandshake");
Dean@384
   229
            Globals.ThisAddIn.LogVerbose(identityPartner.Address);
vb@139
   230
Dean@363
   231
            // Process identities
Dean@384
   232
            if (identityPartner.Username == "")
Dean@363
   233
            {
Dean@384
   234
                identityPartner.Username = identityPartner.Address;
vb@133
   235
            }
vb@133
   236
Dean@415
   237
            identityPartner.UserID = Globals.ThisAddIn.GetUserID(identityPartner.Address, identityPartner.Username);
vb@139
   238
Dean@384
   239
            me = PEPIdentity.GetMyIdentity(this.associatedMailItem);
Dean@363
   240
Dean@363
   241
            // Calculate and add trustwords
Dean@363
   242
            this.CalcTrustwords(me, identityPartner, out trustwordsShort, out trustwordsFull);
Dean@363
   243
            state.TrustwordsShort = trustwordsShort;
Dean@363
   244
            state.TrustwordsFull = trustwordsFull;
Dean@363
   245
Dean@363
   246
            // Calculate and add fingerprint
Dean@384
   247
            ownFpr = this.ToQuadruple(me.Fingerprint);
Dean@384
   248
            partnerFpr = this.ToQuadruple(identityPartner.Fingerprint);
vb@133
   249
Dean@384
   250
            if (me.Fingerprint.CompareTo(identityPartner.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@356
   270
            this.ProcessDoHandshakeResult(result, identityPartner);
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@290
   289
                        // Check if key was previously compromised -- warning for this must be displayed earlier
Dean@281
   290
                        if (identityPartner.comm_type == _pEp_comm_type.pEp_ct_compromized)
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@293
   296
                        this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_trusted;
Dean@279
   297
Dean@279
   298
                        this.UpdateUIFromMailItem();
Dean@279
   299
Dean@279
   300
                        // Update the manager form state
Dean@279
   301
                        if (this.managerForm != null)
Dean@279
   302
                        {
Dean@279
   303
                            this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   304
                        }
Dean@279
   305
Dean@279
   306
                        break;
Dean@279
   307
                    }
vb@133
   308
                case DialogResult.No:
Dean@279
   309
                    {
Dean@279
   310
                        ThisAddIn.pEp.key_compromized(ref identityPartner);
Dean@279
   311
                        identityPartner.comm_type = _pEp_comm_type.pEp_ct_compromized;
Dean@279
   312
                        identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
Dean@293
   313
                        this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_red;
Dean@279
   314
Dean@279
   315
                        this.UpdateUIFromMailItem();
Dean@279
   316
Dean@279
   317
                        // Update the manager form state
Dean@279
   318
                        if (this.managerForm != null)
Dean@279
   319
                        {
Dean@279
   320
                            this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   321
                        }
Dean@279
   322
Dean@279
   323
                        break;
Dean@279
   324
                    }
vb@133
   325
            }
Dean@279
   326
Dean@279
   327
            return;
vb@133
   328
        }
vb@133
   329
Dean@179
   330
        /// <summary>
Dean@179
   331
        /// Reverses any past handshake confirmation by unconfirming the given identity partner.
Dean@179
   332
        /// </summary>
Dean@384
   333
        /// <param name="partner">The identity of the partner to unconfirm.</param>
Dean@384
   334
        private void UndoHandshake(PEPIdentity partner)
vb@133
   335
        {
Dean@384
   336
            pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(partner.ToCOMType());
Dean@280
   337
            ThisAddIn.pEp.key_reset_trust(ref identityPartner);
Dean@279
   338
Dean@179
   339
            this.UpdateUIFromMailItem();
Dean@279
   340
Dean@279
   341
            // Update the manager form state
Dean@279
   342
            if (this.managerForm != null)
Dean@279
   343
            {
Dean@279
   344
                this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   345
            }
Dean@279
   346
Dean@279
   347
            return;
vb@133
   348
        }
vb@133
   349
Dean@161
   350
        /// <summary>
Dean@363
   351
        /// Calculates both the short and full trustwords between the given personal identity and the identity partner.
Dean@363
   352
        /// </summary>
Dean@363
   353
        /// <param name="myIdentity">The personal identity.</param>
Dean@363
   354
        /// <param name="partnerIdentity">The identity of the partner.</param>
Dean@363
   355
        /// <param name="trustwordsShort">The short version of trustwords.</param>
Dean@363
   356
        /// <param name="trustwordsFull">The full/long version of trustwords.</param>
Dean@384
   357
        private void CalcTrustwords(PEPIdentity myIdentity,
Dean@384
   358
                                    PEPIdentity partnerIdentity,
Dean@363
   359
                                    out string trustwordsShort,
Dean@363
   360
                                    out string trustwordsFull)
Dean@363
   361
        {
Dean@363
   362
            string myShort;
Dean@363
   363
            string myLong;
Dean@363
   364
            string partnerShort;
Dean@363
   365
            string partnerLong;
Dean@363
   366
Dean@384
   367
            myShort = ThisAddIn.pEp.trustwords(myIdentity.Fingerprint, max_words: 5).ToLower();
Dean@384
   368
            myLong = ThisAddIn.pEp.trustwords(myIdentity.Fingerprint).ToLower();
Dean@384
   369
            partnerShort = ThisAddIn.pEp.trustwords(partnerIdentity.Fingerprint, max_words: 5).ToLower();
Dean@384
   370
            partnerLong = ThisAddIn.pEp.trustwords(partnerIdentity.Fingerprint).ToLower();
Dean@363
   371
Dean@384
   372
            if (myIdentity.Fingerprint.CompareTo(partnerIdentity.Fingerprint) > 0)
Dean@363
   373
            {
Dean@363
   374
                trustwordsShort = partnerShort + myShort;
Dean@363
   375
                trustwordsFull = partnerLong + myLong;
Dean@363
   376
            }
Dean@363
   377
            else
Dean@363
   378
            {
Dean@363
   379
                trustwordsShort = myShort + " " + partnerShort;
Dean@363
   380
                trustwordsFull = myLong + " " + partnerLong;
Dean@363
   381
            }
Dean@363
   382
Dean@363
   383
            return;
Dean@363
   384
        }
Dean@363
   385
Dean@363
   386
        /// <summary>
Dean@161
   387
        /// Formats the given text string as separated 4-character groups.
Dean@161
   388
        /// Example: 49422235FC99585B891C --> 4942 2235 FC99 585B 891C
Dean@161
   389
        /// </summary>
Dean@161
   390
        /// <param name="text">The text to format in 4-character groups.</param>
Dean@161
   391
        /// <returns>The re-formatted string.</returns>
Dean@161
   392
        private string ToQuadruple(string text)
Dean@159
   393
        {
Dean@159
   394
            List<string> result = new List<string>();
Dean@159
   395
Dean@161
   396
            if (text != null)
Dean@159
   397
            {
Dean@161
   398
                for (int i = 0; i < text.Length; i += 4)
Dean@159
   399
                {
Dean@161
   400
                    try
Dean@161
   401
                    {
Dean@161
   402
                        result.Add(text.Substring(i, 4));
Dean@161
   403
                    }
Dean@161
   404
                    catch (ArgumentOutOfRangeException)
Dean@161
   405
                    {
Dean@161
   406
                        result.Add(text.Substring(i));
Dean@161
   407
                        break;
Dean@161
   408
                    }
Dean@159
   409
                }
Dean@159
   410
            }
Dean@159
   411
Dean@159
   412
            return String.Join(" ", result);
Dean@159
   413
        }
markus@201
   414
Dean@196
   415
        /// <summary>
Dean@196
   416
        /// Makes the unencrypted preview form.
Dean@196
   417
        /// This gets the active form region then fills it's content.
Dean@196
   418
        /// </summary>
Dean@198
   419
        /// <returns>True if successful, otherwise false.</returns>
Dean@198
   420
        private bool MakePreview()
Dean@159
   421
        {
Dean@198
   422
            bool success = true;
Dean@198
   423
            bool isSuccessful;
Dean@198
   424
            byte[] rtfBody;
Dean@198
   425
            string subject;
Dean@159
   426
            WindowFormRegionCollection formRegions;
markus@201
   427
Dean@198
   428
            formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
Dean@159
   429
Dean@198
   430
            if ((formRegions != null) &&
Dean@198
   431
                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@198
   432
                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@198
   433
            {
Dean@198
   434
                // Attempt to the the RTF body
Dean@198
   435
                isSuccessful = this.associatedMailItem.MirrorTryGetRTFBody(out rtfBody);
Dean@198
   436
                if (isSuccessful == false)
Dean@198
   437
                {
Dean@198
   438
                    return (false);
Dean@198
   439
                }
Dean@159
   440
Dean@198
   441
                // Attempt to get the subject
Dean@198
   442
                isSuccessful = this.associatedMailItem.MirrorTryGetSubject(out subject);
Dean@198
   443
                if (isSuccessful == false)
Dean@159
   444
                {
Dean@198
   445
                    return (false);
Dean@159
   446
                }
Dean@196
   447
Dean@198
   448
                // Add data to the form
markus@201
   449
                formRegions.FormRegionPreviewUnencrypted.RichTextBoxPreview.Rtf = System.Text.Encoding.ASCII.GetString(rtfBody, 0, rtfBody.Length);
markus@201
   450
                formRegions.FormRegionPreviewUnencrypted.TextBoxSubject.Text = subject;
Dean@412
   451
                formRegions.FormRegionPreviewUnencrypted.TextBoxFrom.Text = this.associatedMailItem.From.Username + " <" + this.associatedMailItem.From.Address + ">";
Dean@198
   452
            }
Dean@198
   453
            else
Dean@198
   454
            {
Dean@198
   455
                success = false;
Dean@198
   456
            }
Dean@198
   457
Dean@198
   458
            return (success);
Dean@159
   459
        }
markus@201
   460
Dean@179
   461
        /// <summary>
Dean@279
   462
        /// Builds a new manager form state using this encryption state/mail item current state.
Dean@279
   463
        /// </summary>
Dean@279
   464
        /// <returns>A new manager form state.</returns>
Dean@313
   465
        private FormManagePrivacyStatus.State GetManagerState()
Dean@279
   466
        {
Dean@364
   467
            string trustwordsShort;
Dean@364
   468
            string trustwordsFull;
Dean@384
   469
            List<PEPIdentity> identities;
Dean@384
   470
            PEPIdentity myIdentity;
Dean@384
   471
            PEPIdentity incomingIdent;
Dean@259
   472
            _pEp_color identityPartnerColor;
Dean@259
   473
            SelectionItem item;
Dean@312
   474
            PrivacyState state = this.CopyUIToState();
Dean@313
   475
            FormManagePrivacyStatus.State managerState = new FormManagePrivacyStatus.State();
Dean@259
   476
Dean@332
   477
            // Resolve all recipients -- this ensures the identities list is correctly populated
Dean@332
   478
            this.associatedMailItem.ResolveAllRecipients();
Dean@332
   479
Dean@279
   480
            managerState.VisualState = state.Copy();
Dean@279
   481
            managerState.IsIncoming = this.associatedMailItem.IsIncoming;
Dean@259
   482
Dean@259
   483
            if (this.associatedMailItem.IsIncoming)
Dean@259
   484
            {
Dean@384
   485
                identities = new List<PEPIdentity>();
Dean@259
   486
Dean@259
   487
                // Add only one identity
Dean@384
   488
                incomingIdent = new PEPIdentity();
Dean@412
   489
                incomingIdent.Address = this.associatedMailItem.From.Address;
Dean@412
   490
                incomingIdent.Username = this.associatedMailItem.From.Username;
Dean@415
   491
                incomingIdent.UserID = Globals.ThisAddIn.GetUserID(incomingIdent.Address, incomingIdent.Username);
Dean@259
   492
Dean@384
   493
                identities.Add(incomingIdent);
Dean@259
   494
            }
Dean@259
   495
            else
Dean@259
   496
            {
Dean@395
   497
                identities = this.associatedMailItem.Recipients;
Dean@259
   498
            }
Dean@259
   499
Dean@366
   500
            // Get my identity
Dean@384
   501
            myIdentity = PEPIdentity.GetMyIdentity(this.associatedMailItem);
Dean@366
   502
Dean@279
   503
            // Add identities
Dean@394
   504
            identities = PEPIdentity.ToFlatList(identities);
Dean@279
   505
            managerState.Identities.Clear();
Dean@384
   506
            foreach (PEPIdentity ident in identities)
Dean@259
   507
            {
Dean@384
   508
                PEPIdentity partnerIdentity = new PEPIdentity(ThisAddIn.pEp.update_identity(ident.ToCOMType()));
Dean@384
   509
                identityPartnerColor = ThisAddIn.pEp.identity_color(partnerIdentity.ToCOMType());
Dean@259
   510
Dean@259
   511
                item = new SelectionItem();
Dean@384
   512
                item.TextLine1 = partnerIdentity.Username;
Dean@384
   513
                item.TextLine2 = partnerIdentity.Address;
Dean@259
   514
Dean@364
   515
                // Calculate trustwords
Dean@366
   516
                this.CalcTrustwords(myIdentity,
Dean@366
   517
                                    partnerIdentity,
Dean@364
   518
                                    out trustwordsShort,
Dean@364
   519
                                    out trustwordsFull);
Dean@364
   520
Dean@332
   521
                // Don't show both the user name and address if they are the same
Dean@332
   522
                if ((item.TextLine1 != null) &&
Dean@332
   523
                    (item.TextLine2 != null) &&
Dean@332
   524
                    (item.TextLine1 == item.TextLine2))
Dean@332
   525
                {
Dean@332
   526
                    item.IsTwoTextLinesVisible = false;
Dean@332
   527
                }
Dean@332
   528
Dean@259
   529
                // Set image
Dean@359
   530
                switch (PrivacyState.ConvertRatingToPrivacyColor(identityPartnerColor))
Dean@259
   531
                {
Dean@359
   532
                    case PrivacyState.PrivacyColor.Green:
Dean@259
   533
                        item.ItemImage = pEp.Properties.Resources.green;
Dean@259
   534
                        break;
Dean@359
   535
                    case PrivacyState.PrivacyColor.Yellow:
Dean@259
   536
                        item.ItemImage = pEp.Properties.Resources.yellow;
Dean@259
   537
                        break;
Dean@359
   538
                    case PrivacyState.PrivacyColor.Red:
Dean@259
   539
                        item.ItemImage = pEp.Properties.Resources.red;
Dean@259
   540
                        break;
Dean@359
   541
                    case PrivacyState.PrivacyColor.Grey:
Dean@359
   542
                        item.ItemImage = null;
Dean@359
   543
                        break;
Dean@259
   544
                    default:
Dean@259
   545
                        item.ItemImage = null;
Dean@259
   546
                        break;
Dean@259
   547
                }
Dean@259
   548
Dean@259
   549
                // Set button
Dean@384
   550
                if (myIdentity.Equals(partnerIdentity) == false)
Dean@259
   551
                {
Dean@384
   552
                    if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@366
   553
                    {
Dean@366
   554
                        // Undo handshake
Dean@366
   555
                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_StopTrusting;
Dean@366
   556
                        item.IsButtonVisible = true;
Dean@366
   557
                        item.ButtonOnClick = (x, y) => { this.UndoHandshake(partnerIdentity); };
Dean@356
   558
Dean@366
   559
                        item.IsExpandable = false;
Dean@366
   560
                    }
Dean@384
   561
                    else if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_unconfirmed_encryption &&
Dean@384
   562
                             partnerIdentity.CommunicationType < _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@366
   563
                    {
Dean@366
   564
                        // Do handshake
Dean@366
   565
                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_HandshakeAdvanced;
Dean@366
   566
                        item.IsButtonVisible = true;
Dean@366
   567
                        item.ButtonOnClick = (x, y) => { this.DoHandshake(partnerIdentity); };
Dean@356
   568
Dean@366
   569
                        item.IsExpandable = true;
Dean@366
   570
                        item.ExpandedText = Properties.Resources.PrivacyStatus_TrustwordDesc + "\n\n" +
Dean@366
   571
                                            trustwordsShort;
Dean@366
   572
                        item.ExpandedButton1Text = Properties.Resources.PrivacyStatus_SelectTrust;
Dean@366
   573
                        item.ExpandedButton2Text = Properties.Resources.PrivacyStatus_SelectMistrust;
Dean@366
   574
                        item.ExpandedButton1OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.Yes, partnerIdentity); };
Dean@366
   575
                        item.ExpandedButton2OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.No, partnerIdentity); };
Dean@366
   576
                    }
Dean@384
   577
                    else if (partnerIdentity.CommunicationType == _pEp_comm_type.pEp_ct_compromized)
Dean@366
   578
                    {
Dean@366
   579
                        // Redo handshake with confirmation
Dean@417
   580
                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_Handshake;
Dean@366
   581
                        item.IsButtonVisible = true;
Dean@417
   582
                        item.ButtonOnClick = (x, y) => { this.DoHandshakeForCompromisedKey(partnerIdentity); };
Dean@366
   583
                    }
Dean@366
   584
                    else
Dean@366
   585
                    {
Dean@366
   586
                        item.IsButtonVisible = false;
Dean@366
   587
                    }
Dean@281
   588
                }
Dean@259
   589
                else
Dean@259
   590
                {
Dean@259
   591
                    item.IsButtonVisible = false;
Dean@259
   592
                }
Dean@259
   593
Dean@279
   594
                managerState.Identities.Add(item);
Dean@259
   595
            }
Dean@259
   596
Dean@375
   597
            // Attempt to select the first identity requiring a handshake
Dean@375
   598
            for (int i = 0; i < managerState.Identities.Count; i++)
Dean@375
   599
            {
Dean@375
   600
                // Determine if it's a handshake identity by simply seeing if it's expandable
Dean@375
   601
                if (managerState.Identities[i].IsExpandable)
Dean@375
   602
                {
Dean@375
   603
                    managerState.SelectedIdentityIndex = i;
Dean@375
   604
                    break;
Dean@375
   605
                }
Dean@375
   606
            }
Dean@375
   607
Dean@279
   608
            return (managerState);
Dean@279
   609
        }
Dean@279
   610
Dean@279
   611
        /// <summary>
Dean@279
   612
        /// Builds the latest state of the encryption status manager then shows the UI.
Dean@279
   613
        /// </summary>
Dean@279
   614
        private void BuildAndShowManager()
Dean@279
   615
        {
Dean@279
   616
            DialogResult result;
Dean@313
   617
            FormManagePrivacyStatus form;
Dean@313
   618
            FormManagePrivacyStatus.State stateOut;
Dean@279
   619
Dean@259
   620
            // Show the form
Dean@313
   621
            form = new FormManagePrivacyStatus();
Dean@259
   622
            form.StartPosition = FormStartPosition.CenterScreen;
Dean@259
   623
Dean@259
   624
            this.managerForm = form;
Dean@279
   625
            result = form.ShowDialog(this.ParentForm, this.GetManagerState(), out stateOut); // Must show as dialog to block code
Dean@259
   626
Dean@259
   627
            return;
Dean@259
   628
        }
Dean@259
   629
Dean@259
   630
        /// <summary>
Dean@179
   631
        /// Updates the status of the UI state based on the associated mail item.
Dean@179
   632
        /// Any previous state changes in the UI are preserved.
Dean@179
   633
        /// </summary>
Dean@179
   634
        private void UpdateUIFromMailItem()
Dean@159
   635
        {
Dean@312
   636
            PrivacyState state;
Dean@342
   637
            bool? sendUnencryptedProperty;
Dean@159
   638
Dean@173
   639
            if (this.associatedMailItem != null)
Dean@159
   640
            {
Dean@342
   641
                sendUnencryptedProperty = this.associatedMailItem.SendUnencrypted;
Dean@342
   642
Dean@342
   643
                if ((sendUnencryptedProperty != null) &&
Dean@342
   644
                    ((bool)sendUnencryptedProperty == true))
Dean@159
   645
                {
Dean@342
   646
                    // Force unencrypted
Dean@312
   647
                    state = new PrivacyState(_pEp_color.pEp_rating_unencrypted);
Dean@159
   648
                }
Dean@159
   649
                else
Dean@159
   650
                {
Dean@312
   651
                    state = new PrivacyState(this.associatedMailItem.ColorRating);
Dean@159
   652
                }
Dean@179
   653
Dean@179
   654
                this.CopyStateToUI(state);
Dean@159
   655
            }
Dean@159
   656
Dean@179
   657
            return;
Dean@159
   658
        }
Dean@159
   659
Dean@172
   660
        #region StateMethods
Dean@172
   661
Dean@172
   662
        /// <summary>
Dean@172
   663
        /// Connects or disconnects all control events from the UI.
Dean@172
   664
        /// </summary>
Dean@172
   665
        /// <param name="connect">True to connect events, false to disconnect.</param>
Dean@172
   666
        private void ConnectEvents(bool connect)
Dean@172
   667
        {
Dean@172
   668
            // Connect events only if not already connected
markus@201
   669
            if ((connect == true) &&
Dean@172
   670
                (this.eventsAreConnected == false))
Dean@172
   671
            {
Dean@312
   672
                this.ButtonPrivacyStatus.Click += this.ButtonPrivacyStatus_Click;
Dean@312
   673
                this.ButtonPrivacyStatus.MouseUp += this.ButtonPrivacyStatus_MouseUp;
Dean@179
   674
Dean@172
   675
                this.eventsAreConnected = true;
Dean@172
   676
            }
Dean@172
   677
            // Always attempt to disconnect
Dean@172
   678
            else if (connect == false)
Dean@172
   679
            {
Dean@312
   680
                this.ButtonPrivacyStatus.Click -= this.ButtonPrivacyStatus_Click;
Dean@312
   681
                this.ButtonPrivacyStatus.MouseUp -= this.ButtonPrivacyStatus_MouseUp;
Dean@179
   682
Dean@172
   683
                this.eventsAreConnected = false;
Dean@172
   684
            }
Dean@172
   685
Dean@172
   686
            return;
Dean@172
   687
        }
Dean@172
   688
Dean@172
   689
        /// <summary>
Dean@172
   690
        /// Refreshes the UI by reloading the state.
Dean@172
   691
        /// </summary>
Dean@172
   692
        private void RefreshUI()
Dean@172
   693
        {
Dean@172
   694
            this.CopyStateToUI(this.CopyUIToState());
Dean@172
   695
            return;
Dean@172
   696
        }
Dean@172
   697
Dean@172
   698
        /// <summary>
Dean@172
   699
        /// Copies the given state to the UI.
Dean@172
   700
        /// Events are turned off until the process is complete.
Dean@172
   701
        /// </summary>
Dean@172
   702
        /// <param name="state">The state to set to the UI.</param>
Dean@312
   703
        private void CopyStateToUI(PrivacyState state)
Dean@172
   704
        {
Dean@172
   705
            this.ConnectEvents(false);
markus@201
   706
Dean@172
   707
            ///////////////////////////////////////////////////////////
Dean@172
   708
            // Set UI data
Dean@172
   709
            ///////////////////////////////////////////////////////////
Dean@172
   710
Dean@179
   711
            // Save UI state maintained outside of controls
Dean@312
   712
            this.stateUIColorRating = state.PrivacyStatus;
Dean@179
   713
Dean@312
   714
            this.ButtonPrivacyStatus.BackColor = state.BackgroundColor;
Dean@312
   715
            this.ButtonPrivacyStatus.ForeColor = state.ForegroundColor;
Dean@312
   716
            this.ButtonPrivacyStatus.Text = state.ShortText;
Dean@172
   717
Dean@172
   718
            this.ConnectEvents(true);
Dean@172
   719
Dean@172
   720
            return;
Dean@172
   721
        }
Dean@172
   722
Dean@172
   723
        /// <summary>
Dean@172
   724
        /// Copies the UI to a new state.
Dean@172
   725
        /// </summary>
Dean@172
   726
        /// <returns>The state of the UI.</returns>
Dean@312
   727
        private PrivacyState CopyUIToState()
Dean@172
   728
        {
Dean@312
   729
            return (new PrivacyState(this.stateUIColorRating));
Dean@172
   730
        }
Dean@172
   731
Dean@172
   732
        #endregion
Dean@172
   733
Dean@159
   734
        /**************************************************************
Dean@159
   735
         * 
Dean@159
   736
         * Event Handling
Dean@159
   737
         * 
Dean@159
   738
         *************************************************************/
Dean@159
   739
Dean@159
   740
        /// <summary>
Dean@159
   741
        /// Event handler that is called before the form region is displayed.
Dean@159
   742
        /// </summary>
Dean@312
   743
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   744
        {
Dean@173
   745
            this.SetAssociatedMailItem();
Dean@173
   746
Dean@199
   747
            // Connect cryptable mail item events
Dean@189
   748
            if (this.associatedMailItem != null)
Dean@159
   749
            {
Dean@159
   750
                try
Dean@159
   751
                {
Dean@189
   752
                    this.associatedMailItem.PropertyChange += MailItem_PropertyChange;
Dean@179
   753
Dean@189
   754
                    if (this.associatedMailItem.IsInEncryptedStore &&
Dean@198
   755
                        this.associatedMailItem.IsPGPEncrypted)
Dean@159
   756
                    {
Dean@189
   757
                        this.associatedMailItem.Open += MailItem_Open;
Dean@159
   758
                    }
Dean@159
   759
                }
Dean@159
   760
                catch { }
Dean@179
   761
            }
Dean@159
   762
Dean@179
   763
            // Set the default UI state
Dean@312
   764
            this.CopyStateToUI(new PrivacyState());
Dean@179
   765
Dean@199
   766
            // Call the timer tick method manually to refresh data
Dean@199
   767
            this.TimerRefresh_Tick(null, new EventArgs());
Dean@179
   768
Dean@179
   769
            return;
Dean@159
   770
        }
Dean@159
   771
Dean@159
   772
        /// <summary>
Dean@159
   773
        /// Event handler for when the form region is closed.
Dean@159
   774
        /// </summary>
Dean@312
   775
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   776
        {
Dean@199
   777
            // Disconnect cryptable mail item events
Dean@190
   778
            if (this.associatedMailItem != null)
Dean@179
   779
            {
Dean@179
   780
                try
Dean@179
   781
                {
Dean@190
   782
                    this.associatedMailItem.PropertyChange -= MailItem_PropertyChange;
markus@201
   783
                    this.associatedMailItem.Open -= MailItem_Open;
Dean@179
   784
                }
Dean@179
   785
                catch { }
Dean@179
   786
            }
Dean@179
   787
Dean@179
   788
            return;
Dean@179
   789
        }
Dean@179
   790
Dean@179
   791
        /// <summary>
Dean@179
   792
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   793
        /// </summary>
Dean@179
   794
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   795
        {
Dean@199
   796
            bool tryAgain = false;
Dean@199
   797
            bool isSuccessful;
Dean@179
   798
            this.TimerRefresh.Enabled = false; // Only once
markus@201
   799
Dean@179
   800
            // Ensure the tick method is not called more than once
Dean@179
   801
            if (refreshOngoing == false)
Dean@179
   802
            {
Dean@179
   803
                this.refreshOngoing = true;
Dean@179
   804
Dean@179
   805
                if (this.associatedMailItem != null)
Dean@179
   806
                {
Dean@179
   807
                    if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@179
   808
                    {
Dean@179
   809
                        this.UpdateUIFromMailItem();
Dean@199
   810
Dean@199
   811
                        /* Create the unencrypted preview if the mail item is encrypted and
Dean@199
   812
                         * it is in an encrypted (untrusted) store
Dean@199
   813
                         * 
Dean@312
   814
                         * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Dean@199
   815
                         * it also is initialized after FormRegionPreviewUnencrypted.
Dean@199
   816
                         */
Dean@199
   817
                        if (this.associatedMailItem.IsInEncryptedStore &&
Dean@199
   818
                            this.associatedMailItem.IsPGPEncrypted)
Dean@199
   819
                        {
Dean@199
   820
                            isSuccessful = this.MakePreview();
Dean@199
   821
Dean@199
   822
                            if (isSuccessful == false)
Dean@199
   823
                            {
Dean@199
   824
                                tryAgain = true;
Dean@199
   825
                            }
Dean@199
   826
                        }
Dean@179
   827
                    }
Dean@179
   828
                    else
Dean@179
   829
                    {
Dean@179
   830
                        this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@199
   831
                        tryAgain = true;
Dean@179
   832
                    }
Dean@179
   833
                }
Dean@179
   834
Dean@199
   835
                // Set the timer to refresh again later automatically
Dean@199
   836
                if (tryAgain)
Dean@199
   837
                {
Dean@199
   838
                    this.TimerRefresh.Interval = 100;
Dean@199
   839
                    this.TimerRefresh.Enabled = true;
Dean@199
   840
                }
Dean@199
   841
Dean@179
   842
                this.refreshOngoing = false;
Dean@179
   843
            }
Dean@175
   844
Dean@159
   845
            return;
Dean@159
   846
        }
Dean@159
   847
Dean@169
   848
        /// <summary>
Dean@169
   849
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   850
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
   851
        /// </summary>
Dean@169
   852
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   853
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   854
        /// and the inspector is not displayed.</param>
Dean@169
   855
        private void MailItem_Open(ref bool cancel)
Dean@159
   856
        {
Dean@173
   857
            if (this.associatedMailItem != null && this.associatedMailItem.MirrorDisplay())
Dean@169
   858
            {
Dean@169
   859
                cancel = true;
Dean@169
   860
            }
Dean@169
   861
Dean@169
   862
            return;
Dean@159
   863
        }
Dean@159
   864
Dean@169
   865
        /// <summary>
Dean@169
   866
        /// Event handler for when a mail item property is changed.
Dean@169
   867
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
   868
        /// </summary>
Dean@169
   869
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@169
   870
        private void MailItem_PropertyChange(string propertyName)
Dean@159
   871
        {
Dean@179
   872
            switch (propertyName.ToUpper())
Dean@159
   873
            {
Dean@179
   874
                case "TO":
Dean@179
   875
                    // Start the refresh timer
Dean@179
   876
                    this.TimerRefresh.Enabled = true;
Dean@159
   877
                    break;
Dean@159
   878
                // Outlook bug: there are always both events, so one is enough
Dean@159
   879
                //case "CC":
Dean@179
   880
                //    // Start the refresh timer
Dean@179
   881
                //    this.TimerRefresh.Enabled = true;
Dean@159
   882
                //    break;
Dean@159
   883
            }
Dean@169
   884
Dean@169
   885
            return;
Dean@159
   886
        }
Dean@159
   887
Dean@179
   888
        /// <summary>
Dean@312
   889
        /// Event handler for when the privacy status button is clicked.
Dean@179
   890
        /// </summary>
Dean@312
   891
        private void ButtonPrivacyStatus_Click(object sender, EventArgs e)
vb@133
   892
        {
Dean@310
   893
            this.BuildAndShowManager();
Dean@179
   894
            return;
vb@133
   895
        }
vb@133
   896
Dean@179
   897
        /// <summary>
Dean@312
   898
        /// Event handler for when a mouse button is released over the privacy status button.
Dean@179
   899
        /// </summary>
Dean@312
   900
        private void ButtonPrivacyStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   901
        {
Dean@179
   902
            return;
vb@133
   903
        }
vb@133
   904
    }
vb@133
   905
}