Skip to content

Commit

Permalink
Add lock selected database action on toolbar
Browse files Browse the repository at this point in the history
Closes #6445

Switch tab when locking a database and move Lock Database actions to the Database section of the toolbar.
  • Loading branch information
yamabiiko authored and droidmonkey committed Dec 12, 2021
1 parent 4567265 commit 883effa
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 17 deletions.
2 changes: 2 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ License: MIT

Files: share/icons/application/scalable/actions/chevron-double-down.svg
share/icons/application/scalable/actions/chevron-double-right.svg
share/icons/application/scalable/actions/database-lock.svg
share/icons/application/scalable/actions/database-lock-all.svg
share/icons/application/scalable/actions/document-close.svg
share/icons/application/scalable/actions/document-edit.svg
share/icons/application/scalable/actions/document-export.svg
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion share/icons/application/scalable/actions/database-lock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions share/icons/icons.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<file>application/scalable/actions/configure.svg</file>
<file>application/scalable/actions/database-change-key.svg</file>
<file>application/scalable/actions/database-lock.svg</file>
<file>application/scalable/actions/database-lock-all.svg</file>
<file>application/scalable/actions/database-merge.svg</file>
<file>application/scalable/actions/dialog-close.svg</file>
<file>application/scalable/actions/dialog-ok.svg</file>
Expand Down
12 changes: 8 additions & 4 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5142,10 +5142,6 @@ Are you sure you want to continue with this file?</source>
<source>Download &amp;Favicon</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>&amp;Lock Databases</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>&amp;CSV File…</source>
<translation type="unfinished"></translation>
Expand Down Expand Up @@ -5298,6 +5294,14 @@ We recommend you use the AppImage available on our downloads page.</source>
<source>Restore Entry(s)</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>&amp;Lock Database</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Lock &amp;All Databases</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ManageDatabase</name>
Expand Down
27 changes: 27 additions & 0 deletions src/gui/DatabaseTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,33 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath,
updateLastDatabases(filePath);
}

/**
* Tries to lock the database at the given index and if
* it succeeds proceed to switch to the first unlocked database tab
*/
void DatabaseTabWidget::lockAndSwitchToFirstUnlockedDatabase(int index)
{
if (index == -1) {
index = currentIndex();
}
auto dbWidget = databaseWidgetFromIndex(index);
if (!dbWidget) {
return;
}

if (dbWidget->isLocked()) {
// Database is already locked, act like lock all databases instead
lockDatabases();
} else if (dbWidget->lock()) {
for (int i = 0, c = count(); i < c; ++i) {
if (!databaseWidgetFromIndex(i)->isLocked()) {
setCurrentIndex(i);
return;
}
}
}
}

