Skip to content

Commit

Permalink
Fix focus loss when using Auto-Type from locked database
Browse files Browse the repository at this point in the history
* Fixes #10439
  • Loading branch information
droidmonkey committed Apr 24, 2024
1 parent cb1ae44 commit 35af1c6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
30 changes: 16 additions & 14 deletions src/autotype/AutoType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void AutoType::executeAutoTypeActions(const Entry* entry,
MessageBox::critical(getMainWindow(), tr("Auto-Type Error"), errorMsg);
}
qWarning() << errorMsg;
emit autotypeRejected();
emit autotypeFinished();
return;
}

Expand Down Expand Up @@ -318,13 +318,13 @@ void AutoType::executeAutoTypeActions(const Entry* entry,
}

for (const auto& action : asConst(actions)) {
// Cancel Auto-Type if the active window changed
if (m_plugin->activeWindow() != window) {
qWarning("Active window changed, interrupting auto-type.");
emit autotypeRejected();
m_inAutoType.unlock();
return;
break;
}

bool failed = false;
constexpr int max_retries = 5;
for (int i = 1; i <= max_retries; i++) {
auto result = action->exec(m_executor);
Expand All @@ -339,20 +339,22 @@ void AutoType::executeAutoTypeActions(const Entry* entry,
if (getMainWindow()) {
MessageBox::critical(getMainWindow(), tr("Auto-Type Error"), result.errorString());
}
emit autotypeRejected();
m_inAutoType.unlock();
return;
failed = true;
break;
}

Tools::wait(delay);
}
}

m_windowForGlobal = 0;
m_windowTitleForGlobal.clear();
// Last action failed to complete, cancel the rest of the sequence
if (failed) {
break;
}
}

emit autotypePerformed();
resetAutoTypeState();
m_inAutoType.unlock();
emit autotypeFinished();
}

/**
Expand Down Expand Up @@ -387,7 +389,7 @@ void AutoType::performAutoTypeWithSequence(const Entry* entry, const QString& se
void AutoType::startGlobalAutoType(const QString& search)
{
// Never Auto-Type into KeePassXC itself
if (qApp->focusWindow()) {
if (getMainWindow() && (qApp->activeWindow() || qApp->activeModalWidget())) {
return;
}

Expand Down Expand Up @@ -495,7 +497,7 @@ void AutoType::performGlobalAutoType(const QList<QSharedPointer<Database>>& dbLi
connect(selectDialog, &QDialog::rejected, this, [this] {
restoreWindowState();
resetAutoTypeState();
emit autotypeRejected();
emit autotypeFinished();
});

#ifdef Q_OS_MACOS
Expand All @@ -512,7 +514,7 @@ void AutoType::performGlobalAutoType(const QList<QSharedPointer<Database>>& dbLi
} else {
// We should never get here
resetAutoTypeState();
emit autotypeRejected();
emit autotypeFinished();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/autotype/AutoType.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public slots:

signals:
void globalAutoTypeTriggered(const QString& search);
void autotypePerformed();
void autotypeRejected();
void autotypeFinished();
void autotypeRetypeTimeout();

private slots:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/DatabaseTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ DatabaseTabWidget::DatabaseTabWidget(QWidget* parent)
m_dbWidgetStateSync, SLOT(setActive(DatabaseWidget*)));
connect(autoType(), SIGNAL(globalAutoTypeTriggered(const QString&)), SLOT(performGlobalAutoType(const QString&)));
connect(autoType(), SIGNAL(autotypeRetypeTimeout()), SLOT(relockPendingDatabase()));
connect(autoType(), SIGNAL(autotypeRejected()), SLOT(relockPendingDatabase()));
connect(autoType(), SIGNAL(autotypeFinished()), SLOT(relockPendingDatabase()));
connect(m_databaseOpenDialog.data(), &DatabaseOpenDialog::dialogFinished,
this, &DatabaseTabWidget::handleDatabaseUnlockDialogFinished);
// clang-format on
Expand Down

0 comments on commit 35af1c6

Please sign in to comment.