Skip to content

Commit

Permalink
Merge pull request #287 from asashnov/fix-backup-monitor-feature
Browse files Browse the repository at this point in the history
fix appearing of monitored backup save requests
  • Loading branch information
limpkin authored Jun 8, 2018
2 parents 10b424d + c78e092 commit eb7b460
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 61 deletions.
6 changes: 4 additions & 2 deletions gui.pro
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ SOURCES += src/main_gui.cpp \
src/PromptWidget.cpp \
src/DbExportsRegistry.cpp \
src/DbExportsRegistryController.cpp \
src/DbBackupChangeNumbersComparator.cpp
src/DbBackupChangeNumbersComparator.cpp \
src/DbMasterController.cpp

HEADERS += src/MainWindow.h \
src/Common.h \
Expand Down Expand Up @@ -85,7 +86,8 @@ HEADERS += src/MainWindow.h \
src/PromptWidget.h \
src/DbExportsRegistry.h \
src/DbExportsRegistryController.h \
src/DbBackupChangeNumbersComparator.h
src/DbBackupChangeNumbersComparator.h \
src/DbMasterController.h

mac {
HEADERS += src/MacUtils.h
Expand Down
24 changes: 9 additions & 15 deletions src/AppGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "AppGui.h"
#include "version.h"
#include "QSimpleUpdater.h"
#include "DbExportsRegistryController.h"
#include "DbMasterController.h"

#ifdef Q_OS_MAC
#include "MacUtils.h"
Expand All @@ -41,7 +41,7 @@
AppGui::AppGui(int & argc, char ** argv) :
QApplication(argc, argv),
sharedMem("moolticute"),
dbExportsRegistryController(nullptr)
dbMasterController(nullptr)
{
}

Expand Down Expand Up @@ -155,6 +155,9 @@ bool AppGui::initialize()

connectedChanged();

dbMasterController = new DbMasterController(this);
dbMasterController->setWSClient(wsClient);

if (!autoLaunched)
mainWindowShow();

Expand Down Expand Up @@ -226,19 +229,9 @@ bool AppGui::initialize()
createMainWindow();
});

initializeDbExportsRegitry();

return true;
}

void AppGui::initializeDbExportsRegitry()
{
QString regitryFile = getDataDirPath() + "/dbExportsRegistry.ini";
dbExportsRegistryController = new DbExportsRegistryController(regitryFile, this);
dbExportsRegistryController->setMainWindow(win);
dbExportsRegistryController->setWSClient(wsClient);
}

void AppGui::startSSHAgent()
{
//Start ssh agent if needed
Expand Down Expand Up @@ -639,7 +632,9 @@ void AppGui::createMainWindow()

qtAwesome()->initFontAwesome();

win = new MainWindow(wsClient);
Q_ASSERT(dbMasterController);

win = new MainWindow(wsClient, dbMasterController);
connect(win, &MainWindow::destroyed, [this](QObject *)
{
win = nullptr;
Expand All @@ -649,6 +644,5 @@ void AppGui::createMainWindow()
mainWindowHide();
});

if (dbExportsRegistryController)
dbExportsRegistryController->setMainWindow(win);
dbMasterController->setMainWindow(win);
}
5 changes: 2 additions & 3 deletions src/AppGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "DaemonMenuAction.h"
#include <QtAwesome.h>

class DbExportsRegistryController;
class DbMasterController;
class AppGui : public QApplication
{
Q_OBJECT
Expand Down Expand Up @@ -100,8 +100,7 @@ private slots:
void startSSHAgent();
void createMainWindow();

DbExportsRegistryController *dbExportsRegistryController;
void initializeDbExportsRegitry();
DbMasterController *dbMasterController;
};

#endif // APPGUI_H
1 change: 1 addition & 0 deletions src/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Common::MPStatus Common::statusFromString(const QString &st)
return it.key();
}

qWarning() << "Unable to find value from enum for status" << st;
return Common::UnknownStatus;
}

