-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProfileInterface.cpp
2408 lines (2260 loc) · 94.8 KB
/
ProfileInterface.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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* rdr2view Red Dead Redemption 2 Profile Viewer
* Copyright (C) 2016-2019 Syping
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "ProfileInterface.h"
#include "ui_ProfileInterface.h"
#include "PlayerListDialog.h"
#include "SidebarGenerator.h"
#include "SnapmaticWidget.h"
#include "DatabaseThread.h"
#include "SavegameWidget.h"
#include "PictureDialog.h"
#include "PictureExport.h"
#include "StandardPaths.h"
#include "ProfileLoader.h"
#include "ExportThread.h"
#include "ImportDialog.h"
#include "UiModLabel.h"
#include "pcg_basic.h"
#include "AppEnv.h"
#include "config.h"
#include <QNetworkAccessManager>
#include <QProgressDialog>
#include <QNetworkRequest>
#include <QStringBuilder>
#include <QNetworkReply>
#include <QImageReader>
#include <QProgressBar>
#include <QInputDialog>
#include <QPushButton>
#include <QSpacerItem>
#include <QMessageBox>
#include <QMouseEvent>
#include <QFileDialog>
#include <QVBoxLayout>
#include <QEventLoop>
#include <QScrollBar>
#include <QClipboard>
#include <QFileInfo>
#include <QPainter>
#include <QAction>
#include <QDebug>
#include <QColor>
#include <QTimer>
#include <QStyle>
#include <QFile>
#include <QUrl>
#include <QDir>
#include <random>
#include <ctime>
#ifdef GTA5SYNC_TELEMETRY
#include "TelemetryClass.h"
#include <QJsonDocument>
#include <QJsonObject>
#endif
#define importTimeFormat "HHmmss"
#define findRetryLimit 500
ProfileInterface::ProfileInterface(ProfileDatabase *profileDB, CrewDatabase *crewDB, DatabaseThread *threadDB, QWidget *parent) :
QWidget(parent), profileDB(profileDB), crewDB(crewDB), threadDB(threadDB),
ui(new Ui::ProfileInterface)
{
ui->setupUi(this);
ui->cmdImport->setEnabled(false);
ui->cmdCloseProfile->setEnabled(false);
loadingStr = ui->labProfileLoading->text();
enabledPicStr = tr("Enabled pictures: %1 of %2");
selectedWidgts = 0;
profileFolder = "";
contextMenuOpened = false;
isProfileLoaded = false;
previousWidget = nullptr;
profileLoader = nullptr;
saSpacerItem = nullptr;
updatePalette();
QString appVersion = GTA5SYNC_APPVER;
#ifndef GTA5SYNC_BUILDTYPE_REL
#ifdef GTA5SYNC_COMMIT
if (!appVersion.contains("-")) { appVersion = appVersion % "-" % GTA5SYNC_COMMIT; }
#endif
#endif
ui->labVersion->setText(QString("%1 %2").arg(GTA5SYNC_APPSTR, appVersion));
ui->saProfileContent->setFilesDropEnabled(true);
ui->saProfileContent->setImageDropEnabled(true);
// Set Icon for Close Button
if (QIcon::hasThemeIcon("dialog-close"))
{
ui->cmdCloseProfile->setIcon(QIcon::fromTheme("dialog-close"));
}
else if (QIcon::hasThemeIcon("gtk-close"))
{
ui->cmdCloseProfile->setIcon(QIcon::fromTheme("gtk-close"));
}
// Set Icon for Import Button
if (QIcon::hasThemeIcon("document-import"))
{
ui->cmdImport->setIcon(QIcon::fromTheme("document-import"));
}
else if (QIcon::hasThemeIcon("document-open"))
{
ui->cmdImport->setIcon(QIcon::fromTheme("document-open"));
}
// DPI calculation
qreal screenRatio = AppEnv::screenRatio();
#ifndef Q_OS_MAC
ui->hlButtons->setSpacing(6 * screenRatio);
ui->hlButtons->setContentsMargins(9 * screenRatio, 9 * screenRatio, 9 * screenRatio, 9 * screenRatio);
#else
if (QApplication::style()->objectName() == "macintosh")
{
ui->hlButtons->setSpacing(6 * screenRatio);
ui->hlButtons->setContentsMargins(9 * screenRatio, 15 * screenRatio, 15 * screenRatio, 17 * screenRatio);
}
else
{
ui->hlButtons->setSpacing(6 * screenRatio);
ui->hlButtons->setContentsMargins(9 * screenRatio, 9 * screenRatio, 9 * screenRatio, 9 * screenRatio);
}
#endif
// Seed RNG
pcg32_srandom_r(&rng, time(NULL), (intptr_t)&rng);
setMouseTracking(true);
installEventFilter(this);
}
ProfileInterface::~ProfileInterface()
{
for (ProfileWidget *widget : widgets.keys())
{
widget->removeEventFilter(this);
widget->disconnect();
delete widget;
}
widgets.clear();
for (SavegameData *savegame : savegames)
{
delete savegame;
}
savegames.clear();
for (SnapmaticPicture *picture : pictures)
{
delete picture;
}
pictures.clear();
delete profileLoader;
delete ui;
}
void ProfileInterface::setProfileFolder(QString folder, QString profile)
{
profileFolder = folder;
profileName = profile;
}
void ProfileInterface::setupProfileInterface()
{
fixedPictures.clear();
ui->labProfileLoading->setText(tr("Loading..."));
profileLoader = new ProfileLoader(profileFolder, crewDB);
QObject::connect(profileLoader, SIGNAL(savegameLoaded(SavegameData*, QString)), this, SLOT(savegameLoaded_event(SavegameData*, QString)));
QObject::connect(profileLoader, SIGNAL(pictureLoaded(SnapmaticPicture*)), this, SLOT(pictureLoaded_event(SnapmaticPicture*)));
QObject::connect(profileLoader, SIGNAL(pictureFixed(SnapmaticPicture*)), this, SLOT(pictureFixed_event(SnapmaticPicture*)));
QObject::connect(profileLoader, SIGNAL(loadingProgress(int,int)), this, SLOT(loadingProgress(int,int)));
QObject::connect(profileLoader, SIGNAL(finished()), this, SLOT(profileLoaded_p()));
profileLoader->start();
}
void ProfileInterface::savegameLoaded_event(SavegameData *savegame, QString savegamePath)
{
savegameLoaded(savegame, savegamePath, false);
}
void ProfileInterface::savegameLoaded(SavegameData *savegame, QString savegamePath, bool inserted)
{
SavegameWidget *sgdWidget = new SavegameWidget(this);
sgdWidget->setSavegameData(savegame, savegamePath);
sgdWidget->setContentMode(contentMode);
sgdWidget->setMouseTracking(true);
sgdWidget->installEventFilter(this);
widgets[sgdWidget] = "SGD" % QFileInfo(savegamePath).fileName();
savegames += savegame;
if (selectedWidgts != 0 || contentMode == 2) { sgdWidget->setSelectionMode(true); }
QObject::connect(sgdWidget, SIGNAL(savegameDeleted()), this, SLOT(savegameDeleted_event()));
QObject::connect(sgdWidget, SIGNAL(widgetSelected()), this, SLOT(profileWidgetSelected()));
QObject::connect(sgdWidget, SIGNAL(widgetDeselected()), this, SLOT(profileWidgetDeselected()));
QObject::connect(sgdWidget, SIGNAL(allWidgetsSelected()), this, SLOT(selectAllWidgets()));
QObject::connect(sgdWidget, SIGNAL(allWidgetsDeselected()), this, SLOT(deselectAllWidgets()));
QObject::connect(sgdWidget, SIGNAL(contextMenuTriggered(QContextMenuEvent*)), this, SLOT(contextMenuTriggeredSGD(QContextMenuEvent*)));
if (inserted) { insertSavegameIPI(sgdWidget); }
}
void ProfileInterface::pictureLoaded_event(SnapmaticPicture *picture)
{
pictureLoaded(picture, false);
}
void ProfileInterface::pictureFixed_event(SnapmaticPicture *picture)
{
QString fixedPicture = picture->getPictureStr() % " (" % picture->getPictureTitl() % ")";
fixedPictures << fixedPicture;
}
void ProfileInterface::pictureLoaded(SnapmaticPicture *picture, bool inserted)
{
SnapmaticWidget *picWidget = new SnapmaticWidget(profileDB, crewDB, threadDB, profileName, this);
picWidget->setSnapmaticPicture(picture);
picWidget->setContentMode(contentMode);
picWidget->setMouseTracking(true);
picWidget->installEventFilter(this);
widgets[picWidget] = "PIC" % picture->getPictureSortStr();
pictures += picture;
if (selectedWidgts != 0 || contentMode == 2) { picWidget->setSelectionMode(true); }
QObject::connect(picWidget, SIGNAL(pictureDeleted()), this, SLOT(pictureDeleted_event()));
QObject::connect(picWidget, SIGNAL(widgetSelected()), this, SLOT(profileWidgetSelected()));
QObject::connect(picWidget, SIGNAL(widgetDeselected()), this, SLOT(profileWidgetDeselected()));
QObject::connect(picWidget, SIGNAL(allWidgetsSelected()), this, SLOT(selectAllWidgets()));
QObject::connect(picWidget, SIGNAL(allWidgetsDeselected()), this, SLOT(deselectAllWidgets()));
QObject::connect(picWidget, SIGNAL(nextPictureRequested(QWidget*)), this, SLOT(dialogNextPictureRequested(QWidget*)));
QObject::connect(picWidget, SIGNAL(previousPictureRequested(QWidget*)), this, SLOT(dialogPreviousPictureRequested(QWidget*)));
QObject::connect(picWidget, SIGNAL(contextMenuTriggered(QContextMenuEvent*)), this, SLOT(contextMenuTriggeredPIC(QContextMenuEvent*)));
if (inserted) { insertSnapmaticIPI(picWidget); }
}
void ProfileInterface::loadingProgress(int value, int maximum)
{
ui->pbPictureLoading->setMaximum(maximum);
ui->pbPictureLoading->setValue(value);
ui->labProfileLoading->setText(loadingStr.arg(QString::number(value), QString::number(maximum)));
}
void ProfileInterface::insertSnapmaticIPI(QWidget *widget)
{
ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(widget);
if (widgets.contains(proWidget))
{
QString widgetKey = widgets[proWidget];
QStringList widgetsKeyList = widgets.values();
QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive);
#if QT_VERSION >= 0x050600
std::sort(pictureKeyList.rbegin(), pictureKeyList.rend());
#else
qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater<QString>());
#endif
int picIndex = pictureKeyList.indexOf(widgetKey);
ui->vlSnapmatic->insertWidget(picIndex, proWidget);
QApplication::processEvents();
ui->saProfile->ensureWidgetVisible(proWidget, 0, 0);
}
}
void ProfileInterface::insertSavegameIPI(QWidget *widget)
{
ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(widget);
if (widgets.contains(proWidget))
{
QString widgetKey = widgets[proWidget];
QStringList widgetsKeyList = widgets.values();
QStringList savegameKeyList = widgetsKeyList.filter("SGD", Qt::CaseSensitive);
#if QT_VERSION >= 0x050600
std::sort(savegameKeyList.begin(), savegameKeyList.end());
#else
qSort(savegameKeyList.begin(), savegameKeyList.end());
#endif
int sgdIndex = savegameKeyList.indexOf(widgetKey);
ui->vlSavegame->insertWidget(sgdIndex, proWidget);
QApplication::processEvents();
ui->saProfile->ensureWidgetVisible(proWidget, 0, 0);
}
}
void ProfileInterface::dialogNextPictureRequested(QWidget *dialog)
{
PictureDialog *picDialog = qobject_cast<PictureDialog*>(dialog);
ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(sender());
if (widgets.contains(proWidget))
{
QString widgetKey = widgets[proWidget];
QStringList widgetsKeyList = widgets.values();
QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive);
#if QT_VERSION >= 0x050600
std::sort(pictureKeyList.rbegin(), pictureKeyList.rend());
#else
qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater<QString>());
#endif
int picIndex;
if (picDialog->isIndexed())
{
picIndex = picDialog->getIndex();
}
else
{
picIndex = pictureKeyList.indexOf(widgetKey);
}
picIndex++;
if (pictureKeyList.length() > picIndex)
{
QString newWidgetKey = pictureKeyList.at(picIndex);
SnapmaticWidget *picWidget = (SnapmaticWidget*)widgets.key(newWidgetKey);
//picDialog->setMaximumHeight(QWIDGETSIZE_MAX);
picDialog->setSnapmaticPicture(picWidget->getPicture(), picIndex);
//picDialog->setMaximumHeight(picDialog->height());
}
}
}
void ProfileInterface::dialogPreviousPictureRequested(QWidget *dialog)
{
PictureDialog *picDialog = qobject_cast<PictureDialog*>(dialog);
ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(sender());
if (widgets.contains(proWidget))
{
QString widgetKey = widgets[proWidget];
QStringList widgetsKeyList = widgets.values();
QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive);
#if QT_VERSION >= 0x050600
std::sort(pictureKeyList.rbegin(), pictureKeyList.rend());
#else
qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater<QString>());
#endif
int picIndex;
if (picDialog->isIndexed())
{
picIndex = picDialog->getIndex();
}
else
{
picIndex = pictureKeyList.indexOf(widgetKey);
}
if (picIndex > 0)
{
picIndex--;
QString newWidgetKey = pictureKeyList.at(picIndex );
SnapmaticWidget *picWidget = (SnapmaticWidget*)widgets.key(newWidgetKey);
//picDialog->setMaximumHeight(QWIDGETSIZE_MAX);
picDialog->setSnapmaticPicture(picWidget->getPicture(), picIndex);
//picDialog->setMaximumHeight(picDialog->height());
}
}
}
void ProfileInterface::sortingProfileInterface()
{
ui->vlSavegame->setEnabled(false);
ui->vlSnapmatic->setEnabled(false);
QStringList widgetsKeyList = widgets.values();
#if QT_VERSION >= 0x050600
std::sort(widgetsKeyList.begin(), widgetsKeyList.end());
#else
qSort(widgetsKeyList.begin(), widgetsKeyList.end());
#endif
for (QString widgetKey : widgetsKeyList)
{
ProfileWidget *widget = widgets.key(widgetKey);
if (widget->getWidgetType() == "SnapmaticWidget")
{
ui->vlSnapmatic->insertWidget(0, widget);
}
else if (widget->getWidgetType() == "SavegameWidget")
{
ui->vlSavegame->addWidget(widget);
}
}
ui->vlSavegame->setEnabled(true);
ui->vlSnapmatic->setEnabled(true);
QApplication::processEvents();
}
void ProfileInterface::profileLoaded_p()
{
sortingProfileInterface();
saSpacerItem = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
ui->saProfileContent->layout()->addItem(saSpacerItem);
ui->swProfile->setCurrentWidget(ui->pageProfile);
ui->cmdCloseProfile->setEnabled(true);
ui->cmdImport->setEnabled(true);
isProfileLoaded = true;
emit profileLoaded();
if (!fixedPictures.isEmpty())
{
int fixedInt = 0;
QString fixedStr;
for (QString fixedPicture : fixedPictures)
{
if (fixedInt != 0) { fixedStr += "<br>"; }
fixedStr += fixedPicture;
fixedInt++;
}
QMessageBox::information(this, tr("Snapmatic Loader"), tr("<h4>Following Snapmatic Pictures got repaired</h4>%1").arg(fixedStr));
}
}
void ProfileInterface::savegameDeleted_event()
{
savegameDeleted(qobject_cast<SavegameWidget*>(sender()), true);
}
void ProfileInterface::savegameDeleted(SavegameWidget *sgdWidget, bool isRemoteEmited)
{
SavegameData *savegame = sgdWidget->getSavegame();
if (sgdWidget->isSelected()) { sgdWidget->setSelected(false); }
widgets.remove(sgdWidget);
sgdWidget->disconnect();
sgdWidget->removeEventFilter(this);
if (sgdWidget == previousWidget)
{
previousWidget = nullptr;
}
// Deleting when the widget did send a event cause a crash
isRemoteEmited ? sgdWidget->deleteLater() : delete sgdWidget;
savegames.removeAll(savegame);
delete savegame;
}
void ProfileInterface::pictureDeleted_event()
{
pictureDeleted(qobject_cast<SnapmaticWidget*>(sender()), true);
}
void ProfileInterface::pictureDeleted(SnapmaticWidget *picWidget, bool isRemoteEmited)
{
SnapmaticPicture *picture = picWidget->getPicture();
if (picWidget->isSelected()) { picWidget->setSelected(false); }
widgets.remove(picWidget);
picWidget->disconnect();
picWidget->removeEventFilter(this);
if (picWidget == previousWidget)
{
previousWidget = nullptr;
}
// Deleting when the widget did send a event cause a crash
isRemoteEmited ? picWidget->deleteLater() : delete picWidget;
pictures.removeAll(picture);
delete picture;
}
void ProfileInterface::on_cmdCloseProfile_clicked()
{
emit profileClosed();
}
void ProfileInterface::on_cmdImport_clicked()
{
QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
settings.beginGroup("FileDialogs");
bool dontUseNativeDialog = settings.value("DontUseNativeDialog", false).toBool();
settings.beginGroup("ImportCopy");
fileDialogPreOpen: //Work?
QFileDialog fileDialog(this);
fileDialog.setFileMode(QFileDialog::ExistingFiles);
fileDialog.setViewMode(QFileDialog::Detail);
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setOption(QFileDialog::DontUseNativeDialog, dontUseNativeDialog);
fileDialog.setWindowFlags(fileDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
fileDialog.setWindowTitle(tr("Import..."));
fileDialog.setLabelText(QFileDialog::Accept, tr("Import..."));
// Getting readable Image formats
QString imageFormatsStr = " ";
for (QByteArray imageFormat : QImageReader::supportedImageFormats())
{
imageFormatsStr += QString("*.") % QString::fromUtf8(imageFormat).toLower() % " ";
}
QString importableFormatsStr = QString("*.r5e SRDR* PRDR*");
if (!imageFormatsStr.trimmed().isEmpty())
{
importableFormatsStr = QString("*.r5e%1SRDR* PRDR*").arg(imageFormatsStr);
}
QStringList filters;
filters << tr("Importable files (%1)").arg(importableFormatsStr);
filters << tr("RDR 2 Export (*.r5e)");
filters << tr("Savegames files (SRDR*)");
filters << tr("Snapmatic pictures (PRDR*)");
filters << tr("All image files (%1)").arg(imageFormatsStr.trimmed());
filters << tr("All files (**)");
fileDialog.setNameFilters(filters);
QList<QUrl> sidebarUrls = SidebarGenerator::generateSidebarUrls(fileDialog.sidebarUrls());
fileDialog.setSidebarUrls(sidebarUrls);
fileDialog.setDirectory(settings.value(profileName % "+Directory", StandardPaths::documentsLocation()).toString());
fileDialog.restoreGeometry(settings.value(profileName % "+Geometry", "").toByteArray());
if (fileDialog.exec())
{
QStringList selectedFiles = fileDialog.selectedFiles();
if (selectedFiles.length() == 1)
{
QString selectedFile = selectedFiles.at(0);
QDateTime importDateTime = QDateTime::currentDateTime();
if (!importFile(selectedFile, importDateTime, true)) goto fileDialogPreOpen; //Work?
}
else if (selectedFiles.length() > 1)
{
importFilesProgress(selectedFiles);
}
else
{
QMessageBox::warning(this, tr("Import..."), tr("No valid file is selected"));
goto fileDialogPreOpen; //Work?
}
}
settings.setValue(profileName % "+Geometry", fileDialog.saveGeometry());
settings.setValue(profileName % "+Directory", fileDialog.directory().absolutePath());
settings.endGroup();
settings.endGroup();
}
bool ProfileInterface::importFilesProgress(QStringList selectedFiles)
{
int maximumId = selectedFiles.length();
int overallId = 0;
QString errorStr;
QStringList failed;
// Progress dialog
QProgressDialog pbDialog(this);
pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
pbDialog.setWindowTitle(tr("Import..."));
pbDialog.setLabelText(tr("Import file %1 of %2 files").arg(QString::number(1), QString::number(maximumId)));
pbDialog.setRange(1, maximumId);
pbDialog.setValue(1);
pbDialog.setModal(true);
QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
pbBtn.at(0)->setDisabled(true);
QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
pbBar.at(0)->setTextVisible(false);
pbDialog.setAutoClose(false);
pbDialog.show();
// THREADING HERE PLEASE
QDateTime importDateTime = QDateTime::currentDateTime();
for (QString selectedFile : selectedFiles)
{
overallId++;
pbDialog.setValue(overallId);
pbDialog.setLabelText(tr("Import file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
importDateTime = QDateTime::currentDateTime();
if (!importFile(selectedFile, importDateTime, false))
{
failed << QFileInfo(selectedFile).fileName();
}
}
pbDialog.close();
for (QString curErrorStr : failed)
{
errorStr += ", " % curErrorStr;
}
if (errorStr != "")
{
errorStr.remove(0, 2);
QMessageBox::warning(this, tr("Import..."), tr("Import failed with...\n\n%1").arg(errorStr));
return false;
}
return true;
}
bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime, bool notMultiple)
{
QString selectedFileName = QFileInfo(selectedFile).fileName();
if (QFile::exists(selectedFile))
{
if ((selectedFileName.left(4) == "PRDR" && !selectedFileName.contains('.')) || selectedFileName.right(4) == ".r5e")
{
SnapmaticPicture *picture = new SnapmaticPicture(selectedFile);
if (picture->readingPicture(true, true, true))
{
bool success = importSnapmaticPicture(picture, notMultiple);
if (!success) delete picture;
#ifdef GTA5SYNC_TELEMETRY
if (success && notMultiple)
{
QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
telemetrySettings.beginGroup("Telemetry");
bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
telemetrySettings.endGroup();
if (pushUsageData && Telemetry->canPush())
{
QJsonDocument jsonDocument;
QJsonObject jsonObject;
jsonObject["Type"] = "ImportSuccess";
jsonObject["ImportSize"] = QString::number(picture->getContentMaxLength());
jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
jsonObject["ImportType"] = "Snapmatic";
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
}
#endif
return success;
}
else
{
if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("Failed to read Snapmatic picture"));
delete picture;
return false;
}
}
else if (selectedFileName.left(4) == "SRDR")
{
SavegameData *savegame = new SavegameData(selectedFile);
if (savegame->readingSavegame())
{
bool success = importSavegameData(savegame, selectedFile, notMultiple);
if (!success) delete savegame;
#ifdef GTA5SYNC_TELEMETRY
if (success && notMultiple)
{
QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
telemetrySettings.beginGroup("Telemetry");
bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
telemetrySettings.endGroup();
if (pushUsageData && Telemetry->canPush())
{
QJsonDocument jsonDocument;
QJsonObject jsonObject;
jsonObject["Type"] = "ImportSuccess";
jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
jsonObject["ImportType"] = "Savegame";
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
}
#endif
return success;
}
else
{
if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("Failed to read Savegame file"));
delete savegame;
return false;
}
}
else if (isSupportedImageFile(selectedFileName))
{
SnapmaticPicture *picture = new SnapmaticPicture(":/template/template.r5e");
if (picture->readingPicture(true, false, true, false))
{
if (!notMultiple)
{
QFile snapmaticFile(selectedFile);
if (!snapmaticFile.open(QFile::ReadOnly))
{
delete picture;
return false;
}
QImage snapmaticImage;
QImageReader snapmaticImageReader;
snapmaticImageReader.setDecideFormatFromContent(true);
snapmaticImageReader.setDevice(&snapmaticFile);
if (!snapmaticImageReader.read(&snapmaticImage))
{
delete picture;
return false;
}
QString customImageTitle;
QSize snapmaticResolution = SnapmaticPicture::getSnapmaticResolution();
QPixmap snapmaticPixmap(snapmaticResolution);
snapmaticPixmap.fill(Qt::black);
QPainter snapmaticPainter(&snapmaticPixmap);
if (snapmaticImage.height() == snapmaticImage.width())
{
// Avatar mode
int diffWidth = 0;
int diffHeight = 0;
snapmaticImage = snapmaticImage.scaled(470, 470, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (snapmaticImage.width() > snapmaticImage.height())
{
diffHeight = 470 - snapmaticImage.height();
diffHeight = diffHeight / 2;
}
else if (snapmaticImage.width() < snapmaticImage.height())
{
diffWidth = 470 - snapmaticImage.width();
diffWidth = diffWidth / 2;
}
snapmaticPainter.drawImage(145 + diffWidth, 66 + diffHeight, snapmaticImage);
customImageTitle = ImportDialog::tr("Custom Avatar", "Custom Avatar Description in SC, don't use Special Character!");
}
else
{
// Picture mode
int diffWidth = 0;
int diffHeight = 0;
snapmaticImage = snapmaticImage.scaled(snapmaticResolution, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (snapmaticImage.width() != snapmaticResolution.height())
{
diffWidth = snapmaticResolution.height() - snapmaticImage.width();
diffWidth = diffWidth / 2;
}
else if (snapmaticImage.height() != snapmaticResolution.width())
{
diffHeight = snapmaticResolution.width() - snapmaticImage.height();
diffHeight = diffHeight / 2;
}
snapmaticPainter.drawImage(0 + diffWidth, 0 + diffHeight, snapmaticImage);
customImageTitle = ImportDialog::tr("Custom Picture", "Custom Picture Description in SC, don't use Special Character!");
}
snapmaticPainter.end();
if (!picture->setImage(snapmaticPixmap.toImage()))
{
delete picture;
return false;
}
SnapmaticProperties spJson = picture->getSnapmaticProperties();
spJson.uid = getRandomUid();
bool fExists = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid));
bool fExistsBackup = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".bak");
bool fExistsHidden = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".hidden");
int cEnough = 0;
while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
{
spJson.uid = getRandomUid();
fExists = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid));
fExistsBackup = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".bak");
fExistsHidden = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".hidden");
cEnough++;
}
spJson.createdDateTime = importDateTime;
#if QT_VERSION >= 0x060000
spJson.createdTimestamp = QString::number(spJson.createdDateTime.toSecsSinceEpoch()).toUInt();
#else
spJson.createdTimestamp = spJson.createdDateTime.toTime_t();
#endif
picture->setSnapmaticProperties(spJson);
picture->setPicFileName(QString("PRDR3%1").arg(QString::number(spJson.uid)));
picture->setPictureTitle(customImageTitle);
picture->updateStrings();
bool success = importSnapmaticPicture(picture, notMultiple);
if (!success) delete picture;
return success;
}
else
{
bool success = false;
QFile snapmaticFile(selectedFile);
if (!snapmaticFile.open(QFile::ReadOnly))
{
QMessageBox::warning(this, tr("Import..."), tr("Can't import %1 because file can't be open").arg("\""+selectedFileName+"\""));
delete picture;
return false;
}
QImage *snapmaticImage = new QImage();
QImageReader snapmaticImageReader;
snapmaticImageReader.setDecideFormatFromContent(true);
snapmaticImageReader.setDevice(&snapmaticFile);
if (!snapmaticImageReader.read(snapmaticImage))
{
QMessageBox::warning(this, tr("Import..."), tr("Can't import %1 because file can't be parsed properly").arg("\""+selectedFileName+"\""));
delete snapmaticImage;
delete picture;
return false;
}
ImportDialog *importDialog = new ImportDialog(profileName, this);
importDialog->setImage(snapmaticImage);
importDialog->setModal(true);
importDialog->show();
importDialog->exec();
if (importDialog->isImportAgreed())
{
if (picture->setImage(importDialog->image()))
{
SnapmaticProperties spJson = picture->getSnapmaticProperties();
spJson.uid = getRandomUid();
bool fExists = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid));
bool fExistsBackup = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".bak");
bool fExistsHidden = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".hidden");
int cEnough = 0;
while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
{
spJson.uid = getRandomUid();
fExists = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid));
fExistsBackup = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".bak");
fExistsHidden = QFile::exists(profileFolder % "/PRDR3" % QString::number(spJson.uid) % ".hidden");
cEnough++;
}
spJson.createdDateTime = importDateTime;
#if QT_VERSION >= 0x060000
spJson.createdTimestamp = QString::number(spJson.createdDateTime.toSecsSinceEpoch()).toUInt();
#else
spJson.createdTimestamp = spJson.createdDateTime.toTime_t();
#endif
picture->setSnapmaticProperties(spJson);
picture->setPicFileName(QString("PRDR3%1").arg(QString::number(spJson.uid)));
picture->setPictureTitle(importDialog->getImageTitle());
picture->updateStrings();
success = importSnapmaticPicture(picture, notMultiple);
#ifdef GTA5SYNC_TELEMETRY
if (success)
{
QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
telemetrySettings.beginGroup("Telemetry");
bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
telemetrySettings.endGroup();
if (pushUsageData && Telemetry->canPush())
{
QJsonDocument jsonDocument;
QJsonObject jsonObject;
jsonObject["Type"] = "ImportSuccess";
jsonObject["ExtraFlag"] = "Dialog";
jsonObject["ImportSize"] = QString::number(picture->getContentMaxLength());
jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
jsonObject["ImportType"] = "Image";
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
}
#endif
}
}
else
{
delete picture;
success = true;
}
delete importDialog;
if (!success) delete picture;
return success;
}
}
else
{
delete picture;
return false;
}
}
else
{
SnapmaticPicture *picture = new SnapmaticPicture(selectedFile);
SavegameData *savegame = new SavegameData(selectedFile);
if (picture->readingPicture())
{
bool success = importSnapmaticPicture(picture, notMultiple);
delete savegame;
if (!success) delete picture;
#ifdef GTA5SYNC_TELEMETRY
if (success && notMultiple)
{
QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
telemetrySettings.beginGroup("Telemetry");
bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
telemetrySettings.endGroup();
if (pushUsageData && Telemetry->canPush())
{
QJsonDocument jsonDocument;
QJsonObject jsonObject;
jsonObject["Type"] = "ImportSuccess";
jsonObject["ImportSize"] = QString::number(picture->getContentMaxLength());
jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
jsonObject["ImportType"] = "Snapmatic";
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
}
#endif
return success;
}
else if (savegame->readingSavegame())
{
bool success = importSavegameData(savegame, selectedFile, notMultiple);
delete picture;
if (!success) delete savegame;
#ifdef GTA5SYNC_TELEMETRY
if (success && notMultiple)
{
QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
telemetrySettings.beginGroup("Telemetry");
bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
telemetrySettings.endGroup();
if (pushUsageData && Telemetry->canPush())
{
QJsonDocument jsonDocument;
QJsonObject jsonObject;
jsonObject["Type"] = "ImportSuccess";
jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
jsonObject["ImportType"] = "Savegame";
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
}
#endif
return success;
}
else
{
#ifdef GTA5SYNC_DEBUG
qDebug() << "ImportError SnapmaticPicture" << picture->getLastStep();
qDebug() << "ImportError SavegameData" << savegame->getLastStep();
#endif
delete picture;
delete savegame;
if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("Can't import %1 because file format can't be detected").arg("\""+selectedFileName+"\""));
return false;
}
}
}
if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("No valid file is selected"));
return false;
}
bool ProfileInterface::importUrls(const QMimeData *mimeData)
{
QStringList pathList;
for (QUrl currentUrl : mimeData->urls())
{
if (currentUrl.isLocalFile())
{
pathList += currentUrl.toLocalFile();
}
}
if (pathList.length() == 1)
{
QString selectedFile = pathList.at(0);
return importFile(selectedFile, QDateTime::currentDateTime(), true);
}
else if (pathList.length() > 1)
{
return importFilesProgress(pathList);
}
return false;
}
bool ProfileInterface::importRemote(QUrl remoteUrl)
{
bool retValue = false;
QDialog urlPasteDialog(this);
#if QT_VERSION >= 0x050000
urlPasteDialog.setObjectName(QStringLiteral("UrlPasteDialog"));
#else
urlPasteDialog.setObjectName(QString::fromUtf8("UrlPasteDialog"));
#endif
urlPasteDialog.setWindowFlags(urlPasteDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
urlPasteDialog.setWindowTitle(tr("Import..."));
urlPasteDialog.setModal(true);
QVBoxLayout urlPasteLayout(&urlPasteDialog);
#if QT_VERSION >= 0x050000
urlPasteLayout.setObjectName(QStringLiteral("UrlPasteLayout"));
#else
urlPasteLayout.setObjectName(QString::fromUtf8("UrlPasteLayout"));
#endif
urlPasteDialog.setLayout(&urlPasteLayout);
UiModLabel urlPasteLabel(&urlPasteDialog);
#if QT_VERSION >= 0x050000
urlPasteLabel.setObjectName(QStringLiteral("UrlPasteLabel"));
#else
urlPasteLabel.setObjectName(QString::fromUtf8("UrlPasteLabel"));
#endif
urlPasteLabel.setText(tr("Prepare Content for Import..."));
urlPasteLayout.addWidget(&urlPasteLabel);
urlPasteDialog.setFixedSize(urlPasteDialog.sizeHint());
urlPasteDialog.show();
QNetworkAccessManager *netManager = new QNetworkAccessManager();
QNetworkRequest netRequest(remoteUrl);