| 237 | |
| 238 | |
| 239 | bool DataFile::writeFile( const QString& filename ) |
| 240 | { |
| 241 | const QString fullName = nameWithExtension( filename ); |
| 242 | const QString fullNameTemp = fullName + ".new"; |
| 243 | const QString fullNameBak = fullName + ".bak"; |
| 244 | |
| 245 | QFile outfile( fullNameTemp ); |
| 246 | |
| 247 | if( !outfile.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) |
| 248 | { |
| 249 | if( gui ) |
| 250 | { |
| 251 | QMessageBox::critical( NULL, |
| 252 | SongEditor::tr( "Could not write file" ), |
| 253 | SongEditor::tr( "Could not open %1 for writing. You probably are not permitted to " |
| 254 | "write to this file. Please make sure you have write-access to " |
| 255 | "the file and try again." ).arg( fullName ) ); |
| 256 | } |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if( fullName.section( '.', -1 ) == "mmpz" ) |
| 262 | { |
| 263 | QString xml; |
| 264 | QTextStream ts( &xml ); |
| 265 | write( ts ); |
| 266 | outfile.write( qCompress( xml.toUtf8() ) ); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | QTextStream ts( &outfile ); |
| 271 | write( ts ); |
| 272 | } |
| 273 | |
| 274 | outfile.close(); |
| 275 | |
| 276 | // make sure the file has been written correctly |
| 277 | if( QFileInfo( outfile.fileName() ).size() > 0 ) |
| 278 | { |
| 279 | if( ConfigManager::inst()->value( "app", "disablebackup" ).toInt() ) |
| 280 | { |
| 281 | // remove current file |
| 282 | QFile::remove( fullName ); |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | // remove old backup file |
| 287 | QFile::remove( fullNameBak ); |
| 288 | // move current file to backup file |
| 289 | QFile::rename( fullName, fullNameBak ); |
| 290 | } |
| 291 | // move temporary file to current file |
| 292 | QFile::rename( fullNameTemp, fullName ); |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |