-
Notifications
You must be signed in to change notification settings - Fork 250
/
utils.cpp
892 lines (632 loc) · 26.2 KB
/
utils.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
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/* Copyright (c) 2019-2024 hors<[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "utils.h"
Utils::Utils(QObject *pParent) : QObject(pParent)
{
}
QList<Utils::RECORD> Utils::getRecords(QString sRootPath)
{
QList<Utils::RECORD> listResult;
QFileInfo fi(sRootPath);
sRootPath = fi.absoluteDir().path() + QDir::separator() + fi.fileName();
_getRecords(sRootPath, sRootPath, &listResult);
return listResult;
}
bool Utils::checkMData(Utils::MDATA *pMData, QString *psErrorString)
{
bool bResult = true;
if (bResult && (pMData->sName == "")) {
*psErrorString = tr("Invalid name");
bResult = false;
}
if (bResult && (pMData->sBundleFileName == "")) {
*psErrorString = tr("Invalid bundle file name");
bResult = false;
}
if (bResult && (pMData->sRoot == "")) {
if (!XBinary::isDirectoryExists(XBinary::convertPathName(pMData->sRoot))) {
*psErrorString = tr("Invalid root path");
bResult = false;
}
}
return bResult;
}
QString Utils::createBundleName(Utils::MDATA *pMData)
{
QString sResult;
sResult += pMData->sName;
if (pMData->sVersion != "") {
sResult += QString("_%1").arg(pMData->sVersion);
}
return sResult;
}
bool Utils::isPluginValid(QString sFileName, XBinary::PDSTRUCT *pPdStruct)
{
bool bResult = false;
QFile file;
file.setFileName(sFileName);
if (file.open(QIODevice::ReadOnly)) {
bResult = isPluginValid(&file, pPdStruct);
file.close();
}
return bResult;
}
bool Utils::isPluginValid(QIODevice *pDevice, XBinary::PDSTRUCT *pPdStruct)
{
bool bResult = false;
XZip xzip(pDevice);
if (xzip.isValid()) {
QList<XArchive::RECORD> listRecords = xzip.getRecords(-1, pPdStruct);
bResult = XArchive::isArchiveRecordPresent("plugin_info.json", &listRecords, pPdStruct);
}
return bResult;
}
QByteArray Utils::createPluginInfo(Utils::MDATA *pMData, QList<Utils::FILE_RECORD> *pListFileRecords, QList<Utils::DIRECTORY_RECORD> *pListDirectoryRecords)
{
QByteArray baResult;
QJsonObject recordObject;
mDataToObject(pMData, &recordObject);
if ((pMData->sSHA1 == "") || (pMData->listInstallRecords.count() == 0)) // In zip
{
int nFilesCount = pListFileRecords->count();
int nDirectoriesCount = pListDirectoryRecords->count();
QJsonArray installArray;
for (int i = 0; i < nDirectoriesCount; i++) {
QJsonObject record;
record.insert("Path", pListDirectoryRecords->at(i).sPath);
record.insert("Action", actionIdToString(ACTION_MAKEDIRECTORY));
installArray.append(record);
}
for (int i = 0; i < nFilesCount; i++) {
QJsonObject record;
record.insert("Path", pListFileRecords->at(i).sPath);
record.insert("Action", actionIdToString(ACTION_COPYFILE));
record.insert("SHA1", pListFileRecords->at(i).sSHA1);
installArray.append(record);
}
recordObject.insert("Install", installArray);
QJsonArray removeArray;
for (int i = 0; i < nFilesCount; i++) {
QJsonObject record;
record.insert("Path", pListFileRecords->at(i).sPath);
record.insert("Action", actionIdToString(ACTION_REMOVEFILE));
removeArray.append(record);
}
for (int i = nDirectoriesCount - 1; i >= 0; i--) {
QJsonObject record;
record.insert("Path", pListDirectoryRecords->at(i).sPath);
record.insert("Action", actionIdToString(ACTION_REMOVEDIRECTORYIFEMPTY));
removeArray.append(record);
}
recordObject.insert("Remove", removeArray);
}
QJsonDocument doc(recordObject);
baResult.append(doc.toJson());
return baResult;
}
Utils::MDATA Utils::getMDataFromZip(QString sFileName, QString sRootPath, XBinary::PDSTRUCT *pPdStruct)
{
Utils::MDATA result = {};
QFile file;
file.setFileName(sFileName);
if (file.open(QIODevice::ReadOnly)) {
result = getMDataFromZip(&file, sRootPath, pPdStruct);
file.close();
}
return result;
}
Utils::MDATA Utils::getMDataFromZip(QIODevice *pDevice, QString sRootPath, XBinary::PDSTRUCT *pPdStruct)
{
Utils::MDATA result = {};
result.sRoot = sRootPath;
XZip xzip(pDevice);
if (xzip.isValid()) {
QList<XArchive::RECORD> listRecords = xzip.getRecords(-1, pPdStruct);
XArchive::RECORD pluginInfoRecord = XArchive::getArchiveRecord("plugin_info.json", &listRecords);
QByteArray baData = xzip.decompress(&pluginInfoRecord, pPdStruct);
result = getMDataFromData(baData);
}
return result;
}
Utils::MDATA Utils::getMDataFromData(QByteArray baData)
{
Utils::MDATA result = {};
QJsonDocument jsDoc = QJsonDocument::fromJson(baData);
QJsonObject rootObj = jsDoc.object();
objectToMData(&rootObj, &result);
return result;
}
Utils::MDATA Utils::getMDataFromJSONFile(QString sFileName)
{
Utils::MDATA result = {};
QByteArray baData = XBinary::readFile(sFileName);
result = getMDataFromData(baData);
return result;
}
QList<Utils::MDATA> Utils::getInstalledModules(QString sDataPath, QString sRootPath)
{
QList<Utils::MDATA> listResult;
sDataPath = XBinary::convertPathName(sDataPath);
sRootPath = XBinary::convertPathName(sRootPath);
QDir dir(sDataPath + QDir::separator() + "installed");
QFileInfoList listFiles = dir.entryInfoList(QStringList() << "*.json", QDir::Files | QDir::NoSymLinks, QDir::Name);
int nCount = listFiles.count();
for (int i = 0; i < nCount; i++) {
QString sBundleName = listFiles.at(i).filePath();
QFile file;
file.setFileName(sBundleName);
if (file.open(QIODevice::ReadOnly)) {
QByteArray baData = file.readAll();
Utils::MDATA record = getMDataFromData(baData);
record.sBundleFileName = sBundleName;
listResult.append(record);
file.close();
}
}
return listResult;
}
QList<Utils::MDATA> Utils::getModulesFromJSONFile(QString sFileName)
{
QList<Utils::MDATA> listResult;
QByteArray baData = XBinary::readFile(sFileName);
// TODO Check valid
QJsonDocument jsDoc = QJsonDocument::fromJson(baData);
QJsonObject rootObj = jsDoc.object();
// QString sDate=rootObj.value("Date").toString();
QJsonArray arrayModules = rootObj.value("Modules").toArray();
int nCount = arrayModules.count();
for (int i = 0; i < nCount; i++) {
QJsonObject recordObject = arrayModules.at(i).toObject();
MDATA mdata = {};
objectToMData(&recordObject, &mdata);
listResult.append(mdata);
}
return listResult;
}
QDate Utils::getDateFromJSONFile(QString sFileName)
{
QDate dtResult;
QByteArray baData = XBinary::readFile(sFileName);
QJsonDocument jsDoc = QJsonDocument::fromJson(baData);
QJsonObject rootObj = jsDoc.object();
dtResult = QDate::fromString(rootObj.value("Date").toString(), "yyyy-MM-dd");
return dtResult;
}
bool Utils::createServerList(QString sListFileName, QList<QString> *pList, QString sWebPrefix, QString sDate)
{
bool bResult = false;
QJsonArray arrayModules;
int nCount = pList->count();
for (int i = 0; i < nCount; i++) {
MDATA mdata = getMDataFromJSONFile(pList->at(i));
mdata.sSrc = sWebPrefix + "/" + mdata.sSrc;
QJsonObject record;
mDataToObject(&mdata, &record);
arrayModules.append(record);
}
QJsonObject recordObject;
recordObject.insert("Date", sDate);
recordObject.insert("Modules", arrayModules);
QByteArray baResult;
QJsonDocument doc(recordObject);
baResult.append(doc.toJson());
bResult = XBinary::writeToFile(sListFileName, baResult);
return bResult;
}
void Utils::mDataToObject(Utils::MDATA *pMData, QJsonObject *pObject)
{
pObject->insert("Name", QJsonValue::fromVariant(pMData->sName));
pObject->insert("Version", QJsonValue::fromVariant(pMData->sVersion));
pObject->insert("Date", QJsonValue::fromVariant(pMData->sDate));
pObject->insert("Author", QJsonValue::fromVariant(pMData->sAuthor));
pObject->insert("Bugreport", QJsonValue::fromVariant(pMData->sBugreport));
pObject->insert("Info", QJsonValue::fromVariant(pMData->sInfo));
pObject->insert("Size", QJsonValue::fromVariant(pMData->nSize));
pObject->insert("CompressedSize", QJsonValue::fromVariant(pMData->nCompressedSize));
pObject->insert("Is32", QJsonValue::fromVariant(pMData->bIs32));
pObject->insert("Is64", QJsonValue::fromVariant(pMData->bIs64));
pObject->insert("Src", QJsonValue::fromVariant(pMData->sSrc));
pObject->insert("Updated", QJsonValue::fromVariant(pMData->sUpdated));
if (pMData->sSHA1 != "") {
pObject->insert("SHA1", QJsonValue::fromVariant(pMData->sSHA1));
}
if (pMData->sGithub != "") {
pObject->insert("Github", QJsonValue::fromVariant(pMData->sGithub));
}
// TODO optimize
int nInstallCount = pMData->listInstallRecords.count();
int nRemoveCount = pMData->listRemoveRecords.count();
int nConvertCount = pMData->listConvertRecords.count();
int nDownloadCount = pMData->listDownloads.count();
if (nInstallCount) {
QJsonArray jsArray;
for (int i = 0; i < nInstallCount; i++) {
HANDLE_RECORD record = pMData->listInstallRecords.at(i);
QJsonObject recordObj;
handleRecordToObject(&record, &recordObj);
jsArray.append(recordObj);
}
pObject->insert("Install", jsArray);
}
if (nRemoveCount) {
QJsonArray jsArray;
for (int i = 0; i < nRemoveCount; i++) {
HANDLE_RECORD record = pMData->listRemoveRecords.at(i);
QJsonObject recordObj;
handleRecordToObject(&record, &recordObj);
jsArray.append(recordObj);
}
pObject->insert("Remove", jsArray);
}
if (nConvertCount) {
QJsonArray jsArray;
for (int i = 0; i < nConvertCount; i++) {
HANDLE_RECORD record = pMData->listConvertRecords.at(i);
QJsonObject recordObj;
handleRecordToObject(&record, &recordObj);
jsArray.append(recordObj);
}
pObject->insert("Convert", jsArray);
}
if (nDownloadCount) {
QJsonArray jsArray;
for (int i = 0; i < nDownloadCount; i++) {
QJsonObject recordObj;
recordObj.insert("Src", QJsonValue::fromVariant(pMData->listDownloads.at(i)));
jsArray.append(recordObj);
}
pObject->insert("Download", jsArray);
}
}
void Utils::objectToMData(QJsonObject *pObject, Utils::MDATA *pMData)
{
pMData->sName = pObject->value("Name").toString();
pMData->sVersion = pObject->value("Version").toString();
pMData->sDate = pObject->value("Date").toString();
pMData->sAuthor = pObject->value("Author").toString();
pMData->sBugreport = pObject->value("Bugreport").toString();
pMData->sInfo = pObject->value("Info").toString();
pMData->nSize = pObject->value("Size").toInt();
pMData->nCompressedSize = pObject->value("CompressedSize").toInt();
pMData->bIs32 = pObject->value("Is32").toBool();
pMData->bIs64 = pObject->value("Is64").toBool();
pMData->sSrc = pObject->value("Src").toString();
pMData->sSHA1 = pObject->value("SHA1").toString();
pMData->sGithub = pObject->value("Github").toString();
pMData->sUpdated = pObject->value("Updated").toString();
// TODO optimize
QJsonArray installArray = pObject->value("Install").toArray();
QJsonArray removeArray = pObject->value("Remove").toArray();
QJsonArray convertArray = pObject->value("Convert").toArray();
QJsonArray downloadArray = pObject->value("Download").toArray();
int nInstallCount = installArray.count();
for (int i = 0; i < nInstallCount; i++) {
QJsonObject recordObj = installArray.at(i).toObject();
HANDLE_RECORD record = {};
objectToHandleRecord(&recordObj, &record);
pMData->listInstallRecords.append(record);
}
int nRemoveCount = removeArray.count();
for (int i = 0; i < nRemoveCount; i++) {
QJsonObject recordObj = removeArray.at(i).toObject();
HANDLE_RECORD record = {};
objectToHandleRecord(&recordObj, &record);
pMData->listRemoveRecords.append(record);
}
int nConvertCount = convertArray.count();
for (int i = 0; i < nConvertCount; i++) {
QJsonObject recordObj = convertArray.at(i).toObject();
HANDLE_RECORD record = {};
objectToHandleRecord(&recordObj, &record);
pMData->listConvertRecords.append(record);
}
int nDownloadCount = downloadArray.count();
for (int i = 0; i < nDownloadCount; i++) {
QJsonObject recordObj = downloadArray.at(i).toObject();
QString sRecord = recordObj.value("Src").toString();
pMData->listDownloads.append(sRecord);
}
}
void Utils::handleRecordToObject(Utils::HANDLE_RECORD *pHandleRecord, QJsonObject *pObject)
{
pObject->insert("Path", QJsonValue::fromVariant(pHandleRecord->sPath));
pObject->insert("Action", QJsonValue::fromVariant(actionIdToString(pHandleRecord->action)));
if (pHandleRecord->sSHA1 != "") {
pObject->insert("SHA1", QJsonValue::fromVariant(pHandleRecord->sSHA1));
}
if (pHandleRecord->sSrc != "") {
pObject->insert("Src", QJsonValue::fromVariant(pHandleRecord->sSrc));
}
if (pHandleRecord->sPattern != "") {
pObject->insert("Pattern", QJsonValue::fromVariant(pHandleRecord->sPattern));
}
}
void Utils::objectToHandleRecord(QJsonObject *pObject, Utils::HANDLE_RECORD *pHandleRecord)
{
pHandleRecord->sSrc = pObject->value("Src").toString();
pHandleRecord->sPath = pObject->value("Path").toString();
pHandleRecord->sSHA1 = pObject->value("SHA1").toString();
pHandleRecord->sPattern = pObject->value("Pattern").toString();
pHandleRecord->action = stringToActionId(pObject->value("Action").toString());
}
QMap<QString, Utils::STATUS> Utils::getModulesStatusMap(QString sDataPath, QList<Utils::MDATA> *pServerList, QList<Utils::MDATA> *pInstalled)
{
QMap<QString, Utils::STATUS> mapResult;
int nCount = pServerList->count();
for (int i = 0; i < nCount; i++) {
STATUS status = {};
if (pServerList->at(i).sDate != "") {
status.bInstall = true;
}
status.sServerListDate = pServerList->at(i).sDate;
status.sServerListVersion = pServerList->at(i).sVersion;
status.webRecord.sName = pServerList->at(i).sName;
status.webRecord.sFileName = getModuleFileName(sDataPath, pServerList->at(i).sName);
status.webRecord.sLink = pServerList->at(i).sSrc;
mapResult.insert(pServerList->at(i).sName, status);
}
nCount = pInstalled->count();
for (int i = 0; i < nCount; i++) {
QString sName = pInstalled->at(i).sName;
bool bIsServerList = mapResult.contains(sName);
STATUS status = {};
if (bIsServerList) {
status = mapResult.value(sName);
}
status.webRecord.sName = pInstalled->at(i).sName;
status.bInstall = false;
status.bRemove = true;
status.sInstalledDate = pInstalled->at(i).sDate;
status.sInstalledVersion = pInstalled->at(i).sVersion;
if (bIsServerList) {
if ((status.sServerListDate > status.sInstalledDate) && (status.sServerListVersion != status.sInstalledVersion) && (status.sServerListDate != "")) {
status.bUpdate = true;
}
if ((status.sServerListDate > status.sInstalledDate) && (status.sServerListVersion == "snapshot") && (status.sServerListDate != "")) {
status.bUpdate = true; // For x64dbgcote
}
}
mapResult.insert(pInstalled->at(i).sName, status);
}
return mapResult;
}
Utils::MODULES_DATA Utils::getModulesData(QString sDataPath)
{
MODULES_DATA result = {};
result.listServerList = Utils::getModulesFromJSONFile(Utils::getServerListFileName(sDataPath));
result.listInstalled = Utils::getInstalledModules(sDataPath, sDataPath);
result.mapStatus = getModulesStatusMap(sDataPath, &result.listServerList, &result.listInstalled);
result.listUpdates = getUpdates(&result.mapStatus);
return result;
}
QList<Utils::WEB_RECORD> Utils::getUpdates(QMap<QString, STATUS> *pMapStatus)
{
QList<Utils::WEB_RECORD> listResult;
QList<STATUS> listStatuses = pMapStatus->values();
int nCount = listStatuses.count();
for (int i = 0; i < nCount; i++) {
if (listStatuses.at(i).bUpdate) {
WEB_RECORD record = listStatuses.at(i).webRecord;
listResult.append(record);
}
}
return listResult;
}
QString Utils::getInstalledJsonFileName(QString sDataPath, QString sName)
{
return XBinary::convertPathName(sDataPath) + QDir::separator() + "installed" + QDir::separator() + QString("%1.json").arg(sName);
}
QString Utils::getServerListFileName(QString sDataPath)
{
return XBinary::convertPathName(sDataPath) + QDir::separator() + "list.json";
}
QString Utils::getServerLastestListFileName(QString sDataPath)
{
return XBinary::convertPathName(sDataPath) + QDir::separator() + "list.lastest.json";
}
QString Utils::getModuleFileName(QString sDataPath, QString sName)
{
return XBinary::convertPathName(sDataPath) + QDir::separator() + "modules" + QDir::separator() + QString("%1.x64dbg.zip").arg(sName);
}
QString Utils::getConvertPath(QString sDataPath, QString sName)
{
return XBinary::convertPathName(sDataPath) + QDir::separator() + "modules" + QDir::separator() + QString("%1").arg(sName);
}
QString Utils::getConvertDownloadFileName(QString sDataPath, QString sName, QString sPattern)
{
QString sResult;
QString sConvertPath = getConvertPath(sDataPath, sName);
QDir dir(sConvertPath);
QList<QFileInfo> listFiles = dir.entryInfoList(QDir::Files);
int nCount = listFiles.count();
for (int i = 0; i < nCount; i++) {
QString sName = listFiles.at(i).fileName();
if (sName.contains(sPattern)) {
sResult = listFiles.at(i).absoluteFilePath();
break;
}
}
return sResult;
}
QString Utils::getConvertModulePath(QString sDataPath, QString sName)
{
return XBinary::convertPathName(sDataPath) + QDir::separator() + "modules" + QDir::separator() + QString("%1").arg(sName) + QDir::separator() + "module";
}
Utils::MDATA Utils::getMDataByName(QList<MDATA> *pServerList, QString sName)
{
Utils::MDATA result = {};
int nCount = pServerList->count();
for (int i = 0; i < nCount; i++) {
if (pServerList->at(i).sName == sName) {
result = pServerList->at(i);
break;
}
}
return result;
}
QList<QString> Utils::getNamesFromWebRecords(QList<Utils::WEB_RECORD> *pListWebRecords)
{
QList<QString> listResult;
int nCount = pListWebRecords->count();
for (int i = 0; i < nCount; i++) {
listResult.append(pListWebRecords->at(i).sName);
}
return listResult;
}
Utils::WEB_RECORD Utils::getWebRecordByName(QList<Utils::WEB_RECORD> *pListWebRecords, QString sName)
{
Utils::WEB_RECORD result = {};
int nCount = pListWebRecords->count();
for (int i = 0; i < nCount; i++) {
if (pListWebRecords->at(i).sName == sName) {
result = pListWebRecords->at(i);
break;
}
}
return result;
}
bool Utils::isGithubPresent(QString sServerListFileName)
{
bool bResult = false;
QList<MDATA> listMData = getModulesFromJSONFile(sServerListFileName);
int nCount = listMData.count();
for (int i = 0; i < nCount; i++) {
if (listMData.at(i).sGithub != "") {
bResult = true;
break;
}
}
return bResult;
}
bool Utils::updateJsonFile(QString sFileName, QList<MDATA> listMData)
{
bool bResult = false;
QByteArray baData = XBinary::readFile(sFileName);
QJsonDocument jsDoc = QJsonDocument::fromJson(baData);
QJsonObject rootObj = jsDoc.object();
QJsonObject _rootObj;
_rootObj.insert("Date", rootObj.value("Date"));
QJsonArray arrayModules = rootObj.value("Modules").toArray();
QJsonArray _arrayModules;
int nCount = arrayModules.count();
int _nCount = listMData.count();
for (int i = 0; i < nCount; i++) {
QJsonObject recordObject = arrayModules.at(i).toObject();
MDATA mdata = {};
objectToMData(&recordObject, &mdata);
for (int j = 0; j < _nCount; j++) {
if ((mdata.sName == listMData.at(j).sName) && (listMData.at(j).sDate != "")) {
mdata = listMData.at(j);
mdata.sUpdated = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
break;
}
}
QJsonObject _recordObject;
mDataToObject(&mdata, &_recordObject);
_arrayModules.append(_recordObject);
}
_rootObj.insert("Modules", _arrayModules);
QJsonDocument jsResult(_rootObj);
bResult = XBinary::writeToFile(sFileName, jsResult.toJson(QJsonDocument::Indented));
return bResult;
}
bool Utils::updateServerList(QString sOldFileName, QString sNewFileName)
{
bool bResult = false;
if (!XBinary::isFileExists(sOldFileName)) {
bResult = XBinary::copyFile(sNewFileName, sOldFileName);
} else {
qint64 nJFTimeNew = Utils::getDateFromJSONFile(sNewFileName).toJulianDay();
qint64 nJFTime = Utils::getDateFromJSONFile(sOldFileName).toJulianDay();
if (nJFTimeNew > nJFTime) {
if (XBinary::isFileExists(sOldFileName)) {
XBinary::removeFile(sOldFileName);
}
bResult = XBinary::copyFile(sNewFileName, sOldFileName);
} else {
QList<Utils::MDATA> listMData = Utils::getModulesFromJSONFile(sNewFileName);
bResult = updateJsonFile(sOldFileName, listMData);
}
}
return bResult;
}
QString Utils::actionIdToString(Utils::ACTION action)
{
QString sResult = "unknown";
switch (action) {
case ACTION_UNKNOWN: sResult = QString("unknown"); break;
case ACTION_COPYFILE: sResult = QString("copy_file"); break;
case ACTION_REMOVEFILE: sResult = QString("remove_file"); break;
case ACTION_REMOVEDIRECTORYIFEMPTY: sResult = QString("remove_directory_if_empty"); break;
case ACTION_MAKEDIRECTORY: sResult = QString("make_directory"); break;
case ACTION_UNPACKDIRECTORY: sResult = QString("unpack_directory"); break;
case ACTION_UNPACKFILE: sResult = QString("unpack_file"); break;
}
return sResult;
}
Utils::ACTION Utils::stringToActionId(QString sAction)
{
Utils::ACTION result = ACTION_UNKNOWN;
if (sAction == "unknown") result = ACTION_UNKNOWN;
else if (sAction == "copy_file") result = ACTION_COPYFILE;
else if (sAction == "remove_file") result = ACTION_REMOVEFILE;
else if (sAction == "remove_directory_if_empty") result = ACTION_REMOVEDIRECTORYIFEMPTY;
else if (sAction == "make_directory") result = ACTION_MAKEDIRECTORY;
else if (sAction == "unpack_directory") result = ACTION_UNPACKDIRECTORY;
else if (sAction == "unpack_file") result = ACTION_UNPACKFILE;
return result;
}
bool Utils::checkPattern(QString sString, Utils::MDATA *pMData)
{
bool bResult = false;
int nCount = pMData->listConvertRecords.count();
for (int i = 0; i < nCount; i++) {
if (sString.contains(pMData->listConvertRecords.at(i).sPattern)) {
bResult = true;
break;
}
}
return bResult;
}
void Utils::_getRecords(QString sRootPath, QString sCurrentPath, QList<Utils::RECORD> *pListRecords)
{
QFileInfo fi(sCurrentPath);
RECORD record = {};
record.bIsFile = true;
record.sFullPath = fi.absoluteFilePath();
record.sPath = record.sFullPath;
record.bIsFile = fi.isFile();
record.sPath = record.sPath.remove(0, sRootPath.size() + 1);
if (record.sPath != "") {
pListRecords->append(record);
}
if (!record.bIsFile) {
QDir dir(sCurrentPath);
QFileInfoList eil = dir.entryInfoList();
int nCount = eil.count();
for (int i = 0; i < nCount; i++) {
QString sFN = eil.at(i).fileName();
if ((sFN != ".") && (sFN != "..")) {
_getRecords(sRootPath, eil.at(i).absoluteFilePath(), pListRecords);
}
}
}
}