Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

512 lines
20 KiB

  1. #include "status.h"
  2. #include "properties.h"
  3. #include "util.h"
  4. CStatusDialog::CStatusDialog(IInternetGateway* pInternetGateway)
  5. {
  6. m_pInternetGateway = pInternetGateway;
  7. m_pInternetGateway->AddRef();
  8. m_uTimerId = 0;
  9. m_bGettingStatistics = FALSE;
  10. m_bShowingBytes = TRUE;
  11. }
  12. CStatusDialog::~CStatusDialog()
  13. {
  14. m_pInternetGateway->Release();
  15. }
  16. LRESULT CStatusDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  17. {
  18. m_uTimerId = SetTimer(1, 1000);
  19. TCHAR szCloseText[64];
  20. if(0 != ::LoadString(_Module.GetResourceInstance(), IDS_CLOSE, szCloseText, sizeof(szCloseText) / sizeof(TCHAR)))
  21. {
  22. ::SetDlgItemText(GetParent(), IDCANCEL, szCloseText); // set the cancel to close
  23. }
  24. ::ShowWindow(::GetDlgItem(GetParent(), IDOK), SW_HIDE); // hide the original close
  25. ::EnableWindow(::GetDlgItem(GetParent(), IDCANCEL), TRUE); // and re-enable the cancel button
  26. IUPnPService* pWANConnectionService;
  27. HRESULT hr = GetWANConnectionService(m_pInternetGateway, &pWANConnectionService);
  28. if(SUCCEEDED(hr))
  29. {
  30. NETCON_STATUS Status;
  31. hr = GetConnectionStatus(pWANConnectionService, &Status);
  32. if(SUCCEEDED(hr))
  33. {
  34. UpdateButtons(Status);
  35. }
  36. pWANConnectionService->Release();
  37. }
  38. return TRUE;
  39. }
  40. LRESULT CStatusDialog::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  41. {
  42. if(0 != m_uTimerId)
  43. {
  44. ::KillTimer(m_hWnd, m_uTimerId);
  45. }
  46. return 0;
  47. }
  48. LRESULT CStatusDialog::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  49. {
  50. HRESULT hr = S_OK;
  51. if(FALSE == m_bGettingStatistics) // this functions pumps messages, so don't let us be re-entered
  52. {
  53. m_bGettingStatistics = TRUE;
  54. NETCON_STATUS Status = NCS_CONNECTED;
  55. ULONG ulTotalBytesSent = 0;
  56. ULONG ulTotalBytesReceived = 0;
  57. ULONG ulTotalPacketsSent = 0;
  58. ULONG ulTotalPacketsReceived = 0;
  59. ULONG ulSpeedbps = 0;
  60. ULONG ulUptime = 0;
  61. BOOL bStatisticsAvailable = TRUE;
  62. IUPnPService* pWANConnection;
  63. hr = GetWANConnectionService(m_pInternetGateway, &pWANConnection);
  64. if(SUCCEEDED(hr))
  65. {
  66. IUPnPService* pWANCommonInterfaceConfig;
  67. hr = m_pInternetGateway->GetService(SAHOST_SERVICE_WANCOMMONINTERFACECONFIG, &pWANCommonInterfaceConfig);
  68. if(SUCCEEDED(hr))
  69. {
  70. hr = GetConnectionStatus(pWANConnection, &Status);
  71. if(SUCCEEDED(hr))
  72. {
  73. if(NCS_CONNECTED == Status)
  74. {
  75. VARIANT OutArgs;
  76. VariantInit(&OutArgs);
  77. hr = InvokeVoidAction(pWANCommonInterfaceConfig, L"X_GetICSStatistics", &OutArgs);
  78. if(SUCCEEDED(hr))
  79. {
  80. SAFEARRAY* pArray = V_ARRAY(&OutArgs);
  81. LONG lIndex = 0;
  82. VARIANT Param;
  83. lIndex = 0;
  84. hr = SafeArrayGetElement(pArray, &lIndex, &Param);
  85. if(SUCCEEDED(hr))
  86. {
  87. if(V_VT(&Param) == VT_UI4)
  88. {
  89. ulTotalBytesSent = V_UI4(&Param);
  90. }
  91. VariantClear(&Param);
  92. }
  93. lIndex = 1;
  94. hr = SafeArrayGetElement(pArray, &lIndex, &Param);
  95. if(SUCCEEDED(hr))
  96. {
  97. if(V_VT(&Param) == VT_UI4)
  98. {
  99. ulTotalBytesReceived = V_UI4(&Param);
  100. }
  101. VariantClear(&Param);
  102. }
  103. lIndex = 2;
  104. hr = SafeArrayGetElement(pArray, &lIndex, &Param);
  105. if(SUCCEEDED(hr))
  106. {
  107. if(V_VT(&Param) == VT_UI4)
  108. {
  109. ulTotalPacketsSent = V_UI4(&Param);
  110. }
  111. VariantClear(&Param);
  112. }
  113. lIndex = 3;
  114. hr = SafeArrayGetElement(pArray, &lIndex, &Param);
  115. if(SUCCEEDED(hr))
  116. {
  117. if(V_VT(&Param) == VT_UI4)
  118. {
  119. ulTotalPacketsReceived = V_UI4(&Param);
  120. }
  121. VariantClear(&Param);
  122. }
  123. lIndex = 4;
  124. hr = SafeArrayGetElement(pArray, &lIndex, &Param);
  125. if(SUCCEEDED(hr))
  126. {
  127. if(V_VT(&Param) == VT_UI4)
  128. {
  129. ulSpeedbps = V_UI4(&Param);
  130. }
  131. VariantClear(&Param);
  132. }
  133. lIndex = 5;
  134. hr = SafeArrayGetElement(pArray, &lIndex, &Param);
  135. if(SUCCEEDED(hr))
  136. {
  137. if(V_VT(&Param) == VT_UI4)
  138. {
  139. ulUptime = V_UI4(&Param);
  140. }
  141. VariantClear(&Param);
  142. }
  143. VariantClear(&OutArgs);
  144. }
  145. else if(UPNP_E_INVALID_ACTION == hr)
  146. {
  147. VARIANT OutArgs;
  148. LONG lIndex = 0;
  149. VARIANT Param;
  150. hr = InvokeVoidAction(pWANCommonInterfaceConfig, L"GetTotalBytesSent", &OutArgs);
  151. if(SUCCEEDED(hr))
  152. {
  153. lIndex = 0;
  154. hr = SafeArrayGetElement(V_ARRAY(&OutArgs), &lIndex, &Param);
  155. if(SUCCEEDED(hr))
  156. {
  157. if(V_VT(&Param) == VT_UI4)
  158. {
  159. ulTotalBytesSent = V_UI4(&Param);
  160. }
  161. VariantClear(&Param);
  162. }
  163. VariantClear(&OutArgs);
  164. }
  165. if(SUCCEEDED(hr))
  166. {
  167. lIndex = 0;
  168. hr = InvokeVoidAction(pWANCommonInterfaceConfig, L"GetTotalBytesReceived", &OutArgs);
  169. if(SUCCEEDED(hr))
  170. {
  171. hr = SafeArrayGetElement(V_ARRAY(&OutArgs), &lIndex, &Param);
  172. if(SUCCEEDED(hr))
  173. {
  174. if(V_VT(&Param) == VT_UI4)
  175. {
  176. ulTotalBytesReceived = V_UI4(&Param);
  177. }
  178. VariantClear(&Param);
  179. }
  180. VariantClear(&OutArgs);
  181. }
  182. }
  183. if(SUCCEEDED(hr))
  184. {
  185. lIndex = 0;
  186. hr = InvokeVoidAction(pWANCommonInterfaceConfig, L"GetTotalPacketsSent", &OutArgs);
  187. if(SUCCEEDED(hr))
  188. {
  189. hr = SafeArrayGetElement(V_ARRAY(&OutArgs), &lIndex, &Param);
  190. if(SUCCEEDED(hr))
  191. {
  192. if(V_VT(&Param) == VT_UI4)
  193. {
  194. ulTotalPacketsSent = V_UI4(&Param);
  195. }
  196. VariantClear(&Param);
  197. }
  198. VariantClear(&OutArgs);
  199. }
  200. }
  201. if(SUCCEEDED(hr))
  202. {
  203. hr = InvokeVoidAction(pWANCommonInterfaceConfig, L"GetTotalPacketsReceived", &OutArgs);
  204. if(SUCCEEDED(hr))
  205. {
  206. lIndex = 0;
  207. hr = SafeArrayGetElement(V_ARRAY(&OutArgs), &lIndex, &Param);
  208. if(SUCCEEDED(hr))
  209. {
  210. if(V_VT(&Param) == VT_UI4)
  211. {
  212. ulTotalPacketsReceived = V_UI4(&Param);
  213. }
  214. VariantClear(&Param);
  215. }
  216. VariantClear(&OutArgs);
  217. }
  218. }
  219. if(SUCCEEDED(hr))
  220. {
  221. hr = InvokeVoidAction(pWANCommonInterfaceConfig, L"GetCommonLinkProperties", &OutArgs);
  222. if(SUCCEEDED(hr))
  223. {
  224. lIndex = 2;
  225. hr = SafeArrayGetElement(V_ARRAY(&OutArgs), &lIndex, &Param);
  226. if(SUCCEEDED(hr))
  227. {
  228. if(V_VT(&Param) == VT_UI4)
  229. {
  230. ulSpeedbps = V_UI4(&Param);
  231. }
  232. VariantClear(&Param);
  233. }
  234. VariantClear(&OutArgs);
  235. }
  236. }
  237. if(SUCCEEDED(hr))
  238. {
  239. hr = InvokeVoidAction(pWANConnection, L"GetStatusInfo", &OutArgs);
  240. if(SUCCEEDED(hr))
  241. {
  242. lIndex = 2;
  243. hr = SafeArrayGetElement(V_ARRAY(&OutArgs), &lIndex, &Param);
  244. if(SUCCEEDED(hr))
  245. {
  246. if(V_VT(&Param) == VT_UI4)
  247. {
  248. ulUptime = V_UI4(&Param);
  249. }
  250. VariantClear(&Param);
  251. }
  252. VariantClear(&OutArgs);
  253. }
  254. }
  255. if(UPNP_E_INVALID_ACTION == hr)
  256. {
  257. hr = S_OK; // server does not support statistics
  258. bStatisticsAvailable = FALSE;
  259. }
  260. }
  261. }
  262. }
  263. pWANCommonInterfaceConfig->Release();
  264. }
  265. pWANConnection->Release();
  266. }
  267. if(SUCCEEDED(hr) && NCS_CONNECTED == Status)
  268. {
  269. if(TRUE == bStatisticsAvailable)
  270. {
  271. if(0 != ulTotalBytesSent && 0 != ulTotalBytesReceived)
  272. {
  273. if(FALSE == m_bShowingBytes) // switch labels
  274. {
  275. m_bShowingBytes = TRUE;
  276. ::ShowWindow(GetDlgItem(IDC_STATUS_BYTESLABEL), SW_SHOW);
  277. ::ShowWindow(GetDlgItem(IDC_STATUS_PACKETSLABEL), SW_HIDE);
  278. }
  279. SetDlgItemInt(IDC_STATUS_BYTESSENT, ulTotalBytesSent, FALSE);
  280. SetDlgItemInt(IDC_STATUS_BYTESRECEIVED, ulTotalBytesReceived, FALSE);
  281. }
  282. else
  283. {
  284. if(TRUE == m_bShowingBytes) // switch labels
  285. {
  286. m_bShowingBytes = FALSE;
  287. ::ShowWindow(GetDlgItem(IDC_STATUS_PACKETSLABEL), SW_SHOW);
  288. ::ShowWindow(GetDlgItem(IDC_STATUS_BYTESLABEL), SW_HIDE);
  289. }
  290. SetDlgItemInt(IDC_STATUS_BYTESSENT, ulTotalPacketsSent, FALSE);
  291. SetDlgItemInt(IDC_STATUS_BYTESRECEIVED, ulTotalPacketsReceived, FALSE);
  292. }
  293. TCHAR szTimeDuration[128];
  294. hr = FormatTimeDuration(ulUptime, szTimeDuration, sizeof(szTimeDuration) / sizeof(TCHAR));
  295. if(SUCCEEDED(hr))
  296. {
  297. SetDlgItemText(IDC_STATUS_DURATION, szTimeDuration);
  298. } \
  299. TCHAR szBytesPerSecond[128];
  300. hr = FormatBytesPerSecond(ulSpeedbps, szBytesPerSecond, sizeof(szBytesPerSecond) / sizeof(TCHAR));
  301. if(SUCCEEDED(hr))
  302. {
  303. SetDlgItemText(IDC_STATUS_SPEED, szBytesPerSecond);
  304. }
  305. }
  306. else
  307. {
  308. TCHAR szNotAvailable[64];
  309. if(0 == LoadString(_Module.GetResourceInstance(), IDS_NOTAVAILABLE, szNotAvailable, sizeof(szNotAvailable) / sizeof(TCHAR)))
  310. {
  311. szNotAvailable[0] = TEXT('\0');
  312. }
  313. SetDlgItemText(IDC_STATUS_BYTESSENT, szNotAvailable);
  314. SetDlgItemText(IDC_STATUS_BYTESRECEIVED, szNotAvailable);
  315. SetDlgItemText(IDC_STATUS_DURATION, szNotAvailable);
  316. SetDlgItemText(IDC_STATUS_SPEED, szNotAvailable);
  317. }
  318. }
  319. else
  320. {
  321. SetDlgItemText(IDC_STATUS_BYTESSENT, TEXT(""));
  322. SetDlgItemText(IDC_STATUS_BYTESRECEIVED, TEXT(""));
  323. SetDlgItemText(IDC_STATUS_DURATION, TEXT(""));
  324. SetDlgItemText(IDC_STATUS_SPEED, TEXT(""));
  325. }
  326. if(SUCCEEDED(hr) || UPNP_E_ACTION_REQUEST_FAILED == hr) // if we disconnected after getting status this will fail
  327. {
  328. UpdateButtons(Status);
  329. TCHAR szConnectionStatus[64];
  330. hr = ConnectionStatusToString(Status, szConnectionStatus, sizeof(szConnectionStatus) / sizeof(TCHAR));
  331. if(SUCCEEDED(hr))
  332. {
  333. SetDlgItemText(IDC_STATUS_STATUS, szConnectionStatus);
  334. }
  335. }
  336. if(FAILED(hr))
  337. {
  338. ::PropSheet_PressButton(GetParent(), PSBTN_CANCEL);
  339. }
  340. m_bGettingStatistics = FALSE;
  341. }
  342. return 0;
  343. }
  344. LRESULT CStatusDialog::OnProperties(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  345. {
  346. HWND hPropertySheet = GetParent();
  347. if(NULL != hPropertySheet)
  348. {
  349. HWND hTopLevelWindow = ::GetParent(hPropertySheet);
  350. if(NULL != hTopLevelWindow)
  351. {
  352. ::PostMessage(hTopLevelWindow, WM_COMMAND, IDM_TRAYICON_PROPERTIES, (LPARAM)hPropertySheet);
  353. }
  354. }
  355. return 0;
  356. }
  357. LRESULT CStatusDialog::OnDisconnect(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  358. {
  359. HRESULT hr = S_OK;
  360. IUPnPService* pWANConnectionService;
  361. hr = GetWANConnectionService(m_pInternetGateway, &pWANConnectionService);
  362. if(SUCCEEDED(hr))
  363. {
  364. VARIANT OutArgs;
  365. hr = InvokeVoidAction(pWANConnectionService, L"ForceTermination", &OutArgs);
  366. if(SUCCEEDED(hr))
  367. {
  368. VariantClear(&OutArgs);
  369. }
  370. else if(UPNP_ACTION_HRESULT(800) == hr)
  371. {
  372. ShowErrorDialog(m_hWnd, IDS_ACCESSDENIED);
  373. }
  374. pWANConnectionService->Release();
  375. }
  376. return 0;
  377. }
  378. LRESULT CStatusDialog::OnConnect(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  379. {
  380. HRESULT hr = S_OK;
  381. IUPnPService* pWANConnectionService;
  382. hr = GetWANConnectionService(m_pInternetGateway, &pWANConnectionService);
  383. if(SUCCEEDED(hr))
  384. {
  385. VARIANT OutArgs;
  386. hr = InvokeVoidAction(pWANConnectionService, L"RequestConnection", &OutArgs);
  387. if(SUCCEEDED(hr))
  388. {
  389. VariantClear(&OutArgs);
  390. }
  391. else if(UPNP_ACTION_HRESULT(800) == hr)
  392. {
  393. ShowErrorDialog(m_hWnd, IDS_ACCESSDENIED);
  394. }
  395. else if(UPNP_E_DEVICE_TIMEOUT != hr)
  396. {
  397. ShowErrorDialog(m_hWnd, IDS_CONNECTIONFAILED);
  398. }
  399. pWANConnectionService->Release();
  400. }
  401. return 0;
  402. }
  403. HRESULT CStatusDialog::UpdateButtons(NETCON_STATUS Status)
  404. {
  405. NETCON_MEDIATYPE MediaType;
  406. HRESULT hr = m_pInternetGateway->GetMediaType(&MediaType);
  407. if(SUCCEEDED(hr))
  408. {
  409. TCHAR szButtonText[64];
  410. if(NCS_CONNECTED == Status)
  411. {
  412. if(NCM_SHAREDACCESSHOST_RAS == MediaType)
  413. {
  414. if(0 != LoadString(_Module.GetResourceInstance(), IDS_DISCONNECT, szButtonText, sizeof(szButtonText) / sizeof(TCHAR)))
  415. {
  416. SetDlgItemText(IDC_STATUS_DISCONNECT, szButtonText);
  417. }
  418. }
  419. else
  420. {
  421. if(0 != LoadString(_Module.GetResourceInstance(), IDS_DISABLE, szButtonText, sizeof(szButtonText) / sizeof(TCHAR)))
  422. {
  423. SetDlgItemText(IDC_STATUS_DISCONNECT, szButtonText);
  424. }
  425. }
  426. ::ShowWindow(GetDlgItem(IDC_STATUS_DISCONNECT), SW_SHOW);
  427. ::ShowWindow(GetDlgItem(IDC_STATUS_CONNECT), SW_HIDE);
  428. }
  429. else if(NCS_DISCONNECTED == Status)
  430. {
  431. if(NCM_SHAREDACCESSHOST_RAS == MediaType)
  432. {
  433. if(0 != LoadString(_Module.GetResourceInstance(), IDS_CONNECT, szButtonText, sizeof(szButtonText) / sizeof(TCHAR)))
  434. {
  435. SetDlgItemText(IDC_STATUS_CONNECT, szButtonText);
  436. }
  437. }
  438. else
  439. {
  440. if(0 != LoadString(_Module.GetResourceInstance(), IDS_ENABLE, szButtonText, sizeof(szButtonText) / sizeof(TCHAR)))
  441. {
  442. SetDlgItemText(IDC_STATUS_CONNECT, szButtonText);
  443. }
  444. }
  445. ::ShowWindow(GetDlgItem(IDC_STATUS_CONNECT), SW_SHOW);
  446. ::ShowWindow(GetDlgItem(IDC_STATUS_DISCONNECT), SW_HIDE);
  447. }
  448. }
  449. return 0;
  450. }