From 534509adaf6c84df830516ec15677bdc5ad0c029 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Wed, 8 May 2013 20:50:06 +0400 Subject: [PATCH] Handle errors during configuration file loading --- config.cc | 9 +++++---- config.hh | 3 +++ main.cc | 24 +++++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/config.cc b/config.cc index fba21aa6..79a3e26e 100644 --- a/config.cc +++ b/config.cc @@ -48,10 +48,6 @@ namespace return result; } - QString getConfigFileName() - { - return getHomeDir().absoluteFilePath( "config" ); - } } ProxyServer::ProxyServer(): enabled( false ), type( Socks5 ), port( 3128 ) @@ -1559,6 +1555,11 @@ void save( Class const & c ) throw( exError ) renameAtomically( configFile.fileName(), getConfigFileName() ); } +QString getConfigFileName() +{ + return getHomeDir().absoluteFilePath( "config" ); +} + QString getConfigDir() throw( exError ) { return getHomeDir().path() + QDir::separator(); diff --git a/config.hh b/config.hh index ef5ece6f..042b1780 100644 --- a/config.hh +++ b/config.hh @@ -524,6 +524,9 @@ Class load() throw( exError ); /// Saves the configuration void save( Class const & ) throw( exError ); +/// Returns the configuration file name. +QString getConfigFileName(); + /// Returns the main configuration directory. QString getConfigDir() throw( exError ); diff --git a/main.cc b/main.cc index 7384e084..6846000b 100644 --- a/main.cc +++ b/main.cc @@ -21,8 +21,10 @@ #endif #include "termination.hh" +#include "atomic_rename.hh" #include +#include int main( int argc, char ** argv ) { @@ -119,7 +121,27 @@ int main( int argc, char ** argv ) app.setWindowIcon( QIcon( ":/icons/macicon.png" ) ); #endif - Config::Class cfg( Config::load() ); + Config::Class cfg; + for( ; ; ) + { + try + { + cfg = Config::Class( Config::load() ); + } + catch( Config::exError ) + { + QMessageBox mb( QMessageBox::Warning, "GoldenDict", "Error in configuration file. Continue with default settings?", + QMessageBox::Yes | QMessageBox::No ); + mb.exec(); + if( mb.result() != QMessageBox::Yes ) + return -1; + + QString configFile = Config::getConfigFileName(); + renameAtomically( configFile, configFile + ".bad" ); + continue; + } + break; + } if ( Config::isPortableVersion() ) {