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.

270 lines
6.0 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: Routines to watch the Jobs directory and handle change
  7. // notifications.
  8. //
  9. // Contents:
  10. //
  11. // Classes:
  12. //
  13. // Functions:
  14. //
  15. // History: 2/15/1996 RaviR Created
  16. //
  17. //____________________________________________________________________________
  18. #include "..\pch\headers.hxx"
  19. #pragma hdrstop
  20. #include "dbg.h"
  21. #include "macros.h"
  22. #include "jobfldr.hxx"
  23. //#undef DEB_TRACE
  24. //#define DEB_TRACE DEB_USER1
  25. class CNotifyWatch;
  26. class CNotifyWatch
  27. {
  28. public:
  29. CNotifyWatch(CJobFolder * pjf)
  30. : m_hWatch(INVALID_HANDLE_VALUE), m_pJobFolder(pjf) {}
  31. ~CNotifyWatch()
  32. {
  33. if (m_hWatch != INVALID_HANDLE_VALUE)
  34. {
  35. FindCloseChangeNotification(m_hWatch);
  36. m_hWatch = INVALID_HANDLE_VALUE;
  37. }
  38. }
  39. HANDLE m_hWatch; // Returned from FindFirstChangeNotify.
  40. CJobFolder * m_pJobFolder;
  41. private:
  42. CNotifyWatch(void);
  43. };
  44. //------------------------------------------------------------------------
  45. // FUNCTION: NotifyWatchProc
  46. //
  47. //------------------------------------------------------------------------
  48. //____________________________________________________________________________
  49. //
  50. // Function: NotifyWatchProc
  51. //
  52. // Synopsis: Watch the jobs directory and notify CJobFolder when something
  53. // has changed.
  54. //
  55. // Arguments: [pNotify] -- IN
  56. //
  57. // Returns: DWORD
  58. //
  59. // History: 2/20/1996 RaviR Created
  60. //
  61. //____________________________________________________________________________
  62. DWORD
  63. NotifyWatchProc(
  64. CNotifyWatch * pNotify)
  65. {
  66. TRACE_FUNCTION(NotifyWatchProc);
  67. DWORD dwRet;
  68. BOOL bFileChange = FALSE;
  69. UINT cChanges = 0;
  70. if (pNotify == NULL)
  71. {
  72. return((DWORD) -1);
  73. }
  74. Sleep(7000);
  75. while (1)
  76. {
  77. //
  78. // Wait for the Jobs folder to change.
  79. //
  80. dwRet = WaitForSingleObject(pNotify->m_hWatch, 3000);
  81. switch( dwRet )
  82. {
  83. case WAIT_OBJECT_0:
  84. //
  85. // We could handle the change at this point, but we might
  86. // as well wait for a time out and do it all at once.
  87. // Doing nothing just causes us to wait
  88. // another 1.5 secs; i.e. reset the timeout.
  89. //
  90. bFileChange = TRUE;
  91. ++cChanges;
  92. if (FindNextChangeNotification(pNotify->m_hWatch) == FALSE)
  93. {
  94. CHECK_LASTERROR(GetLastError());
  95. }
  96. break;
  97. case WAIT_TIMEOUT:
  98. if (bFileChange == TRUE)
  99. {
  100. DEBUG_OUT((DEB_USER1, "Count of changes = %d\n", cChanges));
  101. pNotify->m_pJobFolder->CheckForChanges();
  102. bFileChange = FALSE;
  103. cChanges = 0;
  104. }
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. return 0;
  111. }
  112. //____________________________________________________________________________
  113. //
  114. // Member: CJobFolder::CheckForChanges
  115. //
  116. // Synopsis: S
  117. //
  118. // Returns: void
  119. //
  120. // History: 2/20/1996 RaviR Created
  121. //
  122. //____________________________________________________________________________
  123. void
  124. CJobFolder::CheckForChanges(void)
  125. {
  126. DEBUG_OUT((DEB_USER1, "CJobFolder::CheckForChanges <<--\n"));
  127. LRESULT lr = ERROR_SUCCESS;
  128. static TCHAR s_szSearchPath[MAX_PATH] = TEXT("");
  129. if (s_szSearchPath[0] == TEXT('\0'))
  130. {
  131. lstrcpy(s_szSearchPath, m_pszFolderPath);
  132. lstrcat(s_szSearchPath, TEXT("\\*") TSZ_DOTJOB);
  133. }
  134. WIN32_FIND_DATA fd;
  135. HANDLE hFind = FindFirstFile(s_szSearchPath, &fd);
  136. if (hFind == INVALID_HANDLE_VALUE)
  137. {
  138. if (GetLastError() != ERROR_FILE_NOT_FOUND)
  139. {
  140. CHECK_LASTERROR(GetLastError());
  141. }
  142. else if (GetLastError() != ERROR_NO_MORE_FILES)
  143. {
  144. if (m_pShellView != NULL)
  145. {
  146. m_pShellView->Refresh();
  147. }
  148. }
  149. }
  150. else
  151. {
  152. UINT cJobs = 0;
  153. BOOL fRefresh = FALSE;
  154. while (1)
  155. {
  156. ++cJobs;
  157. if (CompareFileTime(&fd.ftLastWriteTime, &m_ftLastChecked) > 0)
  158. {
  159. if (CompareFileTime(&fd.ftCreationTime, &m_ftLastChecked) > 0)
  160. {
  161. // this is a new job
  162. DEBUG_OUT((DEB_USER1, "-----------------------------\n"));
  163. DEBUG_OUT((DEB_USER1, "New File <%ws>\n", fd.cFileName));
  164. DEBUG_OUT((DEB_USER1, "-----------------------------\n"));
  165. }
  166. //
  167. // Job file changed since last check.
  168. //
  169. else if (_UpdateJob(fd.cFileName) == FALSE)
  170. {
  171. fRefresh = TRUE;
  172. break;
  173. }
  174. }
  175. //
  176. // Get the next file.
  177. //
  178. if (FindNextFile(hFind, &fd) == FALSE)
  179. {
  180. if (GetLastError() != ERROR_NO_MORE_FILES)
  181. {
  182. CHECK_LASTERROR(GetLastError());
  183. }
  184. break;
  185. }
  186. }
  187. FindClose(hFind);
  188. //
  189. // Refresh if either (fRefresh == TRUE) or
  190. // the count of objects has changed.
  191. //
  192. if (fRefresh == FALSE)
  193. {
  194. ULONG ulObjCount = ShellFolderView_GetObjectCount(m_hwndOwner);
  195. if (ulObjCount != cJobs)
  196. {
  197. fRefresh = TRUE;
  198. }
  199. }
  200. if (fRefresh == TRUE)
  201. {
  202. if (m_pShellView != NULL)
  203. {
  204. //m_pShellView->Refresh();
  205. }
  206. }
  207. //
  208. // Save the current time now that the UI is up to date.
  209. //
  210. SYSTEMTIME st;
  211. GetSystemTime(&st);
  212. SystemTimeToFileTime(&st, &m_ftLastChecked);
  213. }
  214. return;
  215. }