Monitor.vala
[gitlive] / Monitor.vala
index cbf3643..1325375 100644 (file)
@@ -5,21 +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?
-
+//using Gee; // for array list?
+/*
 static int main (string[] args) {
     // A reference to our file
-    var file = File.new_for_path ("data.txt");
+    //var file = File.new_for_path ("data.txt");
+    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;
@@ -29,10 +37,16 @@ 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);
+
+
 /**
  * Monitor class - handles monitor managment for a large tree...
  *
@@ -63,28 +77,28 @@ public class Monitor : Object
     {
        
      
-        this.monitors = new ArrayList<FileMonitor> ();
-        this.top = new ArrayList<string> ();
+        this.monitors = new Array<FileMonitor> ();
+        this.top = new Array<string> ();
         this.paused = false;
     }
      
-    public ArrayList<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
-    public ArrayList<string> top; // list of top level directories..
+    public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
+    public Array<string> top; // list of top level directories..
     public bool paused;
     /**
      * add a directory or file to monitor
      */
     public void add (string add)
     {
-        this.top.add(add);
+        this.top.append_val(add);
     }
     /**
      * start monitoring
      */
     public void start()
     {
-        foreach(var in in this.top) {
-            this.monitor(this.top[i]);
+        for(int i = 0; i < this.top.length ; i++) {
+            this.monitor(this.top.index(i));
         }
     }
     /**
@@ -94,10 +108,10 @@ public class Monitor : Object
     public void stop()
     {
         
-        foreach(var i in  this.monitors) {
-            this.monitors[i].cancel();
+        for(int i = 0; i < this.monitors.length ; i++) {
+            this.monitors.index(i).cancel();
         } 
-        this.monitors = new ArrayList<FileMonitor>(); // clean /destroy/ kill old?
+        this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
     }
     /**
      * pause monitoring - without changing what's monitored 
@@ -120,10 +134,10 @@ public class Monitor : Object
      * 
      * 
      */
-    public void monitor(string path, string fn, int depth)
+    public void monitor(string path, int depth = 0)
     {
          
-       // print("ADD: " + path)
+        print("ADD: " + path + "\n");
         
         //depth = typeof(depth) == 'number'  ? depth *1 : 0;
         depth = depth > 0  ? depth *1 : 0;
@@ -136,11 +150,24 @@ public class Monitor : Object
           
         var f = File.new_for_path(path);
             //var cancel = new Gio.Cancellable ();
-        if (depth > 0) {     
-            var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
-            
-            fm.changed.connect(onEvent);
-            this.monitors.add(fm);
+        if (depth > 0) { 
+  
+            try {
+  
+                 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
+                 fm.changed.connect( ( fm,  f_orig,  of_orig,  event_type) => {
+                        //if (fn) {
+                            this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
+                           // return;
+                        //}
+                        //this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
+                });
+                this.monitors.append_val(fm);
+
+            } catch (Error e) {
+                // FIXME -- show error? do nothing..            
+            }
             // print("ADD path " + depth + ' ' + path);
         }
         // iterate children?
@@ -149,27 +176,35 @@ public class Monitor : Object
             
         //    this.initRepo(path);
         //}
-        
-        var enumerator = directory.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
-
-         var file_enum = f.enumerate_children(
-            FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+    FILE_ATTRIBUTE_STANDARD_TYPE,
-            0, // FileQueryInfoFlags.NONE,
-            null);
-        
+        FileEnumerator file_enum;
+        try {      
+            file_enum = f.enumerate_children(
+               FileAttribute.STANDARD_DISPLAY_NAME + "," +   FileAttribute.STANDARD_TYPE,
+               0, // FileQueryInfoFlags.NONE,
+               null);
+        } catch (Error e) {
+            // FIXME - show error..
+            return;
+        }
         FileInfo next_file;
         
-        while ((next_file = file_enum.next_file(null)) != null) {
-         
+        while (true) {
+            try {        
+                next_file = file_enum.next_file(null);
+            } catch (Error e) {
+                break;
+            }
+            if (next_file == null) {
+                break;
+            }
             //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
             
-            if (next_file.get_file_type() != FILETYPE_DIRECTORY) {
+            if (next_file.get_file_type() != FileType.DIRECTORY) {
                 next_file = null;
                 continue;
             }
             
-            if (next_file.get_file_type() == FILETYPE_SYMBOLIC_LINK) {
+            if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
                 next_file = null;
                 continue;
             }
@@ -178,7 +213,7 @@ public class Monitor : Object
                 next_file = null;
                 continue;
             }
-            var sp = path+'/'+next_file.get_display_name();
+            var sp = path+"/"+next_file.get_display_name();
             // skip modules.
             //print("got a file : " + sp);
          
@@ -186,11 +221,14 @@ public class Monitor : Object
             
             
             
-            this.monitor(sp, fn, depth + 1);
+            this.monitor(sp, depth + 1);
             
         }
-    
-        file_enum.close(null);
+        try {
+            file_enum.close(null);
+        } catch(Error e) {
+            // ignore?
+        }
     }
     
     
@@ -227,7 +265,7 @@ public class Monitor : Object
         if (this.paused) {
             return;
         }
-        
+        print("onEvent");
         var f = this.realpath(f_orig);
         
         var of = this.realpath(of_orig);
@@ -241,7 +279,7 @@ public class Monitor : Object
             dest = new MonitorNamePathDir( of.get_basename(), of.get_path(),  Path.get_dirname(of.get_path()));
             
         }
-        string event_name = "UKNOWN";
+        //string event_name = "UKNOWN";
         
         
         // extract the event names ... - not sure if introspection is feasible in vala..
@@ -251,6 +289,9 @@ public class Monitor : Object
          //    }
          //}
         
+
+
+
         //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
         //print ("got src: " + src.toString());
         //print ("got event: " + src.toString());
@@ -258,26 +299,33 @@ public class Monitor : Object
                 
             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
+                    src.action = "moved";
+                    dest.action = "moved";
                     this.onMoved(src,dest);
                     return;