UI/FormRegionPrivacyStatus.cs
author Dean Looyengoed
Tue, 02 Feb 2016 14:41:11 +0100
changeset 356 919bcfd67cc6
parent 355 c7e10bfe2035
child 359 cea9d72380d3
permissions -rw-r--r--
Populate expanded properties/data in the selection item for in-line handshake.
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@281
   195
        private void DoHandshakeForCompromisedKey(pEp_identity_s identityPartner)
Dean@281
   196
        {
Dean@281
   197
            DialogResult result;
Dean@281
   198
Dean@281
   199
            result = MessageBox.Show(this.ParentForm,
Dean@286
   200
                                     pEp.Properties.Resources.MessageWarningCompromisedKey,
Dean@286
   201
                                     pEp.Properties.Resources.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@179
   216
        private void DoHandshake(pEp_identity_s identityPartner)
vb@133
   217
        {
Dean@155
   218
            string ownShort;
Dean@155
   219
            string ownLong;
Dean@155
   220
            string ownFpr;
Dean@155
   221
            string partnerShort;
Dean@155
   222
            string partnerLong;
Dean@155
   223
            string partnerFpr;
Dean@155
   224
            string[] lines;
Dean@179
   225
            DialogResult result;
Dean@155
   226
            pEp_identity_s me;
Dean@155
   227
            FormHandshake handshakeDialog;
Dean@155
   228
            FormHandshake.State state = new FormHandshake.State();
markus@201
   229
Dean@190
   230
            Globals.ThisAddIn.LogVerbose("doHandshake");
Dean@190
   231
            Globals.ThisAddIn.LogVerbose(identityPartner.address);
vb@139
   232
vb@132
   233
            if (identityPartner.username == "")
vb@133
   234
                identityPartner.username = identityPartner.address;
Dean@190
   235
            Globals.ThisAddIn.LogVerbose(identityPartner.username);
vb@133
   236
dean@144
   237
            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
Dean@190
   238
            Globals.ThisAddIn.LogVerbose(identityPartner.user_id);
vb@133
   239
Dean@173
   240
            if (this.associatedMailItem.IsIncoming)
vb@133
   241
            {
Dean@173
   242
                string entryID = this.associatedMailItem.ReceivedByEntryID;
vb@133
   243
                string address = "";
vb@133
   244
markus@201
   245
                foreach (Outlook.Account a in Globals.ThisAddIn.Application.Session.Accounts)
markus@201
   246
                {
markus@201
   247
                    if (a.CurrentUser.EntryID == entryID)
markus@201
   248
                    {
vb@133
   249
                        address = a.SmtpAddress;
vb@133
   250
                        break;
vb@133
   251
                    }
vb@133
   252
                }
vb@133
   253
vb@133
   254
                if (address == "")
Dean@190
   255
                    address = Globals.ThisAddIn.Application.Session.Accounts[1].SmtpAddress;
vb@133
   256
Dean@190
   257
                me = Globals.ThisAddIn.mySelf(address);
vb@133
   258
            }
vb@133
   259
            else // outgoing
vb@133
   260
            {
Dean@190
   261
                Globals.ThisAddIn.LogVerbose("mailItem.from_address: " + this.associatedMailItem.FromAddress);
Dean@190
   262
                me = Globals.ThisAddIn.mySelf(this.associatedMailItem.FromAddress);
vb@133
   263
            }
vb@133
   264
Dean@190
   265
            Globals.ThisAddIn.LogVerbose(me.fpr);
vb@139
   266
Dean@155
   267
            ownShort = ThisAddIn.pEp.trustwords(me.fpr, max_words: 5).ToLower();
markus@201
   268
            ownLong = ThisAddIn.pEp.trustwords(me.fpr).ToLower();
markus@201
   269
            ownFpr = this.ToQuadruple(me.fpr);
vb@133
   270
Dean@190
   271
            Globals.ThisAddIn.LogVerbose(identityPartner.fpr);
vb@139
   272
Dean@155
   273
            partnerShort = ThisAddIn.pEp.trustwords(identityPartner.fpr, max_words: 5).ToLower();
markus@201
   274
            partnerLong = ThisAddIn.pEp.trustwords(identityPartner.fpr).ToLower();
markus@201
   275
            partnerFpr = this.ToQuadruple(identityPartner.fpr);
vb@133
   276
Dean@155
   277
            if (ownFpr.CompareTo(partnerFpr) > 0)
vb@133
   278
            {
Dean@155
   279
                state.TrustwordsShort = partnerShort + ownShort;
markus@201
   280
                state.TrustwordsFull = partnerLong + ownLong;
Dean@155
   281
Dean@155
   282
                lines = new string[3];
Dean@155
   283
                lines[0] = pEp.Properties.Resources.TrustwordsPartnershortPartner + partnerFpr;
Dean@155
   284
                lines[2] = pEp.Properties.Resources.TrustwordsPartnershortMyself + ownFpr;
Dean@155
   285
                state.Fingerprint = lines;
vb@133
   286
            }
vb@133
   287
            else
vb@133
   288
            {
Dean@155
   289
                state.TrustwordsShort = ownShort + " " + partnerShort;
markus@201
   290
                state.TrustwordsFull = ownLong + " " + partnerLong;
Dean@155
   291
Dean@155
   292
                lines = new string[3];
Dean@155
   293
                lines[0] = pEp.Properties.Resources.TrustwordsOwnshortMyself + ownFpr;
Dean@155
   294
                lines[2] = pEp.Properties.Resources.TrustwordsOwnshortPartner + partnerFpr;
Dean@155
   295
                state.Fingerprint = lines;
vb@133
   296
            }
vb@133
   297
Dean@155
   298
            // Create and show handshake dialog
Dean@155
   299
            handshakeDialog = new FormHandshake(state);
Dean@179
   300
            handshakeDialog.StartPosition = FormStartPosition.CenterScreen;
Dean@179
   301
            result = handshakeDialog.ShowDialog(this);
Dean@155
   302
Dean@356
   303
            this.ProcessDoHandshakeResult(result, identityPartner);
Dean@356
   304
Dean@356
   305
            return;
Dean@356
   306
        }
Dean@356
   307
Dean@356
   308
        /// <summary>
Dean@356
   309
        /// Processes the result of the do handshake dialog after a user makes a selection.
Dean@356
   310
        /// </summary>
Dean@356
   311
        /// <param name="result">The result of the handshake dialog selection.</param>
Dean@356
   312
        /// <param name="partner">The identity of the partner to handshake with.</param>
Dean@356
   313
        private void ProcessDoHandshakeResult(DialogResult result,
Dean@356
   314
                                              pEp_identity_s partner)
Dean@356
   315
        {
Dean@356
   316
            pEp_identity_s identityPartner = partner;
vb@133
   317
vb@133
   318
            switch (result)
vb@133
   319
            {
vb@133
   320
                case DialogResult.Yes:
Dean@279
   321
                    {
Dean@292
   322
                        identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
Dean@292
   323
Dean@290
   324
                        // Check if key was previously compromised -- warning for this must be displayed earlier
Dean@281
   325
                        if (identityPartner.comm_type == _pEp_comm_type.pEp_ct_compromized)
Dean@281
   326
                        {
Dean@281
   327
                            ThisAddIn.pEp.key_reset_trust(ref identityPartner);
Dean@281
   328
                        }
Dean@281
   329
Dean@279
   330
                        identityPartner = ThisAddIn.pEp.trust_personal_key(ref identityPartner);
Dean@293
   331
                        this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_trusted;
Dean@279
   332
Dean@279
   333
                        this.UpdateUIFromMailItem();
Dean@279
   334
Dean@279
   335
                        // Update the manager form state
Dean@279
   336
                        if (this.managerForm != null)
Dean@279
   337
                        {
Dean@279
   338
                            this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   339
                        }
Dean@279
   340
Dean@279
   341
                        break;
Dean@279
   342
                    }
vb@133
   343
                case DialogResult.No:
Dean@279
   344
                    {
Dean@292
   345
                        identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
Dean@292
   346
Dean@279
   347
                        ThisAddIn.pEp.key_compromized(ref identityPartner);
Dean@279
   348
                        identityPartner.comm_type = _pEp_comm_type.pEp_ct_compromized;
Dean@279
   349
                        identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
Dean@293
   350
                        this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_red;
Dean@279
   351
Dean@279
   352
                        this.UpdateUIFromMailItem();
Dean@279
   353
Dean@279
   354
                        // Update the manager form state
Dean@279
   355
                        if (this.managerForm != null)
Dean@279
   356
                        {
Dean@279
   357
                            this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   358
                        }
Dean@279
   359
Dean@279
   360
                        break;
Dean@279
   361
                    }
vb@133
   362
            }
Dean@279
   363
Dean@279
   364
            return;
vb@133
   365
        }
vb@133
   366
Dean@179
   367
        /// <summary>
Dean@179
   368
        /// Reverses any past handshake confirmation by unconfirming the given identity partner.
Dean@179
   369
        /// </summary>
Dean@179
   370
        /// <param name="identityPartner">The identity of the partner to unconfirm.</param>
Dean@179
   371
        private void UndoHandshake(pEp_identity_s identityPartner)
vb@133
   372
        {
Dean@292
   373
            identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
Dean@280
   374
            ThisAddIn.pEp.key_reset_trust(ref identityPartner);
Dean@279
   375
Dean@179
   376
            this.UpdateUIFromMailItem();
Dean@279
   377
Dean@279
   378
            // Update the manager form state
Dean@279
   379
            if (this.managerForm != null)
Dean@279
   380
            {
Dean@279
   381
                this.managerForm.CopyStateToUI(this.GetManagerState());
Dean@279
   382
            }
Dean@279
   383
Dean@279
   384
            return;
vb@133
   385
        }
vb@133
   386
Dean@161
   387
        /// <summary>
Dean@161
   388
        /// Formats the given text string as separated 4-character groups.
Dean@161
   389
        /// Example: 49422235FC99585B891C --> 4942 2235 FC99 585B 891C
Dean@161
   390
        /// </summary>
Dean@161
   391
        /// <param name="text">The text to format in 4-character groups.</param>
Dean@161
   392
        /// <returns>The re-formatted string.</returns>
Dean@161
   393
        private string ToQuadruple(string text)
Dean@159
   394
        {
Dean@159
   395
            List<string> result = new List<string>();
Dean@159
   396
Dean@161
   397
            if (text != null)
Dean@159
   398
            {
Dean@161
   399
                for (int i = 0; i < text.Length; i += 4)
Dean@159
   400
                {
Dean@161
   401
                    try
Dean@161
   402
                    {
Dean@161
   403
                        result.Add(text.Substring(i, 4));
Dean@161
   404
                    }
Dean@161
   405
                    catch (ArgumentOutOfRangeException)
Dean@161
   406
                    {
Dean@161
   407
                        result.Add(text.Substring(i));
Dean@161
   408
                        break;
Dean@161
   409
                    }
Dean@159
   410
                }
Dean@159
   411
            }
Dean@159
   412
Dean@159
   413
            return String.Join(" ", result);
Dean@159
   414
        }
markus@201
   415
Dean@196
   416
        /// <summary>
Dean@196
   417
        /// Makes the unencrypted preview form.
Dean@196
   418
        /// This gets the active form region then fills it's content.
Dean@196
   419
        /// </summary>
Dean@198
   420
        /// <returns>True if successful, otherwise false.</returns>
Dean@198
   421
        private bool MakePreview()
Dean@159
   422
        {
Dean@198
   423
            bool success = true;
Dean@198
   424
            bool isSuccessful;
Dean@198
   425
            byte[] rtfBody;
Dean@198
   426
            string subject;
Dean@159
   427
            WindowFormRegionCollection formRegions;
markus@201
   428
Dean@198
   429
            formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
Dean@159
   430
Dean@198
   431
            if ((formRegions != null) &&
Dean@198
   432
                (formRegions.FormRegionPreviewUnencrypted != null) &&
Dean@198
   433
                (formRegions.FormRegionPreviewUnencrypted.Visible))
Dean@198
   434
            {
Dean@198
   435
                // Attempt to the the RTF body
Dean@198
   436
                isSuccessful = this.associatedMailItem.MirrorTryGetRTFBody(out rtfBody);
Dean@198
   437
                if (isSuccessful == false)
Dean@198
   438
                {
Dean@198
   439
                    return (false);
Dean@198
   440
                }
Dean@159
   441
Dean@198
   442
                // Attempt to get the subject
Dean@198
   443
                isSuccessful = this.associatedMailItem.MirrorTryGetSubject(out subject);
Dean@198
   444
                if (isSuccessful == false)
Dean@159
   445
                {
Dean@198
   446
                    return (false);
Dean@159
   447
                }
Dean@196
   448
Dean@198
   449
                // Add data to the form
markus@201
   450
                formRegions.FormRegionPreviewUnencrypted.RichTextBoxPreview.Rtf = System.Text.Encoding.ASCII.GetString(rtfBody, 0, rtfBody.Length);
markus@201
   451
                formRegions.FormRegionPreviewUnencrypted.TextBoxSubject.Text = subject;
markus@201
   452
                formRegions.FormRegionPreviewUnencrypted.TextBoxFrom.Text = this.associatedMailItem.FromUsername + " <" + this.associatedMailItem.FromAddress + ">";
Dean@198
   453
            }
Dean@198
   454
            else
Dean@198
   455
            {
Dean@198
   456
                success = false;
Dean@198
   457
            }
Dean@198
   458
Dean@198
   459
            return (success);
Dean@159
   460
        }
markus@201
   461
Dean@179
   462
        /// <summary>
Dean@279
   463
        /// Builds a new manager form state using this encryption state/mail item current state.
Dean@279
   464
        /// </summary>
Dean@279
   465
        /// <returns>A new manager form state.</returns>
Dean@313
   466
        private FormManagePrivacyStatus.State GetManagerState()
Dean@279
   467
        {
Dean@259
   468
            pEp_identity_s[] identities;
Dean@259
   469
            pEp_identity_s incomingIdent;
Dean@259
   470
            pEp_identity_s identityPartner;
Dean@259
   471
            _pEp_color identityPartnerColor;
Dean@259
   472
            SelectionItem item;
Dean@312
   473
            PrivacyState state = this.CopyUIToState();
Dean@313
   474
            FormManagePrivacyStatus.State managerState = new FormManagePrivacyStatus.State();
Dean@259
   475
Dean@332
   476
            // Resolve all recipients -- this ensures the identities list is correctly populated
Dean@332
   477
            this.associatedMailItem.ResolveAllRecipients();
Dean@332
   478
Dean@279
   479
            managerState.VisualState = state.Copy();
Dean@279
   480
            managerState.IsIncoming = this.associatedMailItem.IsIncoming;
Dean@259
   481
Dean@259
   482
            if (this.associatedMailItem.IsIncoming)
Dean@259
   483
            {
Dean@259
   484
                identities = new pEp_identity_s[1];
Dean@259
   485
Dean@259
   486
                // Add only one identity
Dean@259
   487
                incomingIdent = new pEp_identity_s();
Dean@259
   488
                incomingIdent.address = this.associatedMailItem.FromAddress;
Dean@259
   489
                incomingIdent.username = this.associatedMailItem.FromUsername;
Dean@259
   490
                incomingIdent.user_id = CryptableMailItem.GetUserIDFromAddress(incomingIdent.address, incomingIdent.username);
Dean@259
   491
Dean@259
   492
                identities[0] = incomingIdent;
Dean@259
   493
            }
Dean@259
   494
            else
Dean@259
   495
            {
Dean@259
   496
                identities = this.associatedMailItem.Addresses;
Dean@259
   497
            }
Dean@259
   498
Dean@279
   499
            // Add identities
Dean@279
   500
            managerState.Identities.Clear();
Dean@259
   501
            foreach (pEp_identity_s ident in identities)
Dean@259
   502
            {
Dean@259
   503
                identityPartner = ThisAddIn.pEp.update_identity(ident);
Dean@259
   504
                identityPartnerColor = ThisAddIn.pEp.identity_color(identityPartner);
Dean@259
   505
Dean@259
   506
                item = new SelectionItem();
Dean@259
   507
                item.TextLine1 = identityPartner.username;
Dean@259
   508
                item.TextLine2 = identityPartner.address;
Dean@259
   509
Dean@332
   510
                // Don't show both the user name and address if they are the same
Dean@332
   511
                if ((item.TextLine1 != null) &&
Dean@332
   512
                    (item.TextLine2 != null) &&
Dean@332
   513
                    (item.TextLine1 == item.TextLine2))
Dean@332
   514
                {
Dean@332
   515
                    item.IsTwoTextLinesVisible = false;
Dean@332
   516
                }
Dean@332
   517
Dean@259
   518
                // Set image
Dean@259
   519
                switch (identityPartnerColor)
Dean@259
   520
                {
Dean@259
   521
                    case _pEp_color.pEp_rating_trusted:
Dean@259
   522
                    case _pEp_color.pEp_rating_trusted_and_anonymized:
Dean@259
   523
                        item.ItemImage = pEp.Properties.Resources.green;
Dean@259
   524
                        break;
Dean@259
   525
                    case _pEp_color.pEp_rating_reliable:
Dean@259
   526
                        item.ItemImage = pEp.Properties.Resources.yellow;
Dean@259
   527
                        break;
Dean@259
   528
                    case _pEp_color.pEp_rating_under_attack:
Dean@259
   529
                        item.ItemImage = pEp.Properties.Resources.red;
Dean@259
   530
                        break;
Dean@259
   531
                    default:
Dean@259
   532
                        item.ItemImage = null;
Dean@259
   533
                        break;
Dean@259
   534
                }
Dean@259
   535
Dean@259
   536
                // Set button
Dean@259
   537
                if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@259
   538
                {
Dean@259
   539
                    // Undo handshake
Dean@345
   540
                    item.TextButton = pEp.Properties.Resources.PrivacyStatus_StopTrusting;
Dean@259
   541
                    item.IsButtonVisible = true;
Dean@356
   542
                    item.ButtonOnClick = (x, y) => { this.UndoHandshake(identityPartner); };
Dean@356
   543
Dean@356
   544
                    item.IsExpandable = false;
Dean@259
   545
                }
Dean@259
   546
                else if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_unconfirmed_encryption &&
Dean@259
   547
                         identityPartner.comm_type < _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@259
   548
                {
Dean@259
   549
                    // Do handshake
Dean@356
   550
                    item.TextButton = pEp.Properties.Resources.PrivacyStatus_HandshakeAdvanced;
Dean@259
   551
                    item.IsButtonVisible = true;
Dean@352
   552
                    item.ButtonOnClick = (x, y) => { this.DoHandshake(identityPartner); };
Dean@356
   553
Dean@356
   554
                    item.IsExpandable = true;
Dean@356
   555
                    item.ExpandedText = Properties.Resources.PrivacyStatus_TrustwordDesc;
Dean@356
   556
                    item.ExpandedButton1Text = Properties.Resources.PrivacyStatus_SelectTrust;
Dean@356
   557
                    item.ExpandedButton2Text = Properties.Resources.PrivacyStatus_SelectMistrust;
Dean@356
   558
                    item.ExpandedButton1OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.Yes, identityPartner); };
Dean@356
   559
                    item.ExpandedButton2OnClick = (x, y) => { this.ProcessDoHandshakeResult(DialogResult.No, identityPartner); };
Dean@259
   560
                }
Dean@281
   561
                else if (identityPartner.comm_type == _pEp_comm_type.pEp_ct_compromized)
Dean@281
   562
                {
Dean@325
   563
                    /*
Dean@281
   564
                    // Redo handshake with confirmation
Dean@281
   565
                    item.TextButton = pEp.Properties.Resources.Handshake;
Dean@281
   566
                    item.IsButtonVisible = true;
Dean@281
   567
                    item.OnClick = (x, y) => { this.DoHandshakeForCompromisedKey(identityPartner); };
Dean@325
   568
                    */
Dean@281
   569
                }
Dean@259
   570
                else
Dean@259
   571
                {
Dean@259
   572
                    // No button, just view
Dean@259
   573
                    item.IsButtonVisible = false;
Dean@259
   574
                }
Dean@259
   575
Dean@279
   576
                managerState.Identities.Add(item);
Dean@259
   577
            }
Dean@259
   578
Dean@279
   579
            return (managerState);
Dean@279
   580
        }
