Krita Source Code Documentation
Loading...
Searching...
No Matches
KisBackup.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
5 SPDX-FileCopyrightText: 2006 Allen Winter <winter@kde.org>
6 SPDX-FileCopyrightText: 2006 Gregory S. Hayes <syncomm@kde.org>
7 SPDX-FileCopyrightText: 2006 Jaison Lee <lee.jaison@gmail.com>
8 SPDX-FileCopyrightText: 2011 Romain Perier <bambi@ubuntu.com>
9
10 SPDX-License-Identifier: LGPL-2.0-only
11*/
12
13#include "KisBackup.h"
14
15#include <QDebug>
16#include <QDir>
17#include <QFileInfo>
18
19bool KisBackup::backupFile(const QString &qFilename, const QString &backupDir)
20{
21 return (simpleBackupFile(qFilename, backupDir, QStringLiteral("~")));
22}
23
24bool KisBackup::simpleBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension)
25{
26 QString backupFileName = qFilename + backupExtension;
27
28 if (!backupDir.isEmpty()) {
29 QFileInfo fileInfo(qFilename);
30 backupFileName = backupDir + QLatin1Char('/') + fileInfo.fileName() + backupExtension;
31 }
32
33 // qCDebug(KCOREADDONS_DEBUG) << "KisBackup copying " << qFilename << " to " << backupFileName;
34 QFile::remove(backupFileName);
35 return QFile::copy(qFilename, backupFileName);
36}
37
38bool KisBackup::numberedBackupFile(const QString &qFilename, const QString &backupDir, const QString &backupExtension, const uint maxBackups)
39{
40 QFileInfo fileInfo(qFilename);
41
42 // The backup file name template.
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 // First, search backupDir for numbered backup files to remove.
51 // Remove all with number 'maxBackups' and greater.
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
59 const QStringList nameFilters = QStringList(nameFilter);
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 // sTemp holds the file name, without the ending backupExtension
68 QString sTemp = fi.fileName();
69
70 sTemp.truncate(fi.fileName().length() - backupExtension.length());
71
72 // compute the backup number
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 // Next, rename max-1 to max, max-2 to max-1, etc.
93 QString to = sTemplate.arg(maxBackupFound + 1);
94
95 for (int i = maxBackupFound; i > 0; i--) {
96 QString from = sTemplate.arg(i);
97 // qCDebug(KCOREADDONS_DEBUG) << "KisBackup renaming " << from << " to " << to;
98 QFile::rename(from, to);
99 to = from;
100 }
101
102 // Finally create most recent backup by copying the file to backup number 1.
103 // qCDebug(KCOREADDONS_DEBUG) << "KisBackup copying " << qFilename << " to " << sTemplate.arg(1);
104 bool r = QFile::copy(qFilename, sTemplate.arg(1));
105 return r;
106}
QList< QString > QStringList
unsigned int uint
static bool backupFile(const QString &filename, const QString &backupDir=QString())
Definition KisBackup.cpp:19
static bool simpleBackupFile(const QString &filename, const QString &backupDir=QString(), const QString &backupExtension=QStringLiteral("~"))
Definition KisBackup.cpp:24
static bool numberedBackupFile(const QString &filename, const QString &backupDir=QString(), const QString &backupExtension=QStringLiteral("~"), const uint maxBackups=10)
Definition KisBackup.cpp:38