Quick.FileMonitor.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.FileMonitor
  4. Description : Watch for single file changes
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 11/09/2017
  8. Modified : 07/04/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.FileMonitor;
  22. interface
  23. {$i QuickLib.inc}
  24. uses
  25. Classes,
  26. {$IFDEF MSWINDOWS}
  27. Windows,
  28. {$ENDIF}
  29. SysUtils,
  30. {$IFDEF FPC}
  31. Quick.Files;
  32. {$ELSE}
  33. System.IOUtils;
  34. {$ENDIF}
  35. type
  36. TMonitorNotify = (mnNone, mnFileCreated, mnFileModified, mnFileDeleted);
  37. TMonitorWatch = set of TMonitorNotify;
  38. TFileChangeNotify = procedure(MonitorNofify : TMonitorNotify) of object;
  39. TQuickFileMonitor = class(TThread)
  40. private
  41. fFileName : string;
  42. fTickEvent : THandle;
  43. fInterval : Integer;
  44. fNotifies : TMonitorWatch;
  45. fEnabled : Boolean;
  46. fExists : Boolean;
  47. fModifedDate : TDateTime;
  48. fCurrentMonitorNotify : TMonitorNotify;
  49. fOnChangeNotify : TFileChangeNotify;
  50. procedure Execute; override;
  51. procedure SetStatus(Status : Boolean);
  52. procedure NotifyEvent;
  53. public
  54. constructor Create;
  55. destructor Destroy; override;
  56. property FileName : string read fFileName write fFileName;
  57. property Interval : Integer read fInterval write fInterval;
  58. property Notifies : TMonitorWatch read fNotifies write fNotifies;
  59. property OnFileChange : TFileChangeNotify read fOnChangeNotify write fOnChangeNotify;
  60. property Enabled : Boolean read fEnabled write SetStatus;
  61. end;
  62. implementation
  63. constructor TQuickFileMonitor.Create;
  64. begin
  65. inherited Create(True);
  66. Self.FreeOnTerminate := False;
  67. fInterval := 1000;
  68. fExists := False;
  69. fModifedDate := 0;
  70. fCurrentMonitorNotify := mnNone;
  71. fNotifies := [mnFileCreated,mnFileModified,mnFileDeleted];
  72. fTickEvent := CreateEvent(nil, True, False, nil);
  73. Self.Resume;
  74. end;
  75. destructor TQuickFileMonitor.Destroy;
  76. begin
  77. if not Terminated then Terminate;
  78. SetEvent(fTickEvent);
  79. CloseHandle(fTickEvent);
  80. inherited;
  81. end;
  82. procedure TQuickFileMonitor.Execute;
  83. var
  84. LastModifiedDate : TDateTime;
  85. begin
  86. inherited;
  87. while not Terminated do
  88. begin
  89. fCurrentMonitorNotify := mnNone;
  90. if WaitForSingleObject(fTickEvent,fInterval) = WAIT_TIMEOUT then
  91. begin
  92. if fEnabled then
  93. begin
  94. if TFile.Exists(fFileName) then
  95. begin
  96. if fExists then
  97. begin
  98. if mnFileModified in fNotifies then
  99. begin
  100. LastModifiedDate := TFile.GetLastWriteTime(fFileName);
  101. if LastModifiedDate > fModifedDate then
  102. begin
  103. fCurrentMonitorNotify := mnFileModified;
  104. fModifedDate := LastModifiedDate;
  105. end;
  106. end;
  107. end
  108. else
  109. begin
  110. if mnFileCreated in fNotifies then fCurrentMonitorNotify := mnFileCreated;
  111. LastModifiedDate := TFile.GetLastWriteTime(fFileName);
  112. fModifedDate := LastModifiedDate;
  113. end;
  114. fExists := True;
  115. end
  116. else
  117. begin
  118. //check if file deleted
  119. if mnFileDeleted in fNotifies then
  120. begin
  121. if fExists then
  122. begin
  123. fExists := False;
  124. fCurrentMonitorNotify := mnFileDeleted;
  125. end;
  126. end;
  127. end;
  128. if fCurrentMonitorNotify <> mnNone then
  129. begin
  130. if IsConsole then NotifyEvent
  131. else Synchronize(NotifyEvent);
  132. end;
  133. end;
  134. end;
  135. end;
  136. end;
  137. procedure TQuickFileMonitor.SetStatus(Status : Boolean);
  138. begin
  139. if fEnabled <> Status then
  140. begin
  141. fEnabled := Status;
  142. //gets current values
  143. if TFile.Exists(fFileName) then
  144. begin
  145. fExists := True;
  146. fModifedDate := TFile.GetLastWriteTime(fFileName);
  147. end;
  148. end;
  149. end;
  150. procedure TQuickFileMonitor.NotifyEvent;
  151. begin
  152. if Assigned(fOnChangeNotify) then fOnChangeNotify(fCurrentMonitorNotify);
  153. end;
  154. end.