Dean@279
   581
Dean@279
   582
        /// <summary>
Dean@279
   583
        /// Builds the latest state of the encryption status manager then shows the UI.
Dean@279
   584
        /// </summary>
Dean@279
   585
        private void BuildAndShowManager()
Dean@279
   586
        {
Dean@279
   587
            DialogResult result;
Dean@313
   588
            FormManagePrivacyStatus form;
Dean@313
   589
            FormManagePrivacyStatus.State stateOut;
Dean@279
   590
Dean@259
   591
            // Show the form
Dean@313
   592
            form = new FormManagePrivacyStatus();
Dean@259
   593
            form.StartPosition = FormStartPosition.CenterScreen;
Dean@259
   594
Dean@259
   595
            this.managerForm = form;
Dean@279
   596
            result = form.ShowDialog(this.ParentForm, this.GetManagerState(), out stateOut); // Must show as dialog to block code
Dean@259
   597
Dean@259
   598
            return;
Dean@259
   599
        }
Dean@259
   600
Dean@259
   601
        /// <summary>
Dean@179
   602
        /// Updates the status of the UI state based on the associated mail item.
Dean@179
   603
        /// Any previous state changes in the UI are preserved.
Dean@179
   604
        /// </summary>
Dean@179
   605
        private void UpdateUIFromMailItem()
Dean@159
   606
        {
Dean@312
   607
            PrivacyState state;
Dean@342
   608
            bool? sendUnencryptedProperty;
Dean@159
   609
Dean@173
   610
            if (this.associatedMailItem != null)
Dean@159
   611
            {
Dean@342
   612
                sendUnencryptedProperty = this.associatedMailItem.SendUnencrypted;
Dean@342
   613
Dean@342
   614
                if ((sendUnencryptedProperty != null) &&
Dean@342
   615
                    ((bool)sendUnencryptedProperty == true))
Dean@159
   616
                {
Dean@342
   617
                    // Force unencrypted
Dean@312
   618
                    state = new PrivacyState(_pEp_color.pEp_rating_unencrypted);
Dean@159
   619
                }
Dean@159
   620
                else
Dean@159
   621
                {
Dean@312
   622
                    state = new PrivacyState(this.associatedMailItem.ColorRating);
Dean@159
   623
                }
Dean@179
   624
Dean@179
   625
                this.CopyStateToUI(state);
Dean@159
   626
            }
Dean@159
   627
Dean@179
   628
            return;
Dean@159
   629
        }
Dean@159
   630
Dean@172
   631
        #region StateMethods
Dean@172
   632
Dean@172
   633
        /// <summary>
Dean@172
   634
        /// Connects or disconnects all control events from the UI.
