Files
KeePassDX/app/src/main/java/com/keepassdroid/database/edit/DeleteGroup.java
2017-11-07 21:45:16 +01:00

128 lines
3.3 KiB
Java

/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.database.edit;
import java.util.ArrayList;
import java.util.List;
import com.keepassdroid.Database;
import com.keepassdroid.GroupBaseActivity;
import com.keepassdroid.app.App;
import com.keepassdroid.database.PwEntry;
import com.keepassdroid.database.PwGroup;
public class DeleteGroup extends RunnableOnFinish {
private Database mDb;
private PwGroup mGroup;
private GroupBaseActivity mAct;
private boolean mDontSave;
public DeleteGroup(Database db, PwGroup group, GroupBaseActivity act, OnFinish finish) {
super(finish);
setMembers(db, group, act, false);
}
public DeleteGroup(Database db, PwGroup group, GroupBaseActivity act, OnFinish finish, boolean dontSave) {
super(finish);
setMembers(db, group, act, dontSave);
}
public DeleteGroup(Database db, PwGroup group, OnFinish finish, boolean dontSave) {
super(finish);
setMembers(db, group, null, dontSave);
}
private void setMembers(Database db, PwGroup group, GroupBaseActivity act, boolean dontSave) {
mDb = db;
mGroup = group;
mAct = act;
mDontSave = dontSave;
mFinish = new AfterDelete(mFinish);
}
@Override
public void run() {
// Remove child entries
List<PwEntry> childEnt = new ArrayList<PwEntry>(mGroup.childEntries);
for ( int i = 0; i < childEnt.size(); i++ ) {
DeleteEntry task = new DeleteEntry(mAct, mDb, childEnt.get(i), null, true);
task.run();
}
// Remove child groups
List<PwGroup> childGrp = new ArrayList<PwGroup>(mGroup.childGroups);
for ( int i = 0; i < childGrp.size(); i++ ) {
DeleteGroup task = new DeleteGroup(mDb, childGrp.get(i), mAct, null, true);
task.run();
}
// Remove from parent
PwGroup parent = mGroup.getParent();
if ( parent != null ) {
parent.childGroups.remove(mGroup);
}
// Remove from PwDatabaseV3
mDb.pm.getGroups().remove(mGroup);
// Save
SaveDB save = new SaveDB(mAct, mDb, mFinish, mDontSave);
save.run();
}
private class AfterDelete extends OnFinish {
public AfterDelete(OnFinish finish) {
super(finish);
}
public void run() {
if ( mSuccess ) {
// Remove from tree global
mDb.pm.groups.remove(mGroup.getId());
// Remove tree from the dirty global (if it is present), not a big deal if this fails
mDb.dirty.remove(mGroup);
// Mark parent dirty
PwGroup parent = mGroup.getParent();
if ( parent != null ) {
mDb.dirty.add(parent);
}
mDb.dirty.add(mDb.pm.rootGroup);
} else {
// Let's not bother recovering from a failure to save a deleted tree. It is too much work.
App.setShutdown();
}
super.run();
}
}
}