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 gio-2.0 --pkg posix Monitor.vala -o /tmp/Monitor
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");
16 MainLoop loop = new MainLoop ();
18 var m = new Monitor();
20 m.add("/home/alan/gitlive");
29 public class MonitorNamePathDir {
35 public MonitorNamePathDir(string name, string path, string dir)
46 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
50 * Monitor class - handles monitor managment for a large tree...
57 change : function () {
62 * x.stop() // stops all scanning.
63 * x.play(); // starts the scanning
70 public class Monitor : Object
79 this.monitors = new Array<FileMonitor> ();
80 this.top = new Array<string> ();
84 public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
85 public Array<string> top; // list of top level directories..
88 * add a directory or file to monitor
90 public void add (string add)
92 this.top.append_val(add);
99 for(int i = 0; i < this.top.length ; i++) {
100 this.monitor(this.top.index(i), ( fm, f_orig, of_orig, event_type) => {
101 this.onEvent (fm, f_orig, of_orig, event_type ) ;
106 * stop / pause monitoring
112 for(int i = 0; i < this.monitors.length ; i++) {
113 this.monitors.index(i).cancel();
115 this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
118 * pause monitoring - without changing what's monitored
125 * resume monitoring - without changing what's monitored
132 * monitor a file or directory (privatish)
134 * initially called with ~/gitlive null 0 (effectvely)
138 public void monitor(string path, onEventHander fn , int depth = 0)
141 print("ADD: " + path + "\n");
143 //depth = typeof(depth) == 'number' ? depth *1 : 0;
144 depth = depth > 0 ? depth *1 : 0;
147 //fn = fn || function (fm, f, of, event_type, uh) {
148 // _this.onEvent(fm, f, of, event_type, uh);
152 var f = File.new_for_path(path);
153 //var cancel = new Gio.Cancellable ();
158 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
160 fm.changed.connect( ( fm, f_orig, of_orig, event_type) => {
162 fn (fm, f_orig, of_orig, event_type ) ;
165 //this.onEvent (fm, f_orig, of_orig, event_type ) ;
167 this.monitors.append_val(fm);
170 // FIXME -- show error? do nothing..
172 // print("ADD path " + depth + ' ' + path);
175 // - this is not used.
176 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
178 // this.initRepo(path);
180 FileEnumerator file_enum;
182 file_enum = f.enumerate_children(
183 FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE,
184 0, // FileQueryInfoFlags.NONE,
187 // FIXME - show error..
194 next_file = file_enum.next_file(null);
198 if (next_file == null) {
201 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
203 if (next_file.get_file_type() != FileType.DIRECTORY) {
208 if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
213 if (next_file.get_display_name()[0] == '.') {
217 var sp = path+"/"+next_file.get_display_name();
219 //print("got a file : " + sp);
225 this.monitor(sp, fn, depth + 1);
229 file_enum.close(null);
237 public File realpath(File file)
243 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
244 var rp = Posix.realpath(file.get_path());
245 return File.new_for_path(rp);
248 // file does not currently exist..
251 // FIX ME - string split?/?
252 var bn = file.get_basename();
253 var ar = file.get_path().split("/");
254 ar.resize(ar.length-1);
255 var dirname = string.joinv("/",ar );
256 var rp = Posix.realpath(dirname);
257 return File.new_for_path(rp + "/" + bn);
264 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
270 var f = this.realpath(f_orig);
272 var of = this.realpath(of_orig);
276 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
277 MonitorNamePathDir dest = null;
280 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
283 //string event_name = "UKNOWN";
286 // extract the event names ... - not sure if introspection is feasible in vala..
287 //for(var i in Gio.FileMonitorEvent) {
288 // if (Gio.FileMonitorEvent[i] == event_type) {
296 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
297 //print ("got src: " + src.toString());
298 //print ("got event: " + src.toString());
302 case FileMonitorEvent.CHANGED:
304 return; // ingore thise?? -wait for changes_done_htin?
306 case FileMonitorEvent.CHANGES_DONE_HINT:
307 this.onChangesDoneHint(src);
310 case FileMonitorEvent.DELETED:
314 case FileMonitorEvent.CREATED:
318 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
319 this.onAttributeChanged(src);
322 case FileMonitorEvent.MOVED: // eg. chmod/chatt
323 this.onMoved(src,dest);
326 // rest are mount related - not really relivant.. maybe add later..
334 /** override these to do stuff.. */
335 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
336 public void onChanged(MonitorNamePathDir src) { }
337 public void onChangesDoneHint(MonitorNamePathDir src) { }
338 public void onDeleted(MonitorNamePathDir src) { }
339 public void onCreated(MonitorNamePathDir src) { }
340 public void onAttributeChanged(MonitorNamePathDir src) { }
341 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }