Use QSaveFile on file write (#6107)

Updating to use the recommended method QSaveFile instead of QFile.
Hopefully fix issue where LMMS in rare occasions would produce empty
files on save. 

---------

Co-authored-by: Kevin Zander <veratil@gmail.com>
This commit is contained in:
Oskar Wallgren
2023-11-03 22:37:19 +01:00
committed by GitHub
parent fccbe5d517
commit 7839a5760c

View File

@@ -35,6 +35,7 @@
#include <QFileInfo>
#include <QDir>
#include <QMessageBox>
#include <QSaveFile>
#include "base64.h"
#include "ConfigManager.h"
@@ -379,12 +380,12 @@ bool DataFile::writeFile(const QString& filename, bool withResources)
}
}
QFile outfile (fullNameTemp);
QSaveFile outfile(fullNameTemp);
if (!outfile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
showError(SongEditor::tr("Could not write file"),
SongEditor::tr("Could not open %1 for writing. You probably are not permitted to"
SongEditor::tr("Could not open %1 for writing. You probably are not permitted to "
"write to this file. Please make sure you have write-access to "
"the file and try again.").arg(fullName));
@@ -405,30 +406,29 @@ bool DataFile::writeFile(const QString& filename, bool withResources)
write( ts );
}
outfile.close();
// make sure the file has been written correctly
if( QFileInfo( outfile.fileName() ).size() > 0 )
if (!outfile.commit())
{
if( ConfigManager::inst()->value( "app", "disablebackup" ).toInt() )
{
// remove current file
QFile::remove( fullName );
}
else
{
// remove old backup file
QFile::remove( fullNameBak );
// move current file to backup file
QFile::rename( fullName, fullNameBak );
}
// move temporary file to current file
QFile::rename( fullNameTemp, fullName );
return true;
showError(SongEditor::tr("Could not write file"),
SongEditor::tr("An unknown error has occured and the file could not be saved."));
return false;
}
return false;
if (ConfigManager::inst()->value("app", "disablebackup").toInt())
{
// remove current file
QFile::remove(fullName);
}
else
{
// remove old backup file
QFile::remove(fullNameBak);
// move current file to backup file
QFile::rename(fullName, fullNameBak);
}
// move temporary file to current file
QFile::rename(fullNameTemp, fullName);
return true;
}