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.

426 lines
9.4 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: enum.cxx
  7. //
  8. // Contents: Implementation of IEnumIDList
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 1/5/1996 RaviR Created
  15. //
  16. //____________________________________________________________________________
  17. #include "..\pch\headers.hxx"
  18. #pragma hdrstop
  19. #include "dbg.h"
  20. #include "macros.h"
  21. #include "..\inc\common.hxx"
  22. #include "..\inc\policy.hxx"
  23. #include "dll.hxx"
  24. #include "jobidl.hxx"
  25. #include "util.hxx"
  26. #include "bitflag.hxx"
  27. #include <StrSafe.h>
  28. //#undef DEB_TRACE
  29. //#define DEB_TRACE DEB_USER1
  30. //
  31. // Private flags
  32. //
  33. #define JE_LOCAL 0x0001
  34. #define JE_ENUMERATED_TEMPLATE 0x0002
  35. //____________________________________________________________________________
  36. //
  37. // Class: CJobsEnum
  38. //
  39. // Purpose: Enumerates jobs in a folder.
  40. //
  41. // History: 1/25/1996 RaviR Created
  42. //____________________________________________________________________________
  43. class CJobsEnum : public IEnumIDList,
  44. public CBitFlag
  45. {
  46. public:
  47. CJobsEnum(ULONG uFlags, LPCTSTR pszFolderPath, IEnumWorkItems *pEnumJobs);
  48. ~CJobsEnum(void);
  49. // IUnknown methods
  50. DECLARE_STANDARD_IUNKNOWN;
  51. // IEnumIDList methods
  52. STDMETHOD(Next)(ULONG celt, LPITEMIDLIST* rgelt, ULONG* pceltFetched);
  53. STDMETHOD(Skip)(ULONG celt);
  54. STDMETHOD(Reset)(void);
  55. STDMETHOD(Clone)(IEnumIDList** ppenum);
  56. private:
  57. ULONG m_uShellFlags; // SHCONTF flags passed in by shell
  58. IEnumWorkItems * m_pEnumJobs;
  59. LPCTSTR m_pszFolderPath;
  60. CDllRef m_DllRef;
  61. };
  62. //____________________________________________________________________________
  63. //
  64. // Member: CJobsEnum::CJobsEnum, Constructor
  65. //
  66. // History: 1/5/1996 RaviR Created
  67. //
  68. //____________________________________________________________________________
  69. inline
  70. CJobsEnum::CJobsEnum(
  71. ULONG uFlags,
  72. LPCTSTR pszFolderPath,
  73. IEnumWorkItems * pEnumJobs)
  74. :
  75. m_ulRefs(1),
  76. m_uShellFlags(uFlags),
  77. m_pEnumJobs(pEnumJobs),
  78. m_pszFolderPath(pszFolderPath)
  79. {
  80. TRACE(CJobsEnum, CJobsEnum);
  81. DEBUG_OUT((DEB_USER1, "FolderPath = %ws\n", pszFolderPath));
  82. if (IsLocalFilename(pszFolderPath))
  83. {
  84. _SetFlag(JE_LOCAL);
  85. }
  86. Win4Assert(m_pEnumJobs != NULL);
  87. m_pEnumJobs->AddRef();
  88. //
  89. // Policy - do not allow the template item if we are not
  90. // allowing job creation
  91. // -- Later, we prevent this flag from being cleared
  92. //
  93. if (RegReadPolicyKey(TS_KEYPOLICY_DENY_CREATE_TASK))
  94. {
  95. DEBUG_OUT((DEB_ITRACE, "Policy CREATE_TASK active - no template wizard\n"));
  96. //
  97. // This next flag means that we have ALREADY shown the template.
  98. // Setting it will force us never to enumerate it
  99. //
  100. _SetFlag(JE_ENUMERATED_TEMPLATE);
  101. }
  102. }
  103. //____________________________________________________________________________
  104. //
  105. // Member: CJobsEnum::~CJobsEnum, Destructor
  106. //____________________________________________________________________________
  107. inline
  108. CJobsEnum::~CJobsEnum()
  109. {
  110. TRACE(CJobsEnum, ~CJobsEnum);
  111. if (m_pEnumJobs != NULL)
  112. {
  113. m_pEnumJobs->Release();
  114. }
  115. // Note: No need to free m_pszFolderPath.
  116. }
  117. //____________________________________________________________________________
  118. //
  119. // Member: IUnknown methods
  120. //____________________________________________________________________________
  121. IMPLEMENT_STANDARD_IUNKNOWN(CJobsEnum);
  122. STDMETHODIMP
  123. CJobsEnum::QueryInterface(REFIID riid, LPVOID* ppvObj)
  124. {
  125. if (IsEqualIID(IID_IUnknown, riid) ||
  126. IsEqualIID(IID_IEnumIDList, riid))
  127. {
  128. *ppvObj = (IUnknown*)(IEnumIDList*) this;
  129. this->AddRef();
  130. return S_OK;
  131. }
  132. *ppvObj = NULL;
  133. return E_NOINTERFACE;
  134. }
  135. //____________________________________________________________________________
  136. //
  137. // Member: CJobsEnum::IEnumIDList::Next
  138. //
  139. // Arguments: [celt] -- IN
  140. // [ppidlOut] -- IN
  141. // [pceltFetched] -- IN
  142. //
  143. // Returns: HRESULT.
  144. //
  145. // History: 1/5/1996 RaviR Created
  146. // 2-12-1997 DavidMun Handle NULL pceltFetched
  147. //
  148. //____________________________________________________________________________
  149. STDMETHODIMP
  150. CJobsEnum::Next(
  151. ULONG celt,
  152. LPITEMIDLIST* ppidlOut,
  153. ULONG* pceltFetched)
  154. {
  155. TRACE(CJobsEnum, Next);
  156. HRESULT hr = S_OK;
  157. CJobID jid;
  158. if (!(m_uShellFlags & SHCONTF_NONFOLDERS))
  159. {
  160. return S_FALSE;
  161. }
  162. if (m_pEnumJobs == NULL)
  163. {
  164. return E_FAIL;
  165. }
  166. if (pceltFetched == NULL && celt != 1)
  167. {
  168. return E_INVALIDARG;
  169. }
  170. if (pceltFetched)
  171. {
  172. *pceltFetched = 0;
  173. }
  174. ULONG curr = 0;
  175. LPWSTR * ppwszJob = NULL;
  176. ULONG ulTemp;
  177. if (_IsFlagSet(JE_LOCAL) &&
  178. !_IsFlagSet(JE_ENUMERATED_TEMPLATE) &&
  179. celt)
  180. {
  181. jid.InitToTemplate();
  182. ppidlOut[curr] = ILClone((LPCITEMIDLIST)(&jid));
  183. if (!ppidlOut[curr])
  184. {
  185. return E_OUTOFMEMORY;
  186. }
  187. DEBUG_OUT((DEB_ITRACE, "Created template\n"));
  188. curr++;
  189. _SetFlag(JE_ENUMERATED_TEMPLATE);
  190. }
  191. while (curr < celt)
  192. {
  193. hr = m_pEnumJobs->Next(1, &ppwszJob, &ulTemp);
  194. CHECK_HRESULT(hr);
  195. if (FAILED(hr))
  196. {
  197. break;
  198. }
  199. else if (ulTemp == 0)
  200. {
  201. hr = S_FALSE;
  202. break;
  203. }
  204. LPTSTR pszJob = (LPTSTR)*ppwszJob;
  205. hr = jid.Load(m_pszFolderPath, pszJob);
  206. CoTaskMemFree(*ppwszJob);
  207. CoTaskMemFree(ppwszJob);
  208. if (S_OK == hr)
  209. {
  210. ppidlOut[curr] = ILClone((LPCITEMIDLIST)(&jid));
  211. if (NULL != ppidlOut[curr])
  212. {
  213. ++curr;
  214. hr = S_OK;
  215. }
  216. else
  217. {
  218. hr = E_OUTOFMEMORY;
  219. }
  220. }
  221. if (hr == E_OUTOFMEMORY)
  222. {
  223. break;
  224. }
  225. }
  226. if (curr > 0 && curr < celt)
  227. {
  228. hr = S_FALSE;
  229. }
  230. if (pceltFetched)
  231. {
  232. *pceltFetched = curr;
  233. }
  234. return hr;
  235. }
  236. //____________________________________________________________________________
  237. //
  238. // Member: CJobsEnum::Skip
  239. //
  240. // Arguments: [celt] -- IN
  241. //
  242. // Returns: HRESULT.
  243. //
  244. // History: 1/5/1996 RaviR Created
  245. //
  246. //____________________________________________________________________________
  247. STDMETHODIMP
  248. CJobsEnum::Skip(
  249. ULONG celt)
  250. {
  251. TRACE(CJobsEnum, Skip);
  252. if (!celt)
  253. {
  254. return E_INVALIDARG;
  255. }
  256. if (_IsFlagSet(JE_LOCAL) && !_IsFlagSet(JE_ENUMERATED_TEMPLATE))
  257. {
  258. celt--;
  259. _SetFlag(JE_ENUMERATED_TEMPLATE);
  260. if (!celt)
  261. {
  262. return S_OK;
  263. }
  264. }
  265. return m_pEnumJobs->Skip(celt);
  266. }
  267. //____________________________________________________________________________
  268. //
  269. // Member: CJobsEnum::Reset
  270. //
  271. // Returns: HRESULT.
  272. //
  273. // History: 1/5/1996 RaviR Created
  274. //
  275. //____________________________________________________________________________
  276. STDMETHODIMP
  277. CJobsEnum::Reset(void)
  278. {
  279. TRACE(CJobsEnum, Reset);
  280. //
  281. // Policy - don't clear flag if we are not allowing job creation
  282. //
  283. if (! RegReadPolicyKey(TS_KEYPOLICY_DENY_CREATE_TASK))
  284. {
  285. DEBUG_OUT((DEB_ITRACE, "Policy CREATE_TASK active - prevent template wiz\n"));
  286. //
  287. // Not clearing this flag maintains that we will have enumerated
  288. // the template on the next go around
  289. //
  290. _ClearFlag(JE_ENUMERATED_TEMPLATE);
  291. }
  292. return m_pEnumJobs->Reset();
  293. }
  294. //____________________________________________________________________________
  295. //
  296. // Member: CJobsEnum::Clone
  297. //
  298. // Arguments: [ppenum] -- IN
  299. //
  300. // Returns: HRESULT.
  301. //
  302. // History: 1/5/1996 RaviR Created
  303. //
  304. //____________________________________________________________________________
  305. STDMETHODIMP
  306. CJobsEnum::Clone(
  307. IEnumIDList** ppenum)
  308. {
  309. TRACE(CJobsEnum, Clone);
  310. return E_FAIL; // not supported
  311. }
  312. //____________________________________________________________________________
  313. //
  314. // Function: JFGetEnumIDList
  315. //
  316. // Synopsis: Function to create the object to enumearte the JobIDList
  317. //
  318. // Arguments: [uFlags] -- IN
  319. // [pszFolderPath] -- IN
  320. // [pEnumJobs] -- IN
  321. // [riid] -- IN
  322. // [ppvObj] -- OUT
  323. //
  324. // Returns: HRESULT
  325. //
  326. // History: 1/24/1996 RaviR Created
  327. //
  328. //____________________________________________________________________________
  329. HRESULT
  330. JFGetEnumIDList(
  331. ULONG uFlags,
  332. LPCTSTR pszFolderPath,
  333. IEnumWorkItems * pEnumJobs,
  334. LPVOID * ppvObj)
  335. {
  336. CJobsEnum * pEnum = new CJobsEnum(uFlags, pszFolderPath, pEnumJobs);
  337. if (NULL == pEnum)
  338. {
  339. CHECK_HRESULT(E_OUTOFMEMORY);
  340. return E_OUTOFMEMORY;
  341. }
  342. pEnumJobs->Reset();
  343. HRESULT hr = pEnum->QueryInterface(IID_IEnumIDList, ppvObj);
  344. pEnum->Release();
  345. return hr;
  346. }