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)
47 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
51 * Monitor class - handles monitor managment for a large tree...
58 change : function () {
63 * x.stop() // stops all scanning.
64 * x.play(); // starts the scanning
71 public class Monitor : Object
80 this.monitors = new Array<FileMonitor> ();
81 this.top = new Array<string> ();
85 public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
86 public Array<string> top; // list of top level directories..
89 * add a directory or file to monitor
91 public void add (string add)
93 this.top.append_val(add);
100 for(int i = 0; i < this.top.length ; i++) {
101 this.monitor(this.top.index(i));
105 * stop / pause monitoring
111 for(int i = 0; i < this.monitors.length ; i++) {
112 this.monitors.index(i).cancel();
114 this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
117 * pause monitoring - without changing what's monitored
124 * resume monitoring - without changing what's monitored
131 * monitor a file or directory (privatish)
133 * initially called with ~/gitlive null 0 (effectvely)
137 public void monitor(string path, int depth = 0)
140 print("ADD: " + path + "\n");
142 //depth = typeof(depth) == 'number' ? depth *1 : 0;
143 depth = depth > 0 ? depth *1 : 0;
146 //fn = fn || function (fm, f, of, event_type, uh) {
147 // _this.onEvent(fm, f, of, event_type, uh);
151 var f = File.new_for_path(path);
152 //var cancel = new Gio.Cancellable ();
157 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
159 fm.changed.connect( ( fm, f_orig, of_orig, event_type) => {
161 this.onEvent (fm, f_orig, of_orig, event_type ) ;
164 //this.onEvent (fm, f_orig, of_orig, event_type ) ;
166 this.monitors.append_val(fm);
169 // FIXME -- show error? do nothing..
171 // print("ADD path " + depth + ' ' + path);
174 // - this is not used.
175 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
177 // this.initRepo(path);
179 FileEnumerator file_enum;
181 file_enum = f.enumerate_children(
182 FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE,
183 0, // FileQueryInfoFlags.NONE,
186 // FIXME - show error..
193 next_file = file_enum.next_file(null);
197 if (next_file == null) {
200 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
202 if (next_file.get_file_type() != FileType.DIRECTORY) {
207 if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
212 if (next_file.get_display_name()[0] == '.') {
216 var sp = path+"/"+next_file.get_display_name();
218 //print("got a file : " + sp);
224 this.monitor(sp, depth + 1);
228 file_enum.close(null);
236 public File realpath(File file)
242 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
243 var rp = Posix.realpath(file.get_path());
244 return File.new_for_path(rp);
247 // file does not currently exist..
250 // FIX ME - string split?/?
251 var bn = file.get_basename();
252 var ar = file.get_path().split("/");
253 ar.resize(ar.length-1);
254 var dirname = string.joinv("/",ar );
255 var rp = Posix.realpath(dirname);
256 return File.new_for_path(rp + "/" + bn);
263 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
269 var f = this.realpath(f_orig);
271 var of = this.realpath(of_orig);
275 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
276 MonitorNamePathDir dest = null;
279 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
282 //string event_name = "UKNOWN";
285 // extract the event names ... - not sure if introspection is feasible in vala..
286 //for(var i in Gio.FileMonitorEvent) {
287 // if (Gio.FileMonitorEvent[i] == event_type) {
295 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
296 //print ("got src: " + src.toString());
297 //print ("got event: " + src.toString());
301 case FileMonitorEvent.CHANGED:
302 src.action = "changed";
304 return; // ingore thise?? -wait for changes_done_htin?
306 case FileMonitorEvent.CHANGES_DONE_HINT:
307 src.action = "changed";
308 this.onChangesDoneHint(src);
311 case FileMonitorEvent.DELETED:
316 case FileMonitorEvent.CREATED:
317 src.action = "created";
321 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
322 src.action = "attrib";
323 this.onAttributeChanged(src);
326 case FileMonitorEvent.MOVED: // eg. chmod/chatt
327 src.action = "moved";
328 dest.action = "moved";
329 this.onMoved(src,dest);
332 // rest are mount related - not really relivant.. maybe add later..
340 /** override these to do stuff.. */
341 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
342 public void onChanged(MonitorNamePathDir src) { }
343 public void onChangesDoneHint(MonitorNamePathDir src) { }
344 public void onDeleted(MonitorNamePathDir src) { }
345 public void onCreated(MonitorNamePathDir src) { }
346 public void onAttributeChanged(MonitorNamePathDir src) { }
347 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }