@@ -274,39 +274,67 @@ bool EditorBase::saveAs(const QString& targetPath)
274274 // watching it, so re-enabling monitor is necessary.
275275 enableFileMonitoring (false );
276276
277- QFile file (actualPath);
278- if (!file.open (QIODevice::WriteOnly))
277+ // If the file already exists, rename it to avoid data losing due to save error
278+ const bool tmpUsed = QFileInfo (actualPath).exists ();
279+ if (tmpUsed)
279280 {
280- QMessageBox::critical (qobject_cast<Application*>(qApp)->getMainWindow (),
281- " Error saving file!" ,
282- " CEED encountered an error trying to save the file " + actualPath);
283- enableFileMonitoring (true );
284- return false ;
281+ QFile prevFile (actualPath);
282+ if (!prevFile.rename (actualPath + " .bak" ))
283+ {
284+ QMessageBox::critical (qobject_cast<Application*>(qApp)->getMainWindow (),
285+ " Error saving file!" ,
286+ " CEED encountered an error trying to save the file " + actualPath);
287+ return false ;
288+ }
285289 }
286290
287- // Do it before obtaining raw data since it may contain relative pathes.
288- // For example imageset XML contains a relative path to underlying image.
289- _filePath = actualPath;
291+ QFile file (actualPath);
292+ if (file.open (QIODevice::WriteOnly))
293+ {
294+ // Do it before obtaining raw data since it may contain relative pathes.
295+ // For example imageset XML contains a relative path to underlying image.
296+ _filePath = actualPath;
290297
291- QByteArray rawData;
292- getRawData (rawData);
293- /*
294- if self.compatibilityManager is not None:
295- outputData = self.compatibilityManager.transform(self.compatibilityManager.EditorNativeType, self.desiredSavingDataType, self.nativeData)
296- */
297- file.write (rawData);
298- file.close ();
298+ QByteArray rawData;
299+ getRawData (rawData);
300+ /*
301+ if self.compatibilityManager is not None:
302+ outputData = self.compatibilityManager.transform(self.compatibilityManager.EditorNativeType, self.desiredSavingDataType, self.nativeData)
303+ */
299304
300- enableFileMonitoring (true );
301- markAsUnchanged ();
305+ const auto written = file.write (rawData);
306+ file.close ();
307+
308+ if (written == rawData.size ())
309+ {
310+ if (tmpUsed) QFile (actualPath + " .bak" ).remove ();
311+ enableFileMonitoring (true );
312+
313+ markAsUnchanged ();
314+
315+ if (prevFilePath != _filePath)
316+ {
317+ _labelText = QFileInfo (_filePath).fileName ();
318+ emit filePathChanged (prevFilePath, _filePath);
319+ }
302320
303- if (prevFilePath != _filePath)
321+ return true ;
322+ }
323+ }
324+
325+ // Something went wrong, show error and restore backup
326+ QMessageBox::critical (qobject_cast<Application*>(qApp)->getMainWindow (),
327+ " Error saving file!" ,
328+ " CEED encountered an error trying to save the file " + actualPath);
329+
330+ if (tmpUsed)
304331 {
305- _labelText = QFileInfo (_filePath). fileName ();
306- emit filePathChanged (prevFilePath, _filePath );
332+ QFile (actualPath). remove ();
333+ QFile (actualPath + " .bak " ). rename (actualPath );
307334 }
335+ enableFileMonitoring (true );
308336
309- return true ;
337+ return false ;
310338}
311339
312340// Either reload file from disk or confirm desynchronization
0 commit comments