EncryptionStatusPanel.cs
author Dean Looyengoed
Tue, 29 Dec 2015 22:48:49 +0100
changeset 173 cf2c9c426576
parent 172 60c6af7c94e5
child 174 b7eae6216fe1
permissions -rw-r--r--
Remove the mailItem property and replace with single associatedMailItem variable.

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