Skip to content

Commit

Permalink
Search entry: respect shortcut config on Copy key
Browse files Browse the repository at this point in the history
If the system Copy key sequence (i.e. Ctrl+C or Cmd+C) is pressed while
inside the search entry without any text being selected, previously we
would copy the currently selected entry's password. This made sense when
keyboard shortcuts were fixed. Now that they are configurable, change it
to re-route the event to the main window, which can then take the
appropriate action (i.e. Ctrl+C might be bound to some other action).
  • Loading branch information
c4rlo committed Jun 16, 2024
1 parent da8874d commit 1c8d6ae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/gui/SearchWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "SearchWidget.h"
#include "gui/MainWindow.h"
#include "ui_SearchHelpWidget.h"
#include "ui_SearchWidget.h"

Expand Down Expand Up @@ -94,10 +95,21 @@ bool SearchWidget::eventFilter(QObject* obj, QEvent* event)
emit escapePressed();
return true;
} else if (keyEvent->matches(QKeySequence::Copy)) {
// If Control+C is pressed in the search edit when no text
// is selected, copy the password of the current entry.
// If the system Copy shortcut (typically Ctrl+C or Cmd+C) is pressed
// in the search edit when no text is selected, route the event to the
// main window. With the default shorcut configuration, this will copy
// the password of the current entry to the clipboard.
if (!m_ui->searchEdit->hasSelectedText()) {
emit copyPressed();
// Prevent infinite recursion, in case the main window ends up
// sending this event back to us. This hasn't actually been observed
// in practice and is just a precaution.
static bool sendingCopyShortcutEvent = false;
if (sendingCopyShortcutEvent) {
return true;
}
sendingCopyShortcutEvent = true;
QCoreApplication::sendEvent(getMainWindow(), event);
sendingCopyShortcutEvent = false;
return true;
}
} else if (keyEvent->matches(QKeySequence::MoveToNextLine)) {
Expand Down Expand Up @@ -135,7 +147,6 @@ void SearchWidget::connectSignals(SignalMultiplexer& mx)
mx.connect(this, SIGNAL(saveSearch(QString)), SLOT(saveSearch(QString)));
mx.connect(this, SIGNAL(caseSensitiveChanged(bool)), SLOT(setSearchCaseSensitive(bool)));
mx.connect(this, SIGNAL(limitGroupChanged(bool)), SLOT(setSearchLimitGroup(bool)));
mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword()));
mx.connect(this, SIGNAL(downPressed()), SLOT(focusOnEntries()));
mx.connect(SIGNAL(requestSearch(QString)), m_ui->searchEdit, SLOT(setText(QString)));
mx.connect(SIGNAL(clearSearch()), this, SLOT(clearSearch()));
Expand Down
1 change: 0 additions & 1 deletion src/gui/SearchWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class SearchWidget : public QWidget
void caseSensitiveChanged(bool state);
void limitGroupChanged(bool state);
void escapePressed();
void copyPressed();
void downPressed();
void enterPressed();
void lostFocus();
Expand Down
10 changes: 5 additions & 5 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,27 +1129,27 @@ void TestGui::testSearch()
searchedEntry->setPassword("password");
QClipboard* clipboard = QApplication::clipboard();

// Attempt password copy with selected test (should fail)
// Copy to clipboard: should copy search text (not password)
QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier);
QVERIFY(clipboard->text() != searchedEntry->password());
QCOMPARE(clipboard->text(), QString("someTHING"));
// Deselect text and confirm password copies
QTest::mouseClick(searchTextEdit, Qt::LeftButton);
QTRY_VERIFY(searchTextEdit->selectedText().isEmpty());
QTRY_VERIFY(searchTextEdit->hasFocus());
QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier);
QCOMPARE(searchedEntry->password(), clipboard->text());
QCOMPARE(clipboard->text(), searchedEntry->password());
// Ensure Down focuses on entry view when search text is selected
QTest::keyClick(searchTextEdit, Qt::Key_A, Qt::ControlModifier);
QTest::keyClick(searchTextEdit, Qt::Key_Down);
QTRY_VERIFY(entryView->hasFocus());
QCOMPARE(entryView->currentEntry(), searchedEntry);
// Test that password copies with entry focused
QTest::keyClick(entryView, Qt::Key_C, Qt::ControlModifier);
QCOMPARE(searchedEntry->password(), clipboard->text());
QCOMPARE(clipboard->text(), searchedEntry->password());
// Refocus back to search edit
QTest::mouseClick(searchTextEdit, Qt::LeftButton);
QTRY_VERIFY(searchTextEdit->hasFocus());
// Test that password does not copy
// Select search text and test that password does not copy
searchTextEdit->selectAll();
QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier);
QTRY_COMPARE(clipboard->text(), QString("someTHING"));
Expand Down

0 comments on commit 1c8d6ae

Please sign in to comment.