Dean@172
   635
        /// </summary>
Dean@172
   636
        /// <param name="connect">True to connect events, false to disconnect.</param>
Dean@172
   637
        private void ConnectEvents(bool connect)
Dean@172
   638
        {
Dean@172
   639
            // Connect events only if not already connected
markus@201
   640
            if ((connect == true) &&
Dean@172
   641
                (this.eventsAreConnected == false))
Dean@172
   642
            {
Dean@312
   643
                this.ButtonPrivacyStatus.Click += this.ButtonPrivacyStatus_Click;
Dean@312
   644
                this.ButtonPrivacyStatus.MouseUp += this.ButtonPrivacyStatus_MouseUp;
Dean@179
   645
Dean@172
   646
                this.eventsAreConnected = true;
Dean@172
   647
            }
Dean@172
   648
            // Always attempt to disconnect
Dean@172
   649
            else if (connect == false)
Dean@172
   650
            {
Dean@312
   651
                this.ButtonPrivacyStatus.Click -= this.ButtonPrivacyStatus_Click;
Dean@312
   652
                this.ButtonPrivacyStatus.MouseUp -= this.ButtonPrivacyStatus_MouseUp;
Dean@179
   653
Dean@172
   654
                this.eventsAreConnected = false;
Dean@172
   655
            }
Dean@172
   656
Dean@172
   657
            return;
Dean@172
   658
        }
Dean@172
   659
Dean@172
   660
        /// <summary>
Dean@172
   661
        /// Refreshes the UI by reloading the state.
Dean@172
   662
        /// </summary>
Dean@172
   663
        private void RefreshUI()
Dean@172
   664
        {
Dean@172
   665
            this.CopyStateToUI(this.CopyUIToState());
Dean@172
   666
            return;
Dean@172
   667
        }
Dean@172
   668
Dean@172
   669
        /// <summary>
Dean@172
   670
        /// Copies the given state to the UI.
Dean@172
   671
        /// Events are turned off until the process is complete.
Dean@172
   672
        /// </summary>
Dean@172
   673
        /// <param name="state">The state to set to the UI.</param>
Dean@312
   674
        private void CopyStateToUI(PrivacyState state)
Dean@172
   675
        {
Dean@172
   676
            this.ConnectEvents(false);
markus@201
   677
Dean@172
   678
            ///////////////////////////////////////////////////////////
Dean@172
   679
            // Set UI data
Dean@172
   680
            ///////////////////////////////////////////////////////////
Dean@172
   681
Dean@179
   682
            // Save UI state maintained outside of controls
Dean@312
   683
            this.stateUIColorRating = state.PrivacyStatus;
Dean@179
   684
Dean@312
   685
            this.ButtonPrivacyStatus.BackColor = state.BackgroundColor;
Dean@312
   686
            this.ButtonPrivacyStatus.ForeColor = state.ForegroundColor;
Dean@312
   687
            this.ButtonPrivacyStatus.Text = state.ShortText;
Dean@172
   688
Dean@172
   689
            this.ConnectEvents(true);
Dean@172
   690
Dean@172
   691
            return;
Dean@172
   692
        }
