MergeBranch.bjs
[gitlive] / Monitor.vala
index 06ef34a..b8e3bce 100644 (file)
@@ -5,22 +5,29 @@
 //var XObject = imports.XObject.XObject;
 //var File    = imports.File.File;
 
-/// # valac --pkg gee-0.8 --pkg gio-2.0  --pkg posix Monitor.val
+/// # valac  --pkg gio-2.0  --pkg posix Monitor.vala -o /tmp/Monitor
 
  
 //using Gee; // for array list?
-
+/*
 static int main (string[] args) {
     // A reference to our file
     //var file = File.new_for_path ("data.txt");
-    //var m = new Monitor();
+    MainLoop loop = new MainLoop ();
+    print("starting");
+    var m = new Monitor();
+    
+    m.add("/home/alan/gitlive");
+    m.start();
+    loop.run ();
+
     return 0;
 
 }
-
+*/
 
 public class  MonitorNamePathDir {
-    
+    public string action;
     public string name;
     public string path;
     public string dir;
@@ -30,10 +37,13 @@ public class  MonitorNamePathDir {
         this.name = name;
         this.path = path;
         this.dir = dir;
+        this.action = "?";
         
     }
 }
 
+
+
 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
 
 
@@ -58,7 +68,12 @@ public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, F
  * 
  */
  
