FolderMan: Fix startFromScratch logic. #1989

(cherry picked from commit 4566ca3127)
This commit is contained in:
Christian Kamm 2014-09-05 10:13:00 +02:00 committed by Olivier Goffart
parent f348eabf19
commit 20dd3b0a69

View file

@ -635,27 +635,41 @@ QString FolderMan::getBackupName( QString fullPathName ) const
bool FolderMan::startFromScratch( const QString& localFolder ) bool FolderMan::startFromScratch( const QString& localFolder )
{ {
if( localFolder.isEmpty() ) return false; if( localFolder.isEmpty() ) {
return false;
}
QFileInfo fi( localFolder ); QFileInfo fi( localFolder );
if( fi.exists() && fi.isDir() ) { QDir parentDir( fi.dir() );
QDir file = fi.dir(); QString folderName = fi.fileName();
// check if there are files in the directory. // Adjust for case where localFolder ends with a /
if( file.count() == 0 ) { if ( fi.isDir() ) {
// directory is existing, but its empty. Use it. folderName = parentDir.dirName();
parentDir.cdUp();
}
if( fi.exists() ) {
// It exists, but is empty -> just reuse it.
if( fi.isDir() && fi.dir().count() == 0 ) {
qDebug() << "startFromScratch: Directory is empty!"; qDebug() << "startFromScratch: Directory is empty!";
return true; return true;
} }
QString newName = getBackupName( fi.absoluteFilePath() ); // Make a backup of the folder/file.
QString newName = getBackupName( parentDir.absoluteFilePath( folderName ) );
if( file.rename( fi.absoluteFilePath(), newName )) { if( !parentDir.rename( fi.absoluteFilePath(), newName ) ) {
if( file.mkdir( fi.absoluteFilePath() ) ) { qDebug() << "startFromScratch: Could not rename" << fi.absoluteFilePath()
return true; << "to" << newName;
} return false;
} }
} }
return false;
if( !parentDir.mkdir( fi.absoluteFilePath() ) ) {
qDebug() << "startFromScratch: Could not mkdir" << fi.absoluteFilePath();
return false;
}
return true;
} }
void FolderMan::setDirtyProxy(bool value) void FolderMan::setDirtyProxy(bool value)