aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/setup/spage_connect.cpp
blob: af0e84cc5ca5176d690e6a1f6651a6a3e62842b8 (plain) (blame)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#define APSTUDIO_READONLY_SYMBOLS
#include "main.h"
#include "./spage_connect.h"
#include "./setup_resource.h"
#include "../nu/ns_wc.h"
#include "./langutil.h"

setup_page_connect::setup_page_connect() : ref(1), hwnd(NULL)
{
	inetMode = 0;
	bPort80  = FALSE;
	szProxy[MAX_PATH] = 0x00;
}
setup_page_connect::~setup_page_connect()
{
}

size_t setup_page_connect::AddRef()
{
	return ++ref;
}

size_t setup_page_connect::Release()
{
	if (1 == ref) 
	{
		delete(this);
		return 0;
	}
	return --ref;
}

HRESULT setup_page_connect::GetName(bool bShort, const wchar_t **pszName)
{
	static wchar_t szName[64] = {0,};
	*pszName = (*szName) ? szName : getStringW(IDS_PAGE_CONNECTIVITY, szName, sizeof(szName)/sizeof(wchar_t));
	return S_OK;
}

HRESULT setup_page_connect::Save(HWND hwndText)
{
	HRESULT hr(S_OK);
	INT count;
	wchar_t app[MAX_PATH] = {0}, *p;

	if (S_FALSE == IsDirty()) return S_OK;

	count = MultiByteToWideCharSZ(CP_ACP, 0, app_name, -1, app, sizeof(app)/sizeof(wchar_t));
	if (!count) hr = S_FALSE;
	if (S_FALSE == hr) return hr;

	if (config_inet_mode != inetMode)
	{
		wchar_t szText[MAX_PATH] = {0};
		config_inet_mode = (char)inetMode;
		WritePrivateProfileStringW(app, L"inet_mode", _itow(inetMode, szText, 10), INI_FILE);
	}

	if (2 == inetMode) szProxy[0] = 0x00;
    if (0 == *szProxy) config_proxy[0] = 0x00;
	else
	{
		count = WideCharToMultiByteSZ(CP_ACP, 0, szProxy, -1, config_proxy, sizeof(config_proxy)/sizeof(char), NULL, NULL);
		if (!count) hr = S_FALSE;
	}
	if (!WritePrivateProfileStringW(app, L"Proxy", szProxy, INI_FILE)) hr = S_FALSE;
	if (bPort80 && 0x00 == *szProxy) bPort80 = FALSE;
	p = (bPort80) ? L"1" : L"";
	if (!WritePrivateProfileStringW(app, L"proxyonly80", p, INI_FILE)) hr = S_FALSE;
	WritePrivateProfileStringW(app, L"proxy80", p, INI_FILE); // old

	if (hwnd) UpdateUI();
	return hr;
}

HRESULT setup_page_connect::Revert(void)
{
	HRESULT hr(S_OK);
	INT count;
	wchar_t app[MAX_PATH] = {0};

	count = MultiByteToWideCharSZ(CP_ACP, 0, app_name, -1, app, sizeof(app)/sizeof(wchar_t));
	if (!count) hr = S_FALSE;

	if (config_inet_mode==3) isInetAvailable(); // autodetect
	inetMode = config_inet_mode;

	if (*config_proxy && 2 != inetMode)
	{
		count = MultiByteToWideCharSZ(CP_ACP, 0, config_proxy, -1, szProxy, sizeof(szProxy)/sizeof(wchar_t));
		if (!count) hr = S_FALSE;
	}
	else szProxy[0] = 0x00;

	bPort80 = (0x00 != *szProxy && 0 != GetPrivateProfileIntW(app, L"proxyonly80", 0, INI_FILE));

	if (hwnd) UpdateUI();

	return hr;
}

HRESULT setup_page_connect::IsDirty(void)
{
	INT res;
	wchar_t app[MAX_PATH] = {0};
	char szTest[MAX_PATH] = {0};

	if (inetMode != config_inet_mode) return S_OK;

	if (2 == inetMode || 0 == *szProxy) szTest[0] = 0x00;
	else
	{
		res = WideCharToMultiByteSZ(CP_ACP, 0, szProxy, -1, szTest, sizeof(szTest)/sizeof(char), NULL, NULL);
		if (!res) return E_OUTOFMEMORY;
	}

	res = CompareStringA(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT),
							NORM_IGNORECASE, szTest, -1, config_proxy, -1);
	if (0 == res) return E_UNEXPECTED;
	if (CSTR_EQUAL != res) return S_OK;

	res = MultiByteToWideCharSZ(CP_ACP, 0, app_name, -1, app, sizeof(app)/sizeof(wchar_t));
	if (!res) return E_OUTOFMEMORY;
	
	return ((bPort80 && 0x00 != *szProxy) != ( 0 != GetPrivateProfileIntW(app, L"proxyonly80", 0, INI_FILE))) ? S_OK : S_FALSE;
}

HRESULT setup_page_connect::Validate(void)
{
	return S_OK;
}

HRESULT setup_page_connect::CreateView(HWND hwndParent, HWND *phwnd)
{
	*phwnd = WACreateDialogParam(MAKEINTRESOURCEW(IDD_SETUP_PAGE_CONNECT), hwndParent, ::DialogProc, (LPARAM)this);
	return S_OK;
}

void setup_page_connect::UpdateUI(void)
{
	int count;

	if (!hwnd || !IsWindow(hwnd)) return;
	
	CheckDlgButton(hwnd, IDC_CHK_PORT80, (bPort80) ? BST_CHECKED : BST_UNCHECKED);
	SetDlgItemTextW(hwnd, IDC_EDT_SERVER, szProxy);

	HWND hwndCB = GetDlgItem(hwnd, IDC_CB_CONNECT);
	if (!hwndCB || !IsWindow(hwndCB)) return;

	count = (INT)SendMessageW(hwndCB, CB_GETCOUNT, 0, 0L);
	if (count > 0)
	{
		int index;
		for (index = 0; index < count && inetMode != (INT)SendMessageW(hwndCB, CB_GETITEMDATA, index, 0L); index++);
		if (index == count) 
		{
			inetMode = (INT)SendMessageW(hwndCB, CB_GETITEMDATA, 0, 0L);
			index = 0;
		}
		SendMessageW(hwndCB, CB_SETCURSEL, index, 0L);
		PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CB_CONNECT, CBN_SELCHANGE), (LPARAM)hwndCB);
	}
}

void setup_page_connect::ComboBox_OnSelChange(HWND hwndCtrl)
{
	INT proxyList[] = { IDC_GRP_PROXY, IDC_LBL_SERVER, IDC_EDT_SERVER, IDC_CHK_PORT80, };
	INT index;

	index = (INT)SendMessageW(hwndCtrl, CB_GETCURSEL, 0, 0L);
	if (CB_ERR == index) return;

	inetMode =  (INT)SendMessageW(hwndCtrl, CB_GETITEMDATA, index, 0L);

	for (int i = 0; i < sizeof(proxyList)/sizeof(INT); i++)
	{
		HWND hwndItem = GetDlgItem(hwnd, proxyList[i]);
		if (IsWindow(hwndItem)) ShowWindow(hwndItem, (2 != inetMode) ? SW_SHOWNA : SW_HIDE);
	}
}

INT_PTR setup_page_connect::OnInitDialog(HWND hwndFocus, LPARAM lParam)
{
	UINT connID[] = { IDS_INST_INET1, IDS_INST_INET2, IDS_INST_INET3, };
	HWND hwndCB = GetDlgItem(hwnd, IDC_CB_CONNECT);

	CheckDlgButton(hwnd, IDC_CHK_PORT80, (bPort80) ? BST_CHECKED : BST_UNCHECKED);
	//SendDlgItemMessageW(hwnd, IDC_EDT_SERVER, EM_LIMITTEXT, sizeof(szProxy)/sizeof(wchar_t), 0);
	SetDlgItemTextW(hwnd, IDC_EDT_SERVER, szProxy);

	if (inetMode < 0) inetMode = 0;
	else if (inetMode >= sizeof(connID)/sizeof(INT)) inetMode = sizeof(connID)/sizeof(INT) - 1;

	for(int i = 0;  i < sizeof(connID)/sizeof(INT); i++) 
	{
		INT index = (INT)SendMessageW(hwndCB, CB_ADDSTRING,0, (LPARAM)getStringW(connID[i], NULL, 0));
		if (CB_ERR != index) SendMessageW(hwndCB, CB_SETITEMDATA, index, (LPARAM)i);
		if (inetMode == i) SendMessageW(hwndCB, CB_SETCURSEL, index, 0L);
	}

	PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CB_CONNECT, CBN_SELCHANGE), (LPARAM)hwndCB);

	return 0;
}

