Re-implement AutoOpen functionality after refactor (#2504)

The database refactor in #2491 removed auto-open functionality.
This commit is contained in:
Jonathan White 2018-11-23 13:24:59 -05:00 committed by Janek Bevendorff
parent 785a64cc3b
commit ff7191eef3
4 changed files with 75 additions and 10 deletions

View File

@ -126,8 +126,10 @@ void DatabaseTabWidget::openDatabase()
* database has been opened already. * database has been opened already.
* *
* @param filePath database file path * @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); QFileInfo fileInfo(filePath);
QString canonicalFilePath = fileInfo.canonicalFilePath(); QString canonicalFilePath = fileInfo.canonicalFilePath();
@ -140,32 +142,44 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath)
auto* dbWidget = databaseWidgetFromIndex(i); auto* dbWidget = databaseWidgetFromIndex(i);
Q_ASSERT(dbWidget); Q_ASSERT(dbWidget);
if (dbWidget && dbWidget->database()->filePath() == canonicalFilePath) { if (dbWidget && dbWidget->database()->filePath() == canonicalFilePath) {
// switch to existing tab if file is already open if (!password.isEmpty()) {
setCurrentIndex(indexOf(dbWidget)); dbWidget->performUnlockDatabase(password);
}
if (!inBackground) {
// switch to existing tab if file is already open
setCurrentIndex(indexOf(dbWidget));
}
return; return;
} }
} }
auto* dbWidget = new DatabaseWidget(QSharedPointer<Database>::create(filePath), this); auto* dbWidget = new DatabaseWidget(QSharedPointer<Database>::create(filePath), this);
addDatabaseTab(dbWidget); addDatabaseTab(dbWidget, inBackground);
if (!password.isEmpty()) {
dbWidget->performUnlockDatabase(password);
}
updateLastDatabases(filePath); updateLastDatabases(filePath);
} }
/** /**
* Add a new database tab containing the given DatabaseWidget * Add a new database tab containing the given DatabaseWidget
* @param filePath * @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(dbWidget->database());
Q_ASSERT(db);
int index = addTab(dbWidget, ""); int index = addTab(dbWidget, "");
updateTabName(index); updateTabName(index);
setCurrentIndex(index);
toggleTabbar(); toggleTabbar();
if (!inBackground) {
setCurrentIndex(index);
}
connect(dbWidget, SIGNAL(databaseFilePathChanged(QString,QString)), SLOT(updateTabName())); 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(closeRequest()), SLOT(closeDatabaseTabFromSender()));
connect(dbWidget, SIGNAL(databaseModified()), SLOT(updateTabName())); connect(dbWidget, SIGNAL(databaseModified()), SLOT(updateTabName()));
connect(dbWidget, SIGNAL(databaseSaved()), SLOT(updateTabName())); connect(dbWidget, SIGNAL(databaseSaved()), SLOT(updateTabName()));

View File

@ -48,8 +48,8 @@ public:
bool hasLockableDatabases() const; bool hasLockableDatabases() const;
public slots: public slots:
void addDatabaseTab(const QString& filePath); void addDatabaseTab(const QString& filePath, bool inBackground = false, const QString& password = {});
void addDatabaseTab(DatabaseWidget* dbWidget); void addDatabaseTab(DatabaseWidget* dbWidget, bool inBackground = false);
bool closeDatabaseTab(int index); bool closeDatabaseTab(int index);
bool closeDatabaseTab(DatabaseWidget* dbWidget); bool closeDatabaseTab(DatabaseWidget* dbWidget);
bool closeAllDatabaseTabs(); bool closeAllDatabaseTabs();

View File

@ -397,6 +397,7 @@ void DatabaseWidget::replaceDatabase(QSharedPointer<Database> db)
m_db = std::move(db); m_db = std::move(db);
connectDatabaseSignals(); connectDatabaseSignals();
m_groupView->changeDatabase(m_db); m_groupView->changeDatabase(m_db);
processAutoOpen();
} }
void DatabaseWidget::cloneEntry() void DatabaseWidget::cloneEntry()
@ -954,6 +955,7 @@ void DatabaseWidget::switchToOpenDatabase(const QString& filePath)
setCurrentWidget(m_unlockDatabaseWidget); setCurrentWidget(m_unlockDatabaseWidget);
} }
} }
void DatabaseWidget::switchToCsvImport(const QString& filePath) void DatabaseWidget::switchToCsvImport(const QString& filePath)
{ {
setCurrentWidget(m_csvImportWizard); setCurrentWidget(m_csvImportWizard);
@ -980,6 +982,18 @@ void DatabaseWidget::switchToImportKeepass1(const QString& filePath)
setCurrentWidget(m_keepass1OpenWidget); 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() void DatabaseWidget::refreshSearch()
{ {
if (isSearchActive()) { if (isSearchActive()) {
@ -1561,3 +1575,37 @@ void DatabaseWidget::emptyRecycleBin()
refreshSearch(); 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());
}
}

View File

@ -132,6 +132,7 @@ signals:
void currentModeChanged(DatabaseWidget::Mode mode); void currentModeChanged(DatabaseWidget::Mode mode);
void groupChanged(); void groupChanged();
void entrySelectionChanged(); void entrySelectionChanged();
void requestOpenDatabase(const QString& filePath, bool inBackground, const QString& password);
void databaseMerged(QSharedPointer<Database> mergedDb); void databaseMerged(QSharedPointer<Database> mergedDb);
void groupContextMenuRequested(const QPoint& globalPos); void groupContextMenuRequested(const QPoint& globalPos);
void entryContextMenuRequested(const QPoint& globalPos); void entryContextMenuRequested(const QPoint& globalPos);
@ -180,6 +181,7 @@ public slots:
void switchToOpenMergeDatabase(const QString& filePath); void switchToOpenMergeDatabase(const QString& filePath);
void switchToOpenMergeDatabase(const QString& filePath, const QString& password, const QString& keyFile); void switchToOpenMergeDatabase(const QString& filePath, const QString& password, const QString& keyFile);
void switchToImportKeepass1(const QString& filePath); void switchToImportKeepass1(const QString& filePath);
void performUnlockDatabase(const QString& password, const QString& keyfile = {});
void emptyRecycleBin(); void emptyRecycleBin();
// Search related slots // Search related slots
@ -226,6 +228,7 @@ private:
int addChildWidget(QWidget* w); int addChildWidget(QWidget* w);
void setClipboardTextAndMinimize(const QString& text); void setClipboardTextAndMinimize(const QString& text);
void setIconFromParent(); void setIconFromParent();
void processAutoOpen();
QSharedPointer<Database> m_db; QSharedPointer<Database> m_db;