Automatic reload of a database when it get modified

When the database if modified by an other instance of KeePassX, KeePassX
detect it and reload automatically the database.
This commit is contained in:
Sebastien Fricker 2014-05-03 16:59:41 +02:00
parent e263c475c9
commit 7a8d4577f1
7 changed files with 162 additions and 0 deletions

View File

@ -29,6 +29,7 @@ set(keepassx_SOURCES
autotype/test/AutoTypeTestInterface.h
core/AutoTypeAssociations.cpp
core/Config.cpp
core/FileSystemWatcher.cpp
core/Database.cpp
core/DatabaseIcons.cpp
core/Endian.cpp
@ -136,6 +137,7 @@ set(keepassx_MOC
autotype/AutoTypeSelectView.h
autotype/ShortcutWidget.h
autotype/WindowSelectComboBox.h
core/FileSystemWatcher.h
core/AutoTypeAssociations.h
core/Config.h
core/Database.h

View File

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

View File

@ -87,6 +87,7 @@ public:
QByteArray transformSeed() const;
quint64 transformRounds() const;
QByteArray transformedMasterKey() const;
const CompositeKey & key() const;
void setCipher(const Uuid& cipher);
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

@ -22,9 +22,11 @@
#include <QDesktopServices>
#include <QHBoxLayout>
#include <QLabel>
#include <QFile>
#include <QLineEdit>
#include <QSplitter>
#include <QTimer>
#include <QtDebug>
#include "autotype/AutoType.h"
#include "core/Config.h"
@ -32,6 +34,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"
@ -159,6 +162,7 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
connect(m_searchUi->searchEdit, SIGNAL(returnPressed()), m_entryView, SLOT(setFocus()));
connect(m_searchTimer, SIGNAL(timeout()), this, SLOT(search()));
connect(closeAction, SIGNAL(triggered()), this, SLOT(closeSearch()));
connect( &m_file_watcher, SIGNAL( fileChanged() ), this, SLOT( databaseModifedExternally() ) );
setCurrentWidget(m_mainWidget);
}
@ -580,8 +584,10 @@ void DatabaseWidget::openDatabase(bool accepted)
m_databaseOpenWidget = Q_NULLPTR;
delete m_keepass1OpenWidget;
m_keepass1OpenWidget = Q_NULLPTR;
m_file_watcher.watchFile( m_filename );
}
else {
m_file_watcher.stopWatching();
if (m_databaseOpenWidget->database()) {
delete m_databaseOpenWidget->database();
}
@ -811,3 +817,25 @@ void DatabaseWidget::updateFilename(const QString& fileName)
{
m_filename = fileName;
}
void DatabaseWidget::databaseModifedExternally()
{
if ( database() == Q_NULLPTR )
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;
}
}

View File

@ -24,6 +24,7 @@
#include "core/Global.h"
#include "gui/entry/EntryModel.h"
#include "core/FileSystemWatcher.h"
class ChangeMasterKeyWidget;
class DatabaseOpenWidget;
@ -118,6 +119,7 @@ private Q_SLOTS:
void switchToGroupEdit(Group* entry, bool create);
void updateMasterKey(bool accepted);
void openDatabase(bool accepted);
void databaseModifedExternally();
void unlockDatabase(bool accepted);
void emitCurrentModeChanged();
void clearLastGroup(Group* group);
@ -152,6 +154,7 @@ private:
QTimer* m_searchTimer;
QWidget* widgetBeforeLock;
QString m_filename;
FileSystemWatcher m_file_watcher;
};
#endif // KEEPASSX_DATABASEWIDGET_H