-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordListModel.cpp
More file actions
executable file
·856 lines (643 loc) · 23.4 KB
/
PasswordListModel.cpp
File metadata and controls
executable file
·856 lines (643 loc) · 23.4 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
#include "PasswordListModel.h"
#include "Encrypter.h"
PasswordListModel::PasswordListModel(QObject *poParent) : QAbstractListModel(poParent)
{
qDebug() << __FUNCTION__;
this->fnInit();
}
PasswordListModel::~PasswordListModel()
{
qDebug() << __FUNCTION__;
delete this->poJsonObject;
}
void PasswordListModel::fnInit()
{
qDebug() << __FUNCTION__;
if (this->poJsonObject) {
delete this->poJsonObject;
}
this->poJsonObject = new QJsonObject();
(*this->poJsonObject)["passwords"] = QJsonArray();
(*this->poJsonObject)["history"] = QJsonArray();
if (this->poPasswordChangeHistoryListModel) {
delete this->poPasswordChangeHistoryListModel;
}
this->poPasswordChangeHistoryListModel = new PasswordChangeHistoryListModel();
this->poPasswordChangeHistoryListModel->poPasswordListModel = this;
}
void PasswordListModel::fnClearPasswordsArray()
{
(*this->poJsonObject)["passwords"] = QJsonArray();
}
QJsonArray PasswordListModel::fnGetPasswordsArray() const
{
return (*this->poJsonObject)["passwords"].toArray();
}
void PasswordListModel::fnSetPasswordsArray(QJsonArray oJsonArray)
{
(*this->poJsonObject)["passwords"] = oJsonArray;
}
QHash<int,QByteArray> PasswordListModel::roleNames() const
{
qDebug() << __FUNCTION__;
return {
{ NameRole, "name" },
{ UserRole, "user" },
{ PasswordRole, "password" },
{ IsDeletedRole, "isDeleted" },
{ AdditionalRole, "additional" },
{ IDRole, "id" },
{ SourceIndexRole, "sourceIndex" },
{ CreatedAtRole, "createdAt" },
{ UpdatedAtRole, "updatedAt" }
};
}
QVariant PasswordListModel::data(const QModelIndex &oIndex, int iRole) const
{
qDebug() << __FUNCTION__;
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
if (!oIndex.isValid())
return QVariant();
if (oIndex.row() >= oPasswordsJsonArray.size())
return QVariant();
QJsonObject oJsonObject = oPasswordsJsonArray.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();
} else
return QVariant();
}
bool PasswordListModel::setData(const QModelIndex &oIndex, const QVariant &oValue, int iRole)
{
qDebug() << __FUNCTION__;
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
if (oIndex.isValid() /*&& iRole == Qt::EditRole*/) {
if (oIndex.row()<oPasswordsJsonArray.size()) {
QJsonObject oJsonObject = oPasswordsJsonArray.at(oIndex.row()).toObject();
this->poPasswordChangeHistoryListModel->fnAddJsonObject("update", oJsonObject);
if (iRole == NameRole) {
oJsonObject["name"] = oValue.toString();
}
if (iRole == UserRole) {
oJsonObject["user"] = oValue.toString();
}
if (iRole == PasswordRole) {
oJsonObject["password"] = oValue.toString();
}
if (iRole == AdditionalRole) {
oJsonObject["additional"] = oValue.toString();
}
if (iRole == IDRole) {
oJsonObject["id"] = oValue.toString();
}
oJsonObject["updatedAt"] = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss");
QJsonValue oJsonValue(oJsonObject);
oPasswordsJsonArray.replace(oIndex.row(), oJsonValue);
this->fnSetPasswordsArray(oPasswordsJsonArray);
}
emit dataChanged(oIndex, oIndex, { iRole });
this->fnSave();
return true;
}
return false;
}
QVariant PasswordListModel::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 PasswordListModel::flags(const QModelIndex &oIndex) const
{
qDebug() << __FUNCTION__;
if (!oIndex.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(oIndex) | Qt::ItemIsEditable;
}
QString PasswordListModel::fnGenerateIndex(int iLength)
{
static QVector<QString> oLettersVector;
QString sResult = "";
if (oLettersVector.length() == 0) {
for (int iIndex=97;iIndex<123;iIndex++) {
oLettersVector.append(QChar(iIndex));
}
for (int iIndex=48;iIndex<58;iIndex++) {
oLettersVector.append(QChar(iIndex));
}
}
qsrand((unsigned int) time(NULL));
for (int iIndex=0; iIndex<iLength; iIndex++) {
sResult.append(oLettersVector[qrand() % oLettersVector.length()]);
}
return sResult;
}
bool PasswordListModel::insertRows(int iPosition, int iRows, const QModelIndex &oParent)
{
qDebug() << __FUNCTION__;
if (iRows==0) {
return true;
}
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
beginInsertRows(QModelIndex(), iPosition, iPosition+iRows-1);
for (int iRow = 0; iRow < iRows; ++iRow) {
QJsonObject oJsonObject;
oJsonObject["id"] = QDateTime::currentDateTime().toMSecsSinceEpoch(); //this->fnGenerateIndex();
oJsonObject["createdAt"] = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss");
qDebug() << "Created JSON object: " << oJsonObject << " at: " << iPosition;
QJsonValue oJsonValue(oJsonObject);
oPasswordsJsonArray.insert(iPosition, oJsonValue);
}
this->fnSetPasswordsArray(oPasswordsJsonArray);
endInsertRows();
this->fnSave();
return true;
}
bool PasswordListModel::removeRows(int iPosition, int iRows, const QModelIndex &oParent)
{
qDebug() << __FUNCTION__ << "iPosition:" << iPosition << "iRows:" << iRows << oParent;
if (iRows==0) {
return true;
}
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
beginRemoveRows(QModelIndex(), iPosition, iPosition+iRows-1);
for (int iRow = iPosition; iRow < iPosition+iRows; ++iRow) {
this->poPasswordChangeHistoryListModel->fnAddJsonObject("delete", oPasswordsJsonArray.at(iPosition).toObject());
oPasswordsJsonArray.removeAt(iPosition);
/*
QJsonObject oJsonObject = this->poJsonArray->at(iRow).toObject();
qDebug() << oJsonObject;
oJsonObject["isDeleted"] = true;
this->poJsonArray->replace(iRow, QJsonValue(oJsonObject));
*/
}
this->fnSetPasswordsArray(oPasswordsJsonArray);
endRemoveRows();
this->fnSave();
return true;
}
QModelIndex PasswordListModel::index(int iRow, int iColumn, const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
return createIndex(iRow, iColumn);
//return QModelIndex();
}
QModelIndex PasswordListModel::parent(const QModelIndex &oChild) const
{
qDebug() << __FUNCTION__;
return QModelIndex();
}
int PasswordListModel::rowCount(const QModelIndex &oParent) const
{
int iRows = this->fnGetPasswordsArray().size();
qDebug() << __FUNCTION__ << iRows;
return iRows;
}
int PasswordListModel::columnCount(const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
Q_UNUSED(oParent);
return 1;
}
bool PasswordListModel::hasChildren(const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
return false;
}
void PasswordListModel::fnSetFilePath(QString aFilePath)
{
qDebug() << __FUNCTION__;
this->sFilePath = aFilePath;
}
QVariant PasswordListModel::fnGetFilePath()
{
qDebug() << __FUNCTION__;
return this->sFilePath;
}
void PasswordListModel::fnSetPassword(QString sPassword)
{
qDebug() << __FUNCTION__;
this->sPassword = sPassword;
}
QString PasswordListModel::fnGetPassword()
{
qDebug() << __FUNCTION__;
return this->sPassword;
}
void PasswordListModel::fnUpdate()
{
qDebug() << __FUNCTION__;
beginResetModel();
endResetModel();
}
QVariant PasswordListModel::fnLoad()
{
qDebug() << __FUNCTION__;
beginResetModel();
if (!this->fnFileExists()) {
endResetModel();
return 0;
}
QFile oFileObj(this->sFilePath);
if (!oFileObj.open(QIODevice::ReadOnly)) {
endResetModel();
return -1;
}
Encrypter oEncrypter;
QByteArray sResult;
int iEncryptResult = oEncrypter.fnDecrypt(this->sPassword, oFileObj.readAll(), sResult);
oFileObj.close();
if (iEncryptResult<0) {
return iEncryptResult*10;
}
QJsonDocument oJsonDocument = QJsonDocument::fromJson(sResult);
if (!oJsonDocument.isObject()) {
endResetModel();
return -2;
}
*this->poJsonObject = oJsonDocument.object();
endResetModel();
return 1;
}
QVariant PasswordListModel::fnSave()
{
qDebug() << __FUNCTION__;
QFile oFileObj(this->sFilePath);
if (!oFileObj.open(QIODevice::WriteOnly)) {
return -1;
}
QJsonDocument oJsonDocument(*this->poJsonObject);
Encrypter oEncrypter;
QByteArray sResult;
int iEncryptResult = oEncrypter.fnEncrypt(this->sPassword, oJsonDocument.toJson(), sResult);
qDebug() << oJsonDocument.toJson();
if (iEncryptResult<0) {
oFileObj.close();
return iEncryptResult*10;
}
oFileObj.write(sResult);
oFileObj.close();
return 1;
}
bool PasswordListModel::fnFileExists()
{
qDebug() << __FUNCTION__;
QFileInfo oFileInfo(this->sFilePath);
return oFileInfo.exists() && oFileInfo.isFile();
}
void PasswordListModel::fnRemoveRow(int iIndex)
{
qDebug() << __FUNCTION__ << iIndex;
this->removeRows(iIndex, 1);
}
QVariant PasswordListModel::fnAddRow(QJsonObject oJsonObject)
{
qDebug() << __FUNCTION__;
int iSize = this->fnSize().toInt();
this->insertRows(iSize, 1);
iSize = this->fnSize().toInt();
int iIndex = iSize-1;
this->fnUpdateRow(iIndex, oJsonObject, false);
return iIndex;
}
void PasswordListModel::fnUpdateRow(int iIndex, QJsonObject oNewJsonObject, bool bAddToHistory)
{
qDebug() << __FUNCTION__;
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
QJsonObject oJsonObject = oPasswordsJsonArray[iIndex].toObject();
this->poPasswordChangeHistoryListModel->fnAddJsonObject("update", oJsonObject);
foreach(const QString& sColumn, oNewJsonObject.keys()) {
if (this->oColumns.contains(sColumn)) {
oJsonObject[sColumn] = oNewJsonObject[sColumn];
}
}
oPasswordsJsonArray.replace(iIndex, oJsonObject);
this->fnSetPasswordsArray(oPasswordsJsonArray);
this->fnSave();
this->fnUpdate();
}
QVariant PasswordListModel::fnSize()
{
qDebug() << __FUNCTION__;
return this->fnGetPasswordsArray().size();
}
QVariant PasswordListModel::fnToByteArray()
{
qDebug() << __FUNCTION__;
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
QJsonDocument oJsonDocument(oPasswordsJsonArray);
Encrypter oEncrypter;
QByteArray sResult;
int iEncryptResult = oEncrypter.fnEncrypt(this->sPassword, oJsonDocument.toJson(), sResult);
qDebug() << "oEncrypter.fnEncrypt" << iEncryptResult;
return sResult;
}
QVariant PasswordListModel::fnFromByteArray(QVariant oByteArray, QVariant iSyncMethod, bool bEncrypted)
{
qDebug() << __FUNCTION__;
beginResetModel();
QJsonDocument oJsonDocument;
int iEncryptResult = 1;
if (bEncrypted) {
Encrypter oEncrypter;
QByteArray oResult;
iEncryptResult = oEncrypter.fnDecrypt(this->sPassword, oByteArray.toByteArray(), oResult);
qDebug() << "oEncrypter.fnDecrypt" << iEncryptResult;
if (iEncryptResult != 1) {
return iEncryptResult;
}
oJsonDocument = QJsonDocument::fromJson(oResult);
} else {
oJsonDocument = QJsonDocument::fromJson(oByteArray.toByteArray());
}
qDebug() << "iSyncMethod.toInt()" << iSyncMethod.toInt();
if (iSyncMethod.toInt() == 0) {
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
for (int iLocalIndex=0; iLocalIndex<oPasswordsJsonArray.size(); iLocalIndex++) {
QJsonObject oLocalJsonObject = oPasswordsJsonArray[iLocalIndex].toObject();
this->poPasswordChangeHistoryListModel->fnAddJsonObject("update", oLocalJsonObject);
}
this->fnSetPasswordsArray(oJsonDocument.array());
}
if (iSyncMethod.toInt() == 1 || iSyncMethod.toInt() == 2) {
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
QJsonArray oRemoteJsonArray = oJsonDocument.array();
qDebug() << "oRemoteJsonArray.size()" << oRemoteJsonArray.size();
qDebug() << "this->poJsonArray->size()" << oPasswordsJsonArray.size();
for (int iRemoteIndex=0; iRemoteIndex<oRemoteJsonArray.size(); iRemoteIndex++) {
QJsonObject oRemoteJsonObject = oRemoteJsonArray[iRemoteIndex].toObject();
/*
if (oRemoteJsonObject["isDeleted"].toBool()) {
continue;
}
*/
bool bFound = false;
if (oPasswordsJsonArray.size()>0) {
for (int iLocalIndex=0; iLocalIndex<oPasswordsJsonArray.size(); iLocalIndex++) {
qDebug() << oRemoteJsonObject << oPasswordsJsonArray[iLocalIndex].toObject() << (oRemoteJsonObject["id"] != oPasswordsJsonArray[iLocalIndex].toObject()["id"]);
QJsonObject oLocalJsonObject = oPasswordsJsonArray[iLocalIndex].toObject();
/*
if (oLocalJsonObject["isDeleted"].toBool()) {
continue;
}
*/
if (oRemoteJsonObject["id"] == oLocalJsonObject["id"]) {
bFound = true;
if (iSyncMethod.toInt() == 1) {
oPasswordsJsonArray.replace(iLocalIndex, oRemoteJsonObject);
this->poPasswordChangeHistoryListModel->fnAddJsonObject("update", oLocalJsonObject);
}
}
}
}
if (!bFound) {
oPasswordsJsonArray.append(oRemoteJsonObject);
}
}
this->fnSetPasswordsArray(oPasswordsJsonArray);
}
endResetModel();
return iEncryptResult;
}
QVariant PasswordListModel::fnEncryptPassword()
{
qDebug() << __FUNCTION__;
Encrypter oEncrypter;
QByteArray sResult;
int iEncryptResult = oEncrypter.fnEncrypt(this->sPassword, this->sPassword.toUtf8(), sResult);
qDebug() << "oEncrypter.fnEncryptPassword" << iEncryptResult;
return sResult;
}
QVariant PasswordListModel::fnCheckPassword(QVariant oByteArray)
{
qDebug() << __FUNCTION__;
Encrypter oEncrypter;
QByteArray oResult;
int iEncryptResult = oEncrypter.fnDecrypt(this->sPassword, oByteArray.toByteArray(), oResult);
qDebug() << "oEncrypter.fnCheckPassword" << iEncryptResult;
if (iEncryptResult != 1) {
return false;
}
return oResult == this->sPassword;
}
QVariant PasswordListModel::fnExport(QString sFilePath, int iType)
{
//QString sFilePath = QUrl(sURL).toLocalFile();
qDebug() << __FUNCTION__ << sFilePath;
QFile oFileObj(sFilePath);
if (!oFileObj.open(QIODevice::WriteOnly)) {
qDebug() << "Can't open file";
return -1;
}
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
if (iType==0) {
QJsonDocument oJsonDocument(oPasswordsJsonArray);
oFileObj.write(oJsonDocument.toJson());
} else if (iType==1) {
int iLength = oPasswordsJsonArray.size();
QString sLine = "";
for (int iIndex=0; iIndex<iLength; iIndex++) {
QJsonObject oCurrentObject = oPasswordsJsonArray.at(iIndex).toObject();
oFileObj.write("name:\n");
sLine = oCurrentObject["name"].toString()+"\n";
oFileObj.write(sLine.toUtf8());
oFileObj.write("user:\n");
sLine = oCurrentObject["user"].toString()+"\n";
oFileObj.write(sLine.toUtf8());
oFileObj.write("password:\n");
sLine = oCurrentObject["password"].toString()+"\n";
oFileObj.write(sLine.toUtf8());
oFileObj.write("<<<additional\n");
sLine = oCurrentObject["additional"].toString()+"\n";
oFileObj.write(sLine.toUtf8());
oFileObj.write("<<<additional\n");
oFileObj.write("\n");
}
}
oFileObj.close();
return 1;
}
QVariant PasswordListModel::fnImport(QString sFilePath, int iSyncType)
{
//QString sFilePath = QUrl(sURL).toLocalFile();
qDebug() << __FUNCTION__ << sFilePath << iSyncType;
beginResetModel();
if (!this->fnFileExists()) {
endResetModel();
qDebug() << "File doesn't exist";
return 0;
}
QFile oFileObj(sFilePath);
if (!oFileObj.open(QIODevice::ReadOnly)) {
endResetModel();
qDebug() << "Can't open file";
return -1;
}
int iResult = 1;
if (iSyncType==0 || iSyncType==1 || iSyncType==2) {
iResult = this->fnFromByteArray(oFileObj.readAll(), iSyncType, false).toInt();
} else if (iSyncType==3 || iSyncType==4 || iSyncType==5) {
if (iSyncType==3) {
this->fnInit();
}
QChar oChar;
QString sCurrentProperty = "";
QString sCurrentPropertyEnd = "";
QString sCurrentPropertyValue = "";
bool bProperty = false;
bool bMultiLineProperty = false;
int iRowIndex = -1;
int iMultiLineSC = 0;
QString Lines = oFileObj.readAll();
for (int iIndex=0; iIndex<Lines.size(); iIndex++) {
oChar = Lines[iIndex];
qDebug() << oChar;
if (!bProperty && !bMultiLineProperty) {
if ((oChar<'a' || oChar>'z') && oChar!=':' && oChar!='\n' && oChar!='<') {
continue;
} else {
if (oChar=='<') {
iMultiLineSC++;
continue;
}
if (oChar>='a' && oChar<='z') {
sCurrentProperty += oChar;
continue;
}
if (oChar=='\n') {
if (iMultiLineSC==3) {
bMultiLineProperty = true;
qDebug() << "bMultiLineProperty" << sCurrentProperty;
} else if (iMultiLineSC==0) {
bProperty = true;
qDebug() << "bProperty" << sCurrentProperty;
}
iMultiLineSC = 0;
continue;
}
}
}
if (bProperty) {
if (oChar!='\n') {
sCurrentPropertyValue += oChar;
} else {
qDebug() << "Save property " << sCurrentProperty << sCurrentPropertyValue;
if (sCurrentProperty=="name") {
if (iSyncType==3) {
iRowIndex = this->fnAddRow().toInt();
}
if (iSyncType==4 || iSyncType==5) {
iRowIndex = this->fnFind(sCurrentProperty, sCurrentPropertyValue).toInt();
if (iRowIndex==-1) {
iRowIndex = this->fnAddRow().toInt();
} else if (iSyncType==5) {
iRowIndex = -1;
}
}
}
if (sCurrentProperty=="name" || sCurrentProperty=="user" || sCurrentProperty=="password") {
if (iRowIndex!=-1) {
this->fnSetValue(iRowIndex, sCurrentProperty, sCurrentPropertyValue);
}
}
sCurrentProperty = "";
sCurrentPropertyValue = "";
bProperty = false;
}
}
if (bMultiLineProperty) {
if (oChar=='<') {
iMultiLineSC++;
}
if (iMultiLineSC>0) {
if (oChar=='\n' || iIndex==Lines.size()-1) {
qDebug() << "Compare " << sCurrentPropertyEnd << ("<<<"+sCurrentProperty);
if (sCurrentPropertyEnd=="<<<"+sCurrentProperty) {
qDebug() << "End of " << sCurrentProperty;
if (sCurrentProperty=="additional" && iRowIndex!=-1) {
this->fnSetValue(iRowIndex, sCurrentProperty, sCurrentPropertyValue);
}
iMultiLineSC = 0;
sCurrentProperty = "";
sCurrentPropertyValue = "";
sCurrentPropertyEnd = "";
bMultiLineProperty = false;
} else {
sCurrentPropertyEnd += oChar;
sCurrentPropertyValue += sCurrentPropertyEnd;
sCurrentPropertyEnd = "";
}
} else {
sCurrentPropertyEnd += oChar;
}
} else {
sCurrentPropertyValue += oChar;
}
}
}
}
endResetModel();
this->fnSave();
return iResult;
}
QVariant PasswordListModel::fnFind(QString sKey, QString sValue)
{
qDebug() << __FUNCTION__;
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
for (int iIndex = 0; iIndex<oPasswordsJsonArray.size(); iIndex++) {
QJsonObject oJsonObject = oPasswordsJsonArray.at(iIndex).toObject();
if (oJsonObject[sKey].isString()) {
if (oJsonObject[sKey]==sValue) {
return iIndex;
}
}
if (oJsonObject[sKey].isDouble()) {
if (QString::number(oJsonObject[sKey].toDouble(), 'f', 0)==sValue) {
return iIndex;
}
}
}
return -1;
}
void PasswordListModel::fnSetValue(int iIndex, QString sKey, QString sValue)
{
qDebug() << __FUNCTION__;
QJsonArray oPasswordsJsonArray = this->fnGetPasswordsArray();
QJsonObject oJsonObject = oPasswordsJsonArray[iIndex].toObject();
oJsonObject[sKey] = sValue;
oPasswordsJsonArray.replace(iIndex, oJsonObject);
this->fnSetPasswordsArray(oPasswordsJsonArray);
}
void PasswordListModel::fnRestoreFromJsonObject(QJsonObject oJsonObject)
{
qDebug() << __FUNCTION__;
int iRecordIndex = this->fnFind("id", QString::number(oJsonObject["id"].toDouble(), 'f', 0)).toInt();
if (iRecordIndex==-1) {
this->fnAddRow(oJsonObject);
} else {
this->fnUpdateRow(iRecordIndex, oJsonObject);
}
}