EncryptionStatusPanel.cs
author Dean Looyengoed
Wed, 30 Dec 2015 04:29:34 +0100
changeset 179 ff5101f87d35
parent 178 a2358eedefef
child 180 1602b0e8fdd6
permissions -rw-r--r--
Finish almost completely rewriting the encryption status panel... yes, it was very necessary.
The preview unencrypted functionality is currently disabled to be added back later.
This fixes a lot of issues and most functionality should work a lot better now.
However, needs further testing and some TODO's remain.
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@159
    14
    /// Partial class for the encryption status panel that is displayed below every outlook message.
Dean@159
    15
    /// </summary>
vb@133
    16
    partial class EncryptionStatusPanel
vb@133
    17
    {
Dean@164
    18
        private static readonly Color COLOR_RED        = Color.Red;
Dean@164
    19
        private static readonly Color COLOR_YELLOW     = Color.Gold;
Dean@164
    20
        private static readonly Color COLOR_GREEN      = Color.Green;
Dean@164
    21
        private static readonly Color COLOR_GREY       = Color.Gainsboro;
Dean@164
    22
        private static readonly Color TEXT_COLOR_BLACK = Color.Black;
Dean@164
    23
        private static readonly Color TEXT_COLOR_WHITE = Color.White;
Dean@162
    24
vb@133
    25
        #region Form Region Factory
vb@133
    26
vb@133
    27
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
vb@133
    28
        [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegion1")]
vb@133
    29
        public partial class FormRegion1Factory
vb@133
    30
        {
vb@133
    31
            // Occurs before the form region is initialized.
vb@133
    32
            // To prevent the form region from appearing, set e.Cancel to true.
vb@133
    33
            // Use e.OutlookItem to get a reference to the current Outlook item.
vb@133
    34
            private void FormRegion1Factory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
vb@133
    35
            {
vb@133
    36
            }
vb@133
    37
        }
vb@133
    38
vb@133
    39
        #endregion
vb@133
    40
Dean@159
    41
        /* Notes:
Dean@179
    42
         * 
Dean@159
    43
         * Use this.OutlookItem to get a reference to the current Outlook item.
Dean@159
    44
         * Use this.OutlookFormRegion to get a reference to the form region.
Dean@179
    45
         * 
Dean@179
    46
         * UI State Managment:
Dean@179
    47
         * 
Dean@179
    48
         * The UI state is almost entirely set from the associated mail item data.
Dean@179
    49
         * However, a separate state class is maintained to represent the UI as some separation is needed.
Dean@179
    50
         * This logical separation MUST be maintained throughout the code.
Dean@179
    51
         * Specific cases are noted where possible.
Dean@179
    52
         * 
Dean@159
    53
         */
Dean@159
    54
Dean@170
    55
        private CryptableMailItem associatedMailItem = null;
Dean@179
    56
        private bool              refreshOngoing     = false;
Dean@179
    57
        private bool              eventsAreConnected = false;
Dean@170
    58
Dean@170
    59
        // UI data or controls not stored in the UI
Dean@179
    60
        private _pEp_color stateUIColorRating; // WARNING: Do NOT use this directly
vb@133
    61
Dean@159
    62
        /**************************************************************
Dean@159
    63
         * 
Dean@159
    64
         * Methods
Dean@159
    65
         * 
Dean@159
    66
         *************************************************************/
Dean@159
    67
Dean@171
    68
        /// <summary>
Dean@171
    69
        /// Method used to show/report an error based on build mode.
Dean@171
    70
        /// </summary>
Dean@171
    71
        /// <param name="reason">The reason/explanation to stop.</param>
Dean@159
    72
        static void StopHere(string reason)
vb@133
    73
        {
Dean@159
    74
#if DEBUG
Dean@159
    75
            MessageBox.Show(reason);
Dean@159
    76
#else
Dean@159
    77
            try
vb@133
    78
            {
Dean@159
    79
                ThisAddIn.pEp.log("internal error", "pEp for Outlook", reason, "in EncryptionStatusPanel.cs");
vb@133
    80
            }
Dean@159
    81
            catch (COMException) { }
Dean@159
    82
            catch (Exception) { }
Dean@159
    83
#endif
vb@133
    84
        }
vb@133
    85
Dean@170
    86
        /// <summary>
Dean@170
    87
        /// Sets the cryptable mail item associated with this encryption status panel.
Dean@170
    88
        /// The associated mail item can then be accessed through its local variable.
Dean@170
    89
        /// </summary>
Dean@170
    90
        private void SetAssociatedMailItem()
Dean@170
    91
        {
Dean@170
    92
            bool errorOccurred = false;
Dean@170
    93
            Outlook.MailItem omi = null;
Dean@170
    94
            Outlook.MAPIFolder omiFolder;
Dean@170
    95
            Outlook.MAPIFolder deletedFolder;
Dean@170
    96
Dean@170
    97
            // Null check
Dean@170
    98
            if (!errorOccurred)
Dean@170
    99
            {
Dean@170
   100
                try
Dean@170
   101
                {
Dean@170
   102
                    if (this.OutlookItem == null)
Dean@170
   103
                    {
Dean@170
   104
                        errorOccurred = true;
Dean@170
   105
                    }
Dean@170
   106
                }
Dean@170
   107
                catch (COMException)
Dean@170
   108
                {
Dean@170
   109
                    errorOccurred = true;
Dean@170
   110
                }
Dean@170
   111
            }
Dean@170
   112
Dean@170
   113
            // Attempt to get and cast the outlook mail item
Dean@170
   114
            if (!errorOccurred)
Dean@170
   115
            {
Dean@170
   116
                try
Dean@170
   117
                {
Dean@170
   118
                    omi = (Outlook.MailItem)this.OutlookItem;
Dean@170
   119
                }
Dean@170
   120
                catch
Dean@170
   121
                {
Dean@170
   122
                    errorOccurred = true;
Dean@170
   123
                }
Dean@170
   124
            }
Dean@170
   125
Dean@170
   126
            // Ensure the mail item is not in the deleted folder
Dean@170
   127
            if (!errorOccurred)
Dean@170
   128
            {
Dean@170
   129
                try
Dean@170
   130
                {
Dean@170
   131
                    omiFolder = omi.Parent;
Dean@170
   132
Dean@170
   133
                    if (omiFolder != null)
Dean@170
   134
                    {
Dean@170
   135
                        deletedFolder = omiFolder.Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
Dean@170
   136
Dean@170
   137
                        if ((deletedFolder != null) && 
Dean@170
   138
                            (omiFolder.EntryID == deletedFolder.EntryID))
Dean@170
   139
                        {
Dean@170
   140
                            errorOccurred = true;
Dean@170
   141
                        }
Dean@170
   142
                    }
Dean@170
   143
                    else
Dean@170
   144
                    {
Dean@170
   145
                        errorOccurred = true;
Dean@170
   146
                    }
Dean@170
   147
                }
Dean@170
   148
                catch
Dean@170
   149
                {
Dean@170
   150
                    errorOccurred = true;
Dean@170
   151
                }
Dean@170
   152
            }
Dean@170
   153
Dean@170
   154
            // Finally set the associated mail item
Dean@170
   155
            if ((errorOccurred) ||
Dean@170
   156
                (omi == null))
Dean@170
   157
            {
Dean@170
   158
                this.associatedMailItem = null;
Dean@170
   159
            }
Dean@170
   160
            else
Dean@170
   161
            {
Dean@170
   162
                // Check if the associated mail item has already been set
Dean@170
   163
                if (this.associatedMailItem != null)
Dean@170
   164
                {
Dean@170
   165
                    // Only re-set the mail item if the EntryID has changed
Dean@170
   166
                    if (this.associatedMailItem.EntryID != omi.EntryID)
Dean@170
   167
                    {
Dean@170
   168
                        this.associatedMailItem = new CryptableMailItem(omi);
Dean@170
   169
                    }
Dean@170
   170
                }
Dean@170
   171
                else
Dean@170
   172
                {
Dean@170
   173
                    this.associatedMailItem = new CryptableMailItem(omi);
Dean@170
   174
                }
Dean@170
   175
            }
Dean@170
   176
Dean@170
   177
            return;
Dean@170
   178
        }
Dean@170
   179
Dean@179
   180
        /// <summary>
Dean@179
   181
        /// Starts the handshake process where the identity of a partner can be confirmed.
Dean@179
   182
        /// </summary>
Dean@179
   183
        /// <param name="identityPartner">The identity of the partner to complete the handshake with.</param>
Dean@179
   184
        private void DoHandshake(pEp_identity_s identityPartner)
vb@133
   185
        {
Dean@155
   186
            string ownShort;
Dean@155
   187
            string ownLong;
Dean@155
   188
            string ownFpr;
Dean@155
   189
            string partnerShort;
Dean@155
   190
            string partnerLong;
Dean@155
   191
            string partnerFpr;
Dean@155
   192
            string[] lines;
Dean@179
   193
            DialogResult result;
Dean@155
   194
            pEp_identity_s me;
Dean@155
   195
            FormHandshake handshakeDialog;
Dean@155
   196
            FormHandshake.State state = new FormHandshake.State();
Dean@176
   197
            
vb@139
   198
            ThisAddIn.verbose("doHandshake");
vb@139
   199
            ThisAddIn.verbose(identityPartner.address);
vb@139
   200
vb@132
   201
            if (identityPartner.username == "")
vb@133
   202
                identityPartner.username = identityPartner.address;
vb@139
   203
            ThisAddIn.verbose(identityPartner.username);
vb@133
   204
dean@144
   205
            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   206
            ThisAddIn.verbose(identityPartner.user_id);
vb@133
   207
Dean@173
   208
            if (this.associatedMailItem.IsIncoming)
vb@133
   209
            {
Dean@173
   210
                string entryID = this.associatedMailItem.ReceivedByEntryID;
vb@133
   211
                string address = "";
vb@133
   212
vb@133
   213
                foreach (Outlook.Account a in ThisAddIn.addIn.Application.Session.Accounts){
vb@133
   214
                    if (a.CurrentUser.EntryID == entryID) {
vb@133
   215
                        address = a.SmtpAddress;
vb@133
   216
                        break;
vb@133
   217
                    }
vb@133
   218
                }
vb@133
   219
vb@133
   220
                if (address == "")
vb@133
   221
                    address = ThisAddIn.addIn.Application.Session.Accounts[1].SmtpAddress;
vb@133
   222
vb@133
   223
                me = ThisAddIn.addIn.mySelf(address);
vb@133
   224
            }
vb@133
   225
            else // outgoing
vb@133
   226
            {
Dean@173
   227
                ThisAddIn.verbose("mailItem.from_address: " + this.associatedMailItem.FromAddress);
Dean@173
   228
                me = ThisAddIn.addIn.mySelf(this.associatedMailItem.FromAddress);
vb@133
   229
            }
vb@133
   230
vb@139
   231
            ThisAddIn.verbose(me.fpr);
vb@139
   232
Dean@155
   233
            ownShort = ThisAddIn.pEp.trustwords(me.fpr, max_words: 5).ToLower();
Dean@155
   234
            ownLong  = ThisAddIn.pEp.trustwords(me.fpr).ToLower();
Dean@161
   235
            ownFpr   = this.ToQuadruple(me.fpr);
vb@133
   236
vb@139
   237
            ThisAddIn.verbose(identityPartner.fpr);
vb@139
   238
Dean@155
   239
            partnerShort = ThisAddIn.pEp.trustwords(identityPartner.fpr, max_words: 5).ToLower();
Dean@155
   240
            partnerLong  = ThisAddIn.pEp.trustwords(identityPartner.fpr).ToLower();
Dean@161
   241
            partnerFpr   = this.ToQuadruple(identityPartner.fpr);
vb@133
   242
Dean@155
   243
            if (ownFpr.CompareTo(partnerFpr) > 0)
vb@133
   244
            {
Dean@155
   245
                state.TrustwordsShort = partnerShort + ownShort;
Dean@155
   246
                state.TrustwordsFull  = partnerLong  + ownLong;
Dean@155
   247
Dean@155
   248
                lines = new string[3];
Dean@155
   249
                lines[0] = pEp.Properties.Resources.TrustwordsPartnershortPartner + partnerFpr;
Dean@155
   250
                lines[2] = pEp.Properties.Resources.TrustwordsPartnershortMyself + ownFpr;
Dean@155
   251
                state.Fingerprint = lines;
vb@133
   252
            }
vb@133
   253
            else
vb@133
   254
            {
Dean@155
   255
                state.TrustwordsShort = ownShort + " " + partnerShort;
Dean@155
   256
                state.TrustwordsFull  = ownLong  + " " + partnerLong;
Dean@155
   257
Dean@155
   258
                lines = new string[3];
Dean@155
   259
                lines[0] = pEp.Properties.Resources.TrustwordsOwnshortMyself + ownFpr;
Dean@155
   260
                lines[2] = pEp.Properties.Resources.TrustwordsOwnshortPartner + partnerFpr;
Dean@155
   261
                state.Fingerprint = lines;
vb@133
   262
            }
vb@133
   263
vb@139
   264
            ThisAddIn.verbose("try to show dialog");
vb@139
   265
Dean@155
   266
            // Create and show handshake dialog
Dean@155
   267
            handshakeDialog = new FormHandshake(state);
Dean@179
   268
            handshakeDialog.StartPosition = FormStartPosition.CenterScreen;
Dean@179
   269
            result = handshakeDialog.ShowDialog(this);
Dean@155
   270
vb@133
   271
            ThisAddIn.verbose("handshakeDialog.ShowDialog(this) => " + result.ToString());
vb@133
   272
vb@133
   273
            switch (result)
vb@133
   274
            {
vb@133
   275
                case DialogResult.Yes:
Dean@173
   276
                    this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_trusted;
vb@132
   277
                    identityPartner = ThisAddIn.pEp.trust_personal_key(identityPartner);
Dean@179
   278
                    this.UpdateUIFromMailItem();
vb@133
   279
                    break;
vb@133
   280
                case DialogResult.No:
vb@133
   281
                    ThisAddIn.pEp.key_compromized(identityPartner);
vb@133
   282
                    identityPartner.comm_type = _pEp_comm_type.pEp_ct_compromized;
Dean@173
   283
                    this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_red;
Dean@179
   284
                    this.UpdateUIFromMailItem();
vb@133
   285
                    break;
vb@133
   286
            }
vb@133
   287
        }
vb@133
   288
Dean@179
   289
        /// <summary>
Dean@179
   290
        /// Reverses any past handshake confirmation by unconfirming the given identity partner.
Dean@179
   291
        /// </summary>
Dean@179
   292
        /// <param name="identityPartner">The identity of the partner to unconfirm.</param>
Dean@179
   293
        private void UndoHandshake(pEp_identity_s identityPartner)
vb@133
   294
        {
vb@133
   295
            ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   296
            identityPartner.comm_type ^= _pEp_comm_type.pEp_ct_confirmed;
vb@133
   297
            ThisAddIn.pEp.update_identity(identityPartner);
Dean@179
   298
Dean@179
   299
            // TODO: review this code, the pEp engine should drive the rating change...
Dean@179
   300
            this.associatedMailItem.ColorRating = _pEp_color.pEp_rating_reliable;
Dean@179
   301
Dean@179
   302
            this.UpdateUIFromMailItem();
vb@133
   303
        }
vb@133
   304
Dean@161
   305
        /// <summary>
Dean@161
   306
        /// Formats the given text string as separated 4-character groups.
Dean@161
   307
        /// Example: 49422235FC99585B891C --> 4942 2235 FC99 585B 891C
Dean@161
   308
        /// </summary>
Dean@161
   309
        /// <param name="text">The text to format in 4-character groups.</param>
Dean@161
   310
        /// <returns>The re-formatted string.</returns>
Dean@161
   311
        private string ToQuadruple(string text)
Dean@159
   312
        {
Dean@159
   313
            List<string> result = new List<string>();
Dean@159
   314
Dean@161
   315
            if (text != null)
Dean@159
   316
            {
Dean@161
   317
                for (int i = 0; i < text.Length; i += 4)
Dean@159
   318
                {
Dean@161
   319
                    try
Dean@161
   320
                    {
Dean@161
   321
                        result.Add(text.Substring(i, 4));
Dean@161
   322
                    }
Dean@161
   323
                    catch (ArgumentOutOfRangeException)
Dean@161
   324
                    {
Dean@161
   325
                        result.Add(text.Substring(i));
Dean@161
   326
                        break;
Dean@161
   327
                    }
Dean@159
   328
                }
Dean@159
   329
            }
Dean@159
   330
Dean@159
   331
            return String.Join(" ", result);
Dean@159
   332
        }
Dean@179
   333
        /* TODO: add this back later
Dean@159
   334
        protected void makePreview()
Dean@159
   335
        {
Dean@159
   336
            WindowFormRegionCollection formRegions;
Dean@159
   337
Dean@159
   338
            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
Dean@159
   339
                return;
Dean@159
   340
            else if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
Dean@159
   341
                formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
Dean@159
   342
            else
Dean@159
   343
                return;
Dean@159
   344
Dean@159
   345
            try
Dean@159
   346
            {
Dean@159
   347
                if (formRegions.PreviewUnencrypted.Visible)
Dean@159
   348
                {
Dean@173
   349
                    byte[] rtf = this.associatedMailItem.MirrorGetRTFBody();
Dean@159
   350
                    formRegions.PreviewUnencrypted.textPreview.Rtf = System.Text.Encoding.ASCII.GetString(rtf, 0, rtf.Length);
Dean@173
   351
                    formRegions.PreviewUnencrypted.textSubject.Text = this.associatedMailItem.MirrorGetSubject();
Dean@173
   352
                    formRegions.PreviewUnencrypted.textFrom.Text = this.associatedMailItem.FromUsername + " <" + this.associatedMailItem.FromAddress + ">";
Dean@159
   353
                }
Dean@159
   354
            }
Dean@159
   355
            catch { }
Dean@159
   356
        }
Dean@179
   357
        */
Dean@179
   358
        /// <summary>
Dean@179
   359
        /// Updates the status of the UI state based on the associated mail item.
Dean@179
   360
        /// Any previous state changes in the UI are preserved.
Dean@179
   361
        /// </summary>
Dean@179
   362
        private void UpdateUIFromMailItem()
Dean@159
   363
        {
Dean@179
   364
            State state;
Dean@159
   365
Dean@173
   366
            if (this.associatedMailItem != null)
Dean@159
   367
            {
Dean@179
   368
                state = this.CopyUIToState();
Dean@179
   369
Dean@179
   370
                // Set encryption status
Dean@179
   371
                if (this.associatedMailItem.SendUnencrypted == CheckState.Checked)
Dean@159
   372
                {
Dean@179
   373
                    // Always set to unencrypted
Dean@179
   374
                    state.EncryptionStatus = _pEp_color.pEp_rating_unencrypted;
Dean@159
   375
                }
Dean@159
   376
                else
Dean@159
   377
                {
Dean@179
   378
                    state.EncryptionStatus = this.associatedMailItem.ColorRating;
Dean@159
   379
                }
Dean@179
   380
Dean@179
   381
                // Set send unencrypted state
Dean@179
   382
                state.SendUnencryptedCheckState = this.associatedMailItem.SendUnencrypted;
Dean@179
   383
Dean@179
   384
                this.CopyStateToUI(state);
Dean@159
   385
            }
Dean@159
   386
Dean@179
   387
            return;
Dean@159
   388
        }
Dean@159
   389
Dean@172
   390
        #region StateMethods
Dean@172
   391
Dean@172
   392
        /// <summary>
Dean@172
   393
        /// Connects or disconnects all control events from the UI.
Dean@172
   394
        /// </summary>
Dean@172
   395
        /// <param name="connect">True to connect events, false to disconnect.</param>
Dean@172
   396
        private void ConnectEvents(bool connect)
Dean@172
   397
        {
Dean@172
   398
            // Connect events only if not already connected
Dean@172
   399
            if ((connect == true) && 
Dean@172
   400
                (this.eventsAreConnected == false))
Dean@172
   401
            {
Dean@179
   402
                this.ButtonEncryptionStatus.Click     += this.ButtonEncryptionStatus_Click;
Dean@179
   403
                this.ButtonEncryptionStatus.MouseUp   += this.ButtonEncryptionStatus_MouseUp;
Dean@179
   404
                this.LinkLabelExplanation.LinkClicked += this.LinkLabelExplanation_LinkClicked;
Dean@179
   405
Dean@179
   406
                this.PictureBoxOptions.Click      += this.PictureBoxOptions_Click;
Dean@179
   407
                this.PictureBoxOptions.MouseHover += this.PictureBoxOptions_MouseHover;
Dean@179
   408
Dean@179
   409
                this.ToolStripMenuItemSendUnencrypted.Click += this.ToolStripMenuItemSendUnencrypted_Click;
Dean@172
   410
Dean@172
   411
                this.eventsAreConnected = true;
Dean@172
   412
            }
Dean@172
   413
            // Always attempt to disconnect
Dean@172
   414
            else if (connect == false)
Dean@172
   415
            {
Dean@179
   416
                this.ButtonEncryptionStatus.Click     -= this.ButtonEncryptionStatus_Click;
Dean@179
   417
                this.ButtonEncryptionStatus.MouseUp   -= this.ButtonEncryptionStatus_MouseUp;
Dean@179
   418
                this.LinkLabelExplanation.LinkClicked -= this.LinkLabelExplanation_LinkClicked;
Dean@179
   419
Dean@179
   420
                this.PictureBoxOptions.Click      -= this.PictureBoxOptions_Click;
Dean@179
   421
                this.PictureBoxOptions.MouseHover -= this.PictureBoxOptions_MouseHover;
Dean@179
   422
Dean@179
   423
                this.ToolStripMenuItemSendUnencrypted.Click -= this.ToolStripMenuItemSendUnencrypted_Click;
Dean@172
   424
Dean@172
   425
                this.eventsAreConnected = false;
Dean@172
   426
            }
Dean@172
   427
Dean@172
   428
            return;
Dean@172
   429
        }
Dean@172
   430
Dean@172
   431
        /// <summary>
Dean@172
   432
        /// Refreshes the UI by reloading the state.
Dean@172
   433
        /// </summary>
Dean@172
   434
        private void RefreshUI()
Dean@172
   435
        {
Dean@172
   436
            this.CopyStateToUI(this.CopyUIToState());
Dean@172
   437
            return;
Dean@172
   438
        }
Dean@172
   439
Dean@172
   440
        /// <summary>
Dean@172
   441
        /// Copies the given state to the UI.
Dean@172
   442
        /// Events are turned off until the process is complete.
Dean@172
   443
        /// </summary>
Dean@172
   444
        /// <param name="state">The state to set to the UI.</param>
Dean@172
   445
        private void CopyStateToUI(State state)
Dean@172
   446
        {
Dean@172
   447
            this.ConnectEvents(false);
Dean@172
   448
            
Dean@172
   449
            ///////////////////////////////////////////////////////////
Dean@172
   450
            // Set UI data
Dean@172
   451
            ///////////////////////////////////////////////////////////
Dean@172
   452
Dean@179
   453
            // Save UI state maintained outside of controls
Dean@179
   454
            this.stateUIColorRating = state.EncryptionStatus;
Dean@179
   455
Dean@172
   456
            this.LinkLabelExplanation.Text        = state.Title;
Dean@172
   457
            this.ButtonEncryptionStatus.BackColor = state.ButtonBackground;
Dean@172
   458
            this.ButtonEncryptionStatus.ForeColor = state.ButtonForeground;
Dean@172
   459
            this.ButtonEncryptionStatus.Text      = state.ButtonText;
Dean@172
   460
Dean@172
   461
            ///////////////////////////////////////////////////////////
Dean@172
   462
            // Set UI selections
Dean@172
   463
            ///////////////////////////////////////////////////////////
Dean@172
   464
Dean@179
   465
            /* Note: When displaying the send unencrypted checked state,
Dean@179
   466
             * convert any indeterminate state to unchecked.
Dean@179
   467
             * This is necessary for the end user.
Dean@179
   468
             * As a result of this change during display, the UI state will 
Dean@179
   469
             * carry this change as soon as a refresh occurs.
Dean@179
   470
             * This is not a problem because the mail item itself is not updated
Dean@179
   471
             * until the user clicks the send unencrypted checkbox themselves.
Dean@179
   472
             * This is a case where the UI state and mail item are different.
Dean@179
   473
             */
Dean@179
   474
            if (state.SendUnencryptedCheckState == CheckState.Checked)
Dean@179
   475
            {
Dean@179
   476
                this.ToolStripMenuItemSendUnencrypted.CheckState = CheckState.Checked;
Dean@179
   477
            }
Dean@179
   478
            else
Dean@179
   479
            {
Dean@179
   480
                // Both indeterminate and unchecked shown as unchecked
Dean@179
   481
                this.ToolStripMenuItemSendUnencrypted.CheckState = CheckState.Unchecked;
Dean@179
   482
            }
Dean@172
   483
Dean@172
   484
            ///////////////////////////////////////////////////////////
Dean@172
   485
            // Set UI control visibility
Dean@172
   486
            ///////////////////////////////////////////////////////////
Dean@172
   487
Dean@172
   488
            this.ConnectEvents(true);
Dean@172
   489
Dean@172
   490
            return;
Dean@172
   491
        }
Dean@172
   492
Dean@172
   493
        /// <summary>
Dean@172
   494
        /// Copies the UI to a new state.
Dean@172
   495
        /// </summary>
Dean@172
   496
        /// <returns>The state of the UI.</returns>
Dean@172
   497
        private State CopyUIToState()
Dean@172
   498
        {
Dean@172
   499
            State state = new State();
Dean@172
   500
Dean@179
   501
            state.EncryptionStatus          = this.stateUIColorRating;
Dean@179
   502
            state.SendUnencryptedCheckState = this.ToolStripMenuItemSendUnencrypted.CheckState;
Dean@172
   503
Dean@172
   504
            return (state);
Dean@172
   505
        }
Dean@172
   506
Dean@172
   507
        #endregion
Dean@172
   508
Dean@159
   509
        /**************************************************************
Dean@159
   510
         * 
Dean@159
   511
         * Event Handling
Dean@159
   512
         * 
Dean@159
   513
         *************************************************************/
Dean@159
   514
Dean@159
   515
        /// <summary>
Dean@159
   516
        /// Event handler that is called before the form region is displayed.
Dean@159
   517
        /// </summary>
Dean@159
   518
        private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   519
        {
Dean@179
   520
            Outlook.MailItem omi = null;
Dean@179
   521
            State state;
Dean@179
   522
Dean@173
   523
            this.SetAssociatedMailItem();
Dean@173
   524
Dean@179
   525
            // TODO: This should be done long before here and only once
Dean@179
   526
            if (!ThisAddIn.addIn.enabled)
Dean@159
   527
            {
Dean@179
   528
                ThisAddIn.addIn.enable();
Dean@159
   529
            }
Dean@179
   530
Dean@179
   531
            // Connect events for the associated outlook mail item directly
Dean@179
   532
            if (this.OutlookItem != null)
Dean@159
   533
            {
Dean@159
   534
                try
Dean@159
   535
                {
Dean@179
   536
                    omi = (Outlook.MailItem)this.OutlookItem;
Dean@169
   537
                    omi.PropertyChange += MailItem_PropertyChange;
Dean@179
   538
Dean@159
   539
                    if (CryptableMailItem.IsEncryptedStore(omi))
Dean@159
   540
                    {
Dean@159
   541
                        if (PreviewUnencrypted.isPGPEncrypted(omi) && omi.Sender != null)
Dean@159
   542
                        {
Dean@169
   543
                            omi.Open += MailItem_Open;
Dean@159
   544
                        }
Dean@159
   545
                    }
Dean@159
   546
                }
Dean@159
   547
                catch { }
Dean@179
   548
            }
Dean@159
   549
Dean@179
   550
            // Set the default UI state
Dean@179
   551
            state = new State();
Dean@179
   552
            state.EncryptionStatus = _pEp_color.pEp_rating_undefined;
Dean@179
   553
            this.CopyStateToUI(state);
Dean@179
   554
Dean@179
   555
            // Check if the mailitem has been dowloaded yet
Dean@179
   556
            if (this.associatedMailItem != null)
Dean@179
   557
            {
Dean@179
   558
                if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@159
   559
                {
Dean@179
   560
                    this.UpdateUIFromMailItem();
Dean@159
   561
                }
Dean@159
   562
                else
Dean@159
   563
                {
Dean@179
   564
                    this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@179
   565
Dean@179
   566
                    //this.makePreview(); // TODO: add this back?
Dean@179
   567
Dean@179
   568
                    // Start the refresh timer
Dean@179
   569
                    this.TimerRefresh.Interval = 50;
Dean@179
   570
                    this.TimerRefresh.Enabled = true;
Dean@159
   571
                }
Dean@159
   572
            }
Dean@179
   573
Dean@179
   574
            return;
Dean@159
   575
        }
Dean@159
   576
Dean@159
   577
        /// <summary>
Dean@159
   578
        /// Event handler for when the form region is closed.
Dean@159
   579
        /// </summary>
Dean@159
   580
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   581
        {
Dean@179
   582
            Outlook.MailItem omi;
Dean@179
   583
Dean@179
   584
            // Disconnect any events for the associated outlook mail item directly
Dean@179
   585
            if (this.OutlookItem != null)
Dean@179
   586
            {
Dean@179
   587
                try
Dean@179
   588
                {
Dean@179
   589
                    omi = (Outlook.MailItem)this.OutlookItem;
Dean@179
   590
                    omi.PropertyChange -= MailItem_PropertyChange;
Dean@179
   591
                    omi.Open           -= MailItem_Open;
Dean@179
   592
                }
Dean@179
   593
                catch { }
Dean@179
   594
            }
Dean@179
   595
Dean@179
   596
            return;
Dean@179
   597
        }
Dean@179
   598
Dean@179
   599
        /// <summary>
Dean@179
   600
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   601
        /// </summary>
Dean@179
   602
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   603
        {
Dean@179
   604
            this.TimerRefresh.Enabled = false; // Only once
Dean@179
   605
            
Dean@179
   606
            // Ensure the tick method is not called more than once
Dean@179
   607
            if (refreshOngoing == false)
Dean@179
   608
            {
Dean@179
   609
                this.refreshOngoing = true;
Dean@179
   610
Dean@179
   611
                if (this.associatedMailItem != null)
Dean@179
   612
                {
Dean@179
   613
                    if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@179
   614
                    {
Dean@179
   615
                        this.UpdateUIFromMailItem();
Dean@179
   616
                    }
Dean@179
   617
                    else
Dean@179
   618
                    {
Dean@179
   619
                        // Set the timer to refresh again later automatically
Dean@179
   620
                        this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@179
   621
                        this.TimerRefresh.Interval = 100;
Dean@179
   622
                        this.TimerRefresh.Enabled = true;
Dean@179
   623
                    }
Dean@179
   624
                }
Dean@179
   625
Dean@179
   626
                this.refreshOngoing = false;
Dean@179
   627
            }
Dean@175
   628
Dean@159
   629
            return;
Dean@159
   630
        }
Dean@159
   631
Dean@169
   632
        /// <summary>
Dean@169
   633
        /// Event handler for when a mail item is being opened in an inspector.
Dean@169
   634
        /// </summary>
Dean@169
   635
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   636
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   637
        /// and the inspector is not displayed.</param>
Dean@169
   638
        private void MailItem_Open(ref bool cancel)
Dean@159
   639
        {
Dean@179
   640
            // Cancel opening if the mirror is displayed instead
Dean@173
   641
            if (this.associatedMailItem != null && this.associatedMailItem.MirrorDisplay())
Dean@169
   642
            {
Dean@169
   643
                cancel = true;
Dean@169
   644
            }
Dean@169
   645
Dean@169
   646
            return;
Dean@159
   647
        }
Dean@159
   648
Dean@169
   649
        /// <summary>
Dean@169
   650
        /// Event handler for when a mail item property is changed.
Dean@169
   651
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
   652
        /// </summary>
Dean@169
   653
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@169
   654
        private void MailItem_PropertyChange(string propertyName)
Dean@159
   655
        {
Dean@179
   656
            switch (propertyName.ToUpper())
Dean@159
   657
            {
Dean@179
   658
                case "TO":
Dean@179
   659
                    // Start the refresh timer
Dean@179
   660
                    this.TimerRefresh.Enabled = true;
Dean@159
   661
                    break;
Dean@159
   662
                // Outlook bug: there are always both events, so one is enough
Dean@159
   663
                //case "CC":
Dean@179
   664
                //    // Start the refresh timer
Dean@179
   665
                //    this.TimerRefresh.Enabled = true;
Dean@159
   666
                //    break;
Dean@159
   667
            }
Dean@169
   668
Dean@169
   669
            return;
Dean@159
   670
        }
Dean@159
   671
Dean@179
   672
        /// <summary>
Dean@179
   673
        /// Event handler for when the encryption status button is clicked.
Dean@179
   674
        /// </summary>
Dean@178
   675
        private void ButtonEncryptionStatus_Click(object sender, EventArgs e)
vb@133
   676
        {
Dean@179
   677
            string text;
Dean@179
   678
            string reason;
Dean@179
   679
            string[] keys;
Dean@179
   680
            State state = this.CopyUIToState();
Dean@179
   681
            
Dean@179
   682
            switch (state.EncryptionStatus)
vb@133
   683
            {
vb@133
   684
                case _pEp_color.pEp_rating_green:
vb@133
   685
                    try
vb@133
   686
                    {
Dean@179
   687
                        keys = this.associatedMailItem.Keylist;
Dean@179
   688
Dean@179
   689
                        if (keys.Length > 0)
vb@133
   690
                        {
Dean@179
   691
                            if (keys.Length > 1)
Dean@179
   692
                            {
Dean@179
   693
                                text = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageMultiple + " {0}.",
Dean@179
   694
                                                     String.Join(", ", keys));
Dean@179
   695
                            }
vb@133
   696
                            else
Dean@179
   697
                            {
Dean@179
   698
                                text = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageSingle + " {0}.", 
Dean@179
   699
                                                     keys[0]);
Dean@179
   700
                            }
Dean@179
   701
Dean@179
   702
                            // Display all keys the message is encrypted with
Dean@179
   703
                            MessageBox.Show(text, 
Dean@179
   704
                                            pEp.Properties.Resources.EncryptionStatusClickMessageBox, 
Dean@179
   705
                                            MessageBoxButtons.OK, 
Dean@179
   706
                                            MessageBoxIcon.Information);
vb@133
   707
                        }
vb@133
   708
                    }
vb@133
   709
                    catch (Exception ex)
vb@133
   710
                    {
vb@133
   711
                        StopHere(ex.ToString());
vb@133
   712
                    }
vb@133
   713
                    break;
vb@133
   714
vb@133
   715
                case _pEp_color.pEp_rating_yellow:
vb@133
   716
                    try
vb@133
   717
                    {
Dean@173
   718
                        if (this.associatedMailItem.IsIncoming)
vb@133
   719
                        {
vb@133
   720
                            pEp_identity_s identityPartner = new pEp_identity_s();
Dean@173
   721
                            identityPartner.address = this.associatedMailItem.FromAddress;
Dean@173
   722
                            identityPartner.username = this.associatedMailItem.FromUsername;
dean@144
   723
                            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   724
vb@133
   725
                            try
vb@133
   726
                            {
vb@133
   727
                                identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   728
                            }
vb@133
   729
                            catch
vb@133
   730
                            {
Dean@179
   731
                                state.EncryptionStatus = _pEp_color.pEp_rating_undefined;
Dean@179
   732
                                this.CopyStateToUI(state);
vb@133
   733
                                return;
vb@133
   734
                            }
Dean@179
   735
                            this.DoHandshake(identityPartner);
vb@133
   736
                        }
vb@133
   737
                        else // outgoing
vb@133
   738
                        {
vb@139
   739
                            ThisAddIn.verbose("yellow, handshake initiated");
vb@139
   740
vb@133
   741
                            List<pEp_identity_s> partnersToCheck = new List<pEp_identity_s>();
vb@133
   742
Dean@173
   743
                            foreach (pEp_identity_s identity in this.associatedMailItem.Addresses)
vb@133
   744
                            {
vb@139
   745
                                ThisAddIn.verbose("adding " + identity.username + " <" + identity.address + ">");
vb@139
   746
vb@133
   747
                                try
vb@133
   748
                                {
vb@133
   749
                                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(identity);
vb@133
   750
                                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_unconfirmed_encryption
vb@133
   751
                                        && identityPartner.comm_type < _pEp_comm_type.pEp_ct_confirmed_encryption)
vb@133
   752
                                    {
vb@133
   753
                                        partnersToCheck.Add(identityPartner);
vb@133
   754
                                    }
vb@133
   755
                                }
vb@133
   756
                                catch
vb@133
   757
                                {
vb@133
   758
                                }
vb@133
   759
                            }
vb@133
   760
vb@133
   761
                            if (partnersToCheck.Count == 0)
vb@133
   762
                            {
Dean@179
   763
                                state.EncryptionStatus = _pEp_color.pEp_rating_undefined;
Dean@179
   764
                                this.CopyStateToUI(state);
vb@133
   765
                                return;
vb@133
   766
                            }
vb@133
   767
Dean@173
   768
                            pEp_identity_s[] addresses = this.associatedMailItem.Addresses;
vb@133
   769
vb@133
   770
                            if (addresses.Length == 1)
vb@133
   771
                            {
Dean@179
   772
                                this.DoHandshake(partnersToCheck[0]);
vb@133
   773
                            }
vb@133
   774
                            else if (addresses.Length > 1)
vb@133
   775
                            {
Dean@178
   776
                                ContextMenuHandshake.Items.Clear();
Dean@179
   777
                                foreach (pEp_identity_s identity in partnersToCheck) 
Dean@179
   778
                                {
Dean@179
   779
                                    text = identity.username + " <" + identity.address + ">";
Dean@179
   780
                                    ContextMenuHandshake.Items.Add(text, 
Dean@179
   781
                                                                   null, 
Dean@179
   782
                                                                   (x, y) => { this.DoHandshake(identity); });
vb@133
   783
                                }
Dean@178
   784
                                ContextMenuHandshake.Show(ButtonEncryptionStatus, 10, 10);
vb@133
   785
                            }
vb@133
   786
                        }
vb@133
   787
                    }
vb@133
   788
                    catch (COMException ex)
vb@133
   789
                    {
vb@133
   790
                        StopHere(ex.ToString());
vb@133
   791
                    }
vb@133
   792
                    catch (Exception ex)
vb@133
   793
                    {
vb@133
   794
                        StopHere(ex.ToString());
vb@133
   795
                    }
vb@133
   796
                    break;
vb@133
   797
vb@133
   798
                case _pEp_color.pEp_rating_unencrypted:
Dean@173
   799
                    if (this.associatedMailItem.IsIncoming)
vb@133
   800
                    {
vb@133
   801
                        MessageBox.Show(pEp.Properties.Resources.RatingUnencryptedMessageboxReasonBody, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   802
                    }
vb@133
   803
                    else
vb@133
   804
                    {
vb@133
   805
                        try
vb@133
   806
                        {
Dean@179
   807
                            reason = "";
Dean@173
   808
                            foreach (pEp_identity_s identity in this.associatedMailItem.Addresses)
vb@133
   809
                            {
vb@133
   810
                                if (identity.comm_type < _pEp_comm_type.pEp_ct_security_by_obscurity)
vb@133
   811
                                {
vb@133
   812
                                    if (reason != "")
vb@133
   813
                                        reason += "\n";
vb@133
   814
                                    // BUG: reason string in English language only and trivial
vb@133
   815
                                    string ct = identity.comm_type.ToString();
vb@133
   816
                                    ct = new string(ct.Skip(7).ToArray());
vb@133
   817
                                    ct = ct.Replace("_", " ");
vb@133
   818
                                    reason += identity.username + " <" + identity.address + ">: " + ct;
vb@133
   819
                                }
vb@133
   820
                            }
vb@133
   821
                            if (reason != "")
vb@133
   822
                                MessageBox.Show(reason, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   823
                        }
vb@133
   824
                        catch (COMException ex)
vb@133
   825
                        {
vb@133
   826
                            StopHere(ex.ToString());
vb@133
   827
                        }
vb@133
   828
                    }
vb@133
   829
                    break;
vb@133
   830
vb@133
   831
                case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   832
                case _pEp_color.pEp_rating_have_no_key:
vb@133
   833
                    try
vb@133
   834
                    {
Dean@179
   835
                        keys = this.associatedMailItem.Keylist;
vb@133
   836
vb@133
   837
                        if (keys.Length > 1)
vb@133
   838
                        {
vb@133
   839
                            if (keys.Length == 2)
Dean@179
   840
                            {
vb@133
   841
                                reason = pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodyNokey;
Dean@179
   842
                            }
vb@133
   843
                            else
Dean@179
   844
                            {
Dean@179
   845
                                reason = string.Format(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodySomekey, 
Dean@179
   846
                                                       keys.Length - 1);
Dean@179
   847
                            }
vb@133
   848
vb@133
   849
                            string keyinfo = "";
vb@133
   850
vb@133
   851
                            foreach (string key in keys)
vb@133
   852
                            {
vb@133
   853
                                if (key.Length >= 8)
vb@133
   854
                                    keyinfo += "\n" + key;
vb@133
   855
                            }
vb@133
   856
vb@133
   857
                            reason += keyinfo;
vb@133
   858
Dean@179
   859
                            MessageBox.Show(reason, 
Dean@179
   860
                                            pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   861
                        }
vb@133
   862
                        else if (keys.Length == 1)
vb@133
   863
                        {
Dean@179
   864
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonError + keys[0], 
Dean@179
   865
                                            pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   866
                        }
vb@133
   867
                        else
vb@133
   868
                        {
Dean@179
   869
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonUnknownformat, 
Dean@179
   870
                                            pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   871
                        }
vb@133
   872
                    }
vb@133
   873
                    catch (COMException ex)
vb@133
   874
                    {
vb@133
   875
                        StopHere(ex.ToString());
vb@133
   876
                    }
vb@133
   877
                    break;
vb@133
   878
                    
vb@133
   879
                case _pEp_color.pEp_rating_undefined:
vb@133
   880
                    break;
vb@133
   881
            }
Dean@179
   882
Dean@179
   883
            return;
vb@133
   884
        }
vb@133
   885
Dean@179
   886
        /// <summary>
Dean@179
   887
        /// Event handler for when a mouse button is released over the encryption status button.
Dean@179
   888
        /// This will show the context menu.
Dean@179
   889
        /// </summary>
Dean@179
   890
        private void ButtonEncryptionStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   891
        {
Dean@179
   892
            bool showContextMenu = false;
Dean@179
   893
            string text;
Dean@179
   894
            Button button;
vb@133
   895
Dean@179
   896
            if ((sender != null) &&
Dean@179
   897
                (e.Button == System.Windows.Forms.MouseButtons.Right))
Dean@179
   898
            { 
Dean@179
   899
                button = (Button)sender;
Dean@179
   900
                
Dean@179
   901
                // Remove all tool strip menu items except the first, which should be send unencrypted
Dean@179
   902
                while (this.ContextMenuEncryptionStatus.Items.Count > 1)
Dean@179
   903
                {
Dean@179
   904
                    this.ContextMenuEncryptionStatus.Items.RemoveAt(1);
Dean@179
   905
                }
vb@133
   906
Dean@179
   907
                // Enable the send unencrypted menu item
Dean@173
   908
                if (this.associatedMailItem != null && 
Dean@173
   909
                    !this.associatedMailItem.IsIncoming && 
Dean@173
   910
                    !this.associatedMailItem.IsInSentFolder && 
Dean@173
   911
                    this.associatedMailItem.OutgoingColor > _pEp_color.pEp_rating_unencrypted)
vb@133
   912
                {
Dean@179
   913
                    this.ToolStripMenuItemSendUnencrypted.Visible = true;
Dean@179
   914
                    showContextMenu = true;
Dean@179
   915
                }
Dean@179
   916
                else
Dean@179
   917
                {
Dean@179
   918
                    this.ToolStripMenuItemSendUnencrypted.Visible = false;
Dean@179
   919
                }
Dean@179
   920
Dean@179
   921
                // Build and enable any options to untrust identities
Dean@179
   922
                if (this.associatedMailItem != null)
Dean@179
   923
                {
Dean@179
   924
                    if (this.associatedMailItem.IsIncoming)
vb@133
   925
                    {
Dean@179
   926
                        // Create and update identity of the sender
Dean@179
   927
                        pEp_identity_s ident;
Dean@179
   928
                        ident          = new pEp_identity_s();
Dean@179
   929
                        ident.address  = this.associatedMailItem.FromAddress;
Dean@179
   930
                        ident.username = this.associatedMailItem.FromUsername;
Dean@179
   931
                        ident.user_id  = CryptableMailItem.GetUserIDFromAddress(ident.address, ident.username);
Dean@179
   932
                        ident = ThisAddIn.pEp.update_identity(ident);
Dean@179
   933
Dean@179
   934
                        text = pEp.Properties.Resources.IdentityTrustText + 
Dean@179
   935
                               ident.username + " <" + ident.address + ">";
Dean@179
   936
Dean@179
   937
                        this.ContextMenuEncryptionStatus.Items.Add(text, 
Dean@179
   938
                                                                   null, 
Dean@179
   939
                                                                   (x, y) => { this.UndoHandshake(ident); });
vb@133
   940
                    }
Dean@179
   941
                    else
Dean@173
   942
                    {
Dean@179
   943
                        foreach (pEp_identity_s ident in this.associatedMailItem.Addresses)
Dean@179
   944
                        {
Dean@179
   945
                            pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(ident);
Dean@179
   946
Dean@179
   947
                            if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@179
   948
                            {
Dean@179
   949
                                text = pEp.Properties.Resources.IdentityTrustText + 
Dean@179
   950
                                       ident.username + " <" + ident.address + ">";
Dean@179
   951
Dean@179
   952
                                this.ContextMenuEncryptionStatus.Items.Add(text, 
Dean@179
   953
                                                                           null, 
Dean@179
   954
                                                                           (x, y) => { this.UndoHandshake(ident); });
Dean@179
   955
                            }
Dean@179
   956
                        }
vb@133
   957
                    }
vb@133
   958
Dean@179
   959
                    showContextMenu = true;
Dean@179
   960
                }
Dean@179
   961
Dean@179
   962
                // Show the context menu
Dean@179
   963
                if (showContextMenu)
Dean@179
   964
                {
Dean@179
   965
                    this.ContextMenuEncryptionStatus.Show(button.PointToScreen(e.Location));
vb@133
   966
                }
vb@133
   967
            }
Dean@179
   968
Dean@179
   969
            return;
vb@133
   970
        }
vb@133
   971
Dean@179
   972
        /// <summary>
Dean@179
   973
        /// Event handler for when the send unencrypted tool strip menu is clicked.
Dean@179
   974
        /// This will update whether the email is sent unencrypted or not.
Dean@179
   975
        /// </summary>
Dean@179
   976
        private void ToolStripMenuItemSendUnencrypted_Click(object sender, EventArgs e)
vb@133
   977
        {
Dean@179
   978
            // Toggle the checked state
Dean@179
   979
            this.ToolStripMenuItemSendUnencrypted.Checked = !this.ToolStripMenuItemSendUnencrypted.Checked;
Dean@179
   980
Dean@179
   981
            // Change the mail item based on UI state, also refresh
Dean@179
   982
            State state = this.CopyUIToState();
Dean@179
   983
            this.associatedMailItem.SendUnencrypted = state.SendUnencryptedCheckState;
Dean@179
   984
            this.CopyStateToUI(state);
Dean@179
   985
Dean@179
   986
            this.UpdateUIFromMailItem();
Dean@179
   987
Dean@179
   988
            return;
Dean@179
   989
        }
Dean@179
   990
Dean@179
   991
        /// <summary>
Dean@179
   992
        /// Event handler for when the explanation link label is clicked.
Dean@179
   993
        /// This will show detailed information for the current encryption state.
Dean@179
   994
        /// </summary>
Dean@179
   995
        private void LinkLabelExplanation_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
Dean@179
   996
        {
Dean@179
   997
            State state = this.CopyUIToState();
Dean@179
   998
Dean@179
   999
            var result = MessageBox.Show(
Dean@179
  1000
                            state.Explanation + "\n\n" + state.Suggestion,
Dean@179
  1001
                            state.Title,
Dean@179
  1002
                            MessageBoxButtons.OK,
Dean@179
  1003
                            state.EncryptionStatus == _pEp_color.pEp_rating_under_attack
Dean@179
  1004
                                ? MessageBoxIcon.Stop
Dean@179
  1005
                                : MessageBoxIcon.Information
Dean@179
  1006
                        );
Dean@179
  1007
Dean@179
  1008
            return;
vb@133
  1009
        }
Dean@151
  1010
Dean@151
  1011
        /// <summary>
Dean@151
  1012
        /// Event handler for when the options picture box is clicked.
Dean@151
  1013
        /// This will open the options form for user editing.
Dean@151
  1014
        /// </summary>
Dean@151
  1015
        private void PictureBoxOptions_Click(object sender, EventArgs e)
Dean@151
  1016
        {
Dean@159
  1017
            DialogResult result;
Dean@152
  1018
            FormOptions frm = new FormOptions();
Dean@154
  1019
            frm.StartPosition = FormStartPosition.CenterScreen;
Dean@159
  1020
            result = frm.ShowDialog(this.ParentForm); // Must show as dialog to block code
Dean@151
  1021
Dean@154
  1022
            if (ThisAddIn.addIn.use_keyserver)
Dean@154
  1023
                ThisAddIn.pEp.start_keyserver_lookup();
Dean@154
  1024
            else
Dean@154
  1025
                ThisAddIn.pEp.stop_keyserver_lookup();
Dean@151
  1026
Dean@151
  1027
            return;
Dean@151
  1028
        }
Dean@151
  1029
Dean@151
  1030
        /// <summary>
Dean@151
  1031
        /// Event handler for when the mouse hovers over the options picture box.
Dean@151
  1032
        /// This is a work-around to show a tool tip.
Dean@151
  1033
        /// </summary>
Dean@151
  1034
        private void PictureBoxOptions_MouseHover(object sender, EventArgs e)
Dean@151
  1035
        {
Dean@151
  1036
            ToolTip toolTip = new ToolTip();
Dean@174
  1037
            toolTip.SetToolTip(this.PictureBoxOptions, "Click to open options"); // TODO: translate me
Dean@151
  1038
            return;
Dean@151
  1039
        }
Dean@166
  1040
Dean@166
  1041
        /**************************************************************
Dean@166
  1042
         * 
Dean@166
  1043
         * Sub-classes
Dean@166
  1044
         * 
Dean@166
  1045
         *************************************************************/
Dean@166
  1046
Dean@166
  1047
        /// <summary>
Dean@166
  1048
        /// Class used to store the state of the encryption status panel.
Dean@166
  1049
        /// </summary>
Dean@166
  1050
        public class State
Dean@166
  1051
        {
Dean@166
  1052
            private _pEp_color _EncryptionStatus;
Dean@179
  1053
            private CheckState _SendUnencryptedCheckState;
Dean@166
  1054
Dean@166
  1055
            private string _Title;
Dean@166
  1056
            private string _Explanation;
Dean@166
  1057
            private string _Suggestion;
Dean@166
  1058
            private Color  _ButtonBackground;
Dean@166
  1059
            private Color  _ButtonForeground;
Dean@166
  1060
            private string _ButtonText;
Dean@166
  1061
Dean@166
  1062
            /**************************************************************
Dean@166
  1063
             * 
Dean@166
  1064
             * Constructors
Dean@166
  1065
             * 
Dean@166
  1066
             *************************************************************/
Dean@166
  1067
Dean@166
  1068
            /// <summary>
Dean@166
  1069
            /// Default constructor.
Dean@166
  1070
            /// </summary>
Dean@166
  1071
            public State()
Dean@166
  1072
            {
Dean@166
  1073
                this.Reset();
Dean@166
  1074
            }
Dean@166
  1075
Dean@166
  1076
            /**************************************************************
Dean@166
  1077
             * 
Dean@166
  1078
             * Property Accessors
Dean@166
  1079
             * 
Dean@166
  1080
             *************************************************************/
Dean@166
  1081
Dean@166
  1082
            /// <summary>
Dean@166
  1083
            /// Gets or sets the encryption status as a pEp color rating.
Dean@166
  1084
            /// </summary>
Dean@166
  1085
            public _pEp_color EncryptionStatus
Dean@166
  1086
            {
Dean@166
  1087
                get { return (this._EncryptionStatus); }
Dean@166
  1088
                set 
Dean@166
  1089
                { 
Dean@166
  1090
                    this._EncryptionStatus = value;
Dean@179
  1091
                    this.UpdateDependentProperties();
Dean@166
  1092
                }
Dean@166
  1093
            }
Dean@166
  1094
Dean@166
  1095
            /// <summary>
Dean@179
  1096
            /// Gets or sets whether the send unencrypted checkbox is checked by the user.
Dean@179
  1097
            /// </summary>
Dean@179
  1098
            public CheckState SendUnencryptedCheckState
Dean@179
  1099
            {
Dean@179
  1100
                get { return (this._SendUnencryptedCheckState); }
Dean@179
  1101
                set { this._SendUnencryptedCheckState = value; }
Dean@179
  1102
            }
Dean@179
  1103
Dean@179
  1104
            /// <summary>
Dean@166
  1105
            /// Gets the title of the encryption status.
Dean@166
  1106
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1107
            /// </summary>
Dean@166
  1108
            public string Title
Dean@166
  1109
            {
Dean@166
  1110
                get { return (this._Title); }
Dean@166
  1111
            }
Dean@166
  1112
Dean@166
  1113
            /// <summary>
Dean@166
  1114
            /// Gets the explanation for the encryption status.
Dean@166
  1115
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1116
            /// </summary>
Dean@166
  1117
            public string Explanation
Dean@166
  1118
            {
Dean@166
  1119
                get { return (this._Explanation); }
Dean@166
  1120
            }
Dean@166
  1121
Dean@166
  1122
            /// <summary>
Dean@166
  1123
            /// Gets the suggestion to improve the encryption status.
Dean@166
  1124
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1125
            /// </summary>
Dean@166
  1126
            public string Suggestion
Dean@166
  1127
            {
Dean@166
  1128
                get { return (this._Suggestion); }
Dean@166
  1129
            }
Dean@166
  1130
Dean@166
  1131
            /// <summary>
Dean@166
  1132
            /// Gets the background color of the encryption button.
Dean@166
  1133
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1134
            /// </summary>
Dean@166
  1135
            public Color ButtonBackground
Dean@166
  1136
            {
Dean@166
  1137
                get { return (this._ButtonBackground); }
Dean@166
  1138
            }
Dean@166
  1139
Dean@166
  1140
            /// <summary>
Dean@166
  1141
            /// Gets the foreground color of the encryption button.
Dean@166
  1142
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1143
            /// </summary>
Dean@166
  1144
            public Color ButtonForeground
Dean@166
  1145
            {
Dean@166
  1146
                get { return (this._ButtonForeground); }
Dean@166
  1147
            }
Dean@166
  1148
Dean@166
  1149
            /// <summary>
Dean@166
  1150
            /// Gets the text of the encryption button.
Dean@166
  1151
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1152
            /// </summary>
Dean@166
  1153
            public string ButtonText
Dean@166
  1154
            {
Dean@166
  1155
                get { return (this._ButtonText); }
Dean@166
  1156
            }
Dean@166
  1157
Dean@166
  1158
            /**************************************************************
Dean@166
  1159
             * 
Dean@166
  1160
             * Methods
Dean@166
  1161
             * 
Dean@166
  1162
             *************************************************************/
Dean@166
  1163
Dean@166
  1164
            /// <summary>
Dean@166
  1165
            /// Resets the object to it's default values.
Dean@166
  1166
            /// </summary>
Dean@166
  1167
            public void Reset()
Dean@166
  1168
            {
Dean@179
  1169
                this._EncryptionStatus          = _pEp_color.pEp_rating_undefined;
Dean@179
  1170
                this._SendUnencryptedCheckState = CheckState.Indeterminate;
Dean@179
  1171
                this.UpdateDependentProperties();
Dean@166
  1172
Dean@166
  1173
                return;
Dean@166
  1174
            }
Dean@166
  1175
Dean@166
  1176
            /// <summary>
Dean@166
  1177
            /// Updates any properties dependent on the encryption status.
Dean@166
  1178
            /// </summary>
Dean@179
  1179
            private void UpdateDependentProperties()
Dean@166
  1180
            {
Dean@166
  1181
                switch (this._EncryptionStatus)
Dean@166
  1182
                {
Dean@166
  1183
                    case _pEp_color.pEp_rating_trusted:
Dean@166
  1184
                    case _pEp_color.pEp_rating_trusted_and_anonymized:
Dean@166
  1185
                        this._Title       = pEp.Properties.Resources.RatingTrustedTitle;
Dean@166
  1186
                        this._Explanation = pEp.Properties.Resources.RatingTrustedExplanation;
Dean@166
  1187
                        this._Suggestion  = pEp.Properties.Resources.RatingTrustedSuggestion;
Dean@166
  1188
                        
Dean@166
  1189
                        this._ButtonBackground = COLOR_GREEN;
Dean@166
  1190
                        this._ButtonForeground = TEXT_COLOR_WHITE;
Dean@166
  1191
                        this._ButtonText       = pEp.Properties.Resources.RatingTrustedButtontext;
Dean@166
  1192
                        break;
Dean@166
  1193
Dean@166
  1194
                    case _pEp_color.pEp_rating_reliable:
Dean@166
  1195
                        this._Title       = pEp.Properties.Resources.RatingReliableTitle;
Dean@166
  1196
                        this._Explanation = pEp.Properties.Resources.RatingReliableExplaination;
Dean@166
  1197
                        this._Suggestion  = pEp.Properties.Resources.RatingReliableSuggestion;
Dean@166
  1198
                        
Dean@166
  1199
                        this._ButtonBackground = COLOR_YELLOW;
Dean@166
  1200
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1201
                        this._ButtonText       = pEp.Properties.Resources.RatingReliableButtontext;
Dean@166
  1202
                        break;
Dean@166
  1203
Dean@166
  1204
                    case _pEp_color.pEp_rating_unreliable:
Dean@166
  1205
                        this._Title       = pEp.Properties.Resources.RatingUnreliableTitle;
Dean@166
  1206
                        this._Explanation = pEp.Properties.Resources.RatingUnreliableExplanation;
Dean@166
  1207
                        this._Suggestion  = pEp.Properties.Resources.RatingUnreliableSuggestion;
Dean@166
  1208
                        
Dean@166
  1209
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1210
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1211
                        this._ButtonText       = pEp.Properties.Resources.RatingUnreliableButtontext;
Dean@166
  1212
                        break;
Dean@166
  1213
Dean@166
  1214
                    case _pEp_color.pEp_rating_unencrypted:
Dean@166
  1215
                        this._Title       = pEp.Properties.Resources.RatingUnencryptedTitle;
Dean@166
  1216
                        this._Explanation = pEp.Properties.Resources.RatingUnencryptedExplanation;
Dean@166
  1217
                        this._Suggestion  = pEp.Properties.Resources.RatingUnencryptedSuggestion;
Dean@166
  1218
                        
Dean@166
  1219
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1220
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1221
                        this._ButtonText       = pEp.Properties.Resources.RatingUnencryptedButtontext;
Dean@166
  1222
                        break;
Dean@166
  1223
Dean@166
  1224
                    case _pEp_color.pEp_rating_under_attack:
Dean@166
  1225
                        this._Title       = pEp.Properties.Resources.RatingUnderattackTitle;
Dean@166
  1226
                        this._Explanation = pEp.Properties.Resources.RatingUnderattackExplanation;
Dean@166
  1227
                        this._Suggestion  = pEp.Properties.Resources.RatingUnderattackSuggestion;
Dean@166
  1228
                        
Dean@166
  1229
                        this._ButtonBackground = COLOR_RED;
Dean@166
  1230
                        this._ButtonForeground = TEXT_COLOR_WHITE;
Dean@166
  1231
                        this._ButtonText       = pEp.Properties.Resources.RatingUnderattackButtontext;
Dean@166
  1232
                        break;
Dean@166
  1233
Dean@166
  1234
                    case _pEp_color.pEp_rating_undefined:
Dean@166
  1235
                        this._Title       = pEp.Properties.Resources.RatingUndefinedTitle;
Dean@166
  1236
                        this._Explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
Dean@166
  1237
                        this._Suggestion  = pEp.Properties.Resources.RatingUndefinedSuggestion;
Dean@166
  1238
                        
Dean@166
  1239
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1240
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1241
                        this._ButtonText       = pEp.Properties.Resources.RatingUndefinedButtontext;
Dean@166
  1242
                        break;
Dean@166
  1243
Dean@166
  1244
                    case _pEp_color.pEp_rating_cannot_decrypt:
Dean@166
  1245
                    case _pEp_color.pEp_rating_have_no_key:
Dean@166
  1246
                        this._Title       = pEp.Properties.Resources.RatingHavenokeyTitle;
Dean@166
  1247
                        this._Explanation = pEp.Properties.Resources.RatingHavenokeyExplanation;
Dean@166
  1248
                        this._Suggestion  = pEp.Properties.Resources.RatingHavenokeySuggestion;
Dean@166
  1249
                        
Dean@166
  1250
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1251
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1252
                        this._ButtonText       = pEp.Properties.Resources.RatingHavenokeyButtontext;
Dean@166
  1253
                        break;
Dean@166
  1254
Dean@166
  1255
                    default:
Dean@166
  1256
                        this._Title       = "";
Dean@166
  1257
                        this._Explanation = "";
Dean@166
  1258
                        this._Suggestion  = "";
Dean@166
  1259
                        
Dean@166
  1260
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1261
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1262
                        this._ButtonText       = "";
Dean@166
  1263
                        break;
Dean@166
  1264
                }
Dean@166
  1265
Dean@166
  1266
                return;
Dean@166
  1267
            }
Dean@166
  1268
Dean@166
  1269
        }
vb@133
  1270
    }
vb@133
  1271
}