diff --git a/Modules.cpp b/Modules.cpp index a710ccdf..aa28b31e 100644 --- a/Modules.cpp +++ b/Modules.cpp @@ -38,12 +38,54 @@ CModule::CModule(void* pDLL, CUser* pUser, const CString& sModName) { m_pManager = pUser->GetManager(); m_pUser = pUser; m_sModName = sModName; + LoadRegistry(); } CModule::~CModule() { while (m_vTimers.size()) { RemTimer(m_vTimers[0]->GetName()); } + SaveRegistry(); +} + +bool CModule::LoadRegistry() +{ + CString sRegistryDir = m_pUser->GetDataPath() + "/" + m_sModName; + CUtils::MakeDir( sRegistryDir ); + return( ( m_mssRegistry.ReadFromDisk( sRegistryDir + "/" + m_pUser->GetUserName() + "-registry.txt", 0600 ) == MCString::MCS_SUCCESS ) ); +} + +bool CModule::SaveRegistry() +{ + CString sRegistryDir = m_pUser->GetDataPath() + "/" + m_sModName; + CUtils::MakeDir( sRegistryDir ); + return( ( m_mssRegistry.WriteToDisk( sRegistryDir + "/" + m_pUser->GetUserName() + "-registry.txt", 0600 ) == MCString::MCS_SUCCESS ) ); +} + +bool CModule::SetNV( const CString & sName, const CString & sValue, bool bWriteToDisk ) +{ + m_mssRegistry[sName] = sValue; + if ( bWriteToDisk ) + return( SaveRegistry() ); + + return( true ); +} + +CString CModule::GetNV( const CString & sName ) +{ + return( m_mssRegistry[sName] ); +} + +bool CModule::DelNV( const CString & sName, bool bWriteToDisk ) +{ + MCString::iterator it = m_mssRegistry.find( sName ); + if ( it != m_mssRegistry.end() ) + m_mssRegistry.erase( it ); + + if ( bWriteToDisk ) + return( SaveRegistry() ); + + return( true ); } bool CModule::AddTimer(CTimer* pTimer) { @@ -57,6 +99,14 @@ bool CModule::AddTimer(CTimer* pTimer) { return true; } +bool CModule::AddTimer(FPTimer_t pFBCallback, const CString& sLabel, u_int uInterval, + u_int uCycles, const CString& sDescription ) +{ + CFPTimer *pTimer = new CFPTimer( this, uInterval, uCycles, sLabel, sDescription ); + pTimer->SetFPCallback( pFBCallback ); + return( AddTimer( pTimer ) ); +} + bool CModule::RemTimer(const CString& sLabel) { for (unsigned int a = 0; a < m_vTimers.size(); a++) { CTimer* pTimer = m_vTimers[a]; diff --git a/Modules.h b/Modules.h index 6bb29543..44872d60 100644 --- a/Modules.h +++ b/Modules.h @@ -16,6 +16,7 @@ class CNick; class CChan; class Csock; class CModule; +class CFPTimer; template class TSocketManager; // !Forward Declarations @@ -40,6 +41,30 @@ protected: CString m_sDescription; }; +typedef void (*FPTimer_t)(CModule *, CFPTimer *); + +class CFPTimer : public CTimer { +public: + CFPTimer(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription) + : CTimer( pModule, uInterval, uCycles, sLabel, sDescription ) { + m_pFBCallback = NULL; + } + + virtual ~CFPTimer() {} + + void SetFPCallback( FPTimer_t p ) { m_pFBCallback = p; } + +protected: + virtual void RunJob() { + if ( m_pFBCallback ) + m_pFBCallback( m_pModule, this ); + } + +private: + FPTimer_t m_pFBCallback; +}; + + class CModInfo { public: @@ -129,11 +154,18 @@ public: // Timer stuff bool AddTimer(CTimer* pTimer); + bool AddTimer(FPTimer_t pFBCallback, const CString& sLabel, u_int uInterval, u_int uCycles = 0, const CString& sDescription = ""); bool RemTimer(const CString& sLabel); bool UnlinkTimer(CTimer* pTimer); CTimer* FindTimer(const CString& sLabel); virtual void ListTimers(); // !Timer stuff + + bool LoadRegistry(); + bool SaveRegistry(); + bool SetNV( const CString & sName, const CString & sValue, bool bWriteToDisk = true ); + CString GetNV( const CString & sName ); + bool DelNV( const CString & sName, bool bWriteToDisk = true ); protected: vector m_vTimers; @@ -141,6 +173,7 @@ protected: TSocketManager* m_pManager; CUser* m_pUser; CString m_sModName; + MCString m_mssRegistry; //!< way to save name/value pairs. Note there is no encryption involved in this }; class CModules : public vector { diff --git a/String.cpp b/String.cpp index c2689298..062acf5b 100644 --- a/String.cpp +++ b/String.cpp @@ -257,6 +257,7 @@ int MCString::WriteToDisk( const CString & sPath, mode_t iMode ) int MCString::ReadFromDisk( const CString & sPath, mode_t iMode ) { + clear(); CFile cFile( sPath ); if ( !cFile.Open( O_RDONLY|O_CREAT, iMode ) ) return( MCS_EOPEN ); diff --git a/String.h b/String.h index dfca389e..26cffb9a 100644 --- a/String.h +++ b/String.h @@ -65,7 +65,7 @@ protected: class MCString : public std::map< CString, CString > { public: MCString() : std::map< CString, CString >() {} - virtual ~MCString() {} + virtual ~MCString() { clear(); } enum {