Dean@172
   693
Dean@172
   694
        /// <summary>
Dean@172
   695
        /// Copies the UI to a new state.
Dean@172
   696
        /// </summary>
Dean@172
   697
        /// <returns>The state of the UI.</returns>
Dean@312
   698
        private PrivacyState CopyUIToState()
Dean@172
   699
        {
Dean@312
   700
            return (new PrivacyState(this.stateUIColorRating));
Dean@172
   701
        }
Dean@172
   702
Dean@172
   703
        #endregion
Dean@172
   704
Dean@159
   705
        /**************************************************************
Dean@159
   706
         * 
Dean@159
   707
         * Event Handling
Dean@159
   708
         * 
Dean@159
   709
         *************************************************************/
Dean@159
   710
Dean@159
   711
        /// <summary>
Dean@159
   712
        /// Event handler that is called before the form region is displayed.
Dean@159
   713
        /// </summary>
Dean@312
   714
        private void FormRegionPrivacyStatus_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   715
        {
Dean@173
   716
            this.SetAssociatedMailItem();
Dean@173
   717
Dean@199
   718
            // Connect cryptable mail item events
Dean@189
   719
            if (this.associatedMailItem != null)
Dean@159
   720
            {
Dean@159
   721
                try
Dean@159
   722
                {
Dean@189
   723
                    this.associatedMailItem.PropertyChange += MailItem_PropertyChange;
Dean@179
   724
Dean@189
   725
                    if (this.associatedMailItem.IsInEncryptedStore &&
Dean@198
   726
                        this.associatedMailItem.IsPGPEncrypted)
Dean@159
   727
                    {
Dean@189
   728
                        this.associatedMailItem.Open += MailItem_Open;
Dean@159
   729
                    }
Dean@159
   730
                }
Dean@159
   731
                catch { }
Dean@179
   732
            }
Dean@159
   733
Dean@179
   734
            // Set the default UI state
Dean@312
   735
            this.CopyStateToUI(new PrivacyState());
Dean@179
   736
Dean@199
   737
            // Call the timer tick method manually to refresh data
Dean@199
   738
            this.TimerRefresh_Tick(null, new EventArgs());
Dean@179
   739
Dean@179
   740
            return;
Dean@159
   741
        }
