Allow selecting any open database in unlock dialog

* Closes #2322

* Show locked databases in tabbed interface in unlock dialog for browser and auto-type workflows.

* Make the DatabaseOpenDialog window Application-Modal so that it blocks input to the main UI when the dialog is open. This reduces corner cases by avoiding the possibility of databases getting closed or unlocked
behind the open dialog.
This commit is contained in:
Allen Wild authored and Jonathan White committed 2021-08-29 16:02:40 -04:00
1 parent 37d29b5e8c
commit 53dcafaa58
6 files changed
+198 -42

No files matched your search

+101 -17
View File
@@ -18,9 +18,12 @@
#include "DatabaseOpenDialog.h"
#include "DatabaseOpenWidget.h"
#include "DatabaseTabWidget.h"
#include "DatabaseWidget.h"
#include <QFileInfo>
#include <QLayout>
#include <QShortcut>
#ifdef Q_OS_WIN
#include <QtPlatformHeaders/QWindowsWindowFunctions>
@@ -29,37 +32,109 @@
DatabaseOpenDialog::DatabaseOpenDialog(QWidget* parent)
: QDialog(parent)
, m_view(new DatabaseOpenWidget(this))
, m_tabBar(new QTabBar(this))
{
setWindowTitle(tr("Unlock Database - KeePassXC"));
setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint);
// block input to the main window/application while the dialog is open
setWindowModality(Qt::ApplicationModal);
#ifdef Q_OS_WIN
QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow);
#endif
connect(m_view, SIGNAL(dialogFinished(bool)), this, SLOT(complete(bool)));
connect(m_view, &DatabaseOpenWidget::dialogFinished, this, &DatabaseOpenDialog::complete);
m_tabBar->setAutoHide(true);
m_tabBar->setExpanding(false);
connect(m_tabBar, &QTabBar::currentChanged, this, &DatabaseOpenDialog::tabChanged);
auto* layout = new QVBoxLayout();
layout->setMargin(0);
setLayout(layout);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(m_tabBar);
layout->addWidget(m_view);
setLayout(layout);
setMinimumWidth(700);
// set up Ctrl+PageUp and Ctrl+PageDown shortcuts to cycle tabs
auto* shortcut = new QShortcut(Qt::CTRL + Qt::Key_PageUp, this);
shortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut, &QShortcut::activated, this, [this]() { selectTabOffset(-1); });
shortcut = new QShortcut(Qt::CTRL + Qt::Key_PageDown, this);
shortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut, &QShortcut::activated, this, [this]() { selectTabOffset(1); });
}
void DatabaseOpenDialog::setFilePath(const QString& filePath)
void DatabaseOpenDialog::selectTabOffset(int offset)
{
m_view->load(filePath);
if (offset == 0 || m_tabBar->count() <= 1) {
return;
}
int tab = m_tabBar->currentIndex() + offset;
int last = m_tabBar->count() - 1;
if (tab < 0) {
tab = last;
} else if (tab > last) {
tab = 0;
}
m_tabBar->setCurrentIndex(tab);
}
void DatabaseOpenDialog::addDatabaseTab(DatabaseWidget* dbWidget)
{
Q_ASSERT(dbWidget);
if (!dbWidget) {
return;
}
// important - we must add the DB widget first, because addTab will fire
// tabChanged immediately which will look for a dbWidget in the list
m_tabDbWidgets.append(dbWidget);
QFileInfo fileInfo(dbWidget->database()->filePath());
m_tabBar->addTab(fileInfo.fileName());
Q_ASSERT(m_tabDbWidgets.count() == m_tabBar->count());
}
void DatabaseOpenDialog::setActiveDatabaseTab(DatabaseWidget* dbWidget)
{
if (!dbWidget) {
return;
}
int index = m_tabDbWidgets.indexOf(dbWidget);
if (index != -1) {
m_tabBar->setCurrentIndex(index);
}
}
void DatabaseOpenDialog::tabChanged(int index)
{
if (index < 0 || index >= m_tabDbWidgets.count()) {
return;
}
if (m_tabDbWidgets.count() == m_tabBar->count()) {
DatabaseWidget* dbWidget = m_tabDbWidgets[index];
setTarget(dbWidget, dbWidget->database()->filePath());
} else {
// if these list sizes don't match, there's a bug somewhere nearby
qWarning("DatabaseOpenDialog: mismatch between tab count %d and DB count %d",
m_tabBar->count(),
m_tabDbWidgets.count());
}
}
/**
* Set target DatabaseWidget to which signals are connected.
*
* @param dbWidget database widget
* Sets the target DB and reloads the UI.
*/
void DatabaseOpenDialog::setTargetDatabaseWidget(DatabaseWidget* dbWidget)
void DatabaseOpenDialog::setTarget(DatabaseWidget* dbWidget, const QString& filePath)
{
if (m_dbWidget) {
disconnect(this, nullptr, m_dbWidget, nullptr);
// reconnect finished signal to new dbWidget, then reload the UI
if (m_currentDbWidget) {
disconnect(this, &DatabaseOpenDialog::dialogFinished, m_currentDbWidget, nullptr);
}
m_dbWidget = dbWidget;
connect(this, &DatabaseOpenDialog::dialogFinished, dbWidget, &DatabaseWidget::unlockDatabase);
m_currentDbWidget = dbWidget;
m_view->load(filePath);
}
void DatabaseOpenDialog::setIntent(DatabaseOpenDialog::Intent intent)
@@ -77,13 +152,21 @@ void DatabaseOpenDialog::clearForms()
m_view->clearForms();
m_db.reset();
m_intent = Intent::None;
if (m_dbWidget) {
disconnect(this, nullptr, m_dbWidget, nullptr);
m_dbWidget = nullptr;
if (m_currentDbWidget) {
disconnect(this, &DatabaseOpenDialog::dialogFinished, m_currentDbWidget, nullptr);
}
m_currentDbWidget.clear();
m_tabDbWidgets.clear();
// block signals while removing tabs so that tabChanged doesn't get called
m_tabBar->blockSignals(true);
while (m_tabBar->count() > 0) {
m_tabBar->removeTab(0);
}
m_tabBar->blockSignals(false);
}
QSharedPointer<Database> DatabaseOpenDialog::database()
QSharedPointer<Database> DatabaseOpenDialog::database() const
{
return m_db;
}
@@ -98,6 +181,7 @@ void DatabaseOpenDialog::complete(bool accepted)
} else {
reject();
}
emit dialogFinished(accepted, m_dbWidget);
emit dialogFinished(accepted, m_currentDbWidget);
clearForms();
}