-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSearchWidget.cpp
More file actions
290 lines (253 loc) · 10.8 KB
/
SearchWidget.cpp
File metadata and controls
290 lines (253 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SearchWidget.h"
#include "gui/MainWindow.h"
#include "ui_SearchHelpWidget.h"
#include "ui_SearchWidget.h"
#include <QKeyEvent>
#include <QMenu>
#include <QShortcut>
#include <QToolButton>
#include "core/SignalMultiplexer.h"
#include "gui/Icons.h"
#include "gui/widgets/PopupHelpWidget.h"
SearchWidget::SearchWidget(QWidget* parent)
: QWidget(parent)
, m_ui(new Ui::SearchWidget())
, m_searchTimer(new QTimer(this))
, m_clearSearchTimer(new QTimer(this))
{
m_ui->setupUi(this);
setFocusProxy(m_ui->searchEdit);
m_helpWidget = new PopupHelpWidget(m_ui->searchEdit);
Ui::SearchHelpWidget helpUi;
helpUi.setupUi(m_helpWidget);
m_searchTimer->setSingleShot(true);
m_clearSearchTimer->setSingleShot(true);
new QShortcut(Qt::CTRL + Qt::Key_J, this, SLOT(toggleHelp()), nullptr, Qt::WidgetWithChildrenShortcut);
connect(m_ui->searchEdit, SIGNAL(textChanged(QString)), SLOT(startSearchTimer()));
connect(m_ui->searchEdit, SIGNAL(textChanged(QString)), SLOT(updateSaveButtonVisibility()));
connect(m_ui->helpIcon, SIGNAL(triggered()), SLOT(toggleHelp()));
connect(m_ui->searchIcon, SIGNAL(triggered()), SLOT(showSearchMenu()));
connect(m_ui->saveIcon, &QAction::triggered, this, [this] { emit saveSearch(m_ui->searchEdit->text()); });
connect(m_searchTimer, SIGNAL(timeout()), SLOT(startSearch()));
connect(m_clearSearchTimer, SIGNAL(timeout()), SLOT(clearSearch()));
connect(this, SIGNAL(escapePressed()), SLOT(clearSearch()));
connect(m_ui->searchEdit, &QLineEdit::returnPressed, this, &SearchWidget::onReturnPressed);
m_ui->searchEdit->setPlaceholderText(tr("Search (%1)…", "Search placeholder text, %1 is the keyboard shortcut")
.arg(QKeySequence(QKeySequence::Find).toString(QKeySequence::NativeText)));
m_ui->searchEdit->installEventFilter(this);
m_searchMenu = new QMenu(this);
m_actionCaseSensitive = m_searchMenu->addAction(tr("Case sensitive"), this, SLOT(updateCaseSensitive()));
m_actionCaseSensitive->setObjectName("actionSearchCaseSensitive");
m_actionCaseSensitive->setCheckable(true);
m_actionLimitGroup = m_searchMenu->addAction(tr("Limit search to selected group"), this, SLOT(updateLimitGroup()));
m_actionLimitGroup->setObjectName("actionSearchLimitGroup");
m_actionLimitGroup->setCheckable(true);
m_actionLimitGroup->setChecked(config()->get(Config::SearchLimitGroup).toBool());
m_actionWaitForEnter = m_searchMenu->addAction(
tr("Press Enter to search"), this, [](bool state) { config()->set(Config::GUI_SearchWaitForEnter, state); });
m_actionWaitForEnter->setObjectName("actionSearchWaitForEnter");
m_actionWaitForEnter->setCheckable(true);
m_actionWaitForEnter->setChecked(config()->get(Config::GUI_SearchWaitForEnter).toBool());
m_ui->searchIcon->setIcon(icons()->icon("system-search"));
m_ui->searchEdit->addAction(m_ui->searchIcon, QLineEdit::LeadingPosition);
m_ui->helpIcon->setIcon(icons()->icon("system-help"));
m_ui->searchEdit->addAction(m_ui->helpIcon, QLineEdit::TrailingPosition);
m_ui->saveIcon->setIcon(icons()->icon("document-save"));
m_ui->searchEdit->addAction(m_ui->saveIcon, QLineEdit::TrailingPosition);
m_ui->saveIcon->setVisible(false);
// Fix initial visibility of actions (bug in Qt)
for (QToolButton* toolButton : m_ui->searchEdit->findChildren<QToolButton*>()) {
toolButton->setVisible(toolButton->defaultAction()->isVisible());
}
}
SearchWidget::~SearchWidget() = default;
bool SearchWidget::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::KeyPress) {
auto keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Escape) {
emit escapePressed();
return true;
} else if (keyEvent->matches(QKeySequence::Copy)) {
// 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 shortcut configuration, this will copy
// the password of the current entry to the clipboard.
if (!m_ui->searchEdit->hasSelectedText()) {
// 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)) {
if (m_ui->searchEdit->cursorPosition() == m_ui->searchEdit->text().length()) {
// If down is pressed at EOL, move the focus to the entry view
emit downPressed();
return true;
} else {
// Otherwise move the cursor to EOL
m_ui->searchEdit->setCursorPosition(m_ui->searchEdit->text().length());
return true;
}
}
} else if (event->type() == QEvent::FocusOut) {
if (config()->get(Config::Security_ClearSearch).toBool()) {
int timeout = config()->get(Config::Security_ClearSearchTimeout).toInt();
if (timeout > 0) {
// Auto-clear search after set timeout (5 minutes by default)
m_clearSearchTimer->start(timeout * 60000); // 60 sec * 1000 ms
}
}
emit lostFocus();
} else if (event->type() == QEvent::FocusIn) {
// Never clear the search if we are using it
m_clearSearchTimer->stop();
}
return QWidget::eventFilter(obj, event);
}
void SearchWidget::connectSignals(SignalMultiplexer& mx)
{
// Connects basically only to the current DatabaseWidget, but allows to switch between instances!
mx.connect(this, SIGNAL(search(QString)), SLOT(search(QString)));
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(downPressed()), SLOT(focusOnEntries()));
mx.connect(SIGNAL(requestSearch(QString)), this, SLOT(performRequestedSearch(QString)));
mx.connect(SIGNAL(clearSearch()), this, SLOT(clearSearch()));
mx.connect(SIGNAL(entrySelectionChanged()), this, SLOT(resetSearchClearTimer()));
mx.connect(SIGNAL(currentModeChanged(DatabaseWidget::Mode)), this, SLOT(resetSearchClearTimer()));
mx.connect(SIGNAL(databaseUnlocked()), this, SLOT(focusSearch()));
mx.connect(this, SIGNAL(enterPressed()), SLOT(switchToEntryEdit()));
}
void SearchWidget::databaseChanged(DatabaseWidget* dbWidget)
{
if (dbWidget) {
// Set current search text from this database
m_ui->searchEdit->setText(dbWidget->getCurrentSearch());
// Enforce search policy
emit caseSensitiveChanged(m_actionCaseSensitive->isChecked());
emit limitGroupChanged(m_actionLimitGroup->isChecked());
} else {
clearSearch();
}
}
void SearchWidget::startSearchTimer()
{
if (m_actionWaitForEnter->isChecked()) {
m_searchTimer->stop();
} else {
m_searchTimer->start(500);
}
}
void SearchWidget::startSearch()
{
m_ui->saveIcon->setVisible(true);
search(m_ui->searchEdit->text());
}
void SearchWidget::resetSearchClearTimer()
{
// Restart the search clear timer if it is running
if (m_clearSearchTimer->isActive()) {
m_clearSearchTimer->start();
}
}
void SearchWidget::updateCaseSensitive()
{
emit caseSensitiveChanged(m_actionCaseSensitive->isChecked());
}
void SearchWidget::updateLimitGroup()
{
config()->set(Config::SearchLimitGroup, m_actionLimitGroup->isChecked());
emit limitGroupChanged(m_actionLimitGroup->isChecked());
}
void SearchWidget::setCaseSensitive(bool state)
{
m_actionCaseSensitive->setChecked(state);
updateCaseSensitive();
}
void SearchWidget::setLimitGroup(bool state)
{
m_actionLimitGroup->setChecked(state);
updateLimitGroup();
}
void SearchWidget::setSearchGroupName(const QString& name)
{
// Quote and escape the group name so it is treated as a single group term,
// even if it contains spaces or special characters.
QString escapedName = name;
escapedName.replace("\\", "\\\\");
escapedName.replace("\"", "\\\"");
QString searchText = QStringLiteral("g:\"") + escapedName + QStringLiteral("\" ");
m_ui->searchEdit->clear();
m_ui->searchEdit->setText(searchText);
m_ui->searchEdit->setFocus();
}
void SearchWidget::focusSearch()
{
m_ui->searchEdit->setFocus();
m_ui->searchEdit->selectAll();
}
void SearchWidget::clearSearch()
{
m_ui->searchEdit->clear();
m_ui->saveIcon->setVisible(false);
emit searchCanceled();
}
void SearchWidget::toggleHelp()
{
if (m_helpWidget->isVisible()) {
m_helpWidget->hide();
} else {
m_helpWidget->show();
}
}
void SearchWidget::showSearchMenu()
{
m_searchMenu->exec(m_ui->searchEdit->mapToGlobal(m_ui->searchEdit->rect().bottomLeft()));
}
void SearchWidget::onReturnPressed()
{
if (m_actionWaitForEnter->isChecked()) {
m_ui->saveIcon->setVisible(true);
emit search(m_ui->searchEdit->text());
} else {
emit enterPressed();
}
}
void SearchWidget::performRequestedSearch(const QString& text)
{
// This method handles saved searches - it should set the text and immediately trigger search
// without any delay, regardless of the "Press Enter to search" setting
m_ui->searchEdit->setText(text);
m_ui->saveIcon->setVisible(!text.isEmpty());
emit search(text);
}
void SearchWidget::updateSaveButtonVisibility()
{
// Show save button whenever there's non-empty text in the search field
m_ui->saveIcon->setVisible(!m_ui->searchEdit->text().isEmpty());
}