2020-03-27 13:27:29 +01:00
|
|
|
|
using System.Collections.Generic;
|
2017-11-28 18:53:10 +01:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Collections.Specialized;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
using System.Linq;
|
2018-06-20 18:41:56 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Input;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using ModernKeePass.Application.Database.Commands.SaveDatabase;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Models;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.AddEntry;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.AddGroup;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.CreateEntry;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.CreateGroup;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.DeleteGroup;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.InsertEntry;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.RemoveEntry;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.RemoveGroup;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.SortEntries;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.SortGroups;
|
2017-10-03 16:06:49 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using ModernKeePass.Domain.Enums;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public class GroupVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
|
2017-09-12 18:20:32 +02:00
|
|
|
|
{
|
2017-10-31 18:49:18 +01:00
|
|
|
|
public GroupVm ParentGroup { get; private set; }
|
|
|
|
|
public GroupVm PreviousGroup { get; private set; }
|
2017-12-04 18:07:03 +01:00
|
|
|
|
|
|
|
|
|
public ObservableCollection<EntryVm> Entries
|
|
|
|
|
{
|
|
|
|
|
get { return _entries; }
|
2018-06-08 12:08:06 +02:00
|
|
|
|
private set { SetProperty(ref _entries, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<EntryVm> SubEntries
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var subEntries = new List<EntryVm>();
|
|
|
|
|
subEntries.AddRange(Entries);
|
|
|
|
|
foreach (var group in Groups)
|
|
|
|
|
{
|
|
|
|
|
subEntries.AddRange(group.SubEntries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return subEntries;
|
|
|
|
|
}
|
2017-12-04 18:07:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
|
2017-12-06 18:29:19 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public string Id => _group.Id;
|
2017-10-05 14:50:42 +02:00
|
|
|
|
public bool IsNotRoot => ParentGroup != null;
|
2018-06-08 12:08:06 +02:00
|
|
|
|
|
2017-10-31 18:49:18 +01:00
|
|
|
|
public bool ShowRestore => IsNotRoot && ParentGroup.IsSelected;
|
2017-12-04 12:20:05 +01:00
|
|
|
|
|
2020-03-27 18:45:13 +01:00
|
|
|
|
public bool IsRecycleOnDelete => GetDatabase().IsRecycleBinEnabled && !IsSelected && !ParentGroup.IsSelected;
|
2018-06-15 18:07:44 +02:00
|
|
|
|
|
2017-10-30 18:34:38 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Is the Group the database Recycle Bin?
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsSelected
|
2017-10-06 16:21:12 -04:00
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var database = GetDatabase();
|
|
|
|
|
return database.IsRecycleBinEnabled && database.RecycleBinId == Id;
|
|
|
|
|
}
|
2017-10-30 18:34:38 +01:00
|
|
|
|
set
|
2017-10-06 16:21:12 -04:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
if (value && _group != null) _database.RecycleBin = this;
|
2017-10-06 16:21:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 18:34:38 +01:00
|
|
|
|
public IOrderedEnumerable<IGrouping<char, EntryVm>> EntriesZoomedOut => from e in Entries
|
2020-03-27 13:27:29 +01:00
|
|
|
|
group e by e.Title.ToUpper().FirstOrDefault() into grp
|
2017-10-30 18:34:38 +01:00
|
|
|
|
orderby grp.Key
|
|
|
|
|
select grp;
|
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public string Title
|
2017-10-06 14:56:16 +02:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
get { return _group == null ? string.Empty : _group.Title; }
|
|
|
|
|
set { _group.Title = value; }
|
2017-10-06 14:56:16 +02:00
|
|
|
|
}
|
2017-10-06 16:21:12 -04:00
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public int Icon
|
2017-09-15 15:58:51 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
if (_group?.Icon != null) return (int) _group?.Icon;
|
2018-06-14 10:20:00 +02:00
|
|
|
|
return -1;
|
2017-09-15 15:58:51 +02:00
|
|
|
|
}
|
2020-03-26 12:25:22 +01:00
|
|
|
|
set { _group.Icon = (Icon)value; }
|
2017-09-15 15:58:51 +02:00
|
|
|
|
}
|
2017-10-25 18:29:50 +02:00
|
|
|
|
|
2017-10-06 14:56:16 +02:00
|
|
|
|
public bool IsEditMode
|
|
|
|
|
{
|
|
|
|
|
get { return _isEditMode; }
|
2018-08-02 17:40:30 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetProperty(ref _isEditMode, value);
|
|
|
|
|
((RelayCommand)SortEntriesCommand).RaiseCanExecuteChanged();
|
|
|
|
|
((RelayCommand)SortGroupsCommand).RaiseCanExecuteChanged();
|
|
|
|
|
}
|
2017-10-06 14:56:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 18:29:19 +01:00
|
|
|
|
public bool IsMenuClosed
|
|
|
|
|
{
|
|
|
|
|
get { return _isMenuClosed; }
|
|
|
|
|
set { SetProperty(ref _isMenuClosed, value); }
|
|
|
|
|
}
|
2018-06-08 18:46:07 +02:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public IEnumerable<IVmEntity> BreadCrumb
|
2018-06-08 18:46:07 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-06-12 18:40:54 +02:00
|
|
|
|
var groups = new Stack<GroupVm>();
|
2018-06-08 18:46:07 +02:00
|
|
|
|
var group = this;
|
|
|
|
|
while (group.ParentGroup != null)
|
|
|
|
|
{
|
|
|
|
|
group = group.ParentGroup;
|
2018-06-12 18:40:54 +02:00
|
|
|
|
groups.Push(group);
|
2018-06-08 18:46:07 +02:00
|
|
|
|
}
|
2018-06-12 09:42:08 +02:00
|
|
|
|
|
|
|
|
|
return groups;
|
2018-06-08 18:46:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
|
|
|
|
public ICommand SaveCommand { get; }
|
|
|
|
|
public ICommand SortEntriesCommand { get; }
|
|
|
|
|
public ICommand SortGroupsCommand { get; }
|
|
|
|
|
public ICommand UndoDeleteCommand { get; }
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
private readonly Application.Group.Models.GroupVm _group;
|
|
|
|
|
private readonly IMediator _mediator;
|
2017-10-06 14:56:16 +02:00
|
|
|
|
private bool _isEditMode;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
private Application.Entry.Models.EntryVm _reorderedEntry;
|
2017-12-04 18:07:03 +01:00
|
|
|
|
private ObservableCollection<EntryVm> _entries = new ObservableCollection<EntryVm>();
|
2017-12-06 18:29:19 +01:00
|
|
|
|
private bool _isMenuClosed = true;
|
2017-10-05 14:50:42 +02:00
|
|
|
|
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public GroupVm() {}
|
2017-12-01 17:59:38 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
internal GroupVm(Application.Group.Models.GroupVm group, GroupVm parent, string recycleBinId = null) : this(group, parent, App.Mediator, recycleBinId)
|
2017-11-23 15:26:57 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public GroupVm(Application.Group.Models.GroupVm group, GroupVm parent, IMediator mediator, string recycleBinId = null)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
_group = group;
|
|
|
|
|
_mediator = mediator;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
ParentGroup = parent;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
SaveCommand = new RelayCommand(async () => await _mediator.Send(new SaveDatabaseCommand()));
|
2018-06-20 18:41:56 +02:00
|
|
|
|
SortEntriesCommand = new RelayCommand(async () =>
|
2018-08-02 17:40:30 +02:00
|
|
|
|
await SortEntriesAsync().ConfigureAwait(false), () => IsEditMode);
|
2018-06-20 18:41:56 +02:00
|
|
|
|
SortGroupsCommand = new RelayCommand(async () =>
|
2018-08-02 17:40:30 +02:00
|
|
|
|
await SortGroupsAsync().ConfigureAwait(false), () => IsEditMode);
|
2020-03-27 18:45:13 +01:00
|
|
|
|
UndoDeleteCommand = new RelayCommand(async () => await Move(PreviousGroup), () => PreviousGroup != null);
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
if (recycleBinId != null && _group.Id.Equals(recycleBinId)) _database.RecycleBin = this;
|
|
|
|
|
Entries = new ObservableCollection<EntryVm>(group.Entries.Select(e => new EntryVm(e, this)));
|
2017-11-28 18:53:10 +01:00
|
|
|
|
Entries.CollectionChanged += Entries_CollectionChanged;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
Groups = new ObservableCollection<GroupVm>(group.SubGroups.Select(g => new GroupVm(g, this, recycleBinId)));
|
2017-09-13 18:37:44 +02:00
|
|
|
|
}
|
2018-02-23 18:09:21 +01:00
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
private async void Entries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2017-11-28 18:53:10 +01:00
|
|
|
|
{
|
|
|
|
|
switch (e.Action)
|
|
|
|
|
{
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
2020-03-27 13:27:29 +01:00
|
|
|
|
var oldIndex = e.OldStartingIndex;
|
|
|
|
|
_reorderedEntry = _group.Entries[oldIndex];
|
|
|
|
|
await _mediator.Send(new RemoveEntryCommand {Entry = _reorderedEntry, ParentGroup = _group});
|
2017-11-28 18:53:10 +01:00
|
|
|
|
break;
|
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
2020-03-27 13:27:29 +01:00
|
|
|
|
if (_reorderedEntry == null)
|
|
|
|
|
{
|
|
|
|
|
var entry = ((EntryVm) e.NewItems[0]).GetEntry();
|
|
|
|
|
await _mediator.Send(new AddEntryCommand {Entry = entry, ParentGroup = _group});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new InsertEntryCommand {Entry = _reorderedEntry, ParentGroup = _group, Index = e.NewStartingIndex});
|
|
|
|
|
}
|
2017-11-28 18:53:10 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-23 18:09:21 +01:00
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public async Task<GroupVm> AddNewGroup(string name = "")
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
var newGroup = await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group});
|
|
|
|
|
var newGroupVm = new GroupVm(newGroup, this) {Title = name, IsEditMode = string.IsNullOrEmpty(name)};
|
|
|
|
|
Groups.Add(newGroupVm);
|
|
|
|
|
return newGroupVm;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public async Task<EntryVm> AddNewEntry()
|
2017-09-25 18:34:27 +02:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
var newEntry = await _mediator.Send(new CreateEntryCommand { ParentGroup = _group });
|
|
|
|
|
var newEntryVm = new EntryVm(newEntry, this) {IsEditMode = true};
|
|
|
|
|
await newEntryVm.GeneratePassword();
|
|
|
|
|
Entries.Add(newEntryVm);
|
|
|
|
|
return newEntryVm;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
}
|
2017-10-31 12:14:26 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public async Task MarkForDelete(string recycleBinTitle)
|
2017-10-17 18:46:05 +02:00
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
var database = GetDatabase();
|
|
|
|
|
if (database.IsRecycleBinEnabled && database.RecycleBinId == null)
|
|
|
|
|
await _mediator.Send(new CreateGroupCommand {ParentGroup = database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
|
|
|
|
|
await Move(database.IsRecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
|
2018-08-02 17:40:30 +02:00
|
|
|
|
((RelayCommand)UndoDeleteCommand).RaiseCanExecuteChanged();
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
2020-03-27 18:45:13 +01:00
|
|
|
|
public async Task UndoDelete()
|
2017-10-18 10:32:51 +02:00
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
await Move(PreviousGroup);
|
2017-10-31 18:49:18 +01:00
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2020-03-27 18:45:13 +01:00
|
|
|
|
public async Task Move(GroupVm destination)
|
2017-10-31 18:49:18 +01:00
|
|
|
|
{
|
|
|
|
|
PreviousGroup = ParentGroup;
|
|
|
|
|
PreviousGroup.Groups.Remove(this);
|
2020-03-27 18:45:13 +01:00
|
|
|
|
await _mediator.Send(new RemoveGroupCommand {ParentGroup = PreviousGroup._group, Group = _group});
|
2017-10-31 18:49:18 +01:00
|
|
|
|
if (destination == null)
|
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
await _mediator.Send(new DeleteGroupCommand { Group = _group });
|
2017-10-31 18:49:18 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ParentGroup = destination;
|
2017-10-18 10:32:51 +02:00
|
|
|
|
ParentGroup.Groups.Add(this);
|
2020-03-27 18:45:13 +01:00
|
|
|
|
await _mediator.Send(new AddGroupCommand {ParentGroup = ParentGroup._group, Group = _group});
|
2017-10-31 18:49:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public async Task CommitDelete()
|
2017-10-31 18:49:18 +01:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new DeleteGroupCommand { Group = _group });
|
2017-10-18 10:32:51 +02:00
|
|
|
|
}
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
2017-10-25 18:29:50 +02:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
return Title;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
}
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
|
|
|
|
private async Task SortEntriesAsync()
|
2017-11-28 18:53:10 +01:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new SortEntriesCommand {Group = _group});
|
|
|
|
|
Entries = new ObservableCollection<EntryVm>(Entries.OrderBy(e => e.Title));
|
2017-11-30 11:05:47 +01:00
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2018-06-20 18:41:56 +02:00
|
|
|
|
private async Task SortGroupsAsync()
|
2017-11-30 11:05:47 +01:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new SortGroupsCommand {Group = _group});
|
|
|
|
|
Groups = new ObservableCollection<GroupVm>(Groups.OrderBy(g => g.Title).ThenBy(g => g._group == null));
|
|
|
|
|
// TODO: should not be needed
|
|
|
|
|
OnPropertyChanged(nameof(Groups));
|
2017-11-28 18:53:10 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 18:45:13 +01:00
|
|
|
|
private DatabaseVm GetDatabase()
|
2020-03-26 12:25:22 +01:00
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
return _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
|
2020-03-26 12:25:22 +01:00
|
|
|
|
}
|
2017-09-12 18:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|