EncryptionStatusPanel.cs
author Dean Looyengoed
Tue, 29 Dec 2015 17:33:22 +0100
changeset 161 2b6b485152c5
parent 159 15d0050bc11a
child 162 5defdeba0056
permissions -rw-r--r--
Comment and rename quadruple method. Add null check just in case.
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
    {
vb@133
    18
        #region Form Region Factory
vb@133
    19
vb@133
    20
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
vb@133
    21
        [Microsoft.Office.Tools.Outlook.FormRegionName("pEp.FormRegion1")]
vb@133
    22
        public partial class FormRegion1Factory
vb@133
    23
        {
vb@133
    24
            // Occurs before the form region is initialized.
vb@133
    25
            // To prevent the form region from appearing, set e.Cancel to true.
vb@133
    26
            // Use e.OutlookItem to get a reference to the current Outlook item.
vb@133
    27
            private void FormRegion1Factory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
vb@133
    28
            {
vb@133
    29
            }
vb@133
    30
        }
vb@133
    31
vb@133
    32
        #endregion
vb@133
    33
Dean@159
    34
        private struct EncStatusData
vb@133
    35
        {
Dean@159
    36
            public string title;
Dean@159
    37
            public string explanation;
Dean@159
    38
            public string suggestion;
Dean@159
    39
            public Color background;
Dean@159
    40
            public Color foreground;
Dean@159
    41
            public string button_text;
Dean@159
    42
            public ContextMenuStrip strip;
Dean@159
    43
        };
Dean@159
    44
Dean@159
    45
        /* Notes:
Dean@159
    46
         * Use this.OutlookItem to get a reference to the current Outlook item.
Dean@159
    47
         * Use this.OutlookFormRegion to get a reference to the form region.
Dean@159
    48
         */
vb@133
    49
vb@133
    50
        private CryptableMailItem _mailItem = null;
Dean@159
    51
        private bool in_reset = false;
Dean@159
    52
        private EncStatusData enc_status_data;
Dean@159
    53
        private _pEp_color _enc_status;
Dean@159
    54
Dean@159
    55
        /**************************************************************
Dean@159
    56
         * 
Dean@159
    57
         * Property Accessors
Dean@159
    58
         * 
Dean@159
    59
         *************************************************************/
vb@133
    60
vb@133
    61
        CryptableMailItem mailItem
vb@133
    62
        {
vb@133
    63
            get
vb@133
    64
            {
vb@133
    65
                try
vb@133
    66
                {
vb@133
    67
                    if (this.OutlookItem == null)
vb@133
    68
                    {
vb@133
    69
                        _mailItem = null;
vb@133
    70
                        return null;
vb@133
    71
                    }
vb@133
    72
                }
vb@133
    73
                catch (COMException)
vb@133
    74
                {
vb@133
    75
                    return null;
vb@133
    76
                }
vb@133
    77
vb@133
    78
                Outlook.MailItem omi = null;
vb@133
    79
vb@133
    80
                try
vb@133
    81
                {
vb@133
    82
                    omi = (Outlook.MailItem)this.OutlookItem;
vb@133
    83
                }
vb@133
    84
                catch
vb@133
    85
                {
vb@133
    86
                    _mailItem = null;
vb@133
    87
                    return null;
vb@133
    88
                }
vb@133
    89
vb@133
    90
                Outlook.MAPIFolder folder = omi.Parent;
vb@136
    91
                Outlook.MAPIFolder deleted = folder.Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
vb@136
    92
                if (deleted != null && folder.EntryID == deleted.EntryID)
vb@133
    93
                {
vb@133
    94
                    _mailItem = null;
vb@133
    95
                    return null;
vb@133
    96
                }
vb@133
    97
vb@133
    98
                if (_mailItem == null) {
vb@133
    99
                    _mailItem = new CryptableMailItem(omi);
vb@133
   100
                }
vb@133
   101
                else
vb@133
   102
                {
vb@133
   103
                    if (_mailItem.EntryID != omi.EntryID)
vb@133
   104
                    {
vb@133
   105
                        _mailItem = new CryptableMailItem(omi);
vb@133
   106
                    }
vb@133
   107
                }
vb@133
   108
vb@133
   109
                return _mailItem;
vb@133
   110
            }
vb@133
   111
        }
vb@133
   112
vb@133
   113
        public _pEp_color enc_status {
vb@133
   114
            get
vb@133
   115
            {
vb@133
   116
                return _enc_status;
vb@133
   117
            }
vb@133
   118
            set
vb@133
   119
            {
vb@133
   120
                if (value != _enc_status)
vb@133
   121
                {
vb@133
   122
                    _enc_status = value;
vb@133
   123
vb@133
   124
                    switch (value)
vb@133
   125
                    {
vb@133
   126
                        case _pEp_color.pEp_rating_trusted:
vb@133
   127
                        case _pEp_color.pEp_rating_trusted_and_anonymized:
vb@133
   128
                            enc_status_data.explanation = pEp.Properties.Resources.RatingTrustedExplanation;
vb@133
   129
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingTrustedSuggestion;
vb@133
   130
                            enc_status_data.title = pEp.Properties.Resources.RatingTrustedTitle;
vb@133
   131
                            enc_status_data.background = Color.Green;
vb@133
   132
                            enc_status_data.foreground = Color.White;
vb@133
   133
                            enc_status_data.button_text = pEp.Properties.Resources.RatingTrustedButtontext;
vb@133
   134
                            enc_status_data.strip = new ContextMenuStrip();
vb@133
   135
                            enc_status_data.strip.Items.Add("");
vb@133
   136
                            enc_status_data.strip.Opening += secure_strip_Opening;
vb@133
   137
                            break;
vb@133
   138
vb@133
   139
                        case _pEp_color.pEp_rating_reliable:
vb@133
   140
                            enc_status_data.explanation = pEp.Properties.Resources.RatingReliableExplaination;
vb@133
   141
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingReliableSuggestion;
vb@133
   142
                            enc_status_data.title = pEp.Properties.Resources.RatingReliableTitle;
vb@133
   143
                            enc_status_data.background = Color.Gold;
vb@133
   144
                            enc_status_data.foreground = Color.Black;
vb@133
   145
                            enc_status_data.button_text = pEp.Properties.Resources.RatingReliableButtontext;
vb@133
   146
                            enc_status_data.strip = null;
vb@133
   147
                            break;
vb@133
   148
vb@133
   149
                        case _pEp_color.pEp_rating_unreliable:
vb@133
   150
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUnreliableExplanation;
vb@133
   151
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUnreliableSuggestion;
vb@133
   152
                            enc_status_data.title = pEp.Properties.Resources.RatingUnreliableTitle;
vb@133
   153
                            enc_status_data.background = Color.Gainsboro;
vb@133
   154
                            enc_status_data.foreground = Color.Black;
vb@133
   155
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUnreliableButtontext;
vb@133
   156
                            enc_status_data.strip = null;
vb@133
   157
                            break;
vb@133
   158
vb@133
   159
                        case _pEp_color.pEp_rating_unencrypted:
vb@133
   160
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUnencryptedExplanation;
vb@133
   161
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUnencryptedSuggestion;
vb@133
   162
                            enc_status_data.title = pEp.Properties.Resources.RatingUnencryptedTitle;
vb@133
   163
                            enc_status_data.background = Color.Gainsboro;
vb@133
   164
                            enc_status_data.foreground = Color.Black;
vb@133
   165
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUnencryptedButtontext;
vb@133
   166
                            enc_status_data.strip = null;
vb@133
   167
                            break;
vb@133
   168
vb@133
   169
                        case _pEp_color.pEp_rating_under_attack:
vb@133
   170
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUnderattackExplanation;
vb@133
   171
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUnderattackSuggestion;
vb@133
   172
                            enc_status_data.title = pEp.Properties.Resources.RatingUnderattackTitle;
vb@133
   173
                            enc_status_data.background = Color.Red;
vb@133
   174
                            enc_status_data.foreground = Color.White;
vb@133
   175
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUnderattackButtontext;
vb@133
   176
                            enc_status_data.strip = null;
vb@133
   177
                            break;
vb@133
   178
vb@133
   179
                        case _pEp_color.pEp_rating_undefined:
vb@133
   180
                            // will not happen
vb@133
   181
                            enc_status_data.explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
vb@133
   182
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingUndefinedSuggestion;
vb@133
   183
                            enc_status_data.title = pEp.Properties.Resources.RatingUndefinedTitle;
vb@133
   184
                            enc_status_data.background = Color.Gainsboro;
vb@133
   185
                            enc_status_data.foreground = Color.Black;
vb@133
   186
                            enc_status_data.button_text = pEp.Properties.Resources.RatingUndefinedButtontext;
vb@133
   187
                            enc_status_data.strip = null;
vb@133
   188
                            break;
vb@133
   189
vb@133
   190
                        case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   191
                        case _pEp_color.pEp_rating_have_no_key:
vb@133
   192
                            enc_status_data.explanation = pEp.Properties.Resources.RatingHavenokeyExplanation;
vb@133
   193
                            enc_status_data.suggestion = pEp.Properties.Resources.RatingHavenokeySuggestion;
vb@133
   194
                            enc_status_data.title = pEp.Properties.Resources.RatingHavenokeyTitle;
vb@133
   195
                            enc_status_data.background = Color.Gainsboro;
vb@133
   196
                            enc_status_data.foreground = Color.Black;
vb@133
   197
                            enc_status_data.button_text = pEp.Properties.Resources.RatingHavenokeyButtontext;
vb@133
   198
                            enc_status_data.strip = null;
vb@133
   199
                            break;
vb@133
   200
                    }
vb@133
   201
vb@133
   202
                    linkExplanation.Text = enc_status_data.title;
vb@133
   203
                    EncryptionStatus.BackColor = enc_status_data.background;
vb@133
   204
                    EncryptionStatus.ForeColor = enc_status_data.foreground;
vb@133
   205
                    EncryptionStatus.Text = enc_status_data.button_text;
vb@133
   206
                    EncryptionStatus.ContextMenuStrip = enc_status_data.strip;
vb@133
   207
                }
vb@133
   208
            }
vb@133
   209
        }
vb@133
   210
Dean@159
   211
        /**************************************************************
Dean@159
   212
         * 
Dean@159
   213
         * Methods
Dean@159
   214
         * 
Dean@159
   215
         *************************************************************/
Dean@159
   216
Dean@159
   217
        static void StopHere(string reason)
vb@133
   218
        {
Dean@159
   219
#if DEBUG
Dean@159
   220
            MessageBox.Show(reason);
Dean@159
   221
#else
Dean@159
   222
            try
vb@133
   223
            {
Dean@159
   224
                ThisAddIn.pEp.log("internal error", "pEp for Outlook", reason, "in EncryptionStatusPanel.cs");
vb@133
   225
            }
Dean@159
   226
            catch (COMException) { }
Dean@159
   227
            catch (Exception) { }
Dean@159
   228
#endif
vb@133
   229
        }
vb@133
   230
vb@133
   231
        private void doHandshake(pEp_identity_s identityPartner)
vb@133
   232
        {
Dean@155
   233
            string ownShort;
Dean@155
   234
            string ownLong;
Dean@155
   235
            string ownFpr;
Dean@155
   236
            string partnerShort;
Dean@155
   237
            string partnerLong;
Dean@155
   238
            string partnerFpr;
Dean@155
   239
            string[] lines;
Dean@155
   240
            pEp_identity_s me;
Dean@155
   241
            FormHandshake handshakeDialog;
Dean@155
   242
            FormHandshake.State state = new FormHandshake.State();
Dean@155
   243
dean@144
   244
            Debug.Assert(mailItem.ColorRating == _pEp_color.pEp_rating_yellow);
vb@132
   245
vb@139
   246
            ThisAddIn.verbose("doHandshake");
vb@139
   247
            ThisAddIn.verbose(identityPartner.address);
vb@139
   248
vb@132
   249
            if (identityPartner.username == "")
vb@133
   250
                identityPartner.username = identityPartner.address;
vb@139
   251
            ThisAddIn.verbose(identityPartner.username);
vb@133
   252
dean@144
   253
            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   254
            ThisAddIn.verbose(identityPartner.user_id);
vb@133
   255
dean@144
   256
            if (mailItem.IsIncoming)
vb@133
   257
            {
dean@144
   258
                string entryID = mailItem.ReceivedByEntryID;
vb@133
   259
                string address = "";
vb@133
   260
vb@133
   261
                foreach (Outlook.Account a in ThisAddIn.addIn.Application.Session.Accounts){
vb@133
   262
                    if (a.CurrentUser.EntryID == entryID) {
vb@133
   263
                        address = a.SmtpAddress;
vb@133
   264
                        break;
vb@133
   265
                    }
vb@133
   266
                }
vb@133
   267
vb@133
   268
                if (address == "")
vb@133
   269
                    address = ThisAddIn.addIn.Application.Session.Accounts[1].SmtpAddress;
vb@133
   270
vb@133
   271
                me = ThisAddIn.addIn.mySelf(address);
vb@133
   272
            }
vb@133
   273
            else // outgoing
vb@133
   274
            {
dean@144
   275
                ThisAddIn.verbose("mailItem.from_address: " + mailItem.FromAddress);
dean@144
   276
                me = ThisAddIn.addIn.mySelf(mailItem.FromAddress);
vb@133
   277
            }
vb@133
   278
vb@139
   279
            ThisAddIn.verbose(me.fpr);
vb@139
   280
Dean@155
   281
            ownShort = ThisAddIn.pEp.trustwords(me.fpr, max_words: 5).ToLower();
Dean@155
   282
            ownLong  = ThisAddIn.pEp.trustwords(me.fpr).ToLower();
Dean@161
   283
            ownFpr   = this.ToQuadruple(me.fpr);
vb@133
   284
vb@139
   285
            ThisAddIn.verbose(identityPartner.fpr);
vb@139
   286
Dean@155
   287
            partnerShort = ThisAddIn.pEp.trustwords(identityPartner.fpr, max_words: 5).ToLower();
Dean@155
   288
            partnerLong  = ThisAddIn.pEp.trustwords(identityPartner.fpr).ToLower();
Dean@161
   289
            partnerFpr   = this.ToQuadruple(identityPartner.fpr);
vb@133
   290
Dean@155
   291
            if (ownFpr.CompareTo(partnerFpr) > 0)
vb@133
   292
            {
Dean@155
   293
                state.TrustwordsShort = partnerShort + ownShort;
Dean@155
   294
                state.TrustwordsFull  = partnerLong  + ownLong;
Dean@155
   295
Dean@155
   296
                lines = new string[3];
Dean@155
   297
                lines[0] = pEp.Properties.Resources.TrustwordsPartnershortPartner + partnerFpr;
Dean@155
   298
                lines[2] = pEp.Properties.Resources.TrustwordsPartnershortMyself + ownFpr;
Dean@155
   299
                state.Fingerprint = lines;
vb@133
   300
            }
vb@133
   301
            else
vb@133
   302
            {
Dean@155
   303
                state.TrustwordsShort = ownShort + " " + partnerShort;
Dean@155
   304
                state.TrustwordsFull  = ownLong  + " " + partnerLong;
Dean@155
   305
Dean@155
   306
                lines = new string[3];
Dean@155
   307
                lines[0] = pEp.Properties.Resources.TrustwordsOwnshortMyself + ownFpr;
Dean@155
   308
                lines[2] = pEp.Properties.Resources.TrustwordsOwnshortPartner + partnerFpr;
Dean@155
   309
                state.Fingerprint = lines;
vb@133
   310
            }
vb@133
   311
vb@139
   312
            ThisAddIn.verbose("try to show dialog");
vb@139
   313
Dean@155
   314
            // Create and show handshake dialog
Dean@155
   315
            handshakeDialog = new FormHandshake(state);
Dean@155
   316
vb@132
   317
            DialogResult result = handshakeDialog.ShowDialog(this);
vb@133
   318
            ThisAddIn.verbose("handshakeDialog.ShowDialog(this) => " + result.ToString());
vb@133
   319
vb@133
   320
            switch (result)
vb@133
   321
            {
vb@133
   322
                case DialogResult.Yes:
dean@144
   323
                    mailItem.ColorRating = _pEp_color.pEp_rating_trusted;
vb@132
   324
                    identityPartner = ThisAddIn.pEp.trust_personal_key(identityPartner);
vb@133
   325
                    reset_enc_status(); 
vb@133
   326
                    break;
vb@133
   327
                case DialogResult.No:
vb@133
   328
                    ThisAddIn.pEp.key_compromized(identityPartner);
vb@133
   329
                    identityPartner.comm_type = _pEp_comm_type.pEp_ct_compromized;
dean@144
   330
                    mailItem.ColorRating = _pEp_color.pEp_rating_red;
vb@133
   331
                    reset_enc_status(); 
vb@133
   332
                    break;
vb@133
   333
            }
vb@133
   334
        }
vb@133
   335
vb@133
   336
        private void undoHandshake(pEp_identity_s identityPartner)
vb@133
   337
        {
vb@133
   338
            ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   339
//            Debug.Assert((identityPartner.comm_type & _pEp_comm_type.pEp_ct_confirmed) != _pEp_comm_type.pEp_ct_unknown);
vb@133
   340
            identityPartner.comm_type ^= _pEp_comm_type.pEp_ct_confirmed;
vb@133
   341
            ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   342
            reset_enc_status();
vb@133
   343
        }
vb@133
   344
Dean@161
   345
        /// <summary>
Dean@161
   346
        /// Formats the given text string as separated 4-character groups.
Dean@161
   347
        /// Example: 49422235FC99585B891C --> 4942 2235 FC99 585B 891C
Dean@161
   348
        /// </summary>
Dean@161
   349
        /// <param name="text">The text to format in 4-character groups.</param>
Dean@161
   350
        /// <returns>The re-formatted string.</returns>
Dean@161
   351
        private string ToQuadruple(string text)
Dean@159
   352
        {
Dean@159
   353
            List<string> result = new List<string>();
Dean@159
   354
Dean@161
   355
            if (text != null)
Dean@159
   356
            {
Dean@161
   357
                for (int i = 0; i < text.Length; i += 4)
Dean@159
   358
                {
Dean@161
   359
                    try
Dean@161
   360
                    {
Dean@161
   361
                        result.Add(text.Substring(i, 4));
Dean@161
   362
                    }
Dean@161
   363
                    catch (ArgumentOutOfRangeException)
Dean@161
   364
                    {
Dean@161
   365
                        result.Add(text.Substring(i));
Dean@161
   366
                        break;
Dean@161
   367
                    }
Dean@159
   368
                }
Dean@159
   369
            }
Dean@159
   370
Dean@159
   371
            return String.Join(" ", result);
Dean@159
   372
        }
Dean@159
   373
Dean@159
   374
        protected void makePreview()
Dean@159
   375
        {
Dean@159
   376
            WindowFormRegionCollection formRegions;
Dean@159
   377
Dean@159
   378
            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
Dean@159
   379
                return;
Dean@159
   380
            else if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
Dean@159
   381
                formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
Dean@159
   382
            else
Dean@159
   383
                return;
Dean@159
   384
Dean@159
   385
            try
Dean@159
   386
            {
Dean@159
   387
                if (formRegions.PreviewUnencrypted.Visible)
Dean@159
   388
                {
Dean@159
   389
                    byte[] rtf = mailItem.MirrorGetRTFBody();
Dean@159
   390
                    formRegions.PreviewUnencrypted.textPreview.Rtf = System.Text.Encoding.ASCII.GetString(rtf, 0, rtf.Length);
Dean@159
   391
                    formRegions.PreviewUnencrypted.textSubject.Text = mailItem.MirrorGetSubject();
Dean@159
   392
                    formRegions.PreviewUnencrypted.textFrom.Text = mailItem.FromUsername + " <" + mailItem.FromAddress + ">";
Dean@159
   393
                }
Dean@159
   394
            }
Dean@159
   395
            catch { }
Dean@159
   396
        }
Dean@159
   397
Dean@159
   398
        protected void reset_enc_status()
Dean@159
   399
        {
Dean@159
   400
            if (in_reset)
Dean@159
   401
                return;
Dean@159
   402
Dean@159
   403
            in_reset = true;
Dean@159
   404
Dean@159
   405
            if (mailItem != null)
Dean@159
   406
            {
Dean@159
   407
                if (_mailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@159
   408
                {
Dean@159
   409
                    if (_mailItem.SendUnencrypted == CheckState.Checked)
Dean@159
   410
                    {
Dean@159
   411
                        enc_status = _pEp_color.pEp_rating_unencrypted;
Dean@159
   412
                    }
Dean@159
   413
                    else
Dean@159
   414
                    {
Dean@159
   415
                        ThisAddIn.verbose("get_color in reset_enc_status");
Dean@159
   416
                        enc_status = _mailItem.ColorRating;
Dean@159
   417
                    }
Dean@159
   418
                }
Dean@159
   419
                else
Dean@159
   420
                {
Dean@159
   421
                    _mailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@159
   422
                    timerRefresh.Interval = 500;
Dean@159
   423
                    timerRefresh.Enabled = true;
Dean@159
   424
                }
Dean@159
   425
            }
Dean@159
   426
Dean@159
   427
            in_reset = false;
Dean@159
   428
        }
Dean@159
   429
Dean@159
   430
        /**************************************************************
Dean@159
   431
         * 
Dean@159
   432
         * Event Handling
Dean@159
   433
         * 
Dean@159
   434
         *************************************************************/
Dean@159
   435
Dean@159
   436
        /// <summary>
Dean@159
   437
        /// Event handler that is called before the form region is displayed.
Dean@159
   438
        /// </summary>
Dean@159
   439
        private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   440
        {
Dean@159
   441
            if (this.OutlookItem == null)
Dean@159
   442
            {
Dean@159
   443
                enc_status = _pEp_color.pEp_rating_undefined;
Dean@159
   444
            }
Dean@159
   445
            else
Dean@159
   446
            {
Dean@159
   447
                Outlook.MailItem omi = (Outlook.MailItem)this.OutlookItem;
Dean@159
   448
                try
Dean@159
   449
                {
Dean@159
   450
                    omi.PropertyChange += mailItem_PropertyChange;
Dean@159
   451
                    if (CryptableMailItem.IsEncryptedStore(omi))
Dean@159
   452
                    {
Dean@159
   453
                        if (PreviewUnencrypted.isPGPEncrypted(omi) && omi.Sender != null)
Dean@159
   454
                        {
Dean@159
   455
                            omi.Open += omi_Open;
Dean@159
   456
                        }
Dean@159
   457
                    }
Dean@159
   458
                }
Dean@159
   459
                catch { }
Dean@159
   460
Dean@159
   461
                if (ThisAddIn.addIn.enabled)
Dean@159
   462
                {
Dean@159
   463
                    reset_enc_status();
Dean@159
   464
                }
Dean@159
   465
                else
Dean@159
   466
                {
Dean@159
   467
                    timerRefresh.Enabled = true;
Dean@159
   468
                }
Dean@159
   469
            }
Dean@159
   470
        }
Dean@159
   471
Dean@159
   472
        /// <summary>
Dean@159
   473
        /// Event handler for when the form region is closed.
Dean@159
   474
        /// </summary>
Dean@159
   475
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   476
        {
Dean@159
   477
            return;
Dean@159
   478
        }
Dean@159
   479
Dean@159
   480
        private void omi_Open(ref bool Cancel)
Dean@159
   481
        {
Dean@159
   482
            if (mailItem != null && _mailItem.MirrorDisplay())
Dean@159
   483
                Cancel = true;
Dean@159
   484
        }
Dean@159
   485
Dean@159
   486
        private void mailItem_PropertyChange(string Name)
Dean@159
   487
        {
Dean@159
   488
            switch (Name)
Dean@159
   489
            {
Dean@159
   490
                case "To":
Dean@159
   491
                    timerRefresh.Enabled = true;
Dean@159
   492
                    break;
Dean@159
   493
                // Outlook bug: there are always both events, so one is enough
Dean@159
   494
                //case "CC":
Dean@159
   495
                //    reset_enc_status();
Dean@159
   496
                //    break;
Dean@159
   497
            }
Dean@159
   498
        }
Dean@159
   499
Dean@159
   500
        private void secure_strip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
Dean@159
   501
        {
Dean@159
   502
            enc_status_data.strip.Items.Clear();
Dean@159
   503
Dean@159
   504
            if (mailItem.IsIncoming)
Dean@159
   505
            {
Dean@159
   506
                pEp_identity_s ident = new pEp_identity_s();
Dean@159
   507
                ident.address = mailItem.FromAddress;
Dean@159
   508
                ident.username = mailItem.FromUsername;
Dean@159
   509
                ident.user_id = CryptableMailItem.GetUserIDFromAddress(ident.address, ident.username);
Dean@159
   510
                ident = ThisAddIn.pEp.update_identity(ident);
Dean@159
   511
                string text = pEp.Properties.Resources.IdentityTrustText + ident.username + " <" + ident.address + ">";
Dean@159
   512
                enc_status_data.strip.Items.Add(text, null, (x, y) => { undoHandshake(ident); });
Dean@159
   513
            }
Dean@159
   514
            else
Dean@159
   515
            {
Dean@159
   516
                foreach (pEp_identity_s ident in mailItem.Addresses)
Dean@159
   517
                {
Dean@159
   518
                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(ident);
Dean@159
   519
                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@159
   520
                    {
Dean@159
   521
                        string text = pEp.Properties.Resources.IdentityTrustText + ident.username + " <" + ident.address + ">";
Dean@159
   522
                        enc_status_data.strip.Items.Add(text, null, (x, y) => { undoHandshake(ident); });
Dean@159
   523
                    }
Dean@159
   524
                }
Dean@159
   525
            }
Dean@159
   526
        }
Dean@159
   527
vb@133
   528
        private void EncryptionStatus_Click(object sender, EventArgs e)
vb@133
   529
        {
vb@133
   530
            switch (enc_status)
vb@133
   531
            {
vb@133
   532
                case _pEp_color.pEp_rating_green:
vb@133
   533
                    try
vb@133
   534
                    {
dean@144
   535
                        string[] key_list = mailItem.Keylist;
vb@133
   536
                        if (key_list.Length > 0)
vb@133
   537
                        {
vb@133
   538
                            string msg;
vb@133
   539
                            if (key_list.Length > 1)
vb@133
   540
                                msg = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageMultiple + " {0}.", String.Join(", ", key_list));
vb@133
   541
                            else
vb@133
   542
                                msg = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageSingle + " {0}.", key_list[0]);
vb@133
   543
                            MessageBox.Show(msg, pEp.Properties.Resources.EncryptionStatusClickMessageBox, MessageBoxButtons.OK, MessageBoxIcon.Information);
vb@133
   544
                        }
vb@133
   545
                    }
vb@133
   546
                    catch (Exception ex)
vb@133
   547
                    {
vb@133
   548
                        StopHere(ex.ToString());
vb@133
   549
                    }
vb@133
   550
                    break;
vb@133
   551
vb@133
   552
                case _pEp_color.pEp_rating_yellow:
vb@133
   553
                    try
vb@133
   554
                    {
dean@144
   555
                        if (mailItem.IsIncoming)
vb@133
   556
                        {
vb@133
   557
                            pEp_identity_s identityPartner = new pEp_identity_s();
dean@144
   558
                            identityPartner.address = mailItem.FromAddress;
dean@144
   559
                            identityPartner.username = mailItem.FromUsername;
dean@144
   560
                            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   561
vb@133
   562
                            try
vb@133
   563
                            {
vb@133
   564
                                identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   565
                            }
vb@133
   566
                            catch
vb@133
   567
                            {
vb@133
   568
                                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   569
                                return;
vb@133
   570
                            }
vb@133
   571
                            doHandshake(identityPartner);
vb@133
   572
                        }
vb@133
   573
                        else // outgoing
vb@133
   574
                        {
vb@139
   575
                            ThisAddIn.verbose("yellow, handshake initiated");
vb@139
   576
vb@133
   577
                            List<pEp_identity_s> partnersToCheck = new List<pEp_identity_s>();
vb@133
   578
dean@144
   579
                            foreach (pEp_identity_s identity in mailItem.Addresses)
vb@133
   580
                            {
vb@139
   581
                                ThisAddIn.verbose("adding " + identity.username + " <" + identity.address + ">");
vb@139
   582
vb@133
   583
                                try
vb@133
   584
                                {
vb@133
   585
                                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(identity);
vb@133
   586
                                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_unconfirmed_encryption
vb@133
   587
                                        && identityPartner.comm_type < _pEp_comm_type.pEp_ct_confirmed_encryption)
vb@133
   588
                                    {
vb@133
   589
                                        partnersToCheck.Add(identityPartner);
vb@133
   590
                                    }
vb@133
   591
                                }
vb@133
   592
                                catch
vb@133
   593
                                {
vb@133
   594
                                }
vb@133
   595
                            }
vb@133
   596
vb@133
   597
                            if (partnersToCheck.Count == 0)
vb@133
   598
                            {
vb@133
   599
                                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   600
                                return;
vb@133
   601
                            }
vb@133
   602
dean@144
   603
                            pEp_identity_s[] addresses = mailItem.Addresses;
vb@133
   604
vb@133
   605
                            if (addresses.Length == 1)
vb@133
   606
                            {
vb@133
   607
                                doHandshake(partnersToCheck[0]);
vb@133
   608
                            }
vb@133
   609
                            else if (addresses.Length > 1)
vb@133
   610
                            {
vb@133
   611
                                contextMenuHandshake.Items.Clear();
vb@133
   612
                                foreach (pEp_identity_s identity in partnersToCheck) {
vb@133
   613
                                    string text = identity.username + " <" + identity.address + ">";
vb@133
   614
                                    contextMenuHandshake.Items.Add(text, null, (x, y) => { doHandshake(identity); });
vb@133
   615
                                }
vb@133
   616
                                contextMenuHandshake.Show(EncryptionStatus, 10, 10);
vb@133
   617
                            }
vb@133
   618
                        }
vb@133
   619
                    }
vb@133
   620
                    catch (COMException ex)
vb@133
   621
                    {
vb@133
   622
                        StopHere(ex.ToString());
vb@133
   623
                    }
vb@133
   624
                    catch (Exception ex)
vb@133
   625
                    {
vb@133
   626
                        StopHere(ex.ToString());
vb@133
   627
                    }
vb@133
   628
                    break;
vb@133
   629
vb@133
   630
                case _pEp_color.pEp_rating_unencrypted:
dean@144
   631
                    if (mailItem.IsIncoming)
vb@133
   632
                    {
vb@133
   633
                        MessageBox.Show(pEp.Properties.Resources.RatingUnencryptedMessageboxReasonBody, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   634
                    }
vb@133
   635
                    else
vb@133
   636
                    {
vb@133
   637
                        try
vb@133
   638
                        {
vb@133
   639
                            string reason = "";
dean@144
   640
                            foreach (pEp_identity_s identity in mailItem.Addresses)
vb@133
   641
                            {
vb@133
   642
                                if (identity.comm_type < _pEp_comm_type.pEp_ct_security_by_obscurity)
vb@133
   643
                                {
vb@133
   644
                                    if (reason != "")
vb@133
   645
                                        reason += "\n";
vb@133
   646
                                    // BUG: reason string in English language only and trivial
vb@133
   647
                                    string ct = identity.comm_type.ToString();
vb@133
   648
                                    ct = new string(ct.Skip(7).ToArray());
vb@133
   649
                                    ct = ct.Replace("_", " ");
vb@133
   650
                                    reason += identity.username + " <" + identity.address + ">: " + ct;
vb@133
   651
                                }
vb@133
   652
                            }
vb@133
   653
                            if (reason != "")
vb@133
   654
                                MessageBox.Show(reason, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   655
                        }
vb@133
   656
                        catch (COMException ex)
vb@133
   657
                        {
vb@133
   658
                            StopHere(ex.ToString());
vb@133
   659
                        }
vb@133
   660
                    }
vb@133
   661
                    break;
vb@133
   662
vb@133
   663
                case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   664
                case _pEp_color.pEp_rating_have_no_key:
vb@133
   665
                    try
vb@133
   666
                    {
dean@144
   667
                        string[] keys = mailItem.Keylist;
vb@133
   668
vb@133
   669
                        if (keys.Length > 1)
vb@133
   670
                        {
vb@133
   671
                            string reason;
vb@133
   672
vb@133
   673
                            if (keys.Length == 2)
vb@133
   674
                                reason = pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodyNokey;
vb@133
   675
                            else
vb@133
   676
                                reason = string.Format(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodySomekey, keys.Length - 1);
vb@133
   677
vb@133
   678
                            string keyinfo = "";
vb@133
   679
vb@133
   680
                            foreach (string key in keys)
vb@133
   681
                            {
vb@133
   682
                                if (key.Length >= 8)
vb@133
   683
                                    keyinfo += "\n" + key;
vb@133
   684
                            }
vb@133
   685
vb@133
   686
                            reason += keyinfo;
vb@133
   687
vb@133
   688
                            MessageBox.Show(reason, pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   689
                        }
vb@133
   690
                        else if (keys.Length == 1)
vb@133
   691
                        {
vb@133
   692
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonError + keys[0], pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   693
                        }
vb@133
   694
                        else
vb@133
   695
                        {
vb@133
   696
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonUnknownformat, pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   697
                        }
vb@133
   698
                    }
vb@133
   699
                    catch (COMException ex)
vb@133
   700
                    {
vb@133
   701
                        StopHere(ex.ToString());
vb@133
   702
                    }
vb@133
   703
                    break;
vb@133
   704
                    
vb@133
   705
                case _pEp_color.pEp_rating_undefined:
vb@133
   706
                    if (mailItem != null)
vb@133
   707
                    {
dean@144
   708
                        enc_status = mailItem.ColorRating;
vb@133
   709
                    }
vb@133
   710
                    break;
vb@133
   711
            }
vb@133
   712
        }
vb@133
   713
vb@133
   714
        private void linkExplanation_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
vb@133
   715
        {
vb@133
   716
            if (enc_status == _pEp_color.pEp_rating_undefined)
vb@133
   717
            {
vb@133
   718
                enc_status_data.explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
vb@133
   719
                enc_status_data.suggestion = pEp.Properties.Resources.RatingUndefinedSuggestion;
vb@133
   720
                enc_status_data.title = pEp.Properties.Resources.RatingUndefinedTitle;
vb@133
   721
                enc_status_data.background = Color.Gainsboro;
vb@133
   722
                enc_status_data.foreground = Color.Black;
vb@133
   723
                enc_status_data.button_text = pEp.Properties.Resources.RatingUndefinedButtontext;
vb@133
   724
                enc_status_data.strip = null;
vb@133
   725
            }
vb@133
   726
vb@133
   727
            var result = MessageBox.Show(
vb@133
   728
                            enc_status_data.explanation + "\n\n" + enc_status_data.suggestion,
vb@133
   729
                            enc_status_data.title,
vb@133
   730
                            MessageBoxButtons.OK,
vb@133
   731
                            enc_status == _pEp_color.pEp_rating_under_attack
vb@133
   732
                                ? MessageBoxIcon.Stop
vb@133
   733
                                : MessageBoxIcon.Information
vb@133
   734
                        );
vb@133
   735
        }
vb@133
   736
vb@133
   737
        private void timerRefresh_Tick(object sender, EventArgs e)
vb@133
   738
        {
vb@133
   739
            timerRefresh.Interval = 100;
vb@133
   740
            timerRefresh.Enabled = false; // only once
vb@133
   741
vb@133
   742
            if (!ThisAddIn.addIn.enabled)
vb@133
   743
                ThisAddIn.addIn.enable();
vb@133
   744
vb@133
   745
            makePreview();
vb@133
   746
vb@133
   747
            reset_enc_status();
vb@133
   748
        }
vb@133
   749
vb@133
   750
        private void EncryptionStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   751
        {
vb@133
   752
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
vb@133
   753
            {
vb@133
   754
                CryptableMailItem cmi = mailItem;
vb@133
   755
dean@144
   756
                if (cmi != null && !cmi.IsIncoming && !cmi.IsInSentFolder && cmi.OutgoingColor > _pEp_color.pEp_rating_unencrypted)
vb@133
   757
                {
dean@144
   758
                    if (cmi.SendUnencrypted == CheckState.Indeterminate)
vb@133
   759
                    {
vb@133
   760
                        sendUnencryptedToolStripMenuItem.CheckState = CheckState.Unchecked;
vb@133
   761
                    }
vb@133
   762
                    else {
dean@144
   763
                        sendUnencryptedToolStripMenuItem.CheckState = cmi.SendUnencrypted;
vb@133
   764
                    }
vb@133
   765
vb@133
   766
                    Control _sender = (Control)sender;
vb@133
   767
                    contextMenuEdit.Show(_sender.PointToScreen(e.Location));
vb@133
   768
                }
vb@133
   769
            }
vb@133
   770
        }
vb@133
   771
vb@133
   772
        private void sendUnencryptedToolStripMenuItem_Click(object sender, EventArgs e)
vb@133
   773
        {
vb@133
   774
            sendUnencryptedToolStripMenuItem.Checked = !sendUnencryptedToolStripMenuItem.Checked;
dean@144
   775
            mailItem.SendUnencrypted = sendUnencryptedToolStripMenuItem.CheckState;
vb@133
   776
            timerRefresh.Enabled = true;
vb@133
   777
        }
Dean@151
   778
Dean@151
   779
        /// <summary>
Dean@151
   780
        /// Event handler for when the options picture box is clicked.
Dean@151
   781
        /// This will open the options form for user editing.
Dean@151
   782
        /// </summary>
Dean@151
   783
        private void PictureBoxOptions_Click(object sender, EventArgs e)
Dean@151
   784
        {
Dean@159
   785
            DialogResult result;
Dean@152
   786
            FormOptions frm = new FormOptions();
Dean@154
   787
            frm.StartPosition = FormStartPosition.CenterScreen;
Dean@159
   788
            result = frm.ShowDialog(this.ParentForm); // Must show as dialog to block code
Dean@151
   789
Dean@154
   790
            if (ThisAddIn.addIn.use_keyserver)
Dean@154
   791
                ThisAddIn.pEp.start_keyserver_lookup();
Dean@154
   792
            else
Dean@154
   793
                ThisAddIn.pEp.stop_keyserver_lookup();
Dean@151
   794
Dean@151
   795
            return;
Dean@151
   796
        }
Dean@151
   797
Dean@151
   798
        /// <summary>
Dean@151
   799
        /// Event handler for when the mouse hovers over the options picture box.
Dean@151
   800
        /// This is a work-around to show a tool tip.
Dean@151
   801
        /// </summary>
Dean@151
   802
        private void PictureBoxOptions_MouseHover(object sender, EventArgs e)
Dean@151
   803
        {
Dean@151
   804
            ToolTip toolTip = new ToolTip();
Dean@151
   805
            toolTip.SetToolTip(this.PictureBoxOptions, "Click to open options");
Dean@151
   806
            return;
Dean@151
   807
        }
vb@133
   808
    }
vb@133
   809
}