39{
40 QFileInfo fileInfo(qFilename);
41
42
43 QString sTemplate;
44
45 if (backupDir.isEmpty()) {
46 sTemplate = qFilename + QLatin1String(".%1") + backupExtension;
47 } else {
48 sTemplate = backupDir + QLatin1Char('/') + fileInfo.fileName() + QLatin1String(".%1") + backupExtension;
49 }
50
51
52 QDir
d = backupDir.isEmpty() ? fileInfo.dir() : backupDir;
53 d.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
54
55 QString nameFilter = fileInfo.fileName() + QLatin1String(".*") + backupExtension;
56 nameFilter.replace('[', '*');
57 nameFilter.replace(']', '*');
58
60 d.setNameFilters(nameFilters);
61 d.setSorting(QDir::Name);
62
63 uint maxBackupFound = 0;
64 const QFileInfoList infoList =
d.entryInfoList();
65 for (const QFileInfo &fi : infoList) {
66 if (fi.fileName().endsWith(backupExtension)) {
67
68 QString sTemp = fi.fileName();
69
70 sTemp.truncate(fi.fileName().length() - backupExtension.length());
71
72
73 int idex = sTemp.lastIndexOf(QLatin1Char('.'));
74 if (idex > 0) {
75 bool ok;
76#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
77 const uint num = QStringView(sTemp).mid(idex + 1).toUInt(&ok);
78#else
79 const uint num = sTemp.midRef(idex + 1).toUInt(&ok);
80#endif
81 if (ok) {
82 if (num >= maxBackups) {
83 QFile::remove(fi.filePath());
84 } else {
85 maxBackupFound = qMax(maxBackupFound, num);
86 }
87 }
88 }
89 }
90 }
91
92
93 QString to = sTemplate.arg(maxBackupFound + 1);
94
95 for (int i = maxBackupFound; i > 0; i--) {
96 QString from = sTemplate.arg(i);
97
98 QFile::rename(from, to);
99 to = from;
100 }
101
102
103
104 bool r = QFile::copy(qFilename, sTemplate.arg(1));
106}
QList< QString > QStringList