/**
* Add a new database tab containing the given DatabaseWidget
* @param filePath
Expand Down
1 change: 1 addition & 0 deletions src/gui/DatabaseTabWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DatabaseTabWidget : public QTabWidget
bool hasLockableDatabases() const;

public slots:
void lockAndSwitchToFirstUnlockedDatabase(int index = -1);
void addDatabaseTab(const QString& filePath,
bool inBackground = false,
const QString& password = {},
Expand Down
30 changes: 25 additions & 5 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ MainWindow::MainWindow()
autoTypeButton->setPopupMode(QToolButton::MenuButtonPopup);
}

auto databaseLockMenu = new QMenu({}, this);
databaseLockMenu->addAction(m_ui->actionLockAllDatabases);
m_ui->actionLockDatabaseToolbar->setMenu(databaseLockMenu);
auto databaseLockButton =
qobject_cast<QToolButton*>(m_ui->toolBar->widgetForAction(m_ui->actionLockDatabaseToolbar));
if (databaseLockButton) {
databaseLockButton->setPopupMode(QToolButton::MenuButtonPopup);
}

restoreGeometry(config()->get(Config::GUI_MainWindowGeometry).toByteArray());
restoreState(config()->get(Config::GUI_MainWindowState).toByteArray());

Expand Down Expand Up @@ -261,7 +270,8 @@ MainWindow::MainWindow()
setShortcut(m_ui->actionDatabaseSave, QKeySequence::Save, Qt::CTRL + Qt::Key_S);
setShortcut(m_ui->actionDatabaseSaveAs, QKeySequence::SaveAs, Qt::CTRL + Qt::SHIFT + Qt::Key_S);
setShortcut(m_ui->actionDatabaseClose, QKeySequence::Close, Qt::CTRL + Qt::Key_W);
m_ui->actionLockDatabases->setShortcut(Qt::CTRL + Qt::Key_L);
m_ui->actionLockDatabase->setShortcut(Qt::CTRL + Qt::Key_L);
m_ui->actionLockAllDatabases->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_L);
setShortcut(m_ui->actionQuit, QKeySequence::Quit, Qt::CTRL + Qt::Key_Q);
setShortcut(m_ui->actionEntryNew, QKeySequence::New, Qt::CTRL + Qt::Key_N);
m_ui->actionEntryEdit->setShortcut(Qt::CTRL + Qt::Key_E);
Expand Down Expand Up @@ -369,7 +379,9 @@ MainWindow::MainWindow()
m_ui->actionReports->setIcon(icons()->icon("reports"));
m_ui->actionDatabaseSettings->setIcon(icons()->icon("document-edit"));
m_ui->actionDatabaseSecurity->setIcon(icons()->icon("database-change-key"));
m_ui->actionLockDatabases->setIcon(icons()->icon("database-lock"));
m_ui->actionLockDatabase->setIcon(icons()->icon("database-lock"));
m_ui->actionLockDatabaseToolbar->setIcon(icons()->icon("database-lock"));
m_ui->actionLockAllDatabases->setIcon(icons()->icon("database-lock-all"));
m_ui->actionQuit->setIcon(icons()->icon("application-exit"));
m_ui->actionDatabaseMerge->setIcon(icons()->icon("database-merge"));
m_ui->menuImport->setIcon(icons()->icon("document-import"));
Expand Down Expand Up @@ -460,7 +472,10 @@ MainWindow::MainWindow()
connect(m_ui->actionImportOpVault, SIGNAL(triggered()), m_ui->tabWidget, SLOT(importOpVaultDatabase()));
connect(m_ui->actionExportCsv, SIGNAL(triggered()), m_ui->tabWidget, SLOT(exportToCsv()));
connect(m_ui->actionExportHtml, SIGNAL(triggered()), m_ui->tabWidget, SLOT(exportToHtml()));
connect(m_ui->actionLockDatabases, SIGNAL(triggered()), m_ui->tabWidget, SLOT(lockDatabases()));
connect(
m_ui->actionLockDatabase, SIGNAL(triggered()), m_ui->tabWidget, SLOT(lockAndSwitchToFirstUnlockedDatabase()));
connect(m_ui->actionLockDatabaseToolbar, SIGNAL(triggered()), m_ui->actionLockDatabase, SIGNAL(triggered()));
connect(m_ui->actionLockAllDatabases, SIGNAL(triggered()), m_ui->tabWidget, SLOT(lockDatabases()));
connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(appExit()));

m_actionMultiplexer.connect(m_ui->actionEntryNew, SIGNAL(triggered()), SLOT(createEntry()));
Expand Down Expand Up @@ -781,7 +796,9 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
m_ui->actionDatabaseOpen->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->menuRecentDatabases->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->menuImport->setEnabled(inDatabaseTabWidgetOrWelcomeWidget);
m_ui->actionLockDatabases->setEnabled(m_ui->tabWidget->hasLockableDatabases());
m_ui->actionLockDatabase->setEnabled(m_ui->tabWidget->hasLockableDatabases());
m_ui->actionLockDatabaseToolbar->setEnabled(m_ui->tabWidget->hasLockableDatabases());
m_ui->actionLockAllDatabases->setEnabled(m_ui->tabWidget->hasLockableDatabases());

if (inDatabaseTabWidget && m_ui->tabWidget->currentIndex() != -1) {
DatabaseWidget* dbWidget = m_ui->tabWidget->currentDatabaseWidget();
Expand Down Expand Up @@ -924,6 +941,9 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
m_ui->actionExportCsv->setEnabled(false);
m_ui->actionExportHtml->setEnabled(false);
m_ui->actionDatabaseMerge->setEnabled(false);
// Only disable the action in the database menu so that the
// menu remains active in the toolbar, if necessary
m_ui->actionLockDatabase->setEnabled(false);

m_searchWidgetAction->setEnabled(false);
break;
Expand Down Expand Up @@ -1392,7 +1412,7 @@ void MainWindow::updateTrayIcon()
menu->addAction(actionToggle);
actionToggle->setIcon(icons()->icon("keepassxc-monochrome-dark"));

menu->addAction(m_ui->actionLockDatabases);
menu->addAction(m_ui->actionLockAllDatabases);

#ifdef Q_OS_MACOS
auto actionQuit = new QAction(tr("Quit KeePassXC"), menu);
Expand Down
29 changes: 23 additions & 6 deletions src/gui/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
<height>22</height>
</rect>
</property>
<property name="contextMenuPolicy">
Expand Down Expand Up @@ -254,6 +254,9 @@
<addaction name="actionDatabaseSaveBackup"/>
<addaction name="actionDatabaseClose"/>
<addaction name="separator"/>
<addaction name="actionLockDatabase"/>
<addaction name="actionLockAllDatabases"/>
<addaction name="separator"/>
<addaction name="actionReports"/>
<addaction name="actionDatabaseSettings"/>
<addaction name="actionDatabaseSecurity"/>
Expand Down Expand Up @@ -355,7 +358,6 @@
<property name="title">
<string>&amp;Tools</string>
</property>
<addaction name="actionLockDatabases"/>
<addaction name="actionPasswordGenerator"/>
<addaction name="actionSettings"/>
</widget>
Expand Down Expand Up @@ -409,6 +411,7 @@
</attribute>
<addaction name="actionDatabaseOpen"/>
<addaction name="actionDatabaseSave"/>
<addaction name="actionLockDatabaseToolbar"/>
<addaction name="separator"/>
<addaction name="actionEntryNew"/>
<addaction name="actionEntryEdit"/>
Expand All @@ -419,8 +422,6 @@
<addaction name="actionEntryCopyURL"/>
<addaction name="actionEntryAutoType"/>
<addaction name="separator"/>
<addaction name="actionLockDatabases"/>
<addaction name="separator"/>
<addaction name="actionPasswordGenerator"/>
<addaction name="actionSettings"/>
<addaction name="separator"/>
Expand Down Expand Up @@ -773,12 +774,20 @@
<string>Open &amp;URL</string>
</property>
</action>
<action name="actionLockDatabases">
<action name="actionLockDatabase">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Lock Database</string>
</property>
</action>
<action name="actionLockAllDatabases">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Lock Databases</string>
<string>Lock &amp;All Databases</string>
</property>
</action>
<action name="actionEntryCopyTitle">
Expand Down Expand Up @@ -1084,6 +1093,14 @@
<string notr="true">Ctrl+R</string>
</property>
</action>
<action name="actionLockDatabaseToolbar">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>&amp;Lock Database</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
2 changes: 1 addition & 1 deletion tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ void TestGui::testDatabaseLocking()
QString origDbName = m_tabWidget->tabText(0);

MessageBox::setNextAnswer(MessageBox::Cancel);
triggerAction("actionLockDatabases");
triggerAction("actionLockAllDatabases");

QCOMPARE(m_tabWidget->tabText(0), origDbName + " [Locked]");

Expand Down

0 comments on commit 883effa

Please sign in to comment.