Skip to content

Commit 917dd05

Browse files
committed
Merge pull request #2604 from SebDieBln/NoNewFilesOnSaveProject
[App] Use the existing files when saving an existing project.
2 parents 6fed1ae + 87d742d commit 917dd05

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

src/app/qgisapp.cpp

+25-3
Original file line numberDiff line numberDiff line change
@@ -4312,13 +4312,35 @@ bool QgisApp::addProject( const QString& projectFile )
43124312

43134313
if ( !QgsProject::instance()->read( projectFile ) )
43144314
{
4315+
QString backupFile = projectFile + "~";
4316+
QString loadBackupPrompt;
4317+
QMessageBox::StandardButtons buttons;
4318+
if ( QFile( backupFile ).exists() )
4319+
{
4320+
loadBackupPrompt = "\n\n" + tr( "Do you want to open the backup file\n%1\ninstead?" ).arg( backupFile );
4321+
buttons |= QMessageBox::Yes;
4322+
buttons |= QMessageBox::No;
4323+
}
4324+
else
4325+
{
4326+
buttons |= QMessageBox::Ok;
4327+
}
43154328
QApplication::restoreOverrideCursor();
43164329
statusBar()->clearMessage();
43174330

4318-
QMessageBox::critical( this,
4319-
tr( "Unable to open project" ),
4320-
QgsProject::instance()->error() );
4331+
int r = QMessageBox::critical( this,
4332+
tr( "Unable to open project" ),
4333+
QgsProject::instance()->error() + loadBackupPrompt,
4334+
buttons );
43214335

4336+
if ( QMessageBox::Yes == r && addProject( backupFile ) )
4337+
{
4338+
// We loaded data from the backup file, but we pretend to work on the original project file.
4339+
QgsProject::instance()->setFileName( projectFile );
4340+
QgsProject::instance()->setDirty( true );
4341+
mProjectLastModified = pfi.lastModified();
4342+
return true;
4343+
}
43224344

43234345
mMapCanvas->freeze( false );
43244346
mMapCanvas->refresh();

src/core/qgsproject.cpp

+39-31
Original file line numberDiff line numberDiff line change
@@ -1002,42 +1002,12 @@ bool QgsProject::write()
10021002
{
10031003
clearError();
10041004

1005-
// Create backup file
1006-
if ( QFile::exists( fileName() ) )
1007-
{
1008-
QString backup = fileName() + '~';
1009-
if ( QFile::exists( backup ) )
1010-
QFile::remove( backup );
1011-
1012-
if ( !QFile::copy( fileName(), backup ) )
1013-
{
1014-
setError( tr( "Unable to create backup file %1" ).arg( backup ) );
1015-
return false;
1016-
}
1017-
1018-
QFileInfo fi( fileName() );
1019-
struct utimbuf tb = { fi.lastRead().toTime_t(), fi.lastModified().toTime_t() };
1020-
utime( backup.toUtf8().constData(), &tb );
1021-
}
1022-
10231005
// if we have problems creating or otherwise writing to the project file,
10241006
// let's find out up front before we go through all the hand-waving
10251007
// necessary to create all the Dom objects
1026-
if ( !imp_->file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
1027-
{
1028-
imp_->file.close(); // even though we got an error, let's make
1029-
// sure it's closed anyway
1030-
1031-
setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
1032-
return false;
1033-
}
1034-
10351008
QFileInfo myFileInfo( imp_->file );
1036-
if ( !myFileInfo.isWritable() )
1009+
if ( myFileInfo.exists() && !myFileInfo.isWritable() )
10371010
{
1038-
// even though we got an error, let's make
1039-
// sure it's closed anyway
1040-
imp_->file.close();
10411011
setError( tr( "%1 is not writable. Please adjust permissions (if possible) and try again." )
10421012
.arg( imp_->file.fileName() ) );
10431013
return false;
@@ -1138,6 +1108,44 @@ bool QgsProject::write()
11381108
// now wrap it up and ship it to the project file
11391109
doc->normalize(); // XXX I'm not entirely sure what this does
11401110

1111+
// Create backup file
1112+
if ( QFile::exists( fileName() ) )
1113+
{
1114+
QFile backupFile( fileName() + '~' );
1115+
bool ok = true;
1116+
ok &= backupFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
1117+
ok &= imp_->file.open( QIODevice::ReadOnly );
1118+
1119+
QByteArray ba;
1120+
while ( ok && !imp_->file.atEnd() )
1121+
{
1122+
ba = imp_->file.read( 10240 );
1123+
ok &= backupFile.write( ba ) == ba.size();
1124+
}
1125+
1126+
imp_->file.close();
1127+
backupFile.close();
1128+
1129+
if ( !ok )
1130+
{
1131+
setError( tr( "Unable to create backup file %1" ).arg( backupFile.fileName() ) );
1132+
return false;
1133+
}
1134+
1135+
QFileInfo fi( fileName() );
1136+
struct utimbuf tb = { fi.lastRead().toTime_t(), fi.lastModified().toTime_t() };
1137+
utime( backupFile.fileName().toUtf8().constData(), &tb );
1138+
}
1139+
1140+
if ( !imp_->file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
1141+
{
1142+
imp_->file.close(); // even though we got an error, let's make
1143+
// sure it's closed anyway
1144+
1145+
setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
1146+
return false;
1147+
}
1148+
11411149
QTemporaryFile tempFile;
11421150
bool ok = tempFile.open();
11431151
if ( ok )

0 commit comments

Comments
 (0)