Leaked source code of windows server 2003
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.

400 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: S A U I O B J. C P P
  7. //
  8. // Contents: Implementation of the LAN ConnectionUI object
  9. //
  10. // Notes:
  11. //
  12. // Created: tongl 8 Oct 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "ncnetcon.h"
  18. #include "ncras.h"
  19. #include "sauiobj.h"
  20. #include "saui.h"
  21. #include "resource.h"
  22. #include "lanhelp.h"
  23. #include "lanui.h"
  24. #include "ncui.h"
  25. //+---------------------------------------------------------------------------
  26. // INetConnectionUI
  27. //
  28. //+---------------------------------------------------------------------------
  29. //
  30. // Member: CSharedAccessConnectionUi::SetConnection
  31. //
  32. // Purpose: Sets the LAN connection that this UI object will operate upon
  33. //
  34. // Arguments:
  35. // pCon [in] LAN connection object to operate on. Can be NULL.
  36. //
  37. // Returns: S_OK if success, OLE error otherwise
  38. //
  39. // Author: danielwe 16 Oct 1997
  40. //
  41. // Notes:
  42. //
  43. STDMETHODIMP CSharedAccessConnectionUi::SetConnection(INetConnection* pCon)
  44. {
  45. HRESULT hr = S_OK;
  46. ReleaseObj(m_pconn);
  47. m_pconn = pCon;
  48. AddRefObj(m_pconn);
  49. TraceError("CSharedAccessConnectionUi::SetConnection", hr);
  50. return hr;
  51. }
  52. //+---------------------------------------------------------------------------
  53. //
  54. // Member: CSharedAccessConnectionUi::Connect
  55. //
  56. // Purpose: Tells the connection to connect, optionally displaying UI of
  57. // connection progress.
  58. //
  59. // Arguments:
  60. // hwndParent [in] Parent window for UI
  61. // dwFlags [in] Flags affecting how UI is shown
  62. //
  63. // Returns: S_OK if success, OLE error otherwise
  64. //
  65. // Author: danielwe 16 Oct 1997
  66. //
  67. // Notes:
  68. //
  69. STDMETHODIMP CSharedAccessConnectionUi::Connect(HWND hwndParent, DWORD dwFlags)
  70. {
  71. HRESULT hr = S_OK;
  72. if (!m_pconn)
  73. {
  74. hr = E_UNEXPECTED;
  75. }
  76. else
  77. {
  78. CSharedAccessConnectionUiDlg dlg; // we are borrowing the CLanConnectionUiDlg because it works so well for us.
  79. HWND hwndDlg;
  80. NETCON_MEDIATYPE MediaType = NCM_NONE; // assume no ui
  81. NETCON_PROPERTIES* pProperties;
  82. hr = m_pconn->GetProperties(&pProperties);
  83. if(SUCCEEDED(hr))
  84. {
  85. MediaType = pProperties->MediaType;
  86. FreeNetconProperties(pProperties);
  87. }
  88. else
  89. {
  90. hr = S_OK; // ok if prev fails
  91. }
  92. if (!(dwFlags & NCUC_NO_UI))
  93. {
  94. // Display UI prior to connect
  95. //
  96. dlg.SetConnection(m_pconn);
  97. hwndDlg = dlg.Create(hwndParent);
  98. if (!hwndDlg)
  99. {
  100. hr = E_FAIL;
  101. }
  102. }
  103. if (SUCCEEDED(hr))
  104. {
  105. hr = m_pconn->Connect();
  106. // Sleep a bit so they can read the text
  107. Sleep(1000);
  108. if (!(dwFlags & NCUC_NO_UI))
  109. {
  110. SetDlgItemText(hwndDlg, IDC_TXT_Caption, c_szEmpty);
  111. Sleep(100);
  112. UINT ids = SUCCEEDED(hr) ?
  113. IDS_SHAREDACCESSUI_CONNECTED :
  114. IDS_LAN_CONNECT_FAILED;
  115. PCWSTR szwResult = SzLoadIds(ids);
  116. SetDlgItemText(hwndDlg, IDC_TXT_Caption, szwResult);
  117. // Sleep a bit so they can read the text
  118. Sleep(1000);
  119. DestroyWindow(hwndDlg);
  120. if(E_ACCESSDENIED == hr)
  121. {
  122. NcMsgBox(_Module.GetResourceInstance(), NULL, IDS_CONFOLD_WARNING_CAPTION, IDS_SHAREDACCESSUI_ACCESSDENIED, MB_OK | MB_ICONEXCLAMATION);
  123. hr = S_OK; // handled the error
  124. }
  125. }
  126. }
  127. }
  128. TraceHr(ttidError, FAL, hr, FALSE, "CSharedAccessConnectionUi::Connect");
  129. return hr;
  130. }
  131. STDMETHODIMP CSharedAccessConnectionUi::Disconnect(HWND hwndParent, DWORD dwFlags)
  132. {
  133. HRESULT hr = S_OK;
  134. if (!m_pconn)
  135. {
  136. hr = E_UNEXPECTED;
  137. }
  138. else
  139. {
  140. hr = m_pconn->Disconnect();
  141. if(E_ACCESSDENIED == hr)
  142. {
  143. NcMsgBox(_Module.GetResourceInstance(), NULL, IDS_CONFOLD_WARNING_CAPTION, IDS_SHAREDACCESSUI_ACCESSDENIED, MB_OK | MB_ICONEXCLAMATION);
  144. hr = S_OK; // handled the error
  145. }
  146. }
  147. TraceHr (ttidError, FAL, hr, FALSE, "CSharedAccessConnectionUi::Disconnect");
  148. return hr;
  149. }
  150. //+---------------------------------------------------------------------------
  151. // INetConnectionPropertyUi2
  152. //
  153. //+---------------------------------------------------------------------------
  154. //
  155. // Member: CSharedAccessConnectionUi::AddPages
  156. //
  157. // Purpose: Called when our UI object shoud add its pages to a property
  158. // sheet for the connection UI owned by the shell.
  159. //
  160. // Arguments:
  161. // pfnAddPage [in] Callback function to add the page
  162. // lParam [in] User-defined paramter required by the callback
  163. // function.
  164. //
  165. // Returns: S_OK if succeeded, otherwise OLE error.
  166. //
  167. // Author: danielwe 28 Oct 1997
  168. //
  169. // Notes:
  170. //
  171. STDMETHODIMP CSharedAccessConnectionUi::AddPages(HWND hwndParent,
  172. LPFNADDPROPSHEETPAGE pfnAddPage,
  173. LPARAM lParam)
  174. {
  175. HRESULT hr = S_OK;
  176. if (!pfnAddPage)
  177. {
  178. hr = E_POINTER;
  179. }
  180. else if (!m_pconn)
  181. {
  182. hr = E_UNEXPECTED;
  183. }
  184. else
  185. {
  186. NETCON_PROPERTIES* pProperties;
  187. hr = m_pconn->GetProperties(&pProperties);
  188. if(SUCCEEDED(hr))
  189. {
  190. if (!m_pspSharedAccessPage)
  191. {
  192. m_pspSharedAccessPage = new CSharedAccessPage(static_cast<INetConnectionPropertyUi *>(this),
  193. m_pnc, m_pconn, m_fReadOnly, m_fNeedReboot,
  194. m_fAccessDenied, g_aHelpIDs_IDD_SHAREDACCESS_GENERAL);
  195. }
  196. if (m_pspSharedAccessPage)
  197. {
  198. (VOID) pfnAddPage(m_pspSharedAccessPage->CreatePage(IDD_SHAREDACCESS_GENERAL, 0),
  199. lParam);
  200. }
  201. FreeNetconProperties(pProperties);
  202. }
  203. }
  204. TraceError("CSharedAccessConnectionUi::AddPages(INetConnectionPropertyUi)", hr);
  205. return hr;
  206. }
  207. STDMETHODIMP
  208. CSharedAccessConnectionUi::GetIcon (
  209. DWORD dwSize,
  210. HICON *phIcon )
  211. {
  212. HRESULT hr;
  213. Assert (phIcon);
  214. hr = HrGetIconFromMediaType(dwSize, NCM_SHAREDACCESSHOST_LAN, NCSM_NONE, 7, 0, phIcon);
  215. TraceError ("CLanConnectionUi::GetIcon (INetConnectionPropertyUi2)", hr);
  216. return hr;
  217. }
  218. //
  219. // INetConnectionUiLock
  220. //
  221. //+---------------------------------------------------------------------------
  222. //
  223. // Member: CSharedAccessConnectionUi::QueryLock
  224. //
  225. // Purpose: Causes the UI object to attempt to get the INetCfg write lock.
  226. //
  227. // Arguments:
  228. // ppszwLockHolder [out] Description of component that holds the
  229. // write lock in the event that it couldn't be
  230. // obtained.
  231. //
  232. // Returns: S_OK if success, S_FALSE if write lock couldn't be obtained,
  233. // OLE or Win32 error otherwise
  234. //
  235. // Author: danielwe 13 Nov 1997
  236. //
  237. // Notes:
  238. //
  239. STDMETHODIMP CSharedAccessConnectionUi::QueryLock(PWSTR* ppszwLockHolder)
  240. {
  241. HRESULT hr = S_OK;
  242. if (!ppszwLockHolder)
  243. {
  244. hr = E_POINTER;
  245. }
  246. else
  247. {
  248. INetCfgLock * pnclock;
  249. AssertSz(!m_pnc, "We're assuming this is in the property sheet "
  250. "context and we don't yet have an INetCfg!");
  251. *ppszwLockHolder = NULL;
  252. // Instantiate an INetCfg
  253. hr = CoCreateInstance(
  254. CLSID_CNetCfg,
  255. NULL,
  256. CLSCTX_INPROC_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  257. IID_INetCfg,
  258. reinterpret_cast<LPVOID *>(&m_pnc));
  259. TraceHr(ttidError, FAL, hr, FALSE, "CoCreateInstance");
  260. if (SUCCEEDED(hr))
  261. {
  262. // Get the locking interface
  263. hr = m_pnc->QueryInterface(IID_INetCfgLock,
  264. reinterpret_cast<LPVOID *>(&pnclock));
  265. if (SUCCEEDED(hr))
  266. {
  267. // Attempt to lock the INetCfg for read/write
  268. hr = pnclock->AcquireWriteLock(0,
  269. SzLoadIds(IDS_SHAREDACCESSUI_LOCK_DESC), ppszwLockHolder);
  270. ReleaseObj(pnclock);
  271. if (NETCFG_E_NEED_REBOOT == hr)
  272. {
  273. // Can't make any changes because we are pending a reboot.
  274. m_fReadOnly = TRUE;
  275. m_fNeedReboot = TRUE;
  276. hr = S_OK;
  277. }
  278. else if(E_ACCESSDENIED == hr)
  279. {
  280. // user not logged on as admin
  281. //
  282. m_fReadOnly = TRUE;
  283. m_fAccessDenied = TRUE;
  284. hr = S_OK;
  285. }
  286. else if (S_FALSE == hr)
  287. {
  288. // We don't have sufficent rights
  289. //
  290. m_fReadOnly = TRUE;
  291. hr = S_OK;
  292. }
  293. }
  294. }
  295. }
  296. TraceError("CSharedAccessConnectionUi::QueryLock", (S_FALSE == hr) ? S_OK : hr);
  297. return hr;
  298. }
  299. //+---------------------------------------------------------------------------
  300. //
  301. // Member: CSharedAccessConnectionUiDlg::OnInitDialog
  302. //
  303. // Purpose: Handles the WM_INITDIALOG message.
  304. //
  305. // Arguments:
  306. // uMsg []
  307. // wParam []
  308. // lParam []
  309. // bHandled []
  310. //
  311. // Returns: TRUE
  312. //
  313. // Author: kenwic 19 Sep 2000
  314. //
  315. // Notes:
  316. //
  317. LRESULT CSharedAccessConnectionUiDlg::OnInitDialog(UINT uMsg, WPARAM wParam,
  318. LPARAM lParam, BOOL& bHandled)
  319. {
  320. HRESULT hr = S_OK;
  321. NETCON_PROPERTIES* pProps;
  322. AssertSz(m_pconn, "No connection object in dialog!");
  323. hr = m_pconn->GetProperties(&pProps);
  324. if (SUCCEEDED(hr))
  325. {
  326. SetDlgItemText(IDC_TXT_Caption, SzLoadIds(IDS_SHAREDACCESSUI_CONNECTING));
  327. SetWindowText(pProps->pszwName);
  328. HICON hLanIconSmall;
  329. HICON hLanIconBig;
  330. hr = HrGetIconFromMediaType(GetSystemMetrics(SM_CXSMICON), NCM_SHAREDACCESSHOST_LAN, NCSM_NONE, 7, 0, &hLanIconSmall);
  331. if (SUCCEEDED(hr))
  332. {
  333. hr = HrGetIconFromMediaType(GetSystemMetrics(SM_CXICON), NCM_SHAREDACCESSHOST_LAN, NCSM_NONE, 7, 0, &hLanIconBig);
  334. if (SUCCEEDED(hr))
  335. {
  336. SetIcon(hLanIconSmall, FALSE);
  337. SetIcon(hLanIconBig, TRUE);
  338. SendDlgItemMessage(IDI_Device_Icon, STM_SETICON, reinterpret_cast<WPARAM>(hLanIconBig), 0);
  339. }
  340. }
  341. FreeNetconProperties(pProps);
  342. }
  343. return TRUE;
  344. }