From f948e69061a397464cc6d3951117a7a33ab68d7d Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Wed, 19 Aug 2015 07:22:54 +0200 Subject: [PATCH] add missing items --- .../database/edit/DeleteMultipleItems.cs | 79 +++++++++++++++ .../database/edit/MoveElements.cs | 99 +++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 src/Kp2aBusinessLogic/database/edit/DeleteMultipleItems.cs create mode 100644 src/Kp2aBusinessLogic/database/edit/MoveElements.cs diff --git a/src/Kp2aBusinessLogic/database/edit/DeleteMultipleItems.cs b/src/Kp2aBusinessLogic/database/edit/DeleteMultipleItems.cs new file mode 100644 index 00000000..d70c4a2d --- /dev/null +++ b/src/Kp2aBusinessLogic/database/edit/DeleteMultipleItems.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Linq; +using Android.Content; +using KeePassLib; +using KeePassLib.Interfaces; + +namespace keepass2android +{ + public class DeleteMultipleItems : DeleteRunnable + { + private readonly List _elementsToDelete; + private readonly bool _canRecycle; + + public DeleteMultipleItems(Context ctx, Database db, List elementsToDelete, OnFinish finish, IKp2aApp app) + : base(finish, app) + { + _elementsToDelete = elementsToDelete; + SetMembers(ctx, db); + + //determine once. The property is queried for each delete operation, but might return false + //after one entry/group is deleted (and thus in recycle bin and thus can't be recycled anymore) + _canRecycle = DetermineCanRecycle(); + } + + private bool DetermineCanRecycle() + { + Android.Util.Log.Debug("KP2A", "CanRecycle?"); + if (!App.GetDb().DatabaseFormat.CanRecycle) + { + Android.Util.Log.Debug("KP2A", "CanRecycle? No because of DB format."); + return false; + } + + + if (_elementsToDelete.OfType().Any(g => !CanRecycleGroup(g))) + { + return false; + } + + if (_elementsToDelete.OfType().Any(e => !CanRecycleGroup(e.ParentGroup))) + { + return false; + } + Android.Util.Log.Debug("KP2A", "CanRecycle? Yes."); + return true; + } + + public override bool CanRecycle + { + get { return _canRecycle; } + } + + protected override UiStringKey QuestionsResourceId + { + get { return UiStringKey.AskDeletePermanentlyItems; } + } + + protected override void PerformDelete(List touchedGroups, List permanentlyDeletedGroups) + { + foreach (var g in _elementsToDelete.OfType()) + { + Android.Util.Log.Debug("KP2A", "Deleting " + g.Name); + DoDeleteGroup(g, touchedGroups, permanentlyDeletedGroups); + + } + + foreach (var e in _elementsToDelete.OfType()) + { + Android.Util.Log.Debug("KP2A", "Deleting " + e.Strings.ReadSafe(PwDefs.TitleField)); + DoDeleteEntry(e, touchedGroups); + } + } + + public override UiStringKey StatusMessage + { + get { return UiStringKey.DeletingItems; } + } + } +} \ No newline at end of file diff --git a/src/Kp2aBusinessLogic/database/edit/MoveElements.cs b/src/Kp2aBusinessLogic/database/edit/MoveElements.cs new file mode 100644 index 00000000..8b923a22 --- /dev/null +++ b/src/Kp2aBusinessLogic/database/edit/MoveElements.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; +using Android.Content; +using KeePassLib; +using KeePassLib.Interfaces; + +namespace keepass2android.database.edit +{ + public class MoveElements: RunnableOnFinish + { + private readonly List _elementsToMove; + private readonly PwGroup _targetGroup; + private readonly Context _ctx; + private readonly IKp2aApp _app; + + public MoveElements(List elementsToMove, PwGroup targetGroup, Context ctx, IKp2aApp app, OnFinish finish) : base(finish) + { + _elementsToMove = elementsToMove; + _targetGroup = targetGroup; + _ctx = ctx; + _app = app; + } + + public override void Run() + { + //check if we will run into problems. Then finish with error before we start doing anything. + foreach (var _elementToMove in _elementsToMove) + { + PwGroup pgParent = _elementToMove.ParentGroup; + if (pgParent != _targetGroup) + { + if (pgParent != null) + { + PwGroup group = _elementToMove as PwGroup; + if (group != null) + { + if ((_targetGroup == group) || (_targetGroup.IsContainedIn(group))) + { + Finish(false, _app.GetResourceString(UiStringKey.CannotMoveGroupHere)); + return; + } + + } + + } + } + + } + + foreach (var elementToMove in _elementsToMove) + { + + _app.GetDb().Dirty.Add(elementToMove.ParentGroup); + + PwGroup pgParent = elementToMove.ParentGroup; + if (pgParent != _targetGroup) + { + if (pgParent != null) // Remove from parent + { + PwEntry entry = elementToMove as PwEntry; + if (entry != null) + { + pgParent.Entries.Remove(entry); + _targetGroup.AddEntry(entry, true, true); + } + else + { + PwGroup group = (PwGroup)elementToMove; + if ((_targetGroup == group) || (_targetGroup.IsContainedIn(group))) + { + Finish(false, _app.GetResourceString(UiStringKey.CannotMoveGroupHere)); + return; + } + pgParent.Groups.Remove(group); + _targetGroup.AddGroup(group, true, true); + } + } + } + + } + + + _onFinishToRun = new ActionOnFinish((success, message) => + { + if (!success) + { // Let's not bother recovering from a failure. + _app.LockDatabase(false); + } + }, OnFinishToRun); + + // Save + SaveDb save = new SaveDb(_ctx, _app, OnFinishToRun, false); + save.SetStatusLogger(StatusLogger); + save.Run(); + } + } +}