mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-27 19:24:08 +00:00
= Move out atomic renaming into a function of its own.
+m Add getHistoryFileName() in preparation for history support.
This commit is contained in:
parent
1db7b7e334
commit
00179e4a48
42
src/atomic_rename.cc
Normal file
42
src/atomic_rename.cc
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
||||
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
||||
|
||||
#include "atomic_rename.hh"
|
||||
#include <QtGlobal>
|
||||
#include <QVector>
|
||||
#include <string> // for wchar_t
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
14
src/atomic_rename.hh
Normal file
14
src/atomic_rename.hh
Normal file
|
@ -0,0 +1,14 @@
|
|||
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
||||
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
||||
|
||||
#ifndef __ATOMIC_RENAME_HH_INCLUDED__
|
||||
#define __ATOMIC_RENAME_HH_INCLUDED__
|
||||
|
||||
#include <QString>
|
||||
|
||||
/// 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
|
|
@ -14,10 +14,9 @@
|
|||
|
||||
#ifdef Q_OS_WIN32
|
||||
#include "shlobj.h"
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#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" );
|
||||
|
|
|
@ -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 );
|
||||
|
||||
|
|
Loading…
Reference in a new issue