forked from bardia16/CPS_Android_HW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatterndatabase.cpp
53 lines (43 loc) · 1.32 KB
/
patterndatabase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "patterndatabase.h"
#include <QDebug>
PatternDatabase::PatternDatabase(QObject *parent)
: QObject(parent)
{
}
void PatternDatabase::addPattern(Pattern *pattern)
{
qDebug() << "Pattern created";
patterns.append(pattern);
}
void PatternDatabase::authenticatePattern(Pattern *pattern)
{
QString status = "failed";
for (Pattern *storedPattern : patterns) {
if (storedPattern->authenticate(pattern)) {
status = "succeed";
}
}
QString output = QStringLiteral("Authentication %1").arg(status);
emit authenticationResult(output);
}
void PatternDatabase::savePatternsToJson(const QString &fileName)
{
QJsonArray jsonArray;
for (Pattern *pattern : patterns) {
jsonArray.append(pattern->toJson());
}
QJsonDocument jsonDoc(jsonArray);
QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/" + fileName;
QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (!dir.exists()) {
dir.mkpath(".");
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Couldn't open save file:" << filePath;
return;
}
file.write(jsonDoc.toJson(QJsonDocument::Indented));
file.close();
qDebug() << "File saved to:" << filePath;
}