Skip to content
Permalink
Browse files

Merge pull request #2604 from SebDieBln/NoNewFilesOnSaveProject

[App] Use the existing files when saving an existing project.
  • Loading branch information
m-kuhn committed Dec 26, 2015
2 parents 6fed1ae + 87d742d commit 917dd055198f52b6288210d0a11f304aeacc2976
Showing with 64 additions and 34 deletions.
  1. +25 −3 src/app/qgisapp.cpp
  2. +39 −31 src/core/qgsproject.cpp
@@ -4312,13 +4312,35 @@ bool QgisApp::addProject( const QString& projectFile )

if ( !QgsProject::instance()->read( projectFile ) )
{
QString backupFile = projectFile + "~";
QString loadBackupPrompt;
QMessageBox::StandardButtons buttons;
if ( QFile( backupFile ).exists() )
{
loadBackupPrompt = "\n\n" + tr( "Do you want to open the backup file\n%1\ninstead?" ).arg( backupFile );
buttons |= QMessageBox::Yes;
buttons |= QMessageBox::No;
}
else
{
buttons |= QMessageBox::Ok;
}
QApplication::restoreOverrideCursor();
statusBar()->clearMessage();

QMessageBox::critical( this,
tr( "Unable to open project" ),
QgsProject::instance()->error() );
int r = QMessageBox::critical( this,
tr( "Unable to open project" ),
QgsProject::instance()->error() + loadBackupPrompt,
buttons );

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

mMapCanvas->freeze( false );
mMapCanvas->refresh();
@@ -1002,42 +1002,12 @@ bool QgsProject::write()
{
clearError();

// Create backup file
if ( QFile::exists( fileName() ) )
{
QString backup = fileName() + '~';
if ( QFile::exists( backup ) )
QFile::remove( backup );

if ( !QFile::copy( fileName(), backup ) )
{
setError( tr( "Unable to create backup file %1" ).arg( backup ) );
return false;
}

QFileInfo fi( fileName() );
struct utimbuf tb = { fi.lastRead().toTime_t(), fi.lastModified().toTime_t() };
utime( backup.toUtf8().constData(), &tb );
}

// if we have problems creating or otherwise writing to the project file,
// let's find out up front before we go through all the hand-waving
// necessary to create all the Dom objects
if ( !imp_->file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
imp_->file.close(); // even though we got an error, let's make
// sure it's closed anyway

setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
return false;
}

QFileInfo myFileInfo( imp_->file );
if ( !myFileInfo.isWritable() )
if ( myFileInfo.exists() && !myFileInfo.isWritable() )
{
// even though we got an error, let's make
// sure it's closed anyway
imp_->file.close();
setError( tr( "%1 is not writable. Please adjust permissions (if possible) and try again." )
.arg( imp_->file.fileName() ) );
return false;
@@ -1138,6 +1108,44 @@ bool QgsProject::write()
// now wrap it up and ship it to the project file
doc->normalize(); // XXX I'm not entirely sure what this does

// Create backup file
if ( QFile::exists( fileName() ) )
{
QFile backupFile( fileName() + '~' );
bool ok = true;
ok &= backupFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
ok &= imp_->file.open( QIODevice::ReadOnly );

QByteArray ba;
while ( ok && !imp_->file.atEnd() )
{
ba = imp_->file.read( 10240 );
ok &= backupFile.write( ba ) == ba.size();
}

imp_->file.close();
backupFile.close();

if ( !ok )
{
setError( tr( "Unable to create backup file %1" ).arg( backupFile.fileName() ) );
return false;
}

QFileInfo fi( fileName() );
struct utimbuf tb = { fi.lastRead().toTime_t(), fi.lastModified().toTime_t() };
utime( backupFile.fileName().toUtf8().constData(), &tb );
}

if ( !imp_->file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
imp_->file.close(); // even though we got an error, let's make
// sure it's closed anyway

setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
return false;
}

QTemporaryFile tempFile;
bool ok = tempFile.open();
if ( ok )

0 comments on commit 917dd05

Please sign in to comment.
You can’t perform that action at this time.