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