Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement AutoOpen functionality after refactor #2504

Merged
merged 2 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/gui/DatabaseTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<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()));
Expand Down
4 changes: 2 additions & 2 deletions src/gui/DatabaseTabWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class DatabaseTabWidget : public QTabWidget
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();
Expand Down
48 changes: 48 additions & 0 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -954,6 +955,7 @@ void DatabaseWidget::switchToOpenDatabase(const QString& filePath)
setCurrentWidget(m_unlockDatabaseWidget);
}
}

void DatabaseWidget::switchToCsvImport(const QString& filePath)
{
setCurrentWidget(m_csvImportWizard);
Expand All @@ -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()) {
Expand Down Expand Up @@ -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());
}
}
3 changes: 3 additions & 0 deletions src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class DatabaseWidget : public QStackedWidget
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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -226,6 +228,7 @@ private slots:
int addChildWidget(QWidget* w);
void setClipboardTextAndMinimize(const QString& text);
void setIconFromParent();
void processAutoOpen();

QSharedPointer<Database> m_db;

Expand Down