2020-05-12 17:14:30 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using GalaSoft.MvvmLight;
|
|
|
|
|
using Messages;
|
2020-05-18 14:14:28 +02:00
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
2020-05-12 17:14:30 +02:00
|
|
|
|
using ModernKeePass.Domain.Enums;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels.ListItems
|
|
|
|
|
{
|
|
|
|
|
public class EntryFieldVm: ViewModelBase
|
|
|
|
|
{
|
2020-05-18 14:14:28 +02:00
|
|
|
|
private readonly ICryptographyClient _cryptography;
|
2020-05-12 17:14:30 +02:00
|
|
|
|
private string _name;
|
|
|
|
|
private string _value;
|
|
|
|
|
private bool _isProtected;
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return _name; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var newName = EntryFieldName.StandardFieldNames.Contains(value) ? $"{value}_1" : value;
|
|
|
|
|
MessengerInstance.Send(new EntryFieldNameChangedMessage { OldName = Name, NewName = newName, Value = Value, IsProtected = IsProtected});
|
|
|
|
|
Set(nameof(Name), ref _name, newName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 11:59:40 +02:00
|
|
|
|
public string DisplayValue => IsProtected? "*****" : _value;
|
|
|
|
|
|
2020-05-12 17:14:30 +02:00
|
|
|
|
public string Value
|
|
|
|
|
{
|
2020-05-18 14:14:28 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return IsProtected? _cryptography.UnProtect(_value).GetAwaiter().GetResult() : _value;
|
|
|
|
|
}
|
2020-05-12 17:14:30 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2020-05-18 22:20:31 +02:00
|
|
|
|
var protectedValue = IsProtected ? _cryptography.Protect(value).GetAwaiter().GetResult() : value;
|
|
|
|
|
MessengerInstance.Send(new EntryFieldValueChangedMessage { FieldName = Name, FieldValue = protectedValue, IsProtected = IsProtected });
|
|
|
|
|
Set(nameof(Value), ref _value, protectedValue);
|
2020-05-20 11:59:40 +02:00
|
|
|
|
RaisePropertyChanged(nameof(DisplayValue));
|
2020-05-12 17:14:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-18 22:20:31 +02:00
|
|
|
|
|
2020-05-12 17:14:30 +02:00
|
|
|
|
public bool IsProtected
|
|
|
|
|
{
|
|
|
|
|
get { return _isProtected; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Set(nameof(IsProtected), ref _isProtected, value);
|
2020-05-20 11:59:40 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(Name)) Value = value ? _value : _cryptography.UnProtect(_value).GetAwaiter().GetResult();
|
2020-05-12 17:14:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 14:14:28 +02:00
|
|
|
|
public EntryFieldVm(ICryptographyClient cryptography)
|
|
|
|
|
{
|
|
|
|
|
_cryptography = cryptography;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Initialize(string fieldName, string fieldValue, bool isProtected)
|
2020-05-12 17:14:30 +02:00
|
|
|
|
{
|
|
|
|
|
_name = fieldName;
|
|
|
|
|
_value = fieldValue;
|
|
|
|
|
_isProtected = isProtected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|