FileMonitor.as 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright (c) 2009, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.air.filesystem
  28. {
  29. import flash.filesystem.File;
  30. import flash.utils.Timer;
  31. import flash.events.TimerEvent;
  32. import flash.events.Event;
  33. import flash.events.EventDispatcher;
  34. import com.adobe.air.filesystem.events.FileMonitorEvent;
  35. /*
  36. Todo:
  37. -Cmonitor changes in multiple attributes
  38. -add support for monitoring multiple files
  39. */
  40. /**
  41. * Dispatched when the modified date of the file being modified changes.
  42. *
  43. * @eventType com.adobe.air.filesystem.events.FileMonitor.CHANGE
  44. */
  45. [Event(name="CHANGE", type="com.adobe.air.filesystem.events.FileMonitor")]
  46. /**
  47. * Dispatched when the file being monitored is moved or deleted. The file
  48. * will be unwatched.
  49. *
  50. * @eventType com.adobe.air.filesystem.events.FileMonitor.MOVE
  51. */
  52. [Event(name="MOVE", type="com.adobe.air.filesystem.events.FileMonitor")]
  53. /**
  54. * Dispatched when the file being monitored is created.
  55. *
  56. * @eventType com.adobe.air.filesystem.events.FileMonitor.CREATE
  57. */
  58. [Event(name="CREATE", type="com.adobe.air.filesystem.events.FileMonitor")]
  59. /**
  60. * Class that monitors files for changes.
  61. */
  62. public class FileMonitor extends EventDispatcher
  63. {
  64. private var _file:File;
  65. private var timer:Timer;
  66. public static const DEFAULT_MONITOR_INTERVAL:Number = 1000;
  67. private var _interval:Number;
  68. private var fileExists:Boolean = false;
  69. private var lastModifiedTime:Number;
  70. /**
  71. * Constructor
  72. *
  73. * @parameter file The File that will be monitored for changes.
  74. *
  75. * @param interval How often in milliseconds the file is polled for
  76. * change events. Default value is 1000, minimum value is 1000
  77. */
  78. public function FileMonitor(file:File = null, interval:Number = -1)
  79. {
  80. this.file = file;
  81. if(interval != -1)
  82. {
  83. if(interval < 1000)
  84. {
  85. _interval = 1000;
  86. }
  87. else
  88. {
  89. _interval = interval;
  90. }
  91. }
  92. else
  93. {
  94. _interval = DEFAULT_MONITOR_INTERVAL;
  95. }
  96. }
  97. /**
  98. * File being monitored for changes.
  99. *
  100. * Setting the property will result in unwatch() being called.
  101. */
  102. public function get file():File
  103. {
  104. return _file;
  105. }
  106. public function set file(file:File):void
  107. {
  108. if(timer && timer.running)
  109. {
  110. unwatch();
  111. }
  112. _file = file;
  113. if(!_file)
  114. {
  115. fileExists = false;
  116. return;
  117. }
  118. //note : this will throw an error if new File() is passed in.
  119. fileExists = _file.exists;
  120. if(fileExists)
  121. {
  122. lastModifiedTime = _file.modificationDate.getTime();
  123. }
  124. }
  125. /**
  126. * How often the system is polled for Volume change events.
  127. */
  128. public function get interval():Number
  129. {
  130. return _interval;
  131. }
  132. /**
  133. * Begins monitoring the specified file for changes.
  134. *
  135. * Broadcasts Event.CHANGE event when the file's modification date has changed.
  136. */
  137. public function watch():void
  138. {
  139. if(!file)
  140. {
  141. //should we throw an error?
  142. return;
  143. }
  144. if(timer && timer.running)
  145. {
  146. return;
  147. }
  148. //check and see if timer is active. if it is, return
  149. if(!timer)
  150. {
  151. timer = new Timer(_interval);
  152. timer.addEventListener(TimerEvent.TIMER, onTimerEvent, false, 0, true);
  153. }
  154. timer.start();
  155. }
  156. /**
  157. * Stops watching the specified file for changes.
  158. */
  159. public function unwatch():void
  160. {
  161. if(!timer)
  162. {
  163. return;
  164. }
  165. timer.stop();
  166. timer.removeEventListener(TimerEvent.TIMER, onTimerEvent);
  167. }
  168. private function onTimerEvent(e:TimerEvent):void
  169. {
  170. var outEvent:FileMonitorEvent;
  171. if(fileExists != _file.exists)
  172. {
  173. if(_file.exists)
  174. {
  175. //file was created
  176. outEvent = new FileMonitorEvent(FileMonitorEvent.CREATE);
  177. lastModifiedTime = _file.modificationDate.getTime();
  178. }
  179. else
  180. {
  181. //file was moved / deleted
  182. outEvent = new FileMonitorEvent(FileMonitorEvent.MOVE);
  183. unwatch();
  184. }
  185. fileExists = _file.exists;
  186. }
  187. else
  188. {
  189. if(!_file.exists)
  190. {
  191. return;
  192. }
  193. var modifiedTime:Number = _file.modificationDate.getTime();
  194. if(modifiedTime == lastModifiedTime)
  195. {
  196. return;
  197. }
  198. lastModifiedTime = modifiedTime;
  199. //file modified
  200. outEvent = new FileMonitorEvent(FileMonitorEvent.CHANGE);
  201. }
  202. if(outEvent)
  203. {
  204. outEvent.file = _file;
  205. dispatchEvent(outEvent);
  206. }
  207. }
  208. }
  209. }