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");
16 var m = new Monitor();
22 public class MonitorNamePathDir {
28 public MonitorNamePathDir(string name, string path, string dir)
37 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
41 * Monitor class - handles monitor managment for a large tree...
48 change : function () {
53 * x.stop() // stops all scanning.
54 * x.play(); // starts the scanning
61 public class Monitor : Object
70 this.monitors = new Array<FileMonitor> ();
71 this.top = new Array<string> ();
75 public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
76 public Array<string> top; // list of top level directories..
79 * add a directory or file to monitor
81 public void add (string add)
83 this.top.append_val(add);
90 for(int i = 0; i < this.monitors.length ; i++) {
91 this.monitor(this.top.index(i), ( fm, f_orig, of_orig, event_type) => {
92 this.onEvent (fm, f_orig, of_orig, event_type ) ;
97 * stop / pause monitoring
103 for(int i = 0; i < this.monitors.length ; i++) {
104 this.monitors.index(i).cancel();
106 this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
109 * pause monitoring - without changing what's monitored
116 * resume monitoring - without changing what's monitored
123 * monitor a file or directory (privatish)
125 * initially called with ~/gitlive null 0 (effectvely)
129 public void monitor(string path, onEventHander fn , int depth = 0)
132 // print("ADD: " + path)
134 //depth = typeof(depth) == 'number' ? depth *1 : 0;
135 depth = depth > 0 ? depth *1 : 0;
138 //fn = fn || function (fm, f, of, event_type, uh) {
139 // _this.onEvent(fm, f, of, event_type, uh);
143 var f = File.new_for_path(path);
144 //var cancel = new Gio.Cancellable ();
148 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
150 fm.changed.connect( ( fm, f_orig, of_orig, event_type) => {
152 fn (fm, f_orig, of_orig, event_type ) ;
155 //this.onEvent (fm, f_orig, of_orig, event_type ) ;
157 this.monitors.append_val(fm);
160 // FIXME -- show error? do nothing..
162 // print("ADD path " + depth + ' ' + path);
165 // - this is not used.
166 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
168 // this.initRepo(path);
170 FileEnumerator file_enum;
172 file_enum = f.enumerate_children(
173 FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE,
174 0, // FileQueryInfoFlags.NONE,
177 // FIXME - show error..
184 next_file = file_enum.next_file(null);
188 if (next_file == null) {
191 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
193 if (next_file.get_file_type() != FileType.DIRECTORY) {
198 if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
203 if (next_file.get_display_name()[0] == '.') {
207 var sp = path+"/"+next_file.get_display_name();
209 //print("got a file : " + sp);
215 this.monitor(sp, fn, depth + 1);
219 file_enum.close(null);
227 public File realpath(File file)
233 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
234 var rp = Posix.realpath(file.get_path());
235 return File.new_for_path(rp);
238 // file does not currently exist..
241 // FIX ME - string split?/?
242 var bn = file.get_basename();
243 var ar = file.get_path().split("/");
244 ar.resize(ar.length-1);
245 var dirname = string.joinv("/",ar );
246 var rp = Posix.realpath(dirname);
247 return File.new_for_path(rp + "/" + bn);
254 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
260 var f = this.realpath(f_orig);
262 var of = this.realpath(of_orig);
266 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
267 MonitorNamePathDir dest = null;
270 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
273 //string event_name = "UKNOWN";
276 // extract the event names ... - not sure if introspection is feasible in vala..
277 //for(var i in Gio.FileMonitorEvent) {
278 // if (Gio.FileMonitorEvent[i] == event_type) {
283 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
284 //print ("got src: " + src.toString());
285 //print ("got event: " + src.toString());
289 case FileMonitorEvent.CHANGED:
291 return; // ingore thise?? -wait for changes_done_htin?
293 case FileMonitorEvent.CHANGES_DONE_HINT:
294 this.onChangesDoneHint(src);
297 case FileMonitorEvent.DELETED:
301 case FileMonitorEvent.CREATED:
305 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
306 this.onAttributeChanged(src);
309 case FileMonitorEvent.MOVED: // eg. chmod/chatt
310 this.onMoved(src,dest);
313 // rest are mount related - not really relivant.. maybe add later..
321 /** override these to do stuff.. */
322 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
323 public void onChanged(MonitorNamePathDir src) { }
324 public void onChangesDoneHint(MonitorNamePathDir src) { }
325 public void onDeleted(MonitorNamePathDir src) { }
326 public void onCreated(MonitorNamePathDir src) { }
327 public void onAttributeChanged(MonitorNamePathDir src) { }
328 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }