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.

667 lines
21 KiB

  1. /*======================================================================================//
  2. | Process Control //
  3. | //
  4. |Copyright (c) 1998 Sequent Computer Systems, Incorporated. All rights reserved. //
  5. | //
  6. |File Name: JobRuleFolder.cpp //
  7. | //
  8. |Description: Implementation of job rule node //
  9. | //
  10. |Created: Paul Skoglund 09-1998 //
  11. | //
  12. |Rev History: //
  13. | //
  14. |=======================================================================================*/
  15. #include "StdAfx.h"
  16. #include "BaseNode.h"
  17. #include "ManagementPages.h"
  18. #include "ManagementRuleWizards.h"
  19. #include "JobPages.h"
  20. #pragma warning(push)
  21. #include <algorithm>
  22. #pragma warning(pop)
  23. using std::find;
  24. using std::list<PCJobSummary*>;
  25. const GUID CJobRuleFolder::m_GUID = {0xff9baf64,0x064e,0x11d2,{0x80, 0x14,0x00,0x10,0x4b,0x9a,0x31,0x06} };
  26. const TCHAR *const CJobRuleFolder::m_szGUID = _T("{ff9baf64-064e-11d2-8014-00104b9a3106}");
  27. // Array of view items to be inserted into the context menu.
  28. const CONTEXTMENUITEMBYID CJobRuleFolder::TopMenuItems[] =
  29. {
  30. { IDS_JRULE_TOP, ID_JRULE_NEW, ID_JRULE_NEW, CCM_INSERTIONPOINTID_PRIMARY_TOP },
  31. { 0, 0, 0, 0 }
  32. };
  33. const CONTEXTMENUITEMBYID CJobRuleFolder::NewMenuItems[] =
  34. {
  35. { IDS_JRULE_NEW, ID_JRULE_NEW, ID_JRULE_NEW, CCM_INSERTIONPOINTID_PRIMARY_NEW },
  36. { 0, 0, 0, 0 }
  37. };
  38. CJobRuleFolder::CJobRuleFolder(CBaseNode *pParent) : CBaseNode(JOBRULE_NODE, pParent), m_ID(0)
  39. {
  40. LoadStringHelper(m_name, IDS_JOBRULE_FOLDER);
  41. }
  42. CJobRuleFolder::~CJobRuleFolder()
  43. {
  44. ClearCache();
  45. ATLTRACE( _T("~CJobRuleFolder end\n"));
  46. }
  47. LPCTSTR CJobRuleFolder::GetNodeName()
  48. {
  49. return m_name;
  50. }
  51. HRESULT CJobRuleFolder::GetDisplayInfo(RESULTDATAITEM &ResultItem)
  52. {
  53. if (ResultItem.bScopeItem)
  54. {
  55. if( ResultItem.mask & RDI_STR )
  56. {
  57. if (0 == ResultItem.nCol)
  58. ResultItem.str = const_cast<LPOLESTR>(GetNodeName());
  59. else
  60. ResultItem.str = _T("");
  61. }
  62. if (ResultItem.mask & RDI_IMAGE)
  63. ResultItem.nImage = sImage();
  64. return S_OK;
  65. }
  66. list<PCJobSummary*>::iterator item;
  67. item = find(Cache.begin(), Cache.end(), reinterpret_cast<PCJobSummary*>(ResultItem.lParam) );
  68. if (item == Cache.end() )
  69. return E_UNEXPECTED;
  70. if (ResultItem.mask & RDI_IMAGE)
  71. ResultItem.nImage = JOBRULEITEMIMAGE;
  72. if (ResultItem.mask & RDI_STR)
  73. {
  74. PCJobSummary &ref = **item;
  75. LPCOLESTR &pstr = ResultItem.str;
  76. switch (ResultItem.nCol)
  77. {
  78. case JOB_COLUMN:
  79. pstr = ref.jobName;
  80. break;
  81. case DESCRIPTION_COLUMN:
  82. pstr = ref.mgmtParms.description;
  83. break;
  84. case APPLY_AFFINITY_COLUMN:
  85. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_AFFINITY);
  86. break;
  87. case AFFINITY_COLUMN:
  88. pstr = FormatAffinity(m_ResultStr, ref.mgmtParms.affinity);
  89. break;
  90. case APPLY_PRIORITY_COLUMN:
  91. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_PRIORITY);
  92. break;
  93. case PRIORITY_COLUMN:
  94. pstr = FormatPriority(m_ResultStr, ref.mgmtParms.priority);
  95. break;
  96. case APPLY_SCHEDULING_CLASS_COLUMN:
  97. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_SCHEDULING_CLASS);
  98. break;
  99. case SCHEDULING_CLASS_COLUMN:
  100. pstr = FormatSchedulingClass(m_ResultStr, ref.mgmtParms.schedClass);
  101. break;
  102. case APPLY_MINMAXWS_COLUMN:
  103. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_WS_MINMAX);
  104. break;
  105. case MINWS_COLUMN:
  106. pstr = FormatMemory(m_ResultStr, ref.mgmtParms.minWS);
  107. break;
  108. case MAXWS_COLUMN:
  109. pstr = FormatMemory(m_ResultStr, ref.mgmtParms.maxWS);
  110. break;
  111. case APPLY_PROC_CMEM_LIMIT_COLUMN:
  112. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_PROC_MEMORY_LIMIT);
  113. break;
  114. case PROC_CMEM_LIMIT_COLUMN:
  115. pstr = FormatMemory(m_ResultStr, ref.mgmtParms.procMemoryLimit);
  116. break;
  117. case APPLY_JOB_CMEM_LIMIT_COLUMN:
  118. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_JOB_MEMORY_LIMIT);
  119. break;
  120. case JOB_CMEM_LIMIT_COLUMN:
  121. pstr = FormatMemory(m_ResultStr, ref.mgmtParms.jobMemoryLimit);
  122. break;
  123. case APPLY_PROCCOUNT_LIMIT_COLUMN:
  124. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_PROC_COUNT_LIMIT);
  125. break;
  126. case PROCCOUNT_LIMIT_COLUMN:
  127. pstr = FormatProcCount(m_ResultStr, ref.mgmtParms.procCountLimit);
  128. break;
  129. case APPLY_PROC_CPUTIME_LIMIT_COLUMN:
  130. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_PROC_TIME_LIMIT);
  131. break;
  132. case PROC_CPUTIME_LIMIT_COLUMN:
  133. pstr = FormatCNSTime(m_ResultStr, ref.mgmtParms.procTimeLimitCNS);
  134. break;
  135. case APPLY_JOB_CPUTIME_LIMIT_COLUMN:
  136. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_APPLY_JOB_TIME_LIMIT);
  137. break;
  138. case JOB_CPUTIME_LIMIT_COLUMN:
  139. pstr = FormatCNSTime(m_ResultStr, ref.mgmtParms.jobTimeLimitCNS);
  140. break;
  141. case ACTION_JOB_CPUTIME_LIMIT_COLUMN:
  142. pstr = FormatCPUTIMELimitAction(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_MSG_ON_JOB_TIME_LIMIT_HIT);
  143. break;
  144. case ENDJOB_ON_NO_PROC_COLUMN:
  145. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_END_JOB_WHEN_EMPTY);
  146. break;
  147. case DIE_ON_UNHANDLED_EXCEPT_COLUMN:
  148. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_SET_DIE_ON_UH_EXCEPTION);
  149. break;
  150. case ALLOW_BREAKAWAY_COLUMN:
  151. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_SET_PROC_BREAKAWAY_OK);
  152. break;
  153. case ALLOW_SILENT_BREAKAWAY_COLUMN:
  154. pstr = FormatApplyFlag(m_ResultStr, ref.mgmtParms.mFlags & PCMFLAG_SET_SILENT_BREAKAWAY);
  155. break;
  156. default:
  157. ASSERT(FALSE);
  158. pstr = _T("");
  159. break;
  160. }
  161. }
  162. return S_OK;
  163. }
  164. HRESULT CJobRuleFolder::OnShow(BOOL bSelecting, HSCOPEITEM hItem, IHeaderCtrl2* ipHeaderCtrl, IConsole2* ipConsole2)
  165. {
  166. ASSERT(hItem == GetID());
  167. if (!bSelecting)
  168. return S_OK;
  169. ITEM_STR str;
  170. LoadStringHelper(str, IDS_JOB_HDR);
  171. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( JOB_COLUMN, str, 0, JOB_COLUMN_WIDTH ));
  172. LoadStringHelper(str, IDS_DESCRIPTION_HDR);
  173. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( DESCRIPTION_COLUMN, str, 0, DESCRIPTION_COLUMN_WIDTH ));
  174. LoadStringHelper(str, IDS_APPLY_AFFINITY_HDR);
  175. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_AFFINITY_COLUMN, str, 0, APPLY_AFFINITY_COLUMN_WIDTH ));
  176. LoadStringHelper(str, IDS_AFFINITY_HDR);
  177. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( AFFINITY_COLUMN, str, 0, AFFINITY_COLUMN_WIDTH ));
  178. LoadStringHelper(str, IDS_APPLY_PRIORITY_HDR);
  179. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_PRIORITY_COLUMN, str, 0, APPLY_PRIORITY_COLUMN_WIDTH ));
  180. LoadStringHelper(str, IDS_PRIORITY_HDR);
  181. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( PRIORITY_COLUMN, str, 0, PRIORITY_COLUMN_WIDTH ));
  182. LoadStringHelper(str, IDS_APPLY_SCHEDULING_CLASS_HDR);
  183. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_SCHEDULING_CLASS_COLUMN, str, 0, APPLY_SCHEDULING_CLASS_COLUMN_WIDTH ));
  184. LoadStringHelper(str, IDS_SCHEDULING_CLASS_HDR);
  185. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( SCHEDULING_CLASS_COLUMN, str, 0, SCHEDULING_CLASS_COLUMN_WIDTH ));
  186. LoadStringHelper(str, IDS_APPLY_MINMAXWS_HDR);
  187. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_MINMAXWS_COLUMN, str, 0, APPLY_MINMAXWS_COLUMN_WIDTH ));
  188. LoadStringHelper(str, IDS_MINWS_HDR);
  189. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( MINWS_COLUMN, str, 0, MINWS_COLUMN_WIDTH ));
  190. LoadStringHelper(str, IDS_MAXWS_HDR);
  191. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( MAXWS_COLUMN, str, 0, MAXWS_COLUMN_WIDTH ));
  192. LoadStringHelper(str, IDS_APPLY_PROC_CMEM_LIMIT_HDR);
  193. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_PROC_CMEM_LIMIT_COLUMN, str, 0, APPLY_PROC_CMEM_LIMIT_COLUMN_WIDTH ));
  194. LoadStringHelper(str, IDS_PROC_CMEM_LIMIT_HDR);
  195. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( PROC_CMEM_LIMIT_COLUMN, str, 0, PROC_CMEM_LIMIT_COLUMN_WIDTH ));
  196. LoadStringHelper(str, IDS_APPLY_JOB_CMEM_LIMIT_HDR);
  197. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_JOB_CMEM_LIMIT_COLUMN, str, 0, APPLY_JOB_CMEM_LIMIT_COLUMN_WIDTH ));
  198. LoadStringHelper(str, IDS_JOB_CMEM_LIMIT_HDR);
  199. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( JOB_CMEM_LIMIT_COLUMN, str, 0, JOB_CMEM_LIMIT_COLUMN_WIDTH ));
  200. LoadStringHelper(str, IDS_APPLY_PROCCOUNT_LIMIT_HDR);
  201. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_PROCCOUNT_LIMIT_COLUMN, str, 0, APPLY_PROCCOUNT_LIMIT_COLUMN_WIDTH ));
  202. LoadStringHelper(str, IDS_PROCCOUNT_LIMIT_HDR);
  203. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( PROCCOUNT_LIMIT_COLUMN, str, 0, PROCCOUNT_LIMIT_COLUMN_WIDTH ));
  204. LoadStringHelper(str, IDS_APPLY_PROC_CPUTIME_LIMIT_HDR);
  205. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_PROC_CPUTIME_LIMIT_COLUMN, str, 0, APPLY_PROC_CPUTIME_LIMIT_COLUMN_WIDTH ));
  206. LoadStringHelper(str, IDS_PROC_CPUTIME_LIMIT_HDR);
  207. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( PROC_CPUTIME_LIMIT_COLUMN, str, 0, PROC_CPUTIME_LIMIT_COLUMN_WIDTH ));
  208. LoadStringHelper(str, IDS_APPLY_JOB_CPUTIME_LIMIT_HDR);
  209. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( APPLY_JOB_CPUTIME_LIMIT_COLUMN, str, 0, APPLY_JOB_CPUTIME_LIMIT_COLUMN_WIDTH ));
  210. LoadStringHelper(str, IDS_JOB_CPUTIME_LIMIT_HDR);
  211. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( JOB_CPUTIME_LIMIT_COLUMN, str, 0, JOB_CPUTIME_LIMIT_COLUMN_WIDTH ));
  212. LoadStringHelper(str, IDS_ACTION_JOB_CPUTIME_LIMIT_HDR);
  213. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( ACTION_JOB_CPUTIME_LIMIT_COLUMN, str, 0, ACTION_JOB_CPUTIME_LIMIT_COLUMN_WIDTH ));
  214. LoadStringHelper(str, IDS_ENDJOB_ON_NO_PROC_HDR);
  215. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( ENDJOB_ON_NO_PROC_COLUMN, str, 0, ENDJOB_ON_NO_PROC_COLUMN_WIDTH ));
  216. LoadStringHelper(str, IDS_DIE_ON_UNHANDLED_EXCEPT_HDR);
  217. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( DIE_ON_UNHANDLED_EXCEPT_COLUMN, str, 0, DIE_ON_UNHANDLED_EXCEPT_COLUMN_WIDTH ));
  218. LoadStringHelper(str, IDS_ALLOW_BREAKAWAY_HDR);
  219. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( ALLOW_BREAKAWAY_COLUMN, str, 0, ALLOW_BREAKAWAY_COLUMN_WIDTH ));
  220. LoadStringHelper(str, IDS_ALLOW_SILENT_BREAKAWAY_HDR);
  221. VERIFY(S_OK == ipHeaderCtrl->InsertColumn( ALLOW_SILENT_BREAKAWAY_COLUMN, str, 0, ALLOW_SILENT_BREAKAWAY_COLUMN_WIDTH ));
  222. return OnRefresh(ipConsole2);
  223. }
  224. HRESULT CJobRuleFolder::AddMenuItems(LPCONTEXTMENUCALLBACK piCallback, long * pInsertionAllowed)
  225. {
  226. HRESULT hr = S_OK;
  227. ITEM_STR name;
  228. ITEM_STR status;
  229. BOOL bConnected = GetPCid();
  230. CONTEXTMENUITEM m = { 0 };
  231. if (*pInsertionAllowed & CCM_INSERTIONALLOWED_TOP)
  232. {
  233. for (const CONTEXTMENUITEMBYID *M = TopMenuItems; M->lCommandID; M++)
  234. {
  235. m.strName = const_cast<TCHAR *>(LoadStringHelper(name, M->strNameID));
  236. m.strStatusBarText = const_cast<TCHAR *>(LoadStringHelper(status, M->strStatusBarTextID));
  237. m.lCommandID = M->lCommandID;
  238. m.lInsertionPointID = M->lInsertionPointID;
  239. m.fFlags = bConnected ? MF_ENABLED : MF_GRAYED;
  240. //m.fSpecialFlags = 0; // currently always 0, initialized to 0
  241. hr = piCallback->AddItem(&m);
  242. if (FAILED(hr))
  243. break;
  244. }
  245. }
  246. if (*pInsertionAllowed & CCM_INSERTIONALLOWED_NEW )
  247. {
  248. for (const CONTEXTMENUITEMBYID *M = NewMenuItems; M->lCommandID; M++)
  249. {
  250. m.strName = const_cast<TCHAR *>(LoadStringHelper(name, M->strNameID));
  251. m.strStatusBarText = const_cast<TCHAR *>(LoadStringHelper(status, M->strStatusBarTextID));
  252. m.lCommandID = M->lCommandID;
  253. m.lInsertionPointID = M->lInsertionPointID;
  254. m.fFlags = bConnected ? MF_ENABLED : MF_GRAYED;
  255. //m.fSpecialFlags = 0; // currently always 0, initialized to 0
  256. hr = piCallback->AddItem(&m);
  257. if (FAILED(hr))
  258. break;
  259. }
  260. }
  261. return hr;
  262. }
  263. HRESULT CJobRuleFolder::OnHelpCmd(IDisplayHelp *ipDisplayHelp)
  264. {
  265. if (!ipDisplayHelp)
  266. return E_UNEXPECTED;
  267. ipDisplayHelp->ShowTopic(const_cast<TCHAR *>(HELP_ru_job));
  268. return S_OK;
  269. }
  270. HRESULT CJobRuleFolder::OnSelect(BOOL bScope, BOOL bSelect, IConsoleVerb* ipConsoleVerb )
  271. {
  272. ASSERT(bScope);
  273. if (bSelect)
  274. {
  275. VERIFY( ipConsoleVerb->SetVerbState( MMC_VERB_REFRESH, ENABLED, TRUE ) == S_OK);
  276. VERIFY( ipConsoleVerb->SetDefaultVerb( MMC_VERB_OPEN ) == S_OK );
  277. }
  278. return S_OK;
  279. }
  280. HRESULT CJobRuleFolder::OnSelect(BOOL bScope, BOOL bSelect, IConsoleVerb* ipConsoleVerb, LPARAM Cookie)
  281. {
  282. ASSERT(!bScope);
  283. VERIFY( ipConsoleVerb->SetVerbState( MMC_VERB_REFRESH, ENABLED, TRUE ) == S_OK);
  284. if (bSelect && !bScope) // incase the rules are changed again leave !bScope test
  285. {
  286. VERIFY( ipConsoleVerb->SetVerbState( MMC_VERB_PROPERTIES, ENABLED, TRUE ) == S_OK);
  287. VERIFY( ipConsoleVerb->SetVerbState( MMC_VERB_DELETE, ENABLED, TRUE ) == S_OK);
  288. VERIFY( ipConsoleVerb->SetDefaultVerb( MMC_VERB_PROPERTIES ) == S_OK );
  289. }
  290. return S_OK;
  291. }
  292. HRESULT CJobRuleFolder::OnMenuCommand(IConsole2 *ipConsole2, long nCommandID )
  293. {
  294. HRESULT hr = S_FALSE;
  295. switch(nCommandID)
  296. {
  297. case ID_JRULE_DEFINE:
  298. case ID_JRULE_NEW:
  299. {
  300. PCid hID = GetPCid();
  301. PCSystemInfo SystemInfo;
  302. if (!hID || !PCGetServiceInfo(hID, &SystemInfo, sizeof(SystemInfo)))
  303. {
  304. ReportPCError();
  305. return S_OK;
  306. }
  307. PCJobSummary *ptr = new PCJobSummary[1];
  308. if (!ptr)
  309. return E_UNEXPECTED;
  310. PCJobDetail JobDetail = { 0 };
  311. if (GroupRuleWizard(IDS_JRULE_CREATE_TITLE, JobDetail, SystemInfo.sysParms, NULL) )
  312. {
  313. if (PCAddJobDetail(hID, &JobDetail) )
  314. {
  315. *ptr = JobDetail.base;
  316. MemBlocks.push_front(ptr);
  317. Cache.push_front(ptr);
  318. VERIFY(S_OK == SendViewChange(ipConsole2, (INT_PTR) ptr, PC_VIEW_ADDITEM));
  319. return S_OK;
  320. }
  321. ReportPCError();
  322. }
  323. delete [] ptr;
  324. }
  325. return S_OK; // we handled the message...
  326. break;
  327. default:
  328. break;
  329. }
  330. return hr;
  331. }
  332. HRESULT CJobRuleFolder::OnMenuCommand(IConsole2 *ipConsole2, long nCommandID, LPARAM Cookie )
  333. {
  334. return OnMenuCommand(ipConsole2, nCommandID);
  335. }
  336. HRESULT CJobRuleFolder::OnViewChange(IResultData *ipResultData, LPARAM thing, LONG_PTR hint)
  337. {
  338. ASSERT(ipResultData);
  339. if (!ipResultData)
  340. return E_UNEXPECTED;
  341. VERIFY(ipResultData->ModifyViewStyle(MMC_SINGLESEL, MMC_NOSORTHEADER) == S_OK);
  342. list<PCJobSummary*>::iterator item;
  343. HRESULT hr = E_UNEXPECTED;
  344. switch (hint)
  345. {
  346. case PC_VIEW_REDRAWALL:// not currently used
  347. hr = ShowAllItems(ipResultData, TRUE);
  348. break;
  349. case PC_VIEW_SETITEM: // not currently used
  350. ASSERT(FALSE); // add smarter support for this hint...
  351. hr = ShowAllItems(ipResultData, TRUE);
  352. break;
  353. case PC_VIEW_UPDATEALL:
  354. hr = ShowAllItems(ipResultData, FALSE);
  355. break;
  356. case PC_VIEW_UPDATEITEM:
  357. {
  358. HRESULTITEM hItem;
  359. hr = ipResultData->FindItemByLParam(thing, &hItem);
  360. if (hr == S_OK)
  361. hr = ipResultData->UpdateItem(hItem);
  362. }
  363. break;
  364. case PC_VIEW_ADDITEM:
  365. item = find(Cache.begin(), Cache.end(), reinterpret_cast<PCJobSummary*>(thing) );
  366. if (item == Cache.end())
  367. hr = E_UNEXPECTED;
  368. else
  369. {
  370. RESULTDATAITEM data = { 0 };
  371. data.mask = RDI_STR | RDI_IMAGE | RDI_PARAM | RDI_STATE;
  372. data.bScopeItem = FALSE;
  373. //data.itemID
  374. //data.nIndex
  375. //data.nCol
  376. data.str = (LPOLESTR)MMC_CALLBACK;
  377. data.nImage = JOBRULEITEMIMAGE;
  378. //data.nState
  379. //data.iIndent //reserved
  380. data.lParam = thing;
  381. //data.nState
  382. hr = ipResultData->InsertItem(&data);
  383. }
  384. break;
  385. case PC_VIEW_DELETEITEM:
  386. {
  387. HRESULTITEM hItem;
  388. hr = ipResultData->FindItemByLParam(thing, &hItem);
  389. if (hr == S_OK)
  390. hr = ipResultData->DeleteItem(hItem, 0);
  391. }
  392. break;
  393. default:
  394. hr = E_UNEXPECTED;
  395. break;
  396. }
  397. ASSERT(hr == S_OK);
  398. return hr;
  399. }
  400. HRESULT CJobRuleFolder::ShowAllItems(IResultData* ipResultData, BOOL bCacheValid)
  401. {
  402. list<PCJobSummary*>::iterator item;
  403. LPARAM selected = 0;
  404. LPARAM focused = 0;
  405. RESULTDATAITEM data;
  406. if (bCacheValid)
  407. {
  408. memset(&data, 0, sizeof(data));
  409. data.nIndex = -1;
  410. data.nState = LVIS_SELECTED;
  411. data.mask = RDI_STATE;
  412. if (S_OK == ipResultData->GetNextItem(&data) && data.nIndex != -1 )
  413. {
  414. selected = data.lParam;
  415. }
  416. memset(&data, 0, sizeof(data));
  417. data.nIndex = -1;
  418. data.nState = LVIS_FOCUSED;
  419. data.mask = RDI_STATE;
  420. if (S_OK == ipResultData->GetNextItem(&data) && data.nIndex != -1)
  421. {
  422. focused = data.lParam;
  423. }
  424. }
  425. ipResultData->DeleteAllRsltItems();
  426. memset(&data, 0, sizeof(data));
  427. data.mask = RDI_STR | RDI_IMAGE | RDI_PARAM | RDI_STATE;
  428. data.bScopeItem = FALSE;
  429. //data.itemID;
  430. data.nIndex = 0;
  431. data.nCol = 0;
  432. data.str = (LPOLESTR)MMC_CALLBACK;
  433. data.nImage = JOBRULEITEMIMAGE;
  434. data.iIndent = 0; //reserved
  435. HRESULT hr = S_OK;
  436. for (list<PCJobSummary*>::iterator i = Cache.begin(); i != Cache.end(); ++i)
  437. {
  438. data.lParam = reinterpret_cast<LPARAM>(*i);
  439. data.nState = 0;
  440. if (data.lParam == selected)
  441. data.nState |= LVIS_SELECTED;
  442. if (data.lParam == focused)
  443. data.nState |= LVIS_FOCUSED;
  444. hr = ipResultData->InsertItem(&data);
  445. if (hr != S_OK)
  446. break;
  447. }
  448. return hr;
  449. }
  450. void CJobRuleFolder::ClearCache()
  451. {
  452. Cache.clear();
  453. for (list<PCJobSummary*>::iterator chunck = MemBlocks.begin(); chunck != MemBlocks.end(); ++chunck)
  454. {
  455. delete [] (*chunck);
  456. }
  457. MemBlocks.clear();
  458. }
  459. BOOL CJobRuleFolder::RefreshCache()
  460. {
  461. PCINT32 res = 0;
  462. PCULONG32 err = 0;
  463. PCJobSummary *last = NULL;
  464. const int MINIMUM_ALLOCATION = min((COM_BUFFER_SIZE/sizeof(PCJobSummary)), 100);
  465. ClearCache();
  466. PCid hID = GetPCid();
  467. if (!hID)
  468. {
  469. ReportPCError();
  470. return false;
  471. }
  472. do
  473. {
  474. PCJobSummary *ptr = new PCJobSummary[MINIMUM_ALLOCATION];
  475. if (!ptr)
  476. {
  477. err = ERROR_OUTOFMEMORY;
  478. break;
  479. }
  480. if (last)
  481. memcpy(ptr, last, sizeof(PCJobSummary));
  482. else
  483. memset(ptr, 0, sizeof(PCJobSummary));
  484. res = PCGetJobSummary( hID, ptr, MINIMUM_ALLOCATION * sizeof(PCJobSummary));
  485. if (res < 0 )
  486. {
  487. err = GetLastPCError();
  488. // ATLTRACE(_T("PCJobSummary returned error 0x%lX\n"), err);
  489. delete [] ptr;
  490. break;
  491. }
  492. if (res > 0)
  493. {
  494. last = &ptr[res - 1];
  495. MemBlocks.push_front(ptr);
  496. for (INT32 i = 0; i < res; i++)
  497. {
  498. Cache.insert(Cache.end(), ptr);
  499. ptr++;
  500. }
  501. }
  502. } while (res > 0 && PCERROR_MORE_DATA == GetLastPCError() );
  503. if (err)
  504. ReportPCError();
  505. return err == 0;
  506. }
  507. HRESULT CJobRuleFolder::QueryPagesFor(LPARAM Cookie)
  508. {
  509. list<PCJobSummary*>::iterator item = find(Cache.begin(), Cache.end(), reinterpret_cast<PCJobSummary*>(Cookie) );
  510. if (item != Cache.end() )
  511. return S_OK;
  512. return E_UNEXPECTED;
  513. }
  514. HRESULT CJobRuleFolder::OnCreatePropertyPages( LPPROPERTYSHEETCALLBACK lpProvider, LONG_PTR handle, DATA_OBJECT_TYPES context, LPARAM Cookie)
  515. {
  516. list<PCJobSummary*>::iterator item;
  517. item = find(Cache.begin(), Cache.end(), reinterpret_cast<PCJobSummary*>(Cookie) );
  518. if (item == Cache.end() )
  519. return E_UNEXPECTED;
  520. return CreatePropertyPagesForJobDetail((*item)->jobName, lpProvider, handle, this);
  521. }
  522. HRESULT CJobRuleFolder::OnPropertyChange(PROPERTY_CHANGE_HDR *pUpdate, IConsole2 *ipConsole2)
  523. {
  524. return OnRefresh(ipConsole2);
  525. }
  526. HRESULT CJobRuleFolder::OnDelete(IConsole2 *ipConsole2, LPARAM Cookie)
  527. {
  528. list<PCJobSummary*>::iterator item;
  529. item = find(Cache.begin(), Cache.end(), reinterpret_cast<PCJobSummary *>(Cookie));
  530. if (item == Cache.end() )
  531. {
  532. SendViewChange(ipConsole2, NULL, PC_VIEW_UPDATEALL);
  533. return E_UNEXPECTED;
  534. }
  535. PCJobDetail Detail;
  536. memset(&Detail, 0, sizeof(Detail));
  537. memcpy(&(Detail.base), (*item), sizeof(PCJobSummary) );
  538. if (!PCDeleteJobDetail(GetPCid(), &Detail))
  539. {
  540. ReportPCError();
  541. return E_UNEXPECTED;
  542. }
  543. Cache.erase(item);
  544. return SendViewChange(ipConsole2, Cookie, PC_VIEW_DELETEITEM);
  545. }
  546. HRESULT CJobRuleFolder::OnRefresh(IConsole2 *ipConsole2)
  547. {
  548. RefreshCache();
  549. return SendViewChange(ipConsole2, NULL, PC_VIEW_UPDATEALL);
  550. }