2017-09-13 18:37:44 +02:00
|
|
|
|
using System.Collections.ObjectModel;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
using System.Linq;
|
2017-10-31 12:14:26 +01:00
|
|
|
|
using System.Text;
|
2017-10-03 18:38:31 +02:00
|
|
|
|
using Windows.UI.Text;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
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;
|
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-02 18:40:54 +02:00
|
|
|
|
public GroupVm ParentGroup { get; }
|
|
|
|
|
public ObservableCollection<EntryVm> Entries { get; set; } = new ObservableCollection<EntryVm>();
|
2017-10-06 16:21:12 -04:00
|
|
|
|
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public ObservableCollection<GroupVm> Groups { get; set; } = new ObservableCollection<GroupVm>();
|
2017-10-06 14:56:16 +02:00
|
|
|
|
|
2017-10-06 16:21:12 -04:00
|
|
|
|
public int EntryCount => Entries.Count() - 1;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
public FontWeight FontWeight => _pwGroup == null ? FontWeights.Bold : FontWeights.Normal;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public int GroupCount => Groups.Count - 1;
|
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;
|
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-10-30 18:34:38 +01:00
|
|
|
|
get { return _app.Database.RecycleBinEnabled && _app.Database.RecycleBin.Id == Id; }
|
|
|
|
|
set
|
2017-10-06 16:21:12 -04:00
|
|
|
|
{
|
2017-10-30 18:34:38 +01:00
|
|
|
|
if (value && _pwGroup != null) _app.Database.RecycleBin = this;
|
2017-10-31 12:14:26 +01:00
|
|
|
|
else if (value && _pwGroup == null)
|
|
|
|
|
{
|
|
|
|
|
var recycleBin = _app.Database.RootGroup.AddNewGroup("Recycle bin");
|
|
|
|
|
recycleBin.IsSelected = true;
|
|
|
|
|
recycleBin.IconSymbol = Symbol.Delete;
|
|
|
|
|
}
|
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
|
|
|
|
|
where !e.IsFirstItem
|
|
|
|
|
group e by e.Name.FirstOrDefault() into grp
|
|
|
|
|
orderby grp.Key
|
|
|
|
|
select grp;
|
|
|
|
|
|
2017-10-06 14:56:16 +02:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
2017-10-30 18:34:38 +01:00
|
|
|
|
get { return _pwGroup == null ? "< New group >" : _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-10-02 18:40:54 +02:00
|
|
|
|
if (_pwGroup == null) return Symbol.Add;
|
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-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;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
private readonly App _app = (App)Application.Current;
|
2017-10-06 14:56:16 +02:00
|
|
|
|
private bool _isEditMode;
|
2017-10-05 14:50:42 +02:00
|
|
|
|
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public GroupVm() {}
|
2017-09-14 18:33:29 +02:00
|
|
|
|
|
2017-10-30 18:34:38 +01:00
|
|
|
|
public GroupVm(PwGroup pwGroup, GroupVm parent, PwUuid recycleBinId = null)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2017-09-25 18:34:27 +02:00
|
|
|
|
_pwGroup = pwGroup;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
ParentGroup = parent;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
|
|
|
|
if (recycleBinId != null && _pwGroup.Uuid.Equals(recycleBinId)) _app.Database.RecycleBin = this;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)).OrderBy(e => e.Name));
|
2017-10-02 18:40:54 +02:00
|
|
|
|
Entries.Insert(0, new EntryVm ());
|
2017-10-30 18:34:38 +01:00
|
|
|
|
Groups = new ObservableCollection<GroupVm>(pwGroup.Groups.Select(g => new GroupVm(g, this, recycleBinId)).OrderBy(g => g.Name));
|
2017-10-02 18:40:54 +02:00
|
|
|
|
Groups.Insert(0, new GroupVm ());
|
2017-09-13 18:37:44 +02:00
|
|
|
|
}
|
2017-09-15 11:24:14 +02: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);
|
|
|
|
|
_pwGroup.AddEntry(pwEntry, true);
|
2017-10-13 15:46:41 +02:00
|
|
|
|
var newEntry = new EntryVm(pwEntry, this) {IsEditMode = true};
|
|
|
|
|
Entries.Add(newEntry);
|
|
|
|
|
return newEntry;
|
2017-10-02 18:40:54 +02:00
|
|
|
|
}
|
2017-10-31 12:14:26 +01:00
|
|
|
|
|
|
|
|
|
public void AddPwEntry(PwEntry entry)
|
|
|
|
|
{
|
|
|
|
|
_pwGroup.AddEntry(entry, true);
|
|
|
|
|
}
|
2017-09-27 18:01:21 +02:00
|
|
|
|
|
2017-10-17 18:46:05 +02:00
|
|
|
|
public void MarkForDelete()
|
|
|
|
|
{
|
2017-10-30 18:34:38 +01:00
|
|
|
|
_app.PendingDeleteEntities.Add(Id, this);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
ParentGroup.Groups.Remove(this);
|
2017-10-30 18:34:38 +01:00
|
|
|
|
if (_app.Database.RecycleBinEnabled && !IsSelected) _app.Database.RecycleBin.Groups.Add(this);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CommitDelete()
|
|
|
|
|
{
|
|
|
|
|
_pwGroup.ParentGroup.Groups.Remove(_pwGroup);
|
2017-10-30 18:34:38 +01:00
|
|
|
|
if (_app.Database.RecycleBinEnabled && !IsSelected) _app.Database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
|
2017-10-31 12:14:26 +01:00
|
|
|
|
else _app.Database.AddDeletedItem(IdUuid);
|
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()
|
|
|
|
|
{
|
|
|
|
|
ParentGroup.Groups.Add(this);
|
2017-10-30 18:34:38 +01:00
|
|
|
|
if (_app.Database.RecycleBinEnabled && !IsSelected) _app.Database.RecycleBin.Groups.Remove(this);
|
2017-10-18 10:32:51 +02:00
|
|
|
|
}
|
2017-10-25 18:29:50 +02:00
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
2017-10-30 18:34:38 +01:00
|
|
|
|
_app.Database.Save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Name;
|
2017-10-25 18:29:50 +02:00
|
|
|
|
}
|
2017-09-12 18:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|