Add Move for Entries and Groups

This commit is contained in:
J-Jamet committed 2018-05-28 19:53:45 +02:00
1 parent eb8ca9355c
commit 459bc40515
6 files changed
+285 -16

No files matched your search

@@ -59,10 +59,12 @@ import com.kunzisoft.keepass.database.PwIcon;
import com.kunzisoft.keepass.database.PwIconStandard;
import com.kunzisoft.keepass.database.PwNode;
import com.kunzisoft.keepass.database.action.AddGroupRunnable;
import com.kunzisoft.keepass.database.action.CopyEntryRunnable;
import com.kunzisoft.keepass.database.action.AfterActionNodeOnFinish;
import com.kunzisoft.keepass.database.action.CopyEntryRunnable;
import com.kunzisoft.keepass.database.action.DeleteEntryRunnable;
import com.kunzisoft.keepass.database.action.DeleteGroupRunnable;
import com.kunzisoft.keepass.database.action.MoveEntryRunnable;
import com.kunzisoft.keepass.database.action.MoveGroupRunnable;
import com.kunzisoft.keepass.database.action.OnFinishRunnable;
import com.kunzisoft.keepass.database.action.UpdateGroupRunnable;
import com.kunzisoft.keepass.dialogs.AssignMasterKeyDialogFragment;
@@ -302,6 +304,7 @@ public class GroupActivity extends ListNodesActivity
case R.id.menu_paste:
switch (node.getType()) {
case GROUP:
Log.e(TAG, "Copy not allowed for group");
break;
case ENTRY:
copyNode((PwEntry) node, mCurrentGroup);
@@ -317,7 +320,6 @@ public class GroupActivity extends ListNodesActivity
}
});
// TODO COPY
return false;
}
@@ -334,10 +336,68 @@ public class GroupActivity extends ListNodesActivity
@Override
public boolean onMoveMenuClick(PwNode node) {
// TODO MOVE
startActionMode(new ActionMode.Callback() { // TODO Encapsulate
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
actionMode.setTitle(R.string.where);
actionMode.getMenuInflater().inflate(R.menu.node_paste_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
actionMode.finish();
switch (menuItem.getItemId()) {
case R.id.menu_paste:
switch (node.getType()) {
case GROUP:
moveGroup((PwGroup) node, mCurrentGroup);
break;
case ENTRY:
moveEntry((PwEntry) node, mCurrentGroup);
break;
}
return true;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
return false;
}
private void moveGroup(PwGroup groupToMove, PwGroup newParent) {
MoveGroupRunnable task = new MoveGroupRunnable(this, App.getDB(), groupToMove, newParent,
new AfterAddNode(new Handler()));
task.setUpdateProgressTaskStatus(
new UpdateProgressTaskStatus(this,
SaveDatabaseProgressTaskDialogFragment.start(
getSupportFragmentManager())
));
new Thread(task).start();
}
private void moveEntry(PwEntry entryToMove, PwGroup newParent) {
MoveEntryRunnable task = new MoveEntryRunnable(this, App.getDB(), entryToMove, newParent,
new AfterAddNode(new Handler()));
task.setUpdateProgressTaskStatus(
new UpdateProgressTaskStatus(this,
SaveDatabaseProgressTaskDialogFragment.start(
getSupportFragmentManager())
));
new Thread(task).start();
}
@Override
public boolean onDeleteMenuClick(PwNode node) {
switch (node.getType()) {
@@ -286,9 +286,11 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
// Edition
clearMenu = contextMenu.add(Menu.NONE, MENU_EDIT, Menu.NONE, R.string.menu_edit);
clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener);
// Copy
clearMenu = contextMenu.add(Menu.NONE, MENU_COPY, Menu.NONE, R.string.menu_copy);
clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener);
// Copy (not for group)
if (node.getType().equals(PwNode.Type.ENTRY)) {
clearMenu = contextMenu.add(Menu.NONE, MENU_COPY, Menu.NONE, R.string.menu_copy);
clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener);
}
// Move
clearMenu = contextMenu.add(Menu.NONE, MENU_MOVE, Menu.NONE, R.string.menu_move);
clearMenu.setOnMenuItemClickListener(mOnMyActionClickListener);
@@ -662,6 +662,11 @@ public class Database {
}
}
/**
* @return A duplicate entry with the same values, a new UUID,
* @param entryToCopy
* @param newParent
*/
public @Nullable PwEntry copyEntry(PwEntry entryToCopy, PwGroup newParent) {
try {
// TODO encapsulate
@@ -670,11 +675,13 @@ public class Database {
PwEntryV3 entryV3Copied = ((PwEntryV3) entryToCopy).clone();
entryV3Copied.setUUID(UUID.randomUUID());
entryV3Copied.setParent((PwGroupV3) newParent);
addEntryTo(entryV3Copied, newParent);
return entryV3Copied;
case V4:
PwEntryV4 entryV4Copied = ((PwEntryV4) entryToCopy).clone();
entryV4Copied.setUUID(UUID.randomUUID());
entryV4Copied.setParent((PwGroupV4) newParent);
addEntryTo(entryV4Copied, newParent);
return entryV4Copied;
}
} catch (Exception e) {
@@ -683,19 +690,14 @@ public class Database {
return null;
}
// TODO copy group
public void copyGroup(PwGroup groupToCopy, PwGroup newParent) {
}
// TODO move entry
public void moveEntry(PwEntry entryToMove, PwGroup newParent) {
removeEntryFrom(entryToMove, entryToMove.parent);
addEntryTo(entryToMove, newParent);
}
// TODO move group
public void moveGroup(PwGroup groupToMove, PwGroup newParent) {
removeGroupFrom(groupToMove, groupToMove.parent);
addGroupTo(groupToMove, newParent);
}
public void deleteEntry(PwEntry entry) {
@@ -57,7 +57,6 @@ public class CopyEntryRunnable extends RunnableOnFinish {
public void run() {
// Update entry with new values
mEntryCopied = mDb.copyEntry(mEntryToCopy, mNewParent);
mDb.addEntryTo(mEntryCopied, mNewParent);
if (mEntryCopied != null) {
mEntryCopied.touch(true, true);
@@ -0,0 +1,100 @@
/*
* Copyright 2018 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 3 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.kunzisoft.keepass.database.action;
import android.content.Context;
import android.util.Log;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwEntry;
import com.kunzisoft.keepass.database.PwGroup;
public class MoveEntryRunnable extends RunnableOnFinish {
private static final String TAG = MoveEntryRunnable.class.getName();
private Database mDb;
private PwEntry mEntryToMove;
private PwGroup mOldParent;
private PwGroup mNewParent;
private Context mContext;
private boolean mDontSave;
public MoveEntryRunnable(Context context, Database db, PwEntry oldE, PwGroup newParent, AfterActionNodeOnFinish afterAddNode) {
this(context, db, oldE, newParent, afterAddNode, false);
}
public MoveEntryRunnable(Context context, Database db, PwEntry oldE, PwGroup newParent, AfterActionNodeOnFinish afterAddNode, boolean dontSave) {
super(afterAddNode);
this.mDb = db;
this.mEntryToMove = oldE;
this.mNewParent = newParent;
this.mContext = context;
this.mDontSave = dontSave;
this.mFinish = new AfterMove(afterAddNode);
}
@Override
public void run() {
// Move entry in new parent
mOldParent = mEntryToMove.getParent();
mDb.moveEntry(mEntryToMove, mNewParent);
if (mEntryToMove != null) {
mEntryToMove.touch(true, true);
// Commit to disk
SaveDBRunnable save = new SaveDBRunnable(mContext, mDb, mFinish, mDontSave);
save.run();
} else {
Log.e(TAG, "Unable to create a copy of the entry");
}
}
private class AfterMove extends OnFinishRunnable {
AfterMove(OnFinishRunnable finish) {
super(finish);
}
@Override
public void run() {
if ( !mSuccess ) {
// If we fail to save, try to remove in the first place
try {
mDb.moveEntry(mEntryToMove, mOldParent);
} catch (Exception e) {
Log.i(TAG, "Unable to replace the entry");
}
}
// TODO Better callback
AfterActionNodeOnFinish afterAddNode =
(AfterActionNodeOnFinish) super.mOnFinish;
afterAddNode.mSuccess = mSuccess;
afterAddNode.mMessage = mMessage;
afterAddNode.run(null, mEntryToMove);
super.run();
}
}
}
@@ -0,0 +1,106 @@
/*
* Copyright 2018 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 3 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.kunzisoft.keepass.database.action;
import android.content.Context;
import android.util.Log;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwGroup;
public class MoveGroupRunnable extends RunnableOnFinish {
private static final String TAG = MoveGroupRunnable.class.getName();
private Database mDb;
private PwGroup mGroupToMove;
private PwGroup mOldParent;
private PwGroup mNewParent;
private Context mContext;
private boolean mDontSave;
public MoveGroupRunnable(Context context, Database db, PwGroup groupToMove, PwGroup newParent, AfterActionNodeOnFinish afterAddNode) {
this(context, db, groupToMove, newParent, afterAddNode, false);
}
public MoveGroupRunnable(Context context, Database db, PwGroup groupToMove, PwGroup newParent, AfterActionNodeOnFinish afterAddNode, boolean dontSave) {
super(afterAddNode);
this.mDb = db;
this.mGroupToMove = groupToMove;
this.mNewParent = newParent;
this.mContext = context;
this.mDontSave = dontSave;
this.mFinish = new AfterMove(afterAddNode);
}
@Override
public void run() {
mOldParent = mGroupToMove.getParent();
// Move group in new parent if not in the current group
if (!mGroupToMove.equals(mNewParent)
&& !mNewParent.isContainedIn(mGroupToMove)) {
mDb.moveGroup(mGroupToMove, mNewParent);
if (mGroupToMove != null) {
mGroupToMove.touch(true, true);
// Commit to disk
SaveDBRunnable save = new SaveDBRunnable(mContext, mDb, mFinish, mDontSave);
save.run();
} else {
Log.e(TAG, "Unable to create a copy of the group");
}
} else {
// Only finish thread
mFinish.setResult(false);
mFinish.run();
Log.e(TAG, "Unable to move a group in itself");
}
}
private class AfterMove extends OnFinishRunnable {
AfterMove(OnFinishRunnable finish) {
super(finish);
}
@Override
public void run() {
if ( !mSuccess ) {
// If we fail to save, try to move in the first place
try {
mDb.moveGroup(mGroupToMove, mOldParent);
} catch (Exception e) {
Log.i(TAG, "Unable to replace the group");
}
}
// TODO Better callback
AfterActionNodeOnFinish afterAddNode =
(AfterActionNodeOnFinish) super.mOnFinish;
afterAddNode.mSuccess = mSuccess;
afterAddNode.mMessage = mMessage;
afterAddNode.run(null, mGroupToMove);
super.run();
}
}
}