EncryptionStatusPanel.cs
author Dean Looyengoed
Tue, 29 Dec 2015 01:25:30 +0100
changeset 159 15d0050bc11a
parent 156 dcb5ac2a9cd4
child 161 2b6b485152c5
permissions -rw-r--r--
Initial refactoring of the encryption status panel
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@155
   283
            ownFpr   = quadruple(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@155
   289
            partnerFpr   = quadruple(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@159
   345
        private string quadruple(string text)
Dean@159
   346
        {
Dean@159
   347
            List<string> result = new List<string>();
Dean@159
   348
Dean@159
   349
            for (int i = 0; i < text.Length; i+=4)
Dean@159
   350
            {
Dean@159
   351
                try
Dean@159
   352
                {
Dean@159
   353
                    result.Add(text.Substring(i, 4));
Dean@159
   354
                }
Dean@159
   355
                catch (ArgumentOutOfRangeException)
Dean@159
   356
                {
Dean@159
   357
                    result.Add(text.Substring(i));
Dean@159
   358
                    break;
Dean@159
   359
                }
Dean@159
   360
            }
Dean@159
   361
Dean@159
   362
            return String.Join(" ", result);
Dean@159
   363
        }
Dean@159
   364
Dean@159
   365
        protected void makePreview()
Dean@159
   366
        {
Dean@159
   367
            WindowFormRegionCollection formRegions;
Dean@159
   368
Dean@159
   369
            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
Dean@159
   370
                return;
Dean@159
   371
            else if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
Dean@159
   372
                formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveExplorer()];
Dean@159
   373
            else
Dean@159
   374
                return;
Dean@159
   375
Dean@159
   376
            try
Dean@159
   377
            {
Dean@159
   378
                if (formRegions.PreviewUnencrypted.Visible)
Dean@159
   379
                {
Dean@159
   380
                    byte[] rtf = mailItem.MirrorGetRTFBody();
Dean@159
   381
                    formRegions.PreviewUnencrypted.textPreview.Rtf = System.Text.Encoding.ASCII.GetString(rtf, 0, rtf.Length);
Dean@159
   382
                    formRegions.PreviewUnencrypted.textSubject.Text = mailItem.MirrorGetSubject();
Dean@159
   383
                    formRegions.PreviewUnencrypted.textFrom.Text = mailItem.FromUsername + " <" + mailItem.FromAddress + ">";
Dean@159
   384
                }
Dean@159
   385
            }
Dean@159
   386
            catch { }
Dean@159
   387
        }
Dean@159
   388
Dean@159
   389
        protected void reset_enc_status()
Dean@159
   390
        {
Dean@159
   391
            if (in_reset)
Dean@159
   392
                return;
Dean@159
   393
Dean@159
   394
            in_reset = true;
Dean@159
   395
Dean@159
   396
            if (mailItem != null)
Dean@159
   397
            {
Dean@159
   398
                if (_mailItem.DownloadState == Outlook.OlDownloadState.olFullItem)
Dean@159
   399
                {
Dean@159
   400
                    if (_mailItem.SendUnencrypted == CheckState.Checked)
Dean@159
   401
                    {
Dean@159
   402
                        enc_status = _pEp_color.pEp_rating_unencrypted;
Dean@159
   403
                    }
Dean@159
   404
                    else
Dean@159
   405
                    {
Dean@159
   406
                        ThisAddIn.verbose("get_color in reset_enc_status");
Dean@159
   407
                        enc_status = _mailItem.ColorRating;
Dean@159
   408
                    }
Dean@159
   409
                }
Dean@159
   410
                else
Dean@159
   411
                {
Dean@159
   412
                    _mailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
Dean@159
   413
                    timerRefresh.Interval = 500;
Dean@159
   414
                    timerRefresh.Enabled = true;
Dean@159
   415
                }
Dean@159
   416
            }
Dean@159
   417
Dean@159
   418
            in_reset = false;
Dean@159
   419
        }
Dean@159
   420
Dean@159
   421
        /**************************************************************
Dean@159
   422
         * 
Dean@159
   423
         * Event Handling
Dean@159
   424
         * 
Dean@159
   425
         *************************************************************/
Dean@159
   426
Dean@159
   427
        /// <summary>
Dean@159
   428
        /// Event handler that is called before the form region is displayed.
Dean@159
   429
        /// </summary>
Dean@159
   430
        private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
Dean@159
   431
        {
Dean@159
   432
            if (this.OutlookItem == null)
Dean@159
   433
            {
Dean@159
   434
                enc_status = _pEp_color.pEp_rating_undefined;
Dean@159
   435
            }
Dean@159
   436
            else
Dean@159
   437
            {
Dean@159
   438
                Outlook.MailItem omi = (Outlook.MailItem)this.OutlookItem;
Dean@159
   439
                try
Dean@159
   440
                {
Dean@159
   441
                    omi.PropertyChange += mailItem_PropertyChange;
Dean@159
   442
                    if (CryptableMailItem.IsEncryptedStore(omi))
Dean@159
   443
                    {
Dean@159
   444
                        if (PreviewUnencrypted.isPGPEncrypted(omi) && omi.Sender != null)
Dean@159
   445
                        {
Dean@159
   446
                            omi.Open += omi_Open;
Dean@159
   447
                        }
Dean@159
   448
                    }
Dean@159
   449
                }
Dean@159
   450
                catch { }
Dean@159
   451
Dean@159
   452
                if (ThisAddIn.addIn.enabled)
Dean@159
   453
                {
Dean@159
   454
                    reset_enc_status();
Dean@159
   455
                }
Dean@159
   456
                else
Dean@159
   457
                {
Dean@159
   458
                    timerRefresh.Enabled = true;
Dean@159
   459
                }
Dean@159
   460
            }
Dean@159
   461
        }
Dean@159
   462
Dean@159
   463
        /// <summary>
Dean@159
   464
        /// Event handler for when the form region is closed.
Dean@159
   465
        /// </summary>
Dean@159
   466
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
Dean@159
   467
        {
Dean@159
   468
            return;
Dean@159
   469
        }
Dean@159
   470
Dean@159
   471
        private void omi_Open(ref bool Cancel)
Dean@159
   472
        {
Dean@159
   473
            if (mailItem != null && _mailItem.MirrorDisplay())
Dean@159
   474
                Cancel = true;
Dean@159
   475
        }
Dean@159
   476
Dean@159
   477
        private void mailItem_PropertyChange(string Name)
Dean@159
   478
        {
Dean@159
   479
            switch (Name)
Dean@159
   480
            {
Dean@159
   481
                case "To":
Dean@159
   482
                    timerRefresh.Enabled = true;
Dean@159
   483
                    break;
Dean@159
   484
                // Outlook bug: there are always both events, so one is enough
Dean@159
   485
                //case "CC":
Dean@159
   486
                //    reset_enc_status();
Dean@159
   487
                //    break;
Dean@159
   488
            }
Dean@159
   489
        }
Dean@159
   490
Dean@159
   491
        private void secure_strip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
Dean@159
   492
        {
Dean@159
   493
            enc_status_data.strip.Items.Clear();
Dean@159
   494
Dean@159
   495
            if (mailItem.IsIncoming)
Dean@159
   496
            {
Dean@159
   497
                pEp_identity_s ident = new pEp_identity_s();
Dean@159
   498
                ident.address = mailItem.FromAddress;
Dean@159
   499
                ident.username = mailItem.FromUsername;
Dean@159
   500
                ident.user_id = CryptableMailItem.GetUserIDFromAddress(ident.address, ident.username);
Dean@159
   501
                ident = ThisAddIn.pEp.update_identity(ident);
Dean@159
   502
                string text = pEp.Properties.Resources.IdentityTrustText + ident.username + " <" + ident.address + ">";
Dean@159
   503
                enc_status_data.strip.Items.Add(text, null, (x, y) => { undoHandshake(ident); });
Dean@159
   504
            }
Dean@159
   505
            else
Dean@159
   506
            {
Dean@159
   507
                foreach (pEp_identity_s ident in mailItem.Addresses)
Dean@159
   508
                {
Dean@159
   509
                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(ident);
Dean@159
   510
                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_confirmed_encryption)
Dean@159
   511
                    {
Dean@159
   512
                        string text = pEp.Properties.Resources.IdentityTrustText + ident.username + " <" + ident.address + ">";
Dean@159
   513
                        enc_status_data.strip.Items.Add(text, null, (x, y) => { undoHandshake(ident); });
Dean@159
   514
                    }
Dean@159
   515
                }
Dean@159
   516
            }
Dean@159
   517
        }
