spage_connect.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #define APSTUDIO_READONLY_SYMBOLS
  2. #include "main.h"
  3. #include "./spage_connect.h"
  4. #include "./setup_resource.h"
  5. #include "../nu/ns_wc.h"
  6. #include "./langutil.h"
  7. setup_page_connect::setup_page_connect() : ref(1), hwnd(NULL)
  8. {
  9. inetMode = 0;
  10. bPort80 = FALSE;
  11. szProxy[MAX_PATH] = 0x00;
  12. }
  13. setup_page_connect::~setup_page_connect()
  14. {
  15. }
  16. size_t setup_page_connect::AddRef()
  17. {
  18. return ++ref;
  19. }
  20. size_t setup_page_connect::Release()
  21. {
  22. if (1 == ref)
  23. {
  24. delete(this);
  25. return 0;
  26. }
  27. return --ref;
  28. }
  29. HRESULT setup_page_connect::GetName(bool bShort, const wchar_t **pszName)
  30. {
  31. static wchar_t szName[64] = {0,};
  32. *pszName = (*szName) ? szName : getStringW(IDS_PAGE_CONNECTIVITY, szName, sizeof(szName)/sizeof(wchar_t));
  33. return S_OK;
  34. }
  35. HRESULT setup_page_connect::Save(HWND hwndText)
  36. {
  37. HRESULT hr(S_OK);
  38. INT count;
  39. wchar_t app[MAX_PATH] = {0}, *p;
  40. if (S_FALSE == IsDirty()) return S_OK;
  41. count = MultiByteToWideCharSZ(CP_ACP, 0, app_name, -1, app, sizeof(app)/sizeof(wchar_t));
  42. if (!count) hr = S_FALSE;
  43. if (S_FALSE == hr) return hr;
  44. if (config_inet_mode != inetMode)
  45. {
  46. wchar_t szText[MAX_PATH] = {0};
  47. config_inet_mode = (char)inetMode;
  48. WritePrivateProfileStringW(app, L"inet_mode", _itow(inetMode, szText, 10), INI_FILE);
  49. }
  50. if (2 == inetMode) szProxy[0] = 0x00;
  51. if (0 == *szProxy) config_proxy[0] = 0x00;
  52. else
  53. {
  54. count = WideCharToMultiByteSZ(CP_ACP, 0, szProxy, -1, config_proxy, sizeof(config_proxy)/sizeof(char), NULL, NULL);
  55. if (!count) hr = S_FALSE;
  56. }
  57. if (!WritePrivateProfileStringW(app, L"Proxy", szProxy, INI_FILE)) hr = S_FALSE;
  58. if (bPort80 && 0x00 == *szProxy) bPort80 = FALSE;
  59. p = (bPort80) ? L"1" : L"";
  60. if (!WritePrivateProfileStringW(app, L"proxyonly80", p, INI_FILE)) hr = S_FALSE;
  61. WritePrivateProfileStringW(app, L"proxy80", p, INI_FILE); // old
  62. if (hwnd) UpdateUI();
  63. return hr;
  64. }
  65. HRESULT setup_page_connect::Revert(void)
  66. {
  67. HRESULT hr(S_OK);
  68. INT count;
  69. wchar_t app[MAX_PATH] = {0};
  70. count = MultiByteToWideCharSZ(CP_ACP, 0, app_name, -1, app, sizeof(app)/sizeof(wchar_t));
  71. if (!count) hr = S_FALSE;
  72. if (config_inet_mode==3) isInetAvailable(); // autodetect
  73. inetMode = config_inet_mode;
  74. if (*config_proxy && 2 != inetMode)
  75. {
  76. count = MultiByteToWideCharSZ(CP_ACP, 0, config_proxy, -1, szProxy, sizeof(szProxy)/sizeof(wchar_t));
  77. if (!count) hr = S_FALSE;
  78. }
  79. else szProxy[0] = 0x00;
  80. bPort80 = (0x00 != *szProxy && 0 != GetPrivateProfileIntW(app, L"proxyonly80", 0, INI_FILE));
  81. if (hwnd) UpdateUI();
  82. return hr;
  83. }
  84. HRESULT setup_page_connect::IsDirty(void)
  85. {
  86. INT res;
  87. wchar_t app[MAX_PATH] = {0};
  88. char szTest[MAX_PATH] = {0};
  89. if (inetMode != config_inet_mode) return S_OK;
  90. if (2 == inetMode || 0 == *szProxy) szTest[0] = 0x00;
  91. else
  92. {
  93. res = WideCharToMultiByteSZ(CP_ACP, 0, szProxy, -1, szTest, sizeof(szTest)/sizeof(char), NULL, NULL);
  94. if (!res) return E_OUTOFMEMORY;
  95. }
  96. res = CompareStringA(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT),
  97. NORM_IGNORECASE, szTest, -1, config_proxy, -1);
  98. if (0 == res) return E_UNEXPECTED;
  99. if (CSTR_EQUAL != res) return S_OK;
  100. res = MultiByteToWideCharSZ(CP_ACP, 0, app_name, -1, app, sizeof(app)/sizeof(wchar_t));
  101. if (!res) return E_OUTOFMEMORY;
  102. return ((bPort80 && 0x00 != *szProxy) != ( 0 != GetPrivateProfileIntW(app, L"proxyonly80", 0, INI_FILE))) ? S_OK : S_FALSE;
  103. }
  104. HRESULT setup_page_connect::Validate(void)
  105. {
  106. return S_OK;
  107. }
  108. HRESULT setup_page_connect::CreateView(HWND hwndParent, HWND *phwnd)
  109. {
  110. *phwnd = WACreateDialogParam(MAKEINTRESOURCEW(IDD_SETUP_PAGE_CONNECT), hwndParent, ::DialogProc, (LPARAM)this);
  111. return S_OK;
  112. }
  113. void setup_page_connect::UpdateUI(void)
  114. {
  115. int count;
  116. if (!hwnd || !IsWindow(hwnd)) return;
  117. CheckDlgButton(hwnd, IDC_CHK_PORT80, (bPort80) ? BST_CHECKED : BST_UNCHECKED);
  118. SetDlgItemTextW(hwnd, IDC_EDT_SERVER, szProxy);
  119. HWND hwndCB = GetDlgItem(hwnd, IDC_CB_CONNECT);
  120. if (!hwndCB || !IsWindow(hwndCB)) return;
  121. count = (INT)SendMessageW(hwndCB, CB_GETCOUNT, 0, 0L);
  122. if (count > 0)
  123. {
  124. int index;
  125. for (index = 0; index < count && inetMode != (INT)SendMessageW(hwndCB, CB_GETITEMDATA, index, 0L); index++);
  126. if (index == count)
  127. {
  128. inetMode = (INT)SendMessageW(hwndCB, CB_GETITEMDATA, 0, 0L);
  129. index = 0;
  130. }
  131. SendMessageW(hwndCB, CB_SETCURSEL, index, 0L);
  132. PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CB_CONNECT, CBN_SELCHANGE), (LPARAM)hwndCB);
  133. }
  134. }
  135. void setup_page_connect::ComboBox_OnSelChange(HWND hwndCtrl)
  136. {
  137. INT proxyList[] = { IDC_GRP_PROXY, IDC_LBL_SERVER, IDC_EDT_SERVER, IDC_CHK_PORT80, };
  138. INT index;
  139. index = (INT)SendMessageW(hwndCtrl, CB_GETCURSEL, 0, 0L);
  140. if (CB_ERR == index) return;
  141. inetMode = (INT)SendMessageW(hwndCtrl, CB_GETITEMDATA, index, 0L);
  142. for (int i = 0; i < sizeof(proxyList)/sizeof(INT); i++)
  143. {
  144. HWND hwndItem = GetDlgItem(hwnd, proxyList[i]);
  145. if (IsWindow(hwndItem)) ShowWindow(hwndItem, (2 != inetMode) ? SW_SHOWNA : SW_HIDE);
  146. }
  147. }
  148. INT_PTR setup_page_connect::OnInitDialog(HWND hwndFocus, LPARAM lParam)
  149. {
  150. UINT connID[] = { IDS_INST_INET1, IDS_INST_INET2, IDS_INST_INET3, };
  151. HWND hwndCB = GetDlgItem(hwnd, IDC_CB_CONNECT);
  152. CheckDlgButton(hwnd, IDC_CHK_PORT80, (bPort80) ? BST_CHECKED : BST_UNCHECKED);
  153. //SendDlgItemMessageW(hwnd, IDC_EDT_SERVER, EM_LIMITTEXT, sizeof(szProxy)/sizeof(wchar_t), 0);
  154. SetDlgItemTextW(hwnd, IDC_EDT_SERVER, szProxy);
  155. if (inetMode < 0) inetMode = 0;
  156. else if (inetMode >= sizeof(connID)/sizeof(INT)) inetMode = sizeof(connID)/sizeof(INT) - 1;
  157. for(int i = 0; i < sizeof(connID)/sizeof(INT); i++)
  158. {
  159. INT index = (INT)SendMessageW(hwndCB, CB_ADDSTRING,0, (LPARAM)getStringW(connID[i], NULL, 0));
  160. if (CB_ERR != index) SendMessageW(hwndCB, CB_SETITEMDATA, index, (LPARAM)i);
  161. if (inetMode == i) SendMessageW(hwndCB, CB_SETCURSEL, index, 0L);
  162. }
  163. PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CB_CONNECT, CBN_SELCHANGE), (LPARAM)hwndCB);
  164. return 0;
  165. }
  166. void setup_page_connect::OnSize(UINT nType, INT cx, INT cy)
  167. {
  168. INT ctrlList[] = { IDC_CB_CONNECT, IDC_GRP_PROXY, IDC_LBL_SERVER, IDC_EDT_SERVER, IDC_CHK_PORT80, };
  169. RECT rw;
  170. HDWP hdwp = BeginDeferWindowPos(sizeof(ctrlList)/sizeof(INT));
  171. if (!hdwp) return;
  172. for (int i = 0; i < sizeof(ctrlList)/sizeof(INT); i++)
  173. {
  174. HWND hwndCtrl = GetDlgItem(hwnd, ctrlList[i]);
  175. if (hwndCtrl)
  176. {
  177. GetWindowRect(hwndCtrl, &rw);
  178. MapWindowPoints(HWND_DESKTOP, hwnd, (POINT*)&rw, 2);
  179. hdwp = DeferWindowPos(hdwp, hwndCtrl, NULL, max(0, (cx - (rw.right - rw.left))/2), rw.top, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
  180. }
  181. }
  182. if (hdwp) EndDeferWindowPos(hdwp);
  183. }
  184. void setup_page_connect::OnCommand(INT nCtrlID, INT nEvntID, HWND hwndCtrl)
  185. {
  186. switch(nCtrlID)
  187. {
  188. case IDC_CB_CONNECT:
  189. switch(nEvntID)
  190. {
  191. case CBN_SELCHANGE: ComboBox_OnSelChange(hwndCtrl); break;
  192. }
  193. break;
  194. case IDC_EDT_SERVER:
  195. switch(nEvntID)
  196. {
  197. case EN_CHANGE:
  198. GetWindowTextW(hwndCtrl, szProxy, sizeof(szProxy)/sizeof(wchar_t));
  199. EnableWindow(GetDlgItem(hwnd, IDC_CHK_PORT80), 0x00 != *szProxy);
  200. if(0x00 == *szProxy)
  201. {
  202. CheckDlgButton(hwnd, IDC_CHK_PORT80, BST_UNCHECKED);
  203. PostMessageW(hwnd, WM_COMMAND, MAKEWPARAM(IDC_CHK_PORT80, BN_CLICKED), (LPARAM)GetDlgItem(hwnd, IDC_CHK_PORT80));
  204. }
  205. break;
  206. }
  207. break;
  208. case IDC_CHK_PORT80:
  209. switch(nEvntID)
  210. {
  211. case BN_CLICKED: bPort80 = (BST_CHECKED & (INT)SendMessageW(hwndCtrl, BM_GETSTATE, 0, 0L)); break;
  212. }
  213. break;
  214. }
  215. }
  216. INT_PTR setup_page_connect::PageDlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  217. {
  218. switch(uMsg)
  219. {
  220. case WM_INITDIALOG: return OnInitDialog((HWND)wParam, lParam);
  221. case WM_DESTROY: break;
  222. case WM_SIZE: OnSize((UINT)wParam, LOWORD(lParam), HIWORD(lParam)); break;
  223. case WM_COMMAND: OnCommand(LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
  224. }
  225. return 0;
  226. }
  227. static INT_PTR WINAPI DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  228. {
  229. setup_page_connect *pInst = (setup_page_connect*)GetPropW(hwnd, L"SETUPPAGE");
  230. switch(uMsg)
  231. {
  232. case WM_INITDIALOG:
  233. pInst = (setup_page_connect*)lParam;
  234. if (pInst)
  235. {
  236. pInst->hwnd = hwnd;
  237. SetPropW(hwnd, L"SETUPPAGE", pInst);
  238. }
  239. break;
  240. case WM_DESTROY:
  241. if (pInst)
  242. {
  243. pInst->PageDlgProc(uMsg, wParam, lParam);
  244. RemovePropW(hwnd, L"SETUPPAGE");
  245. pInst = NULL;
  246. }
  247. break;
  248. }
  249. return (pInst) ? pInst->PageDlgProc(uMsg, wParam, lParam) : 0;
  250. }
  251. #ifdef CBCLASS
  252. #undef CBCLASS
  253. #endif
  254. #define CBCLASS setup_page_connect
  255. START_DISPATCH
  256. CB(ADDREF, AddRef)
  257. CB(RELEASE, Release)
  258. CB(API_SETUPPAGE_GET_NAME, GetName)
  259. CB(API_SETUPPAGE_CREATEVIEW, CreateView)
  260. CB(API_SETUPPAGE_SAVE, Save)
  261. CB(API_SETUPPAGE_REVERT, Revert)
  262. CB(API_SETUPPAGE_ISDIRTY, IsDirty)
  263. CB(API_SETUPPAGE_VALIDATE, Validate)
  264. END_DISPATCH
  265. #undef CBCLASS