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