void setup_page_connect::OnSize(UINT nType, INT cx, INT cy)
{
	INT ctrlList[] = { IDC_CB_CONNECT, IDC_GRP_PROXY, IDC_LBL_SERVER, IDC_EDT_SERVER, IDC_CHK_PORT80, };
	RECT rw;
	HDWP hdwp = BeginDeferWindowPos(sizeof(ctrlList)/sizeof(INT));
	if (!hdwp) return;

	for (int i = 0; i < sizeof(ctrlList)/sizeof(INT); i++)
	{
		HWND hwndCtrl = GetDlgItem(hwnd, ctrlList[i]);
		if (hwndCtrl)
		{
			GetWindowRect(hwndCtrl, &rw);
			MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rw, 2);
			hdwp = DeferWindowPos(hdwp, hwndCtrl, NULL, max(0, (cx - (rw.right - rw.left))/2), rw.top, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
		}
	}
	if (hdwp) EndDeferWindowPos(hdwp);
}

void setup_page_connect::OnCommand(INT nCtrlID, INT nEvntID, HWND hwndCtrl)
{
	switch(nCtrlID)
	{		
		case IDC_CB_CONNECT:
			switch(nEvntID)
			{
				case CBN_SELCHANGE: ComboBox_OnSelChange(hwndCtrl); break;
			}
			break;
		case IDC_EDT_SERVER:
			switch(nEvntID)
			{
				case EN_CHANGE: 
					GetWindowTextW(hwndCtrl, szProxy, sizeof(szProxy)/sizeof(wchar_t));
					EnableWindow(GetDlgItem(hwnd, IDC_CHK_PORT80), 0x00 != *szProxy);
					if(0x00 == *szProxy)
					{
						CheckDlgButton(hwnd, IDC_CHK_PORT80, BST_UNCHECKED);
						PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CHK_PORT80, BN_CLICKED), (LPARAM)GetDlgItem(hwnd, IDC_CHK_PORT80));
					}
					break;
			}
			break;
		case IDC_CHK_PORT80:
			switch(nEvntID)
			{
				case BN_CLICKED: bPort80 = (BST_CHECKED & (INT)SendMessageW(hwndCtrl, BM_GETSTATE, 0, 0L)); break;
			}
			break;
	}
}

INT_PTR setup_page_connect::PageDlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
		case WM_INITDIALOG: return OnInitDialog((HWND)wParam, lParam);
		case WM_DESTROY:		break;
		case WM_SIZE:		OnSize((UINT)wParam, LOWORD(lParam), HIWORD(lParam)); break;
		case WM_COMMAND:		OnCommand(LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
	}
	return 0;
}

static INT_PTR WINAPI DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	setup_page_connect *pInst = (setup_page_connect*)GetPropW(hwnd, L"SETUPPAGE");

	switch(uMsg)
	{
		case WM_INITDIALOG:
			pInst = (setup_page_connect*)lParam;
			if (pInst)
			{				
				pInst->hwnd = hwnd;
				SetPropW(hwnd, L"SETUPPAGE", pInst);
			}
			break;
		case WM_DESTROY:
			if (pInst) 
			{	
				pInst->PageDlgProc(uMsg, wParam, lParam);
				RemovePropW(hwnd,  L"SETUPPAGE");
				pInst = NULL;
			}
			break;
	}
	return (pInst) ? pInst->PageDlgProc(uMsg, wParam, lParam) : 0;
}

#ifdef CBCLASS
#undef CBCLASS
#endif

#define CBCLASS setup_page_connect
START_DISPATCH
CB(ADDREF, AddRef)
CB(RELEASE, Release)
CB(API_SETUPPAGE_GET_NAME, GetName)
CB(API_SETUPPAGE_CREATEVIEW, CreateView)
CB(API_SETUPPAGE_SAVE, Save)
CB(API_SETUPPAGE_REVERT, Revert)
CB(API_SETUPPAGE_ISDIRTY, IsDirty)
CB(API_SETUPPAGE_VALIDATE, Validate)
END_DISPATCH
#undef CBCLASS