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.
*
* @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) {
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<Database>::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()));

View File

@ -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();

View File

@ -397,6 +397,7 @@ void DatabaseWidget::replaceDatabase(QSharedPointer<Database> 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());
}
}

View File

@ -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<Database> 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<Database> m_db;