Automatic reload the database when the file is externally modified

This commit is contained in:
Jonathan White 2016-11-07 22:52:32 -05:00
commit 06b1baa454
10 changed files with 182 additions and 6 deletions

View file

@ -21,6 +21,7 @@
#include <QDesktopServices>
#include <QHBoxLayout>
#include <QLabel>
#include <QFile>
#include <QLineEdit>
#include <QKeyEvent>
#include <QSplitter>
@ -28,6 +29,8 @@
#include <QProcess>
#include <QHeaderView>
#include <QApplication>
#include <QTimer>
#include <QtDebug>
#include "autotype/AutoType.h"
#include "core/Config.h"
@ -36,6 +39,7 @@
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "format/KeePass2Reader.h"
#include "gui/ChangeMasterKeyWidget.h"
#include "gui/Clipboard.h"
#include "gui/DatabaseOpenWidget.h"
@ -658,8 +662,11 @@ void DatabaseWidget::openDatabase(bool accepted)
m_databaseOpenWidget = nullptr;
delete m_keepass1OpenWidget;
m_keepass1OpenWidget = nullptr;
if (config()->get("AutoReloadOnChange").toBool() )
m_file_watcher.watchFile( m_filename );
}
else {
m_file_watcher.stopWatching();
if (m_databaseOpenWidget->database()) {
delete m_databaseOpenWidget->database();
}
@ -932,6 +939,31 @@ void DatabaseWidget::updateFilename(const QString& fileName)
m_filename = fileName;
}
void DatabaseWidget::databaseModifedExternally()
{
if ( database() == Q_NULLPTR )
return;
if ( ! config()->get("AutoReloadOnChange").toBool() )
return;
KeePass2Reader reader;
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
// TODO: error message
return;
}
Database* db = reader.readDatabase(&file, database()->key() );
if ( db )
{
Database* oldDb = m_db;
m_db = db;
m_groupView->changeDatabase(m_db);
Q_EMIT databaseChanged(m_db);
delete oldDb;
}
}
int DatabaseWidget::numberOfSelectedEntries() const
{
return m_entryView->numberOfSelectedEntries();