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.

223 lines
5.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. /* File: history.cpp
  3. Description: To prevent the watchdog from excessive repeate notifications,
  4. the system administrator can set a minimum period for notification
  5. silence. This class (CHistory) manages the reading of this setting
  6. along with remembering the last time an action occured. A client
  7. of the object is able to ask it if a given action should be performed
  8. on the basis of past actions.
  9. CHistory
  10. Revision History:
  11. Date Description Programmer
  12. -------- --------------------------------------------------- ----------
  13. 07/01/97 Initial creation. BrianAu
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <precomp.hxx>
  17. #pragma hdrstop
  18. #include "history.h"
  19. #include "policy.h"
  20. //
  21. // Defined in dskquowd.cpp
  22. //
  23. extern TCHAR g_szRegSubKeyUser[];
  24. extern TCHAR g_szRegSubKeyAdmin[];
  25. //
  26. // History-related registry parameter value names.
  27. //
  28. const TCHAR CHistory::SZ_REG_LAST_NOTIFY_POPUP_TIME[] = TEXT("LastNotifyPopupTime");
  29. const TCHAR CHistory::SZ_REG_LAST_NOTIFY_EMAIL_TIME[] = TEXT("LastNotifyEmailTime");
  30. CHistory::CHistory(
  31. CPolicy& policy
  32. ) : m_policy(policy)
  33. {
  34. //
  35. // Build our registry parameter table. This will create
  36. // any keys (using the defaults provided) that don't already exist.
  37. //
  38. FILETIME ft = {0, 0};
  39. m_RegParams.AddBinaryParam(HKEY_CURRENT_USER,
  40. g_szRegSubKeyAdmin,
  41. SZ_REG_LAST_NOTIFY_POPUP_TIME,
  42. (LPBYTE)&ft,
  43. sizeof(ft));
  44. m_RegParams.AddBinaryParam(HKEY_CURRENT_USER,
  45. g_szRegSubKeyAdmin,
  46. SZ_REG_LAST_NOTIFY_EMAIL_TIME,
  47. (LPBYTE)&ft,
  48. sizeof(ft));
  49. }
  50. //
  51. // Compare the current time with the last time a popup was displayed
  52. // and the minimum popup period stored in the registry.
  53. // If the elapsed time since the last popup exceeds the minimum period
  54. // value stored in the registry, return TRUE.
  55. //
  56. BOOL
  57. CHistory::ShouldPopupDialog(
  58. VOID
  59. )
  60. {
  61. FILETIME ftLastPopup;
  62. FILETIME ftNow;
  63. DWORD dwMinPeriod = 0;
  64. GetSysTime(&ftNow);
  65. m_RegParams.GetParameter(HKEY_CURRENT_USER,
  66. g_szRegSubKeyAdmin,
  67. SZ_REG_LAST_NOTIFY_POPUP_TIME,
  68. (LPBYTE)&ftLastPopup,
  69. sizeof(ftLastPopup));
  70. //
  71. // Get the minimum popup period from the policy object.
  72. //
  73. dwMinPeriod = (DWORD)m_policy.GetMinNotifyPopupDialogPeriod();
  74. return (INT)dwMinPeriod <= CalcDiffMinutes(ftNow, ftLastPopup);
  75. }
  76. //
  77. // Compare the current time with the last time email was sent
  78. // and the minimum email period stored in the registry.
  79. // If the elapsed time since the last email exceeds the minimum period
  80. // value stored in the registry, return TRUE.
  81. //
  82. BOOL
  83. CHistory::ShouldSendEmail(
  84. VOID
  85. )
  86. {
  87. FILETIME ftLastEmail;
  88. FILETIME ftNow;
  89. DWORD dwMinPeriod = 0;
  90. GetSysTime(&ftNow);
  91. m_RegParams.GetParameter(HKEY_CURRENT_USER,
  92. g_szRegSubKeyAdmin,
  93. SZ_REG_LAST_NOTIFY_EMAIL_TIME,
  94. (LPBYTE)&ftLastEmail,
  95. sizeof(ftLastEmail));
  96. //
  97. // Get the minimum popup period from the policy object.
  98. //
  99. dwMinPeriod = (DWORD)m_policy.GetMinNotifyEmailPeriod();
  100. return (INT)dwMinPeriod <= CalcDiffMinutes(ftNow, ftLastEmail);
  101. }
  102. //
  103. // Should we do ANY notifications based on history information?
  104. //
  105. BOOL
  106. CHistory::ShouldDoAnyNotifications(
  107. VOID
  108. )
  109. {
  110. return ShouldSendEmail() ||
  111. ShouldPopupDialog();
  112. }
  113. //
  114. // Save the current system time in the registry as the "last time"
  115. // a popup was displayed.
  116. //
  117. VOID
  118. CHistory::RecordDialogPoppedUp(
  119. VOID
  120. )
  121. {
  122. FILETIME ftNow;
  123. GetSysTime(&ftNow);
  124. m_RegParams.SetParameter(HKEY_CURRENT_USER,
  125. g_szRegSubKeyAdmin,
  126. SZ_REG_LAST_NOTIFY_POPUP_TIME,
  127. (LPBYTE)&ftNow,
  128. sizeof(ftNow));
  129. }
  130. //
  131. // Save the current system time in the registry as the "last time"
  132. // an email message was sent.
  133. //
  134. VOID
  135. CHistory::RecordEmailSent(
  136. VOID
  137. )
  138. {
  139. FILETIME ftNow;
  140. GetSysTime(&ftNow);
  141. m_RegParams.SetParameter(HKEY_CURRENT_USER,
  142. g_szRegSubKeyAdmin,
  143. SZ_REG_LAST_NOTIFY_EMAIL_TIME,
  144. (LPBYTE)&ftNow,
  145. sizeof(ftNow));
  146. }
  147. //
  148. // Get the system time as a FILETIME struct.
  149. //
  150. VOID
  151. CHistory::GetSysTime(
  152. LPFILETIME pftOut
  153. )
  154. {
  155. SYSTEMTIME st;
  156. GetSystemTime(&st);
  157. SystemTimeToFileTime(&st, pftOut);
  158. }
  159. //
  160. // Calculate the difference between two FILETIME structures and
  161. // return the difference converted to minutes.
  162. //
  163. INT
  164. CHistory::CalcDiffMinutes(
  165. const FILETIME& ftA,
  166. const FILETIME& ftB
  167. )
  168. {
  169. LARGE_INTEGER liMinA;
  170. LARGE_INTEGER liMinB;
  171. LARGE_INTEGER liDiff;
  172. liMinA.LowPart = ftA.dwLowDateTime;
  173. liMinA.HighPart = ftA.dwHighDateTime;
  174. liMinB.LowPart = ftB.dwLowDateTime;
  175. liMinB.HighPart = ftB.dwHighDateTime;
  176. liDiff.QuadPart = liMinA.QuadPart - liMinB.QuadPart;
  177. return (INT)(liDiff.QuadPart / ((__int64)600000000L));
  178. }