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.

404 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. Task.cpp
  7. Implementation of the task holder/enumerator object
  8. FILE HISTORY:
  9. */
  10. #include <stdafx.h>
  11. #include "task.h"
  12. DEBUG_DECLARE_INSTANCE_COUNTER(CTaskList);
  13. CTaskList::CTaskList()
  14. : m_uIndex(0),
  15. m_cRef(1)
  16. {
  17. DEBUG_INCREMENT_INSTANCE_COUNTER(CTaskList);
  18. }
  19. CTaskList::~CTaskList()
  20. {
  21. DEBUG_DECREMENT_INSTANCE_COUNTER(CTaskList);
  22. }
  23. IMPLEMENT_ADDREF_RELEASE(CTaskList)
  24. STDMETHODIMP CTaskList::QueryInterface(REFIID riid, LPVOID *ppv)
  25. {
  26. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  27. // Is the pointer bad?
  28. if (ppv == NULL)
  29. return E_INVALIDARG;
  30. // Place NULL in *ppv in case of failure
  31. *ppv = NULL;
  32. // This is the non-delegating IUnknown implementation
  33. if (riid == IID_IUnknown)
  34. *ppv = (LPVOID) this;
  35. else if (riid == IID_IEnumTASK)
  36. *ppv = (IEnumTASK *) this;
  37. // If we're going to return an interface, AddRef it first
  38. if (*ppv)
  39. {
  40. ((LPUNKNOWN) *ppv)->AddRef();
  41. return hrOK;
  42. }
  43. else
  44. return E_NOINTERFACE;
  45. }
  46. /*!--------------------------------------------------------------------------
  47. CTaskList::Next
  48. -
  49. Author: EricDav
  50. ---------------------------------------------------------------------------*/
  51. STDMETHODIMP
  52. CTaskList::Next
  53. (
  54. ULONG celt,
  55. MMC_TASK * rgelt,
  56. ULONG * pceltFetched
  57. )
  58. {
  59. HRESULT hr = hrOK;
  60. COM_PROTECT_TRY
  61. {
  62. // caller alloc's array of MMC_TASKs
  63. // callee fills MMC_TASK elements (via CoTaskMemAlloc)
  64. if ((rgelt == NULL) || (pceltFetched == NULL))
  65. return E_INVALIDARG;
  66. Assert(!IsBadWritePtr (rgelt, celt*sizeof(MMC_TASK)));
  67. Assert(!IsBadWritePtr (pceltFetched, sizeof(ULONG)));
  68. ULONG nTaskNumber = (ULONG)m_arrayTasks.GetSize();
  69. for (ULONG i = 0; i < celt; i++)
  70. {
  71. if (m_uIndex >= nTaskNumber) // cannot fetch anymore
  72. {
  73. if (pceltFetched != NULL)
  74. *pceltFetched = i;
  75. return S_FALSE; // failure
  76. }
  77. if (FillTask(&rgelt[i], m_uIndex))
  78. {
  79. m_uIndex++;
  80. }
  81. else
  82. {
  83. if (pceltFetched)
  84. *pceltFetched = i;
  85. return S_FALSE; // failure
  86. }
  87. }
  88. // if we get here all is well
  89. if (pceltFetched)
  90. *pceltFetched = celt;
  91. }
  92. COM_PROTECT_CATCH
  93. return hr;
  94. }
  95. /*!--------------------------------------------------------------------------
  96. CTaskList::Skip
  97. -
  98. Author: EricDav
  99. ---------------------------------------------------------------------------*/
  100. STDMETHODIMP
  101. CTaskList::Skip
  102. (
  103. ULONG celt
  104. )
  105. {
  106. HRESULT hr = hrOK;
  107. COM_PROTECT_TRY
  108. {
  109. m_uIndex += celt;
  110. }
  111. COM_PROTECT_CATCH
  112. return hr;
  113. }
  114. /*!--------------------------------------------------------------------------
  115. CTaskList::Reset
  116. -
  117. Author: EricDav
  118. ---------------------------------------------------------------------------*/
  119. STDMETHODIMP
  120. CTaskList::Reset()
  121. {
  122. HRESULT hr = hrOK;
  123. COM_PROTECT_TRY
  124. {
  125. m_uIndex = 0;
  126. }
  127. COM_PROTECT_CATCH
  128. return hr;
  129. }
  130. /*!--------------------------------------------------------------------------
  131. CTaskList::Clone
  132. -
  133. Author: EricDav
  134. ---------------------------------------------------------------------------*/
  135. STDMETHODIMP
  136. CTaskList::Clone
  137. (
  138. IEnumTASK ** ppEnumTASK
  139. )
  140. {
  141. CTaskList * pEnumTasks = NULL;
  142. SPIEnumTask spEnumTasks;
  143. HRESULT hr = hrOK;
  144. COM_PROTECT_TRY
  145. {
  146. if (ppEnumTASK == NULL)
  147. return E_INVALIDARG;
  148. *ppEnumTASK = NULL;
  149. // clone maintaining state info
  150. pEnumTasks = new CTaskList;
  151. spEnumTasks = pEnumTasks;
  152. pEnumTasks->_Clone(m_uIndex, m_arrayTasks);
  153. CORg (pEnumTasks->QueryInterface(IID_IEnumTASK, (void**)ppEnumTASK));
  154. COM_PROTECT_ERROR_LABEL;
  155. }
  156. COM_PROTECT_CATCH
  157. return hr;
  158. }
  159. /*!--------------------------------------------------------------------------
  160. CTaskList::AddTask
  161. -
  162. Author: EricDav
  163. ---------------------------------------------------------------------------*/
  164. HRESULT
  165. CTaskList::AddTask
  166. (
  167. LPOLESTR pszMouseOverBitmapResource,
  168. LPOLESTR pszMouseOffBitmapResource,
  169. LPOLESTR pszText,
  170. LPOLESTR pszHelpString,
  171. MMC_ACTION_TYPE mmcAction,
  172. long nCommandID
  173. )
  174. {
  175. HRESULT hr = hrOK;
  176. MMC_TASK mmcTask;
  177. COM_PROTECT_TRY
  178. {
  179. mmcTask.sDisplayObject.eDisplayType = MMC_TASK_DISPLAY_TYPE_BITMAP;
  180. mmcTask.sDisplayObject.uBitmap.szMouseOverBitmap = pszMouseOverBitmapResource;
  181. mmcTask.sDisplayObject.uBitmap.szMouseOffBitmap = pszMouseOffBitmapResource;
  182. mmcTask.szText = pszText;
  183. mmcTask.szHelpString = pszHelpString;
  184. mmcTask.eActionType = mmcAction;
  185. mmcTask.nCommandID = nCommandID;
  186. m_arrayTasks.Add(mmcTask);
  187. }
  188. COM_PROTECT_CATCH
  189. return hr;
  190. }
  191. /*!--------------------------------------------------------------------------
  192. CTaskList::AddTask
  193. -
  194. Author: EricDav
  195. ---------------------------------------------------------------------------*/
  196. HRESULT
  197. CTaskList::AddTask
  198. (
  199. LPOLESTR pszMouseOverBitmapResource,
  200. LPOLESTR pszMouseOffBitmapResource,
  201. LPOLESTR pszText,
  202. LPOLESTR pszHelpString,
  203. MMC_ACTION_TYPE mmcAction,
  204. LPOLESTR pszActionURLorScript
  205. )
  206. {
  207. HRESULT hr = hrOK;
  208. MMC_TASK mmcTask;
  209. COM_PROTECT_TRY
  210. {
  211. mmcTask.sDisplayObject.eDisplayType = MMC_TASK_DISPLAY_TYPE_BITMAP;
  212. mmcTask.sDisplayObject.uBitmap.szMouseOverBitmap = pszMouseOverBitmapResource;
  213. mmcTask.sDisplayObject.uBitmap.szMouseOffBitmap = pszMouseOffBitmapResource;
  214. mmcTask.szText = pszText;
  215. mmcTask.szHelpString = pszHelpString;
  216. mmcTask.eActionType = mmcAction;
  217. switch (mmcTask.eActionType)
  218. {
  219. case MMC_ACTION_LINK:
  220. mmcTask.szActionURL = pszActionURLorScript;
  221. break;
  222. case MMC_ACTION_SCRIPT:
  223. mmcTask.szScript = pszActionURLorScript;
  224. break;
  225. default:
  226. Assert (FALSE); // bad task
  227. break;
  228. }
  229. m_arrayTasks.Add(mmcTask);
  230. }
  231. COM_PROTECT_CATCH
  232. return hr;
  233. }
  234. /*!--------------------------------------------------------------------------
  235. CTaskList::FillTask
  236. -
  237. Author: EricDav
  238. ---------------------------------------------------------------------------*/
  239. BOOL
  240. CTaskList::FillTask
  241. (
  242. MMC_TASK * pmmcTask,
  243. int nIndex
  244. )
  245. {
  246. HRESULT hr = hrOK;
  247. COM_PROTECT_TRY
  248. {
  249. // right now we only support bitmap display types
  250. pmmcTask->sDisplayObject.eDisplayType = m_arrayTasks[nIndex].sDisplayObject.eDisplayType;
  251. switch (m_arrayTasks[nIndex].sDisplayObject.eDisplayType)
  252. {
  253. case MMC_TASK_DISPLAY_TYPE_BITMAP:
  254. {
  255. pmmcTask->sDisplayObject.uBitmap.szMouseOverBitmap = (LPOLESTR) CoTaskMemAlloc (sizeof(OLECHAR)*(lstrlen(m_arrayTasks[nIndex].sDisplayObject.uBitmap.szMouseOverBitmap)+1));
  256. if (pmmcTask->sDisplayObject.uBitmap.szMouseOverBitmap)
  257. {
  258. lstrcpy (pmmcTask->sDisplayObject.uBitmap.szMouseOverBitmap, m_arrayTasks[nIndex].sDisplayObject.uBitmap.szMouseOverBitmap);
  259. }
  260. pmmcTask->sDisplayObject.uBitmap.szMouseOffBitmap = (LPOLESTR) CoTaskMemAlloc (sizeof(OLECHAR)*(lstrlen(m_arrayTasks[nIndex].sDisplayObject.uBitmap.szMouseOffBitmap)+1));
  261. if (pmmcTask->sDisplayObject.uBitmap.szMouseOffBitmap)
  262. {
  263. lstrcpy (pmmcTask->sDisplayObject.uBitmap.szMouseOffBitmap, m_arrayTasks[nIndex].sDisplayObject.uBitmap.szMouseOffBitmap);
  264. }
  265. }
  266. break;
  267. default:
  268. Panic0("unsupported display type in task");
  269. break;
  270. }
  271. //
  272. // Add button text
  273. //
  274. pmmcTask->szText = (LPOLESTR)CoTaskMemAlloc (sizeof(OLECHAR)*(lstrlen(m_arrayTasks[nIndex].szText)+1));
  275. if (pmmcTask->szText)
  276. {
  277. lstrcpy (pmmcTask->szText, m_arrayTasks[nIndex].szText);
  278. //
  279. // Add help string
  280. //
  281. pmmcTask->szHelpString = (LPOLESTR)CoTaskMemAlloc (sizeof(OLECHAR)*(lstrlen(m_arrayTasks[nIndex].szHelpString)+1));
  282. if (pmmcTask->szHelpString)
  283. {
  284. lstrcpy (pmmcTask->szHelpString, m_arrayTasks[nIndex].szHelpString);
  285. }
  286. }
  287. pmmcTask->eActionType = m_arrayTasks[nIndex].eActionType;
  288. switch (pmmcTask->eActionType)
  289. {
  290. case MMC_ACTION_ID:
  291. pmmcTask->nCommandID = m_arrayTasks[nIndex].nCommandID;
  292. break;
  293. case MMC_ACTION_LINK:
  294. pmmcTask->szActionURL = (LPOLESTR) CoTaskMemAlloc (sizeof(OLECHAR)*(lstrlen(m_arrayTasks[nIndex].szActionURL)+1));
  295. if (pmmcTask->szActionURL)
  296. {
  297. lstrcpy (pmmcTask->szActionURL, m_arrayTasks[nIndex].szActionURL);
  298. }
  299. break;
  300. case MMC_ACTION_SCRIPT:
  301. pmmcTask->szScript = (LPOLESTR) CoTaskMemAlloc (sizeof(OLECHAR)*(lstrlen(m_arrayTasks[nIndex].szScript)+1));
  302. if (pmmcTask->szActionURL)
  303. {
  304. lstrcpy (pmmcTask->szScript, m_arrayTasks[nIndex].szScript);
  305. }
  306. break;
  307. default:
  308. Assert (FALSE); // bad task
  309. break;
  310. }
  311. }
  312. COM_PROTECT_CATCH
  313. return SUCCEEDED(hr) ? TRUE : FALSE;
  314. }
  315. /*!--------------------------------------------------------------------------
  316. CTaskList::_Clone
  317. -
  318. Author: EricDav
  319. ---------------------------------------------------------------------------*/
  320. HRESULT
  321. CTaskList::_Clone
  322. (
  323. int nIndex,
  324. CTaskListArray & arrayTasks
  325. )
  326. {
  327. HRESULT hr = hrOK;
  328. COM_PROTECT_TRY
  329. {
  330. m_uIndex = nIndex;
  331. m_arrayTasks.RemoveAll();
  332. m_arrayTasks.Append(arrayTasks);
  333. }
  334. COM_PROTECT_CATCH
  335. return hr;
  336. }