From aa0fd533e6db2745912af3cf9c0f7c326f4bc87d Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sat, 21 May 2011 16:53:01 +0000 Subject: [PATCH] Store file name in settings class on creation and do not require it for each load and save call --- src/main.cpp | 12 ++++++------ src/settings.cpp | 20 +++++++++----------- src/settings.h | 19 +++++++++++-------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2e9a287b..e4764742 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -344,8 +344,8 @@ int main ( int argc, char** argv ) CClient Client ( iPortNumber ); // load settings from init-file - CSettings Settings ( &Client ); - Settings.Load ( strIniFileName ); + CSettings Settings ( &Client, strIniFileName ); + Settings.Load(); // GUI object CLlconClientDlg ClientDlg ( @@ -364,7 +364,7 @@ int main ( int argc, char** argv ) app.exec(); // save settings to init-file - Settings.Save ( strIniFileName ); + Settings.Save(); } else { @@ -387,8 +387,8 @@ int main ( int argc, char** argv ) Server.SetUseDefaultCentralServerAddress ( true ); // load settings from init-file - CSettings Settings ( &Server ); - Settings.Load ( strIniFileName ); + CSettings Settings ( &Server, strIniFileName ); + Settings.Load(); // update server list AFTER restoring the settings from the // settings file @@ -410,7 +410,7 @@ int main ( int argc, char** argv ) app.exec(); // save settings to init-file - Settings.Save ( strIniFileName ); + Settings.Save(); } else { diff --git a/src/settings.cpp b/src/settings.cpp index cf6297fb..92c34b84 100755 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -26,7 +26,7 @@ /* Implementation *************************************************************/ -void CSettings::ReadIniFile ( const QString& sFileName ) +void CSettings::Load() { int iValue; bool bValue; @@ -34,7 +34,7 @@ void CSettings::ReadIniFile ( const QString& sFileName ) // prepare file name for loading initialization data from XML file and read // data from file if possible - QFile file ( GetIniFileNameWithPath ( sFileName ) ); + QFile file ( strFileName ); if ( file.open ( QIODevice::ReadOnly ) ) { QTextStream in ( &file ); @@ -240,7 +240,7 @@ void CSettings::ReadIniFile ( const QString& sFileName ) } } -void CSettings::WriteIniFile ( const QString& sFileName ) +void CSettings::Save() { // create XML document for storing initialization parameters QDomDocument IniXMLDocument; @@ -366,7 +366,7 @@ void CSettings::WriteIniFile ( const QString& sFileName ) // prepare file name for storing initialization data in XML file and store // XML data in file - QFile file ( GetIniFileNameWithPath ( sFileName ) ); + QFile file ( strFileName ); if ( file.open ( QIODevice::WriteOnly ) ) { QTextStream out ( &file ); @@ -378,12 +378,12 @@ void CSettings::WriteIniFile ( const QString& sFileName ) // Help functions ************************************************************** -QString CSettings::GetIniFileNameWithPath ( const QString& sFileName ) +void CSettings::SetFileName ( const QString& sNFiName ) { // return the file name with complete path, take care if given file name is // empty - QString sCurFileName = sFileName; - if ( sCurFileName.isEmpty() ) + strFileName = sNFiName; + if ( strFileName.isEmpty() ) { // we use the Qt default setting file paths for the different OSs by // utilizing the QSettings class @@ -403,15 +403,13 @@ QString CSettings::GetIniFileNameWithPath ( const QString& sFileName ) // append the actual file name if ( bIsClient ) { - sCurFileName = sConfigDir + "/" + DEFAULT_INI_FILE_NAME; + strFileName = sConfigDir + "/" + DEFAULT_INI_FILE_NAME; } else { - sCurFileName = sConfigDir + "/" + DEFAULT_INI_FILE_NAME_SERVER; + strFileName = sConfigDir + "/" + DEFAULT_INI_FILE_NAME_SERVER; } } - - return sCurFileName; } void CSettings::SetNumericIniSet ( QDomDocument& xmlFile, diff --git a/src/settings.h b/src/settings.h index 7fbfd298..0e37917b 100755 --- a/src/settings.h +++ b/src/settings.h @@ -39,17 +39,19 @@ class CSettings { public: - CSettings ( CClient* pNCliP ) : pClient ( pNCliP ), bIsClient ( true ) {} - CSettings ( CServer* pNSerP ) : pServer ( pNSerP ), bIsClient ( false ) {} + CSettings ( CClient* pNCliP, const QString& sNFiName ) : + pClient ( pNCliP ), bIsClient ( true ) + { SetFileName ( sNFiName ); } - void Load ( const QString& sFileName = "" ) { ReadIniFile ( sFileName ); } - void Save ( const QString& sFileName = "" ) { WriteIniFile ( sFileName ); } + CSettings ( CServer* pNSerP, const QString& sNFiName ) : + pServer ( pNSerP ), bIsClient ( false ) + { SetFileName ( sNFiName ); } + + void Load(); + void Save(); protected: - void ReadIniFile ( const QString& sFileName ); - void WriteIniFile ( const QString& sFileName ); - - QString GetIniFileNameWithPath ( const QString& sFileName ); + void SetFileName ( const QString& sNFiName ); // init file access function for read/write void SetNumericIniSet ( QDomDocument& xmlFile, const QString& strSection, @@ -73,6 +75,7 @@ protected: CServer* pServer; // for server bool bIsClient; + QString strFileName; }; #endif // !defined ( SETTINGS_H__3B0BA660_DGEG56G456G9876D31912__INCLUDED_ )