mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Status text and password box border colors are updated according to database status Update composite key in Settings work Some code cleanup
180 lines
6.0 KiB
C#
180 lines
6.0 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Windows.UI.Text;
|
|
using Windows.UI.Xaml;
|
|
using Windows.UI.Xaml.Controls;
|
|
using ModernKeePass.Common;
|
|
using ModernKeePass.Interfaces;
|
|
using ModernKeePass.Mappings;
|
|
using ModernKeePassLib;
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
{
|
|
public class GroupVm : NotifyPropertyChangedBase, IPwEntity, ISelectableModel
|
|
{
|
|
public GroupVm ParentGroup { get; private set; }
|
|
public GroupVm PreviousGroup { get; private set; }
|
|
public ObservableCollection<EntryVm> Entries { get; set; } = new ObservableCollection<EntryVm>();
|
|
|
|
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
|
|
|
|
public int EntryCount => Entries.Count() - 1;
|
|
public FontWeight FontWeight => _pwGroup == null ? FontWeights.Bold : FontWeights.Normal;
|
|
public int GroupCount => Groups.Count - 1;
|
|
public PwUuid IdUuid => _pwGroup?.Uuid;
|
|
public string Id => IdUuid?.ToHexString();
|
|
public bool IsNotRoot => ParentGroup != null;
|
|
|
|
public bool ShowRestore => IsNotRoot && ParentGroup.IsSelected;
|
|
/// <summary>
|
|
/// Is the Group the database Recycle Bin?
|
|
/// </summary>
|
|
public bool IsSelected
|
|
{
|
|
get { return _app.Database.RecycleBinEnabled && _app.Database.RecycleBin?.Id == Id; }
|
|
set
|
|
{
|
|
if (value && _pwGroup != null) _app.Database.RecycleBin = this;
|
|
/*else if (value && _pwGroup == null)
|
|
{
|
|
var recycleBin = _app.Database.RootGroup.AddNewGroup("Recycle bin");
|
|
recycleBin.IsSelected = true;
|
|
recycleBin.IconSymbol = Symbol.Delete;
|
|
}*/
|
|
}
|
|
}
|
|
|
|
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; }
|
|
set { _pwGroup.Name = value; }
|
|
}
|
|
|
|
public Symbol IconSymbol
|
|
{
|
|
get
|
|
{
|
|
if (_pwGroup == null) return Symbol.Add;
|
|
var result = PwIconToSegoeMapping.GetSymbolFromIcon(_pwGroup.IconId);
|
|
return result == Symbol.More ? Symbol.Folder : result;
|
|
}
|
|
set { _pwGroup.IconId = PwIconToSegoeMapping.GetIconFromSymbol(value); }
|
|
}
|
|
|
|
public bool IsEditMode
|
|
{
|
|
get { return _isEditMode; }
|
|
set { SetProperty(ref _isEditMode, value); }
|
|
}
|
|
|
|
public string Path
|
|
{
|
|
get
|
|
{
|
|
if (ParentGroup == null) return string.Empty;
|
|
var path = new StringBuilder(ParentGroup.Path);
|
|
path.Append($" > {ParentGroup.Name}");
|
|
return path.ToString();
|
|
}
|
|
}
|
|
|
|
private readonly PwGroup _pwGroup;
|
|
private readonly App _app = Application.Current as App;
|
|
private bool _isEditMode;
|
|
|
|
public GroupVm() {}
|
|
|
|
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, recycleBinId)).OrderBy(g => g.Name));
|
|
Groups.Insert(0, new GroupVm ());
|
|
}
|
|
|
|
public GroupVm AddNewGroup(string name = "")
|
|
{
|
|
var pwGroup = new PwGroup(true, true, name, PwIcon.Folder);
|
|
_pwGroup.AddGroup(pwGroup, true);
|
|
var newGroup = new GroupVm(pwGroup, this) {Name = name, IsEditMode = string.IsNullOrEmpty(name)};
|
|
Groups.Add(newGroup);
|
|
return newGroup;
|
|
}
|
|
|
|
public EntryVm AddNewEntry()
|
|
{
|
|
var pwEntry = new PwEntry(true, true);
|
|
_pwGroup.AddEntry(pwEntry, true);
|
|
var newEntry = new EntryVm(pwEntry, this) {IsEditMode = true};
|
|
Entries.Add(newEntry);
|
|
return newEntry;
|
|
}
|
|
|
|
public void AddPwEntry(PwEntry entry)
|
|
{
|
|
_pwGroup.AddEntry(entry, true);
|
|
}
|
|
|
|
public void RemovePwEntry(PwEntry entry)
|
|
{
|
|
_pwGroup.Entries.Remove(entry);
|
|
}
|
|
|
|
public void MarkForDelete()
|
|
{
|
|
if (_app.Database.RecycleBinEnabled && _app.Database.RecycleBin?.IdUuid == null)
|
|
_app.Database.CreateRecycleBin();
|
|
Move(_app.Database.RecycleBinEnabled && !IsSelected ? _app.Database.RecycleBin : null);
|
|
}
|
|
|
|
|
|
public void UndoDelete()
|
|
{
|
|
Move(PreviousGroup);
|
|
}
|
|
|
|
public void Move(GroupVm destination)
|
|
{
|
|
PreviousGroup = ParentGroup;
|
|
PreviousGroup.Groups.Remove(this);
|
|
PreviousGroup._pwGroup.Groups.Remove(_pwGroup);
|
|
if (destination == null)
|
|
{
|
|
_app.Database.AddDeletedItem(IdUuid);
|
|
return;
|
|
}
|
|
ParentGroup = destination;
|
|
ParentGroup.Groups.Add(this);
|
|
ParentGroup._pwGroup.AddGroup(_pwGroup, true);
|
|
}
|
|
|
|
public void CommitDelete()
|
|
{
|
|
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
|
if (_app.Database.RecycleBinEnabled && !PreviousGroup.IsSelected) _app.Database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
|
|
else _app.Database.AddDeletedItem(IdUuid);
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
_app.Database.Save();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return Name;
|
|
}
|
|
}
|
|
}
|