Skip to content

Commit

Permalink
Enable entry actions when editing an entry
Browse files Browse the repository at this point in the history
* Fixes keepassxreboot#2118

Enables select entry actions when editing an entry. This allows users to copy the password of the entry, for example.

Note: unsaved changes to the entry will not be copied to the clipboard.
  • Loading branch information
droidmonkey committed Oct 21, 2019
1 parent 1e69427 commit 56a3e4d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 42 deletions.
69 changes: 43 additions & 26 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ bool DatabaseWidget::isSearchActive() const
return m_entryView->inSearchMode();
}

bool DatabaseWidget::isEntryEditActive() const
{
return currentWidget() == m_editEntryWidget;
}

bool DatabaseWidget::isEditWidgetModified() const
{
if (currentWidget() == m_editEntryWidget) {
Expand Down Expand Up @@ -397,7 +402,7 @@ void DatabaseWidget::replaceDatabase(QSharedPointer<Database> db)

void DatabaseWidget::cloneEntry()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return;
Expand All @@ -409,7 +414,7 @@ void DatabaseWidget::cloneEntry()

void DatabaseWidget::showTotp()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return;
Expand All @@ -421,7 +426,7 @@ void DatabaseWidget::showTotp()

void DatabaseWidget::copyTotp()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return;
Expand All @@ -431,7 +436,7 @@ void DatabaseWidget::copyTotp()

void DatabaseWidget::setupTotp()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return;
Expand Down Expand Up @@ -568,47 +573,47 @@ void DatabaseWidget::setFocus()

void DatabaseWidget::copyTitle()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
setClipboardTextAndMinimize(currentEntry->resolveMultiplePlaceholders(currentEntry->title()));
}
}

void DatabaseWidget::copyUsername()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
setClipboardTextAndMinimize(currentEntry->resolveMultiplePlaceholders(currentEntry->username()));
}
}

void DatabaseWidget::copyPassword()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
setClipboardTextAndMinimize(currentEntry->resolveMultiplePlaceholders(currentEntry->password()));
}
}

void DatabaseWidget::copyURL()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
setClipboardTextAndMinimize(currentEntry->resolveMultiplePlaceholders(currentEntry->url()));
}
}

void DatabaseWidget::copyNotes()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
setClipboardTextAndMinimize(currentEntry->resolveMultiplePlaceholders(currentEntry->notes()));
}
}

void DatabaseWidget::copyAttribute(QAction* action)
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
setClipboardTextAndMinimize(
currentEntry->resolveMultiplePlaceholders(currentEntry->attributes()->value(action->data().toString())));
Expand All @@ -617,7 +622,7 @@ void DatabaseWidget::copyAttribute(QAction* action)

void DatabaseWidget::showTotpKeyQrCode()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
auto totpDisplayDialog = new TotpExportSettingsDialog(this, currentEntry);
totpDisplayDialog->open();
Expand All @@ -638,15 +643,15 @@ void DatabaseWidget::setClipboardTextAndMinimize(const QString& text)

void DatabaseWidget::performAutoType()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
autoType()->performAutoType(currentEntry, window());
}
}

void DatabaseWidget::openUrl()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
openUrlForEntry(currentEntry);
}
Expand Down Expand Up @@ -749,6 +754,15 @@ void DatabaseWidget::openUrlForEntry(Entry* entry)
}
}

Entry* DatabaseWidget::currentSelectedEntry()
{
if (currentWidget() == m_editEntryWidget) {
return m_editEntryWidget->currentEntry();
}

return m_entryView->currentEntry();
}

void DatabaseWidget::createGroup()
{
Q_ASSERT(m_groupView->currentGroup());
Expand Down Expand Up @@ -845,7 +859,8 @@ void DatabaseWidget::switchToMainView(bool previousDialogAccepted)

void DatabaseWidget::switchToHistoryView(Entry* entry)
{
m_historyEditEntryWidget->loadEntry(entry, false, true, m_editEntryWidget->entryTitle(), m_db);
auto entryTitle = m_editEntryWidget->currentEntry() ? m_editEntryWidget->currentEntry()->title() : "";
m_historyEditEntryWidget->loadEntry(entry, false, true, entryTitle, m_db);
setCurrentWidget(m_historyEditEntryWidget);
}

Expand All @@ -869,10 +884,13 @@ void DatabaseWidget::switchToEntryEdit(Entry* entry, bool create)
group = currentGroup();
} else {
group = entry->group();
// Ensure we have only this entry selected
m_entryView->setCurrentEntry(entry);
}

Q_ASSERT(group);

// Setup the entry edit widget and display
m_editEntryWidget->loadEntry(entry, create, false, group->name(), m_db);
setCurrentWidget(m_editEntryWidget);
}
Expand Down Expand Up @@ -1099,8 +1117,7 @@ void DatabaseWidget::switchToImportOpVault(const QString& fileName)

void DatabaseWidget::switchToEntryEdit()
{
Entry* entry = m_entryView->currentEntry();

auto entry = m_entryView->currentEntry();
if (!entry) {
return;
}
Expand All @@ -1110,8 +1127,7 @@ void DatabaseWidget::switchToEntryEdit()

void DatabaseWidget::switchToGroupEdit()
{
Group* group = m_groupView->currentGroup();

auto group = m_groupView->currentGroup();
if (!group) {
return;
}
Expand Down Expand Up @@ -1362,8 +1378,9 @@ bool DatabaseWidget::lock()
m_groupBeforeLock = m_db->rootGroup()->uuid();
}

if (m_entryView->currentEntry()) {
m_entryBeforeLock = m_entryView->currentEntry()->uuid();
auto currentEntry = currentSelectedEntry();
if (currentEntry) {
m_entryBeforeLock = currentEntry->uuid();
}

endSearch();
Expand Down Expand Up @@ -1482,7 +1499,7 @@ bool DatabaseWidget::currentEntryHasFocus()

bool DatabaseWidget::currentEntryHasTitle()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return false;
Expand All @@ -1492,7 +1509,7 @@ bool DatabaseWidget::currentEntryHasTitle()

bool DatabaseWidget::currentEntryHasUsername()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return false;
Expand All @@ -1502,7 +1519,7 @@ bool DatabaseWidget::currentEntryHasUsername()

bool DatabaseWidget::currentEntryHasPassword()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return false;
Expand All @@ -1512,7 +1529,7 @@ bool DatabaseWidget::currentEntryHasPassword()

bool DatabaseWidget::currentEntryHasUrl()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return false;
Expand All @@ -1522,7 +1539,7 @@ bool DatabaseWidget::currentEntryHasUrl()

bool DatabaseWidget::currentEntryHasTotp()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return false;
Expand All @@ -1532,7 +1549,7 @@ bool DatabaseWidget::currentEntryHasTotp()

bool DatabaseWidget::currentEntryHasNotes()
{
Entry* currentEntry = m_entryView->currentEntry();
auto currentEntry = currentSelectedEntry();
Q_ASSERT(currentEntry);
if (!currentEntry) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class DatabaseWidget : public QStackedWidget
DatabaseWidget::Mode currentMode() const;
bool isLocked() const;
bool isSearchActive() const;
bool isEntryEditActive() const;

QString getCurrentSearch();
void refreshSearch();
Expand Down Expand Up @@ -234,6 +235,7 @@ private slots:
void processAutoOpen();
bool confirmDeleteEntries(QList<Entry*> entries, bool permanent);
void performIconDownloads(const QList<Entry*>& entries, bool force = false);
Entry* currentSelectedEntry();

QSharedPointer<Database> m_db;

Expand Down
38 changes: 29 additions & 9 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,33 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
case DatabaseWidget::Mode::EditMode:
case DatabaseWidget::Mode::ImportMode:
case DatabaseWidget::Mode::LockedMode: {
const QList<QAction*> entryActions = m_ui->menuEntries->actions();
for (QAction* action : entryActions) {
action->setEnabled(false);
bool editEntryActive = dbWidget->isEntryEditActive();
const auto editEntryActionsMask = QList<QAction*>({m_ui->actionEntryCopyUsername,
m_ui->actionEntryCopyPassword,
m_ui->actionEntryCopyURL,
m_ui->actionEntryOpenUrl,
m_ui->actionEntryAutoType,
m_ui->actionEntryDownloadIcon,
m_ui->actionEntryCopyNotes,
m_ui->actionEntryCopyTitle,
m_ui->menuEntryCopyAttribute->menuAction(),
m_ui->menuEntryTotp->menuAction(),
m_ui->actionEntrySetupTotp});

auto entryActions = m_ui->menuEntries->actions();
entryActions << m_ui->menuEntryCopyAttribute->actions();
entryActions << m_ui->menuEntryTotp->actions();
for (auto action : entryActions) {
// Enable select actions when editing an entry
bool enabled = editEntryActive && editEntryActionsMask.contains(action);
if (action->menu()) {
action->menu()->setEnabled(enabled);
}
action->setEnabled(enabled);
}

const QList<QAction*> groupActions = m_ui->menuGroups->actions();
for (QAction* action : groupActions) {
const auto groupActions = m_ui->menuGroups->actions();
for (auto action : groupActions) {
action->setEnabled(false);
}

Expand All @@ -666,13 +686,13 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
}
m_ui->actionDatabaseClose->setEnabled(true);
} else {
const QList<QAction*> entryActions = m_ui->menuEntries->actions();
for (QAction* action : entryActions) {
const auto entryActions = m_ui->menuEntries->actions();
for (auto action : entryActions) {
action->setEnabled(false);
}

const QList<QAction*> groupActions = m_ui->menuGroups->actions();
for (QAction* action : groupActions) {
const auto groupActions = m_ui->menuGroups->actions();
for (auto action : groupActions) {
action->setEnabled(false);
}

Expand Down
8 changes: 2 additions & 6 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,13 +809,9 @@ void EditEntryWidget::toggleHideNotes(bool visible)
m_mainUi->notesHint->setVisible(!visible);
}

QString EditEntryWidget::entryTitle() const
Entry* EditEntryWidget::currentEntry() const
{
if (m_entry) {
return m_entry->title();
} else {
return QString();
}
return m_entry;
}

void EditEntryWidget::loadEntry(Entry* entry,
Expand Down
2 changes: 1 addition & 1 deletion src/gui/entry/EditEntryWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class EditEntryWidget : public EditWidget
void
loadEntry(Entry* entry, bool create, bool history, const QString& parentName, QSharedPointer<Database> database);

QString entryTitle() const;
Entry* currentEntry() const;
void clear();

signals:
Expand Down

0 comments on commit 56a3e4d

Please sign in to comment.