new settings classes for client and server, derived from a common base class

This commit is contained in:
Volker Fischer 2020-06-29 20:43:41 +02:00
parent a192acacf9
commit 059d9ef203
7 changed files with 705 additions and 675 deletions

View file

@ -27,7 +27,7 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
CClientDlg::CClientDlg ( CClient* pNCliP, CClientDlg::CClientDlg ( CClient* pNCliP,
CSettings* pNSetP, CClientSettings* pNSetP,
const QString& strConnOnStartupAddress, const QString& strConnOnStartupAddress,
const int iCtrlMIDIChannel, const int iCtrlMIDIChannel,
const bool bNewShowComplRegConnList, const bool bNewShowComplRegConnList,

View file

@ -73,7 +73,7 @@ class CClientDlg : public QDialog, private Ui_CClientDlgBase
public: public:
CClientDlg ( CClient* pNCliP, CClientDlg ( CClient* pNCliP,
CSettings* pNSetP, CClientSettings* pNSetP,
const QString& strConnOnStartupAddress, const QString& strConnOnStartupAddress,
const int iCtrlMIDIChannel, const int iCtrlMIDIChannel,
const bool bNewShowComplRegConnList, const bool bNewShowComplRegConnList,
@ -96,7 +96,7 @@ protected:
void Disconnect(); void Disconnect();
CClient* pClient; CClient* pClient;
CSettings* pSettings; CClientSettings* pSettings;
bool bConnected; bool bConnected;
bool bConnectDlgWasShown; bool bConnectDlgWasShown;

View file

@ -617,7 +617,7 @@ int main ( int argc, char** argv )
strClientName ); strClientName );
// load settings from init-file // load settings from init-file
CSettings Settings ( &Client, strIniFileName ); CClientSettings Settings ( &Client, strIniFileName );
Settings.Load(); Settings.Load();
#ifndef HEADLESS #ifndef HEADLESS
@ -670,7 +670,7 @@ int main ( int argc, char** argv )
if ( bUseGUI ) if ( bUseGUI )
{ {
// load settings from init-file // load settings from init-file
CSettings Settings ( &Server, strIniFileName ); CServerSettings Settings ( &Server, strIniFileName );
Settings.Load(); Settings.Load();
// update server list AFTER restoring the settings from the // update server list AFTER restoring the settings from the

View file

@ -27,7 +27,7 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
CServerDlg::CServerDlg ( CServer* pNServP, CServerDlg::CServerDlg ( CServer* pNServP,
CSettings* pNSetP, CServerSettings* pNSetP,
const bool bStartMinimized, const bool bStartMinimized,
QWidget* parent, QWidget* parent,
Qt::WindowFlags f ) Qt::WindowFlags f )

View file

@ -53,7 +53,7 @@ class CServerDlg : public QDialog, private Ui_CServerDlgBase
public: public:
CServerDlg ( CServer* pNServP, CServerDlg ( CServer* pNServP,
CSettings* pNSetP, CServerSettings* pNSetP,
const bool bStartMinimized, const bool bStartMinimized,
QWidget* parent = nullptr, QWidget* parent = nullptr,
Qt::WindowFlags f = nullptr ); Qt::WindowFlags f = nullptr );
@ -70,7 +70,7 @@ protected:
QTimer Timer; QTimer Timer;
CServer* pServer; CServer* pServer;
CSettings* pSettings; CServerSettings* pSettings;
CVector<QTreeWidgetItem*> vecpListViewItems; CVector<QTreeWidgetItem*> vecpListViewItems;
QMutex ListViewMutex; QMutex ListViewMutex;

View file

@ -28,13 +28,9 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
void CSettings::Load() void CSettings::Load()
{ {
int iIdx;
int iValue;
bool bValue;
QDomDocument IniXMLDocument;
// prepare file name for loading initialization data from XML file and read // prepare file name for loading initialization data from XML file and read
// data from file if possible // data from file if possible
QDomDocument IniXMLDocument;
QFile file ( strFileName ); QFile file ( strFileName );
if ( file.open ( QIODevice::ReadOnly ) ) if ( file.open ( QIODevice::ReadOnly ) )
@ -45,11 +41,203 @@ void CSettings::Load()
file.close(); file.close();
} }
// read the settings from the given XML file
ReadFromXML ( IniXMLDocument );
}
// Actual settings data --------------------------------------------------- void CSettings::Save()
if ( bIsClient )
{ {
// client: // create XML document for storing initialization parameters
QDomDocument IniXMLDocument;
// write the settings in the XML file
WriteToXML ( IniXMLDocument );
// prepare file name for storing initialization data in XML file and store
// XML data in file
QFile file ( strFileName );
if ( file.open ( QIODevice::WriteOnly ) )
{
QTextStream out ( &file );
out << IniXMLDocument.toString();
file.close();
}
}
void CSettings::SetFileName ( const QString& sNFiName,
const QString& sDefaultFileName )
{
// return the file name with complete path, take care if given file name is
// empty
strFileName = sNFiName;
if ( strFileName.isEmpty() )
{
// we use the Qt default setting file paths for the different OSs by
// utilizing the QSettings class
const QSettings TempSettingsObject (
QSettings::IniFormat, QSettings::UserScope, APP_NAME, APP_NAME );
const QString sConfigDir =
QFileInfo ( TempSettingsObject.fileName() ).absolutePath();
// make sure the directory exists
if ( !QFile::exists ( sConfigDir ) )
{
QDir TempDirectoryObject;
TempDirectoryObject.mkpath ( sConfigDir );
}
// append the actual file name
strFileName = sConfigDir + "/" + sDefaultFileName;
}
}
void CSettings::SetNumericIniSet ( QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
const int iValue )
{
// convert input parameter which is an integer to string and store
PutIniSetting ( xmlFile, strSection, strKey, QString("%1").arg(iValue) );
}
bool CSettings::GetNumericIniSet ( const QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
const int iRangeStart,
const int iRangeStop,
int& iValue )
{
// init return value
bool bReturn = false;
const QString strGetIni = GetIniSetting ( xmlFile, strSection, strKey );
// check if it is a valid parameter
if ( !strGetIni.isEmpty() )
{
// convert string from init file to integer
iValue = strGetIni.toInt();
// check range
if ( ( iValue >= iRangeStart ) && ( iValue <= iRangeStop ) )
{
bReturn = true;
}
}
return bReturn;
}
void CSettings::SetFlagIniSet ( QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
const bool bValue )
{
// we encode true -> "1" and false -> "0"
if ( bValue == true )
{
PutIniSetting ( xmlFile, strSection, strKey, "1" );
}
else
{
PutIniSetting ( xmlFile, strSection, strKey, "0" );
}
}
bool CSettings::GetFlagIniSet ( const QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
bool& bValue )
{
// init return value
bool bReturn = false;
const QString strGetIni = GetIniSetting ( xmlFile, strSection, strKey );
if ( !strGetIni.isEmpty() )
{
if ( strGetIni.toInt() )
{
bValue = true;
}
else
{
bValue = false;
}
bReturn = true;
}
return bReturn;
}
// Init-file routines using XML ***********************************************
QString CSettings::GetIniSetting ( const QDomDocument& xmlFile,
const QString& sSection,
const QString& sKey,
const QString& sDefaultVal )
{
// init return parameter with default value
QString sResult ( sDefaultVal );
// get section
QDomElement xmlSection = xmlFile.firstChildElement ( sSection );
if ( !xmlSection.isNull() )
{
// get key
QDomElement xmlKey = xmlSection.firstChildElement ( sKey );
if ( !xmlKey.isNull() )
{
// get value
sResult = xmlKey.text();
}
}
return sResult;
}
void CSettings::PutIniSetting ( QDomDocument& xmlFile,
const QString& sSection,
const QString& sKey,
const QString& sValue )
{
// check if section is already there, if not then create it
QDomElement xmlSection = xmlFile.firstChildElement ( sSection );
if ( xmlSection.isNull() )
{
// create new root element and add to document
xmlSection = xmlFile.createElement ( sSection );
xmlFile.appendChild ( xmlSection );
}
// check if key is already there, if not then create it
QDomElement xmlKey = xmlSection.firstChildElement ( sKey );
if ( xmlKey.isNull() )
{
xmlKey = xmlFile.createElement ( sKey );
xmlSection.appendChild ( xmlKey );
}
// add actual data to the key
QDomText currentValue = xmlFile.createTextNode ( sValue );
xmlKey.appendChild ( currentValue );
}
// Client settings -------------------------------------------------------------
void CClientSettings::ReadFromXML ( const QDomDocument& IniXMLDocument )
{
int iIdx;
int iValue;
bool bValue;
// IP addresses // IP addresses
for ( iIdx = 0; iIdx < MAX_NUM_SERVER_ADDR_ITEMS; iIdx++ ) for ( iIdx = 0; iIdx < MAX_NUM_SERVER_ADDR_ITEMS; iIdx++ )
@ -365,89 +553,11 @@ if ( GetFlagIniSet ( IniXMLDocument, "client", "defcentservaddr", bValue ) )
pClient->bWindowWasShownConnect = bValue; pClient->bWindowWasShownConnect = bValue;
} }
} }
else
{
// server:
// central server address type (note that it is important void CClientSettings::WriteToXML ( QDomDocument& IniXMLDocument )
// to set this setting prior to the "central server address")
if ( GetNumericIniSet ( IniXMLDocument, "server", "centservaddrtype",
0, static_cast<int> ( AT_CUSTOM ), iValue ) )
{
pServer->SetCentralServerAddressType ( static_cast<ECSAddType> ( iValue ) );
}
else
{
// if no address type is given, choose one from the operating system locale
pServer->SetCentralServerAddressType ( CLocale::GetCentralServerAddressType ( QLocale::system().country() ) );
}
// TODO compatibility to old version
if ( GetFlagIniSet ( IniXMLDocument, "server", "defcentservaddr", bValue ) )
{
// only the case that manual was set in old ini must be considered
if ( !bValue )
{
pServer->SetCentralServerAddressType ( AT_CUSTOM );
}
}
// central server address (to be set after the "use default central
// server address)
pServer->SetServerListCentralServerAddress (
GetIniSetting ( IniXMLDocument, "server", "centralservaddr" ) );
// server list enabled flag
if ( GetFlagIniSet ( IniXMLDocument, "server", "servlistenabled", bValue ) )
{
pServer->SetServerListEnabled ( bValue );
}
// name
pServer->SetServerName ( GetIniSetting ( IniXMLDocument, "server", "name" ) );
// city
pServer->SetServerCity ( GetIniSetting ( IniXMLDocument, "server", "city" ) );
// country
if ( GetNumericIniSet ( IniXMLDocument, "server", "country",
0, static_cast<int> ( QLocale::LastCountry ), iValue ) )
{
pServer->SetServerCountry ( static_cast<QLocale::Country> ( iValue ) );
}
// start minimized on OS start
if ( GetFlagIniSet ( IniXMLDocument, "server", "autostartmin", bValue ) )
{
pServer->SetAutoRunMinimized ( bValue );
}
// licence type
if ( GetNumericIniSet ( IniXMLDocument, "server", "licencetype",
0, 1 /* LT_CREATIVECOMMONS */, iValue ) )
{
pServer->SetLicenceType ( static_cast<ELicenceType> ( iValue ) );
}
// window position of the main window
pServer->vecWindowPosMain = FromBase64ToByteArray (
GetIniSetting ( IniXMLDocument, "server", "winposmain_base64" ) );
}
}
void CSettings::Save()
{ {
int iIdx; int iIdx;
// create XML document for storing initialization parameters
QDomDocument IniXMLDocument;
// Actual settings data ---------------------------------------------------
if ( bIsClient )
{
// client:
// IP addresses // IP addresses
for ( iIdx = 0; iIdx < MAX_NUM_SERVER_ADDR_ITEMS; iIdx++ ) for ( iIdx = 0; iIdx < MAX_NUM_SERVER_ADDR_ITEMS; iIdx++ )
{ {
@ -636,10 +746,81 @@ void CSettings::Save()
SetFlagIniSet ( IniXMLDocument, "client", "winviscon", SetFlagIniSet ( IniXMLDocument, "client", "winviscon",
pClient->bWindowWasShownConnect ); pClient->bWindowWasShownConnect );
} }
// Server settings -------------------------------------------------------------
void CServerSettings::ReadFromXML ( const QDomDocument& IniXMLDocument )
{
int iValue;
bool bValue;
// central server address type (note that it is important
// to set this setting prior to the "central server address")
if ( GetNumericIniSet ( IniXMLDocument, "server", "centservaddrtype",
0, static_cast<int> ( AT_CUSTOM ), iValue ) )
{
pServer->SetCentralServerAddressType ( static_cast<ECSAddType> ( iValue ) );
}
else else
{ {
// server: // if no address type is given, choose one from the operating system locale
pServer->SetCentralServerAddressType ( CLocale::GetCentralServerAddressType ( QLocale::system().country() ) );
}
// TODO compatibility to old version
if ( GetFlagIniSet ( IniXMLDocument, "server", "defcentservaddr", bValue ) )
{
// only the case that manual was set in old ini must be considered
if ( !bValue )
{
pServer->SetCentralServerAddressType ( AT_CUSTOM );
}
}
// central server address (to be set after the "use default central
// server address)
pServer->SetServerListCentralServerAddress (
GetIniSetting ( IniXMLDocument, "server", "centralservaddr" ) );
// server list enabled flag
if ( GetFlagIniSet ( IniXMLDocument, "server", "servlistenabled", bValue ) )
{
pServer->SetServerListEnabled ( bValue );
}
// name
pServer->SetServerName ( GetIniSetting ( IniXMLDocument, "server", "name" ) );
// city
pServer->SetServerCity ( GetIniSetting ( IniXMLDocument, "server", "city" ) );
// country
if ( GetNumericIniSet ( IniXMLDocument, "server", "country",
0, static_cast<int> ( QLocale::LastCountry ), iValue ) )
{
pServer->SetServerCountry ( static_cast<QLocale::Country> ( iValue ) );
}
// start minimized on OS start
if ( GetFlagIniSet ( IniXMLDocument, "server", "autostartmin", bValue ) )
{
pServer->SetAutoRunMinimized ( bValue );
}
// licence type
if ( GetNumericIniSet ( IniXMLDocument, "server", "licencetype",
0, 1 /* LT_CREATIVECOMMONS */, iValue ) )
{
pServer->SetLicenceType ( static_cast<ELicenceType> ( iValue ) );
}
// window position of the main window
pServer->vecWindowPosMain = FromBase64ToByteArray (
GetIniSetting ( IniXMLDocument, "server", "winposmain_base64" ) );
}
void CServerSettings::WriteToXML ( QDomDocument& IniXMLDocument )
{
// central server address // central server address
PutIniSetting ( IniXMLDocument, "server", "centralservaddr", PutIniSetting ( IniXMLDocument, "server", "centralservaddr",
pServer->GetServerListCentralServerAddress() ); pServer->GetServerListCentralServerAddress() );
@ -676,179 +857,3 @@ void CSettings::Save()
PutIniSetting ( IniXMLDocument, "server", "winposmain_base64", PutIniSetting ( IniXMLDocument, "server", "winposmain_base64",
ToBase64 ( pServer->vecWindowPosMain ) ); ToBase64 ( pServer->vecWindowPosMain ) );
} }
// prepare file name for storing initialization data in XML file and store
// XML data in file
QFile file ( strFileName );
if ( file.open ( QIODevice::WriteOnly ) )
{
QTextStream out ( &file );
out << IniXMLDocument.toString();
file.close();
}
}
// Help functions **************************************************************
void CSettings::SetFileName ( const QString& sNFiName,
const QString& sDefaultFileName )
{
// return the file name with complete path, take care if given file name is
// empty
strFileName = sNFiName;
if ( strFileName.isEmpty() )
{
// we use the Qt default setting file paths for the different OSs by
// utilizing the QSettings class
const QSettings TempSettingsObject (
QSettings::IniFormat, QSettings::UserScope, APP_NAME, APP_NAME );
const QString sConfigDir =
QFileInfo ( TempSettingsObject.fileName() ).absolutePath();
// make sure the directory exists
if ( !QFile::exists ( sConfigDir ) )
{
QDir TempDirectoryObject;
TempDirectoryObject.mkpath ( sConfigDir );
}
// append the actual file name
strFileName = sConfigDir + "/" + sDefaultFileName;
}
}
void CSettings::SetNumericIniSet ( QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
const int iValue )
{
// convert input parameter which is an integer to string and store
PutIniSetting ( xmlFile, strSection, strKey, QString("%1").arg(iValue) );
}
bool CSettings::GetNumericIniSet ( const QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
const int iRangeStart,
const int iRangeStop,
int& iValue )
{
// init return value
bool bReturn = false;
const QString strGetIni = GetIniSetting ( xmlFile, strSection, strKey );
// check if it is a valid parameter
if ( !strGetIni.isEmpty() )
{
// convert string from init file to integer
iValue = strGetIni.toInt();
// check range
if ( ( iValue >= iRangeStart ) && ( iValue <= iRangeStop ) )
{
bReturn = true;
}
}
return bReturn;
}
void CSettings::SetFlagIniSet ( QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
const bool bValue )
{
// we encode true -> "1" and false -> "0"
if ( bValue == true )
{
PutIniSetting ( xmlFile, strSection, strKey, "1" );
}
else
{
PutIniSetting ( xmlFile, strSection, strKey, "0" );
}
}
bool CSettings::GetFlagIniSet ( const QDomDocument& xmlFile,
const QString& strSection,
const QString& strKey,
bool& bValue )
{
// init return value
bool bReturn = false;
const QString strGetIni = GetIniSetting ( xmlFile, strSection, strKey );
if ( !strGetIni.isEmpty() )
{
if ( strGetIni.toInt() )
{
bValue = true;
}
else
{
bValue = false;
}
bReturn = true;
}
return bReturn;
}
// Init-file routines using XML ***********************************************
QString CSettings::GetIniSetting ( const QDomDocument& xmlFile,
const QString& sSection,
const QString& sKey,
const QString& sDefaultVal )
{
// init return parameter with default value
QString sResult ( sDefaultVal );
// get section
QDomElement xmlSection = xmlFile.firstChildElement ( sSection );
if ( !xmlSection.isNull() )
{
// get key
QDomElement xmlKey = xmlSection.firstChildElement ( sKey );
if ( !xmlKey.isNull() )
{
// get value
sResult = xmlKey.text();
}
}
return sResult;
}
void CSettings::PutIniSetting ( QDomDocument& xmlFile,
const QString& sSection,
const QString& sKey,
const QString& sValue )
{
// check if section is already there, if not then create it
QDomElement xmlSection = xmlFile.firstChildElement ( sSection );
if ( xmlSection.isNull() )
{
// create new root element and add to document
xmlSection = xmlFile.createElement ( sSection );
xmlFile.appendChild ( xmlSection );
}
// check if key is already there, if not then create it
QDomElement xmlKey = xmlSection.firstChildElement ( sKey );
if ( xmlKey.isNull() )
{
xmlKey = xmlFile.createElement ( sKey );
xmlSection.appendChild ( xmlKey );
}
// add actual data to the key
QDomText currentValue = xmlFile.createTextNode ( sValue );
xmlKey.appendChild ( currentValue );
}

View file

@ -39,18 +39,15 @@
class CSettings class CSettings
{ {
public: public:
CSettings ( CClient* pNCliP, const QString& sNFiName ) : CSettings() : strFileName ( "" ) {}
pClient ( pNCliP ), bIsClient ( true ), strFileName ( "" )
{ SetFileName ( sNFiName, DEFAULT_INI_FILE_NAME ); }
CSettings ( CServer* pNSerP, const QString& sNFiName ) :
pServer ( pNSerP ), bIsClient ( false ), strFileName ( "" )
{ SetFileName ( sNFiName, DEFAULT_INI_FILE_NAME_SERVER); }
void Load(); void Load();
void Save(); void Save();
protected: protected:
virtual void ReadFromXML ( const QDomDocument& IniXMLDocument ) = 0;
virtual void WriteToXML ( QDomDocument& IniXMLDocument ) = 0;
void SetFileName ( const QString& sNFiName, void SetFileName ( const QString& sNFiName,
const QString& sDefaultFileName ); const QString& sDefaultFileName );
@ -106,10 +103,38 @@ protected:
const QString& sKey, const QString& sKey,
const QString& sValue = "" ); const QString& sValue = "" );
// pointer to the client/server object which stores the various settings
CClient* pClient; // for client
CServer* pServer; // for server
bool bIsClient;
QString strFileName; QString strFileName;
}; };
class CClientSettings : public CSettings
{
public:
CClientSettings ( CClient* pNCliP, const QString& sNFiName ) : pClient ( pNCliP )
{ SetFileName ( sNFiName, DEFAULT_INI_FILE_NAME ); }
protected:
virtual void ReadFromXML ( const QDomDocument& IniXMLDocument ) override;
virtual void WriteToXML ( QDomDocument& IniXMLDocument ) override;
CClient* pClient;
};
class CServerSettings : public CSettings
{
public:
CServerSettings ( CServer* pNSerP, const QString& sNFiName ) : pServer ( pNSerP )
{ SetFileName ( sNFiName, DEFAULT_INI_FILE_NAME_SERVER); }
protected:
virtual void ReadFromXML ( const QDomDocument& IniXMLDocument ) override;
virtual void WriteToXML ( QDomDocument& IniXMLDocument ) override;
CServer* pServer;
};