diff --git a/goldendict.pro b/goldendict.pro index c45fc157..c4cc13ac 100644 --- a/goldendict.pro +++ b/goldendict.pro @@ -290,7 +290,6 @@ HEADERS += \ src/audioplayerinterface.hh \ src/btreeidx.hh \ src/chunkedstorage.hh \ - src/common/atomic_rename.hh \ src/common/base_type.hh \ src/common/ex.hh \ src/common/file.hh \ @@ -419,7 +418,6 @@ SOURCES += \ src/audioplayerfactory.cc \ src/btreeidx.cc \ src/chunkedstorage.cc \ - src/common/atomic_rename.cc \ src/common/file.cc \ src/common/filetype.cc \ src/common/folding.cc \ diff --git a/src/common/atomic_rename.cc b/src/common/atomic_rename.cc deleted file mode 100644 index d4e0fc1e..00000000 --- a/src/common/atomic_rename.cc +++ /dev/null @@ -1,42 +0,0 @@ -/* This file is (c) 2008-2012 Konstantin Isakov - * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ - -#include "atomic_rename.hh" -#include -#include -#include // for wchar_t -#include -#include - -#ifdef Q_OS_WIN32 - #include -#endif - -#include - - -bool renameAtomically( QString const & oldName, QString const & newName ) -{ -#ifdef Q_OS_WIN32 - - QString srcFile( QDir::toNativeSeparators( oldName ) ); - QVector< wchar_t > srcFileW( srcFile.size() + 1 ); - srcFileW[ srcFile.toWCharArray( srcFileW.data() ) ] = 0; - - QString destFile( QDir::toNativeSeparators( newName ) ); - QVector< wchar_t > destFileW( destFile.size() + 1 ); - destFileW[ destFile.toWCharArray( destFileW.data() ) ] = 0; - - if ( !MoveFileExW( srcFileW.data(), destFileW.data(), MOVEFILE_REPLACE_EXISTING ) ) - return false; - -#else - - if ( rename( QFile::encodeName( QDir::toNativeSeparators( oldName ) ).data(), - QFile::encodeName( QDir::toNativeSeparators( newName ) ).data() ) ) - return false; - -#endif - - return true; -} diff --git a/src/common/atomic_rename.hh b/src/common/atomic_rename.hh deleted file mode 100644 index 4d28c568..00000000 --- a/src/common/atomic_rename.hh +++ /dev/null @@ -1,14 +0,0 @@ -/* This file is (c) 2008-2012 Konstantin Isakov - * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ - -#ifndef __ATOMIC_RENAME_HH_INCLUDED__ -#define __ATOMIC_RENAME_HH_INCLUDED__ - -#include - -/// Performs an atomic rename of file, from oldBame to newName. If newName -/// exists, it gets overwritten. Names should feature Qt-style separators -/// (straight slashes). Returns true on success, false on failure. -bool renameAtomically( QString const & oldName, QString const & newName ); - -#endif diff --git a/src/config.cc b/src/config.cc index d8162780..92cafb87 100644 --- a/src/config.cc +++ b/src/config.cc @@ -3,7 +3,7 @@ #include "config.hh" #include "folding.hh" -#include +#include #include #include #include @@ -17,8 +17,6 @@ #include -#include "atomic_rename.hh" - #include #if defined( HAVE_X11 ) @@ -1310,7 +1308,7 @@ void saveGroup( Group const & data, QDomElement & group ) void save( Class const & c ) { - QFile configFile( getConfigFileName() + ".tmp" ); + QSaveFile configFile( getConfigFileName() ); if ( !configFile.open( QFile::WriteOnly ) ) throw exCantWriteConfigFile(); @@ -2233,14 +2231,9 @@ void save( Class const & c ) hd.appendChild( opt ); } - QByteArray result( dd.toByteArray() ); - - if ( configFile.write( result ) != result.size() ) + configFile.write( dd.toByteArray() ); + if ( !configFile.commit() ) throw exCantWriteConfigFile(); - - configFile.close(); - - renameAtomically( configFile.fileName(), getConfigFileName() ); } QString getConfigFileName() diff --git a/src/history.cc b/src/history.cc index 7b125757..8a4662c8 100644 --- a/src/history.cc +++ b/src/history.cc @@ -3,8 +3,9 @@ #include "history.hh" #include "config.hh" -#include "atomic_rename.hh" #include +#include +#include History::History( unsigned size, unsigned maxItemLength_ ): maxSize( size ), @@ -117,7 +118,7 @@ bool History::save() if ( !dirty ) return true; - QFile file( Config::getHistoryFileName() + ".tmp" ); + QSaveFile file( Config::getHistoryFileName() ); if ( !file.open( QFile::WriteOnly | QIODevice::Text ) ) return false; @@ -136,11 +137,13 @@ bool History::save() return false; } - file.close(); + if ( file.commit() ) { + dirty = false; + return true; + } - dirty = false; - - return renameAtomically( file.fileName(), Config::getHistoryFileName() ); + qDebug() << "Failed to save history file"; + return false; } void History::clear() diff --git a/src/main.cc b/src/main.cc index 8d573e69..c909b7af 100644 --- a/src/main.cc +++ b/src/main.cc @@ -21,12 +21,12 @@ #endif #include "termination.hh" -#include "atomic_rename.hh" #include #include #include #include #include +#include #include #include "gddebug.hh" @@ -498,7 +498,10 @@ int main( int argc, char ** argv ) return -1; QString configFile = Config::getConfigFileName(); - renameAtomically( configFile, configFile + ".bad" ); + QFile::rename( configFile, + configFile % QStringLiteral( "." ) + % QDateTime::currentDateTime().toString( QStringLiteral( "yyyyMMdd_HHmmss" ) ) + % QStringLiteral( ".bad" ) ); continue; } break; diff --git a/src/ui/favoritespanewidget.cc b/src/ui/favoritespanewidget.cc index 3aac937f..788df174 100644 --- a/src/ui/favoritespanewidget.cc +++ b/src/ui/favoritespanewidget.cc @@ -9,12 +9,15 @@ #include #include #include +#include +#include +#include + #include #include #include "favoritespanewidget.hh" #include "gddebug.hh" -#include "atomic_rename.hh" #include "globalbroadcaster.hh" /************************************************** FavoritesPaneWidget *********************************************/ @@ -624,7 +627,10 @@ void FavoritesModel::readData() dom.clear(); favoritesFile.close(); - renameAtomically( m_favoritesFilename, m_favoritesFilename + ".bak" ); + QFile::rename( m_favoritesFilename, + m_favoritesFilename % QStringLiteral( "." ) + % QDateTime::currentDateTime().toString( QStringLiteral( "yyyyMMdd_HHmmss" ) ) + % QStringLiteral( ".bad" ) ); } else favoritesFile.close(); @@ -642,7 +648,7 @@ void FavoritesModel::saveData() if ( !dirty ) return; - QFile tmpFile( m_favoritesFilename + ".tmp" ); + QSaveFile tmpFile( m_favoritesFilename ); if ( !tmpFile.open( QFile::WriteOnly ) ) { gdWarning( "Can't write favorites file, error: %s", tmpFile.errorString().toUtf8().data() ); return; @@ -661,11 +667,12 @@ void FavoritesModel::saveData() return; } - tmpFile.close(); - - if ( renameAtomically( tmpFile.fileName(), m_favoritesFilename ) ) + if ( tmpFile.commit() ) { dirty = false; - + } + else { + qDebug() << "Failed to save favorite file"; + } dom.clear(); }