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

@ -40,6 +40,7 @@ set(keepassx_SOURCES
autotype/test/AutoTypeTestInterface.h autotype/test/AutoTypeTestInterface.h
core/AutoTypeAssociations.cpp core/AutoTypeAssociations.cpp
core/Config.cpp core/Config.cpp
core/FileSystemWatcher.cpp
core/Database.cpp core/Database.cpp
core/DatabaseIcons.cpp core/DatabaseIcons.cpp
core/Endian.cpp core/Endian.cpp

View File

@ -92,6 +92,7 @@ void Config::init(const QString& fileName)
m_defaults.insert("RememberLastKeyFiles", true); m_defaults.insert("RememberLastKeyFiles", true);
m_defaults.insert("OpenPreviousDatabasesOnStartup", true); m_defaults.insert("OpenPreviousDatabasesOnStartup", true);
m_defaults.insert("AutoSaveAfterEveryChange", false); m_defaults.insert("AutoSaveAfterEveryChange", false);
m_defaults.insert("AutoReloadOnChange", true);
m_defaults.insert("AutoSaveOnExit", false); m_defaults.insert("AutoSaveOnExit", false);
m_defaults.insert("ShowToolbar", true); m_defaults.insert("ShowToolbar", true);
m_defaults.insert("MinimizeOnCopy", false); m_defaults.insert("MinimizeOnCopy", false);

View File

@ -324,3 +324,9 @@ void Database::startModifiedTimer()
} }
m_timer->start(150); m_timer->start(150);
} }
const CompositeKey & Database::key() const
{
return m_data.key;
}

View File

@ -88,6 +88,7 @@ public:
QByteArray transformSeed() const; QByteArray transformSeed() const;
quint64 transformRounds() const; quint64 transformRounds() const;
QByteArray transformedMasterKey() const; QByteArray transformedMasterKey() const;
const CompositeKey & key() const;
void setCipher(const Uuid& cipher); void setCipher(const Uuid& cipher);
void setCompressionAlgo(Database::CompressionAlgorithm algo); void setCompressionAlgo(Database::CompressionAlgorithm algo);

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FileSystemWatcher.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>
FileSystemWatcher::FileSystemWatcher (): QFileSystemWatcher ( )
{
connect(this,SIGNAL( fileChanged ( const QString & )),
this,SLOT( fileChangedSlot ( const QString & )));
connect(this,SIGNAL( directoryChanged ( const QString & )),
this,SLOT( directoryChangedSlot ( const QString & )));
}
void FileSystemWatcher::watchFile(const QString &file)
{
_file=file;
if (!files().isEmpty())
removePaths(files());
if (!directories().isEmpty())
removePaths(directories());
QFileInfo fileInfo(file);
if (fileInfo.exists())
addPath(file);
QString filePath=fileInfo.absoluteDir().path();
QFileInfo filePathInfo(filePath);
if (filePathInfo.exists())
addPath(filePath);
}
void FileSystemWatcher::stopWatching()
{
watchFile( QString() );
}
void FileSystemWatcher::fileChangedSlot ( const QString & )
{
QFileInfo fileInfo(_file);
if ( fileInfo.exists() )
fileChanged();
}
void FileSystemWatcher::directoryChangedSlot ( const QString & )
{
if (!files().contains(_file))
{
QFileInfo fileInfo(_file);
if ( fileInfo.exists() )
{
addPath(_file);
fileChanged();
}
}
}
FileSystemWatcher::~FileSystemWatcher()
{
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILE_SYSTEM_WATCHER_H
#define FILE_SYSTEM_WATCHER_H
#include <QFileSystemWatcher>
#include <QString>
#include <QObject>
class FileSystemWatcher : public QFileSystemWatcher
{
Q_OBJECT ;
public:
FileSystemWatcher ();
void watchFile( const QString & );
void stopWatching();
virtual ~FileSystemWatcher();
private:
QString _file;
private Q_SLOTS:
void directoryChangedSlot ( const QString & );
void fileChangedSlot ( const QString & );
Q_SIGNALS:
void fileChanged();
};
#endif

View File

@ -21,6 +21,7 @@
#include <QDesktopServices> #include <QDesktopServices>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QFile>
#include <QLineEdit> #include <QLineEdit>
#include <QKeyEvent> #include <QKeyEvent>
#include <QSplitter> #include <QSplitter>
@ -28,6 +29,8 @@
#include <QProcess> #include <QProcess>
#include <QHeaderView> #include <QHeaderView>
#include <QApplication> #include <QApplication>
#include <QTimer>
#include <QtDebug>
#include "autotype/AutoType.h" #include "autotype/AutoType.h"
#include "core/Config.h" #include "core/Config.h"
@ -36,6 +39,7 @@
#include "core/Group.h" #include "core/Group.h"
#include "core/Metadata.h" #include "core/Metadata.h"
#include "core/Tools.h" #include "core/Tools.h"
#include "format/KeePass2Reader.h"
#include "gui/ChangeMasterKeyWidget.h" #include "gui/ChangeMasterKeyWidget.h"
#include "gui/Clipboard.h" #include "gui/Clipboard.h"
#include "gui/DatabaseOpenWidget.h" #include "gui/DatabaseOpenWidget.h"
@ -658,8 +662,11 @@ void DatabaseWidget::openDatabase(bool accepted)
m_databaseOpenWidget = nullptr; m_databaseOpenWidget = nullptr;
delete m_keepass1OpenWidget; delete m_keepass1OpenWidget;
m_keepass1OpenWidget = nullptr; m_keepass1OpenWidget = nullptr;
if (config()->get("AutoReloadOnChange").toBool() )
m_file_watcher.watchFile( m_filename );
} }
else { else {
m_file_watcher.stopWatching();
if (m_databaseOpenWidget->database()) { if (m_databaseOpenWidget->database()) {
delete m_databaseOpenWidget->database(); delete m_databaseOpenWidget->database();
} }
@ -932,6 +939,31 @@ void DatabaseWidget::updateFilename(const QString& fileName)
m_filename = 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 int DatabaseWidget::numberOfSelectedEntries() const
{ {
return m_entryView->numberOfSelectedEntries(); return m_entryView->numberOfSelectedEntries();

View File

@ -24,6 +24,7 @@
#include "core/Uuid.h" #include "core/Uuid.h"
#include "gui/entry/EntryModel.h" #include "gui/entry/EntryModel.h"
#include "core/FileSystemWatcher.h"
class ChangeMasterKeyWidget; class ChangeMasterKeyWidget;
class DatabaseOpenWidget; class DatabaseOpenWidget;
@ -148,6 +149,7 @@ private Q_SLOTS:
void updateMasterKey(bool accepted); void updateMasterKey(bool accepted);
void openDatabase(bool accepted); void openDatabase(bool accepted);
void mergeDatabase(bool accepted); void mergeDatabase(bool accepted);
void databaseModifedExternally();
void unlockDatabase(bool accepted); void unlockDatabase(bool accepted);
void emitCurrentModeChanged(); void emitCurrentModeChanged();
void clearLastGroup(Group* group); void clearLastGroup(Group* group);
@ -183,6 +185,8 @@ private:
QString m_lastSearchText; QString m_lastSearchText;
bool m_searchCaseSensitive; bool m_searchCaseSensitive;
bool m_searchCurrentGroup; bool m_searchCurrentGroup;
FileSystemWatcher m_file_watcher;
}; };
#endif // KEEPASSX_DATABASEWIDGET_H #endif // KEEPASSX_DATABASEWIDGET_H

View File

@ -106,6 +106,7 @@ void SettingsWidget::loadSettings()
config()->get("OpenPreviousDatabasesOnStartup").toBool()); config()->get("OpenPreviousDatabasesOnStartup").toBool());
m_generalUi->autoSaveAfterEveryChangeCheckBox->setChecked(config()->get("AutoSaveAfterEveryChange").toBool()); m_generalUi->autoSaveAfterEveryChangeCheckBox->setChecked(config()->get("AutoSaveAfterEveryChange").toBool());
m_generalUi->autoSaveOnExitCheckBox->setChecked(config()->get("AutoSaveOnExit").toBool()); m_generalUi->autoSaveOnExitCheckBox->setChecked(config()->get("AutoSaveOnExit").toBool());
m_generalUi->autoReloadOnChangeCheckBox->setChecked(config()->get("AutoReloadOnChange").toBool());
m_generalUi->minimizeOnCopyCheckBox->setChecked(config()->get("MinimizeOnCopy").toBool()); m_generalUi->minimizeOnCopyCheckBox->setChecked(config()->get("MinimizeOnCopy").toBool());
m_generalUi->useGroupIconOnEntryCreationCheckBox->setChecked(config()->get("UseGroupIconOnEntryCreation").toBool()); m_generalUi->useGroupIconOnEntryCreationCheckBox->setChecked(config()->get("UseGroupIconOnEntryCreation").toBool());
m_generalUi->autoTypeEntryTitleMatchCheckBox->setChecked(config()->get("AutoTypeEntryTitleMatch").toBool()); m_generalUi->autoTypeEntryTitleMatchCheckBox->setChecked(config()->get("AutoTypeEntryTitleMatch").toBool());
@ -159,6 +160,7 @@ void SettingsWidget::saveSettings()
config()->set("AutoSaveAfterEveryChange", config()->set("AutoSaveAfterEveryChange",
m_generalUi->autoSaveAfterEveryChangeCheckBox->isChecked()); m_generalUi->autoSaveAfterEveryChangeCheckBox->isChecked());
config()->set("AutoSaveOnExit", m_generalUi->autoSaveOnExitCheckBox->isChecked()); config()->set("AutoSaveOnExit", m_generalUi->autoSaveOnExitCheckBox->isChecked());
config()->set("AutoReloadOnChange", m_generalUi->autoReloadOnChangeCheckBox->isChecked());
config()->set("MinimizeOnCopy", m_generalUi->minimizeOnCopyCheckBox->isChecked()); config()->set("MinimizeOnCopy", m_generalUi->minimizeOnCopyCheckBox->isChecked());
config()->set("UseGroupIconOnEntryCreation", config()->set("UseGroupIconOnEntryCreation",
m_generalUi->useGroupIconOnEntryCreationCheckBox->isChecked()); m_generalUi->useGroupIconOnEntryCreationCheckBox->isChecked());

View File

@ -56,44 +56,51 @@
</widget> </widget>
</item> </item>
<item row="5" column="0"> <item row="5" column="0">
<widget class="QCheckBox" name="autoReloadOnChangeCheckBox">
<property name="text">
<string>Automatically reload when the database is expernally modified</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QCheckBox" name="minimizeOnCopyCheckBox"> <widget class="QCheckBox" name="minimizeOnCopyCheckBox">
<property name="text"> <property name="text">
<string>Minimize when copying to clipboard</string> <string>Minimize when copying to clipboard</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="6" column="0"> <item row="7" column="0">
<widget class="QCheckBox" name="useGroupIconOnEntryCreationCheckBox"> <widget class="QCheckBox" name="useGroupIconOnEntryCreationCheckBox">
<property name="text"> <property name="text">
<string>Use group icon on entry creation</string> <string>Use group icon on entry creation</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="0"> <item row="8" column="0">
<widget class="QLabel" name="autoTypeShortcutLabel"> <widget class="QLabel" name="autoTypeShortcutLabel">
<property name="text"> <property name="text">
<string>Global Auto-Type shortcut</string> <string>Global Auto-Type shortcut</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="1"> <item row="8" column="1">
<widget class="ShortcutWidget" name="autoTypeShortcutWidget"/> <widget class="ShortcutWidget" name="autoTypeShortcutWidget"/>
</item> </item>
<item row="8" column="0"> <item row="9" column="0">
<widget class="QCheckBox" name="autoTypeEntryTitleMatchCheckBox"> <widget class="QCheckBox" name="autoTypeEntryTitleMatchCheckBox">
<property name="text"> <property name="text">
<string>Use entry title to match windows for global auto-type</string> <string>Use entry title to match windows for global auto-type</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="9" column="0"> <item row="10" column="0">
<widget class="QLabel" name="languageLabel"> <widget class="QLabel" name="languageLabel">
<property name="text"> <property name="text">
<string>Language</string> <string>Language</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="9" column="1"> <item row="10" column="1">
<widget class="QComboBox" name="languageComboBox"/> <widget class="QComboBox" name="languageComboBox"/>
</item> </item>
<item row="10" column="0"> <item row="10" column="0">