-public class Monitor : Object
+public abstract class gitMonitorBase : Object
+{
+       public abstract void monitor(string path, int depth = 0);
+} 
+public class Monitor : gitMonitorBase
 {
 
 
@@ -80,6 +95,8 @@ public class Monitor : Object
      */
     public void add (string add)
     {
+        
+        print("Monitor.add: " + add);
         this.top.append_val(add);
     }
     /**
@@ -87,10 +104,8 @@ public class Monitor : Object
      */
     public void start()
     {
-        for(int i = 0; i < this.monitors.size ; i++) {
-            this.monitor(this.top[i], ( fm,  f_orig,  of_orig,  event_type) => {
-                this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
-                } );
+        for(int i = 0; i < this.top.length ; i++) {
+            this.monitor(this.top.index(i));
         }
     }
     /**
@@ -100,8 +115,8 @@ public class Monitor : Object
     public void stop()
     {
         
-        for(int i = 0; i < this.monitors.size ; i++) {
-            this.monitors[i].cancel();
+        for(int i = 0; i < this.monitors.length ; i++) {
+            this.monitors.index(i).cancel();
         } 
         this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
     }
@@ -126,10 +141,10 @@ public class Monitor : Object
      * 
      * 
      */
-    public void monitor(string path, onEventHander fn , int depth = 0)
+    public override void monitor(string path, int depth = 0)
     {
          
-       // print("ADD: " + path)
+        //GLib.debug("ADD: (%d): %s\n", depth, path);
         
         //depth = typeof(depth) == 'number'  ? depth *1 : 0;
         depth = depth > 0  ? depth *1 : 0;
@@ -138,25 +153,21 @@ public class Monitor : Object
         //fn = fn || function (fm, f, of, event_type, uh) {
         //    _this.onEvent(fm, f, of, event_type, uh);
         //}
-       
-          
+                 
         var f = File.new_for_path(path);
             //var cancel = new Gio.Cancellable ();
-        if (depth > 0) {   
-            try {
+        if (depth > 0) { 
   
-                 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
+            try {
+                 var fm = f.monitor(FileMonitorFlags.SEND_MOVED + FileMonitorFlags.WATCH_MOVES,null);  
+                 //var fm = f.monitor(FileMonitorFlags.WATCH_MOVES,null);
  
-                 fm.changed.connect( ( fm,  f_orig,  of_orig,  event_type) => {
-                        //if (fn) {
-                            fn (fm,  f_orig,  of_orig,  event_type ) ;
-                           // return;
-                        //}
-                        //this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
-                });
+                 fm.changed.connect( this.onEvent );
                 this.monitors.append_val(fm);
 
             } catch (Error e) {
+                  GLib.debug("Error adding monitor: %s\n", e.message);
+                  GLib.debug("Try: \n\nsudo su\necho 512 > /proc/sys/fs/inotify/max_user_instances\n");     
                 // FIXME -- show error? do nothing..            
             }
             // print("ADD path " + depth + ' ' + path);
@@ -168,34 +179,43 @@ public class Monitor : Object
         //    this.initRepo(path);
         //}
         FileEnumerator file_enum;
+        var cancellable = new Cancellable ();
         try {      
             file_enum = f.enumerate_children(
                FileAttribute.STANDARD_DISPLAY_NAME + "," +   FileAttribute.STANDARD_TYPE,
-               0, // FileQueryInfoFlags.NONE,
-               null);
+                       FileQueryInfoFlags.NOFOLLOW_SYMLINKS,  // FileQueryInfoFlags.NONE,
+               cancellable);
         } catch (Error e) {
             // FIXME - show error..
             return;
         }
         FileInfo next_file;
         
-        while (true) {
-            try {        
-                next_file = file_enum.next_file(null);
-            } catch (Error e) {
+        while (cancellable.is_cancelled () == false ) {
+            try {
+                next_file = file_enum .next_file (cancellable);
+            } catch(Error e) {
+                print(e.message);
                 break;
             }
+
             if (next_file == null) {
                 break;
             }
+
             //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
-            
+
             if (next_file.get_file_type() != FileType.DIRECTORY) {
                 next_file = null;
                 continue;
             }
-            
-            if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
+
+
+            //stdout.printf("Monitor.monitor: got file %s : type :%u\n",
+            //        next_file.get_display_name(), next_file.get_file_type());
+
+
+            if (next_file.get_is_symlink()) {
                 next_file = null;
                 continue;
             }
@@ -212,7 +232,7 @@ public class Monitor : Object
             
             
             
-            this.monitor(sp, fn, depth + 1);
+            this.monitor(sp, depth + 1);
             
         }
         try {
@@ -251,65 +271,78 @@ public class Monitor : Object
     
     
      
-    public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
+    public void onEvent(File f_orig, File? of_orig, FileMonitorEvent event_type)
     {
         if (this.paused) {
             return;
         }
-        
+       // print("onEvent\n");
         var f = this.realpath(f_orig);
         
-        var of = this.realpath(of_orig);
-        
  
         MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
-        MonitorNamePathDir dest = null;
-        
-        if (of != null) {
-            dest = new MonitorNamePathDir( of.get_basename(), of.get_path(),  Path.get_dirname(of.get_path()));
-            
-        }
-        //string event_name = "UKNOWN";
-        
-        
-        // extract the event names ... - not sure if introspection is feasible in vala..
-        //for(var i in Gio.FileMonitorEvent) {
-         //    if (Gio.FileMonitorEvent[i] == event_type) {
-         //        event_name = i;
-         //    }
-         //}
-        
-        //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
-        //print ("got src: " + src.toString());
-        //print ("got event: " + src.toString());
+               
+        GLib.debug("onEvent src: %s", f.get_path());
+    
         try {
                 
+
             switch(event_type) {
                 case FileMonitorEvent.CHANGED:
+                    src.action = "changed";
                     this.onChanged(src);
                     return; // ingore thise?? -wait for changes_done_htin?
                     
                 case FileMonitorEvent.CHANGES_DONE_HINT:
+                    src.action = "changed";
                     this.onChangesDoneHint(src);
                     return;
                     
                 case FileMonitorEvent.DELETED:
+                    src.action = "rm";
                     this.onDeleted(src);
                     return;
                     
                 case FileMonitorEvent.CREATED:
+                    src.action = "created";
                     this.onCreated(src);
                     return;
                 
                 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
+                    src.action = "attrib";
                     this.onAttributeChanged(src);
                     return;
                 
                 case FileMonitorEvent.MOVED: // eg. chmod/chatt
+                case FileMonitorEvent.MOVED_IN: // eg. chmod/chatt
+                case FileMonitorEvent.MOVED_OUT: // eg. chmod/chatt                                
+                case FileMonitorEvent.RENAMED: // eg. chmod/chatt                      
+                                       if (of_orig == null) { // move from outside of monitoring?
+                                               this.onChanged(src);
+                                               return;
+                                       }
+                                       
+                                       
+                   var of = this.realpath(of_orig);
+                   var   dest = new MonitorNamePathDir(
+                               of.get_basename(), 
+                               of.get_path(),  
+                               Path.get_dirname(of.get_path())
+                    );
+                    
+                                       if (dest == null) {
+                                               
+                                               return;
+                                       }
+                    src.action = "moved";
+                    dest.action = "moved";
                     this.onMoved(src,dest);
                     return; 
-                
+                    
+                default:
+                    stdout.printf("event type not handled %u", event_type);
+                    break;
                 // rest are mount related - not really relivant.. maybe add later..
             } 
         } catch(Error e) {
@@ -318,14 +351,15 @@ public class Monitor : Object
         
     }
     
+
     /** override these to do stuff.. */
-    public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
-    public void onChanged(MonitorNamePathDir src) { }
-    public void onChangesDoneHint(MonitorNamePathDir src) { }
-    public void onDeleted(MonitorNamePathDir src) { }
-    public void onCreated(MonitorNamePathDir src) { }
-    public void onAttributeChanged(MonitorNamePathDir src) { }
-    public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
+    //public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
+    public virtual void  onChanged(MonitorNamePathDir src) { }
+    public virtual void onChangesDoneHint(MonitorNamePathDir src) { }
+    public virtual void onDeleted(MonitorNamePathDir src) { }
+    public virtual void onCreated(MonitorNamePathDir src) { }
+    public virtual void onAttributeChanged(MonitorNamePathDir src) { }
+    public virtual void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
           
     
 }