Expand Down
9 changes: 0 additions & 9 deletions src/DbBackupsTrackerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ DbBackupsTrackerController::DbBackupsTrackerController(MainWindow *window, WSCli

connect(wsClient, &WSClient::fwVersionChanged,
this, &DbBackupsTrackerController::handleFirmwareVersionChange);
connect(wsClient, &WSClient::cardDbMetadataChanged,
this, &DbBackupsTrackerController::handleCardDbMetadataChanged);
connect(wsClient, &WSClient::statusChanged,
this, &DbBackupsTrackerController::handleDeviceStatusChanged);
connect(wsClient, &WSClient::connectedChanged,
this, &DbBackupsTrackerController::handleDeviceConnectedChanged);

handleFirmwareVersionChange(wsClient->get_fwVersion());
handleDeviceStatusChanged(wsClient->get_status());
Expand Down Expand Up @@ -166,9 +160,6 @@ void DbBackupsTrackerController::askForExportBackup()

void DbBackupsTrackerController::exportDbBackup()
{
connect(wsClient, &WSClient::dbExported, this,
&DbBackupsTrackerController::handleExportDbResult);

QString format;
try
{
Expand Down
11 changes: 6 additions & 5 deletions src/DbBackupsTrackerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ class DbBackupsTrackerController : public QObject

QString getBackupFilePath();

// they are not slots anymore. Will be called by dbMasterController.
void handleCardDbMetadataChanged(QString cardId, int credentialsDbChangeNumber, int dataDbChangeNumber);
void handleExportDbResult(const QByteArray &d, bool success);
void handleDeviceStatusChanged(const Common::MPStatus &status);
void handleDeviceConnectedChanged(const bool &connected);

signals:
void backupFilePathChanged(const QString &path);

public slots:
void setBackupFilePath(const QString &path);

protected slots:
void handleCardDbMetadataChanged(QString cardId, int credentialsDbChangeNumber, int dataDbChangeNumber);
void handleGreaterDbBackupChangeNumber();
void handleLowerDbBackupChangeNumber();
void handleExportDbResult(const QByteArray &d, bool success);
void handleNewTrack(const QString &cardId, const QString &path);
void handleDeviceStatusChanged(const Common::MPStatus &status);
void handleDeviceConnectedChanged(const bool &connected);
void handleFirmwareVersionChange(const QString &version);


private:
DbBackupsTracker dbBackupsTracker;
MainWindow *window;
Expand Down
25 changes: 17 additions & 8 deletions src/DbExportsRegistryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

DbExportsRegistryController::DbExportsRegistryController(const QString &settingsFilePath, QObject *parent) :
QObject(parent), isExportRequestMessageVisible(false), window(nullptr)
, wasDbExportRecommendedWithoutMainWindow(false)
{
dbExportsRegistry = new DbExportsRegistry(settingsFilePath, this);
connect(dbExportsRegistry, &DbExportsRegistry::dbExportRecommended, this, &DbExportsRegistryController::handleDbExportRecommended);
Expand All @@ -21,18 +22,24 @@ void DbExportsRegistryController::setMainWindow(MainWindow *window)
{
DbExportsRegistryController::window = window;
if (wasDbExportRecommendedWithoutMainWindow)
handleDbExportRecommended();
{
/* with main window there was DBBackupTrackerController also created.
* In its constructor it used similar QTimer::singleShot call
* of handleCardDbMetadataChanged(), that will
* hide the prompt which handleDbExportRecommended() will show.
* So to avoid it, we call handleDbExportRecommended() also deffered
* to be executed after DbBackupsTrackerController::handleCardDbMetadataChanged()
* (this workaround is only needed when run with --autolaunched option).
*/
QTimer::singleShot(500, this, SLOT(handleDbExportRecommended()));
}

wasDbExportRecommendedWithoutMainWindow = false;
}

void DbExportsRegistryController::setWSClient(WSClient *wsClient)
{
DbExportsRegistryController::wsClient = wsClient;
connect(wsClient, &WSClient::cardDbMetadataChanged, this, &DbExportsRegistryController::handleCardIdChanged);
connect(wsClient, &WSClient::statusChanged, this, &DbExportsRegistryController::handleDeviceStatusChanged);
connect(wsClient, &WSClient::connectedChanged, this, &DbExportsRegistryController::handleDeviceConnectedChanged);
connect(wsClient, &WSClient::dbExported, this, &DbExportsRegistryController::registerDbExported);
dbExportsRegistry->setCurrentCardDbMetadata(wsClient->get_cardId(), wsClient->get_credentialsDbChangeNumber(), wsClient->get_dataDbChangeNumber());
}

Expand Down Expand Up @@ -100,7 +107,10 @@ void DbExportsRegistryController::handleDbExportRecommended()

void DbExportsRegistryController::handleExportDbResult(const QByteArray &d, bool success)
{
disconnect(wsClient, &WSClient::dbExported, this, &DbExportsRegistryController::handleExportDbResult);
if (! handleExportResultEnabled)
return;
handleExportResultEnabled = false;

if (window)
window->handleBackupExported();

Expand Down Expand Up @@ -138,8 +148,7 @@ void DbExportsRegistryController::writeDbToFile(const QByteArray &d, QString fna

void DbExportsRegistryController::exportDbBackup()
{
connect(wsClient, &WSClient::dbExported, this,
&DbExportsRegistryController::handleExportDbResult);
handleExportResultEnabled = true;

QString format = "SympleCrypt";

Expand Down
15 changes: 8 additions & 7 deletions src/DbExportsRegistryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ class DbExportsRegistryController : public QObject
MainWindow *window;
WSClient *wsClient;
bool wasDbExportRecommendedWithoutMainWindow;
bool handleExportResultEnabled = false;
public:
explicit DbExportsRegistryController(const QString &settingsFilePath, QObject *parent = nullptr);
virtual ~DbExportsRegistryController();

void setMainWindow(MainWindow *window);
void setWSClient(WSClient *wsClient);
void writeDbToFile(const QByteArray &d, QString fname);

signals:

public slots:
// they are not slots anymore to prevent any attempts of connection to wsClient.
// they will be called by DbMasterController when no monitored backup is active.
void handleCardIdChanged(QString cardId, int credentialsDbChangeNumber, int dataDbChangeNumber);
void handleDeviceStatusChanged(const Common::MPStatus &status);
void handleDeviceConnectedChanged(const bool &);
void registerDbExported(const QByteArray &, bool success);
void handleExportDbResult(const QByteArray &d, bool success);

protected slots:
void handleDbExportRecommended();
void handleExportDbResult(const QByteArray &d, bool success);
void handleDeviceStatusChanged(const Common::MPStatus &status);
void handleDeviceConnectedChanged(const bool &);

private:
void writeDbToFile(const QByteArray &d, QString fname);
void exportDbBackup();
void hidePrompt();
};
Expand Down
99 changes: 99 additions & 0 deletions src/DbMasterController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "DbMasterController.h"
#include "AppGui.h"

#include "DbBackupsTrackerController.h"
#include "DbExportsRegistryController.h"


DbMasterController::DbMasterController(QObject *parent)
: QObject(parent)
{
QString regitryFile = AppGui::getDataDirPath() + "/dbExportsRegistry.ini";
dbExportsRegistryController = new DbExportsRegistryController(regitryFile, this);
}

void DbMasterController::setMainWindow(MainWindow *window)
{
dbExportsRegistryController->setMainWindow(window);

if (! dbBackupsTrackerController && window && wsClient)
{
dbBackupsTrackerController = new DbBackupsTrackerController(window, wsClient,
AppGui::getDataDirPath() + "/dbBackupTracks.ini", this);
connect(dbBackupsTrackerController, SIGNAL(backupFilePathChanged(QString)),
this, SIGNAL(backupFilePathChanged(QString)));
}
}

void DbMasterController::setWSClient(WSClient *client)
{
Q_ASSERT(client);

wsClient = client;

connect(wsClient, &WSClient::cardDbMetadataChanged, this, &DbMasterController::handleCardIdChanged);
connect(wsClient, &WSClient::statusChanged, this, &DbMasterController::handleDeviceStatusChanged);
connect(wsClient, &WSClient::connectedChanged, this, &DbMasterController::handleDeviceConnectedChanged);
connect(wsClient, &WSClient::dbExported, this, &DbMasterController::registerDbExported);

dbExportsRegistryController->setWSClient(wsClient);
}

void DbMasterController::setBackupFilePath(const QString &path)
{
Q_ASSERT(dbBackupsTrackerController);
dbBackupsTrackerController->setBackupFilePath(path);
}

QString DbMasterController::getBackupFilePath()
{
if (! dbBackupsTrackerController)
return "";

return dbBackupsTrackerController->getBackupFilePath();
}

void DbMasterController::handleCardIdChanged(QString cardId, int credentialsDbChangeNumber, int dataDbChangeNumber)
{
if (dbBackupsTrackerController) {
dbBackupsTrackerController->handleCardDbMetadataChanged(cardId, credentialsDbChangeNumber, dataDbChangeNumber);
if (! dbBackupsTrackerController->getBackupFilePath().isEmpty())
return; // monitored backup is active, no need of 'export registry' functionality
}

dbExportsRegistryController->handleCardIdChanged(cardId, credentialsDbChangeNumber, dataDbChangeNumber);
}

void DbMasterController::handleDeviceStatusChanged(const Common::MPStatus &status)
{
if (dbBackupsTrackerController) {
dbBackupsTrackerController->handleDeviceStatusChanged(status);
if (! dbBackupsTrackerController->getBackupFilePath().isEmpty())
return;
}

dbExportsRegistryController->handleDeviceStatusChanged(status);
}

void DbMasterController::handleDeviceConnectedChanged(const bool &connected)
{
if (dbBackupsTrackerController) {
dbBackupsTrackerController->handleDeviceConnectedChanged(connected);
if (! dbBackupsTrackerController->getBackupFilePath().isEmpty())
return;
}

dbExportsRegistryController->handleDeviceConnectedChanged(connected);
}

void DbMasterController::registerDbExported(const QByteArray &data, bool success)
{
if (dbBackupsTrackerController) {
dbBackupsTrackerController->handleExportDbResult(data, success);
if (! dbBackupsTrackerController->getBackupFilePath().isEmpty())
return;
}

dbExportsRegistryController->registerDbExported(data, success);
dbExportsRegistryController->handleExportDbResult(data, success);
}
Loading

0 comments on commit eb7b460

Please sign in to comment.