diff --git a/src/atomic_rename.cc b/src/atomic_rename.cc new file mode 100644 index 00000000..53bc3576 --- /dev/null +++ b/src/atomic_rename.cc @@ -0,0 +1,42 @@ +/* This file is (c) 2008-2009 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/atomic_rename.hh b/src/atomic_rename.hh new file mode 100644 index 00000000..b0e28e3e --- /dev/null +++ b/src/atomic_rename.hh @@ -0,0 +1,14 @@ +/* This file is (c) 2008-2009 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 0854cf72..4881ae41 100644 --- a/src/config.cc +++ b/src/config.cc @@ -14,10 +14,9 @@ #ifdef Q_OS_WIN32 #include "shlobj.h" -#include #endif -#include +#include "atomic_rename.hh" namespace Config { @@ -983,27 +982,7 @@ void save( Class const & c ) throw( exError ) configFile.close(); - /// Now rename it atomically. Qt does not have such a facility. - -#ifdef Q_OS_WIN32 - - QString srcFile( QDir::toNativeSeparators( configFile.fileName() ) ); - QVector< wchar_t > srcFileW( srcFile.size() + 1 ); - srcFileW[ srcFile.toWCharArray( srcFileW.data() ) ] = 0; - - QString destFile( QDir::toNativeSeparators( getConfigFileName() ) ); - QVector< wchar_t > destFileW( destFile.size() + 1 ); - destFileW[ destFile.toWCharArray( destFileW.data() ) ] = 0; - - if ( !MoveFileExW( srcFileW.data(), destFileW.data(), MOVEFILE_REPLACE_EXISTING ) ) - throw exCantWriteConfigFile(); -#else - - if ( rename( QFile::encodeName( QDir::toNativeSeparators( configFile.fileName() ) ).data(), - QFile::encodeName( QDir::toNativeSeparators( getConfigFileName() ) ).data() ) ) - throw exCantWriteConfigFile(); - -#endif + renameAtomically( configFile.fileName(), getConfigFileName() ); } QString getIndexDir() throw( exError ) @@ -1023,6 +1002,11 @@ QString getPidFileName() throw( exError ) return getHomeDir().filePath( "pid" ); } +QString getHistoryFileName() throw( exError ) +{ + return getHomeDir().filePath( "history" ); +} + QString getUserCssFileName() throw( exError ) { return getHomeDir().filePath( "article-style.css" ); diff --git a/src/config.hh b/src/config.hh index f5cac5e3..38735ca2 100644 --- a/src/config.hh +++ b/src/config.hh @@ -353,6 +353,9 @@ QString getIndexDir() throw( exError ); /// the process. QString getPidFileName() throw( exError ); +/// Returns the filename of a history file which stores search history. +QString getHistoryFileName() throw( exError ); + /// Returns the user .css file name. QString getUserCssFileName() throw( exError );