UI/FormRegionPrivacyStatus.cs
author Dean Looyengoed
Fri, 05 Feb 2016 22:49:58 +0100
changeset 384 a0aa034cb7f3
parent 378 e20e56a3d3a9
child 394 a04b30dd83ab
permissions -rw-r--r--
Create and implement new classes for PEPAttachment, PEPIdentity and PEPMessage.
These classes are based on the pEp engine structures but contain many other related methods.
This is a reorganization/re-write in preparation for the MsgProcessor.
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@281
   202
                                     MessageBoxButtons.OKCancel);
Dean@281
   203
Dean@281
   204
            if (result == DialogResult.OK)
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@384
   237
            identityPartner.UserID = Globals.ThisAddIn.GetUserIDFromAddress(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;
markus@201
   451
                formRegions.FormRegionPreviewUnencrypted.TextBoxFrom.Text = this.associatedMailItem.FromUsername + " <" + this.associatedMailItem.FromAddress + ">";
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@384
   489
                incomingIdent.Address = this.associatedMailItem.FromAddress;
Dean@384
   490
                incomingIdent.Username = this.associatedMailItem.FromUsername;
Dean@384
   491
                incomingIdent.UserID = Globals.ThisAddIn.GetUserIDFromAddress(incomingIdent.Address, incomingIdent.Username);
Dean@259
   492
Dean@384
   493
                identities.Add(incomingIdent);
Dean@259
   494
            }
Dean@259
   495
            else
Dean@259
   496
            {
Dean@259
   497
                identities = this.associatedMailItem.Addresses;
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@279
   504
            managerState.Identities.Clear();
Dean@384
   505
            foreach (PEPIdentity ident in identities)
Dean@259
   506
            {
Dean@384
   507
                PEPIdentity partnerIdentity = new PEPIdentity(ThisAddIn.pEp.update_identity(ident.ToCOMType()));
Dean@384
   508
                identityPartnerColor = ThisAddIn.pEp.identity_color(partnerIdentity.ToCOMType());
Dean@259
   509
Dean@259
   510
                item = new SelectionItem();
Dean@384
   511
                item.TextLine1 = partnerIdentity.Username;
Dean@384
   512
                item.TextLine2 = partnerIdentity.Address;
Dean@259
   513
Dean@364
   514
                // Calculate trustwords
Dean@366
   515
                this.CalcTrustwords(myIdentity,
Dean@366
   516
                                    partnerIdentity,
Dean@364
   517
                                    out trustwordsShort,
Dean@364
   518
                                    out trustwordsFull);
Dean@364
   519
Dean@332
   520
                // Don't show both the user name and address if they are the same
Dean@332
   521
                if ((item.TextLine1 != null) &&
Dean@332
   522
                    (item.TextLine2 != null) &&
Dean@332
   523
                    (item.TextLine1 == item.TextLine2))
Dean@332
   524
                {
Dean@332
   525
                    item.IsTwoTextLinesVisible = false;
Dean@332
   526
                }
Dean@332
   527
Dean@259
   528
                // Set image
Dean@359
   529
                switch (PrivacyState.ConvertRatingToPrivacyColor(identityPartnerColor))
Dean@259
   530
                {
Dean@359
   531
                    case PrivacyState.PrivacyColor.Green:
Dean@259
   532
                        item.ItemImage = pEp.Properties.Resources.green;
Dean@259
   533
                        break;
Dean@359
   534
                    case PrivacyState.PrivacyColor.Yellow:
Dean@259
   535
                        item.ItemImage = pEp.Properties.Resources.yellow;
Dean@259
   536
                        break;
Dean@359
   537
                    case PrivacyState.PrivacyColor.Red:
Dean@259
   538
                        item.ItemImage = pEp.Properties.Resources.red;
Dean@259
   539
                        break;
Dean@359
   540
                    case PrivacyState.PrivacyColor.Grey:
Dean@359
   541
                        item.ItemImage = null;
Dean@359
   542
                        break;
Dean@259
   543
                    default:
Dean@259
   544
                        item.ItemImage = null;
Dean@259
   545
                        break;
Dean@259
   546
                }
Dean@259
   547
Dean@259
   548
                // Set button
Dean@384
   549
                if (myIdentity.Equals(partnerIdentity) == false)
Dean@259
   550
                {
Dean@384
   551
                    if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@366
   552
                    {
Dean@366
   553
                        // Undo handshake
Dean@366
   554
                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_StopTrusting;
Dean@366
   555
                        item.IsButtonVisible = true;
Dean@366
   556
                        item.ButtonOnClick = (x, y) => { this.UndoHandshake(partnerIdentity); };
Dean@356
   557
Dean@366
   558
                        item.IsExpandable = false;
Dean@366
   559
                    }
Dean@384
   560
                    else if (partnerIdentity.CommunicationType >= _pEp_comm_type.pEp_ct_unconfirmed_encryption &&
Dean@384
   561
                             partnerIdentity.CommunicationType < _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@366
   562
                    {
Dean@366
   563
                        // Do handshake
Dean@366
   564
                        item.TextButton = pEp.Properties.Resources.PrivacyStatus_HandshakeAdvanced;
Dean@366
   565
                        item.IsButtonVisible = true;
Dean@366
   566
                        item.ButtonOnClick = (x, y) => { this.DoHandshake(partnerIdentity); };
Dean@356
   567
Dean@366
   568
                        item.IsExpandable = true;
Dean@366
   569
                        item.ExpandedText = Properties.Resources.PrivacyStatus_TrustwordDesc + "\n\n" +
Dean@366
   570
                                            trustwordsShort;
Dean@366
   571
                        item.ExpandedButton1Text = Properties.Resources.PrivacyStatus_SelectTrust;
Dean@366
   572
                        item.ExpandedButton2Text = Properties.Resources.PrivacyStatus_SelectMistrust;
Dean@366
   573
                        item.ExpandedButton1OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.Yes, partnerIdentity); };
Dean@366
   574
                        item.ExpandedButton2OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.No, partnerIdentity); };
Dean@366
   575
                    }
Dean@384
   576
                    else if (partnerIdentity.CommunicationType == _pEp_comm_type.pEp_ct_compromized)
Dean@366
   577
                    {
Dean@366
   578
                        /*
Dean@366
   579
                        // Redo handshake with confirmation
Dean@366
   580
                        item.TextButton = pEp.Properties.Resources.Handshake;
Dean@366
   581
                        item.IsButtonVisible = true;
Dean@366
   582
                        item.OnClick = (x, y) => { this.DoHandshakeForCompromisedKey(identityPartner); };
Dean@366
   583
                        */
Dean@366
   584
                    }
Dean@366
   585
                    else
Dean@366
   586
                    {
Dean@366
   587
                        item.IsButtonVisible = false;
Dean@366
   588
                    }
Dean@281
   589
                }
Dean@259
   590
                else
Dean@259
   591
                {
Dean@259
   592
                    item.IsButtonVisible = false;
Dean@259
   593
                }
Dean@259
   594
Dean@279
   595
                managerState.Identities.Add(item);
Dean@259
   596
            }
Dean@259
   597
Dean@375
   598
            // Attempt to select the first identity requiring a handshake
Dean@375
   599
            for (int i = 0; i < managerState.Identities.Count; i++)
Dean@375
   600
            {
Dean@375
   601
                // Determine if it's a handshake identity by simply seeing if it's expandable
Dean@375
   602
                if (managerState.Identities[i].IsExpandable)
Dean@375
   603
                {
Dean@375
   604
                    managerState.SelectedIdentityIndex = i;
Dean@375
   605
                    break;
Dean@375
   606
                }
Dean@375
   607
            }
Dean@375
   608
Dean@279
   609
            return (managerState);
Dean@279
   610
        }
Dean@279
   611
Dean@279
   612
        /// <summary>
Dean@279
   613
        /// Builds the latest state of the encryption status manager then shows the UI.
Dean@279
   614
        /// </summary>
Dean@279
   615
        private void BuildAndShowManager()
Dean@279
   616
        {
Dean@279
   617
            DialogResult result;
Dean@313
   618
            FormManagePrivacyStatus form;
Dean@313
   619
            FormManagePrivacyStatus.State stateOut;
Dean@279
   620
Dean@259
   621
            // Show the form
Dean@313
   622
            form = new FormManagePrivacyStatus();
Dean@259
   623
            form.StartPosition = FormStartPosition.CenterScreen;
Dean@259
   624
Dean@259
   625
            this.managerForm = form;
Dean@279
   626
            result = form.ShowDialog(this.ParentForm, this.GetManagerState(), out stateOut); // Must show as dialog to block code
Dean@259
   627
Dean@259
   628
            return;
Dean@259
   629
        }
Dean@259
   630
Dean@259
   631
        /// <summary>
Dean@179
   632
        /// Updates the status of the UI state based on the associated mail item.
Dean@179
   633
        /// Any previous state changes in the UI are preserved.
Dean@179
   634
        /// </summary>
Dean@179
   635
        private void UpdateUIFromMailItem()
Dean@159
   636
        {
Dean@312
   637
            PrivacyState state;
Dean@342
   638
            bool? sendUnencryptedProperty;
Dean@159
   639
Dean@173
   640
            if (this.associatedMailItem != null)
Dean@159
   641
            {
Dean@342
   642
                sendUnencryptedProperty = this.associatedMailItem.SendUnencrypted;
Dean@342
   643
Dean@342
   644
                if ((sendUnencryptedProperty != null) &&
Dean@342
   645
                    ((bool)sendUnencryptedProperty == true))
Dean@159
   646
                {
Dean@342
   647
                    // Force unencrypted
Dean@312
   648
                    state = new PrivacyState(_pEp_color.pEp_rating_unencrypted);
Dean@159
   649
                }
Dean@159
   650
                else
Dean@159
   651
                {
Dean@312
   652
                    state = new PrivacyState(this.associatedMailItem.ColorRating);
Dean@159
   653
                }
Dean@179
   654
Dean@179
   655
                this.CopyStateToUI(state);
Dean@159
   656
            }
Dean@159
   657
Dean@179
   658
            return;
Dean@159
   659
        }
Dean@159
   660
Dean@172
   661
        #region StateMethods
Dean@172
   662
Dean@172
   663
        /// <summary>
Dean@172
   664
        /// Connects or disconnects all control events from the UI.
Dean@172
   665
        /// </summary>
Dean@172
   666
        /// <param name="connect">True to connect events, false to disconnect.</param>
Dean@172
   667
        private void ConnectEvents(bool connect)
Dean@172
   668
        {
Dean@172
   669
            // Connect events only if not already connected
markus@201
   670
            if ((connect == true) &&
Dean@172
   671
                (this.eventsAreConnected == false))
Dean@172
   672
            {
Dean@312
   673
                this.ButtonPrivacyStatus.Click += this.ButtonPrivacyStatus_Click;
Dean@312
   674
                this.ButtonPrivacyStatus.MouseUp += this.ButtonPrivacyStatus_MouseUp;
Dean@179
   675
Dean@172
   676
                this.eventsAreConnected = true;
Dean@172
   677
            }
Dean@172
   678
            // Always attempt to disconnect
Dean@172
   679
            else if (connect == false)
Dean@172
   680
            {
Dean@312
   681
                this.ButtonPrivacyStatus.Click -= this.ButtonPrivacyStatus_Click;
Dean@312
   682
                this.ButtonPrivacyStatus.MouseUp -= this.ButtonPrivacyStatus_MouseUp;
Dean@179
   683
Dean@172
   684
                this.eventsAreConnected = false;
Dean@172
   685
            }
Dean@172
   686
Dean@172
   687
            return;
Dean@172
   688
        }
Dean@172
   689
Dean@172
   690
        /// <summary>
Dean@172
   691
        /// Refreshes the UI by reloading the state.
Dean@172
   692
        /// </summary>
Dean@172
   693
        private void RefreshUI()
Dean@172
   694
        {
Dean@172
   695
            this.CopyStateToUI(this.CopyUIToState());
Dean@172
   696
            return;
Dean@172
   697
        }
Dean@172
   698
Dean@172
   699
        /// <summary>
Dean@172
   700
        /// Copies the given state to the UI.
Dean@172
   701
        /// Events are turned off until the process is complete.
Dean@172
   702
        /// </summary>
Dean@172
   703
        /// <param name="state">The state to set to the UI.</param>
Dean@312
   704
        private void CopyStateToUI(PrivacyState state)
Dean@172
   705
        {
Dean@172
   706
            this.ConnectEvents(false);
markus@201
   707
Dean@172
   708
            ///////////////////////////////////////////////////////////
Dean@172
   709
            // Set UI data
Dean@172
   710
            ///////////////////////////////////////////////////////////
Dean@172
   711
Dean@179
   712
            // Save UI state maintained outside of controls
Dean@312
   713
            this.stateUIColorRating = state.PrivacyStatus;
Dean@179
   714
Dean@312
   715
            this.ButtonPrivacyStatus.BackColor = state.BackgroundColor;
Dean@312
   716
            this.ButtonPrivacyStatus.ForeColor = state.ForegroundColor;
Dean@312
   717
            this.ButtonPrivacyStatus.Text = state.ShortText;
Dean@172
   718
Dean@172
   719
            this.ConnectEvents(true);
Dean@172
   720
Dean@172
   721
            return;
Dean@172
   722
        }
Dean@172
   723
Dean@172
   724
        /// <summary>
Dean@172
   725
        /// Copies the UI to a new state.
Dean@172
   726
        /// </summary>
Dean@172
   727
        /// <returns>The state of the UI.</returns>
Dean@312
   728
        private PrivacyState CopyUIToState()
Dean@172
   729
        {
Dean@312
   730
            return (new PrivacyState(this.stateUIColorRating));
Dean@172
   731
        }
Dean@172
   732
Dean@172
   733
        #endregion
Dean@172
   734
Dean@159
   735
        /**************************************************************
Dean@159
   736
         * 
Dean@159
   737
         * Event Handling
Dean@159
   738
         * 
Dean@159
   739
         *************************************************************/
Dean@159
   740
Dean@159
   741
        /// <summary>
Dean@159
   742
        /// Event handler that is called before the form region is displayed.
Dean@159
   743
        /// </summary>
Dean@312
   744
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   745
        {
Dean@173
   746
            this.SetAssociatedMailItem();
Dean@173
   747
Dean@199
   748
            // Connect cryptable mail item events
Dean@189
   749
            if (this.associatedMailItem != null)
Dean@159
   750
            {
Dean@159
   751
                try
Dean@159
   752
                {
Dean@189
   753
                    this.associatedMailItem.PropertyChange += MailItem_PropertyChange;
Dean@179
   754
Dean@189
   755
                    if (this.associatedMailItem.IsInEncryptedStore &&
Dean@198
   756
                        this.associatedMailItem.IsPGPEncrypted)
Dean@159
   757
                    {
Dean@189
   758
                        this.associatedMailItem.Open += MailItem_Open;
Dean@159
   759
                    }
Dean@159
   760
                }
Dean@159
   761
                catch { }
Dean@179
   762
            }
Dean@159
   763
Dean@179
   764
            // Set the default UI state
Dean@312
   765
            this.CopyStateToUI(new PrivacyState());
Dean@179
   766
Dean@199
   767
            // Call the timer tick method manually to refresh data
Dean@199
   768
            this.TimerRefresh_Tick(null, new EventArgs());
Dean@179
   769
Dean@179
   770
            return;
Dean@159
   771
        }
Dean@159
   772
Dean@159
   773
        /// <summary>
Dean@159
   774
        /// Event handler for when the form region is closed.
Dean@159
   775
        /// </summary>
Dean@312
   776
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   777
        {
Dean@199
   778
            // Disconnect cryptable mail item events
Dean@190
   779
            if (this.associatedMailItem != null)
Dean@179
   780
            {
Dean@179
   781
                try
Dean@179
   782
                {
Dean@190
   783
                    this.associatedMailItem.PropertyChange -= MailItem_PropertyChange;
markus@201
   784
                    this.associatedMailItem.Open -= MailItem_Open;
Dean@179
   785
                }
Dean@179
   786
                catch { }
Dean@179
   787
            }
Dean@179
   788
Dean@179
   789
            return;
Dean@179
   790
        }
Dean@179
   791
Dean@179
   792
        /// <summary>
Dean@179
   793
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   794
        /// </summary>
Dean@179
   795
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   796
        {
Dean@199
   797
            bool tryAgain = false;
Dean@199
   798
            bool isSuccessful;
Dean@179
   799
            this.TimerRefresh.Enabled = false; // Only once
markus@201
   800
Dean@179
   801
            // Ensure the tick method is not called more than once
Dean@179
   802
            if (refreshOngoing == false)
Dean@179
   803
            {
Dean@179
   804
                this.refreshOngoing = true;
Dean@179
   805
Dean@179
   806
                if (this.associatedMailItem != null)
Dean@179
   807
                {
Dean@179
   808
                    if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@179
   809
                    {
Dean@179
   810
                        this.UpdateUIFromMailItem();
Dean@199
   811
Dean@199
   812
                        /* Create the unencrypted preview if the mail item is encrypted and
Dean@199
   813
                         * it is in an encrypted (untrusted) store
Dean@199
   814
                         * 
Dean@312
   815
                         * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Dean@199
   816
                         * it also is initialized after FormRegionPreviewUnencrypted.
Dean@199
   817
                         */
Dean@199
   818
                        if (this.associatedMailItem.IsInEncryptedStore &&
Dean@199
   819
                            this.associatedMailItem.IsPGPEncrypted)
Dean@199
   820
                        {
Dean@199
   821
                            isSuccessful = this.MakePreview();
Dean@199
   822
Dean@199
   823
                            if (isSuccessful == false)
Dean@199
   824
                            {
Dean@199
   825
                                tryAgain = true;
Dean@199
   826
                            }
Dean@199
   827
                        }
Dean@179
   828
                    }
Dean@179
   829
                    else
Dean@179
   830
                    {
Dean@179
   831
                        this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@199
   832
                        tryAgain = true;
Dean@179
   833
                    }
Dean@179
   834
                }
Dean@179
   835
Dean@199
   836
                // Set the timer to refresh again later automatically
Dean@199
   837
                if (tryAgain)
Dean@199
   838
                {
Dean@199
   839
                    this.TimerRefresh.Interval = 100;
Dean@199
   840
                    this.TimerRefresh.Enabled = true;
Dean@199
   841
                }
Dean@199
   842
Dean@179
   843
                this.refreshOngoing = false;
Dean@179
   844
            }
Dean@175
   845
Dean@159
   846
            return;
Dean@159
   847
        }
Dean@159
   848
Dean@169
   849
        /// <summary>
Dean@169
   850
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   851
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
   852
        /// </summary>
Dean@169
   853
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   854
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   855
        /// and the inspector is not displayed.</param>
Dean@169
   856
        private void MailItem_Open(ref bool cancel)
Dean@159
   857
        {
Dean@173
   858
            if (this.associatedMailItem != null && this.associatedMailItem.MirrorDisplay())
Dean@169
   859
            {
Dean@169
   860
                cancel = true;
Dean@169
   861
            }
Dean@169
   862
Dean@169
   863
            return;
Dean@159
   864
        }
Dean@159
   865
Dean@169
   866
        /// <summary>
Dean@169
   867
        /// Event handler for when a mail item property is changed.
Dean@169
   868
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
   869
        /// </summary>
Dean@169
   870
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@169
   871
        private void MailItem_PropertyChange(string propertyName)
Dean@159
   872
        {
Dean@179
   873
            switch (propertyName.ToUpper())
Dean@159
   874
            {
Dean@179
   875
                case "TO":
Dean@179
   876
                    // Start the refresh timer
Dean@179
   877
                    this.TimerRefresh.Enabled = true;
Dean@159
   878
                    break;
Dean@159
   879
                // Outlook bug: there are always both events, so one is enough
Dean@159
   880
                //case "CC":
Dean@179
   881
                //    // Start the refresh timer
Dean@179
   882
                //    this.TimerRefresh.Enabled = true;
Dean@159
   883
                //    break;
Dean@159
   884
            }
Dean@169
   885
Dean@169
   886
            return;
Dean@159
   887
        }
Dean@159
   888
Dean@179
   889
        /// <summary>
Dean@312
   890
        /// Event handler for when the privacy status button is clicked.
Dean@179
   891
        /// </summary>
Dean@312
   892
        private void ButtonPrivacyStatus_Click(object sender, EventArgs e)
vb@133
   893
        {
Dean@310
   894
            this.BuildAndShowManager();
Dean@179
   895
            return;
vb@133
   896
        }
vb@133
   897
Dean@179
   898
        /// <summary>
Dean@312
   899
        /// Event handler for when a mouse button is released over the privacy status button.
Dean@179
   900
        /// </summary>
Dean@312
   901
        private void ButtonPrivacyStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   902
        {
Dean@179
   903
            return;
vb@133
   904
        }
vb@133
   905
    }
vb@133
   906
}