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.

370 lines
9.8 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. SchdTask.cpp
  5. Abstract:
  6. CSchdTask - Class that allows access to a scheduled task.
  7. Check the task
  8. Create the task
  9. Delete the task
  10. Save the task
  11. Show property page
  12. Show task description in text box
  13. Author:
  14. Art Bragg 9/4/97
  15. Revision History:
  16. --*/
  17. #include "stdafx.h"
  18. #include "SchdTask.h"
  19. #include "SchedSht.h"
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CSchdTask
  22. //
  23. // Description: Save arguments to data members. Create the Scheduling Agent
  24. // object.
  25. //
  26. // Arguments:
  27. // szComputerName - Name of HSM computer owning task scheduler
  28. // taskID - Resource ID for task name
  29. // propPageTitleID - Resource ID for property page title
  30. // pEdit - Edit control to show description in
  31. //
  32. // Returns:
  33. // S_OK, S_XXXX
  34. //
  35. ///////////////////////////////////////////////////////////////////////////////////
  36. //
  37. CSchdTask::CSchdTask
  38. (
  39. CString szComputerName,
  40. const TCHAR* task,
  41. int propPageTitleID,
  42. const TCHAR* parameters,
  43. const TCHAR* comment,
  44. CEdit* pEdit
  45. )
  46. {
  47. HRESULT hr = S_OK;
  48. try {
  49. WsbTraceIn( L"CSchdTask::CSchdTask", L"ComputerName = <%ls> task = <%ls> propPageTitleID = <%d> pEdit = <%ld>",
  50. szComputerName, task, propPageTitleID, pEdit );
  51. m_pTask = NULL;
  52. m_szComputerName = szComputerName;
  53. // Save the property page title resource ID
  54. m_propPageTitleID = propPageTitleID;
  55. // Save the pointer to the control in which to display the schedule text
  56. m_pEdit = pEdit;
  57. WsbAffirmHr( m_pSchedAgent.CoCreateInstance( CLSID_CSchedulingAgent ) );
  58. // Get the hsm computer and prepend "\\"
  59. CString szHsmName ("\\\\" + szComputerName);
  60. // Tell the task manager which computer to look on
  61. m_pSchedAgent->SetTargetComputer (szHsmName);
  62. m_szJobTitle = task;
  63. m_szParameters = parameters;
  64. m_szComment = comment;
  65. } WsbCatch (hr);
  66. WsbTraceOut( L"CSchdTask::CSchdTask", L"hr = <%ls>", WsbHrAsString( hr ) );
  67. }
  68. //////////////////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Function: CheckTaskExists
  71. //
  72. // Description: Tries to access the task owned by the object. If the task does not
  73. // exist, returns S_FALSE and if caller requested puts up an error and
  74. // creates the task.
  75. //
  76. // Arguments: bCreateTask - true = put up an error and create task if it doesn't exist
  77. //
  78. // Returns: S_OK - Task exists
  79. // S_FALSE - Task did not exist (may have been created)
  80. // S_XXXX - Error
  81. //
  82. /////////////////////////////////////////////////////////////////////////////////////////////
  83. HRESULT
  84. CSchdTask::CheckTaskExists(
  85. BOOL bCreateTask
  86. )
  87. {
  88. WsbTraceIn( L"CSchdTask::CheckTaskExists", L"bCreateTask = <%ld>", bCreateTask );
  89. HRESULT hr = S_OK;
  90. try {
  91. //
  92. // Get the task we're interested in
  93. //
  94. CComPtr <IUnknown> pIU;
  95. if( m_pSchedAgent->Activate( m_szJobTitle, IID_ITask, &pIU ) == S_OK ) {
  96. //
  97. // QI to the task interface and save it
  98. //
  99. m_pTask.Release( );
  100. WsbAffirmHr( pIU->QueryInterface( IID_ITask, (void **) &m_pTask ) );
  101. } else {
  102. //
  103. // The task doesn't exist - create it if the caller wanted
  104. // us to.
  105. //
  106. if( bCreateTask ) {
  107. CString sMessage;
  108. AfxFormatString2( sMessage, IDS_ERR_MANAGE_TASK, m_szJobTitle, m_szComputerName );
  109. AfxMessageBox( sMessage, RS_MB_ERROR );
  110. //
  111. // Create the task
  112. //
  113. WsbAffirmHr( CreateTask( ) );
  114. WsbAffirmHr( Save( ) );
  115. }
  116. //
  117. // Return false (the task does or did not exist)
  118. //
  119. hr = S_FALSE;
  120. }
  121. } WsbCatch( hr );
  122. WsbTraceOut( L"CSchdTask::CheckTaskExists", L"hr = <%ls>", WsbHrAsString( hr ) );
  123. return( hr );
  124. }
  125. //////////////////////////////////////////////////////////////////////////////////////////////
  126. //
  127. // Function: CreateTask
  128. //
  129. // Description: Creates the data-member task in the task scheduler.
  130. //
  131. // Arguments: None
  132. //
  133. // Returns: S_OK, S_XXXX
  134. //
  135. /////////////////////////////////////////////////////////////////////////////////////////////
  136. HRESULT
  137. CSchdTask::CreateTask()
  138. {
  139. WsbTraceIn( L"CSchdTask::CreateTask", L"");
  140. HRESULT hr = S_OK;
  141. try {
  142. //
  143. // Need to connect to the HSM engine and let it create it
  144. // so that it runs under the LocalSystem account
  145. //
  146. CComPtr<IHsmServer> pServer;
  147. WsbAffirmHr( HsmConnectFromName( HSMCONN_TYPE_HSM, m_szComputerName, IID_IHsmServer, (void**)&pServer ) );
  148. WsbAffirmHr( pServer->CreateTask( m_szJobTitle, m_szParameters, m_szComment, TASK_TIME_TRIGGER_DAILY, 2, 0, TRUE ) );
  149. //
  150. // And Configure it
  151. //
  152. m_pTask.Release( );
  153. WsbAffirmHr( m_pSchedAgent->Activate( m_szJobTitle, IID_ITask, (IUnknown**)&m_pTask ) );
  154. } WsbCatch (hr);
  155. WsbTraceOut( L"CSchdTask::CreateTask", L"hr = <%ls>", WsbHrAsString( hr ) );
  156. return( hr );
  157. }
  158. ////////////////////////////////////////////////////////////////////////////////////////////
  159. //
  160. // Function: DeleteTask
  161. //
  162. // Description: Deletes the data-member task from the task scheduler
  163. //
  164. // Arguments: None
  165. //
  166. // Returns: S_OK, S_XXX
  167. //
  168. ////////////////////////////////////////////////////////////////////////////////////////////
  169. HRESULT
  170. CSchdTask::DeleteTask()
  171. {
  172. WsbTraceIn( L"CSchdTask::CreateTask", L"");
  173. HRESULT hr = S_OK;
  174. try {
  175. WsbAffirmPointer (m_pSchedAgent);
  176. WsbAffirmHr (m_pSchedAgent->Delete( m_szJobTitle ));
  177. } WsbCatch (hr);
  178. WsbTraceOut( L"CSchdTask::DeleteTask", L"hr = <%ls>", WsbHrAsString( hr ) );
  179. return hr;
  180. }
  181. //////////////////////////////////////////////////////////////////////////////////////////////
  182. //
  183. // Function: ShowPropertySheet
  184. //
  185. // Description: Shows a property sheet for the data-member task.
  186. //
  187. // Arguments: None
  188. //
  189. // Returns: S_OK, S_XXX
  190. //
  191. //////////////////////////////////////////////////////////////////////////////////////////////
  192. HRESULT
  193. CSchdTask::ShowPropertySheet()
  194. {
  195. WsbTraceIn( L"CSchdTask::ShowPropertySheet", L"");
  196. CScheduleSheet scheduleSheet(m_propPageTitleID , m_pTask, 0, 0 );
  197. scheduleSheet.DoModal( );
  198. WsbTraceOut( L"CSchdTask::ShowPropertySheet", L"hr = <%ls>", WsbHrAsString( S_OK ) );
  199. return S_OK;
  200. }
  201. //////////////////////////////////////////////////////////////////////////////////////////////
  202. //
  203. // Function: UpdateDescription
  204. //
  205. // Description: Displays the data-member task's summary in the data-member text box.
  206. //
  207. // Arguments: None
  208. //
  209. // Returns: S_OK, S_XXX
  210. //
  211. //////////////////////////////////////////////////////////////////////////////////////////////
  212. HRESULT
  213. CSchdTask::UpdateDescription
  214. (
  215. void
  216. )
  217. {
  218. WsbTraceIn( L"CSchdTask::UpdateDescription", L"" );
  219. HRESULT hr = S_OK;
  220. try {
  221. //
  222. // And set schedule text into the text box.
  223. //
  224. CString buildString;
  225. WORD triggerCount, triggerIndex;
  226. WsbAffirmHr( m_pTask->GetTriggerCount( &triggerCount ) );
  227. CWsbStringPtr scheduleString;
  228. for( triggerIndex = 0; triggerIndex < triggerCount; triggerIndex++ ) {
  229. WsbAffirmHr( m_pTask->GetTriggerString( triggerIndex, &scheduleString ) );
  230. buildString += scheduleString;
  231. buildString += L"\r\n";
  232. scheduleString.Free( );
  233. }
  234. m_pEdit->SetWindowText( buildString );
  235. //
  236. // Now check to see if we should add a scroll bar
  237. //
  238. //
  239. // It seems the only way to know that an edit control needs a scrollbar
  240. // is to force it to scroll to the bottom and see if the first
  241. // visible line is the first actual line
  242. //
  243. m_pEdit->LineScroll( MAXSHORT );
  244. if( m_pEdit->GetFirstVisibleLine( ) > 0 ) {
  245. //
  246. // Add the scroll styles
  247. //
  248. m_pEdit->ModifyStyle( 0, WS_VSCROLL | ES_AUTOVSCROLL, SWP_DRAWFRAME );
  249. } else {
  250. //
  251. // Remove the scrollbar (set range to 0)
  252. //
  253. m_pEdit->SetScrollRange( SB_VERT, 0, 0, TRUE );
  254. }
  255. //
  256. // Remove selection
  257. //
  258. m_pEdit->PostMessage( EM_SETSEL, -1, 0 );
  259. } WsbCatch( hr );
  260. WsbTraceOut( L"CSchdTask::UpdateDescription", L"hr = <%ls>", WsbHrAsString( hr ) );
  261. return( hr );
  262. }
  263. //////////////////////////////////////////////////////////////////////////////////////////////
  264. //
  265. // Function: Save
  266. //
  267. // Description: Saves the data member task to the task scheduler
  268. //
  269. // Arguments: None
  270. //
  271. // Returns: S_OK, S_XXX
  272. //
  273. //////////////////////////////////////////////////////////////////////////////////////////////
  274. HRESULT
  275. CSchdTask::Save (void)
  276. {
  277. WsbTraceIn( L"CSchdTask::Save", L"" );
  278. HRESULT hr = S_OK;
  279. try {
  280. CComPtr<IPersistFile> pPersist;
  281. WsbAffirmHr( m_pTask->QueryInterface( IID_IPersistFile, (void**)&pPersist ) );
  282. WsbAffirmHr( pPersist->Save( 0, 0 ) );
  283. } WsbCatch (hr);
  284. WsbTraceOut( L"CSchdTask::Save", L"hr = <%ls>", WsbHrAsString( hr ) );
  285. return( hr );
  286. }