2017-10-13 11:48:58 +02:00
|
|
|
|
using System.ComponentModel;
|
2017-10-05 18:40:24 +02:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-09-29 18:08:20 +02:00
|
|
|
|
using ModernKeePass.Mappings;
|
2017-09-26 15:38:58 +02:00
|
|
|
|
using ModernKeePassLib;
|
2017-10-16 18:31:45 +02:00
|
|
|
|
using ModernKeePassLib.Cryptography.PasswordGenerator;
|
2017-09-26 15:38:58 +02:00
|
|
|
|
using ModernKeePassLib.Security;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using System;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2017-10-17 18:46:05 +02:00
|
|
|
|
public class EntryVm : INotifyPropertyChanged, IPwEntity
|
2017-09-12 18:20:32 +02:00
|
|
|
|
{
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public GroupVm ParentGroup { get; }
|
2017-10-06 14:56:16 +02:00
|
|
|
|
public PwEntry Entry { get; }
|
|
|
|
|
|
|
|
|
|
public System.Drawing.Color? BackgroundColor => Entry?.BackgroundColor;
|
|
|
|
|
public System.Drawing.Color? ForegroundColor => Entry?.ForegroundColor;
|
2017-10-12 18:37:49 +02:00
|
|
|
|
public bool IsRevealPasswordEnabled => !string.IsNullOrEmpty(Password);
|
2017-10-05 18:40:24 +02:00
|
|
|
|
|
2017-10-16 18:31:45 +02:00
|
|
|
|
public double PasswordLength { get; set; } = 25;
|
|
|
|
|
public bool UpperCasePatternSelected { get; set; } = true;
|
|
|
|
|
public bool LowerCasePatternSelected { get; set; } = true;
|
|
|
|
|
public bool DigitsPatternSelected { get; set; } = true;
|
|
|
|
|
public bool MinusPatternSelected { get; set; }
|
|
|
|
|
public bool UnderscorePatternSelected { get; set; }
|
|
|
|
|
public bool SpacePatternSelected { get; set; }
|
|
|
|
|
public bool SpecialPatternSelected { get; set; }
|
|
|
|
|
public bool BracketsPatternSelected { get; set; }
|
|
|
|
|
public string CustomChars { get; set; } = string.Empty;
|
|
|
|
|
|
2017-10-17 18:46:05 +02:00
|
|
|
|
public string Name
|
2017-09-26 14:32:15 +02:00
|
|
|
|
{
|
2017-10-02 18:40:54 +02:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var title = GetEntryValue(PwDefs.TitleField);
|
|
|
|
|
return title == null ? "New entry" : title;
|
|
|
|
|
}
|
2017-09-26 14:32:15 +02:00
|
|
|
|
set { SetEntryValue(PwDefs.TitleField, value); }
|
|
|
|
|
}
|
2017-10-11 11:20:05 +02:00
|
|
|
|
|
|
|
|
|
public string Id => Entry.Uuid.ToHexString();
|
|
|
|
|
|
2017-09-26 14:32:15 +02:00
|
|
|
|
public string UserName
|
|
|
|
|
{
|
|
|
|
|
get { return GetEntryValue(PwDefs.UserNameField); }
|
|
|
|
|
set { SetEntryValue(PwDefs.UserNameField, value); }
|
|
|
|
|
}
|
|
|
|
|
public string Password
|
|
|
|
|
{
|
|
|
|
|
get { return GetEntryValue(PwDefs.PasswordField); }
|
2017-10-13 11:48:58 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetEntryValue(PwDefs.PasswordField, value);
|
|
|
|
|
NotifyPropertyChanged("Password");
|
|
|
|
|
}
|
2017-09-26 14:32:15 +02:00
|
|
|
|
}
|
|
|
|
|
public string Url
|
|
|
|
|
{
|
|
|
|
|
get { return GetEntryValue(PwDefs.UrlField); }
|
|
|
|
|
set { SetEntryValue(PwDefs.UrlField, value); }
|
|
|
|
|
}
|
|
|
|
|
public string Notes
|
|
|
|
|
{
|
|
|
|
|
get { return GetEntryValue(PwDefs.NotesField); }
|
|
|
|
|
set { SetEntryValue(PwDefs.NotesField, value); }
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 18:08:20 +02:00
|
|
|
|
public Symbol IconSymbol
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-10-06 14:56:16 +02:00
|
|
|
|
if (Entry == null) return Symbol.Add;
|
|
|
|
|
var result = PwIconToSegoeMapping.GetSymbolFromIcon(Entry.IconId);
|
2017-09-29 18:08:20 +02:00
|
|
|
|
return result == Symbol.More ? Symbol.Permissions : result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-06 14:56:16 +02:00
|
|
|
|
|
|
|
|
|
public bool IsEditMode
|
|
|
|
|
{
|
|
|
|
|
get { return _isEditMode; }
|
2017-10-13 11:48:58 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isEditMode = value;
|
|
|
|
|
NotifyPropertyChanged("IsEditMode");
|
|
|
|
|
}
|
2017-10-06 14:56:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 17:57:36 +02:00
|
|
|
|
public bool IsRevealPassword
|
|
|
|
|
{
|
|
|
|
|
get { return _isRevealPassword; }
|
2017-10-13 11:48:58 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_isRevealPassword = value;
|
|
|
|
|
NotifyPropertyChanged("IsRevealPassword");
|
|
|
|
|
}
|
2017-10-06 17:57:36 +02:00
|
|
|
|
}
|
2017-10-16 18:31:45 +02:00
|
|
|
|
|
2017-10-13 11:48:58 +02:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
2017-10-06 14:56:16 +02:00
|
|
|
|
private bool _isEditMode;
|
2017-10-06 17:57:36 +02:00
|
|
|
|
private bool _isRevealPassword;
|
2017-10-13 11:48:58 +02:00
|
|
|
|
private void NotifyPropertyChanged(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
2017-09-13 18:37:44 +02:00
|
|
|
|
|
2017-09-14 18:33:29 +02:00
|
|
|
|
public EntryVm() { }
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public EntryVm(PwEntry entry, GroupVm parent)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2017-10-06 14:56:16 +02:00
|
|
|
|
Entry = entry;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
ParentGroup = parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveEntry()
|
|
|
|
|
{
|
|
|
|
|
ParentGroup.RemoveEntry(this);
|
2017-09-13 18:37:44 +02:00
|
|
|
|
}
|
2017-09-15 11:24:14 +02:00
|
|
|
|
|
2017-10-16 18:31:45 +02:00
|
|
|
|
public void GeneratePassword()
|
|
|
|
|
{
|
|
|
|
|
var pwProfile = new PwProfile()
|
|
|
|
|
{
|
|
|
|
|
GeneratorType = PasswordGeneratorType.CharSet,
|
|
|
|
|
Length = (uint)PasswordLength,
|
|
|
|
|
CharSet = new PwCharSet()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (UpperCasePatternSelected) pwProfile.CharSet.Add(PwCharSet.UpperCase);
|
|
|
|
|
if (LowerCasePatternSelected) pwProfile.CharSet.Add(PwCharSet.LowerCase);
|
|
|
|
|
if (DigitsPatternSelected) pwProfile.CharSet.Add(PwCharSet.Digits);
|
|
|
|
|
if (SpecialPatternSelected) pwProfile.CharSet.Add(PwCharSet.SpecialChars);
|
|
|
|
|
if (MinusPatternSelected) pwProfile.CharSet.Add('-');
|
|
|
|
|
if (UnderscorePatternSelected) pwProfile.CharSet.Add('_');
|
|
|
|
|
if (SpacePatternSelected) pwProfile.CharSet.Add(' ');
|
|
|
|
|
if (BracketsPatternSelected) pwProfile.CharSet.Add(PwCharSet.Brackets);
|
|
|
|
|
|
|
|
|
|
pwProfile.CharSet.Add(CustomChars);
|
|
|
|
|
|
|
|
|
|
ProtectedString password;
|
|
|
|
|
PwGenerator.Generate(out password, pwProfile, null, new CustomPwGeneratorPool());
|
|
|
|
|
|
|
|
|
|
Entry?.Strings.Set(PwDefs.PasswordField, password);
|
|
|
|
|
NotifyPropertyChanged("Password");
|
|
|
|
|
NotifyPropertyChanged("IsRevealPasswordEnabled");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 14:32:15 +02:00
|
|
|
|
private string GetEntryValue(string key)
|
|
|
|
|
{
|
2017-10-06 14:56:16 +02:00
|
|
|
|
return Entry?.Strings.GetSafe(key).ReadString();
|
2017-09-26 14:32:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetEntryValue(string key, string newValue)
|
|
|
|
|
{
|
2017-10-06 14:56:16 +02:00
|
|
|
|
Entry?.Strings.Set(key, new ProtectedString(true, newValue));
|
2017-09-26 14:32:15 +02:00
|
|
|
|
}
|
2017-10-17 18:46:05 +02:00
|
|
|
|
|
|
|
|
|
public void CommitDelete()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2017-09-12 18:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|