using System.Collections.Generic; using System.Linq; using Android.App; using Android.Content; using KeePassLib; using KeePassLib.Interfaces; namespace keepass2android { public class DeleteMultipleItemsFromOneDatabase : DeleteRunnable { private readonly List _elementsToDelete; private readonly bool _canRecycle; public DeleteMultipleItemsFromOneDatabase(Activity activity, Database db, List elementsToDelete, OnFinish finish, IKp2aApp app) : base(activity, finish, app) { _elementsToDelete = elementsToDelete; SetMembers(activity, 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(); ShowDatabaseIocInStatus = true; } private bool DetermineCanRecycle() { Android.Util.Log.Debug("KP2A", "CanRecycle?"); if (!Db.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 QuestionRecycleResourceId { get { return UiStringKey.AskDeletePermanentlyItems; } } protected override UiStringKey QuestionNoRecycleResourceId { get { return UiStringKey.AskDeletePermanentlyItemsNoRecycle; } } 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; } } } }