-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.cpp
307 lines (284 loc) · 8.72 KB
/
store.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <QDebug>
#include <QUuid>
#include <QSettings>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "store.h"
Store::Store(QObject *parent) :
QAbstractListModel(parent)
{
mDataRoles[ID] = "ID";
mDataRoles[TypeRole] = "type",
mDataRoles[StyleRole] = "style",
mDataRoles[TitleRole] = "title",
mDataRoles[LoginRole] = "login",
mDataRoles[NumberRole] = "number",
mDataRoles[PasswordRole] = "password",
mDataRoles[PinRole] = "pin",
mDataRoles[RelateRole] = "relate",
mDataRoles[DescriptionRole] = "description";
mStoreChanged = false;
isOpen = false;
}
Store::~Store()
{
close();
}
int Store::rowCount(const QModelIndex &parent) const
{
return parent.isValid() || !isOpen? 0: mData.size();
}
QVariant Store::data(const QModelIndex &index, int role) const
{
QVariant result;
if (index.isValid() && isOpen) {
int row = index.row();
if (row >= 0 && row < mData.size()) {
const StoreItem &storeItem = mData[row];
switch(role) {
case ID:
result = storeItem.ID;
break;
case TypeRole:
result = storeItem.type;
break;
case StyleRole:
result = storeItem.style;
break;
case TitleRole:
result = storeItem.title;
break;
case LoginRole:
result = storeItem.login;
break;
case NumberRole:
result = storeItem.number;
break;
case PasswordRole:
result = storeItem.password;
break;
case PinRole:
result = storeItem.pin;
break;
case RelateRole:
result = storeItem.relate;
break;
case DescriptionRole:
result = storeItem.description;
break;
}
}
}
return result;
}
bool Store::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!isOpen) return false;
bool result = false;
if (index.isValid()) {
int row = index.row();
if (row >= 0 && row < mData.size()) {
StoreItem &storeItem = mData[row];
switch(role) {
case TypeRole:
storeItem.type = value.toInt();
result = true;
break;
case StyleRole:
storeItem.style = value.toInt();
result = true;
break;
case TitleRole:
storeItem.title = value.toString();
result = true;
break;
case LoginRole:
storeItem.login = value.toString();
result = true;
break;
case NumberRole:
storeItem.number = value.toString();
result = true;
break;
case PasswordRole:
storeItem.password = value.toString();
result = true;
break;
case PinRole:
storeItem.pin = value.toString();
result = true;
break;
case RelateRole:
storeItem.relate = value.toString();
result = true;
break;
case DescriptionRole:
storeItem.description = value.toString();
result = true;
break;
}
}
}
if (result){
mStoreChanged = true;
emit dataChanged(index, index);
}
return result;
}
bool Store::exists(const QVariantMap &vault)
{
QSettings settings;
mStore.setFileName(QFileInfo(settings.fileName()).absolutePath() + "/" + vault.value("file").toString());
return mStore.exists();
}
bool Store::open(const QVariantMap& vault, const quint64 key)
{
if (isOpen) close();
crypto.setKey(key);
if (this->exists(vault)){
mStore.open(QIODevice::ReadOnly);
QByteArray fileContents = crypto.decryptToByteArray(mStore.readAll());
//Test the crypto data for errors
//THis would tell us if the key provided is the right key
if (crypto.lastError() != SimpleCrypt::ErrorNoError){
mStore.close();
return false;
}
QJsonDocument json = QJsonDocument::fromJson(fileContents);
mStore.close();
QJsonArray data = json.array();
beginInsertRows(QModelIndex(), mData.count(), mData.count());
for (int i=0; i< data.count(); i++){
QJsonObject item = data.at(i).toObject();
if (item.contains("storeVersion")){
//This is a file header
mHeader = item.toVariantMap();
}else{
StoreItem storeItem;
storeItem.ID = item.value("ID").toString();
storeItem.type = item.value("type").toInt();
storeItem.style = item.value("style").toInt();
storeItem.title = item.value("title").toString();
storeItem.login = item.value("login").toString();
storeItem.number = item.value("number").toString();
storeItem.password = item.value("password").toString();
storeItem.pin = item.value("pin").toString();
storeItem.relate = item.value("relate").toString();
storeItem.description = item.value("description").toString();
mData.append(storeItem);
}
}
endInsertRows();
mStore.close();
} else {
mHeader.title = vault.value("title").toString();
mHeader.description = vault.value("description").toString();
sync(); //If store file does not exist, create it
}
mStoreChanged = false;
return isOpen = true;
}
void Store::close()
{
if (!isOpen) return;
if (mStoreChanged) sync();
beginRemoveRows(QModelIndex(), 0, mData.count());
mData.clear();
endRemoveRows();
mStoreChanged = false;
isOpen = false;
}
QVariantMap Store::header()
{
return (QVariantMap) mHeader;
}
void Store::add(const QVariantMap &v)
{
if (!isOpen) return;
qDebug("Add row");
StoreItem storeItem;
beginInsertRows(QModelIndex(), mData.count(), mData.count());
storeItem = v;
storeItem.ID = QUuid::createUuid().toString();
mData.append(storeItem);
endInsertRows();
// mStoreChanged = true;
sync();
}
QVariantMap Store::get(const QString &id)
{
return (QVariantMap) mData[findElementIndexById(id)];
}
void Store::set(const QString &id, const QVariantMap &v)
{
StoreItem &storeItem = mData[findElementIndexById(id)];
storeItem = v;
// mStoreChanged = true;
sync();
}
void Store::remove(const QString &id)
{
if (!isOpen) return;
qDebug("Remove row");
int index = findElementIndexById(id);
beginRemoveRows(QModelIndex(),index, index);
mData.removeAt(index);
endRemoveRows();
// mStoreChanged = true;
sync();
}
QString Store::encode(const QString &v)
{
if (!isOpen) return v;
return crypto.encryptToString(v);
}
QString Store::decode(const QString &v)
{
if (!isOpen) return v;
return crypto.decryptToString(v);
}
int Store::findElementIndexById(const QString id) const
{
int index = -1;
for(int i=0; i<mData.count();++i){
if(mData[i].ID == id){
index = i;
break;
}
}
return index;
}
void Store::sync()
{
QJsonArray data;
QJsonObject header;
header.insert("storeVersion", VERSION);
header.insert("title", mHeader.title);
header.insert("description", mHeader.description);
data.append(header);
for (int i=0; i < mData.count(); i++){
QJsonObject item;
StoreItem storeItem = mData.at(i);
item.insert("ID", QJsonValue(storeItem.ID));
item.insert("type", QJsonValue(storeItem.type));
item.insert("style", QJsonValue(storeItem.style));
item.insert("title", QJsonValue(storeItem.title));
item.insert("login", QJsonValue(storeItem.login));
item.insert("number", QJsonValue(storeItem.number));
item.insert("password", QJsonValue(storeItem.password));
item.insert("pin", QJsonValue(storeItem.pin));
item.insert("relate", QJsonValue(storeItem.relate));
item.insert("description", QJsonValue(storeItem.description));
data.append(item);
}
QJsonDocument json = QJsonDocument(data);
QByteArray fileContents = crypto.encryptToByteArray(json.toJson());
if ( mStore.open(QIODevice::WriteOnly) )
{
mStore.write(fileContents);
mStore.close();
}
mStoreChanged = false;
}