mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 08:00:16 -04:00
New Database settings page
Implements Recycle Bin (new group creation still needs to be implemented) Code refactoring
This commit is contained in:
@@ -14,13 +14,13 @@ namespace ModernKeePass.ViewModels
|
||||
public class EntryVm : INotifyPropertyChanged, IPwEntity
|
||||
{
|
||||
public GroupVm ParentGroup { get; }
|
||||
public PwEntry Entry { get; }
|
||||
|
||||
public System.Drawing.Color? BackgroundColor => Entry?.BackgroundColor;
|
||||
public System.Drawing.Color? ForegroundColor => Entry?.ForegroundColor;
|
||||
public System.Drawing.Color? BackgroundColor => _pwEntry?.BackgroundColor;
|
||||
public System.Drawing.Color? ForegroundColor => _pwEntry?.ForegroundColor;
|
||||
public bool IsRevealPasswordEnabled => !string.IsNullOrEmpty(Password);
|
||||
public bool HasExpired => HasExpirationDate && Entry.ExpiryTime < DateTime.Now;
|
||||
public bool HasExpired => HasExpirationDate && _pwEntry.ExpiryTime < DateTime.Now;
|
||||
public double PasswordComplexityIndicator => QualityEstimation.EstimatePasswordBits(Password.ToCharArray());
|
||||
public bool IsFirstItem => _pwEntry == null;
|
||||
|
||||
public double PasswordLength { get; set; } = 25;
|
||||
public bool UpperCasePatternSelected { get; set; } = true;
|
||||
@@ -38,12 +38,12 @@ namespace ModernKeePass.ViewModels
|
||||
get
|
||||
{
|
||||
var title = GetEntryValue(PwDefs.TitleField);
|
||||
return title == null ? "New entry" : title;
|
||||
return title == null ? "< New entry >" : title;
|
||||
}
|
||||
set { SetEntryValue(PwDefs.TitleField, value); }
|
||||
}
|
||||
|
||||
public string Id => Entry.Uuid.ToHexString();
|
||||
public string Id => _pwEntry?.Uuid.ToHexString();
|
||||
|
||||
public string UserName
|
||||
{
|
||||
@@ -75,22 +75,22 @@ namespace ModernKeePass.ViewModels
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Entry == null) return Symbol.Add;
|
||||
if (_pwEntry == null) return Symbol.Add;
|
||||
if (HasExpired) return Symbol.Priority;
|
||||
var result = PwIconToSegoeMapping.GetSymbolFromIcon(Entry.IconId);
|
||||
var result = PwIconToSegoeMapping.GetSymbolFromIcon(_pwEntry.IconId);
|
||||
return result == Symbol.More ? Symbol.Permissions : result;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTimeOffset ExpiryDate
|
||||
{
|
||||
get { return new DateTimeOffset(Entry.ExpiryTime.Date); }
|
||||
set { if (HasExpirationDate) Entry.ExpiryTime = value.DateTime; }
|
||||
get { return new DateTimeOffset(_pwEntry.ExpiryTime.Date); }
|
||||
set { if (HasExpirationDate) _pwEntry.ExpiryTime = value.DateTime; }
|
||||
}
|
||||
public TimeSpan ExpiryTime
|
||||
{
|
||||
get { return Entry.ExpiryTime.TimeOfDay; }
|
||||
set { if (HasExpirationDate) Entry.ExpiryTime = Entry.ExpiryTime.Date.Add(value); }
|
||||
get { return _pwEntry.ExpiryTime.TimeOfDay; }
|
||||
set { if (HasExpirationDate) _pwEntry.ExpiryTime = _pwEntry.ExpiryTime.Date.Add(value); }
|
||||
}
|
||||
|
||||
public bool IsEditMode
|
||||
@@ -114,16 +114,17 @@ namespace ModernKeePass.ViewModels
|
||||
}
|
||||
public bool HasExpirationDate
|
||||
{
|
||||
get { return Entry.Expires; }
|
||||
get { return _pwEntry.Expires; }
|
||||
set
|
||||
{
|
||||
Entry.Expires = value;
|
||||
_pwEntry.Expires = value;
|
||||
NotifyPropertyChanged("HasExpirationDate");
|
||||
}
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private readonly PwEntry _pwEntry;
|
||||
private bool _isEditMode;
|
||||
private bool _isRevealPassword;
|
||||
|
||||
@@ -135,7 +136,7 @@ namespace ModernKeePass.ViewModels
|
||||
public EntryVm() { }
|
||||
public EntryVm(PwEntry entry, GroupVm parent)
|
||||
{
|
||||
Entry = entry;
|
||||
_pwEntry = entry;
|
||||
ParentGroup = parent;
|
||||
}
|
||||
|
||||
@@ -163,7 +164,7 @@ namespace ModernKeePass.ViewModels
|
||||
ProtectedString password;
|
||||
PwGenerator.Generate(out password, pwProfile, null, new CustomPwGeneratorPool());
|
||||
|
||||
Entry?.Strings.Set(PwDefs.PasswordField, password);
|
||||
_pwEntry?.Strings.Set(PwDefs.PasswordField, password);
|
||||
NotifyPropertyChanged("Password");
|
||||
NotifyPropertyChanged("IsRevealPasswordEnabled");
|
||||
NotifyPropertyChanged("PasswordComplexityIndicator");
|
||||
@@ -171,12 +172,12 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
private string GetEntryValue(string key)
|
||||
{
|
||||
return Entry?.Strings.GetSafe(key).ReadString();
|
||||
return _pwEntry?.Strings.GetSafe(key).ReadString();
|
||||
}
|
||||
|
||||
private void SetEntryValue(string key, string newValue)
|
||||
{
|
||||
Entry?.Strings.Set(key, new ProtectedString(true, newValue));
|
||||
_pwEntry?.Strings.Set(key, new ProtectedString(true, newValue));
|
||||
}
|
||||
|
||||
public void MarkForDelete()
|
||||
@@ -187,7 +188,7 @@ namespace ModernKeePass.ViewModels
|
||||
}
|
||||
public void CommitDelete()
|
||||
{
|
||||
Entry.ParentGroup.Entries.Remove(Entry);
|
||||
_pwEntry.ParentGroup.Entries.Remove(_pwEntry);
|
||||
}
|
||||
|
||||
public void UndoDelete()
|
||||
|
@@ -7,11 +7,10 @@ using ModernKeePass.Common;
|
||||
using ModernKeePass.Interfaces;
|
||||
using ModernKeePass.Mappings;
|
||||
using ModernKeePassLib;
|
||||
using System;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class GroupVm : NotifyPropertyChangedBase, IPwEntity
|
||||
public class GroupVm : NotifyPropertyChangedBase, IPwEntity, ISelectableModel
|
||||
{
|
||||
public GroupVm ParentGroup { get; }
|
||||
public ObservableCollection<EntryVm> Entries { get; set; } = new ObservableCollection<EntryVm>();
|
||||
@@ -19,26 +18,33 @@ namespace ModernKeePass.ViewModels
|
||||
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
|
||||
|
||||
public int EntryCount => Entries.Count() - 1;
|
||||
public int GroupCount => Groups.Count - 1;
|
||||
public bool IsNotRoot => ParentGroup != null;
|
||||
public FontWeight FontWeight => _pwGroup == null ? FontWeights.Bold : FontWeights.Normal;
|
||||
public string Id => _pwGroup.Uuid.ToHexString();
|
||||
|
||||
public IOrderedEnumerable<IGrouping<char, EntryVm>> EntriesZoomedOut
|
||||
public int GroupCount => Groups.Count - 1;
|
||||
public PwUuid IdUuid => _pwGroup?.Uuid;
|
||||
public string Id => IdUuid?.ToHexString();
|
||||
public bool IsNotRoot => ParentGroup != null;
|
||||
/// <summary>
|
||||
/// Is the Group the database Recycle Bin?
|
||||
/// </summary>
|
||||
public bool IsSelected
|
||||
{
|
||||
get
|
||||
get { return _app.Database.RecycleBinEnabled && _app.Database.RecycleBin.Id == Id; }
|
||||
set
|
||||
{
|
||||
return from e in Entries
|
||||
where e.Entry != null
|
||||
group e by e.Name.FirstOrDefault() into grp
|
||||
orderby grp.Key
|
||||
select grp;
|
||||
// TODO: if _pwGroup is null, create a new group
|
||||
if (value && _pwGroup != null) _app.Database.RecycleBin = this;
|
||||
}
|
||||
}
|
||||
|
||||
public IOrderedEnumerable<IGrouping<char, EntryVm>> EntriesZoomedOut => from e in Entries
|
||||
where !e.IsFirstItem
|
||||
group e by e.Name.FirstOrDefault() into grp
|
||||
orderby grp.Key
|
||||
select grp;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return _pwGroup == null ? "New group" : _pwGroup.Name; }
|
||||
get { return _pwGroup == null ? "< New group >" : _pwGroup.Name; }
|
||||
set { _pwGroup.Name = value; }
|
||||
}
|
||||
|
||||
@@ -59,18 +65,20 @@ namespace ModernKeePass.ViewModels
|
||||
}
|
||||
|
||||
private readonly PwGroup _pwGroup;
|
||||
private bool _isLeftPaneOpen;
|
||||
private readonly App _app = (App)Application.Current;
|
||||
private bool _isEditMode;
|
||||
|
||||
public GroupVm() {}
|
||||
|
||||
public GroupVm(PwGroup pwGroup, GroupVm parent)
|
||||
public GroupVm(PwGroup pwGroup, GroupVm parent, PwUuid recycleBinId = null)
|
||||
{
|
||||
_pwGroup = pwGroup;
|
||||
ParentGroup = parent;
|
||||
|
||||
if (recycleBinId != null && _pwGroup.Uuid.Equals(recycleBinId)) _app.Database.RecycleBin = this;
|
||||
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)).OrderBy(e => e.Name));
|
||||
Entries.Insert(0, new EntryVm ());
|
||||
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this)).OrderBy(g => g.Name));
|
||||
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this, recycleBinId)).OrderBy(g => g.Name));
|
||||
Groups.Insert(0, new GroupVm ());
|
||||
}
|
||||
|
||||
@@ -94,24 +102,31 @@ namespace ModernKeePass.ViewModels
|
||||
|
||||
public void MarkForDelete()
|
||||
{
|
||||
var app = (App)Application.Current;
|
||||
app.PendingDeleteEntities.Add(Id, this);
|
||||
_app.PendingDeleteEntities.Add(Id, this);
|
||||
ParentGroup.Groups.Remove(this);
|
||||
if (_app.Database.RecycleBinEnabled && !IsSelected) _app.Database.RecycleBin.Groups.Add(this);
|
||||
}
|
||||
|
||||
public void CommitDelete()
|
||||
{
|
||||
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
||||
if (_app.Database.RecycleBinEnabled && !IsSelected) _app.Database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
|
||||
}
|
||||
|
||||
public void UndoDelete()
|
||||
{
|
||||
ParentGroup.Groups.Add(this);
|
||||
if (_app.Database.RecycleBinEnabled && !IsSelected) _app.Database.RecycleBin.Groups.Remove(this);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var app = (App)Application.Current;
|
||||
app.Database.Save();
|
||||
_app.Database.Save();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
30
ModernKeePass/ViewModels/Items/ListMenuItemVm.cs
Normal file
30
ModernKeePass/ViewModels/Items/ListMenuItemVm.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Interfaces;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class ListMenuItemVm : NotifyPropertyChangedBase, IIsEnabled, ISelectableModel
|
||||
{
|
||||
private bool _isSelected;
|
||||
|
||||
public string Title { get; set; }
|
||||
|
||||
public int Group { get; set; } = 0;
|
||||
public Type PageType { get; set; }
|
||||
public Symbol SymbolIcon { get; set; }
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get { return _isSelected; }
|
||||
set { SetProperty(ref _isSelected, value); }
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Title;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,32 +1,10 @@
|
||||
using System;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Interfaces;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class MainMenuItemVm: NotifyPropertyChangedBase, IIsEnabled, ISelectableModel
|
||||
public class MainMenuItemVm: ListMenuItemVm
|
||||
{
|
||||
private bool _isSelected;
|
||||
|
||||
public string Title { get; set; }
|
||||
|
||||
public Type PageType { get; set; }
|
||||
public object Parameter { get; set; }
|
||||
public Frame Destination { get; set; }
|
||||
public int Group { get; set; } = 0;
|
||||
public Symbol SymbolIcon { get; set; }
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get { return _isSelected; }
|
||||
set { SetProperty(ref _isSelected, value); }
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Title;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage;
|
||||
using ModernKeePass.Common;
|
||||
using Windows.Storage.AccessCache;
|
||||
|
||||
|
45
ModernKeePass/ViewModels/Items/SettingsVM.cs
Normal file
45
ModernKeePass/ViewModels/Items/SettingsVM.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Interfaces;
|
||||
using ModernKeePass.Pages;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class SettingsVM : NotifyPropertyChangedBase, IHasSelectableObject
|
||||
{
|
||||
private ListMenuItemVm _selectedItem;
|
||||
|
||||
public ObservableCollection<ListMenuItemVm> MenuItems { get; set; }
|
||||
public ISelectableModel SelectedItem
|
||||
{
|
||||
get { return _selectedItem; }
|
||||
set
|
||||
{
|
||||
if (_selectedItem == value) return;
|
||||
if (_selectedItem != null)
|
||||
{
|
||||
_selectedItem.IsSelected = false;
|
||||
}
|
||||
|
||||
SetProperty(ref _selectedItem, (ListMenuItemVm)value);
|
||||
|
||||
if (_selectedItem != null)
|
||||
{
|
||||
_selectedItem.IsSelected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SettingsVM()
|
||||
{
|
||||
MenuItems = new ObservableCollection<ListMenuItemVm>
|
||||
{
|
||||
new ListMenuItemVm { Title = "Database", SymbolIcon = Symbol.Setting, PageType = typeof(SettingsDatabasePage), IsSelected = true },
|
||||
//new ListMenuItemVm { Title = "General", SymbolIcon = Symbol.Edit, PageType = typeof(SettingsGeneralPage) }
|
||||
};
|
||||
SelectedItem = MenuItems.FirstOrDefault(m => m.IsSelected);
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,7 +7,6 @@ using Windows.UI.Xaml.Controls;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Interfaces;
|
||||
using ModernKeePass.Pages;
|
||||
using ModernKeePass.Pages.BasePages;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
|
67
ModernKeePass/ViewModels/SettingsDatabaseVm.cs
Normal file
67
ModernKeePass/ViewModels/SettingsDatabaseVm.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Windows.Storage;
|
||||
using Windows.UI.Xaml;
|
||||
using ModernKeePass.Common;
|
||||
using ModernKeePass.Interfaces;
|
||||
|
||||
namespace ModernKeePass.ViewModels
|
||||
{
|
||||
public class SettingsDatabaseVm: NotifyPropertyChangedBase, IHasSelectableObject
|
||||
{
|
||||
private readonly App _app = (App)Application.Current;
|
||||
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings;
|
||||
private GroupVm _selectedItem;
|
||||
|
||||
public bool HasRecycleBin
|
||||
{
|
||||
get { return _app.Database.RecycleBinEnabled; }
|
||||
set
|
||||
{
|
||||
_app.Database.RecycleBinEnabled = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<GroupVm> Groups { get; set; }
|
||||
|
||||
public ISelectableModel SelectedItem
|
||||
{
|
||||
get { return Groups.FirstOrDefault(g => g.IsSelected); }
|
||||
set
|
||||
{
|
||||
if (_selectedItem == value) return;
|
||||
if (_selectedItem != null)
|
||||
{
|
||||
_selectedItem.IsSelected = false;
|
||||
}
|
||||
|
||||
SetProperty(ref _selectedItem, (GroupVm)value);
|
||||
|
||||
if (_selectedItem != null)
|
||||
{
|
||||
_selectedItem.IsSelected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SettingsDatabaseVm()
|
||||
{
|
||||
Groups = _app.Database.RootGroup.Groups;
|
||||
}
|
||||
|
||||
// TODO: Move to another setting class (or a static class)
|
||||
private T GetSetting<T>(string property)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (T) Convert.ChangeType(_localSettings.Values[property], typeof(T));
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user