-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordChangeHistoryListModel.cpp
More file actions
executable file
·242 lines (180 loc) · 6.21 KB
/
PasswordChangeHistoryListModel.cpp
File metadata and controls
executable file
·242 lines (180 loc) · 6.21 KB
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
#include "PasswordChangeHistoryListModel.h"
PasswordChangeHistoryListModel::PasswordChangeHistoryListModel(QObject *poParent):
QAbstractListModel(poParent)
{
qDebug() << __FUNCTION__;
}
PasswordChangeHistoryListModel::~PasswordChangeHistoryListModel()
{
qDebug() << __FUNCTION__;
}
void PasswordChangeHistoryListModel::fnClearHistoryArray()
{
(*this->poPasswordListModel->poJsonObject)["history"] = QJsonArray();
}
QJsonArray PasswordChangeHistoryListModel::fnGetHistoryArray() const
{
return (*this->poPasswordListModel->poJsonObject)["history"].toArray();
}
void PasswordChangeHistoryListModel::fnSetHistoryArray(QJsonArray oJsonArray)
{
(*this->poPasswordListModel->poJsonObject)["history"] = oJsonArray;
}
QHash<int, QByteArray> PasswordChangeHistoryListModel::roleNames() const
{
qDebug() << __FUNCTION__;
return {
{ NameRole, "name" },
{ UserRole, "user" },
{ PasswordRole, "password" },
{ IsDeletedRole, "isDeleted" },
{ AdditionalRole, "additional" },
{ IDRole, "id" },
{ SourceIndexRole, "sourceIndex" },
{ CreatedAtRole, "createdAt" },
{ UpdatedAtRole, "updatedAt" },
{ TimestampRole, "timestamp" },
{ EventTypeRole, "eventType" }
};
}
QVariant PasswordChangeHistoryListModel::data(const QModelIndex &oIndex, int iRole) const
{
qDebug() << __FUNCTION__;
QJsonArray oHistoryJsonArray = this->fnGetHistoryArray();
if (!oIndex.isValid())
return QVariant();
if (oIndex.row() >= oHistoryJsonArray.size())
return QVariant();
QJsonObject oJsonObject = oHistoryJsonArray.at(oIndex.row()).toObject();
if (iRole == NameRole) {
return oJsonObject["name"].toString();
} if (iRole == UserRole) {
return oJsonObject["user"].toString();
} if (iRole == PasswordRole) {
return oJsonObject["password"].toString();
} if (iRole == IsDeletedRole) {
return oJsonObject["isDeleted"].toBool();
} if (iRole == AdditionalRole) {
return oJsonObject["additional"].toString();
} if (iRole == IDRole) {
return QString::number(oJsonObject["id"].toDouble(), 'f', 0);;
} if (iRole == SourceIndexRole) {
return oIndex.row();
} if (iRole == CreatedAtRole) {
return oJsonObject["createdAt"].toString();
} if (iRole == UpdatedAtRole) {
return oJsonObject["updatedAt"].toString();
} if (iRole == TimestampRole) {
return oJsonObject["timestamp"].toString();
} if (iRole == EventTypeRole) {
return oJsonObject["eventType"].toString();
} else
return QVariant();
}
bool PasswordChangeHistoryListModel::setData(const QModelIndex &oIndex, const QVariant &oValue, int iRole)
{
qDebug() << __FUNCTION__;
return false;
}
QVariant PasswordChangeHistoryListModel::headerData(int iSection, Qt::Orientation oOrientation, int iRole) const
{
qDebug() << __FUNCTION__;
if (iRole != Qt::DisplayRole)
return QVariant();
if (oOrientation == Qt::Horizontal)
return QString("Column %1").arg(iSection);
else
return QString("Row %1").arg(iSection);
}
Qt::ItemFlags PasswordChangeHistoryListModel::flags(const QModelIndex &oIndex) const
{
qDebug() << __FUNCTION__;
if (!oIndex.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(oIndex) | Qt::ItemIsEditable;
}
bool PasswordChangeHistoryListModel::insertRows(int iPosition, int iRows, const QModelIndex &oParent)
{
qDebug() << __FUNCTION__;
return false;
}
bool PasswordChangeHistoryListModel::removeRows(int iPosition, int iRows, const QModelIndex &oParent)
{
qDebug() << __FUNCTION__;
if (iRows==0) {
return true;
}
QJsonArray oHistoryJsonArray = this->fnGetHistoryArray();
beginRemoveRows(QModelIndex(), iPosition, iPosition+iRows-1);
for (int iRow = iPosition; iRow < iPosition+iRows; ++iRow) {
oHistoryJsonArray.removeAt(iPosition);
/*
QJsonObject oJsonObject = this->poJsonArray->at(iRow).toObject();
qDebug() << oJsonObject;
oJsonObject["isDeleted"] = true;
this->poJsonArray->replace(iRow, QJsonValue(oJsonObject));
*/
}
this->fnSetHistoryArray(oHistoryJsonArray);
endRemoveRows();
this->poPasswordListModel->fnSave();
return true;
}
QModelIndex PasswordChangeHistoryListModel::index(int iRow, int iColumn, const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
return createIndex(iRow, iColumn);
}
QModelIndex PasswordChangeHistoryListModel::parent(const QModelIndex &oChild) const
{
qDebug() << __FUNCTION__;
return QModelIndex();
}
int PasswordChangeHistoryListModel::rowCount(const QModelIndex &oParent) const
{
int iRows = this->fnGetHistoryArray().size();
qDebug() << __FUNCTION__ << iRows;
return iRows;
}
int PasswordChangeHistoryListModel::columnCount(const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
Q_UNUSED(oParent);
return 1;
}
bool PasswordChangeHistoryListModel::hasChildren(const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
return false;
}
void PasswordChangeHistoryListModel::fnAddJsonObject(QString sEventType, QJsonObject oJsonObject)
{
qDebug() << __FUNCTION__;
QJsonArray oHistoryJsonArray = this->fnGetHistoryArray();
beginInsertRows(QModelIndex(), 0, 0);
oJsonObject["timestamp"] = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss");
oJsonObject["eventType"] = sEventType;
QJsonValue oJsonValue(oJsonObject);
oHistoryJsonArray.insert(0, oJsonValue);
this->fnSetHistoryArray(oHistoryJsonArray);
endInsertRows();
//this->poPasswordListModel->fnSave();
}
void PasswordChangeHistoryListModel::fnClear()
{
beginResetModel();
this->fnClearHistoryArray();
endResetModel();
}
void PasswordChangeHistoryListModel::fnRemoveRow(int iIndex)
{
qDebug() << __FUNCTION__ << iIndex;
this->removeRows(iIndex, 1);
}
void PasswordChangeHistoryListModel::fnRestore(int iIndex)
{
qDebug() << __FUNCTION__ << iIndex;
QJsonArray oHistoryJsonArray = this->fnGetHistoryArray();
QJsonObject oJsonObject = oHistoryJsonArray.at(iIndex).toObject();
this->poPasswordListModel->fnRestoreFromJsonObject(oJsonObject);
}