-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathDatabaseOpenDialog.cpp
More file actions
257 lines (222 loc) · 7.78 KB
/
DatabaseOpenDialog.cpp
File metadata and controls
257 lines (222 loc) · 7.78 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
/*
* Copyright (C) 2025 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 "DatabaseOpenDialog.h"
#include "DatabaseOpenWidget.h"
#include "DatabaseTabWidget.h"
#include "DatabaseWidget.h"
#include <QFileInfo>
#include <QLayout>
#include <QShortcut>
DatabaseOpenDialog::DatabaseOpenDialog(QWidget* parent)
: QDialog(parent)
, m_view(new DatabaseOpenWidget(this))
, m_tabBar(new QTabBar(this))
{
setWindowTitle(tr("Unlock Database - KeePassXC"));
setWindowFlags(Qt::Dialog);
setWindowFlag(Qt::WindowContextHelpButtonHint, false);
#ifdef Q_OS_LINUX
// Linux requires this to overcome some Desktop Environments (also no Quick Unlock)
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
#endif
// block input to the main window/application while the dialog is open
setWindowModality(Qt::ApplicationModal);
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->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(m_tabBar);
layout->addWidget(m_view);
setLayout(layout);
setMinimumWidth(700);
// set up Ctrl+PageUp / Ctrl+PageDown and Ctrl+Tab / Ctrl+Shift+Tab shortcuts to cycle tabs
// Ctrl+Tab is broken on Mac, so use Alt (i.e. the Option key) - https://bugreports.qt.io/browse/QTBUG-8596
auto dbTabModifier = Qt::CTRL;
#ifdef Q_OS_MACOS
dbTabModifier = Qt::ALT;
#endif
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(dbTabModifier | Qt::SHIFT | Qt::Key_Tab, 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); });
shortcut = new QShortcut(dbTabModifier | Qt::Key_Tab, this);
shortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(shortcut, &QShortcut::activated, this, [this]() { selectTabOffset(1); });
}
void DatabaseOpenDialog::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
QTimer::singleShot(100, this, [this] {
if (m_view->isOnQuickUnlockScreen() && !m_view->unlockingDatabase()) {
m_view->triggerQuickUnlock();
}
});
}
void DatabaseOpenDialog::selectTabOffset(int offset)
{
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 %" PRIdQSIZETYPE "",
m_tabBar->count(),
m_tabDbWidgets.count());
}
}
/**
* Sets the target DB and reloads the UI.
*/
void DatabaseOpenDialog::setTarget(DatabaseWidget* dbWidget, const QString& filePath)
{
// reconnect finished signal to new dbWidget, then reload the UI
if (m_currentDbWidget) {
disconnect(this, &DatabaseOpenDialog::dialogFinished, m_currentDbWidget, nullptr);
}
connect(this, &DatabaseOpenDialog::dialogFinished, dbWidget, &DatabaseWidget::unlockDatabase);
m_currentDbWidget = dbWidget;
m_view->load(filePath);
}
void DatabaseOpenDialog::setIntent(DatabaseOpenDialog::Intent intent)
{
m_intent = intent;
}
DatabaseOpenDialog::Intent DatabaseOpenDialog::intent() const
{
return m_intent;
}
void DatabaseOpenDialog::clearForms()
{
m_view->clearForms();
m_db.reset();
m_intent = Intent::None;
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);
}
void DatabaseOpenDialog::showMessage(const QString& text, MessageWidget::MessageType type, int autoHideTimeout)
{
m_view->showMessage(text, type, autoHideTimeout);
}
QSharedPointer<Database> DatabaseOpenDialog::database() const
{
return m_db;
}
void DatabaseOpenDialog::completeExternalUnlock(DatabaseWidget* dbWidget)
{
if (!dbWidget) {
return;
}
hide();
// The database is already unlocked, so do not route this completion back through
// DatabaseWidget::unlockDatabase() and replace the database a second time.
if (m_currentDbWidget) {
disconnect(this, &DatabaseOpenDialog::dialogFinished, m_currentDbWidget, nullptr);
}
emit dialogFinished(true, dbWidget);
clearForms();
QDialog::done(QDialog::Accepted);
}
void DatabaseOpenDialog::done(int result)
{
hide();
emit dialogFinished(result == QDialog::Accepted, m_currentDbWidget);
clearForms();
QDialog::done(result);
#if QT_VERSION < QT_VERSION_CHECK(6, 3, 0)
// CDialogs are not really closed, just hidden, pre Qt 6.3?
if (testAttribute(Qt::WA_DeleteOnClose)) {
setAttribute(Qt::WA_DeleteOnClose, false);
deleteLater();
}
#endif
}
void DatabaseOpenDialog::complete(bool accepted)
{
// save DB, since DatabaseOpenWidget will reset its data after accept() is called
m_db = m_view->database();
if (m_db && m_intent == Intent::RemoteSync) {
m_db->markAsTemporaryDatabase();
}
if (accepted) {
accept();
} else {
reject();
}
}
void DatabaseOpenDialog::closeEvent(QCloseEvent* e)
{
emit dialogFinished(false, m_currentDbWidget);
clearForms();
QDialog::closeEvent(e);
}