summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/SmartAutoReplier/SettingsHandler.cpp
blob: c95ea0c5390019a146dec67b2ef0907416bd07ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 *  Smart Auto Replier (SAR) - auto replier plugin for Miranda IM
 *
 *  Copyright (C) 2004 - 2012 by Volodymyr M. Shcherbyna <volodymyr@shcherbyna.com>
 *
 *      This file is part of SAR.
 *
 *  SAR is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SAR is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SAR.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"
#include "settingshandler.h"
#include "modemesshandler.h"

/// we need hinst
extern HINSTANCE hInst;
extern CCrushLog CRUSHLOGOBJ;
extern INT	     g_nCurrentMode;

/// ctor
CSettingsHandler::CSettingsHandler(void) : m_bResettings(false),
m_replSize(sizeof(REPLYER_SETTINGS))
{
BEGIN_PROTECT_AND_LOG_CODE
	MakeFullPath(m_szSettFileName, sizeof(m_szSettFileName), SETTINGS_DB);
	m_rulesStorage.Init();
END_PROTECT_AND_LOG_CODE
}

/// d'ctor
CSettingsHandler::~CSettingsHandler(void)
{
BEGIN_PROTECT_AND_LOG_CODE
	DeInit();
	m_rulesStorage.Flush();
END_PROTECT_AND_LOG_CODE
}

/// changing settings...
void CSettingsHandler::setSettings(REPLYER_SETTINGS & settings, COMMON_RULE_ITEM * pcommrule)
{
BEGIN_PROTECT_AND_LOG_CODE
	m_Settings = settings;
#ifdef _DEBUG
	bool bflushed =
#endif
	Serialize(&m_Settings,  m_replSize);
	if (pcommrule)
	{
		m_rulesStorage.setCommonRule(*pcommrule);
		DWORD dw = NULL;
		m_rulesStorage.Serialize(NULL, dw); 
	}
END_PROTECT_AND_LOG_CODE
}

bool CSettingsHandler::Serialize(ISettingsStream *pSettings, DWORD & ObjectSize)
{
BEGIN_PROTECT_AND_LOG_CODE
	bool bRetVal = false;
	HANDLE hFile = CreateFile (m_szSettFileName, GENERIC_WRITE,
							   FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
							   FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == NULL || hFile == INVALID_HANDLE_VALUE)
		return bRetVal; ///
	else
	{	/// check ptr for null
		bool bValid = (pSettings != NULL);
		if (bValid) /// for readibility..
			bValid = (IsBadReadPtr(pSettings, sizeof(pSettings)) == TRUE ? false : true);
		
		DWORD dwWritten = NULL;
		bRetVal = (WriteFile (hFile, (bValid ? pSettings : &m_Settings), sizeof(REPLYER_SETTINGS), &dwWritten, NULL) == TRUE ? true : false);
		CloseHandle (hFile);
		return bRetVal;
	}
END_PROTECT_AND_LOG_CODE
	return false;
}
 
bool CSettingsHandler::Deserialize(DWORD & ObjectSize)
{
BEGIN_PROTECT_AND_LOG_CODE
	bool bRetVal = false;
	HANDLE hFile = CreateFile (m_szSettFileName, GENERIC_READ,
							   FILE_SHARE_READ, NULL, OPEN_EXISTING,
							   FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == NULL || hFile == INVALID_HANDLE_VALUE)
		return bRetVal; ///
	else
	{
		DWORD dwSizeHigh = 0;
		DWORD dwSize = GetFileSize(hFile, &dwSizeHigh);

		if (dwSize != ObjectSize)
		{
			bool bDel = NotifyAboutWrongSettings(m_szSettFileName);
			if (bDel)
			{
				CloseHandle (hFile);
				DeleteFile (m_szSettFileName);
				m_bResettings = true;
				Init();
				m_bResettings = false;
				return true;
			}
		}
		DWORD dwReaded = NULL;
		bRetVal = (ReadFile (hFile, &m_Settings, ObjectSize, &dwReaded, NULL) == TRUE ? true : false);
		CloseHandle (hFile);

		return bRetVal;
	}
END_PROTECT_AND_LOG_CODE
	return false;
}

void CSettingsHandler::Init(void)
{
BEGIN_PROTECT_AND_LOG_CODE

	if (!PathFileExists(m_szSettFileName))
	{	/// create file with def settings...		
		/// create def. configuration and store it to file...
		m_Settings.bDisableWhenModeIsSet = SETTINGS_DEF_COMMON_EN_DISONMODEON;
		m_Settings.ModeDisValue = SETTINGS_DEF_COMMON_EN_ONMODEDISVAL;
		m_Settings.bShowAURDlgWhenModeChanges = false;
		m_Settings.bEnableWhenModeIsSet = SETTINGS_DEF_COMMON_EN_ONMODEON;
		m_Settings.ModeValue = SETTINGS_DEF_COMMON_EN_ONMODEONVAL;

		m_Settings.bDisableWhenMirandaIsOn = SETTINGS_DEF_COMMON_DWMION;
		m_Settings.ReplayDelay = SETTINGS_DEF_COMMON_VALREPDELAY;
		m_Settings.bEnabled = true;
		m_Settings.bFirstRun  = !m_bResettings;
		m_Settings.bSaveToHistory = false;

		Serialize(&m_Settings, m_replSize);

		if (m_Settings.bFirstRun)
		{ /// show only once to user readme file...
			m_Settings.bFirstRun = false;
			TCHAR * szreadme = NULL;
			szreadme = new TCHAR[MAX_PATH + 1];
			if (szreadme)
			{			
				//MakeFullPath(szreadme, MAX_PATH, READMEFILE);
				::ShellExecute(NULL, TEXT("open"), TEXT("http://www.shcherbyna.com/?page_id=1747"), NULL, NULL, SW_SHOW);
				//::ShellExecute(NULL, "open", TEXT("http://shcherbyna.com/forum/viewforum.php?f=8"), NULL, NULL, SW_SHOW);

				delete szreadme;
			}
		}
	}
	else
	{
		Deserialize(m_replSize);
	}
END_PROTECT_AND_LOG_CODE
}

void CSettingsHandler::DeInit(void)
{
BEGIN_PROTECT_AND_LOG_CODE
	Serialize(&m_Settings, m_replSize);
END_PROTECT_AND_LOG_CODE
}

CScriptsReader & CSettingsHandler::getScriptsReader(void)
{
	return m_scriptsReader;
}

REPLYER_SETTINGS & CSettingsHandler::getSettings(void)
{
BEGIN_PROTECT_AND_LOG_CODE
	return m_Settings;
END_PROTECT_AND_LOG_CODE
}

DWORD CSettingsHandler::AddReplyAction(RULE_ITEM item, bool & bExists)
{
BEGIN_PROTECT_AND_LOG_CODE
	return m_rulesStorage.AddRuleItem(item, bExists);
END_PROTECT_AND_LOG_CODE
	return 0;
}

CRulesStorage & CSettingsHandler::getStorage(void)
{
BEGIN_PROTECT_AND_LOG_CODE
	return m_rulesStorage;
END_PROTECT_AND_LOG_CODE
}