EncryptionStatusPanel.cs
author Dean Looyengoed
Wed, 30 Dec 2015 21:49:50 +0100
changeset 190 a685c30e07d2
parent 189 40f4e17a5744
child 194 7ba9e4b15b82
permissions -rw-r--r--
Refactor/comment/partial rewrite of the ThisAddIn class.
Several static members (like ThisAddIn addIn) were removed.
Overal big cleanup but some items remain to be completed here.
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
            
Dean@190
   198
            Globals.ThisAddIn.LogVerbose("doHandshake");
Dean@190
   199
            Globals.ThisAddIn.LogVerbose(identityPartner.address);
vb@139
   200
vb@132
   201
            if (identityPartner.username == "")
vb@133
   202
                identityPartner.username = identityPartner.address;
Dean@190
   203
            Globals.ThisAddIn.LogVerbose(identityPartner.username);
vb@133
   204
dean@144
   205
            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
Dean@190
   206
            Globals.ThisAddIn.LogVerbose(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
Dean@190
   213
                foreach (Outlook.Account a in Globals.ThisAddIn.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 == "")
Dean@190
   221
                    address = Globals.ThisAddIn.Application.Session.Accounts[1].SmtpAddress;
vb@133
   222
Dean@190
   223
                me = Globals.ThisAddIn.mySelf(address);
vb@133
   224
            }
vb@133
   225
            else // outgoing
vb@133
   226
            {
Dean@190
   227
                Globals.ThisAddIn.LogVerbose("mailItem.from_address: " + this.associatedMailItem.FromAddress);
Dean@190
   228
                me = Globals.ThisAddIn.mySelf(this.associatedMailItem.FromAddress);
vb@133
   229
            }
vb@133
   230
Dean@190
   231
            Globals.ThisAddIn.LogVerbose(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
Dean@190
   237
            Globals.ThisAddIn.LogVerbose(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
Dean@190
   264
            Globals.ThisAddIn.LogVerbose("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
Dean@190
   271
            Globals.ThisAddIn.LogVerbose("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
            State state;
Dean@179
   521
Dean@173
   522
            this.SetAssociatedMailItem();
Dean@173
   523
Dean@189
   524
            // Connect events
Dean@189
   525
            if (this.associatedMailItem != null)
Dean@159
   526
            {
Dean@159
   527
                try
Dean@159
   528
                {
Dean@189
   529
                    this.associatedMailItem.PropertyChange += MailItem_PropertyChange;
Dean@179
   530
Dean@189
   531
                    if (this.associatedMailItem.IsInEncryptedStore &&
Dean@189
   532
                        this.associatedMailItem.IsPGPEncrypted) // TODO: previously, (omi.Sender != null) was also checked. Sender is broken under some circumstances though
Dean@159
   533
                    {
Dean@189
   534
                        this.associatedMailItem.Open += MailItem_Open;
Dean@159
   535
                    }
Dean@159
   536
                }
Dean@159
   537
                catch { }
Dean@179
   538
            }
Dean@159
   539
Dean@179
   540
            // Set the default UI state
Dean@179
   541
            state = new State();
Dean@179
   542
            state.EncryptionStatus = _pEp_color.pEp_rating_undefined;
Dean@179
   543
            this.CopyStateToUI(state);
Dean@179
   544
Dean@179
   545
            // Check if the mailitem has been dowloaded yet
Dean@179
   546
            if (this.associatedMailItem != null)
Dean@179
   547
            {
Dean@179
   548
                if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@159
   549
                {
Dean@179
   550
                    this.UpdateUIFromMailItem();
Dean@159
   551
                }
Dean@159
   552
                else
Dean@159
   553
                {
Dean@179
   554
                    this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@179
   555
Dean@179
   556
                    //this.makePreview(); // TODO: add this back?
Dean@179
   557
Dean@179
   558
                    // Start the refresh timer
Dean@179
   559
                    this.TimerRefresh.Interval = 50;
Dean@179
   560
                    this.TimerRefresh.Enabled = true;
Dean@159
   561
                }
Dean@159
   562
            }
Dean@179
   563
Dean@179
   564
            return;
Dean@159
   565
        }
Dean@159
   566
Dean@159
   567
        /// <summary>
Dean@159
   568
        /// Event handler for when the form region is closed.
Dean@159
   569
        /// </summary>
Dean@159
   570
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   571
        {
Dean@190
   572
            // Disconnect events
Dean@190
   573
            if (this.associatedMailItem != null)
Dean@179
   574
            {
Dean@179
   575
                try
Dean@179
   576
                {
Dean@190
   577
                    this.associatedMailItem.PropertyChange -= MailItem_PropertyChange;
Dean@190
   578
                    this.associatedMailItem.Open           -= MailItem_Open;
Dean@179
   579
                }
Dean@179
   580
                catch { }
Dean@179
   581
            }
Dean@179
   582
Dean@179
   583
            return;
Dean@179
   584
        }
Dean@179
   585
Dean@179
   586
        /// <summary>
Dean@179
   587
        /// Event handler called after the refresh timer has elapsed.
Dean@179
   588
        /// </summary>
Dean@179
   589
        private void TimerRefresh_Tick(object sender, EventArgs e)
Dean@179
   590
        {
Dean@179
   591
            this.TimerRefresh.Enabled = false; // Only once
Dean@179
   592
            
Dean@179
   593
            // Ensure the tick method is not called more than once
Dean@179
   594
            if (refreshOngoing == false)
Dean@179
   595
            {
Dean@179
   596
                this.refreshOngoing = true;
Dean@179
   597
Dean@179
   598
                if (this.associatedMailItem != null)
Dean@179
   599
                {
Dean@179
   600
                    if (this.associatedMailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@179
   601
                    {
Dean@179
   602
                        this.UpdateUIFromMailItem();
Dean@179
   603
                    }
Dean@179
   604
                    else
Dean@179
   605
                    {
Dean@179
   606
                        // Set the timer to refresh again later automatically
Dean@179
   607
                        this.associatedMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@179
   608
                        this.TimerRefresh.Interval = 100;
Dean@179
   609
                        this.TimerRefresh.Enabled = true;
Dean@179
   610
                    }
Dean@179
   611
                }
Dean@179
   612
Dean@179
   613
                this.refreshOngoing = false;
Dean@179
   614
            }
Dean@175
   615
Dean@159
   616
            return;
Dean@159
   617
        }
Dean@159
   618
Dean@169
   619
        /// <summary>
Dean@169
   620
        /// Event handler for when a mail item is being opened in an inspector.
Dean@180
   621
        /// See: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx
Dean@169
   622
        /// </summary>
Dean@169
   623
        /// <param name="cancel">Whether to cancel the event: Value is False when the event occurs. 
Dean@169
   624
        /// If the event procedure sets this argument to True, the open operation is not completed 
Dean@169
   625
        /// and the inspector is not displayed.</param>
Dean@169
   626
        private void MailItem_Open(ref bool cancel)
Dean@159
   627
        {
Dean@179
   628
            // Cancel opening if the mirror is displayed instead
Dean@173
   629
            if (this.associatedMailItem != null && this.associatedMailItem.MirrorDisplay())
Dean@169
   630
            {
Dean@169
   631
                cancel = true;
Dean@169
   632
            }
Dean@169
   633
Dean@169
   634
            return;
Dean@159
   635
        }
Dean@159
   636
Dean@169
   637
        /// <summary>
Dean@169
   638
        /// Event handler for when a mail item property is changed.
Dean@169
   639
        /// See: https://msdn.microsoft.com/en-us/library/office/ff866739.aspx
Dean@169
   640
        /// </summary>
Dean@169
   641
        /// <param name="propertyName">The name of the property that was changed.</param>
Dean@169
   642
        private void MailItem_PropertyChange(string propertyName)
Dean@159
   643
        {
Dean@179
   644
            switch (propertyName.ToUpper())
Dean@159
   645
            {
Dean@179
   646
                case "TO":
Dean@179
   647
                    // Start the refresh timer
Dean@179
   648
                    this.TimerRefresh.Enabled = true;
Dean@159
   649
                    break;
Dean@159
   650
                // Outlook bug: there are always both events, so one is enough
Dean@159
   651
                //case "CC":
Dean@179
   652
                //    // Start the refresh timer
Dean@179
   653
                //    this.TimerRefresh.Enabled = true;
Dean@159
   654
                //    break;
Dean@159
   655
            }
Dean@169
   656
Dean@169
   657
            return;
Dean@159
   658
        }
Dean@159
   659
Dean@179
   660
        /// <summary>
Dean@179
   661
        /// Event handler for when the encryption status button is clicked.
Dean@179
   662
        /// </summary>
Dean@178
   663
        private void ButtonEncryptionStatus_Click(object sender, EventArgs e)
vb@133
   664
        {
Dean@179
   665
            string text;
Dean@179
   666
            string reason;
Dean@179
   667
            string[] keys;
Dean@179
   668
            State state = this.CopyUIToState();
Dean@179
   669
            
Dean@179
   670
            switch (state.EncryptionStatus)
vb@133
   671
            {
vb@133
   672
                case _pEp_color.pEp_rating_green:
vb@133
   673
                    try
vb@133
   674
                    {
Dean@179
   675
                        keys = this.associatedMailItem.Keylist;
Dean@179
   676
Dean@179
   677
                        if (keys.Length > 0)
vb@133
   678
                        {
Dean@179
   679
                            if (keys.Length > 1)
Dean@179
   680
                            {
Dean@179
   681
                                text = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageMultiple + " {0}.",
Dean@179
   682
                                                     String.Join(", ", keys));
Dean@179
   683
                            }
vb@133
   684
                            else
Dean@179
   685
                            {
Dean@179
   686
                                text = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageSingle + " {0}.", 
Dean@179
   687
                                                     keys[0]);
Dean@179
   688
                            }
Dean@179
   689
Dean@179
   690
                            // Display all keys the message is encrypted with
Dean@179
   691
                            MessageBox.Show(text, 
Dean@179
   692
                                            pEp.Properties.Resources.EncryptionStatusClickMessageBox, 
Dean@179
   693
                                            MessageBoxButtons.OK, 
Dean@179
   694
                                            MessageBoxIcon.Information);
vb@133
   695
                        }
vb@133
   696
                    }
vb@133
   697
                    catch (Exception ex)
vb@133
   698
                    {
vb@133
   699
                        StopHere(ex.ToString());
vb@133
   700
                    }
vb@133
   701
                    break;
vb@133
   702
vb@133
   703
                case _pEp_color.pEp_rating_yellow:
vb@133
   704
                    try
vb@133
   705
                    {
Dean@173
   706
                        if (this.associatedMailItem.IsIncoming)
vb@133
   707
                        {
vb@133
   708
                            pEp_identity_s identityPartner = new pEp_identity_s();
Dean@173
   709
                            identityPartner.address = this.associatedMailItem.FromAddress;
Dean@173
   710
                            identityPartner.username = this.associatedMailItem.FromUsername;
dean@144
   711
                            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   712
vb@133
   713
                            try
vb@133
   714
                            {
vb@133
   715
                                identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   716
                            }
vb@133
   717
                            catch
vb@133
   718
                            {
Dean@179
   719
                                state.EncryptionStatus = _pEp_color.pEp_rating_undefined;
Dean@179
   720
                                this.CopyStateToUI(state);
vb@133
   721
                                return;
vb@133
   722
                            }
Dean@179
   723
                            this.DoHandshake(identityPartner);
vb@133
   724
                        }
vb@133
   725
                        else // outgoing
vb@133
   726
                        {
Dean@190
   727
                            Globals.ThisAddIn.LogVerbose("yellow, handshake initiated");
vb@139
   728
vb@133
   729
                            List<pEp_identity_s> partnersToCheck = new List<pEp_identity_s>();
vb@133
   730
Dean@173
   731
                            foreach (pEp_identity_s identity in this.associatedMailItem.Addresses)
vb@133
   732
                            {
Dean@190
   733
                                Globals.ThisAddIn.LogVerbose("adding " + identity.username + " <" + identity.address + ">");
vb@139
   734
vb@133
   735
                                try
vb@133
   736
                                {
vb@133
   737
                                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(identity);
vb@133
   738
                                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_unconfirmed_encryption
vb@133
   739
                                        && identityPartner.comm_type < _pEp_comm_type.pEp_ct_confirmed_encryption)
vb@133
   740
                                    {
vb@133
   741
                                        partnersToCheck.Add(identityPartner);
vb@133
   742
                                    }
vb@133
   743
                                }
vb@133
   744
                                catch
vb@133
   745
                                {
vb@133
   746
                                }
vb@133
   747
                            }
vb@133
   748
vb@133
   749
                            if (partnersToCheck.Count == 0)
vb@133
   750
                            {
Dean@179
   751
                                state.EncryptionStatus = _pEp_color.pEp_rating_undefined;
Dean@179
   752
                                this.CopyStateToUI(state);
vb@133
   753
                                return;
vb@133
   754
                            }
vb@133
   755
Dean@173
   756
                            pEp_identity_s[] addresses = this.associatedMailItem.Addresses;
vb@133
   757
vb@133
   758
                            if (addresses.Length == 1)
vb@133
   759
                            {
Dean@179
   760
                                this.DoHandshake(partnersToCheck[0]);
vb@133
   761
                            }
vb@133
   762
                            else if (addresses.Length > 1)
vb@133
   763
                            {
Dean@178
   764
                                ContextMenuHandshake.Items.Clear();
Dean@179
   765
                                foreach (pEp_identity_s identity in partnersToCheck) 
Dean@179
   766
                                {
Dean@179
   767
                                    text = identity.username + " <" + identity.address + ">";
Dean@179
   768
                                    ContextMenuHandshake.Items.Add(text, 
Dean@179
   769
                                                                   null, 
Dean@179
   770
                                                                   (x, y) => { this.DoHandshake(identity); });
vb@133
   771
                                }
Dean@178
   772
                                ContextMenuHandshake.Show(ButtonEncryptionStatus, 10, 10);
vb@133
   773
                            }
vb@133
   774
                        }
vb@133
   775
                    }
vb@133
   776
                    catch (COMException ex)
vb@133
   777
                    {
vb@133
   778
                        StopHere(ex.ToString());
vb@133
   779
                    }
vb@133
   780
                    catch (Exception ex)
vb@133
   781
                    {
vb@133
   782
                        StopHere(ex.ToString());
vb@133
   783
                    }
vb@133
   784
                    break;
vb@133
   785
vb@133
   786
                case _pEp_color.pEp_rating_unencrypted:
Dean@173
   787
                    if (this.associatedMailItem.IsIncoming)
vb@133
   788
                    {
vb@133
   789
                        MessageBox.Show(pEp.Properties.Resources.RatingUnencryptedMessageboxReasonBody, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   790
                    }
vb@133
   791
                    else
vb@133
   792
                    {
vb@133
   793
                        try
vb@133
   794
                        {
Dean@179
   795
                            reason = "";
Dean@173
   796
                            foreach (pEp_identity_s identity in this.associatedMailItem.Addresses)
vb@133
   797
                            {
vb@133
   798
                                if (identity.comm_type < _pEp_comm_type.pEp_ct_security_by_obscurity)
vb@133
   799
                                {
vb@133
   800
                                    if (reason != "")
vb@133
   801
                                        reason += "\n";
vb@133
   802
                                    // BUG: reason string in English language only and trivial
vb@133
   803
                                    string ct = identity.comm_type.ToString();
vb@133
   804
                                    ct = new string(ct.Skip(7).ToArray());
vb@133
   805
                                    ct = ct.Replace("_", " ");
vb@133
   806
                                    reason += identity.username + " <" + identity.address + ">: " + ct;
vb@133
   807
                                }
vb@133
   808
                            }
vb@133
   809
                            if (reason != "")
vb@133
   810
                                MessageBox.Show(reason, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   811
                        }
vb@133
   812
                        catch (COMException ex)
vb@133
   813
                        {
vb@133
   814
                            StopHere(ex.ToString());
vb@133
   815
                        }
vb@133
   816
                    }
vb@133
   817
                    break;
vb@133
   818
vb@133
   819
                case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   820
                case _pEp_color.pEp_rating_have_no_key:
vb@133
   821
                    try
vb@133
   822
                    {
Dean@179
   823
                        keys = this.associatedMailItem.Keylist;
vb@133
   824
vb@133
   825
                        if (keys.Length > 1)
vb@133
   826
                        {
vb@133
   827
                            if (keys.Length == 2)
Dean@179
   828
                            {
vb@133
   829
                                reason = pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodyNokey;
Dean@179
   830
                            }
vb@133
   831
                            else
Dean@179
   832
                            {
Dean@179
   833
                                reason = string.Format(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodySomekey, 
Dean@179
   834
                                                       keys.Length - 1);
Dean@179
   835
                            }
vb@133
   836
vb@133
   837
                            string keyinfo = "";
vb@133
   838
vb@133
   839
                            foreach (string key in keys)
vb@133
   840
                            {
vb@133
   841
                                if (key.Length >= 8)
vb@133
   842
                                    keyinfo += "\n" + key;
vb@133
   843
                            }
vb@133
   844
vb@133
   845
                            reason += keyinfo;
vb@133
   846
Dean@179
   847
                            MessageBox.Show(reason, 
Dean@179
   848
                                            pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   849
                        }
vb@133
   850
                        else if (keys.Length == 1)
vb@133
   851
                        {
Dean@179
   852
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonError + keys[0], 
Dean@179
   853
                                            pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   854
                        }
vb@133
   855
                        else
vb@133
   856
                        {
Dean@179
   857
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonUnknownformat, 
Dean@179
   858
                                            pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   859
                        }
vb@133
   860
                    }
vb@133
   861
                    catch (COMException ex)
vb@133
   862
                    {
vb@133
   863
                        StopHere(ex.ToString());
vb@133
   864
                    }
vb@133
   865
                    break;
vb@133
   866
                    
vb@133
   867
                case _pEp_color.pEp_rating_undefined:
vb@133
   868
                    break;
vb@133
   869
            }
Dean@179
   870
Dean@179
   871
            return;
vb@133
   872
        }
vb@133
   873
Dean@179
   874
        /// <summary>
Dean@179
   875
        /// Event handler for when a mouse button is released over the encryption status button.
Dean@179
   876
        /// This will show the context menu.
Dean@179
   877
        /// </summary>
Dean@179
   878
        private void ButtonEncryptionStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   879
        {
Dean@179
   880
            bool showContextMenu = false;
Dean@179
   881
            string text;
Dean@179
   882
            Button button;
vb@133
   883
Dean@179
   884
            if ((sender != null) &&
Dean@179
   885
                (e.Button == System.Windows.Forms.MouseButtons.Right))
Dean@179
   886
            { 
Dean@179
   887
                button = (Button)sender;
Dean@179
   888
                
Dean@179
   889
                // Remove all tool strip menu items except the first, which should be send unencrypted
Dean@179
   890
                while (this.ContextMenuEncryptionStatus.Items.Count > 1)
Dean@179
   891
                {
Dean@179
   892
                    this.ContextMenuEncryptionStatus.Items.RemoveAt(1);
Dean@179
   893
                }
vb@133
   894
Dean@179
   895
                // Enable the send unencrypted menu item
Dean@173
   896
                if (this.associatedMailItem != null && 
Dean@173
   897
                    !this.associatedMailItem.IsIncoming && 
Dean@173
   898
                    !this.associatedMailItem.IsInSentFolder && 
Dean@173
   899
                    this.associatedMailItem.OutgoingColor > _pEp_color.pEp_rating_unencrypted)
vb@133
   900
                {
Dean@179
   901
                    this.ToolStripMenuItemSendUnencrypted.Visible = true;
Dean@179
   902
                    showContextMenu = true;
Dean@179
   903
                }
Dean@179
   904
                else
Dean@179
   905
                {
Dean@179
   906
                    this.ToolStripMenuItemSendUnencrypted.Visible = false;
Dean@179
   907
                }
Dean@179
   908
Dean@179
   909
                // Build and enable any options to untrust identities
Dean@179
   910
                if (this.associatedMailItem != null)
Dean@179
   911
                {
Dean@179
   912
                    if (this.associatedMailItem.IsIncoming)
vb@133
   913
                    {
Dean@179
   914
                        // Create and update identity of the sender
Dean@179
   915
                        pEp_identity_s ident;
Dean@179
   916
                        ident          = new pEp_identity_s();
Dean@179
   917
                        ident.address  = this.associatedMailItem.FromAddress;
Dean@179
   918
                        ident.username = this.associatedMailItem.FromUsername;
Dean@179
   919
                        ident.user_id  = CryptableMailItem.GetUserIDFromAddress(ident.address, ident.username);
Dean@179
   920
                        ident = ThisAddIn.pEp.update_identity(ident);
Dean@179
   921
Dean@179
   922
                        text = pEp.Properties.Resources.IdentityTrustText + 
Dean@179
   923
                               ident.username + " <" + ident.address + ">";
Dean@179
   924
Dean@179
   925
                        this.ContextMenuEncryptionStatus.Items.Add(text, 
Dean@179
   926
                                                                   null, 
Dean@179
   927
                                                                   (x, y) => { this.UndoHandshake(ident); });
vb@133
   928
                    }
Dean@179
   929
                    else
Dean@173
   930
                    {
Dean@179
   931
                        foreach (pEp_identity_s ident in this.associatedMailItem.Addresses)
Dean@179
   932
                        {
Dean@179
   933
                            pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(ident);
Dean@179
   934
Dean@179
   935
                            if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@179
   936
                            {
Dean@179
   937
                                text = pEp.Properties.Resources.IdentityTrustText + 
Dean@179
   938
                                       ident.username + " <" + ident.address + ">";
Dean@179
   939
Dean@179
   940
                                this.ContextMenuEncryptionStatus.Items.Add(text, 
Dean@179
   941
                                                                           null, 
Dean@179
   942
                                                                           (x, y) => { this.UndoHandshake(ident); });
Dean@179
   943
                            }
Dean@179
   944
                        }
vb@133
   945
                    }
vb@133
   946
Dean@179
   947
                    showContextMenu = true;
Dean@179
   948
                }
Dean@179
   949
Dean@179
   950
                // Show the context menu
Dean@179
   951
                if (showContextMenu)
Dean@179
   952
                {
Dean@179
   953
                    this.ContextMenuEncryptionStatus.Show(button.PointToScreen(e.Location));
vb@133
   954
                }
vb@133
   955
            }
Dean@179
   956
Dean@179
   957
            return;
vb@133
   958
        }
vb@133
   959
Dean@179
   960
        /// <summary>
Dean@179
   961
        /// Event handler for when the send unencrypted tool strip menu is clicked.
Dean@179
   962
        /// This will update whether the email is sent unencrypted or not.
Dean@179
   963
        /// </summary>
Dean@179
   964
        private void ToolStripMenuItemSendUnencrypted_Click(object sender, EventArgs e)
vb@133
   965
        {
Dean@180
   966
            /* Note: This portion of the code follows convention for easier maintainabilty.
Dean@180
   967
             * It is not the most simple approach -- however, please don't simplify without consideration.
Dean@180
   968
             */
Dean@180
   969
Dean@179
   970
            // Toggle the checked state
Dean@179
   971
            this.ToolStripMenuItemSendUnencrypted.Checked = !this.ToolStripMenuItemSendUnencrypted.Checked;
Dean@179
   972
Dean@179
   973
            // Change the mail item based on UI state, also refresh
Dean@179
   974
            State state = this.CopyUIToState();
Dean@179
   975
            this.associatedMailItem.SendUnencrypted = state.SendUnencryptedCheckState;
Dean@179
   976
            this.CopyStateToUI(state);
Dean@179
   977
Dean@179
   978
            this.UpdateUIFromMailItem();
Dean@179
   979
Dean@179
   980
            return;
Dean@179
   981
        }
Dean@179
   982
Dean@179
   983
        /// <summary>
Dean@179
   984
        /// Event handler for when the explanation link label is clicked.
Dean@179
   985
        /// This will show detailed information for the current encryption state.
Dean@179
   986
        /// </summary>
Dean@179
   987
        private void LinkLabelExplanation_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
Dean@179
   988
        {
Dean@179
   989
            State state = this.CopyUIToState();
Dean@179
   990
Dean@179
   991
            var result = MessageBox.Show(
Dean@179
   992
                            state.Explanation + "\n\n" + state.Suggestion,
Dean@179
   993
                            state.Title,
Dean@179
   994
                            MessageBoxButtons.OK,
Dean@179
   995
                            state.EncryptionStatus == _pEp_color.pEp_rating_under_attack
Dean@179
   996
                                ? MessageBoxIcon.Stop
Dean@179
   997
                                : MessageBoxIcon.Information
Dean@179
   998
                        );
Dean@179
   999
Dean@179
  1000
            return;
vb@133
  1001
        }
Dean@151
  1002
Dean@151
  1003
        /// <summary>
Dean@151
  1004
        /// Event handler for when the options picture box is clicked.
Dean@151
  1005
        /// This will open the options form for user editing.
Dean@151
  1006
        /// </summary>
Dean@151
  1007
        private void PictureBoxOptions_Click(object sender, EventArgs e)
Dean@151
  1008
        {
Dean@159
  1009
            DialogResult result;
Dean@152
  1010
            FormOptions frm = new FormOptions();
Dean@154
  1011
            frm.StartPosition = FormStartPosition.CenterScreen;
Dean@159
  1012
            result = frm.ShowDialog(this.ParentForm); // Must show as dialog to block code
Dean@151
  1013
Dean@151
  1014
            return;
Dean@151
  1015
        }
Dean@151
  1016
Dean@151
  1017
        /// <summary>
Dean@151
  1018
        /// Event handler for when the mouse hovers over the options picture box.
Dean@151
  1019
        /// This is a work-around to show a tool tip.
Dean@151
  1020
        /// </summary>
Dean@151
  1021
        private void PictureBoxOptions_MouseHover(object sender, EventArgs e)
Dean@151
  1022
        {
Dean@151
  1023
            ToolTip toolTip = new ToolTip();
Dean@174
  1024
            toolTip.SetToolTip(this.PictureBoxOptions, "Click to open options"); // TODO: translate me
Dean@151
  1025
            return;
Dean@151
  1026
        }
Dean@166
  1027
Dean@166
  1028
        /**************************************************************
Dean@166
  1029
         * 
Dean@166
  1030
         * Sub-classes
Dean@166
  1031
         * 
Dean@166
  1032
         *************************************************************/
Dean@166
  1033
Dean@166
  1034
        /// <summary>
Dean@166
  1035
        /// Class used to store the state of the encryption status panel.
Dean@166
  1036
        /// </summary>
Dean@166
  1037
        public class State
Dean@166
  1038
        {
Dean@166
  1039
            private _pEp_color _EncryptionStatus;
Dean@179
  1040
            private CheckState _SendUnencryptedCheckState;
Dean@166
  1041
Dean@166
  1042
            private string _Title;
Dean@166
  1043
            private string _Explanation;
Dean@166
  1044
            private string _Suggestion;
Dean@166
  1045
            private Color  _ButtonBackground;
Dean@166
  1046
            private Color  _ButtonForeground;
Dean@166
  1047
            private string _ButtonText;
Dean@166
  1048
Dean@166
  1049
            /**************************************************************
Dean@166
  1050
             * 
Dean@166
  1051
             * Constructors
Dean@166
  1052
             * 
Dean@166
  1053
             *************************************************************/
Dean@166
  1054
Dean@166
  1055
            /// <summary>
Dean@166
  1056
            /// Default constructor.
Dean@166
  1057
            /// </summary>
Dean@166
  1058
            public State()
Dean@166
  1059
            {
Dean@166
  1060
                this.Reset();
Dean@166
  1061
            }
Dean@166
  1062
Dean@166
  1063
            /**************************************************************
Dean@166
  1064
             * 
Dean@166
  1065
             * Property Accessors
Dean@166
  1066
             * 
Dean@166
  1067
             *************************************************************/
Dean@166
  1068
Dean@166
  1069
            /// <summary>
Dean@166
  1070
            /// Gets or sets the encryption status as a pEp color rating.
Dean@166
  1071
            /// </summary>
Dean@166
  1072
            public _pEp_color EncryptionStatus
Dean@166
  1073
            {
Dean@166
  1074
                get { return (this._EncryptionStatus); }
Dean@166
  1075
                set 
Dean@166
  1076
                { 
Dean@166
  1077
                    this._EncryptionStatus = value;
Dean@179
  1078
                    this.UpdateDependentProperties();
Dean@166
  1079
                }
Dean@166
  1080
            }
Dean@166
  1081
Dean@166
  1082
            /// <summary>
Dean@179
  1083
            /// Gets or sets whether the send unencrypted checkbox is checked by the user.
Dean@179
  1084
            /// </summary>
Dean@179
  1085
            public CheckState SendUnencryptedCheckState
Dean@179
  1086
            {
Dean@179
  1087
                get { return (this._SendUnencryptedCheckState); }
Dean@179
  1088
                set { this._SendUnencryptedCheckState = value; }
Dean@179
  1089
            }
Dean@179
  1090
Dean@179
  1091
            /// <summary>
Dean@166
  1092
            /// Gets the title of the encryption status.
Dean@166
  1093
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1094
            /// </summary>
Dean@166
  1095
            public string Title
Dean@166
  1096
            {
Dean@166
  1097
                get { return (this._Title); }
Dean@166
  1098
            }
Dean@166
  1099
Dean@166
  1100
            /// <summary>
Dean@166
  1101
            /// Gets the explanation for the encryption status.
Dean@166
  1102
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1103
            /// </summary>
Dean@166
  1104
            public string Explanation
Dean@166
  1105
            {
Dean@166
  1106
                get { return (this._Explanation); }
Dean@166
  1107
            }
Dean@166
  1108
Dean@166
  1109
            /// <summary>
Dean@166
  1110
            /// Gets the suggestion to improve the encryption status.
Dean@166
  1111
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1112
            /// </summary>
Dean@166
  1113
            public string Suggestion
Dean@166
  1114
            {
Dean@166
  1115
                get { return (this._Suggestion); }
Dean@166
  1116
            }
Dean@166
  1117
Dean@166
  1118
            /// <summary>
Dean@166
  1119
            /// Gets the background color of the encryption button.
Dean@166
  1120
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1121
            /// </summary>
Dean@166
  1122
            public Color ButtonBackground
Dean@166
  1123
            {
Dean@166
  1124
                get { return (this._ButtonBackground); }
Dean@166
  1125
            }
Dean@166
  1126
Dean@166
  1127
            /// <summary>
Dean@166
  1128
            /// Gets the foreground color of the encryption button.
Dean@166
  1129
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1130
            /// </summary>
Dean@166
  1131
            public Color ButtonForeground
Dean@166
  1132
            {
Dean@166
  1133
                get { return (this._ButtonForeground); }
Dean@166
  1134
            }
Dean@166
  1135
Dean@166
  1136
            /// <summary>
Dean@166
  1137
            /// Gets the text of the encryption button.
Dean@166
  1138
            /// This is dependent on EncryptionStatus and therefore automatically set.
Dean@166
  1139
            /// </summary>
Dean@166
  1140
            public string ButtonText
Dean@166
  1141
            {
Dean@166
  1142
                get { return (this._ButtonText); }
Dean@166
  1143
            }
Dean@166
  1144
Dean@166
  1145
            /**************************************************************
Dean@166
  1146
             * 
Dean@166
  1147
             * Methods
Dean@166
  1148
             * 
Dean@166
  1149
             *************************************************************/
Dean@166
  1150
Dean@166
  1151
            /// <summary>
Dean@166
  1152
            /// Resets the object to it's default values.
Dean@166
  1153
            /// </summary>
Dean@166
  1154
            public void Reset()
Dean@166
  1155
            {
Dean@179
  1156
                this._EncryptionStatus          = _pEp_color.pEp_rating_undefined;
Dean@179
  1157
                this._SendUnencryptedCheckState = CheckState.Indeterminate;
Dean@179
  1158
                this.UpdateDependentProperties();
Dean@166
  1159
Dean@166
  1160
                return;
Dean@166
  1161
            }
Dean@166
  1162
Dean@166
  1163
            /// <summary>
Dean@166
  1164
            /// Updates any properties dependent on the encryption status.
Dean@166
  1165
            /// </summary>
Dean@179
  1166
            private void UpdateDependentProperties()
Dean@166
  1167
            {
Dean@166
  1168
                switch (this._EncryptionStatus)
Dean@166
  1169
                {
Dean@166
  1170
                    case _pEp_color.pEp_rating_trusted:
Dean@166
  1171
                    case _pEp_color.pEp_rating_trusted_and_anonymized:
Dean@166
  1172
                        this._Title       = pEp.Properties.Resources.RatingTrustedTitle;
Dean@166
  1173
                        this._Explanation = pEp.Properties.Resources.RatingTrustedExplanation;
Dean@166
  1174
                        this._Suggestion  = pEp.Properties.Resources.RatingTrustedSuggestion;
Dean@166
  1175
                        
Dean@166
  1176
                        this._ButtonBackground = COLOR_GREEN;
Dean@166
  1177
                        this._ButtonForeground = TEXT_COLOR_WHITE;
Dean@166
  1178
                        this._ButtonText       = pEp.Properties.Resources.RatingTrustedButtontext;
Dean@166
  1179
                        break;
Dean@166
  1180
Dean@166
  1181
                    case _pEp_color.pEp_rating_reliable:
Dean@166
  1182
                        this._Title       = pEp.Properties.Resources.RatingReliableTitle;
Dean@182
  1183
                        this._Explanation = pEp.Properties.Resources.RatingReliableExplanation;
Dean@166
  1184
                        this._Suggestion  = pEp.Properties.Resources.RatingReliableSuggestion;
Dean@166
  1185
                        
Dean@166
  1186
                        this._ButtonBackground = COLOR_YELLOW;
Dean@166
  1187
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1188
                        this._ButtonText       = pEp.Properties.Resources.RatingReliableButtontext;
Dean@166
  1189
                        break;
Dean@166
  1190
Dean@166
  1191
                    case _pEp_color.pEp_rating_unreliable:
Dean@166
  1192
                        this._Title       = pEp.Properties.Resources.RatingUnreliableTitle;
Dean@166
  1193
                        this._Explanation = pEp.Properties.Resources.RatingUnreliableExplanation;
Dean@166
  1194
                        this._Suggestion  = pEp.Properties.Resources.RatingUnreliableSuggestion;
Dean@166
  1195
                        
Dean@166
  1196
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1197
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1198
                        this._ButtonText       = pEp.Properties.Resources.RatingUnreliableButtontext;
Dean@166
  1199
                        break;
Dean@166
  1200
Dean@166
  1201
                    case _pEp_color.pEp_rating_unencrypted:
Dean@166
  1202
                        this._Title       = pEp.Properties.Resources.RatingUnencryptedTitle;
Dean@166
  1203
                        this._Explanation = pEp.Properties.Resources.RatingUnencryptedExplanation;
Dean@166
  1204
                        this._Suggestion  = pEp.Properties.Resources.RatingUnencryptedSuggestion;
Dean@166
  1205
                        
Dean@166
  1206
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1207
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1208
                        this._ButtonText       = pEp.Properties.Resources.RatingUnencryptedButtontext;
Dean@166
  1209
                        break;
Dean@166
  1210
Dean@166
  1211
                    case _pEp_color.pEp_rating_under_attack:
Dean@166
  1212
                        this._Title       = pEp.Properties.Resources.RatingUnderattackTitle;
Dean@166
  1213
                        this._Explanation = pEp.Properties.Resources.RatingUnderattackExplanation;
Dean@166
  1214
                        this._Suggestion  = pEp.Properties.Resources.RatingUnderattackSuggestion;
Dean@166
  1215
                        
Dean@166
  1216
                        this._ButtonBackground = COLOR_RED;
Dean@166
  1217
                        this._ButtonForeground = TEXT_COLOR_WHITE;
Dean@166
  1218
                        this._ButtonText       = pEp.Properties.Resources.RatingUnderattackButtontext;
Dean@166
  1219
                        break;
Dean@166
  1220
Dean@166
  1221
                    case _pEp_color.pEp_rating_undefined:
Dean@166
  1222
                        this._Title       = pEp.Properties.Resources.RatingUndefinedTitle;
Dean@166
  1223
                        this._Explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
Dean@166
  1224
                        this._Suggestion  = pEp.Properties.Resources.RatingUndefinedSuggestion;
Dean@166
  1225
                        
Dean@166
  1226
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1227
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1228
                        this._ButtonText       = pEp.Properties.Resources.RatingUndefinedButtontext;
Dean@166
  1229
                        break;
Dean@166
  1230
Dean@166
  1231
                    case _pEp_color.pEp_rating_cannot_decrypt:
Dean@166
  1232
                    case _pEp_color.pEp_rating_have_no_key:
Dean@166
  1233
                        this._Title       = pEp.Properties.Resources.RatingHavenokeyTitle;
Dean@166
  1234
                        this._Explanation = pEp.Properties.Resources.RatingHavenokeyExplanation;
Dean@166
  1235
                        this._Suggestion  = pEp.Properties.Resources.RatingHavenokeySuggestion;
Dean@166
  1236
                        
Dean@166
  1237
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1238
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1239
                        this._ButtonText       = pEp.Properties.Resources.RatingHavenokeyButtontext;
Dean@166
  1240
                        break;
Dean@166
  1241
Dean@166
  1242
                    default:
Dean@166
  1243
                        this._Title       = "";
Dean@166
  1244
                        this._Explanation = "";
Dean@166
  1245
                        this._Suggestion  = "";
Dean@166
  1246
                        
Dean@166
  1247
                        this._ButtonBackground = COLOR_GREY;
Dean@166
  1248
                        this._ButtonForeground = TEXT_COLOR_BLACK;
Dean@166
  1249
                        this._ButtonText       = "";
Dean@166
  1250
                        break;
Dean@166
  1251
                }
Dean@166
  1252
Dean@166
  1253
                return;
Dean@166
  1254
            }
Dean@166
  1255
Dean@166
  1256
        }
vb@133
  1257
    }
vb@133
  1258
}