2011-11-13 08:55:20 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
|
2017-06-09 17:40:36 -04:00
|
|
|
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
|
2011-11-13 08:55:20 -05:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
#include "DatabaseTabWidget.h"
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2013-10-03 09:18:16 -04:00
|
|
|
#include <QFileInfo>
|
2017-02-24 21:40:49 -05:00
|
|
|
#include <QPushButton>
|
2018-03-31 16:01:30 -04:00
|
|
|
#include <QTabWidget>
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2012-07-14 13:09:28 -04:00
|
|
|
#include "autotype/AutoType.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "core/AsyncTask.h"
|
2012-05-27 17:01:14 -04:00
|
|
|
#include "core/Config.h"
|
2011-11-13 08:55:20 -05:00
|
|
|
#include "core/Database.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "core/Global.h"
|
2012-05-02 10:22:52 -04:00
|
|
|
#include "core/Group.h"
|
2011-11-13 08:55:20 -05:00
|
|
|
#include "core/Metadata.h"
|
2015-07-14 16:14:34 -04:00
|
|
|
#include "format/CsvExporter.h"
|
2015-09-13 06:38:19 -04:00
|
|
|
#include "gui/Clipboard.h"
|
2011-11-13 08:55:20 -05:00
|
|
|
#include "gui/DatabaseWidget.h"
|
2014-05-17 05:16:27 -04:00
|
|
|
#include "gui/DatabaseWidgetStateSync.h"
|
2012-05-16 03:54:21 -04:00
|
|
|
#include "gui/DragTabBar.h"
|
2012-05-12 04:08:41 -04:00
|
|
|
#include "gui/FileDialog.h"
|
2013-10-08 16:09:20 -04:00
|
|
|
#include "gui/MessageBox.h"
|
2018-03-31 16:01:30 -04:00
|
|
|
#include "gui/UnlockDatabaseDialog.h"
|
2012-05-16 04:16:32 -04:00
|
|
|
#include "gui/entry/EntryView.h"
|
|
|
|
#include "gui/group/GroupView.h"
|
2011-11-13 08:55:20 -05:00
|
|
|
|
|
|
|
DatabaseManagerStruct::DatabaseManagerStruct()
|
2015-07-24 12:28:12 -04:00
|
|
|
: dbWidget(nullptr)
|
2011-11-13 08:55:20 -05:00
|
|
|
, modified(false)
|
|
|
|
, readOnly(false)
|
2018-01-17 20:13:13 -05:00
|
|
|
, saveAttempts(0)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-27 17:01:14 -04:00
|
|
|
const int DatabaseTabWidget::LastDatabasesCount = 5;
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
DatabaseTabWidget::DatabaseTabWidget(QWidget* parent)
|
|
|
|
: QTabWidget(parent)
|
2017-02-20 20:19:11 -05:00
|
|
|
, m_dbWidgetStateSync(new DatabaseWidgetStateSync(this))
|
2018-02-05 07:25:16 -05:00
|
|
|
, m_dbPendingLock(nullptr)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
2012-05-16 03:54:21 -04:00
|
|
|
DragTabBar* tabBar = new DragTabBar(this);
|
|
|
|
setTabBar(tabBar);
|
2015-07-18 12:27:17 -04:00
|
|
|
setDocumentMode(true);
|
2012-05-16 03:54:21 -04:00
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
connect(this, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int)));
|
2014-05-17 05:16:27 -04:00
|
|
|
connect(this, SIGNAL(currentChanged(int)), SLOT(emitActivateDatabaseChanged()));
|
2018-03-31 16:01:30 -04:00
|
|
|
connect(
|
|
|
|
this, SIGNAL(activateDatabaseChanged(DatabaseWidget*)), m_dbWidgetStateSync, SLOT(setActive(DatabaseWidget*)));
|
2012-07-14 13:09:28 -04:00
|
|
|
connect(autoType(), SIGNAL(globalShortcutTriggered()), SLOT(performGlobalAutoType()));
|
2018-02-05 07:25:16 -05:00
|
|
|
connect(autoType(), SIGNAL(autotypePerformed()), SLOT(relockPendingDatabase()));
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 17:40:51 -04:00
|
|
|
DatabaseTabWidget::~DatabaseTabWidget()
|
|
|
|
{
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
deleteDatabase(i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:22:07 -04:00
|
|
|
void DatabaseTabWidget::toggleTabbar()
|
|
|
|
{
|
2012-04-20 05:29:31 -04:00
|
|
|
if (count() > 1) {
|
2017-02-14 13:17:35 -05:00
|
|
|
tabBar()->show();
|
|
|
|
} else {
|
|
|
|
tabBar()->hide();
|
2012-04-20 05:29:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
void DatabaseTabWidget::newDatabase()
|
2011-11-16 12:47:17 -05:00
|
|
|
{
|
|
|
|
DatabaseManagerStruct dbStruct;
|
|
|
|
Database* db = new Database();
|
2018-01-20 07:47:30 -05:00
|
|
|
db->rootGroup()->setName(tr("Root", "Root group"));
|
2011-12-30 12:43:24 -05:00
|
|
|
dbStruct.dbWidget = new DatabaseWidget(db, this);
|
2017-10-12 17:30:51 -04:00
|
|
|
|
2017-02-13 11:35:38 -05:00
|
|
|
CompositeKey emptyKey;
|
|
|
|
db->setKey(emptyKey);
|
2011-11-16 12:47:17 -05:00
|
|
|
|
|
|
|
insertDatabase(db, dbStruct);
|
2017-10-12 17:30:51 -04:00
|
|
|
|
2017-02-13 11:35:38 -05:00
|
|
|
if (!saveDatabaseAs(db)) {
|
|
|
|
closeDatabase(db);
|
|
|
|
return;
|
|
|
|
}
|
2012-04-11 14:35:52 -04:00
|
|
|
|
2017-02-16 17:18:04 -05:00
|
|
|
dbStruct.dbWidget->switchToMasterKeyChange(true);
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
void DatabaseTabWidget::openDatabase()
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
2012-06-05 13:41:21 -04:00
|
|
|
QString filter = QString("%1 (*.kdbx);;%2 (*)").arg(tr("KeePass 2 Database"), tr("All files"));
|
2018-03-31 16:01:30 -04:00
|
|
|
QString fileName = fileDialog()->getOpenFileName(this, tr("Open database"), QDir::homePath(), filter);
|
2011-11-13 08:55:20 -05:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
openDatabase(fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, const QString& keyFile)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
2012-07-08 04:15:05 -04:00
|
|
|
QFileInfo fileInfo(fileName);
|
|
|
|
QString canonicalFilePath = fileInfo.canonicalFilePath();
|
2012-07-28 04:19:32 -04:00
|
|
|
if (canonicalFilePath.isEmpty()) {
|
2017-03-10 09:58:42 -05:00
|
|
|
emit messageGlobal(tr("File not found!"), MessageWidget::Error);
|
2012-07-28 04:19:32 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-08 04:15:05 -04:00
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
2017-11-26 16:58:54 -05:00
|
|
|
if (i.value().fileInfo.canonicalFilePath() == canonicalFilePath) {
|
2017-10-18 22:08:30 -04:00
|
|
|
if (!i.value().dbWidget->dbHasKey() && !(pw.isNull() && keyFile.isEmpty())) {
|
|
|
|
// If the database is locked and a pw or keyfile is provided, unlock it
|
2017-11-26 16:58:54 -05:00
|
|
|
i.value().dbWidget->switchToOpenDatabase(i.value().fileInfo.absoluteFilePath(), pw, keyFile);
|
2017-10-09 07:26:24 -04:00
|
|
|
} else {
|
2017-10-18 22:08:30 -04:00
|
|
|
setCurrentIndex(databaseIndex(i.key()));
|
2017-10-09 07:26:24 -04:00
|
|
|
}
|
2012-07-08 04:15:05 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
DatabaseManagerStruct dbStruct;
|
|
|
|
|
2012-07-06 12:50:52 -04:00
|
|
|
// test if we can read/write or read the file
|
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::ReadWrite)) {
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2015-01-21 11:19:05 -05:00
|
|
|
// can't open
|
2018-03-31 16:01:30 -04:00
|
|
|
emit messageGlobal(tr("Unable to open the database.").append("\n").append(file.errorString()),
|
|
|
|
MessageWidget::Error);
|
2011-11-13 08:55:20 -05:00
|
|
|
return;
|
2018-03-31 16:01:30 -04:00
|
|
|
} else {
|
2012-04-28 13:35:52 -04:00
|
|
|
// can only open read-only
|
2012-06-28 03:21:15 -04:00
|
|
|
dbStruct.readOnly = true;
|
2012-04-28 13:35:52 -04:00
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
2012-07-06 12:50:52 -04:00
|
|
|
file.close();
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
Database* db = new Database();
|
|
|
|
dbStruct.dbWidget = new DatabaseWidget(db, this);
|
2017-11-26 16:58:54 -05:00
|
|
|
dbStruct.fileInfo = fileInfo;
|
2012-07-08 04:15:05 -04:00
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
insertDatabase(db, dbStruct);
|
2012-04-24 05:47:16 -04:00
|
|
|
|
2015-01-21 11:19:05 -05:00
|
|
|
if (dbStruct.readOnly) {
|
2017-03-10 09:58:42 -05:00
|
|
|
emit messageTab(tr("File opened in read only mode."), MessageWidget::Warning);
|
2015-01-21 11:19:05 -05:00
|
|
|
}
|
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
updateLastDatabases(dbStruct.fileInfo.absoluteFilePath());
|
2012-06-29 17:02:44 -04:00
|
|
|
|
2017-11-26 22:30:18 -05:00
|
|
|
if (!pw.isNull() || !keyFile.isEmpty()) {
|
2017-11-26 16:58:54 -05:00
|
|
|
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath(), pw, keyFile);
|
|
|
|
} else {
|
|
|
|
dbStruct.dbWidget->switchToOpenDatabase(dbStruct.fileInfo.absoluteFilePath());
|
2012-04-24 05:47:16 -04:00
|
|
|
}
|
2018-03-31 16:01:30 -04:00
|
|
|
|
2017-11-27 22:46:03 -05:00
|
|
|
emit messageDismissTab();
|
2011-12-26 13:17:11 -05:00
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2017-01-08 19:33:21 -05:00
|
|
|
void DatabaseTabWidget::importCsv()
|
|
|
|
{
|
2018-03-13 15:55:37 -04:00
|
|
|
QString filter = QString("%1 (*.csv);;%2 (*)").arg(tr("CSV file"), tr("All files"));
|
2018-03-31 16:01:30 -04:00
|
|
|
QString fileName = fileDialog()->getOpenFileName(this, tr("Open CSV file"), QString(), filter);
|
2017-01-08 19:33:21 -05:00
|
|
|
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Database* db = new Database();
|
|
|
|
DatabaseManagerStruct dbStruct;
|
|
|
|
dbStruct.dbWidget = new DatabaseWidget(db, this);
|
|
|
|
|
|
|
|
insertDatabase(db, dbStruct);
|
|
|
|
dbStruct.dbWidget->switchToImportCsv(fileName);
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:37:42 -05:00
|
|
|
void DatabaseTabWidget::mergeDatabase()
|
|
|
|
{
|
2018-05-18 17:06:28 -04:00
|
|
|
auto dbWidget = currentDatabaseWidget();
|
|
|
|
if (dbWidget && dbWidget->currentMode() != DatabaseWidget::LockedMode) {
|
|
|
|
QString filter = QString("%1 (*.kdbx);;%2 (*)").arg(tr("KeePass 2 Database"), tr("All files"));
|
|
|
|
const QString fileName = fileDialog()->getOpenFileName(this, tr("Merge database"), QString(),
|
|
|
|
filter);
|
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
mergeDatabase(fileName);
|
|
|
|
}
|
2016-11-07 22:37:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseTabWidget::mergeDatabase(const QString& fileName)
|
|
|
|
{
|
|
|
|
currentDatabaseWidget()->switchToOpenMergeDatabase(fileName);
|
|
|
|
}
|
|
|
|
|
2012-05-12 04:08:41 -04:00
|
|
|
void DatabaseTabWidget::importKeePass1Database()
|
|
|
|
{
|
2018-03-13 15:55:37 -04:00
|
|
|
QString filter = QString("%1 (*.kdb);;%2 (*)").arg(tr("KeePass 1 database"), tr("All files"));
|
2018-03-31 16:01:30 -04:00
|
|
|
QString fileName = fileDialog()->getOpenFileName(this, tr("Open KeePass 1 database"), QString(), filter);
|
2012-05-12 04:08:41 -04:00
|
|
|
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
Database* db = new Database();
|
|
|
|
DatabaseManagerStruct dbStruct;
|
|
|
|
dbStruct.dbWidget = new DatabaseWidget(db, this);
|
2016-11-11 17:58:47 -05:00
|
|
|
dbStruct.dbWidget->databaseModified();
|
2012-06-28 03:21:15 -04:00
|
|
|
dbStruct.modified = true;
|
2011-12-26 13:17:11 -05:00
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
insertDatabase(db, dbStruct);
|
2011-12-26 13:17:11 -05:00
|
|
|
|
2012-07-06 12:50:52 -04:00
|
|
|
dbStruct.dbWidget->switchToImportKeepass1(fileName);
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
bool DatabaseTabWidget::closeDatabase(Database* db)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
|
|
|
Q_ASSERT(db);
|
|
|
|
|
|
|
|
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
|
|
|
|
int index = databaseIndex(db);
|
|
|
|
Q_ASSERT(index != -1);
|
2012-04-21 14:36:26 -04:00
|
|
|
|
2016-11-11 16:26:07 -05:00
|
|
|
dbStruct.dbWidget->closeUnlockDialog();
|
2012-04-21 14:36:26 -04:00
|
|
|
QString dbName = tabText(index);
|
|
|
|
if (dbName.right(1) == "*") {
|
|
|
|
dbName.chop(1);
|
|
|
|
}
|
2016-07-31 18:22:04 -04:00
|
|
|
if (dbStruct.dbWidget->isInEditMode() && db->hasKey() && dbStruct.dbWidget->isEditWidgetModified()) {
|
2018-03-31 16:01:30 -04:00
|
|
|
QMessageBox::StandardButton result = MessageBox::question(
|
|
|
|
this,
|
|
|
|
tr("Close?"),
|
2017-02-06 14:23:51 -05:00
|
|
|
tr("\"%1\" is in edit mode.\nDiscard changes and close anyway?").arg(dbName.toHtmlEscaped()),
|
2018-03-31 16:01:30 -04:00
|
|
|
QMessageBox::Discard | QMessageBox::Cancel,
|
|
|
|
QMessageBox::Cancel);
|
2015-05-12 13:46:48 -04:00
|
|
|
if (result == QMessageBox::Cancel) {
|
2012-04-21 14:36:26 -04:00
|
|
|
return false;
|
2012-04-11 12:40:52 -04:00
|
|
|
}
|
2012-04-21 14:36:26 -04:00
|
|
|
}
|
|
|
|
if (dbStruct.modified) {
|
2012-06-12 03:53:59 -04:00
|
|
|
if (config()->get("AutoSaveOnExit").toBool()) {
|
2015-07-18 11:36:08 -04:00
|
|
|
if (!saveDatabase(db)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-23 12:51:02 -05:00
|
|
|
} else if (dbStruct.dbWidget->currentMode() != DatabaseWidget::LockedMode) {
|
2012-06-10 15:54:58 -04:00
|
|
|
QMessageBox::StandardButton result =
|
2018-03-31 16:01:30 -04:00
|
|
|
MessageBox::question(this,
|
|
|
|
tr("Save changes?"),
|
|
|
|
tr("\"%1\" was modified.\nSave changes?").arg(dbName.toHtmlEscaped()),
|
|
|
|
QMessageBox::Yes | QMessageBox::Discard | QMessageBox::Cancel,
|
|
|
|
QMessageBox::Yes);
|
2012-06-10 15:54:58 -04:00
|
|
|
if (result == QMessageBox::Yes) {
|
2015-07-18 11:36:08 -04:00
|
|
|
if (!saveDatabase(db)) {
|
2017-11-23 12:51:02 -05:00
|
|
|
return false;
|
2015-07-18 11:36:08 -04:00
|
|
|
}
|
2017-11-23 12:51:02 -05:00
|
|
|
} else if (result == QMessageBox::Cancel) {
|
2012-06-10 15:54:58 -04:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-11 14:35:52 -04:00
|
|
|
}
|
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2012-06-29 17:40:51 -04:00
|
|
|
deleteDatabase(db);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseTabWidget::deleteDatabase(Database* db)
|
|
|
|
{
|
|
|
|
const DatabaseManagerStruct dbStruct = m_dbList.value(db);
|
2017-11-26 16:58:54 -05:00
|
|
|
bool emitDatabaseWithFileClosed = dbStruct.fileInfo.exists() && !dbStruct.readOnly;
|
|
|
|
QString filePath = dbStruct.fileInfo.absoluteFilePath();
|
2013-03-24 18:21:59 -04:00
|
|
|
|
2012-06-29 17:40:51 -04:00
|
|
|
int index = databaseIndex(db);
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
removeTab(index);
|
2012-04-20 05:29:31 -04:00
|
|
|
toggleTabbar();
|
2012-04-11 14:35:52 -04:00
|
|
|
m_dbList.remove(db);
|
2011-11-13 08:55:20 -05:00
|
|
|
delete dbStruct.dbWidget;
|
|
|
|
delete db;
|
2013-03-24 18:21:59 -04:00
|
|
|
|
|
|
|
if (emitDatabaseWithFileClosed) {
|
2017-03-10 09:58:42 -05:00
|
|
|
emit databaseWithFileClosed(filePath);
|
2013-03-24 18:21:59 -04:00
|
|
|
}
|
2012-04-11 14:35:52 -04:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:22:07 -04:00
|
|
|
bool DatabaseTabWidget::closeAllDatabases()
|
|
|
|
{
|
2012-04-11 14:35:52 -04:00
|
|
|
while (!m_dbList.isEmpty()) {
|
|
|
|
if (!closeDatabase()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
bool DatabaseTabWidget::saveDatabase(Database* db, QString filePath)
|
2011-11-16 12:47:17 -05:00
|
|
|
{
|
|
|
|
DatabaseManagerStruct& dbStruct = m_dbList[db];
|
|
|
|
|
2017-11-26 22:30:18 -05:00
|
|
|
// Never allow saving a locked database; it causes corruption
|
|
|
|
Q_ASSERT(dbStruct.dbWidget->currentMode() != DatabaseWidget::LockedMode);
|
|
|
|
// Release build interlock
|
2017-11-23 12:51:02 -05:00
|
|
|
if (dbStruct.dbWidget->currentMode() == DatabaseWidget::LockedMode) {
|
|
|
|
// We return true since a save is not required
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-11 17:58:47 -05:00
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
if (!dbStruct.readOnly) {
|
|
|
|
if (filePath.isEmpty()) {
|
|
|
|
filePath = dbStruct.fileInfo.canonicalFilePath();
|
|
|
|
}
|
|
|
|
|
2017-06-14 19:50:19 -04:00
|
|
|
dbStruct.dbWidget->blockAutoReload(true);
|
2018-01-14 18:04:33 -05:00
|
|
|
// TODO: Make this async, but lock out the database widget to prevent re-entrance
|
2018-01-28 10:14:36 -05:00
|
|
|
bool useAtomicSaves = config()->get("UseAtomicSaves", true).toBool();
|
2018-01-17 20:13:13 -05:00
|
|
|
QString errorMessage = db->saveToFile(filePath, useAtomicSaves, config()->get("BackupBeforeSave").toBool());
|
2017-06-14 19:50:19 -04:00
|
|
|
dbStruct.dbWidget->blockAutoReload(false);
|
|
|
|
|
|
|
|
if (errorMessage.isEmpty()) {
|
|
|
|
// successfully saved database file
|
|
|
|
dbStruct.modified = false;
|
2018-01-17 20:13:13 -05:00
|
|
|
dbStruct.saveAttempts = 0;
|
2017-11-26 22:30:18 -05:00
|
|
|
dbStruct.fileInfo = QFileInfo(filePath);
|
2017-06-14 19:50:19 -04:00
|
|
|
dbStruct.dbWidget->databaseSaved();
|
|
|
|
updateTabName(db);
|
|
|
|
emit messageDismissTab();
|
|
|
|
return true;
|
2017-02-24 12:43:15 -05:00
|
|
|
} else {
|
2017-09-23 11:45:20 -04:00
|
|
|
dbStruct.modified = true;
|
|
|
|
updateTabName(db);
|
2018-01-17 20:13:13 -05:00
|
|
|
|
|
|
|
if (++dbStruct.saveAttempts > 2 && useAtomicSaves) {
|
|
|
|
// Saving failed 3 times, issue a warning and attempt to resolve
|
2018-03-31 16:01:30 -04:00
|
|
|
auto choice = MessageBox::question(this,
|
|
|
|
tr("Disable safe saves?"),
|
2018-01-17 20:13:13 -05:00
|
|
|
tr("KeePassXC has failed to save the database multiple times. "
|
2018-03-31 16:01:30 -04:00
|
|
|
"This is likely caused by file sync services holding a lock on "
|
|
|
|
"the save file.\nDisable safe saves and try again?"),
|
|
|
|
QMessageBox::Yes | QMessageBox::No,
|
|
|
|
QMessageBox::Yes);
|
2018-01-17 20:13:13 -05:00
|
|
|
if (choice == QMessageBox::Yes) {
|
|
|
|
config()->set("UseAtomicSaves", false);
|
|
|
|
return saveDatabase(db, filePath);
|
|
|
|
}
|
|
|
|
// Reset save attempts without changing anything
|
|
|
|
dbStruct.saveAttempts = 0;
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
emit messageTab(tr("Writing the database failed.").append("\n").append(errorMessage), MessageWidget::Error);
|
2016-07-31 11:03:25 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-24 12:43:15 -05:00
|
|
|
} else {
|
2015-01-11 10:20:59 -05:00
|
|
|
return saveDatabaseAs(db);
|
2011-12-24 13:22:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 10:20:59 -05:00
|
|
|
bool DatabaseTabWidget::saveDatabaseAs(Database* db)
|
2011-12-24 13:22:42 -05:00
|
|
|
{
|
2017-02-13 11:35:38 -05:00
|
|
|
while (true) {
|
|
|
|
DatabaseManagerStruct& dbStruct = m_dbList[db];
|
2017-11-26 22:30:18 -05:00
|
|
|
QString oldFilePath;
|
2017-11-26 16:58:54 -05:00
|
|
|
if (dbStruct.fileInfo.exists()) {
|
2017-11-26 22:30:18 -05:00
|
|
|
oldFilePath = dbStruct.fileInfo.absoluteFilePath();
|
2017-02-13 11:35:38 -05:00
|
|
|
} else {
|
2017-11-26 22:30:18 -05:00
|
|
|
oldFilePath = QDir::toNativeSeparators(QDir::homePath() + "/" + tr("Passwords").append(".kdbx"));
|
2015-07-18 18:41:35 -04:00
|
|
|
}
|
2018-03-31 16:01:30 -04:00
|
|
|
QString newFilePath = fileDialog()->getSaveFileName(this,
|
|
|
|
tr("Save database as"),
|
|
|
|
oldFilePath,
|
2017-12-20 10:18:01 -05:00
|
|
|
tr("KeePass 2 Database").append(" (*.kdbx)"),
|
2018-03-31 16:01:30 -04:00
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
"kdbx");
|
2017-11-26 22:30:18 -05:00
|
|
|
if (!newFilePath.isEmpty()) {
|
|
|
|
// Ensure we don't recurse back into this function
|
|
|
|
dbStruct.readOnly = false;
|
|
|
|
|
|
|
|
if (!saveDatabase(db, newFilePath)) {
|
2017-11-26 16:58:54 -05:00
|
|
|
// Failed to save, try again
|
2017-02-13 11:35:38 -05:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-18 18:41:35 -04:00
|
|
|
|
2017-12-20 10:18:01 -05:00
|
|
|
dbStruct.dbWidget->updateFilePath(dbStruct.fileInfo.absoluteFilePath());
|
2017-11-26 16:58:54 -05:00
|
|
|
updateLastDatabases(dbStruct.fileInfo.absoluteFilePath());
|
2017-02-13 11:35:38 -05:00
|
|
|
return true;
|
2011-12-24 13:22:42 -05:00
|
|
|
}
|
2017-11-26 22:30:18 -05:00
|
|
|
|
|
|
|
// Canceled file selection
|
|
|
|
return false;
|
2015-01-11 10:20:59 -05:00
|
|
|
}
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
bool DatabaseTabWidget::closeDatabase(int index)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
2011-11-16 12:47:17 -05:00
|
|
|
if (index == -1) {
|
2011-12-30 12:43:24 -05:00
|
|
|
index = currentIndex();
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
|
2012-05-02 16:39:45 -04:00
|
|
|
setCurrentIndex(index);
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
return closeDatabase(indexDatabase(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseTabWidget::closeDatabaseFromSender()
|
|
|
|
{
|
|
|
|
Q_ASSERT(sender());
|
|
|
|
DatabaseWidget* dbWidget = static_cast<DatabaseWidget*>(sender());
|
|
|
|
Database* db = databaseFromDatabaseWidget(dbWidget);
|
2012-05-02 16:39:45 -04:00
|
|
|
int index = databaseIndex(db);
|
|
|
|
setCurrentIndex(index);
|
2012-04-11 14:35:52 -04:00
|
|
|
closeDatabase(db);
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 10:20:59 -05:00
|
|
|
bool DatabaseTabWidget::saveDatabase(int index)
|
2011-11-16 12:47:17 -05:00
|
|
|
{
|
|
|
|
if (index == -1) {
|
2011-12-30 12:43:24 -05:00
|
|
|
index = currentIndex();
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 10:20:59 -05:00
|
|
|
return saveDatabase(indexDatabase(index));
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 10:20:59 -05:00
|
|
|
bool DatabaseTabWidget::saveDatabaseAs(int index)
|
2011-12-24 13:22:42 -05:00
|
|
|
{
|
|
|
|
if (index == -1) {
|
2011-12-30 12:43:24 -05:00
|
|
|
index = currentIndex();
|
2011-12-24 13:22:42 -05:00
|
|
|
}
|
2015-01-11 10:20:59 -05:00
|
|
|
|
|
|
|
return saveDatabaseAs(indexDatabase(index));
|
2011-12-24 13:22:42 -05:00
|
|
|
}
|
|
|
|
|
2015-07-14 16:14:34 -04:00
|
|
|
void DatabaseTabWidget::exportToCsv()
|
|
|
|
{
|
|
|
|
Database* db = indexDatabase(currentIndex());
|
|
|
|
if (!db) {
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
QString fileName = fileDialog()->getSaveFileName(
|
|
|
|
this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, 0, "csv");
|
2015-07-14 16:14:34 -04:00
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CsvExporter csvExporter;
|
|
|
|
if (!csvExporter.exportDatabase(fileName, db)) {
|
2018-03-31 16:01:30 -04:00
|
|
|
emit messageGlobal(tr("Writing the CSV file failed.").append("\n").append(csvExporter.errorString()),
|
|
|
|
MessageWidget::Error);
|
2015-07-14 16:14:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-13 11:52:37 -05:00
|
|
|
void DatabaseTabWidget::changeMasterKey()
|
|
|
|
{
|
|
|
|
currentDatabaseWidget()->switchToMasterKeyChange();
|
|
|
|
}
|
|
|
|
|
2012-04-19 10:20:20 -04:00
|
|
|
void DatabaseTabWidget::changeDatabaseSettings()
|
|
|
|
{
|
|
|
|
currentDatabaseWidget()->switchToDatabaseSettings();
|
|
|
|
}
|
|
|
|
|
2012-05-28 12:38:33 -04:00
|
|
|
bool DatabaseTabWidget::readOnly(int index)
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
index = currentIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexDatabaseManagerStruct(index).readOnly;
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:23:55 -04:00
|
|
|
bool DatabaseTabWidget::canSave(int index)
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
index = currentIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
const DatabaseManagerStruct& dbStruct = indexDatabaseManagerStruct(index);
|
2017-11-26 16:58:54 -05:00
|
|
|
return dbStruct.modified && !dbStruct.readOnly;
|
2017-10-24 13:23:55 -04:00
|
|
|
}
|
|
|
|
|
2017-03-20 22:29:36 -04:00
|
|
|
bool DatabaseTabWidget::isModified(int index)
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
index = currentIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexDatabaseManagerStruct(index).modified;
|
|
|
|
}
|
|
|
|
|
2017-03-21 22:01:44 -04:00
|
|
|
QString DatabaseTabWidget::databasePath(int index)
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
index = currentIndex();
|
|
|
|
}
|
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
return indexDatabaseManagerStruct(index).fileInfo.absoluteFilePath();
|
2017-03-21 22:01:44 -04:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
void DatabaseTabWidget::updateTabName(Database* db)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
|
|
|
int index = databaseIndex(db);
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
|
|
|
|
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
|
|
|
|
|
|
|
|
QString tabName;
|
2011-11-16 12:47:17 -05:00
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
if (dbStruct.fileInfo.exists()) {
|
2011-11-16 12:47:17 -05:00
|
|
|
if (db->metadata()->name().isEmpty()) {
|
2017-11-26 16:58:54 -05:00
|
|
|
tabName = dbStruct.fileInfo.fileName();
|
|
|
|
} else {
|
2011-12-25 13:47:41 -05:00
|
|
|
tabName = db->metadata()->name();
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
2011-12-25 13:47:41 -05:00
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
setTabToolTip(index, dbStruct.fileInfo.absoluteFilePath());
|
|
|
|
} else {
|
2011-11-16 12:47:17 -05:00
|
|
|
if (db->metadata()->name().isEmpty()) {
|
|
|
|
tabName = tr("New database");
|
2017-11-26 16:58:54 -05:00
|
|
|
} else {
|
2018-03-13 16:02:38 -04:00
|
|
|
tabName = tr("%1 [New database]", "tab modifier").arg(db->metadata()->name());
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
2012-10-12 06:12:00 -04:00
|
|
|
|
|
|
|
if (dbStruct.dbWidget->currentMode() == DatabaseWidget::LockedMode) {
|
2018-03-13 16:02:38 -04:00
|
|
|
tabName = tr("%1 [locked]", "tab modifier").arg(tabName);
|
2012-10-12 06:12:00 -04:00
|
|
|
}
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
if (dbStruct.modified) {
|
|
|
|
tabName.append("*");
|
|
|
|
}
|
2012-10-12 06:12:00 -04:00
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
setTabText(index, tabName);
|
2017-03-10 09:58:42 -05:00
|
|
|
emit tabNameChanged();
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2012-10-12 06:12:00 -04:00
|
|
|
void DatabaseTabWidget::updateTabNameFromDbSender()
|
2012-05-14 11:04:05 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(qobject_cast<Database*>(sender()));
|
|
|
|
|
|
|
|
updateTabName(static_cast<Database*>(sender()));
|
|
|
|
}
|
|
|
|
|
2012-10-12 06:12:00 -04:00
|
|
|
void DatabaseTabWidget::updateTabNameFromDbWidgetSender()
|
|
|
|
{
|
|
|
|
Q_ASSERT(qobject_cast<DatabaseWidget*>(sender()));
|
|
|
|
Q_ASSERT(databaseFromDatabaseWidget(qobject_cast<DatabaseWidget*>(sender())));
|
|
|
|
|
|
|
|
DatabaseWidget* dbWidget = static_cast<DatabaseWidget*>(sender());
|
|
|
|
updateTabName(databaseFromDatabaseWidget(dbWidget));
|
2017-10-09 07:26:24 -04:00
|
|
|
|
|
|
|
Database* db = dbWidget->database();
|
2018-03-31 16:01:30 -04:00
|
|
|
Group* autoload = db->rootGroup()->findChildByName("AutoOpen");
|
2017-10-12 17:30:51 -04:00
|
|
|
if (autoload) {
|
2017-10-09 07:26:24 -04:00
|
|
|
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
|
2017-12-07 22:40:37 -05:00
|
|
|
QDir dbFolder(dbStruct.fileInfo.canonicalPath());
|
2017-10-09 07:26:24 -04:00
|
|
|
for (auto entry : autoload->entries()) {
|
2017-10-12 17:30:51 -04:00
|
|
|
if (entry->url().isEmpty() || entry->password().isEmpty()) {
|
2017-10-09 07:26:24 -04:00
|
|
|
continue;
|
2017-10-12 17:30:51 -04:00
|
|
|
}
|
2017-10-09 07:26:24 -04:00
|
|
|
QFileInfo filepath;
|
2017-10-12 17:30:51 -04:00
|
|
|
if (entry->url().startsWith("file://")) {
|
2017-10-09 07:26:24 -04:00
|
|
|
QUrl url(entry->url());
|
|
|
|
filepath.setFile(url.toLocalFile());
|
2018-03-31 16:01:30 -04:00
|
|
|
} else {
|
2017-10-09 07:26:24 -04:00
|
|
|
filepath.setFile(entry->url());
|
|
|
|
if (filepath.isRelative()) {
|
|
|
|
filepath.setFile(dbFolder, entry->url());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:30:51 -04:00
|
|
|
if (!filepath.isFile()) {
|
2017-10-09 07:26:24 -04:00
|
|
|
continue;
|
2017-10-12 17:30:51 -04:00
|
|
|
}
|
2017-10-09 07:26:24 -04:00
|
|
|
|
|
|
|
openDatabase(filepath.canonicalFilePath(), entry->password(), "");
|
|
|
|
}
|
|
|
|
}
|
2012-10-12 06:12:00 -04:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
int DatabaseTabWidget::databaseIndex(Database* db)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
|
|
|
QWidget* dbWidget = m_dbList.value(db).dbWidget;
|
2011-12-30 12:43:24 -05:00
|
|
|
return indexOf(dbWidget);
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
Database* DatabaseTabWidget::indexDatabase(int index)
|
2011-11-13 08:55:20 -05:00
|
|
|
{
|
2011-12-30 12:43:24 -05:00
|
|
|
QWidget* dbWidget = widget(index);
|
2011-11-13 08:55:20 -05:00
|
|
|
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value().dbWidget == dbWidget) {
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
2011-11-16 12:47:17 -05:00
|
|
|
|
2012-05-28 12:38:33 -04:00
|
|
|
DatabaseManagerStruct DatabaseTabWidget::indexDatabaseManagerStruct(int index)
|
|
|
|
{
|
|
|
|
QWidget* dbWidget = widget(index);
|
|
|
|
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value().dbWidget == dbWidget) {
|
|
|
|
return i.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DatabaseManagerStruct();
|
|
|
|
}
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
Database* DatabaseTabWidget::databaseFromDatabaseWidget(DatabaseWidget* dbWidget)
|
|
|
|
{
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value().dbWidget == dbWidget) {
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2012-04-11 14:35:52 -04:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct)
|
2011-11-16 12:47:17 -05:00
|
|
|
{
|
|
|
|
m_dbList.insert(db, dbStruct);
|
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
addTab(dbStruct.dbWidget, "");
|
2012-04-20 05:29:31 -04:00
|
|
|
toggleTabbar();
|
2011-11-16 12:47:17 -05:00
|
|
|
updateTabName(db);
|
2011-12-24 18:56:04 -05:00
|
|
|
int index = databaseIndex(db);
|
2011-12-30 12:43:24 -05:00
|
|
|
setCurrentIndex(index);
|
2012-06-28 03:21:15 -04:00
|
|
|
connectDatabase(db);
|
2012-05-05 05:41:23 -04:00
|
|
|
connect(dbStruct.dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabaseFromSender()));
|
2016-11-11 17:58:47 -05:00
|
|
|
connect(dbStruct.dbWidget, SIGNAL(databaseChanged(Database*, bool)), SLOT(changeDatabase(Database*, bool)));
|
2012-10-12 06:12:00 -04:00
|
|
|
connect(dbStruct.dbWidget, SIGNAL(unlockedDatabase()), SLOT(updateTabNameFromDbWidgetSender()));
|
2016-10-08 12:55:05 -04:00
|
|
|
connect(dbStruct.dbWidget, SIGNAL(unlockedDatabase()), SLOT(emitDatabaseUnlockedFromDbWidgetSender()));
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
2011-12-29 14:03:20 -05:00
|
|
|
|
2011-12-30 12:43:24 -05:00
|
|
|
DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget()
|
2011-12-29 14:03:20 -05:00
|
|
|
{
|
2011-12-30 12:43:24 -05:00
|
|
|
Database* db = indexDatabase(currentIndex());
|
2011-12-29 14:03:20 -05:00
|
|
|
if (db) {
|
|
|
|
return m_dbList[db].dbWidget;
|
2018-03-31 16:01:30 -04:00
|
|
|
} else {
|
2015-07-24 12:28:12 -04:00
|
|
|
return nullptr;
|
2011-12-29 14:03:20 -05:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 14:35:52 -04:00
|
|
|
|
2014-05-15 16:53:59 -04:00
|
|
|
bool DatabaseTabWidget::hasLockableDatabases() const
|
2012-10-12 06:12:00 -04:00
|
|
|
{
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
DatabaseWidget::Mode mode = i.value().dbWidget->currentMode();
|
|
|
|
|
2018-03-31 16:01:30 -04:00
|
|
|
if ((mode == DatabaseWidget::ViewMode || mode == DatabaseWidget::EditMode) && i.value().dbWidget->dbHasKey()) {
|
2012-10-12 06:12:00 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseTabWidget::lockDatabases()
|
|
|
|
{
|
2015-09-13 06:38:19 -04:00
|
|
|
clipboard()->clearCopiedText();
|
|
|
|
|
2015-01-11 10:20:59 -05:00
|
|
|
for (int i = 0; i < count(); i++) {
|
|
|
|
DatabaseWidget* dbWidget = static_cast<DatabaseWidget*>(widget(i));
|
|
|
|
Database* db = databaseFromDatabaseWidget(dbWidget);
|
2012-10-12 06:12:00 -04:00
|
|
|
|
2017-12-20 10:18:01 -05:00
|
|
|
if (dbWidget->currentMode() == DatabaseWidget::LockedMode || !dbWidget->dbHasKey()) {
|
2015-01-11 10:20:59 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// show the correct tab widget before we are asking questions about it
|
|
|
|
setCurrentWidget(dbWidget);
|
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
if (dbWidget->currentMode() == DatabaseWidget::EditMode && dbWidget->isEditWidgetModified()) {
|
2015-01-11 10:20:59 -05:00
|
|
|
QMessageBox::StandardButton result =
|
2018-03-31 16:01:30 -04:00
|
|
|
MessageBox::question(this,
|
|
|
|
tr("Lock database"),
|
|
|
|
tr("Can't lock the database as you are currently editing it.\nPlease press cancel "
|
|
|
|
"to finish your changes or discard them."),
|
|
|
|
QMessageBox::Discard | QMessageBox::Cancel,
|
|
|
|
QMessageBox::Cancel);
|
2015-01-11 10:20:59 -05:00
|
|
|
if (result == QMessageBox::Cancel) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
if (m_dbList[db].modified) {
|
2015-01-11 10:20:59 -05:00
|
|
|
QMessageBox::StandardButton result =
|
2018-03-31 16:01:30 -04:00
|
|
|
MessageBox::question(this,
|
|
|
|
tr("Lock database"),
|
|
|
|
tr("This database has been modified.\nDo you want to save the database before "
|
|
|
|
"locking it?\nOtherwise your changes are lost."),
|
|
|
|
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
|
|
|
|
QMessageBox::Cancel);
|
2015-01-11 10:20:59 -05:00
|
|
|
if (result == QMessageBox::Save) {
|
|
|
|
if (!saveDatabase(db)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-31 16:01:30 -04:00
|
|
|
} else if (result == QMessageBox::Discard) {
|
2015-01-11 10:20:59 -05:00
|
|
|
m_dbList[db].modified = false;
|
2017-01-28 22:02:35 -05:00
|
|
|
m_dbList[db].dbWidget->databaseSaved();
|
2018-03-31 16:01:30 -04:00
|
|
|
} else if (result == QMessageBox::Cancel) {
|
2015-01-11 10:20:59 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dbWidget->lock();
|
|
|
|
// database has changed so we can't use the db variable anymore
|
|
|
|
updateTabName(dbWidget->database());
|
2016-10-08 12:55:05 -04:00
|
|
|
|
2017-03-10 09:58:42 -05:00
|
|
|
emit databaseLocked(dbWidget);
|
2012-10-12 06:12:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 07:25:16 -05:00
|
|
|
/**
|
|
|
|
* This function relock the pending database when autotype has been performed successfully
|
|
|
|
* A database is marked as pending when it's unlocked after a global Auto-Type invocation
|
|
|
|
*/
|
|
|
|
void DatabaseTabWidget::relockPendingDatabase()
|
|
|
|
{
|
|
|
|
if (!m_dbPendingLock || !config()->get("security/relockautotype").toBool()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dbPendingLock->currentMode() == DatabaseWidget::LockedMode || !m_dbPendingLock->dbHasKey()) {
|
|
|
|
m_dbPendingLock = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dbPendingLock->lock();
|
|
|
|
|
|
|
|
emit databaseLocked(m_dbPendingLock);
|
|
|
|
m_dbPendingLock = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
void DatabaseTabWidget::modified()
|
|
|
|
{
|
2012-05-14 11:04:05 -04:00
|
|
|
Q_ASSERT(qobject_cast<Database*>(sender()));
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
Database* db = static_cast<Database*>(sender());
|
|
|
|
DatabaseManagerStruct& dbStruct = m_dbList[db];
|
2012-06-10 10:28:46 -04:00
|
|
|
|
2017-11-26 16:58:54 -05:00
|
|
|
if (config()->get("AutoSaveAfterEveryChange").toBool() && !dbStruct.readOnly) {
|
2012-06-24 11:53:01 -04:00
|
|
|
saveDatabase(db);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
if (!dbStruct.modified) {
|
|
|
|
dbStruct.modified = true;
|
2016-11-11 17:58:47 -05:00
|
|
|
dbStruct.dbWidget->databaseModified();
|
2012-04-11 14:35:52 -04:00
|
|
|
updateTabName(db);
|
|
|
|
}
|
|
|
|
}
|
2012-05-27 17:01:14 -04:00
|
|
|
|
|
|
|
void DatabaseTabWidget::updateLastDatabases(const QString& filename)
|
|
|
|
{
|
|
|
|
if (!config()->get("RememberLastDatabases").toBool()) {
|
|
|
|
config()->set("LastDatabases", QVariant());
|
2018-03-31 16:01:30 -04:00
|
|
|
} else {
|
2012-05-27 17:01:14 -04:00
|
|
|
QStringList lastDatabases = config()->get("LastDatabases", QVariant()).toStringList();
|
|
|
|
lastDatabases.prepend(filename);
|
|
|
|
lastDatabases.removeDuplicates();
|
|
|
|
|
|
|
|
while (lastDatabases.count() > LastDatabasesCount) {
|
|
|
|
lastDatabases.removeLast();
|
|
|
|
}
|
|
|
|
config()->set("LastDatabases", lastDatabases);
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 03:21:15 -04:00
|
|
|
|
2016-11-11 17:58:47 -05:00
|
|
|
void DatabaseTabWidget::changeDatabase(Database* newDb, bool unsavedChanges)
|
2012-06-28 03:21:15 -04:00
|
|
|
{
|
|
|
|
Q_ASSERT(sender());
|
|
|
|
Q_ASSERT(!m_dbList.contains(newDb));
|
|
|
|
|
|
|
|
DatabaseWidget* dbWidget = static_cast<DatabaseWidget*>(sender());
|
|
|
|
Database* oldDb = databaseFromDatabaseWidget(dbWidget);
|
|
|
|
DatabaseManagerStruct dbStruct = m_dbList[oldDb];
|
2016-11-11 17:58:47 -05:00
|
|
|
dbStruct.modified = unsavedChanges;
|
2012-06-28 03:21:15 -04:00
|
|
|
m_dbList.remove(oldDb);
|
|
|
|
m_dbList.insert(newDb, dbStruct);
|
|
|
|
|
|
|
|
updateTabName(newDb);
|
|
|
|
connectDatabase(newDb, oldDb);
|
|
|
|
}
|
|
|
|
|
2014-05-17 05:16:27 -04:00
|
|
|
void DatabaseTabWidget::emitActivateDatabaseChanged()
|
|
|
|
{
|
2017-03-10 09:58:42 -05:00
|
|
|
emit activateDatabaseChanged(currentDatabaseWidget());
|
2014-05-17 05:16:27 -04:00
|
|
|
}
|
|
|
|
|
2016-10-08 12:55:05 -04:00
|
|
|
void DatabaseTabWidget::emitDatabaseUnlockedFromDbWidgetSender()
|
|
|
|
{
|
2017-03-10 09:58:42 -05:00
|
|
|
emit databaseUnlocked(static_cast<DatabaseWidget*>(sender()));
|
2016-10-08 12:55:05 -04:00
|
|
|
}
|
|
|
|
|
2012-06-28 03:21:15 -04:00
|
|
|
void DatabaseTabWidget::connectDatabase(Database* newDb, Database* oldDb)
|
|
|
|
{
|
|
|
|
if (oldDb) {
|
|
|
|
oldDb->disconnect(this);
|
|
|
|
}
|
|
|
|
|
2012-10-12 06:12:00 -04:00
|
|
|
connect(newDb, SIGNAL(nameTextChanged()), SLOT(updateTabNameFromDbSender()));
|
2012-06-28 03:21:15 -04:00
|
|
|
connect(newDb, SIGNAL(modified()), SLOT(modified()));
|
|
|
|
newDb->setEmitModified(true);
|
|
|
|
}
|
2012-07-14 13:09:28 -04:00
|
|
|
|
|
|
|
void DatabaseTabWidget::performGlobalAutoType()
|
|
|
|
{
|
2014-04-12 09:29:03 -04:00
|
|
|
QList<Database*> unlockedDatabases;
|
|
|
|
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
DatabaseWidget::Mode mode = i.value().dbWidget->currentMode();
|
|
|
|
|
|
|
|
if (mode != DatabaseWidget::LockedMode) {
|
|
|
|
unlockedDatabases.append(i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:26:07 -05:00
|
|
|
if (unlockedDatabases.size() > 0) {
|
|
|
|
autoType()->performGlobalAutoType(unlockedDatabases);
|
2018-03-31 16:01:30 -04:00
|
|
|
} else if (m_dbList.size() > 0) {
|
2018-02-05 07:25:16 -05:00
|
|
|
m_dbPendingLock = indexDatabaseManagerStruct(0).dbWidget;
|
|
|
|
m_dbPendingLock->showUnlockDialog();
|
2016-11-11 16:26:07 -05:00
|
|
|
}
|
2012-07-14 13:09:28 -04:00
|
|
|
}
|