UI/Models/Dialog.cs
author Thomas
Thu, 24 Oct 2019 11:54:07 +0200
branchsync
changeset 2829 cf97661db398
parent 2789 04b1369a0456
child 2835 417b06496e98
permissions -rw-r--r--
Use RatingFromCommType
Thomas@2715
     1
´╗┐using pEpCOMServerAdapterLib;
Thomas@2715
     2
using System;
Thomas@2780
     3
using System.Collections.Generic;
Thomas@2709
     4
Thomas@2715
     5
namespace pEp.UI.Models
Thomas@2709
     6
{
Thomas@2709
     7
    internal class Dialog
Thomas@2709
     8
    {
Thomas@2715
     9
        /// <summary>
Thomas@2715
    10
        /// The different dialog types.
Thomas@2715
    11
        /// </summary>
Thomas@2780
    12
        public enum Type
Thomas@2709
    13
        {
Thomas@2712
    14
            Undefined,
Thomas@2709
    15
            Handshake,
Thomas@2709
    16
            KeySync,
Thomas@2710
    17
            KeyImportPGP,
Thomas@2710
    18
            ForceProtection
Thomas@2709
    19
        }
Thomas@2709
    20
Thomas@2712
    21
        /// <summary>
Thomas@2780
    22
        /// Gets or sets the dialog's own identity.
Thomas@2712
    23
        /// </summary>
Thomas@2780
    24
        public PEPIdentity Myself { get; private set; }
Thomas@2712
    25
Thomas@2712
    26
        /// <summary>
Thomas@2780
    27
        /// Gets or sets the sync partner identities.
Thomas@2712
    28
        /// </summary>
Thomas@2780
    29
        public List<PEPIdentity> PartnerIdentities { get; private set; }
Thomas@2712
    30
Thomas@2712
    31
        /// <summary>
Thomas@2780
    32
        /// Gets or sets the secondary sync partner identities.
Thomas@2712
    33
        /// </summary>
Thomas@2780
    34
        public List<PEPIdentity> SecondaryPartnerIdentities { get; private set; }
Thomas@2712
    35
Thomas@2715
    36
        /// <summary>
Thomas@2780
    37
        /// The type of this dialog.
Thomas@2780
    38
        /// </summary>
Thomas@2780
    39
        public Type DialogType { get; } = Type.Undefined;
Thomas@2780
    40
Thomas@2780
    41
        /// <summary>
Thomas@2780
    42
        /// Constructor for a dialog with multiple partner identities.
Thomas@2715
    43
        /// </summary>
Thomas@2715
    44
        /// <param name="type">The type of the dialog.</param>
Thomas@2715
    45
        /// <param name="myself">The myself identity.</param>
Thomas@2780
    46
        /// <param name="partnerIdentities">The partner identities.</param>
Thomas@2780
    47
        /// <param name="secondaryPartnerIdentities">The partner identities.</param>
Thomas@2780
    48
        public Dialog(Type type, PEPIdentity myself, List<PEPIdentity> partnerIdentities, List<PEPIdentity> secondaryPartnerIdentities = null)
Thomas@2712
    49
        {
Thomas@2780
    50
            this.DialogType = type;
Thomas@2712
    51
Thomas@2780
    52
            if (this.DialogType == Type.KeySync)
Thomas@2715
    53
            {
Thomas@2780
    54
                // In Keysync mode, only one handshake is valid
Thomas@2780
    55
                if (partnerIdentities?.Count == 1)
Thomas@2780
    56
                {
Thomas@2780
    57
                    // Set rating manually to reliable (to show correct UI)
Thomas@2780
    58
                    partnerIdentities[0].Rating = pEpRating.pEpRatingReliable;
Thomas@2789
    59
Thomas@2789
    60
                    this.Myself = myself;
Thomas@2789
    61
                    this.PartnerIdentities = partnerIdentities;
Thomas@2780
    62
                }
Thomas@2780
    63
                else
Thomas@2780
    64
                {
Thomas@2780
    65
                    throw new Exception("Cannot create sync dialog, because handshake count doesn't match. Expected count: 1. Actual count: " + partnerIdentities?.Count ?? "<null>");
Thomas@2780
    66
                }
Thomas@2727
    67
            }
Thomas@2727
    68
            else
Thomas@2727
    69
            {
Thomas@2780
    70
                // In other modes, update myself and partner identities   
Thomas@2726
    71
                try
Thomas@2726
    72
                {
Thomas@2726
    73
                    pEpIdentity _myself = myself.ToCOMType();
Thomas@2726
    74
                    _myself = ThisAddIn.PEPEngine.Myself(_myself);
Thomas@2726
    75
                    this.Myself = new PEPIdentity(_myself);
Thomas@2709
    76
Thomas@2726
    77
                }
Thomas@2726
    78
                catch (Exception ex)
Thomas@2726
    79
                {
Thomas@2726
    80
                    Log.Error("Dialog.Ctr: Error updating myself. " + ex.ToString());
Thomas@2726
    81
                }
Thomas@2726
    82
Thomas@2780
    83
                if (partnerIdentities != null)
Thomas@2780
    84
                {
Thomas@2780
    85
                    this.UpdateIdentities(ref partnerIdentities);
Thomas@2780
    86
                    this.PartnerIdentities = partnerIdentities;
Thomas@2780
    87
                }
Thomas@2780
    88
Thomas@2780
    89
                if (secondaryPartnerIdentities != null)
Thomas@2780
    90
                {
Thomas@2780
    91
                    this.UpdateIdentities(ref secondaryPartnerIdentities);
Thomas@2780
    92
                    this.SecondaryPartnerIdentities = secondaryPartnerIdentities;
Thomas@2780
    93
                }
Thomas@2780
    94
            }
Thomas@2780
    95
        }
Thomas@2780
    96
Thomas@2780
    97
        /// <summary>
Thomas@2780
    98
        /// Constructor for a dialog with one partner identity.
Thomas@2780
    99
        /// </summary>
Thomas@2780
   100
        /// <param name="type">The type of the dialog.</param>
Thomas@2780
   101
        /// <param name="myself">The myself identity.</param>
Thomas@2780
   102
        /// <param name="partner">The partner identity.</param>
Thomas@2780
   103
        public Dialog(Type type, PEPIdentity myself, PEPIdentity partner) : this(type, myself, new List<PEPIdentity> { partner })
Thomas@2780
   104
        {
Thomas@2780
   105
        }
Thomas@2780
   106
Thomas@2780
   107
        /// <summary>
Thomas@2780
   108
        /// Updates a list of pEp identities.
Thomas@2780
   109
        /// </summary>
Thomas@2780
   110
        /// <param name="identities">The identities to update.</param>
Thomas@2780
   111
        public void UpdateIdentities(ref List<PEPIdentity> identities)
Thomas@2780
   112
        {
Thomas@2780
   113
            for (int i = 0; i < identities?.Count; i++)
Thomas@2780
   114
            {
Thomas@2780
   115
                PEPIdentity identity = identities[i];
Thomas@2726
   116
                try
Thomas@2726
   117
                {
Thomas@2780
   118
                    pEpIdentity _identity = identity.ToCOMType();
Thomas@2780
   119
                    _identity = ThisAddIn.PEPEngine.UpdateIdentity(_identity);
Thomas@2829
   120
                    identity = new PEPIdentity(_identity)
Thomas@2829
   121
                    {
Thomas@2829
   122
                        Rating = ThisAddIn.PEPEngine.RatingFromCommType(_identity.CommType)
Thomas@2829
   123
                    };
Thomas@2726
   124
Thomas@2726
   125
                }
Thomas@2726
   126
                catch (Exception ex)
Thomas@2726
   127
                {
Thomas@2726
   128
                    Log.Error("Dialog.Ctr: Error updating partner. " + ex.ToString());
Thomas@2726
   129
                }
Thomas@2709
   130
Thomas@2780
   131
                identities[i] = identity;
Thomas@2709
   132
            }
Thomas@2709
   133
        }
Thomas@2709
   134
    }
Thomas@2709
   135
}