Dean@159
   518
vb@133
   519
        private void EncryptionStatus_Click(object sender, EventArgs e)
vb@133
   520
        {
vb@133
   521
            switch (enc_status)
vb@133
   522
            {
vb@133
   523
                case _pEp_color.pEp_rating_green:
vb@133
   524
                    try
vb@133
   525
                    {
dean@144
   526
                        string[] key_list = mailItem.Keylist;
vb@133
   527
                        if (key_list.Length > 0)
vb@133
   528
                        {
vb@133
   529
                            string msg;
vb@133
   530
                            if (key_list.Length > 1)
vb@133
   531
                                msg = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageMultiple + " {0}.", String.Join(", ", key_list));
vb@133
   532
                            else
vb@133
   533
                                msg = String.Format(pEp.Properties.Resources.EncryptionStatusClickMessageSingle + " {0}.", key_list[0]);
vb@133
   534
                            MessageBox.Show(msg, pEp.Properties.Resources.EncryptionStatusClickMessageBox, MessageBoxButtons.OK, MessageBoxIcon.Information);
vb@133
   535
                        }
vb@133
   536
                    }
vb@133
   537
                    catch (Exception ex)
vb@133
   538
                    {
vb@133
   539
                        StopHere(ex.ToString());
vb@133
   540
                    }
vb@133
   541
                    break;
vb@133
   542
vb@133
   543
                case _pEp_color.pEp_rating_yellow:
vb@133
   544
                    try
vb@133
   545
                    {
dean@144
   546
                        if (mailItem.IsIncoming)
vb@133
   547
                        {
vb@133
   548
                            pEp_identity_s identityPartner = new pEp_identity_s();
dean@144
   549
                            identityPartner.address = mailItem.FromAddress;
dean@144
   550
                            identityPartner.username = mailItem.FromUsername;
dean@144
   551
                            identityPartner.user_id = CryptableMailItem.GetUserIDFromAddress(identityPartner.address, identityPartner.username);
vb@139
   552
vb@133
   553
                            try
vb@133
   554
                            {
vb@133
   555
                                identityPartner = ThisAddIn.pEp.update_identity(identityPartner);
vb@133
   556
                            }
vb@133
   557
                            catch
vb@133
   558
                            {
vb@133
   559
                                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   560
                                return;
vb@133
   561
                            }
vb@133
   562
                            doHandshake(identityPartner);
vb@133
   563
                        }
vb@133
   564
                        else // outgoing
vb@133
   565
                        {
vb@139
   566
                            ThisAddIn.verbose("yellow, handshake initiated");
vb@139
   567
vb@133
   568
                            List<pEp_identity_s> partnersToCheck = new List<pEp_identity_s>();
vb@133
   569
dean@144
   570
                            foreach (pEp_identity_s identity in mailItem.Addresses)
vb@133
   571
                            {
vb@139
   572
                                ThisAddIn.verbose("adding " + identity.username + " <" + identity.address + ">");
vb@139
   573
vb@133
   574
                                try
vb@133
   575
                                {
vb@133
   576
                                    pEp_identity_s identityPartner = ThisAddIn.pEp.update_identity(identity);
vb@133
   577
                                    if (identityPartner.comm_type >= _pEp_comm_type.pEp_ct_unconfirmed_encryption
vb@133
   578
                                        && identityPartner.comm_type < _pEp_comm_type.pEp_ct_confirmed_encryption)
vb@133
   579
                                    {
vb@133
   580
                                        partnersToCheck.Add(identityPartner);
vb@133
   581
                                    }
vb@133
   582
                                }
vb@133
   583
                                catch
vb@133
   584
                                {
vb@133
   585
                                }
vb@133
   586
                            }
vb@133
   587
vb@133
   588
                            if (partnersToCheck.Count == 0)
vb@133
   589
                            {
vb@133
   590
                                enc_status = _pEp_color.pEp_rating_undefined;
vb@133
   591
                                return;
vb@133
   592
                            }
vb@133
   593
dean@144
   594
                            pEp_identity_s[] addresses = mailItem.Addresses;
vb@133
   595
vb@133
   596
                            if (addresses.Length == 1)
vb@133
   597
                            {
vb@133
   598
                                doHandshake(partnersToCheck[0]);
vb@133
   599
                            }
vb@133
   600
                            else if (addresses.Length > 1)
vb@133
   601
                            {
vb@133
   602
                                contextMenuHandshake.Items.Clear();
vb@133
   603
                                foreach (pEp_identity_s identity in partnersToCheck) {
vb@133
   604
                                    string text = identity.username + " <" + identity.address + ">";
vb@133
   605
                                    contextMenuHandshake.Items.Add(text, null, (x, y) => { doHandshake(identity); });
vb@133
   606
                                }
vb@133
   607
                                contextMenuHandshake.Show(EncryptionStatus, 10, 10);
vb@133
   608
                            }
vb@133
   609
                        }
vb@133
   610
                    }
vb@133
   611
                    catch (COMException ex)
vb@133
   612
                    {
vb@133
   613
                        StopHere(ex.ToString());
vb@133
   614
                    }
vb@133
   615
                    catch (Exception ex)
vb@133
   616
                    {
vb@133
   617
                        StopHere(ex.ToString());
vb@133
   618
                    }
vb@133
   619
                    break;
vb@133
   620
vb@133
   621
                case _pEp_color.pEp_rating_unencrypted:
dean@144
   622
                    if (mailItem.IsIncoming)
vb@133
   623
                    {
vb@133
   624
                        MessageBox.Show(pEp.Properties.Resources.RatingUnencryptedMessageboxReasonBody, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   625
                    }
vb@133
   626
                    else
vb@133
   627
                    {
vb@133
   628
                        try
vb@133
   629
                        {
vb@133
   630
                            string reason = "";
dean@144
   631
                            foreach (pEp_identity_s identity in mailItem.Addresses)
vb@133
   632
                            {
vb@133
   633
                                if (identity.comm_type < _pEp_comm_type.pEp_ct_security_by_obscurity)
vb@133
   634
                                {
vb@133
   635
                                    if (reason != "")
vb@133
   636
                                        reason += "\n";
vb@133
   637
                                    // BUG: reason string in English language only and trivial
vb@133
   638
                                    string ct = identity.comm_type.ToString();
vb@133
   639
                                    ct = new string(ct.Skip(7).ToArray());
vb@133
   640
                                    ct = ct.Replace("_", " ");
vb@133
   641
                                    reason += identity.username + " <" + identity.address + ">: " + ct;
vb@133
   642
                                }
vb@133
   643
                            }
vb@133
   644
                            if (reason != "")
vb@133
   645
                                MessageBox.Show(reason, pEp.Properties.Resources.RatingUnencryptedMessageboxReasonTitle);
vb@133
   646
                        }
vb@133
   647
                        catch (COMException ex)
vb@133
   648
                        {
vb@133
   649
                            StopHere(ex.ToString());
vb@133
   650
                        }
vb@133
   651
                    }
vb@133
   652
                    break;
vb@133
   653
vb@133
   654
                case _pEp_color.pEp_rating_cannot_decrypt:
vb@133
   655
                case _pEp_color.pEp_rating_have_no_key:
vb@133
   656
                    try
vb@133
   657
                    {
dean@144
   658
                        string[] keys = mailItem.Keylist;
vb@133
   659
vb@133
   660
                        if (keys.Length > 1)
vb@133
   661
                        {
vb@133
   662
                            string reason;
vb@133
   663
vb@133
   664
                            if (keys.Length == 2)
vb@133
   665
                                reason = pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodyNokey;
vb@133
   666
                            else
vb@133
   667
                                reason = string.Format(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonBodySomekey, keys.Length - 1);
vb@133
   668
vb@133
   669
                            string keyinfo = "";
vb@133
   670
vb@133
   671
                            foreach (string key in keys)
vb@133
   672
                            {
vb@133
   673
                                if (key.Length >= 8)
vb@133
   674
                                    keyinfo += "\n" + key;
vb@133
   675
                            }
vb@133
   676
vb@133
   677
                            reason += keyinfo;
vb@133
   678
vb@133
   679
                            MessageBox.Show(reason, pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   680
                        }
vb@133
   681
                        else if (keys.Length == 1)
vb@133
   682
                        {
vb@133
   683
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonError + keys[0], pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   684
                        }
vb@133
   685
                        else
vb@133
   686
                        {
vb@133
   687
                            MessageBox.Show(pEp.Properties.Resources.RatingHavenokeyMessageboxReasonUnknownformat, pEp.Properties.Resources.RatingHavenokeyTitle);
vb@133
   688
                        }
vb@133
   689
                    }
vb@133
   690
                    catch (COMException ex)
vb@133
   691
                    {
vb@133
   692
                        StopHere(ex.ToString());
vb@133
   693
                    }
vb@133
   694
                    break;
vb@133
   695
                    
vb@133
   696
                case _pEp_color.pEp_rating_undefined:
vb@133
   697
                    if (mailItem != null)
vb@133
   698
                    {
dean@144
   699
                        enc_status = mailItem.ColorRating;
vb@133
   700
                    }
vb@133
   701
                    break;
vb@133
   702
            }
vb@133
   703
        }
vb@133
   704
vb@133
   705
        private void linkExplanation_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
vb@133
   706
        {
vb@133
   707
            if (enc_status == _pEp_color.pEp_rating_undefined)
vb@133
   708
            {
vb@133
   709
                enc_status_data.explanation = pEp.Properties.Resources.RatingUndefinedExplanation;
vb@133
   710
                enc_status_data.suggestion = pEp.Properties.Resources.RatingUndefinedSuggestion;
vb@133
   711
                enc_status_data.title = pEp.Properties.Resources.RatingUndefinedTitle;
vb@133
   712
                enc_status_data.background = Color.Gainsboro;
vb@133
   713
                enc_status_data.foreground = Color.Black;
vb@133
   714
                enc_status_data.button_text = pEp.Properties.Resources.RatingUndefinedButtontext;
vb@133
   715
                enc_status_data.strip = null;
vb@133
   716
            }
vb@133
   717
vb@133
   718
            var result = MessageBox.Show(
vb@133
   719
                            enc_status_data.explanation + "\n\n" + enc_status_data.suggestion,
vb@133
   720
                            enc_status_data.title,
vb@133
   721
                            MessageBoxButtons.OK,
vb@133
   722
                            enc_status == _pEp_color.pEp_rating_under_attack
vb@133
   723
                                ? MessageBoxIcon.Stop
vb@133
   724
                                : MessageBoxIcon.Information
vb@133
   725
                        );
vb@133
   726
        }
vb@133
   727
vb@133
   728
        private void timerRefresh_Tick(object sender, EventArgs e)
vb@133
   729
        {
vb@133
   730
            timerRefresh.Interval = 100;
vb@133
   731
            timerRefresh.Enabled = false; // only once
vb@133
   732
vb@133
   733
            if (!ThisAddIn.addIn.enabled)
vb@133
   734
                ThisAddIn.addIn.enable();
vb@133
   735
vb@133
   736
            makePreview();
vb@133
   737
vb@133
   738
            reset_enc_status();
vb@133
   739
        }
vb@133
   740
vb@133
   741
        private void EncryptionStatus_MouseUp(object sender, MouseEventArgs e)
vb@133
   742
        {
vb@133
   743
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
vb@133
   744
            {
vb@133
   745
                CryptableMailItem cmi = mailItem;
vb@133
   746
dean@144
   747
                if (cmi != null && !cmi.IsIncoming && !cmi.IsInSentFolder && cmi.OutgoingColor > _pEp_color.pEp_rating_unencrypted)
vb@133
   748
                {
dean@144
   749
                    if (cmi.SendUnencrypted == CheckState.Indeterminate)
vb@133
   750
                    {
vb@133
   751
                        sendUnencryptedToolStripMenuItem.CheckState = CheckState.Unchecked;
vb@133
   752
                    }
vb@133
   753
                    else {
dean@144
   754
                        sendUnencryptedToolStripMenuItem.CheckState = cmi.SendUnencrypted;
vb@133
   755
                    }
vb@133
   756
vb@133
   757
                    Control _sender = (Control)sender;
vb@133
   758
                    contextMenuEdit.Show(_sender.PointToScreen(e.Location));
vb@133
   759
                }
vb@133
   760
            }
vb@133
   761
        }
vb@133
   762
vb@133
   763
        private void sendUnencryptedToolStripMenuItem_Click(object sender, EventArgs e)
vb@133
   764
        {
vb@133
   765
            sendUnencryptedToolStripMenuItem.Checked = !sendUnencryptedToolStripMenuItem.Checked;
dean@144
   766
            mailItem.SendUnencrypted = sendUnencryptedToolStripMenuItem.CheckState;
vb@133
   767
            timerRefresh.Enabled = true;
vb@133
   768
        }
Dean@151
   769
Dean@151
   770
        /// <summary>
Dean@151
   771
        /// Event handler for when the options picture box is clicked.
Dean@151
   772
        /// This will open the options form for user editing.
Dean@151
   773
        /// </summary>
Dean@151
   774
        private void PictureBoxOptions_Click(object sender, EventArgs e)
Dean@151
   775
        {
Dean@159
   776
            DialogResult result;
Dean@152
   777
            FormOptions frm = new FormOptions();
Dean@154
   778
            frm.StartPosition = FormStartPosition.CenterScreen;
Dean@159
   779
            result = frm.ShowDialog(this.ParentForm); // Must show as dialog to block code
Dean@151
   780
Dean@154
   781
            if (ThisAddIn.addIn.use_keyserver)
Dean@154
   782
                ThisAddIn.pEp.start_keyserver_lookup();
Dean@154
   783
            else
Dean@154
   784
                ThisAddIn.pEp.stop_keyserver_lookup();
Dean@151
   785
Dean@151
   786
            return;
Dean@151
   787
        }
Dean@151
   788
Dean@151
   789
        /// <summary>
Dean@151
   790
        /// Event handler for when the mouse hovers over the options picture box.
Dean@151
   791
        /// This is a work-around to show a tool tip.
Dean@151
   792
        /// </summary>
Dean@151
   793
        private void PictureBoxOptions_MouseHover(object sender, EventArgs e)
Dean@151
   794
        {
Dean@151
   795
            ToolTip toolTip = new ToolTip();
Dean@151
   796
            toolTip.SetToolTip(this.PictureBoxOptions, "Click to open options");
Dean@151
   797
            return;
Dean@151
   798
        }
vb@133
   799
    }
vb@133
   800
}