Dean@159
   742
Dean@159
   743
        /// <summary>
Dean@159
   744
        /// Event handler for when the form region is closed.
Dean@159
   745
        /// </summary>
Dean@312
   746
        private void FormRegionPrivacyStatus_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   747
        {
Dean@199
   748
            // Disconnect cryptable mail item events
Dean@190
   749
            if (this.associatedMailItem != null)
Dean@179
   750
            {
Dean@179
   751
                try
Dean@179
   752
                {
Dean@190
   753
                    this.associatedMailItem.PropertyChange -= MailItem_PropertyChange;
markus@201
   754
                    this.associatedMailItem.Open -= MailItem_Open;
Dean@179
   755
                }
Dean@179
   756
                catch { }
Dean@179
   757
            }
Dean@179
   758
Dean@179
   759
            return;
Dean@179
   760
        }
Dean@179
   761
Dean@179
   762
        /// <summary>
Dean@179
   763
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   764
        /// </summary>
Dean@179
   765
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   766
        {
Dean@199
   767
            bool tryAgain = false;
Dean@199
   768
            bool isSuccessful;
Dean@179
   769
            this.TimerRefresh.Enabled = false; // Only once
markus@201
   770
Dean@179
   771
            // Ensure the tick method is not called more than once
Dean@179
   772
            if (refreshOngoing == false)
Dean@179
   773
            {
Dean@179
   774
                this.refreshOngoing = true;
Dean@179
   775
Dean@179
   776
                if (this.associatedMailItem != null)
Dean@179
   777
                {
Dean@179
   778
                    if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@179
   779
                    {
Dean@179
   780
                        this.UpdateUIFromMailItem();
Dean@199
   781
Dean@199
   782
                        /* Create the unencrypted preview if the mail item is encrypted and
Dean@199
   783
                         * it is in an encrypted (untrusted) store
Dean@199
   784
                         * 
Dean@312
   785
                         * This is done here because FormRegionPrivacyStatus has the cryptable mail item and
Dean@199
   786
                         * it also is initialized after FormRegionPreviewUnencrypted.
Dean@199
   787
                         */
Dean@199
   788
                        if (this.associatedMailItem.IsInEncryptedStore &&
Dean@199
   789
                            this.associatedMailItem.IsPGPEncrypted)
Dean@199
   790
                        {
Dean@199
   791
                            isSuccessful = this.MakePreview();
Dean@199
   792
Dean@199
   793
                            if (isSuccessful == false)
Dean@199
   794
                            {
Dean@199
   795
                                tryAgain = true;
Dean@199
   796
                            }
Dean@199
   797
                        }
Dean@179
   798
                    }
Dean@179
   799
                    else
Dean@179
   800
                    {
Dean@179
   801
                        this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@199
   802
                        tryAgain = true;
Dean@179
   803
                    }
Dean@179
   804
                }
Dean@179
   805
Dean@199
   806
                // Set the timer to refresh again later automatically
Dean@199
   807
                if (tryAgain)
Dean@199
   808
                {
Dean@199
   809
                    this.TimerRefresh.Interval = 100;
Dean@199
   810
                    this.TimerRefresh.Enabled = true;
Dean@199
   811
                }
Dean@199
   812
Dean@179
   813
                this.refreshOngoing = false;
Dean@179
   814
            }
Dean@175
   815
Dean@159
   816
            return;
Dean@159
   817
        }
