1 //<Script type="text/javascript">
2 //var Gio = imports.gi.Gio;
3 //var GLib = imports.gi.GLib;
5 //var XObject = imports.XObject.XObject;
6 //var File = imports.File.File;
8 /// # valac --pkg gee-0.8 --pkg gio-2.0 --pkg posix Monitor.val
11 using Gee; // for array list?
13 static int main (string[] args) {
14 // A reference to our file
15 var file = File.new_for_path ("data.txt");
21 public class MonitorNamePathDir {
27 public MonitorNamePathDir(string name, string path, string dir)
36 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
40 * Monitor class - handles monitor managment for a large tree...
47 change : function () {
52 * x.stop() // stops all scanning.
53 * x.play(); // starts the scanning
60 public class Monitor : Object
69 this.monitors = new ArrayList<FileMonitor> ();
70 this.top = new ArrayList<string> ();
74 public ArrayList<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
75 public ArrayList<string> top; // list of top level directories..
78 * add a directory or file to monitor
80 public void add (string add)
89 for(int i = 0; i < this.monitors.size ; i++) {
90 this.monitor(this.top[i], ( fm, f_orig, of_orig, event_type) => {
91 this.onEvent (fm, f_orig, of_orig, event_type ) ;
96 * stop / pause monitoring
102 for(int i = 0; i < this.monitors.size ; i++) {
103 this.monitors[i].cancel();
105 this.monitors = new ArrayList<FileMonitor>(); // clean /destroy/ kill old?
108 * pause monitoring - without changing what's monitored
115 * resume monitoring - without changing what's monitored
122 * monitor a file or directory (privatish)
124 * initially called with ~/gitlive null 0 (effectvely)
128 public void monitor(string path, onEventHander fn , int depth = 0)
131 // print("ADD: " + path)
133 //depth = typeof(depth) == 'number' ? depth *1 : 0;
134 depth = depth > 0 ? depth *1 : 0;
137 //fn = fn || function (fm, f, of, event_type, uh) {
138 // _this.onEvent(fm, f, of, event_type, uh);
142 var f = File.new_for_path(path);
143 //var cancel = new Gio.Cancellable ();
145 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
147 fm.changed.connect( ( fm, f_orig, of_orig, event_type) => {
149 fn (fm, f_orig, of_orig, event_type ) ;
152 //this.onEvent (fm, f_orig, of_orig, event_type ) ;
154 this.monitors.add(fm);
155 // print("ADD path " + depth + ' ' + path);
158 // - this is not used.
159 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
161 // this.initRepo(path);
165 var file_enum = f.enumerate_children(
166 FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE,
167 0, // FileQueryInfoFlags.NONE,
172 while ((next_file = file_enum.next_file(null)) != null) {
174 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
176 if (next_file.get_file_type() != FileType.DIRECTORY) {
181 if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
186 if (next_file.get_display_name()[0] == '.') {
190 var sp = path+"/"+next_file.get_display_name();
192 //print("got a file : " + sp);
198 this.monitor(sp, fn, depth + 1);
202 file_enum.close(null);
207 public File realpath(File file)
213 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
214 var rp = Posix.realpath(file.get_path());
215 return File.new_for_path(rp);
218 // file does not currently exist..
221 // FIX ME - string split?/?
222 var bn = file.get_basename();
223 var ar = file.get_path().split("/");
224 ar.resize(ar.length-1);
225 var dirname = string.joinv("/",ar );
226 var rp = Posix.realpath(dirname);
227 return File.new_for_path(rp + "/" + bn);
234 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
240 var f = this.realpath(f_orig);
242 var of = this.realpath(of_orig);
246 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
247 MonitorNamePathDir dest = null;
250 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
253 //string event_name = "UKNOWN";
256 // extract the event names ... - not sure if introspection is feasible in vala..
257 //for(var i in Gio.FileMonitorEvent) {
258 // if (Gio.FileMonitorEvent[i] == event_type) {
263 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
264 //print ("got src: " + src.toString());
265 //print ("got event: " + src.toString());
269 case FileMonitorEvent.CHANGED:
271 return; // ingore thise?? -wait for changes_done_htin?
273 case FileMonitorEvent.CHANGES_DONE_HINT:
274 this.onChangesDoneHint(src);
277 case FileMonitorEvent.DELETED:
281 case FileMonitorEvent.CREATED:
285 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
286 this.onAttributeChanged(src);
289 case FileMonitorEvent.MOVED: // eg. chmod/chatt
290 this.onMoved(src,dest);
293 // rest are mount related - not really relivant.. maybe add later..
301 /** override these to do stuff.. */
302 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
303 public void onChanged(MonitorNamePathDir src) { }
304 public void onChangesDoneHint(MonitorNamePathDir src) { }
305 public void onDeleted(MonitorNamePathDir src) { }
306 public void onCreated(MonitorNamePathDir src) { }
307 public void onAttributeChanged(MonitorNamePathDir src) { }
308 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }