Skip to content

Commit

Permalink
Some code fix & formating
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Nov 24, 2017
1 parent dea7a89 commit 26b110b
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 126 deletions.
63 changes: 32 additions & 31 deletions src/DbBackupsTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,29 @@
#include <QDir>
#include <QStandardPaths>

DbBackupsTracker::DbBackupsTracker(QObject* parent)
: QObject(parent)
DbBackupsTracker::DbBackupsTracker(QObject* parent):
QObject(parent)
{
loadTracks();

connect(&watcher, &QFileSystemWatcher::fileChanged, [=] (const QString &path) {
connect(&watcher, &QFileSystemWatcher::fileChanged, [=] (const QString &path)
{
qDebug () << "FILE CHANGED" << path;
});
connect(&watcher, &QFileSystemWatcher::fileChanged, this,
&DbBackupsTracker::checkDbBackupSynchronization);
connect(&watcher, &QFileSystemWatcher::fileChanged,
this, &DbBackupsTracker::checkDbBackupSynchronization);
}

DbBackupsTracker::~DbBackupsTracker()
{
}

QString
DbBackupsTracker::getTrackPath(const QString& cardId) const
QString DbBackupsTracker::getTrackPath(const QString& cardId) const
{
return tracks.value(cardId, "");
}

QString
DbBackupsTracker::getCardId() const
QString DbBackupsTracker::getCardId() const
{
return cardId;
}
Expand All @@ -45,8 +44,7 @@ int DbBackupsTracker::getCredentialsDbChangeNumber() const
return credentialsDbChangeNumber;
}

QString
DbBackupsTracker::readFile(QString path) const
QString DbBackupsTracker::readFile(QString path) const
{
QString content;
QFile f(path);
Expand All @@ -59,8 +57,7 @@ DbBackupsTracker::readFile(QString path) const
return content;
}

int DbBackupsTracker::extractCredentialsDbChangeNumberEncryptedBackup(
QJsonDocument d) const
int DbBackupsTracker::extractCredentialsDbChangeNumberEncryptedBackup(const QJsonDocument &d) const
{
QJsonObject root = d.object();
if (root.contains("credentialsDbChangeNumber"))
Expand All @@ -69,8 +66,7 @@ int DbBackupsTracker::extractCredentialsDbChangeNumberEncryptedBackup(
return 0;
}

int DbBackupsTracker::extractCredentialsDbChangeNumberLegacyBackup(
QJsonDocument d) const
int DbBackupsTracker::extractCredentialsDbChangeNumberLegacyBackup(const QJsonDocument &d) const
{
QJsonArray root = d.array();
QJsonValue val = root.at(root.size() - 3);
Expand All @@ -80,7 +76,7 @@ int DbBackupsTracker::extractCredentialsDbChangeNumberLegacyBackup(
return 0;
}

int DbBackupsTracker::extractCredentialsDbChangeNumber(const QString& content) const
int DbBackupsTracker::extractCredentialsDbChangeNumber(const QString &content) const
{
int cn = 0;
QJsonDocument d = QJsonDocument::fromJson(content.toLocal8Bit());
Expand All @@ -92,8 +88,7 @@ int DbBackupsTracker::extractCredentialsDbChangeNumber(const QString& content) c
return cn;
}

int DbBackupsTracker::extractDataDbChangeNumberEncryptedBackup(
QJsonDocument d) const
int DbBackupsTracker::extractDataDbChangeNumberEncryptedBackup(const QJsonDocument &d) const
{
QJsonObject root = d.object();
if (root.contains("dataDbChangeNumber"))
Expand All @@ -112,7 +107,7 @@ bool DbBackupsTracker::isAnEncryptedBackup(const QJsonDocument &d) const
return d.isObject();
}

int DbBackupsTracker::extractDataDbChangeNumberLegacyBackup(QJsonDocument d) const
int DbBackupsTracker::extractDataDbChangeNumberLegacyBackup(const QJsonDocument &d) const
{
QJsonArray root = d.array();
QJsonValue val = root.at(root.size() - 2);
Expand All @@ -122,7 +117,7 @@ int DbBackupsTracker::extractDataDbChangeNumberLegacyBackup(QJsonDocument d) con
return 0;
}

int DbBackupsTracker::extractDataDbChangeNumber(const QString& content) const
int DbBackupsTracker::extractDataDbChangeNumber(const QString &content) const
{
int cn = 0;
QJsonDocument d = QJsonDocument::fromJson(content.toLocal8Bit());
Expand Down Expand Up @@ -153,7 +148,8 @@ bool DbBackupsTracker::isUpdateRequired() const
int backupDCN = tryGetDataDbBackupChangeNumber();

return (backupCCN > credentialsDbChangeNumber || backupDCN > dataDbChangeNumber);
} catch (DbBackupsTrackerNoBackupFileSet)
}
catch (DbBackupsTrackerNoBackupFileSet)
{
return false;
}
Expand All @@ -168,7 +164,8 @@ bool DbBackupsTracker::isBackupRequired() const

return (backupCCN < credentialsDbChangeNumber || backupDCN < dataDbChangeNumber);

} catch (DbBackupsTrackerNoBackupFileSet)
}
catch (DbBackupsTrackerNoBackupFileSet)
{
return false;
}
Expand All @@ -186,6 +183,7 @@ QString DbBackupsTracker::getTrackedBackupFileFormat()
{
QString content = tryReadBackupFile();
QJsonDocument d = QJsonDocument::fromJson(content.toLocal8Bit());

if (isALegacyBackup(d))
return "none";

Expand Down Expand Up @@ -218,8 +216,11 @@ QString DbBackupsTracker::tryReadBackupFile() const
{
DbBackupsTrackerNoBackupFileSet ex;
ex.raise();
} else
}
else
{
return readFile(path);
}

