Restore previous drag & drop logic

This commit is contained in:
vuurvlieg 2024-03-25 13:09:15 +01:00
parent 9b6c36b517
commit ebb2d88bc0
5 changed files with 20 additions and 89 deletions

View file

@ -177,13 +177,11 @@ public:
CloneNewUuid = 1, // generate a random uuid for the clone CloneNewUuid = 1, // generate a random uuid for the clone
CloneResetTimeInfo = 2, // set all TimeInfo attributes except LastModificationTime to the current time CloneResetTimeInfo = 2, // set all TimeInfo attributes except LastModificationTime to the current time
CloneIncludeHistory = 4, // clone the history items CloneIncludeHistory = 4, // clone the history items
CloneDefault = CloneNewUuid | CloneResetTimeInfo,
CloneCopy = CloneNewUuid | CloneResetTimeInfo | CloneIncludeHistory,
CloneRenameTitle = 8, // add "-Clone" after the original title CloneRenameTitle = 8, // add "-Clone" after the original title
CloneUserAsRef = 16, // Add the user as a reference to the original entry CloneUserAsRef = 16, // Add the user as a reference to the original entry
ClonePassAsRef = 32, // Add the password as a reference to the original entry ClonePassAsRef = 32, // Add the password as a reference to the original entry
CloneCopy = CloneNewUuid | CloneResetTimeInfo | CloneIncludeHistory,
CloneExactCopy = CloneIncludeHistory,
CloneDefault = CloneNewUuid | CloneResetTimeInfo,
}; };
Q_DECLARE_FLAGS(CloneFlags, CloneFlag) Q_DECLARE_FLAGS(CloneFlags, CloneFlag)

View file

@ -49,11 +49,8 @@ public:
CloneNewUuid = 1, // generate a random uuid for the clone CloneNewUuid = 1, // generate a random uuid for the clone
CloneResetTimeInfo = 2, // set all TimeInfo attributes except LastModificationTime to the current time CloneResetTimeInfo = 2, // set all TimeInfo attributes except LastModificationTime to the current time
CloneIncludeEntries = 4, // clone the group entries CloneIncludeEntries = 4, // clone the group entries
CloneDefault = CloneNewUuid | CloneResetTimeInfo | CloneIncludeEntries,
CloneRenameTitle = 8, // add "- Clone" after the original title CloneRenameTitle = 8, // add "- Clone" after the original title
CloneCopy = CloneNewUuid | CloneResetTimeInfo | CloneIncludeEntries,
CloneExactCopy = CloneIncludeEntries,
CloneDefault = CloneCopy,
}; };
Q_DECLARE_FLAGS(CloneFlags, CloneFlag) Q_DECLARE_FLAGS(CloneFlags, CloneFlag)

View file

