-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaults.cpp
176 lines (153 loc) · 4.11 KB
/
vaults.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
#include <QDebug>
#include <QUuid>
#include <QSettings>
#include <QFileInfo>
#include <QFile>
#include "vaults.h"
Vaults::Vaults(QObject *parent) :
QAbstractListModel(parent)
{
mDataRoles[ID] = "ID";
mDataRoles[TitleRole] = "title";
mDataRoles[FileNameRole] = "file";
mDataRoles[DescriptionRole] = "description";
QSettings mSettings;
int size = mSettings.beginReadArray("vaults");
for (int i = 0; i < size; ++i) {
mSettings.setArrayIndex(i);
Vault vault;
vault.ID = mSettings.value("ID").toString();
vault.file = mSettings.value("file").toString();
vault.title = mSettings.value("title").toString();
vault.description = mSettings.value("description").toString();
mData.append(vault);
}
mSettings.endArray();
mDataChanged = false;
}
Vaults::~Vaults()
{
if (mDataChanged) synch();
}
void Vaults::synch()
{
QSettings mSettings;
mSettings.beginWriteArray("vaults", mData.count());
for (int i = 0; i < mData.size(); ++i) {
mSettings.setArrayIndex(i);
mSettings.setValue("ID", mData.at(i).ID);
mSettings.setValue("file", mData.at(i).file);
mSettings.setValue("title", mData.at(i).title);
mSettings.setValue("description", mData.at(i).description);
}
mSettings.endArray();
mDataChanged = false;
}
int Vaults::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return mData.size();
}
QVariant Vaults::data(const QModelIndex &index, int role) const
{
QVariant result;
//qDebug() << Q_FUNC_INFO << index << role;
if (index.isValid()) {
int row = index.row();
if (row >= 0 && row < mData.size()) {
const Vault &vault = mData[row];
switch(role) {
case ID:
result = vault.ID;
break;
case TitleRole:
result = vault.title;
break;
case DescriptionRole:
result = vault.description;
break;
case FileNameRole:
result = vault.file;
break;
}
}
}
return result;
}
bool Vaults::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool result = false;
if (index.isValid()) {
int row = index.row();
if (row >= 0 && row < mData.size()) {
Vault &vault = mData[row];
switch(role) {
case TitleRole:
vault.title = value.toString();
result = true;
break;
case DescriptionRole:
vault.description = value.toString();
result = true;
break;
}
}
}
if (result){
emit dataChanged(index, index);
mDataChanged = result;
}
return result;
}
QVariantMap Vaults::get(const QString& id)
{
return (QVariantMap) mData[findElementIndexById(id)];
}
void Vaults::set(const QString &id, const QVariantMap &v)
{
Vault &vault = mData[findElementIndexById(id)];
vault = v;
// mDataChanged = true;
synch();
}
void Vaults::add(const QVariantMap &v)
{
qDebug("Add row");
Vault vault;
beginInsertRows(QModelIndex(), mData.count(), mData.count());
vault = v;
vault.ID = QUuid::createUuid().toString();
vault.file = "store" + vault.ID;
mData.append(vault);
endInsertRows();
// mDataChanged = true;
synch();
}
void Vaults::remove(const QString& id)
{
qDebug("Remove row");
int index = findElementIndexById(id);
Vault &vault = mData[index];
beginRemoveRows(QModelIndex(),index, index);
QSettings settings;
QFile mStore;
mStore.setFileName(QFileInfo(settings.fileName()).absolutePath() + "/" + vault.file);
mStore.remove();
mData.removeAt(index);
endRemoveRows();
// mDataChanged = true;
synch();
}
int Vaults::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;
}