Skip to content

Commit f7e5de2

Browse files
committed
Fix db tracking to work with new delayed startup
Plus other small fixes
1 parent 00d5bcf commit f7e5de2

8 files changed

Lines changed: 81 additions & 57 deletions

File tree

src/DbBackupsTracker.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ DbBackupsTracker::DbBackupsTracker(QObject* parent):
1616
QObject(parent)
1717
{
1818
loadTracks();
19-
20-
connect(&watcher, &QFileSystemWatcher::fileChanged, [=] (const QString &path)
21-
{
22-
qDebug () << "FILE CHANGED" << path;
23-
});
2419
connect(&watcher, &QFileSystemWatcher::fileChanged,
2520
this, &DbBackupsTracker::checkDbBackupSynchronization);
2621
}
@@ -63,7 +58,7 @@ int DbBackupsTracker::extractCredentialsDbChangeNumberEncryptedBackup(const QJso
6358
if (root.contains("credentialsDbChangeNumber"))
6459
return root.value("credentialsDbChangeNumber").toInt();
6560

66-
return 0;
61+
return -1;
6762
}
6863

6964
int DbBackupsTracker::extractCredentialsDbChangeNumberLegacyBackup(const QJsonDocument &d) const
@@ -73,12 +68,12 @@ int DbBackupsTracker::extractCredentialsDbChangeNumberLegacyBackup(const QJsonDo
7368
if (val.isDouble())
7469
return val.toInt();
7570

76-
return 0;
71+
return -1;
7772
}
7873

7974
int DbBackupsTracker::extractCredentialsDbChangeNumber(const QString &content) const
8075
{
81-
int cn = 0;
76+
int cn = -1;
8277
QJsonDocument d = QJsonDocument::fromJson(content.toLocal8Bit());
8378
if (isALegacyBackup(d))
8479
cn = extractCredentialsDbChangeNumberLegacyBackup(d);
@@ -94,7 +89,7 @@ int DbBackupsTracker::extractDataDbChangeNumberEncryptedBackup(const QJsonDocume
9489
if (root.contains("dataDbChangeNumber"))
9590
return root.value("dataDbChangeNumber").toInt();
9691

97-
return 0;
92+
return -1;
9893
}
9994

10095
bool DbBackupsTracker::isALegacyBackup(const QJsonDocument &d) const
@@ -114,12 +109,12 @@ int DbBackupsTracker::extractDataDbChangeNumberLegacyBackup(const QJsonDocument
114109
if (val.isDouble())
115110
return val.toInt();
116111

117-
return 0;
112+
return -1;
118113
}
119114

120115
int DbBackupsTracker::extractDataDbChangeNumber(const QString &content) const
121116
{
122-
int cn = 0;
117+
int cn = -1;
123118
QJsonDocument d = QJsonDocument::fromJson(content.toLocal8Bit());
124119
if (d.isObject())
125120
cn = extractDataDbChangeNumberEncryptedBackup(d);
@@ -143,7 +138,6 @@ bool DbBackupsTracker::isUpdateRequired() const
143138
{
144139
try
145140
{
146-
147141
int backupCCN = tryGetCredentialsDbBackupChangeNumber();
148142
int backupDCN = tryGetDataDbBackupChangeNumber();
149143

@@ -307,6 +301,12 @@ void DbBackupsTracker::checkDbBackupSynchronization()
307301
int backupCCN = tryGetCredentialsDbBackupChangeNumber();
308302
int backupDCN = tryGetDataDbBackupChangeNumber();
309303

304+
// < 0 values are a failure to read the file.
305+
if (backupCCN < 0 || backupDCN < 0)
306+
return;
307+
308+
qDebug () << "Backup file changed: " << backupCCN << " - " << backupDCN;
309+
310310
if (isDbBackupChangeNumberGreater(backupCCN, backupDCN))
311311
emit greaterDbBackupChangeNumber();
312312

@@ -315,7 +315,7 @@ void DbBackupsTracker::checkDbBackupSynchronization()
315315
}
316316
catch (DbBackupsTrackerNoBackupFileSet)
317317
{
318-
qDebug() << "No backup file for " << cardId;
318+
qDebug() << "No backup file for " << cardId;
319319
}
320320
}
321321

src/DbBackupsTrackerController.cpp

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ DbBackupsTrackerController::DbBackupsTrackerController(MainWindow *window, WSCli
4747
this, &DbBackupsTrackerController::handleDeviceConnectedChanged);
4848

4949
handleFirmwareVersionChange(wsClient->get_fwVersion());
50+
handleDeviceStatusChanged(wsClient->get_status());
51+
handleDeviceConnectedChanged(wsClient->isConnected());
52+
53+
//Delay the init in the next event loop call. Not in constructor.
54+
QTimer::singleShot(0, [=]()
55+
{
56+
handleCardDbMetadataChanged(wsClient->get_cardId(),
57+
wsClient->get_credentialsDbChangeNumber(),
58+
wsClient->get_dataDbChangeNumber());
59+
});
5060
}
5161

5262
void DbBackupsTrackerController::setBackupFilePath(const QString &path)
@@ -67,6 +77,7 @@ void DbBackupsTrackerController::handleCardDbMetadataChanged(QString cardId,
6777
int credentialsDbChangeNumber,
6878
int dataDbChangeNumber)
6979
{
80+
qDebug() << "Card meta data changed " << cardId << " - " << credentialsDbChangeNumber << " - " << dataDbChangeNumber;
7081
window->hidePrompt();
7182

7283
dbBackupsTracker.setCardId(cardId);
@@ -79,28 +90,27 @@ void DbBackupsTrackerController::handleCardDbMetadataChanged(QString cardId,
7990
emit backupFilePathChanged(path);
8091
}
8192

82-
8393
void DbBackupsTrackerController::askForImportBackup()
8494
{
85-
askImportMessage = new QMessageBox(window);
86-
askImportMessage->setWindowTitle(tr("Import db backup"));
87-
askImportMessage->setText(tr("Credentials in the backup file are more recent. "
88-
"Do you want to import credentials to the device?"));
89-
90-
askImportMessage->addButton(QMessageBox::Yes);
91-
askImportMessage->addButton(QMessageBox::No);
92-
askImportMessage->setDefaultButton(QMessageBox::No);
93-
askImportMessage->setModal(true);
94-
95-
int btn = askImportMessage->exec();
96-
askImportMessage->deleteLater();
97-
askImportMessage = nullptr;
98-
99-
if (btn == QMessageBox::Yes)
100-
{
101-
QString data = readDbBackupFile();
102-
importDbBackup(data);
103-
}
95+
askImportMessage = new QMessageBox(window);
96+
askImportMessage->setWindowTitle(tr("Import db backup"));
97+
askImportMessage->setText(tr("Credentials in the backup file are more recent. "
98+
"Do you want to import credentials to the device?"));
99+
100+
askImportMessage->addButton(QMessageBox::Yes);
101+
askImportMessage->addButton(QMessageBox::No);
102+
askImportMessage->setDefaultButton(QMessageBox::No);
103+
askImportMessage->setModal(true);
104+
105+
int btn = askImportMessage->exec();
106+
askImportMessage->deleteLater();
107+
askImportMessage = nullptr;
108+
109+
if (btn == QMessageBox::Yes)
110+
{
111+
QString data = readDbBackupFile();
112+
importDbBackup(data);
113+
}
104114
}
105115

106116
void DbBackupsTrackerController::importDbBackup(QString data)
@@ -117,6 +127,8 @@ void DbBackupsTrackerController::importDbBackup(QString data)
117127

118128
void DbBackupsTrackerController::handleGreaterDbBackupChangeNumber()
119129
{
130+
qDebug() << "Backup file is greater than device";
131+
120132
hideExportRequestIfVisible();
121133
hideImportRequestIfVisible();
122134
askForImportBackup();
@@ -133,9 +145,9 @@ void DbBackupsTrackerController::askForExportBackup()
133145
std::function<void()> onReject = [this]()
134146
{
135147
QMessageBox::StandardButton btn = QMessageBox::warning(
136-
window, tr("Be careful"),
137-
tr("By denying you can loose your changes. Do you want to continue?"),
138-
QMessageBox::Yes | QMessageBox::No);
148+
window, tr("Be careful"),
149+
tr("By denying you can loose your changes. Do you want to continue?"),
150+
QMessageBox::Yes | QMessageBox::No);
139151

140152
if (btn == QMessageBox::Yes)
141153
{
@@ -173,6 +185,8 @@ void DbBackupsTrackerController::exportDbBackup()
173185

174186
void DbBackupsTrackerController::handleLowerDbBackupChangeNumber()
175187
{
188+
qDebug() << "Device is newer than backup";
189+
176190
hideImportRequestIfVisible();
177191
hideExportRequestIfVisible();
178192
askForExportBackup();
@@ -191,7 +205,7 @@ void DbBackupsTrackerController::clearTrackerCardInfo()
191205
dbBackupsTracker.setCardId("");
192206
dbBackupsTracker.setDataDbChangeNumber(0);
193207
dbBackupsTracker.setCredentialsDbChangeNumber(0);
194-
}
208+
}
195209

196210

197211
void DbBackupsTrackerController::handleExportDbResult(const QByteArray &d, bool success)
@@ -241,14 +255,16 @@ void DbBackupsTrackerController::handleFirmwareVersionChange(const QString &)
241255
{
242256
if (wsClient->isFw12())
243257
{
258+
qDebug() << "Setup db tracking";
244259
window->showDbBackTrackingControls(true);
245260
connectDbBackupsTracker();
246-
} else
261+
}
262+
else
247263
{
264+
qDebug() << "Db tracking is not available for fw < 1.2";
248265
window->showDbBackTrackingControls(false);
249266
disconnectDbBackupsTracker();
250267
}
251-
252268
}
253269

254270
void DbBackupsTrackerController::hideExportRequestIfVisible()
@@ -266,13 +282,12 @@ void DbBackupsTrackerController::hideImportRequestIfVisible()
266282
askImportMessage->reject();
267283
}
268284

269-
270285
QString DbBackupsTrackerController::getBackupFilePath()
271286
{
272-
QString id = dbBackupsTracker.getCardId();
273-
QString file = dbBackupsTracker.getTrackPath(id);
287+
QString id = dbBackupsTracker.getCardId();
288+
QString file = dbBackupsTracker.getTrackPath(id);
274289

275-
return file;
290+
return file;
276291
}
277292

278293
QString DbBackupsTrackerController::readDbBackupFile()

src/FilesCache.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool FilesCache::save(QList<QVariantMap> files)
5050

5151
QList<QVariantMap> FilesCache::load()
5252
{
53-
if (!m_dbChangeNumberSet || m_cardCPZ.isNull())
53+
if (!m_dbChangeNumberSet || m_cardCPZ.isEmpty())
5454
{
5555
qDebug() << "dbChangeNumberSet not set or null CPZ";
5656
return QList<QVariantMap>();
@@ -105,7 +105,7 @@ void FilesCache::resetState()
105105

106106
bool FilesCache::setDbChangeNumber(quint8 changeNumber)
107107
{
108-
if (m_dbChangeNumberSet && m_dbChangeNumber != changeNumber && !m_cardCPZ.isNull())
108+
if (m_dbChangeNumberSet && m_dbChangeNumber != changeNumber && !m_cardCPZ.isEmpty())
109109
{
110110
qDebug() << "dbChangeNumber updated, triggering file storage";
111111
auto tempDb = load();
@@ -117,7 +117,7 @@ bool FilesCache::setDbChangeNumber(quint8 changeNumber)
117117
m_dbChangeNumber = changeNumber;
118118
m_dbChangeNumberSet = true;
119119

120-
if (!m_cardCPZ.isNull())
120+
if (!m_cardCPZ.isEmpty())
121121
return true;
122122
else
123123
return false;
@@ -146,8 +146,8 @@ bool FilesCache::setCardCPZ(QByteArray cardCPZ)
146146
m_filePath = dataDir.absoluteFilePath(fileName);
147147

148148
qint64 m_key = 0;
149-
for (int i = 0; i < std::min(8, cardCPZ.size()) ; i ++)
150-
m_key += (static_cast<unsigned int>(cardCPZ[i]) & 0xFF) << (i*8);
149+
for (int i = 0;i < std::min(8, cardCPZ.size());i++)
150+
m_key += (static_cast<unsigned int>(cardCPZ[i]) & 0xFF) << (i * 8);
151151

152152
m_simpleCrypt.setKey(m_key);
153153
m_simpleCrypt.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);

src/FilesCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public slots:
2929
private:
3030
QByteArray m_cardCPZ;
3131
QString m_filePath;
32-
qint64 m_key;
32+
qint64 m_key = 0;
3333
bool m_dbChangeNumberSet = false;
34-
quint8 m_dbChangeNumber;
34+
quint8 m_dbChangeNumber = -1;
3535
SimpleCrypt m_simpleCrypt;
3636
};
3737

src/MainWindow.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ MainWindow::MainWindow(WSClient *client, QWidget *parent) :
4343
bAdvancedTabVisible(false),
4444
dbBackupTrakingControlsVisible(false),
4545
previousWidget(nullptr),
46-
m_passwordProfilesModel(new PasswordProfilesModel(this)),
47-
dbBackupsTrackerController(this, client, this)
46+
m_passwordProfilesModel(new PasswordProfilesModel(this))
4847
{
4948
QSettings s;
5049
bSSHKeysTabVisibleOnDemand = s.value("settings/SSHKeysTabsVisibleOnDemand", true).toBool();
@@ -56,6 +55,8 @@ MainWindow::MainWindow(WSClient *client, QWidget *parent) :
5655
ui->setupUi(this);
5756
refreshAppLangCb();
5857

58+
dbBackupsTrackerController = new DbBackupsTrackerController(this, client, this);
59+
5960
ui->checkBoxLongPress->setChecked(s.value("settings/long_press_cancel", true).toBool());
6061
connect(ui->checkBoxLongPress, &QCheckBox::toggled, [this](bool checked)
6162
{
@@ -161,8 +162,8 @@ MainWindow::MainWindow(WSClient *client, QWidget *parent) :
161162
// DB Backups UI
162163
ui->toolButton_clearBackupFilePath->setIcon(AppGui::qtAwesome()->icon(fa::remove));
163164
ui->toolButton_setBackupFilePath->setIcon(AppGui::qtAwesome()->icon(fa::foldero));
164-
ui->lineEdit_dbBackupFilePath->setText(dbBackupsTrackerController.getBackupFilePath());
165-
connect(&dbBackupsTrackerController, &DbBackupsTrackerController::backupFilePathChanged, [=] (const QString &path)
165+
ui->lineEdit_dbBackupFilePath->setText(dbBackupsTrackerController->getBackupFilePath());
166+
connect(dbBackupsTrackerController, &DbBackupsTrackerController::backupFilePathChanged, [=] (const QString &path)
166167
{
167168
ui->lineEdit_dbBackupFilePath->setText(path);
168169
});
@@ -1317,7 +1318,7 @@ void MainWindow::retranslateUi()
13171318
void MainWindow::on_toolButton_clearBackupFilePath_released()
13181319
{
13191320
ui->lineEdit_dbBackupFilePath->clear();
1320-
dbBackupsTrackerController.setBackupFilePath("");
1321+
dbBackupsTrackerController->setBackupFilePath("");
13211322
}
13221323

13231324
void MainWindow::on_toolButton_setBackupFilePath_released()
@@ -1331,6 +1332,6 @@ void MainWindow::on_toolButton_setBackupFilePath_released()
13311332
{
13321333
QStringList fileNames = dialog.selectedFiles();
13331334
ui->lineEdit_dbBackupFilePath->setText(fileNames.first());
1334-
dbBackupsTrackerController.setBackupFilePath(fileNames.first());
1335+
dbBackupsTrackerController->setBackupFilePath(fileNames.first());
13351336
}
13361337
}

src/MainWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private slots:
143143
QMap<QWidget *, QPushButton *> m_tabMap;
144144
QWidget *previousWidget;
145145
PasswordProfilesModel *m_passwordProfilesModel;
146-
DbBackupsTrackerController dbBackupsTrackerController;
146+
DbBackupsTrackerController *dbBackupsTrackerController;
147147
};
148148

149149
#endif // MAINWINDOW_H

src/WSClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ void WSClient::onTextMessageReceived(const QString &message)
317317
int credentialsDbChangeNumber = data["credentialsDbChangeNumber"].toInt();
318318
int dataDbChangeNumber = data["dataDbChangeNumber"].toInt();
319319

320+
set_cardId(cardId);
321+
set_credentialsDbChangeNumber(credentialsDbChangeNumber);
322+
set_dataDbChangeNumber(dataDbChangeNumber);
323+
320324
emit cardDbMetadataChanged(cardId, credentialsDbChangeNumber, dataDbChangeNumber);
321325
}
322326
}

src/WSClient.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class WSClient: public QObject
6464

6565
QT_WRITABLE_PROPERTY(qint64, uid, -1)
6666

67+
QT_WRITABLE_PROPERTY(QString, cardId, QString())
68+
QT_WRITABLE_PROPERTY(int, credentialsDbChangeNumber, 0)
69+
QT_WRITABLE_PROPERTY(int, dataDbChangeNumber, 0)
70+
6771
public:
6872
explicit WSClient(QObject *parent = nullptr);
6973
~WSClient();

0 commit comments

Comments
 (0)