return "";
}
Expand All @@ -230,7 +231,8 @@ void DbBackupsTracker::track(const QString path)
{
DbBackupsTrackerNoCardIdSet ex;
ex.raise();
} else
}
else
{
watchPath(path);

Expand Down Expand Up @@ -281,7 +283,8 @@ void DbBackupsTracker::checkDbBackupSynchronization()

if (backupCCN < credentialsDbChangeNumber || backupDCN < dataDbChangeNumber)
emit lowerDbBackupChangeNumber();
} catch (DbBackupsTrackerNoBackupFileSet)
}
catch (DbBackupsTrackerNoBackupFileSet)
{
qDebug() << "No backup file for " << cardId;
}
Expand Down Expand Up @@ -343,22 +346,20 @@ QString DbBackupsTracker::getSettingsFilePath()

void DbBackupsTrackerNoCardIdSet::raise() const
{
throw * this;
throw *this;
}

DbBackupsTrackerNoCardIdSet*
DbBackupsTrackerNoCardIdSet::clone() const
DbBackupsTrackerNoCardIdSet *DbBackupsTrackerNoCardIdSet::clone() const
{
return new DbBackupsTrackerNoCardIdSet(*this);
}

void DbBackupsTrackerNoBackupFileSet::raise() const
{
throw * this;
throw *this;
}

DbBackupsTrackerNoBackupFileSet*
DbBackupsTrackerNoBackupFileSet::clone() const
DbBackupsTrackerNoBackupFileSet *DbBackupsTrackerNoBackupFileSet::clone() const
{
return new DbBackupsTrackerNoBackupFileSet(*this);
}
43 changes: 24 additions & 19 deletions src/DbBackupsTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ class DbBackupsTrackerNoCardIdSet : public QException
{
public:
void raise() const;
DbBackupsTrackerNoCardIdSet* clone() const;
DbBackupsTrackerNoCardIdSet *clone() const;
};

class DbBackupsTrackerNoBackupFileSet : public QException
{
public:
void raise() const;
DbBackupsTrackerNoBackupFileSet* clone() const;
DbBackupsTrackerNoBackupFileSet *clone() const;
};

class DbBackupsTracker : public QObject
{
Q_OBJECT
Q_PROPERTY(QString cardId READ getCardId WRITE setCardId NOTIFY cardIdChanged)
Q_PROPERTY(
int credentialsDbChangeNumber READ getCredentialsDbChangeNumber WRITE
setCredentialsDbChangeNumber NOTIFY credentialsDbChangeNumberChanged)
Q_PROPERTY(int dataDbChangeNumber READ getDataDbChangeNumber WRITE
setDataDbChangeNumber NOTIFY dataDbChangeNumberChanged)
Q_PROPERTY(int credentialsDbChangeNumber READ getCredentialsDbChangeNumber
WRITE setCredentialsDbChangeNumber NOTIFY credentialsDbChangeNumberChanged)
Q_PROPERTY(int dataDbChangeNumber READ getDataDbChangeNumber
WRITE setDataDbChangeNumber NOTIFY dataDbChangeNumberChanged)

public:
explicit DbBackupsTracker(QObject* parent = nullptr);
explicit DbBackupsTracker(QObject *parent = nullptr);
~DbBackupsTracker();

QString getTrackPath(const QString& cardId) const;
QString getTrackPath(const QString &cardId) const;
QString getCardId() const;

int getCredentialsDbChangeNumber() const;
Expand All @@ -61,7 +61,7 @@ class DbBackupsTracker : public QObject
signals:
void cardIdChanged(QString cardId);
void credentialsDbChangeNumberChanged(int credentialsDbChangeNumber);
void newTrack(const QString& cardId, const QString& path);
void newTrack(const QString &cardId, const QString &path);

void greaterDbBackupChangeNumber();
void lowerDbBackupChangeNumber();
Expand All @@ -79,27 +79,32 @@ protected slots:
void checkDbBackupSynchronization();

private:
void saveTracks();
void loadTracks();
QString getSettingsFilePath();
QFileSystemWatcher watcher;
QMap<QString, QString> tracks;
QString cardId;
int credentialsDbChangeNumber;
int dataDbChangeNumber;

void saveTracks();
void loadTracks();
QString getSettingsFilePath();

int tryGetCredentialsDbBackupChangeNumber() const;
int tryGetDataDbBackupChangeNumber() const;
void watchPath(const QString path);
QString tryReadBackupFile() const;
QString readFile(QString path) const;
int extractCredentialsDbChangeNumber(const QString& content) const;
int extractDataDbChangeNumber(const QString& content) const;

int extractCredentialsDbChangeNumber(const QString &content) const;
int extractDataDbChangeNumber(const QString &content) const;

bool isALegacyBackup(const QJsonDocument &d) const;
bool isAnEncryptedBackup(const QJsonDocument &d) const;
int extractCredentialsDbChangeNumberEncryptedBackup(QJsonDocument d) const;
int extractCredentialsDbChangeNumberLegacyBackup(QJsonDocument d) const;
int extractDataDbChangeNumberEncryptedBackup(QJsonDocument d) const;
int extractDataDbChangeNumberLegacyBackup(QJsonDocument d) const;

int extractCredentialsDbChangeNumberEncryptedBackup(const QJsonDocument &d) const;
int extractCredentialsDbChangeNumberLegacyBackup(const QJsonDocument &d) const;
int extractDataDbChangeNumberEncryptedBackup(const QJsonDocument &d) const;
int extractDataDbChangeNumberLegacyBackup(const QJsonDocument &d) const;
};

#endif // DBBACKUPSTRACKER_H
Loading

0 comments on commit 26b110b

Please sign in to comment.