Cleaned up code and added spots for todo work. Fully Working!

This commit is contained in:
Jonathan White 2016-11-09 07:38:14 -05:00
parent 06b1baa454
commit 37aedc8b03
7 changed files with 62 additions and 155 deletions

View File

@ -40,7 +40,6 @@ 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

@ -1,75 +0,0 @@
/*
* 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

@ -1,47 +0,0 @@
/*
* 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

@ -29,7 +29,6 @@
#include <QProcess> #include <QProcess>
#include <QHeaderView> #include <QHeaderView>
#include <QApplication> #include <QApplication>
#include <QTimer>
#include <QtDebug> #include <QtDebug>
#include "autotype/AutoType.h" #include "autotype/AutoType.h"
@ -157,8 +156,12 @@ DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent)
connect(m_databaseOpenMergeWidget, SIGNAL(editFinished(bool)), SLOT(mergeDatabase(bool))); connect(m_databaseOpenMergeWidget, SIGNAL(editFinished(bool)), SLOT(mergeDatabase(bool)));
connect(m_keepass1OpenWidget, SIGNAL(editFinished(bool)), SLOT(openDatabase(bool))); connect(m_keepass1OpenWidget, SIGNAL(editFinished(bool)), SLOT(openDatabase(bool)));
connect(m_unlockDatabaseWidget, SIGNAL(editFinished(bool)), SLOT(unlockDatabase(bool))); connect(m_unlockDatabaseWidget, SIGNAL(editFinished(bool)), SLOT(unlockDatabase(bool)));
connect(&m_fileWatcher, SIGNAL(fileChanged(QString)), this, SLOT(onWatchedFileChanged()));
connect(&m_fileWatchTimer, SIGNAL(timeout()), this, SLOT(reloadDatabaseFile()));
connect(this, SIGNAL(currentChanged(int)), this, SLOT(emitCurrentModeChanged())); connect(this, SIGNAL(currentChanged(int)), this, SLOT(emitCurrentModeChanged()));
m_fileWatchTimer.setSingleShot(true);
m_searchCaseSensitive = false; m_searchCaseSensitive = false;
m_searchCurrentGroup = false; m_searchCurrentGroup = false;
@ -662,11 +665,10 @@ 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_fileWatcher.addPath(m_filename);
m_file_watcher.watchFile( m_filename );
} }
else { else {
m_file_watcher.stopWatching(); m_fileWatcher.removePath(m_filename);
if (m_databaseOpenWidget->database()) { if (m_databaseOpenWidget->database()) {
delete m_databaseOpenWidget->database(); delete m_databaseOpenWidget->database();
} }
@ -939,29 +941,52 @@ void DatabaseWidget::updateFilename(const QString& fileName)
m_filename = fileName; m_filename = fileName;
} }
void DatabaseWidget::databaseModifedExternally() void DatabaseWidget::onWatchedFileChanged()
{ {
if ( database() == Q_NULLPTR ) if (m_fileWatchTimer.isActive())
return; return;
if ( ! config()->get("AutoReloadOnChange").toBool() ) m_fileWatchTimer.start(500);
}
void DatabaseWidget::reloadDatabaseFile()
{
if (m_db == nullptr)
return; return;
// TODO: Also check if db is currently modified before reloading
if (! config()->get("AutoReloadOnChange").toBool()) {
// Ask if we want to reload the db
QMessageBox::StandardButton mb = MessageBox::question(this, tr("Reload database file"),
tr("The database file has changed. Do you want to load the changes?"),
QMessageBox::Yes | QMessageBox::No);
if (mb == QMessageBox::No) {
// TODO: taint database
// Rewatch the database file
m_fileWatcher.addPath(m_filename);
return;
}
}
KeePass2Reader reader; KeePass2Reader reader;
QFile file(m_filename); QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) { if (file.open(QIODevice::ReadOnly)) {
// TODO: error message
return;
}
Database* db = reader.readDatabase(&file, database()->key()); Database* db = reader.readDatabase(&file, database()->key());
if ( db ) if (db != nullptr) {
{ replaceDatabase(db);
Database* oldDb = m_db;
m_db = db;
m_groupView->changeDatabase(m_db);
Q_EMIT databaseChanged(m_db);
delete oldDb;
} }
else {
// TODO: error message for failure to read the new db
}
}
else {
// TODO: error message for failure to open db file
}
// Rewatch the database file
m_fileWatcher.addPath(m_filename);
} }
int DatabaseWidget::numberOfSelectedEntries() const int DatabaseWidget::numberOfSelectedEntries() const

View File

@ -20,11 +20,12 @@
#include <QScopedPointer> #include <QScopedPointer>
#include <QStackedWidget> #include <QStackedWidget>
#include <QFileSystemWatcher>
#include <QTimer>
#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;
@ -42,6 +43,7 @@ class QMenu;
class QSplitter; class QSplitter;
class QLabel; class QLabel;
class UnlockDatabaseWidget; class UnlockDatabaseWidget;
class QFileSystemWatcher;
class DatabaseWidget : public QStackedWidget class DatabaseWidget : public QStackedWidget
{ {
@ -149,10 +151,12 @@ 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);
// Database autoreload slots
void onWatchedFileChanged();
void reloadDatabaseFile();
private: private:
void setClipboardTextAndMinimize(const QString& text); void setClipboardTextAndMinimize(const QString& text);
@ -186,7 +190,8 @@ private:
bool m_searchCaseSensitive; bool m_searchCaseSensitive;
bool m_searchCurrentGroup; bool m_searchCurrentGroup;
FileSystemWatcher m_file_watcher; QFileSystemWatcher m_fileWatcher;
QTimer m_fileWatchTimer;
}; };
#endif // KEEPASSX_DATABASEWIDGET_H #endif // KEEPASSX_DATABASEWIDGET_H

View File

@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>684</width> <width>684</width>
<height>394</height> <height>430</height>
</rect> </rect>
</property> </property>
<layout class="QFormLayout" name="formLayout"> <layout class="QFormLayout" name="formLayout">
@ -103,14 +103,7 @@
<item row="10" 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="12" column="0">
<widget class="QCheckBox" name="systrayShowCheckBox">
<property name="text">
<string>Show a system tray icon</string>
</property>
</widget>
</item>
<item row="11" column="0">
<layout class="QHBoxLayout" name="systray1Layout"> <layout class="QHBoxLayout" name="systray1Layout">
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum> <enum>QLayout::SetMaximumSize</enum>
@ -149,7 +142,7 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="12" column="0"> <item row="13" column="0">
<layout class="QHBoxLayout" name="systray2Layout"> <layout class="QHBoxLayout" name="systray2Layout">
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum> <enum>QLayout::SetMaximumSize</enum>
@ -179,7 +172,7 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="13" column="0"> <item row="14" column="0">
<layout class="QHBoxLayout" name="systray3Layout"> <layout class="QHBoxLayout" name="systray3Layout">
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum> <enum>QLayout::SetMaximumSize</enum>
@ -209,6 +202,13 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="11" column="0">
<widget class="QCheckBox" name="systrayShowCheckBox">
<property name="text">
<string>Show a system tray icon</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>

Binary file not shown.