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");
18 var m = new Monitor();
20 m.add("/home/alan/gitlive");
28 public class MonitorNamePathDir {
34 public MonitorNamePathDir(string name, string path, string dir)
43 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
47 * Monitor class - handles monitor managment for a large tree...
54 change : function () {
59 * x.stop() // stops all scanning.
60 * x.play(); // starts the scanning
67 public class Monitor : Object
76 this.monitors = new Array<FileMonitor> ();
77 this.top = new Array<string> ();
81 public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
82 public Array<string> top; // list of top level directories..
85 * add a directory or file to monitor
87 public void add (string add)
89 this.top.append_val(add);
96 for(int i = 0; i < this.monitors.length ; i++) {
97 this.monitor(this.top.index(i), ( fm, f_orig, of_orig, event_type) => {
98 this.onEvent (fm, f_orig, of_orig, event_type ) ;
103 * stop / pause monitoring
109 for(int i = 0; i < this.monitors.length ; i++) {
110 this.monitors.index(i).cancel();
112 this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
115 * pause monitoring - without changing what's monitored
122 * resume monitoring - without changing what's monitored
129 * monitor a file or directory (privatish)
131 * initially called with ~/gitlive null 0 (effectvely)
135 public void monitor(string path, onEventHander fn , int depth = 0)
138 // print("ADD: " + path)
140 //depth = typeof(depth) == 'number' ? depth *1 : 0;
141 depth = depth > 0 ? depth *1 : 0;
144 //fn = fn || function (fm, f, of, event_type, uh) {
145 // _this.onEvent(fm, f, of, event_type, uh);
149 var f = File.new_for_path(path);
150 //var cancel = new Gio.Cancellable ();
154 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
156 fm.changed.connect( ( fm, f_orig, of_orig, event_type) => {
158 fn (fm, f_orig, of_orig, event_type ) ;
161 //this.onEvent (fm, f_orig, of_orig, event_type ) ;
163 this.monitors.append_val(fm);
166 // FIXME -- show error? do nothing..
168 // print("ADD path " + depth + ' ' + path);
171 // - this is not used.
172 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
174 // this.initRepo(path);
176 FileEnumerator file_enum;
178 file_enum = f.enumerate_children(
179 FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE,
180 0, // FileQueryInfoFlags.NONE,
183 // FIXME - show error..
190 next_file = file_enum.next_file(null);
194 if (next_file == null) {
197 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
199 if (next_file.get_file_type() != FileType.DIRECTORY) {
204 if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
209 if (next_file.get_display_name()[0] == '.') {
213 var sp = path+"/"+next_file.get_display_name();
215 //print("got a file : " + sp);
221 this.monitor(sp, fn, depth + 1);
225 file_enum.close(null);
233 public File realpath(File file)
239 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
240 var rp = Posix.realpath(file.get_path());
241 return File.new_for_path(rp);
244 // file does not currently exist..
247 // FIX ME - string split?/?
248 var bn = file.get_basename();
249 var ar = file.get_path().split("/");
250 ar.resize(ar.length-1);
251 var dirname = string.joinv("/",ar );
252 var rp = Posix.realpath(dirname);
253 return File.new_for_path(rp + "/" + bn);
260 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
266 var f = this.realpath(f_orig);
268 var of = this.realpath(of_orig);
272 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
273 MonitorNamePathDir dest = null;
276 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
279 //string event_name = "UKNOWN";
282 // extract the event names ... - not sure if introspection is feasible in vala..
283 //for(var i in Gio.FileMonitorEvent) {
284 // if (Gio.FileMonitorEvent[i] == event_type) {
292 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
293 //print ("got src: " + src.toString());
294 //print ("got event: " + src.toString());
298 case FileMonitorEvent.CHANGED:
300 return; // ingore thise?? -wait for changes_done_htin?
302 case FileMonitorEvent.CHANGES_DONE_HINT:
303 this.onChangesDoneHint(src);
306 case FileMonitorEvent.DELETED:
310 case FileMonitorEvent.CREATED:
314 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
315 this.onAttributeChanged(src);
318 case FileMonitorEvent.MOVED: // eg. chmod/chatt
319 this.onMoved(src,dest);
322 // rest are mount related - not really relivant.. maybe add later..
330 /** override these to do stuff.. */
331 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
332 public void onChanged(MonitorNamePathDir src) { }
333 public void onChangesDoneHint(MonitorNamePathDir src) { }
334 public void onDeleted(MonitorNamePathDir src) { }
335 public void onCreated(MonitorNamePathDir src) { }
336 public void onAttributeChanged(MonitorNamePathDir src) { }
337 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }