Check project version and create backup on save if project has been converted by Kdenlive

parent 3fb2efbb
......@@ -1076,7 +1076,7 @@ void Bin::slotReloadClip()
doc.setContent(&f, false);
f.close();
DocumentChecker d(QUrl::fromLocalFile(path), doc);
if (!d.hasErrorInClips() && doc.documentElement().attribute(QStringLiteral("modified")) == QLatin1String("1")) {
if (!d.hasErrorInClips() && doc.documentElement().hasAttribute(QStringLiteral("modified"))) {
QString backupFile = path + QStringLiteral(".backup");
KIO::FileCopyJob *copyjob = KIO::file_copy(QUrl::fromLocalFile(path), QUrl::fromLocalFile(backupFile));
if (copyjob->exec()) {
......@@ -1266,7 +1266,7 @@ void Bin::createClip(const QDomElement &xml)
doc.setContent(&f, false);
f.close();
DocumentChecker d(QUrl::fromLocalFile(path), doc);
if (!d.hasErrorInClips() && doc.documentElement().attribute(QStringLiteral("modified")) == QLatin1String("1")) {
if (!d.hasErrorInClips() && doc.documentElement().hasAttribute(QStringLiteral("modified"))) {
QString backupFile = path + QStringLiteral(".backup");
KIO::FileCopyJob *copyjob = KIO::file_copy(QUrl::fromLocalFile(path), QUrl::fromLocalFile(backupFile));
if (copyjob->exec()) {
......
......@@ -103,7 +103,7 @@ bool DocumentChecker::hasErrorInClips()
storageFolder = projectDir.absolutePath();
EffectsList::setProperty(playlists.at(i).toElement(), QStringLiteral("kdenlive:docproperties.storagefolder"),
projectDir.absoluteFilePath(documentid));
m_doc.documentElement().setAttribute(QStringLiteral("modified"), QStringLiteral("1"));
m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
}
break;
}
......@@ -463,7 +463,7 @@ bool DocumentChecker::hasErrorInClips()
if (max > 0) {
// original doc was modified
m_doc.documentElement().setAttribute(QStringLiteral("modified"), QStringLiteral("1"));
m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
}
// Check clips with available proxies but missing original source clips
......@@ -495,7 +495,7 @@ bool DocumentChecker::hasErrorInClips()
}
if (max > 0) {
// original doc was modified
m_doc.documentElement().setAttribute(QStringLiteral("modified"), QStringLiteral("1"));
m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
}
m_ui.treeWidget->resizeColumnToContents(0);
connect(m_ui.recursiveSearch, &QAbstractButton::pressed, this, &DocumentChecker::slotSearchClips);
......@@ -616,7 +616,7 @@ void DocumentChecker::slotSearchClips()
m_ui.recursiveSearch->setEnabled(true);
if (fixed) {
// original doc was modified
m_doc.documentElement().setAttribute(QStringLiteral("modified"), QStringLiteral("1"));
m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
}
checkStatus();
}
......@@ -1076,7 +1076,7 @@ void DocumentChecker::slotDeleteSelected()
}
}
}
m_doc.documentElement().setAttribute(QStringLiteral("modified"), QStringLiteral("1"));
m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
checkStatus();
}
}
......
......@@ -58,7 +58,6 @@ bool DocumentValidator::validate(const double currentVersion)
if (mlt.isNull()) {
return false;
}
QDomElement kdenliveDoc = mlt.firstChildElement(QStringLiteral("kdenlivedoc"));
QString rootDir = mlt.attribute(QStringLiteral("root"));
if (rootDir == QLatin1String("$CURRENTPATH")) {
......@@ -385,9 +384,9 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
if (!docs.isEmpty()) {
infoXmlNode = m_doc.elementsByTagName(QStringLiteral("kdenlivedoc")).at(0);
infoXml = infoXmlNode.toElement();
infoXml.setAttribute(QStringLiteral("upgraded"), QStringLiteral("1"));
infoXml.setAttribute(QStringLiteral("upgraded"), 1);
}
m_doc.documentElement().setAttribute(QStringLiteral("upgraded"), QStringLiteral("1"));
m_doc.documentElement().setAttribute(QStringLiteral("upgraded"), 1);
if (version <= 0.6) {
QDomElement infoXml_old = infoXmlNode.cloneNode(true).toElement(); // Needed for folders
......
......@@ -48,6 +48,7 @@
#include <KBookmarkManager>
#include <KIO/CopyJob>
#include <KIO/JobUiDelegate>
#include <KIO/FileCopyJob>
#include <KMessageBox>
#include <klocalizedstring.h>
......@@ -78,6 +79,7 @@ KdenliveDoc::KdenliveDoc(const QUrl &url, const QString &projectFolder, QUndoGro
, m_autosave(nullptr)
, m_url(url)
, m_modified(false)
, m_documentOpenStatus(CleanProject)
, m_projectFolder(projectFolder)
{
m_commandStack = std::make_shared<DocUndoStack>(undoGroup);
......@@ -225,10 +227,12 @@ KdenliveDoc::KdenliveDoc(const QUrl &url, const QString &projectFolder, QUndoGro
success = !d.hasErrorInClips();
if (success) {
loadDocumentProperties();
if (m_document.documentElement().attribute(QStringLiteral("modified")) == QLatin1String("1")) {
setModified(true);
}
if (validator.isModified()) {
if (m_document.documentElement().hasAttribute(QStringLiteral("upgraded"))) {
m_documentOpenStatus = UpgradedProject;
pCore->displayMessage(i18n("Your project was upgraded, a backup will be created on next save"), ErrorMessage);
} else if (m_document.documentElement().hasAttribute(QStringLiteral("modified")) || validator.isModified()) {
m_documentOpenStatus = ModifiedProject;
pCore->displayMessage(i18n("Your project was modified on opening, a backup will be created on next save"), ErrorMessage);
setModified(true);
}
}
......@@ -552,8 +556,35 @@ bool KdenliveDoc::saveSceneList(const QString &path, const QString &scene)
// Backup current version
backupLastSavedVersion(path);
QFile file(path);
if (m_documentOpenStatus != CleanProject) {
// create visible backup file and warn user
QString baseFile = path.section(QStringLiteral(".kdenlive"), 0, 0);
int ct = 0;
QString backupFile = baseFile + QStringLiteral("_backup") + QString::number(ct) + QStringLiteral(".kdenlive");
while (QFile::exists(backupFile)) {
ct++;
backupFile = baseFile + QStringLiteral("_backup") + QString::number(ct) + QStringLiteral(".kdenlive");
}
QString message;
if (m_documentOpenStatus == UpgradedProject) {
message = i18n("Your project file was upgraded to the latest Kdenlive document version.\nTo make sure you don't lose data, a backup copy called %1 "
"was created.",
backupFile);
} else {
message = i18n("Your project file was modified by Kdenlive.\nTo make sure you don't lose data, a backup copy called %1 was created.", backupFile);
}
KIO::FileCopyJob *copyjob = KIO::file_copy(QUrl::fromLocalFile(path), QUrl::fromLocalFile(backupFile));
if (copyjob->exec()) {
KMessageBox::information(QApplication::activeWindow(), message);
m_documentOpenStatus = CleanProject;
} else {
KMessageBox::information(
QApplication::activeWindow(), i18n("Your project file was upgraded to the latest Kdenlive document version, but it was not possible to create the backup copy %1.",
backupFile));
}
}
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qCWarning(KDENLIVE_LOG) << "////// ERROR writing to file: " << path;
KMessageBox::error(QApplication::activeWindow(), i18n("Cannot write to file %1", path));
......
......@@ -189,6 +189,10 @@ private:
/** @brief Tells whether the current document has been changed after being saved. */
bool m_modified;
/** @brief Tells whether the current document was modified by Kdenlive on opening, and a backup should be created on save. */
enum DOCSTATUS {CleanProject, ModifiedProject, UpgradedProject};
DOCSTATUS m_documentOpenStatus;
/** @brief The project folder, used to store project files (titles, effects...). */
QString m_projectFolder;
QList<int> m_undoChunks;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment