Skip to content

Commit

Permalink
Use QSaveFile on file write (LMMS#6107)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zonkmachine and Veratil authored Nov 3, 2023
1 parent fccbe5d commit 7839a57
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/core/DataFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <QFileInfo>
#include <QDir>
#include <QMessageBox>
#include <QSaveFile>

#include "base64.h"
#include "ConfigManager.h"
Expand Down Expand Up @@ -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));

Expand All @@ -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 );
showError(SongEditor::tr("Could not write file"),
SongEditor::tr("An unknown error has occured and the file could not be saved."));
return false;
}

return true;
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 false;
return true;
}


Expand Down

0 comments on commit 7839a57

Please sign in to comment.