2017-11-28 18:53:10 +01:00
|
|
|
|
using System;
|
2018-06-08 12:08:06 +02: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;
|
2017-10-31 12:14:26 +01:00
|
|
|
|
using System.Text;
|
2017-09-29 18:08:20 +02:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2017-10-03 16:06:49 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-09-29 18:08:20 +02:00
|
|
|
|
using ModernKeePass.Mappings;
|
2018-02-23 18:09:21 +01:00
|
|
|
|
using ModernKeePass.Services;
|
2017-09-26 15:38:58 +02:00
|
|
|
|
using ModernKeePassLib;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2017-10-30 18:34:38 +01:00
|
|
|
|
public class GroupVm : NotifyPropertyChangedBase, IPwEntity, 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
|
|
|
|
|
2017-10-30 18:34:38 +01:00
|
|
|
|
public PwUuid IdUuid => _pwGroup?.Uuid;
|
|
|
|
|
public string Id => IdUuid?.ToHexString();
|
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
|
|
|
|
|
|
|
|
|
public bool IsRecycleOnDelete => _database.RecycleBinEnabled && !IsSelected && !ParentGroup.IsSelected;
|
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
|
|
|
|
{
|
2017-11-28 16:57:16 -05:00
|
|
|
|
get { return _database != null && _database.RecycleBinEnabled && _database.RecycleBin?.Id == Id; }
|
2017-10-30 18:34:38 +01:00
|
|
|
|
set
|
2017-10-06 16:21:12 -04:00
|
|
|
|
{
|
2017-11-23 15:26:57 +01:00
|
|
|
|
if (value && _pwGroup != 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
|
2018-06-05 16:23:09 +02:00
|
|
|
|
group e by e.Name.ToUpper().FirstOrDefault() into grp
|
2017-10-30 18:34:38 +01:00
|
|
|
|
orderby grp.Key
|
|
|
|
|
select grp;
|
|
|
|
|
|
2017-10-06 14:56:16 +02:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
2017-12-01 17:59:38 +01:00
|
|
|
|
get { return _pwGroup == null ? string.Empty : _pwGroup.Name; }
|
2017-10-06 14:56:16 +02:00
|
|
|
|
set { _pwGroup.Name = value; }
|
|
|
|
|
}
|
2017-10-06 16:21:12 -04:00
|
|
|
|
|
2017-09-29 18:08:20 +02:00
|
|
|
|
public Symbol IconSymbol
|
2017-09-15 15:58:51 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-09-29 18:08:20 +02:00
|
|
|
|
var result = PwIconToSegoeMapping.GetSymbolFromIcon(_pwGroup.IconId);
|
|
|
|
|
return result == Symbol.More ? Symbol.Folder : result;
|
2017-09-15 15:58:51 +02:00
|
|
|
|
}
|
2017-10-31 12:14:26 +01:00
|
|
|
|
set { _pwGroup.IconId = PwIconToSegoeMapping.GetIconFromSymbol(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; }
|
|
|
|
|
set { SetProperty(ref _isEditMode, value); }
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
public ObservableCollection<GroupVm> BreadCrumb
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-06-12 09:42:08 +02:00
|
|
|
|
var groups = new ObservableCollection<GroupVm>();
|
2018-06-08 18:46:07 +02:00
|
|
|
|
var group = this;
|
|
|
|
|
while (group.ParentGroup != null)
|
|
|
|
|
{
|
|
|
|
|
group = group.ParentGroup;
|
2018-06-12 09:42:08 +02:00
|
|
|
|
groups.Insert(0, 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
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-14 17:54:14 +01:00
|
|
|
|
|
2017-10-31 12:14:26 +01:00
|
|
|
|
public string Path
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (ParentGroup == null) return string.Empty;
|
|
|
|
|
var path = new StringBuilder(ParentGroup.Path);
|
|
|
|
|
path.Append($" > {ParentGroup.Name}");
|
|
|
|
|
return path.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-02 18:40:54 +02:00
|
|
|
|
private readonly PwGroup _pwGroup;
|
2018-02-23 18:09:21 +01:00
|
|
|
|
private readonly IDatabaseService _database;
|
2017-10-06 14:56:16 +02:00
|
|
|
|
private bool _isEditMode;
|
2017-11-28 18:53:10 +01:00
|
|
|
|
private PwEntry _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
|
|
|
|
|
2017-11-23 15:26:57 +01:00
|
|
|
|
internal GroupVm(PwGroup pwGroup, GroupVm parent, PwUuid recycleBinId = null) : this(pwGroup, parent,
|
2018-02-23 18:09:21 +01:00
|
|
|
|
DatabaseService.Instance, recycleBinId)
|
2017-11-23 15:26:57 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
2018-02-23 18:09:21 +01:00
|
|
|
|
public GroupVm(PwGroup pwGroup, GroupVm parent, IDatabaseService database, PwUuid recycleBinId = null)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2017-09-25 18:34:27 +02:00
|
|
|
|
_pwGroup = pwGroup;
|
2017-11-23 15:26:57 +01:00
|
|
|
|
_database = database;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
ParentGroup = parent;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
2017-11-23 15:26:57 +01:00
|
|
|
|
if (recycleBinId != null && _pwGroup.Uuid.Equals(recycleBinId)) _database.RecycleBin = this;
|
2017-11-28 18:53:10 +01:00
|
|
|
|
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)));
|
|
|
|
|
Entries.CollectionChanged += Entries_CollectionChanged;
|
|
|
|
|
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this, recycleBinId)));
|
2017-09-13 18:37:44 +02:00
|
|
|
|
}
|
2018-02-23 18:09:21 +01:00
|
|
|
|
|
2017-11-28 18:53:10 +01:00
|
|
|
|
private void Entries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
switch (e.Action)
|
|
|
|
|
{
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
|
var oldIndex = (uint) e.OldStartingIndex;
|
|
|
|
|
_reorderedEntry = _pwGroup.Entries.GetAt(oldIndex);
|
|
|
|
|
_pwGroup.Entries.RemoveAt(oldIndex);
|
|
|
|
|
break;
|
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
2017-11-29 19:13:38 +01:00
|
|
|
|
if (_reorderedEntry == null) _pwGroup.AddEntry(((EntryVm) e.NewItems[0]).GetPwEntry(), true);
|
2017-11-28 18:53:10 +01:00
|
|
|
|
else _pwGroup.Entries.Insert((uint)e.NewStartingIndex, _reorderedEntry);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-23 18:09:21 +01:00
|
|
|
|
|
2017-10-31 12:14:26 +01:00
|
|
|
|
public GroupVm AddNewGroup(string name = "")
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
2017-10-31 12:14:26 +01:00
|
|
|
|
var pwGroup = new PwGroup(true, true, name, PwIcon.Folder);
|
2017-09-25 18:34:27 +02:00
|
|
|
|
_pwGroup.AddGroup(pwGroup, true);
|
2017-10-31 12:14:26 +01:00
|
|
|
|
var newGroup = new GroupVm(pwGroup, this) {Name = name, IsEditMode = string.IsNullOrEmpty(name)};
|
2017-10-13 15:46:41 +02:00
|
|
|
|
Groups.Add(newGroup);
|
|
|
|
|
return newGroup;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2017-10-31 12:14:26 +01:00
|
|
|
|
public EntryVm AddNewEntry()
|
2017-09-25 18:34:27 +02:00
|
|
|
|
{
|
|
|
|
|
var pwEntry = new PwEntry(true, true);
|
2017-10-13 15:46:41 +02:00
|
|
|
|
var newEntry = new EntryVm(pwEntry, this) {IsEditMode = true};
|
2017-11-27 15:26:36 +01:00
|
|
|
|
newEntry.GeneratePassword();
|
2017-10-13 15:46:41 +02:00
|
|
|
|
Entries.Add(newEntry);
|
|
|
|
|
return newEntry;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
}
|
2017-10-31 12:14:26 +01:00
|
|
|
|
|
2018-03-12 17:30:03 +01:00
|
|
|
|
public void MarkForDelete(string recycleBinTitle)
|
2017-10-17 18:46:05 +02:00
|
|
|
|
{
|
2017-11-23 15:26:57 +01:00
|
|
|
|
if (_database.RecycleBinEnabled && _database.RecycleBin?.IdUuid == null)
|
2018-03-12 17:30:03 +01:00
|
|
|
|
_database.CreateRecycleBin(recycleBinTitle);
|
2017-11-23 15:26:57 +01:00
|
|
|
|
Move(_database.RecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
2017-10-18 10:32:51 +02:00
|
|
|
|
public void UndoDelete()
|
|
|
|
|
{
|
2017-10-31 18:49:18 +01:00
|
|
|
|
Move(PreviousGroup);
|
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2017-10-31 18:49:18 +01:00
|
|
|
|
public void Move(GroupVm destination)
|
|
|
|
|
{
|
|
|
|
|
PreviousGroup = ParentGroup;
|
|
|
|
|
PreviousGroup.Groups.Remove(this);
|
|
|
|
|
PreviousGroup._pwGroup.Groups.Remove(_pwGroup);
|
|
|
|
|
if (destination == null)
|
|
|
|
|
{
|
2017-11-23 15:26:57 +01:00
|
|
|
|
_database.AddDeletedItem(IdUuid);
|
2017-10-31 18:49:18 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ParentGroup = destination;
|
2017-10-18 10:32:51 +02:00
|
|
|
|
ParentGroup.Groups.Add(this);
|
2017-10-31 18:49:18 +01:00
|
|
|
|
ParentGroup._pwGroup.AddGroup(_pwGroup, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CommitDelete()
|
|
|
|
|
{
|
|
|
|
|
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
2017-11-23 15:26:57 +01:00
|
|
|
|
if (_database.RecycleBinEnabled && !PreviousGroup.IsSelected) _database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
|
|
|
|
|
else _database.AddDeletedItem(IdUuid);
|
2017-10-18 10:32:51 +02:00
|
|
|
|
}
|
2017-10-25 18:29:50 +02:00
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
2017-11-23 15:26:57 +01:00
|
|
|
|
_database.Save();
|
2017-10-30 18:34:38 +01:00
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2017-11-28 18:53:10 +01:00
|
|
|
|
public void SortEntries()
|
|
|
|
|
{
|
2017-11-30 11:05:47 +01:00
|
|
|
|
var comparer = new PwEntryComparer(PwDefs.TitleField, true, false);
|
2017-11-28 18:53:10 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_pwGroup.Entries.Sort(comparer);
|
|
|
|
|
Entries = new ObservableCollection<EntryVm>(Entries.OrderBy(e => e.Name));
|
2017-11-30 11:05:47 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-12-01 17:59:38 +01:00
|
|
|
|
MessageDialogHelper.ShowErrorDialog(e);
|
2017-11-30 11:05:47 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2017-11-30 11:05:47 +01:00
|
|
|
|
public void SortGroups()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_pwGroup.SortSubGroups(false);
|
2017-11-30 18:56:56 +01:00
|
|
|
|
Groups = new ObservableCollection<GroupVm>(Groups.OrderBy(g => g.Name).ThenBy(g => g._pwGroup == null));
|
2017-11-30 11:05:47 +01:00
|
|
|
|
OnPropertyChanged("Groups");
|
2017-11-28 18:53:10 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-12-01 17:59:38 +01:00
|
|
|
|
MessageDialogHelper.ShowErrorDialog(e);
|
2017-11-28 18:53:10 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 18:34:38 +01:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Name;
|
2017-10-25 18:29:50 +02:00
|
|
|
|
}
|
2017-09-12 18:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|