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