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.

425 lines
10 KiB

  1. //-------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation, 1996 - 1999
  3. //
  4. // File: droptrgt.cpp
  5. //
  6. // Contents: The cpp file to implement IDropTarget
  7. //
  8. // History: March-9th-98 xiaohs created
  9. //
  10. //--------------------------------------------------------------
  11. #include "wzrdpvk.h"
  12. #include "mgrcert.h"
  13. //============================================================================
  14. //
  15. // the definition of the CCertMgrDropTarget class which supports IDropTarget
  16. //============================================================================
  17. class CCertMgrDropTarget : public IDropTarget
  18. {
  19. private:
  20. LPDATAOBJECT m_pDataObj;
  21. ULONG m_cRefs;
  22. DWORD m_grfKeyStateLast;
  23. BOOL m_fHasHDROP;
  24. DWORD m_dwEffectLastReturned;
  25. HWND m_hwndDlg;
  26. CERT_MGR_INFO *m_pCertMgrInfo;
  27. public:
  28. CCertMgrDropTarget(HWND hwndDlg,
  29. CERT_MGR_INFO *pCertMgrInfo);
  30. ~CCertMgrDropTarget();
  31. STDMETHODIMP QueryInterface (REFIID riid,LPVOID FAR *ppv);
  32. STDMETHODIMP_(ULONG) AddRef ();
  33. STDMETHODIMP_(ULONG) Release ();
  34. STDMETHODIMP DragEnter (LPDATAOBJECT pDataObj,
  35. DWORD grfKeyState,
  36. POINTL pt,
  37. LPDWORD pdwEffect);
  38. STDMETHODIMP DragOver (DWORD grfKeyState,
  39. POINTL pt,
  40. LPDWORD pdwEffect);
  41. STDMETHODIMP DragLeave ();
  42. STDMETHODIMP Drop (LPDATAOBJECT pDataObj,
  43. DWORD grfKeyState,
  44. POINTL pt,
  45. LPDWORD pdwEffect);
  46. DWORD GetDropEffect (LPDWORD pdwEffect);
  47. };
  48. //============================================================================
  49. //
  50. // Implementation of CCertMgrDropTarget_CreateInstance
  51. //============================================================================
  52. HRESULT CCertMgrDropTarget_CreateInstance(HWND hwndDlg,
  53. CERT_MGR_INFO *pCertMgrInfo,
  54. IDropTarget **ppIDropTarget)
  55. {
  56. CCertMgrDropTarget *pCCertMgrDropTarget=NULL;
  57. *ppIDropTarget=NULL;
  58. pCCertMgrDropTarget = (CCertMgrDropTarget *)new CCertMgrDropTarget(hwndDlg, pCertMgrInfo);
  59. if(pCCertMgrDropTarget)
  60. {
  61. *ppIDropTarget=(IDropTarget *)pCCertMgrDropTarget;
  62. return S_OK;
  63. }
  64. return E_OUTOFMEMORY;
  65. }
  66. //============================================================================
  67. //
  68. // Implementation of the CCertMgrDropTarget class
  69. //============================================================================
  70. //
  71. // Constructor
  72. //
  73. CCertMgrDropTarget::CCertMgrDropTarget(HWND hwndDlg,
  74. CERT_MGR_INFO *pCertMgrInfo)
  75. {
  76. m_cRefs = 1;
  77. m_pDataObj = NULL;
  78. m_grfKeyStateLast = 0;
  79. m_fHasHDROP = FALSE;
  80. m_dwEffectLastReturned = 0;
  81. m_hwndDlg = hwndDlg;
  82. m_pCertMgrInfo = pCertMgrInfo;
  83. }
  84. //
  85. // Destructor
  86. //
  87. CCertMgrDropTarget::~CCertMgrDropTarget()
  88. {
  89. }
  90. //
  91. // QueryInterface
  92. //
  93. STDMETHODIMP CCertMgrDropTarget::QueryInterface(REFIID riid, LPVOID *ppv)
  94. {
  95. HRESULT hr = E_NOINTERFACE;
  96. *ppv = NULL;
  97. // Any interface on this object is the object pointer
  98. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDropTarget))
  99. {
  100. *ppv = (LPDROPTARGET)this;
  101. AddRef();
  102. hr = NOERROR;
  103. }
  104. return hr;
  105. }
  106. //
  107. // AddRef
  108. //
  109. STDMETHODIMP_(ULONG) CCertMgrDropTarget::AddRef()
  110. {
  111. return ++m_cRefs;
  112. }
  113. //
  114. // Release
  115. //
  116. STDMETHODIMP_(ULONG) CCertMgrDropTarget::Release()
  117. {
  118. if (--m_cRefs)
  119. {
  120. return m_cRefs;
  121. }
  122. delete this;
  123. return 0L;
  124. }
  125. //
  126. // DragEnter
  127. //
  128. STDMETHODIMP CCertMgrDropTarget::DragEnter(LPDATAOBJECT pDataObj,
  129. DWORD grfKeyState,
  130. POINTL pt,
  131. LPDWORD pdwEffect)
  132. {
  133. HRESULT hr = E_INVALIDARG;
  134. // Release any old data object we might have
  135. if (m_pDataObj)
  136. {
  137. m_pDataObj->Release();
  138. }
  139. m_grfKeyStateLast = grfKeyState;
  140. m_pDataObj = pDataObj;
  141. //
  142. // See if we will be able to get CF_HDROP from this guy
  143. //
  144. if (pDataObj)
  145. {
  146. pDataObj->AddRef();
  147. LPENUMFORMATETC penum;
  148. hr = pDataObj->EnumFormatEtc(DATADIR_GET, &penum);
  149. if (SUCCEEDED(hr))
  150. {
  151. FORMATETC fmte;
  152. ULONG celt;
  153. while (S_OK == penum->Next(1, &fmte, &celt))
  154. {
  155. if (fmte.cfFormat==CF_HDROP && (fmte.tymed & TYMED_HGLOBAL))
  156. {
  157. m_fHasHDROP = TRUE;
  158. hr=S_OK;
  159. break;
  160. }
  161. }
  162. penum->Release();
  163. }
  164. }
  165. // Save the drop effect
  166. if (pdwEffect)
  167. {
  168. *pdwEffect = m_dwEffectLastReturned = GetDropEffect(pdwEffect);
  169. }
  170. return hr;
  171. }
  172. //
  173. // GetDropEffect
  174. //
  175. DWORD CCertMgrDropTarget::GetDropEffect(LPDWORD pdwEffect)
  176. {
  177. if (m_fHasHDROP)
  178. {
  179. return (*pdwEffect) & (DROPEFFECT_COPY);
  180. }
  181. else
  182. {
  183. return DROPEFFECT_NONE;
  184. }
  185. }
  186. //
  187. // DragOver
  188. //
  189. STDMETHODIMP CCertMgrDropTarget::DragOver(DWORD grfKeyState, POINTL pt, LPDWORD pdwEffect)
  190. {
  191. if (m_grfKeyStateLast == grfKeyState)
  192. {
  193. // Return the effect we saved at dragenter time
  194. if (*pdwEffect)
  195. {
  196. *pdwEffect = m_dwEffectLastReturned;
  197. }
  198. }
  199. else
  200. {
  201. if (*pdwEffect)
  202. {
  203. *pdwEffect = m_dwEffectLastReturned = GetDropEffect(pdwEffect);
  204. }
  205. }
  206. m_grfKeyStateLast = grfKeyState;
  207. return S_OK;
  208. }
  209. //
  210. // DragLeave
  211. //
  212. STDMETHODIMP CCertMgrDropTarget::DragLeave()
  213. {
  214. if (m_pDataObj)
  215. {
  216. m_pDataObj->Release();
  217. m_pDataObj = NULL;
  218. }
  219. return S_OK;
  220. }
  221. //
  222. // Drop
  223. //
  224. STDMETHODIMP CCertMgrDropTarget::Drop(LPDATAOBJECT pDataObj,
  225. DWORD grfKeyState,
  226. POINTL pt,
  227. LPDWORD pdwEffect)
  228. {
  229. HRESULT hr = S_OK;
  230. FORMATETC fmte = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  231. STGMEDIUM medium;
  232. DWORD dwFileCount=0;
  233. BOOL fOneFileSucceeded=FALSE;
  234. BOOL fOneFileFailed=FALSE;
  235. DWORD dwIndex=0;
  236. WCHAR wszPath[MAX_PATH];
  237. UINT idsErrMsg=0;
  238. CRYPTUI_WIZ_IMPORT_SRC_INFO ImportSrcInfo;
  239. DWORD dwExpectedContentType=CERT_QUERY_CONTENT_FLAG_CTL | CERT_QUERY_CONTENT_FLAG_CERT | CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED;
  240. DWORD dwContentType=0;
  241. DWORD dwException=0;
  242. //
  243. // Take the new data object, since OLE can give us a different one than
  244. // it did in DragEnter
  245. //
  246. if (m_pDataObj)
  247. {
  248. m_pDataObj->Release();
  249. }
  250. m_pDataObj = pDataObj;
  251. if (pDataObj)
  252. {
  253. pDataObj->AddRef();
  254. }
  255. __try {
  256. //get the file names
  257. if (SUCCEEDED(pDataObj->GetData(&fmte, &medium)))
  258. {
  259. dwFileCount=DragQueryFileU((HDROP)medium.hGlobal, (UINT)-1, NULL, 0);
  260. }
  261. else
  262. return E_INVALIDARG;
  263. //process the file one at a time
  264. for(dwIndex=0; dwIndex < dwFileCount; dwIndex++)
  265. {
  266. if(DragQueryFileU((HDROP)medium.hGlobal, dwIndex, wszPath, MAX_PATH))
  267. {
  268. //make sure the file is either a cert or a PKCS7 file
  269. if(!CryptQueryObject(
  270. CERT_QUERY_OBJECT_FILE,
  271. wszPath,
  272. dwExpectedContentType,
  273. CERT_QUERY_FORMAT_FLAG_ALL,
  274. 0,
  275. NULL,
  276. &dwContentType,
  277. NULL,
  278. NULL,
  279. NULL,
  280. NULL))
  281. {
  282. fOneFileFailed=TRUE;
  283. }
  284. else
  285. {
  286. //since the CTL itself is a PKCS#7, we need to differentiate them
  287. if(CERT_QUERY_CONTENT_CTL == dwContentType)
  288. fOneFileFailed=TRUE;
  289. else
  290. {
  291. memset(&ImportSrcInfo, 0, sizeof(ImportSrcInfo));
  292. ImportSrcInfo.dwSize=sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO);
  293. ImportSrcInfo.dwSubjectChoice=CRYPTUI_WIZ_IMPORT_SUBJECT_FILE;
  294. ImportSrcInfo.pwszFileName=wszPath;
  295. //call the import wizard UIless mode
  296. CryptUIWizImport(CRYPTUI_WIZ_NO_UI,
  297. m_hwndDlg,
  298. NULL,
  299. &ImportSrcInfo,
  300. NULL);
  301. fOneFileSucceeded=TRUE;
  302. }
  303. }
  304. }
  305. }
  306. //display messages based on the result
  307. if(1 == dwFileCount)
  308. {
  309. if(fOneFileFailed)
  310. idsErrMsg=IDS_ALL_INVALID_DROP_FILE;
  311. }
  312. else
  313. {
  314. if( 1 < dwFileCount)
  315. {
  316. if(fOneFileFailed && fOneFileSucceeded)
  317. idsErrMsg=IDS_SOME_INVALID_DROP_FILE;
  318. else
  319. {
  320. if(fOneFileFailed && (FALSE==fOneFileSucceeded))
  321. idsErrMsg=IDS_ALL_INVALID_DROP_FILE;
  322. }
  323. }
  324. }
  325. if(idsErrMsg)
  326. I_MessageBox(m_hwndDlg,
  327. idsErrMsg,
  328. IDS_CERT_MGR_TITLE,
  329. m_pCertMgrInfo->pCertMgrStruct->pwszTitle,
  330. MB_ICONEXCLAMATION|MB_OK|MB_APPLMODAL);
  331. //release the medium
  332. ReleaseStgMedium(&medium);
  333. //refresh the listView window
  334. if(idsErrMsg == 0)
  335. RefreshCertListView(m_hwndDlg, m_pCertMgrInfo);
  336. } __except(EXCEPTION_EXECUTE_HANDLER) {
  337. dwException = GetExceptionCode();
  338. }
  339. DragLeave();
  340. return dwException ? dwException : S_OK;
  341. }