From ff7191eef3f10a5d70a975177308872dbb750291 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Fri, 23 Nov 2018 13:24:59 -0500 Subject: [PATCH] Re-implement AutoOpen functionality after refactor (#2504) The database refactor in #2491 removed auto-open functionality. --- src/gui/DatabaseTabWidget.cpp | 30 ++++++++++++++++------ src/gui/DatabaseTabWidget.h | 4 +-- src/gui/DatabaseWidget.cpp | 48 +++++++++++++++++++++++++++++++++++ src/gui/DatabaseWidget.h | 3 +++ 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 49c5a1545..7f1f9942e 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -126,8 +126,10 @@ void DatabaseTabWidget::openDatabase() * database has been opened already. * * @param filePath database file path + * @param password optional, password to unlock database + * @param inBackground optional, don't focus tab after opening */ -void DatabaseTabWidget::addDatabaseTab(const QString& filePath) +void DatabaseTabWidget::addDatabaseTab(const QString& filePath, bool inBackground, const QString& password) { QFileInfo fileInfo(filePath); QString canonicalFilePath = fileInfo.canonicalFilePath(); @@ -140,32 +142,44 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath) auto* dbWidget = databaseWidgetFromIndex(i); Q_ASSERT(dbWidget); if (dbWidget && dbWidget->database()->filePath() == canonicalFilePath) { - // switch to existing tab if file is already open - setCurrentIndex(indexOf(dbWidget)); + if (!password.isEmpty()) { + dbWidget->performUnlockDatabase(password); + } + if (!inBackground) { + // switch to existing tab if file is already open + setCurrentIndex(indexOf(dbWidget)); + } return; } } auto* dbWidget = new DatabaseWidget(QSharedPointer::create(filePath), this); - addDatabaseTab(dbWidget); + addDatabaseTab(dbWidget, inBackground); + if (!password.isEmpty()) { + dbWidget->performUnlockDatabase(password); + } updateLastDatabases(filePath); } /** * Add a new database tab containing the given DatabaseWidget * @param filePath + * @param inBackground optional, don't focus tab after opening */ -void DatabaseTabWidget::addDatabaseTab(DatabaseWidget* dbWidget) +void DatabaseTabWidget::addDatabaseTab(DatabaseWidget* dbWidget, bool inBackground) { - auto db = dbWidget->database(); - Q_ASSERT(db); + Q_ASSERT(dbWidget->database()); int index = addTab(dbWidget, ""); updateTabName(index); - setCurrentIndex(index); toggleTabbar(); + if (!inBackground) { + setCurrentIndex(index); + } + connect(dbWidget, SIGNAL(databaseFilePathChanged(QString,QString)), SLOT(updateTabName())); + connect(dbWidget, SIGNAL(requestOpenDatabase(QString,bool,QString)), SLOT(addDatabaseTab(QString,bool,QString))); connect(dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabaseTabFromSender())); connect(dbWidget, SIGNAL(databaseModified()), SLOT(updateTabName())); connect(dbWidget, SIGNAL(databaseSaved()), SLOT(updateTabName())); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index d24b45af8..f9d4bdf15 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -48,8 +48,8 @@ public: bool hasLockableDatabases() const; public slots: - void addDatabaseTab(const QString& filePath); - void addDatabaseTab(DatabaseWidget* dbWidget); + void addDatabaseTab(const QString& filePath, bool inBackground = false, const QString& password = {}); + void addDatabaseTab(DatabaseWidget* dbWidget, bool inBackground = false); bool closeDatabaseTab(int index); bool closeDatabaseTab(DatabaseWidget* dbWidget); bool closeAllDatabaseTabs(); diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 60c529fe6..782c9b83f 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -397,6 +397,7 @@ void DatabaseWidget::replaceDatabase(QSharedPointer db) m_db = std::move(db); connectDatabaseSignals(); m_groupView->changeDatabase(m_db); + processAutoOpen(); } void DatabaseWidget::cloneEntry() @@ -954,6 +955,7 @@ void DatabaseWidget::switchToOpenDatabase(const QString& filePath) setCurrentWidget(m_unlockDatabaseWidget); } } + void DatabaseWidget::switchToCsvImport(const QString& filePath) { setCurrentWidget(m_csvImportWizard); @@ -980,6 +982,18 @@ void DatabaseWidget::switchToImportKeepass1(const QString& filePath) setCurrentWidget(m_keepass1OpenWidget); } +void DatabaseWidget::performUnlockDatabase(const QString& password, const QString& keyfile) +{ + if (password.isEmpty() && keyfile.isEmpty()) { + return; + } + + if (!m_db->isInitialized() || isLocked()) { + switchToOpenDatabase(); + m_databaseOpenWidget->enterKey(password, keyfile); + } +} + void DatabaseWidget::refreshSearch() { if (isSearchActive()) { @@ -1561,3 +1575,37 @@ void DatabaseWidget::emptyRecycleBin() refreshSearch(); } } + +void DatabaseWidget::processAutoOpen() +{ + Q_ASSERT(m_db); + + auto* autoopenGroup = m_db->rootGroup()->findGroupByPath("/AutoOpen"); + if (!autoopenGroup) { + return; + } + + for (const auto* entry : autoopenGroup->entries()) { + if (entry->url().isEmpty() || entry->password().isEmpty()) { + continue; + } + QFileInfo filepath; + if (entry->url().startsWith("file://")) { + QUrl url(entry->url()); + filepath.setFile(url.toLocalFile()); + } else { + filepath.setFile(entry->url()); + if (filepath.isRelative()) { + QFileInfo currentpath(m_db->filePath()); + filepath.setFile(currentpath.absoluteDir(), entry->url()); + } + } + + if (!filepath.isFile()) { + continue; + } + + // Request to open the database file in the background + emit requestOpenDatabase(filepath.canonicalFilePath(), true, entry->password()); + } +} diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 1a908cf19..addabe060 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -132,6 +132,7 @@ signals: void currentModeChanged(DatabaseWidget::Mode mode); void groupChanged(); void entrySelectionChanged(); + void requestOpenDatabase(const QString& filePath, bool inBackground, const QString& password); void databaseMerged(QSharedPointer mergedDb); void groupContextMenuRequested(const QPoint& globalPos); void entryContextMenuRequested(const QPoint& globalPos); @@ -180,6 +181,7 @@ public slots: void switchToOpenMergeDatabase(const QString& filePath); void switchToOpenMergeDatabase(const QString& filePath, const QString& password, const QString& keyFile); void switchToImportKeepass1(const QString& filePath); + void performUnlockDatabase(const QString& password, const QString& keyfile = {}); void emptyRecycleBin(); // Search related slots @@ -226,6 +228,7 @@ private: int addChildWidget(QWidget* w); void setClipboardTextAndMinimize(const QString& text); void setIconFromParent(); + void processAutoOpen(); QSharedPointer m_db;