summaryrefslogtreecommitdiff
path: root/plugins/CurrencyRates/src/CommonOptionDlg.cpp
blob: 2878f2a91fbecf14b6886167b97d303d9956508d (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "StdAfx.h"

typedef boost::shared_ptr<CAdvProviderSettings> TAdvSettingsPtr;
typedef std::map<const ICurrencyRatesProvider*, TAdvSettingsPtr> TAdvSettings;

TAdvSettings g_aAdvSettings;

CAdvProviderSettings* get_adv_settings(const ICurrencyRatesProvider* pProvider, bool bCreateIfNonExist)
{
	TAdvSettings::iterator i = g_aAdvSettings.find(pProvider);
	if (i != g_aAdvSettings.end())
		return i->second.get();

	if (true == bCreateIfNonExist) {
		TAdvSettingsPtr pAdvSet(new CAdvProviderSettings(pProvider));
		g_aAdvSettings.insert(std::make_pair(pProvider, pAdvSet));
		return pAdvSet.get();
	}

	return nullptr;
}

void remove_adv_settings(const ICurrencyRatesProvider *pProvider)
{
	TAdvSettings::iterator i = g_aAdvSettings.find(pProvider);
	if (i != g_aAdvSettings.end())
		g_aAdvSettings.erase(i);
}

void CommonOptionDlgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp, CCommonDlgProcData& rData)
{
	switch (msg) {
	case WM_INITDIALOG:
		{
			assert(rData.m_pCurrencyRatesProvider);

			CCurrencyRatesProviderVisitorDbSettings visitor;
			rData.m_pCurrencyRatesProvider->Accept(visitor);
			assert(visitor.m_pszDbRefreshRateType);
			assert(visitor.m_pszDbRefreshRateValue);
			assert(visitor.m_pszDbDisplayNameFormat);
			assert(visitor.m_pszDbStatusMsgFormat);
			assert(visitor.m_pszDbTendencyFormat);

			// set contact list display format
			tstring sDspNameFrmt = CurrencyRates_DBGetStringT(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbDisplayNameFormat, visitor.m_pszDefDisplayFormat);
			::SetDlgItemText(hWnd, IDC_EDIT_CONTACT_LIST_FORMAT, sDspNameFrmt.c_str());

			// set status message display format
			tstring sStatusMsgFrmt = CurrencyRates_DBGetStringT(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbStatusMsgFormat, visitor.m_pszDefStatusMsgFormat);
			::SetDlgItemText(hWnd, IDC_EDIT_STATUS_MESSAGE_FORMAT, sStatusMsgFrmt.c_str());

			// set tendency format
			tstring sTendencyFrmt = CurrencyRates_DBGetStringT(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbTendencyFormat, visitor.m_pszDefTendencyFormat);
			::SetDlgItemText(hWnd, IDC_EDIT_TENDENCY_FORMAT, sTendencyFrmt.c_str());

			// refresh rate
			HWND hwndCombo = ::GetDlgItem(hWnd, IDC_COMBO_REFRESH_RATE);
			LPCTSTR pszRefreshRateTypes[] = { TranslateT("Seconds"), TranslateT("Minutes"), TranslateT("Hours") };
			for (int i = 0; i < _countof(pszRefreshRateTypes); ++i)
				::SendMessage(hwndCombo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pszRefreshRateTypes[i]));

			int nRefreshRateType = db_get_w(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbRefreshRateType, RRT_MINUTES);
			if (nRefreshRateType < RRT_SECONDS || nRefreshRateType > RRT_HOURS)
				nRefreshRateType = RRT_MINUTES;

			UINT nRate = db_get_w(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbRefreshRateValue, 1);
			switch (nRefreshRateType) {
			default:
			case RRT_SECONDS:
			case RRT_MINUTES:
				if (nRate < 1 || nRate > 60)
					nRate = 1;

				spin_set_range(::GetDlgItem(hWnd, IDC_SPIN_REFRESH_RATE), 1, 60);
				break;
			case RRT_HOURS:
				if (nRate < 1 || nRate > 24)
					nRate = 1;

				spin_set_range(::GetDlgItem(hWnd, IDC_SPIN_REFRESH_RATE), 1, 24);
				break;
			}

			::SendMessage(hwndCombo, CB_SETCURSEL, nRefreshRateType, 0);
			::SetDlgItemInt(hWnd, IDC_EDIT_REFRESH_RATE, nRate, FALSE);

			PropSheet_UnChanged(::GetParent(hWnd), hWnd);
		}
		break;

	case WM_COMMAND:
		switch (HIWORD(wp)) {
		case CBN_SELCHANGE:
			if (IDC_COMBO_REFRESH_RATE == LOWORD(wp)) {
				ERefreshRateType nType = static_cast<ERefreshRateType>(::SendMessage(reinterpret_cast<HWND>(lp), CB_GETCURSEL, 0, 0));
				switch (nType) {
				default:
				case RRT_SECONDS:
				case RRT_MINUTES:
					spin_set_range(::GetDlgItem(hWnd, IDC_SPIN_REFRESH_RATE), 1, 60);
					break;
				case RRT_HOURS:
					spin_set_range(::GetDlgItem(hWnd, IDC_SPIN_REFRESH_RATE), 1, 24);
					BOOL bOk = FALSE;
					UINT nRefreshRate = ::GetDlgItemInt(hWnd, IDC_EDIT_REFRESH_RATE, &bOk, FALSE);
					if (TRUE == bOk && nRefreshRate > 24)
						::SetDlgItemInt(hWnd, IDC_EDIT_REFRESH_RATE, 24, FALSE);
					break;
				}

				PropSheet_Changed(::GetParent(hWnd), hWnd);
			}
			break;

		case EN_CHANGE:
			switch (LOWORD(wp)) {
			case IDC_EDIT_REFRESH_RATE:
			case IDC_EDIT_CONTACT_LIST_FORMAT:
			case IDC_EDIT_STATUS_MESSAGE_FORMAT:
			case IDC_EDIT_TENDENCY_FORMAT:
				if (reinterpret_cast<HWND>(lp) == ::GetFocus())
					PropSheet_Changed(::GetParent(hWnd), hWnd);
				break;
			}
			break;

		case BN_CLICKED:
			switch (LOWORD(wp)) {
			case IDC_BUTTON_DESCRIPTION:
				show_variable_list(hWnd, rData.m_pCurrencyRatesProvider);
				break;
			case IDC_BUTTON_ADVANCED_SETTINGS:
				CAdvProviderSettings* pAdvSet = get_adv_settings(rData.m_pCurrencyRatesProvider, true);
				assert(pAdvSet);
				if (true == ShowSettingsDlg(hWnd, pAdvSet))
					PropSheet_Changed(::GetParent(hWnd), hWnd);
				break;
			}
			break;
		}
		break;

	case WM_NOTIFY:
		{
			LPNMHDR pNMHDR = reinterpret_cast<LPNMHDR>(lp);
			switch (pNMHDR->code) {
			case PSN_KILLACTIVE:
				{
					BOOL bOk = FALSE;
					UINT nRefreshRate = ::GetDlgItemInt(hWnd, IDC_EDIT_REFRESH_RATE, &bOk, FALSE);
					ERefreshRateType nType = static_cast<ERefreshRateType>(::SendDlgItemMessage(hWnd, IDC_COMBO_REFRESH_RATE, CB_GETCURSEL, 0, 0));
					switch (nType) {
					default:
					case RRT_MINUTES:
					case RRT_SECONDS:
						if (FALSE == bOk || nRefreshRate < 1 || nRefreshRate > 60) {
							prepare_edit_ctrl_for_error(::GetDlgItem(hWnd, IDC_EDIT_REFRESH_RATE));
							CurrencyRates_MessageBox(hWnd, TranslateT("Enter integer value between 1 and 60."), MB_OK | MB_ICONERROR);
							bOk = FALSE;
						}
						break;
					case RRT_HOURS:
						if (FALSE == bOk || nRefreshRate < 1 || nRefreshRate > 24) {
							prepare_edit_ctrl_for_error(::GetDlgItem(hWnd, IDC_EDIT_REFRESH_RATE));
							CurrencyRates_MessageBox(hWnd, TranslateT("Enter integer value between 1 and 24."), MB_OK | MB_ICONERROR);
							bOk = FALSE;
						}
						break;
					}

					if (TRUE == bOk) {
						HWND hEdit = ::GetDlgItem(hWnd, IDC_EDIT_CONTACT_LIST_FORMAT);
						assert(IsWindow(hEdit));

						tstring s = get_window_text(hEdit);
						if (true == s.empty()) {
							prepare_edit_ctrl_for_error(hEdit);
							CurrencyRates_MessageBox(hWnd, TranslateT("Enter text to display in contact list."), MB_OK | MB_ICONERROR);
							bOk = FALSE;
						}
					}

					::SetWindowLongPtr(hWnd, DWLP_MSGRESULT, (TRUE == bOk) ? FALSE : TRUE);
				}
				break;

			case PSN_APPLY:
				BOOL bOk = FALSE;
				UINT nRefreshRate = ::GetDlgItemInt(hWnd, IDC_EDIT_REFRESH_RATE, &bOk, FALSE);
				assert(TRUE == bOk);
				ERefreshRateType nType = static_cast<ERefreshRateType>(::SendDlgItemMessage(hWnd, IDC_COMBO_REFRESH_RATE, CB_GETCURSEL, 0, 0));

				assert(rData.m_pCurrencyRatesProvider);

				CCurrencyRatesProviderVisitorDbSettings visitor;
				rData.m_pCurrencyRatesProvider->Accept(visitor);
				assert(visitor.m_pszDbRefreshRateType);
				assert(visitor.m_pszDbRefreshRateValue);
				assert(visitor.m_pszDbDisplayNameFormat);
				assert(visitor.m_pszDbStatusMsgFormat);

				rData.m_bFireSetingsChangedEvent = true;
				db_set_w(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbRefreshRateType, nType);
				db_set_w(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbRefreshRateValue, nRefreshRate);

				tstring s = get_window_text(::GetDlgItem(hWnd, IDC_EDIT_CONTACT_LIST_FORMAT));
				db_set_ws(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbDisplayNameFormat, s.c_str());

				s = get_window_text(::GetDlgItem(hWnd, IDC_EDIT_STATUS_MESSAGE_FORMAT));
				db_set_ws(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbStatusMsgFormat, s.c_str());

				s = get_window_text(::GetDlgItem(hWnd, IDC_EDIT_TENDENCY_FORMAT));
				db_set_ws(NULL, CURRENCYRATES_MODULE_NAME, visitor.m_pszDbTendencyFormat, s.c_str());

				CAdvProviderSettings* pAdvSet = get_adv_settings(rData.m_pCurrencyRatesProvider, false);
				if (pAdvSet)
					pAdvSet->SaveToDb();
				break;
			}
		}
		break;

	case WM_DESTROY:
		remove_adv_settings(rData.m_pCurrencyRatesProvider);
		break;
	}
}