Files
Work/aladin/appreg.cpp
2024-08-07 09:12:07 -04:00

196 lines
4.5 KiB
C++

#include <aladin/appreg.hpp>
#include <commctrl/commctrl.hpp>
AppReg::AppReg(void)
: mAladinKeyName(STRING_ALADINKEYNAME),
mSerialKeyName(STRING_SERIALKEYNAME),
mSettingsEMAIL(STRING_SETTINGSEMAIL),
mSerialPort(STRING_SERIALPORT),
mSerialBaud(STRING_SERIALBAUD),
mSerialData(STRING_SERIALDATA),
mSerialParity(STRING_SERIALPARITY),
mSerialStop(STRING_SERIALSTOP),
mHistoryKeyName(STRING_HISTORYKEYNAME),
mHistoryKeyShortName(STRING_HISTORYKEYSHORTNAME),
mRegKeySerial(RegKey::CurrentUser)
{
guarantee();
cacheHistory();
}
AppReg::AppReg(const AppReg &someAppReg)
: mAladinKeyName(STRING_ALADINKEYNAME),
mSerialKeyName(STRING_SERIALKEYNAME),
mSettingsEMAIL(STRING_SETTINGSEMAIL),
mSerialPort(STRING_SERIALPORT),
mSerialBaud(STRING_SERIALBAUD),
mSerialData(STRING_SERIALDATA),
mSerialParity(STRING_SERIALPARITY),
mSerialStop(STRING_SERIALSTOP),
mRegKeySerial(RegKey::CurrentUser)
{
*this=someAppReg;
}
AppReg::~AppReg()
{
}
AppReg &AppReg::operator=(const AppReg &/*someAppReg*/)
{
return *this;
}
bool AppReg::getHistory(Block<String> &nameList)
{
nameList=mCachedNames;
return nameList.size()?true:false;
}
bool AppReg::setHistory(Block<String> &nameList)
{
RegKey regKey(RegKey::CurrentUser);
mRegKeyHistory.closeKey();
regKey.openKey(String(STRING_ALADINKEYNAME));
regKey.deleteKey(mHistoryKeyShortName);
regKey.closeKey();
mRegKeyHistory.createKey(mHistoryKeyName,"");
mRegKeyHistory.openKey(mHistoryKeyName);
for(int itemIndex=0;itemIndex<nameList.size();itemIndex++)mRegKeyHistory.setValue(nameList[itemIndex],0);
return nameList.size()?true:false;
}
bool AppReg::insertHistory(const String &strName)
{
for(int itemIndex=0;itemIndex<mCachedNames.size();itemIndex++)if(mCachedNames[itemIndex]==strName)return FALSE;
mCachedNames.insert(&strName);
if(mCachedNames.size()>MaxCachedNames)
{
Block<String> mruCachedNames;
for(int itemIndex=0;itemIndex<MaxCachedNames;itemIndex++)mruCachedNames.insert(&String());
for(itemIndex=0;itemIndex<MaxCachedNames;itemIndex++)mruCachedNames[mruCachedNames.size()-itemIndex-1]=mCachedNames[mCachedNames.size()-itemIndex-1];
mCachedNames=mruCachedNames;
setHistory(mCachedNames);
}
else mRegKeyHistory.setValue(strName,0);
return true;
}
void AppReg::cacheHistory(void)
{
int itemIndex(0);
String entryName;
DWORD status;
mCachedNames.remove();
while(mRegKeyHistory.enumValue(itemIndex++,entryName,status))mCachedNames.insert(&entryName);
}
bool AppReg::setBaud(const String &strBaud)
{
return mRegKeySerial.setValue(mSerialBaud,strBaud);
}
String AppReg::getBaud(void)
{
String strBaud;
mRegKeySerial.queryValue(mSerialBaud,strBaud);
return strBaud;
}
bool AppReg::setParity(const String &strParity)
{
return mRegKeySerial.setValue(mSerialParity,strParity);
}
String AppReg::getParity(void)
{
String strParity;
mRegKeySerial.queryValue(mSerialParity,strParity);
return strParity;
}
bool AppReg::setDataBits(const String &strDataBits)
{
return mRegKeySerial.setValue(mSerialData,strDataBits);
}
String AppReg::getDataBits(void)
{
String strData;
mRegKeySerial.queryValue(mSerialData,strData);
return strData;
}
bool AppReg::setStopBits(const String &stopBits)
{
return mRegKeySerial.setValue(mSerialStop,stopBits);
}
String AppReg::getStopBits(void)
{
String strStop;
mRegKeySerial.queryValue(mSerialStop,strStop);
return strStop;
}
bool AppReg::setPort(const String &strPort)
{
return mRegKeySerial.setValue(mSerialPort,strPort);
}
String AppReg::getPort(void)
{
String strPort;
mRegKeySerial.queryValue(mSerialPort,strPort);
return strPort;
}
String AppReg::getSerialSettings(void)
{
String serialSettings;
serialSettings+="baud=";
serialSettings+=getBaud();
serialSettings+=" ";
serialSettings+="parity=";
serialSettings+=getParity();
serialSettings+=" ";
serialSettings+="data=";
serialSettings+=getDataBits();
serialSettings+=" ";
serialSettings+="stop=";
serialSettings+=getStopBits();
return serialSettings;
}
void AppReg::guarantee(void)
{
if(!mRegKeySerial.openKey(mSerialKeyName))
{
CommControl commControl;
Block<String> deviceList;
commControl.enumerateDevices(deviceList);
mRegKeySerial.createKey(mSerialKeyName,"");
mRegKeySerial.openKey(mSerialKeyName);
setBaud("19200");
setParity("N");
setDataBits("8");
setStopBits("1");
setPort(deviceList.size()?deviceList[0]:"COM1");
}
if(!mRegKeyHistory.openKey(mHistoryKeyName))
{
mRegKeyHistory.createKey(mHistoryKeyName,"");
mRegKeyHistory.openKey(mHistoryKeyName);
}
}
BOOL AppReg::isOkay(void)const
{
return mRegKeySerial.isOkay();
}