Dean@159
   818
Dean@169
   819
        /// <summary>
Dean@169
   820
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   821
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
   822
        /// </summary>
Dean@169
   823
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   824
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   825
        /// and the inspector is not displayed.</param>
Dean@169
   826
        private void MailItem_Open(ref bool cancel)
Dean@159
   827
        {
Dean@173
   828
            if (this.associatedMailItem != null && this.associatedMailItem.MirrorDisplay())
Dean@169
   829
            {
Dean@169
   830
                cancel = true;
Dean@169
   831
            }
Dean@169
   832
Dean@169
   833
            return;
Dean@159
   834
        }
Dean@159
   835
Dean@169
   836
        /// <summary>
Dean@169
   837
        /// Event handler for when a mail item property is changed.
Dean@169
   838
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
   839
        /// </summary>
Dean@169
   840
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@169
   841
        private void MailItem_PropertyChange(string propertyName)
Dean@159
   842
        {
Dean@179
   843
            switch (propertyName.ToUpper())
Dean@159
   844
            {
Dean@179
   845
                case "TO":
Dean@179
   846
                    // Start the refresh timer
Dean@179
   847
                    this.TimerRefresh.Enabled = true;
Dean@159
   848
                    break;
Dean@159
   849
                // Outlook bug: there are always both events, so one is enough
Dean@159
   850
                //case "CC":
Dean@179
   851
                //    // Start the refresh timer
Dean@179
   852
                //    this.TimerRefresh.Enabled = true;
Dean@159
   853
                //    break;
Dean@159
   854
            }
Dean@169
   855
Dean@169
   856
            return;
Dean@159
   857
        }
Dean@159
   858
Dean@179
   859
        /// <summary>
Dean@312
   860
        /// Event handler for when the privacy status button is clicked.
Dean@179
   861
        /// </summary>
Dean@312
   862
        private void ButtonPrivacyStatus_Click(object sender, EventArgs e)
vb@133
   863
        {
Dean@310
   864
            this.BuildAndShowManager();
Dean@179
   865
            return;
vb@133
   866
        }
vb@133
   867
Dean@179
   868
        /// <summary>
Dean@312
   869
        /// Event handler for when a mouse button is released over the privacy status button.
Dean@179
   870
        /// </summary>
Dean@312
   871
        private void ButtonPrivacyStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   872
        {
Dean@179
   873
            return;
vb@133
   874
        }
vb@133
   875
    }
vb@133
   876
}