@ -708,10 +708,6 @@ QList<DatabaseWidget*> MainWindow::getOpenDatabases()
return dbWidgets; return dbWidgets;
} }
DatabaseWidget* MainWindow::currentDatabaseWidget() {
return m_ui->tabWidget->currentDatabaseWidget();
}
void MainWindow::showErrorMessage(const QString& message) void MainWindow::showErrorMessage(const QString& message)
{ {
m_ui->globalMessageWidget->showMessage(message, MessageWidget::Error); m_ui->globalMessageWidget->showMessage(message, MessageWidget::Error);

View file

@ -52,7 +52,6 @@ public:
~MainWindow() override; ~MainWindow() override;
QList<DatabaseWidget*> getOpenDatabases(); QList<DatabaseWidget*> getOpenDatabases();
DatabaseWidget* currentDatabaseWidget();
void restoreConfigState(); void restoreConfigState();
void setAllowScreenCapture(bool state); void setAllowScreenCapture(bool state);

View file

@ -25,7 +25,6 @@
#include "core/Tools.h" #include "core/Tools.h"
#include "gui/DatabaseIcons.h" #include "gui/DatabaseIcons.h"
#include "gui/Icons.h" #include "gui/Icons.h"
#include "gui/MainWindow.h"
#include "keeshare/KeeShare.h" #include "keeshare/KeeShare.h"
GroupModel::GroupModel(Database* db, QObject* parent) GroupModel::GroupModel(Database* db, QObject* parent)
@ -230,13 +229,6 @@ bool GroupModel::dropMimeData(const QMimeData* data,
Group* parentGroup = groupFromIndex(parent); Group* parentGroup = groupFromIndex(parent);
auto showErrorMessage = [](const QString& errorMessage){
auto dbWidget = getMainWindow()->currentDatabaseWidget();
if(dbWidget) {
dbWidget->showErrorMessage(errorMessage);
}
};
if (isGroup) { if (isGroup) {
QUuid dbUuid; QUuid dbUuid;
QUuid groupUuid; QUuid groupUuid;
@ -266,49 +258,17 @@ bool GroupModel::dropMimeData(const QMimeData* data,
Group* group = dragGroup; Group* group = dragGroup;
if (sourceDb != targetDb) { if (sourceDb != targetDb) {
QSet<QUuid> customIcons = group->customIconsRecursive();
targetDb->metadata()->copyCustomIcons(customIcons, sourceDb->metadata());
// Always clone the group across db's to reset UUIDs
group = dragGroup->clone(Entry::CloneDefault | Entry::CloneIncludeHistory);
if (action == Qt::MoveAction) { if (action == Qt::MoveAction) {
Group* binGroup = sourceDb->metadata()->recycleBin(); // Remove the original group from the sourceDb
if(binGroup && binGroup->uuid() == dragGroup->uuid()) {
showErrorMessage(tr("Move Error: %1 group cannot be moved").arg(tr("Recycle bin")));
return true;
}
// the move is complex when any group or entry is known in the other db
bool complexMove = false;
for (const Group* g: group->groupsRecursive(true)) {
if (targetDb->containsDeletedObject(g->uuid()) || targetDb->rootGroup()->findGroupByUuid(g->uuid())) {
complexMove = true;
break;
}
}
for (const Entry* e: group->entriesRecursive(false)) {
if (targetDb->containsDeletedObject(e->uuid()) || targetDb->rootGroup()->findEntryByUuid(e->uuid())) {
complexMove = true;
break;
}
}
// TODO: unable to handle complex moves until the Merger interface supports single group/entry merging.
if (complexMove) {
showErrorMessage(tr("Move Error: (sub-)group(s) and/or entry(s) already present in this database"));
return true;
}
group = dragGroup->clone(Entry::CloneExactCopy, Group::CloneExactCopy);
}
targetDb->metadata()->copyCustomIcons(dragGroup->customIconsRecursive(), sourceDb->metadata());
}
if (action == Qt::MoveAction) {
// delete the original group when moving a clone
if (group != dragGroup) {
QList<DeletedObject> delObjects(sourceDb->deletedObjects());
delete dragGroup; delete dragGroup;
// prevent group(s)/entry(s) from ending up on the deleted object list by restoring the previous list.
sourceDb->setDeletedObjects(delObjects);
} }
} else { // Action == Qt::CopyAction } else if (action == Qt::CopyAction) {
group = dragGroup->clone(Entry::CloneCopy, Group::CloneCopy); group = dragGroup->clone(Entry::CloneCopy);
} }
group->setParent(parentGroup, row); group->setParent(parentGroup, row);
@ -317,13 +277,11 @@ bool GroupModel::dropMimeData(const QMimeData* data,
return false; return false;
} }
int numEntries = 0, numEntriesNotMoved = 0;
while (!stream.atEnd()) { while (!stream.atEnd()) {
QUuid dbUuid; QUuid dbUuid;
QUuid entryUuid; QUuid entryUuid;
stream >> dbUuid >> entryUuid; stream >> dbUuid >> entryUuid;
++numEntries;
Database* db = Database::databaseByUuid(dbUuid); Database* db = Database::databaseByUuid(dbUuid);
if (!db) { if (!db) {
continue; continue;
@ -340,39 +298,22 @@ bool GroupModel::dropMimeData(const QMimeData* data,
Entry* entry = dragEntry; Entry* entry = dragEntry;
if (sourceDb != targetDb) { if (sourceDb != targetDb) {
QUuid customIcon = entry->iconUuid();
if (!customIcon.isNull() && !targetDb->metadata()->hasCustomIcon(customIcon)) {
targetDb->metadata()->addCustomIcon(customIcon, sourceDb->metadata()->customIcon(customIcon).data);
}
// Reset the UUID when moving across db boundary
entry = dragEntry->clone(Entry::CloneDefault | Entry::CloneIncludeHistory);
if (action == Qt::MoveAction) { if (action == Qt::MoveAction) {
// the move is complex when the entry is known in the other db
bool complexMove = targetDb->containsDeletedObject(entry->uuid())
|| targetDb->rootGroup()->findEntryByUuid(entry->uuid());
// TODO: unable to handle complex moves until the Merger interface supports single group/entry merging.
if (complexMove) {
++numEntriesNotMoved;
continue;
}
entry = dragEntry->clone(Entry::CloneExactCopy);
}
targetDb->metadata()->copyCustomIcon(entry->iconUuid(), sourceDb->metadata());
}
if (action == Qt::MoveAction) {
// delete the original entry when moving a clone
if (entry != dragEntry) {
QList<DeletedObject> delObjects(sourceDb->deletedObjects());
delete dragEntry; delete dragEntry;
// prevent entry from ending up on the deleted object list by restoring the previous list.
sourceDb->setDeletedObjects(delObjects);
} }
} else { // Action == Qt::CopyAction } else if (action == Qt::CopyAction) {
entry = dragEntry->clone(Entry::CloneCopy); entry = dragEntry->clone(Entry::CloneCopy);
} }
entry->setGroup(parentGroup); entry->setGroup(parentGroup);
} }
if (numEntriesNotMoved) {
showErrorMessage(tr("Move Error: %1 of %2 entry(s) already present in this database").arg(numEntriesNotMoved).arg(numEntries));
}
} }
return true; return true;