-
Notifications
You must be signed in to change notification settings - Fork 2
/
Storage.cpp
188 lines (155 loc) · 5.31 KB
/
Storage.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Created by Arne Gockeln on 29.07.17.
//
#include <QIODevice>
#include <QFile>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#include <QMessageBox>
#include "Storage.h"
#include "ServiceItem.h"
using namespace TwoFA;
// will append to /Users/<username>/
#ifndef kSTORAGE_DIR
#define kSTORAGE_DIR ".twofa"
#endif
// will append to /Users/<username>/<kSTORAGE_DIR>/
#ifndef kSTORAGE_FILENAME
#define kSTORAGE_FILENAME "twofa.xml"
#endif
Storage* Storage::m_instance = 0;
Storage* Storage::getInstance() {
if(m_instance == 0){
m_instance = new Storage();
}
return m_instance;
}
Storage::Storage() {
checkStorage();
}
/**
* Check if storage file exists,
* if not create it
*/
void Storage::checkStorage() {
QString storagePath = QString(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
storagePath.append("/");
storagePath.append(kSTORAGE_DIR);
m_filename = storagePath;
m_filename.append("/");
m_filename.append(kSTORAGE_FILENAME);
QDir dir;
if(!dir.exists(storagePath)){
// create directory
dir.mkpath(storagePath);
// check if its read write possible
// if the file does not exist, it will be created
QFile file(m_filename);
if(file.open(QIODevice::ReadWrite)){
// write default data
QXmlStreamWriter xmlStreamWriter(&file);
xmlStreamWriter.setAutoFormatting(true);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeDTD("<!DOCTYPE twofa>");
xmlStreamWriter.writeStartElement("twofa");
xmlStreamWriter.writeAttribute("version", "1.0");
xmlStreamWriter.writeStartElement("sites");
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeEndDocument();
file.close();
} else {
QMessageBox::critical(Q_NULLPTR, QObject::tr("Error"), QObject::tr("Cannot open file %1:\n%2.").arg(m_filename).arg(file.errorString()));
}
}
}
/**
* Write tableModel data to xml file
*
* @param t_tableModel
*/
void Storage::write(TableModel *t_tableModel) {
if (m_filename.isEmpty()) {
QMessageBox::critical(Q_NULLPTR, QObject::tr("Error"), QObject::tr("Storage filename is empty!"));
return;
}
QFile file(m_filename);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::critical(Q_NULLPTR, QObject::tr("Error"),
QObject::tr("Cannot write to file %1:\n%2.")
.arg(m_filename)
.arg(file.errorString())
);
return;
}
QXmlStreamWriter xmlStreamWriter(&file);
xmlStreamWriter.setAutoFormatting(true);
xmlStreamWriter.writeStartDocument();
xmlStreamWriter.writeDTD("<!DOCTYPE twofa>");
xmlStreamWriter.writeStartElement("twofa");
xmlStreamWriter.writeAttribute("version", "1.0");
xmlStreamWriter.writeStartElement("sites");
QList<ServiceItem> serviceList = t_tableModel->getList();
if (serviceList.isEmpty()){
return;
}
for (int i = 0; i < serviceList.size(); i++) {
ServiceItem serviceItem(serviceList.at(i));
xmlStreamWriter.writeStartElement("site");
xmlStreamWriter.writeAttribute("name", serviceItem.getName());
xmlStreamWriter.writeAttribute("secret", serviceItem.getSecret());
xmlStreamWriter.writeAttribute("interval", QString::number(serviceItem.getInterval()));
xmlStreamWriter.writeEndElement();
}
xmlStreamWriter.writeEndElement();
xmlStreamWriter.writeEndDocument();
file.close();
}
/**
* Read data from xml file and populate tableModel
* @param t_tableModel
*/
void Storage::read(TableModel *t_tableModel){
if(m_filename.isEmpty()){
QMessageBox::critical(Q_NULLPTR, QObject::tr("Error"), QObject::tr("Storage filename is empty!"));
return;
}
QFile file(m_filename);
if(!file.open(QFile::ReadOnly)){
QMessageBox::critical(Q_NULLPTR, QObject::tr("Error"),
QObject::tr("Cannot read file %1:\n%2.")
.arg(m_filename)
.arg(file.errorString())
);
return;
}
QXmlStreamReader xmlStreamReader(&file);
while(!xmlStreamReader.atEnd() && !xmlStreamReader.hasError()){
// read next element
QXmlStreamReader::TokenType token = xmlStreamReader.readNext();
// if this is the start document token, continue
if(token == QXmlStreamReader::StartDocument){
continue;
}
// if this is the start of an element, try to read it
if(token == QXmlStreamReader::StartElement){
if(xmlStreamReader.name() == "site"){
ServiceItem serviceItem;
serviceItem.setName(xmlStreamReader.attributes().value("name").toString());
serviceItem.setSecret(xmlStreamReader.attributes().value("secret").toString());
serviceItem.setInterval(xmlStreamReader.attributes().value("interval").toInt());
if(!t_tableModel->getList().contains(serviceItem)){
t_tableModel->insertRows(0, 1, QModelIndex());
QModelIndex index = t_tableModel->index(0, 0, QModelIndex());
t_tableModel->setData(index, serviceItem.getName(), Qt::EditRole);
index = t_tableModel->index(0, 1, QModelIndex());
t_tableModel->setData(index, serviceItem.getSecret(), Qt::EditRole);
index = t_tableModel->index(0, 2, QModelIndex());
t_tableModel->setData(index, serviceItem.getInterval(), Qt::EditRole);
}
}
}
}
xmlStreamReader.clear();
file.close();
}