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.

473 lines
9.4 KiB

  1. #include "stdafx.h"
  2. #include "evtview.h"
  3. #include "globals.h"
  4. #include "SInfoDlg.h"
  5. #include "schview.h"
  6. extern CScheduleView oScheduleView ;
  7. CScheduleInfo oSchedule ;
  8. void
  9. DoAction (CPtrList &p)
  10. {
  11. POSITION pos ;
  12. SCHEDULE_ACTIONINFO *pInfo ;
  13. PROCESS_INFORMATION sProcessInformation ;
  14. STARTUPINFO sStartupInfo ;
  15. GetStartupInfo (&sStartupInfo) ;
  16. pos = p.GetHeadPosition () ;
  17. while (pos)
  18. {
  19. pInfo = (SCHEDULE_ACTIONINFO *)p.GetNext (pos) ;
  20. if (!CreateProcess (NULL, (LPWSTR)(LPCWSTR)pInfo->stParam, NULL, NULL, FALSE, 0, NULL, NULL, &sStartupInfo, &sProcessInformation))
  21. {
  22. WCHAR szBuf [100] ;
  23. wsprintf (szBuf, L"CreateProcess Failed : %ld", GetLastError()) ;
  24. AfxMessageBox (szBuf) ;
  25. }
  26. }
  27. }
  28. void CheckForTime ()
  29. {
  30. POSITION pos = ptrlstSInfo.GetHeadPosition (), posPrev ;
  31. SCHEDULE_INFO *pSInfo ;
  32. CTime t = CTime::GetCurrentTime () ;
  33. while (pos)
  34. {
  35. posPrev = pos ;
  36. pSInfo = (SCHEDULE_INFO *) ptrlstSInfo.GetNext (pos) ;
  37. if (pSInfo->lstTimeInfo.GetCount() && t >= pSInfo->minTime)
  38. {
  39. DoAction (pSInfo->lstActionInfo) ;
  40. }
  41. ComputeAbsoluteTime (pSInfo) ;
  42. if (pSInfo->lstTimeInfo.GetCount() == 0 &&
  43. pSInfo->lstEventInfo.GetCount() == 0)
  44. {
  45. FreeEventList (pSInfo->lstEventInfo) ;
  46. FreeActionList (pSInfo->lstActionInfo) ;
  47. FreeTimeList (pSInfo->lstTimeInfo) ;
  48. delete pSInfo ;
  49. ptrlstSInfo.RemoveAt (posPrev) ;
  50. }
  51. }
  52. ResetTimer () ;
  53. if (oScheduleView.GetSafeHwnd())
  54. oScheduleView.PostMessage (WM_REFRESH, 0, 0) ;
  55. }
  56. void CheckForEvent (PEVTFILTER_TYPE pEvent)
  57. {
  58. POSITION pos = ptrlstSInfo.GetHeadPosition (), posPrev ;
  59. SCHEDULE_INFO *pSInfo ;
  60. POSITION pos1 ;
  61. SCHEDULE_EVENTINFO *pEInfo ;
  62. while (pos)
  63. {
  64. posPrev = pos ;
  65. pSInfo = (SCHEDULE_INFO *) ptrlstSInfo.GetNext (pos) ;
  66. if (pSInfo->lstEventInfo.GetCount())
  67. {
  68. pos1 = pSInfo->lstEventInfo.GetHeadPosition () ;
  69. while (pos1)
  70. {
  71. pEInfo = (SCHEDULE_EVENTINFO *)pSInfo->lstEventInfo.GetNext (pos1) ;
  72. if ((pEInfo->dwFilter & pEvent->dwFilter) &&
  73. ((wcslen (pEInfo->szObjectName) == 0) ||
  74. (wcscmp (pEInfo->szObjectName, pEvent->szObjectName) == 0)) &&
  75. ((wcslen (pEInfo->szSourceName) == 0) ||
  76. (wcscmp (pEInfo->szSourceName, pEvent->szSourceName) == 0)))
  77. {
  78. DoAction (pSInfo->lstActionInfo) ;
  79. break ;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. LRESULT CALLBACK WindowProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  86. {
  87. switch (msg)
  88. {
  89. case WM_TIMER:
  90. CheckForTime () ;
  91. break ;
  92. case WM_GOTEVENT:
  93. CheckForEvent ((PEVTFILTER_TYPE)lParam) ;
  94. break ;
  95. default:
  96. return DefWindowProc (hWnd, msg, wParam, lParam) ;
  97. }
  98. return 0 ;
  99. }
  100. HWND InitWindow ()
  101. {
  102. WNDCLASS sWndClass ;
  103. sWndClass.style = 0 ;
  104. sWndClass.lpfnWndProc = WindowProc ;
  105. sWndClass.cbClsExtra = 0 ;
  106. sWndClass.cbWndExtra = 0 ;
  107. sWndClass.hInstance = AfxGetApp()->m_hInstance ;
  108. sWndClass.hIcon = NULL ;
  109. sWndClass.hCursor = NULL ;
  110. sWndClass.hbrBackground = NULL ;
  111. sWndClass.lpszMenuName = NULL ;
  112. sWndClass.lpszClassName = L"EVTVIEW_SCHEDULE" ;
  113. RegisterClass ( &sWndClass) ;
  114. return CreateWindow (sWndClass.lpszClassName, L"EVTVIEW_SCHEDULE", 0, 0, 0, 0, 0, NULL, NULL, sWndClass.hInstance, NULL) ;
  115. }
  116. ScheduleInit ()
  117. {
  118. hScheduleWnd = InitWindow () ;
  119. if (hScheduleWnd != NULL)
  120. {
  121. }
  122. else
  123. {
  124. AfxMessageBox (L"Create Window failed in Schedule Thread") ;
  125. }
  126. return 0 ;
  127. }
  128. void ScheduleDeInit()
  129. {
  130. UnregisterClass (L"EVTVIEW_SCHEDULE", AfxGetApp()->m_hInstance) ;
  131. POSITION pos = ptrlstSInfo.GetHeadPosition () ;
  132. SCHEDULE_INFO *pSInfo ;
  133. while (pos)
  134. {
  135. pSInfo = (SCHEDULE_INFO *)ptrlstSInfo.GetNext (pos) ;
  136. FreeEventList (pSInfo->lstEventInfo) ;
  137. FreeActionList (pSInfo->lstActionInfo) ;
  138. FreeTimeList (pSInfo->lstTimeInfo) ;
  139. delete pSInfo ;
  140. }
  141. }
  142. void FreeEventList (CPtrList &ptrlst)
  143. {
  144. POSITION pos ;
  145. pos = ptrlst.GetHeadPosition () ;
  146. while (pos)
  147. {
  148. delete (SCHEDULE_EVENTINFO *) ptrlst.GetNext (pos) ;
  149. }
  150. ptrlst.RemoveAll () ;
  151. }
  152. void FreeActionList (CPtrList &ptrlst)
  153. {
  154. POSITION pos ;
  155. pos = ptrlst.GetHeadPosition () ;
  156. while (pos)
  157. {
  158. delete (SCHEDULE_ACTIONINFO *) ptrlst.GetNext (pos) ;
  159. }
  160. ptrlst.RemoveAll () ;
  161. }
  162. void FreeTimeList (CPtrList &ptrlst)
  163. {
  164. POSITION pos ;
  165. pos = ptrlst.GetHeadPosition () ;
  166. while (pos)
  167. {
  168. delete (SCHEDULE_TIMEINFO *) ptrlst.GetNext (pos) ;
  169. }
  170. ptrlst.RemoveAll () ;
  171. }
  172. CTime GetNextTime (SCHEDULE_TIMEINFO *pInfo)
  173. {
  174. CTime curTime = CTime::GetCurrentTime (), newTime ;
  175. SCHEDULE_TIMEINFO tmpInfo ;
  176. tmpInfo = *pInfo ;
  177. int iYear, iYearStart, iYearEnd ;
  178. int iMon, iMonStart, iMonEnd ;
  179. int iDay, iDayStart, iDayEnd ;
  180. int iHour, iHourStart, iHourEnd ;
  181. int iMin, iMinStart, iMinEnd ;
  182. int iSec, iSecStart, iSecEnd ;
  183. if (tmpInfo.iYear == -1)
  184. {
  185. iYearStart = curTime.GetYear () ;
  186. iYearEnd = 2038 ;
  187. }
  188. else
  189. iYearStart = iYearEnd = tmpInfo.iYear ;
  190. if (tmpInfo.iMonth == -1)
  191. {
  192. iMonStart = 1 ;
  193. iMonEnd = 12 ;
  194. }
  195. else
  196. iMonStart = iMonEnd = tmpInfo.iMonth ;
  197. if (tmpInfo.iDay == -1)
  198. {
  199. iDayStart = 1 ;
  200. iDayEnd = 31 ;
  201. }
  202. else
  203. iDayStart = iDayEnd = tmpInfo.iDay ;
  204. if (tmpInfo.iHour == -1)
  205. {
  206. iHourStart = 0 ;
  207. iHourEnd = 23 ;
  208. }
  209. else
  210. iHourStart = iHourEnd = tmpInfo.iHour ;
  211. if (tmpInfo.iMin == -1)
  212. {
  213. iMinStart = 0 ;
  214. iMinEnd = 59 ;
  215. }
  216. else
  217. iMinStart = iMinEnd = tmpInfo.iMin ;
  218. if (tmpInfo.iSec == -1)
  219. {
  220. iSecStart = 0 ;
  221. iSecEnd = 59 ;
  222. }
  223. else
  224. iSecStart = iSecEnd = tmpInfo.iSec ;
  225. for (iYear = iYearStart; iYear <= iYearEnd; iYear++)
  226. {
  227. for (iMon = iMonStart; iMon <= iMonEnd ; iMon++)
  228. {
  229. for (iDay = iDayStart; iDay <= iDayEnd; iDay++)
  230. {
  231. for (iHour = iHourStart; iHour <= iHourEnd; iHour++)
  232. {
  233. for (iMin = iMinStart; iMin <= iMinEnd; iMin++)
  234. {
  235. for (iSec = iSecStart; iSec <= iSecEnd; iSec++)
  236. {
  237. newTime = CTime (iYear, iMon, iDay, iHour, iMin, iSec) ;
  238. if (newTime > curTime)
  239. return newTime ;
  240. } // Second
  241. } // Minutes
  242. }
  243. } // Day
  244. } // Month
  245. } // Year
  246. // Give a old value so that this entry will be deleted
  247. newTime = curTime - CTimeSpan (1, 1, 1, 1) ;
  248. return newTime ;
  249. }
  250. void
  251. ComputeAbsoluteTime (SCHEDULE_INFO *pSInfo)
  252. {
  253. CPtrList & ptrlst = pSInfo->lstTimeInfo ;
  254. POSITION pos = ptrlst.GetHeadPosition (), posPrev ;
  255. SCHEDULE_TIMEINFO *pInfo ;
  256. CTime curTime = CTime::GetCurrentTime (), minTime ;
  257. BOOL bFirstFlag = TRUE ;
  258. while (pos)
  259. {
  260. posPrev = pos ;
  261. pInfo = (SCHEDULE_TIMEINFO *) ptrlst.GetNext (pos) ;
  262. pInfo->ctime = GetNextTime (pInfo) ;
  263. if (pInfo->ctime < curTime)
  264. {
  265. oSchedule.Terminate () ;
  266. delete pInfo ;
  267. ptrlst.RemoveAt (posPrev) ;
  268. continue ;
  269. }
  270. if (bFirstFlag)
  271. {
  272. minTime = pInfo->ctime ;
  273. bFirstFlag = FALSE ;
  274. }
  275. else if (minTime > pInfo->ctime)
  276. minTime = pInfo->ctime ;
  277. }
  278. pSInfo->minTime = minTime ;
  279. }
  280. void
  281. ResetTimer ()
  282. {
  283. KillTimer (hScheduleWnd, nIDTimer) ;
  284. CTime minTime ;
  285. BOOL bFirstFlag = TRUE ;
  286. POSITION pos = ptrlstSInfo.GetHeadPosition () ;
  287. SCHEDULE_INFO *pSInfo ;
  288. while (pos)
  289. {
  290. pSInfo = (SCHEDULE_INFO *) ptrlstSInfo.GetNext (pos) ;
  291. if (pSInfo->lstTimeInfo.GetCount() && bFirstFlag)
  292. {
  293. bFirstFlag = FALSE ;
  294. minTime = pSInfo->minTime ;
  295. }
  296. else if (pSInfo->lstTimeInfo.GetCount() && minTime > pSInfo->minTime)
  297. minTime = pSInfo->minTime ;
  298. }
  299. if (!bFirstFlag)
  300. {
  301. CTimeSpan s = minTime - CTime::GetCurrentTime () ;
  302. int i = s.GetTotalSeconds() ;
  303. TRACE (L"SetTime %d\n", i) ;
  304. //i = 2 ;
  305. nIDTimer = SetTimer (hScheduleWnd , 77, i*1000, NULL) ;
  306. // nIDTimer = SetTimer (hScheduleWnd , 77, s.GetTotalSeconds()*1000, NULL) ;
  307. }
  308. }
  309. // Copies from p2 to p1
  310. void
  311. CopyScheduleInfo (SCHEDULE_INFO *p1, SCHEDULE_INFO *p2)
  312. {
  313. SCHEDULE_TIMEINFO *pTimeInfo ;
  314. SCHEDULE_EVENTINFO *pEventInfo ;
  315. SCHEDULE_ACTIONINFO *pActionInfo ;
  316. POSITION pos ;
  317. FreeEventList (p1->lstEventInfo) ;
  318. FreeActionList (p1->lstActionInfo) ;
  319. FreeTimeList (p1->lstTimeInfo) ;
  320. p1->minTime = p2->minTime ;
  321. pos = p2->lstTimeInfo.GetHeadPosition () ;
  322. while (pos)
  323. {
  324. pTimeInfo = (SCHEDULE_TIMEINFO *) p2->lstTimeInfo.GetNext (pos) ;
  325. p1->lstTimeInfo.AddTail (new SCHEDULE_TIMEINFO(*pTimeInfo)) ;
  326. }
  327. pos = p2->lstEventInfo.GetHeadPosition () ;
  328. while (pos)
  329. {
  330. pEventInfo = (SCHEDULE_EVENTINFO *) p2->lstEventInfo.GetNext (pos) ;
  331. p1->lstEventInfo.AddTail (new SCHEDULE_EVENTINFO(*pEventInfo)) ;
  332. }
  333. pos = p2->lstActionInfo.GetHeadPosition () ;
  334. while (pos)
  335. {
  336. pActionInfo = (SCHEDULE_ACTIONINFO *) p2->lstActionInfo.GetNext (pos) ;
  337. p1->lstActionInfo.AddTail (new SCHEDULE_ACTIONINFO(*pActionInfo)) ;
  338. }
  339. }
  340. AddSchedule ()
  341. {
  342. SCHEDULE_INFO *pSInfo = new SCHEDULE_INFO ;
  343. oSchedule.pSInfo = pSInfo ;
  344. if (oSchedule.DoModal () == IDOK)
  345. {
  346. ComputeAbsoluteTime (pSInfo) ;
  347. if (pSInfo->lstTimeInfo.GetCount() ||
  348. pSInfo->lstEventInfo.GetCount() )
  349. {
  350. ptrlstSInfo.AddTail (pSInfo) ;
  351. if (pSInfo->lstTimeInfo.GetCount())
  352. ResetTimer () ;
  353. }
  354. else
  355. {
  356. FreeEventList (pSInfo->lstEventInfo) ;
  357. FreeActionList (pSInfo->lstActionInfo) ;
  358. FreeTimeList (pSInfo->lstTimeInfo) ;
  359. delete pSInfo ;
  360. }
  361. if (oScheduleView.GetSafeHwnd())
  362. oScheduleView.PostMessage (WM_REFRESH, 0, 0) ;
  363. }
  364. else
  365. {
  366. FreeEventList (pSInfo->lstEventInfo) ;
  367. FreeActionList (pSInfo->lstActionInfo) ;
  368. FreeTimeList (pSInfo->lstTimeInfo) ;
  369. delete pSInfo ;
  370. }
  371. return 0 ;
  372. }
  373. ModifySchedule (SCHEDULE_INFO *pSOldInfo)
  374. {
  375. int iRet ;
  376. SCHEDULE_INFO *pSInfo = new SCHEDULE_INFO ;
  377. CopyScheduleInfo (pSInfo, pSOldInfo) ;
  378. oSchedule.pSInfo = pSInfo ;
  379. if ((iRet = (int)oSchedule.DoModal ()) == IDOK)
  380. {
  381. ComputeAbsoluteTime (pSInfo) ;
  382. CopyScheduleInfo (pSOldInfo, pSInfo) ;
  383. ResetTimer () ;
  384. }
  385. FreeEventList (pSInfo->lstEventInfo) ;
  386. FreeActionList (pSInfo->lstActionInfo) ;
  387. FreeTimeList (pSInfo->lstTimeInfo) ;
  388. delete pSInfo ;
  389. return iRet ;
  390. }