Monitor.vala
[gitlive] / Monitor.vala
index 56f92cf..3bdc946 100644 (file)
@@ -13,12 +13,13 @@ 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();
+    return 0;
 
 }
 
 
-class  MonitorNamePathDir {
+public class  MonitorNamePathDir {
     
     public string name;
     public string path;
@@ -33,6 +34,9 @@ class  MonitorNamePathDir {
     }
 }
 
+public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
+
+
 /**
  * Monitor class - handles monitor managment for a large tree...
  *
@@ -68,7 +72,7 @@ public class Monitor : Object
         this.paused = false;
     }
      
-    public ArrayList<FileMonitor> monitors;// Array of GioFileMonitors
+    public ArrayList<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
     public ArrayList<string> top; // list of top level directories..
     public bool paused;
     /**
@@ -83,8 +87,10 @@ public class Monitor : Object
      */
     public void start()
     {
-        foreach(var in in this.top) {
-            this.monitor(this.top[i]);
+        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 ) ;
+                } );
         }
     }
     /**
@@ -94,7 +100,7 @@ public class Monitor : Object
     public void stop()
     {
         
-        foreach(var i in  this.monitors) {
+        for(int i = 0; i < this.monitors.size ; i++) {
             this.monitors[i].cancel();
         } 
         this.monitors = new ArrayList<FileMonitor>(); // clean /destroy/ kill old?
@@ -120,7 +126,7 @@ public class Monitor : Object
      * 
      * 
      */
-    public void monitor(string path, string fn, int depth)
+    public void monitor(string path, onEventHander fn , int depth = 0)
     {
          
        // print("ADD: " + path)
@@ -136,11 +142,23 @@ 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) {
+                            fn (fm,  f_orig,  of_orig,  event_type ) ;
+                           // return;
+                        //}
+                        //this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
+                });
+                this.monitors.add(fm);
+
+            } catch (Error e) {
+                // FIXME -- show error? do nothing..            
+            }
             // print("ADD path " + depth + ' ' + path);
         }
         // iterate children?
@@ -150,26 +168,31 @@ 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,
+            FileAttribute.STANDARD_DISPLAY_NAME + "," +   FileAttribute.STANDARD_TYPE,
             0, // FileQueryInfoFlags.NONE,
             null);
         
         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 +201,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);
          
@@ -189,8 +212,11 @@ public class Monitor : Object
             this.monitor(sp, fn, depth + 1);
             
         }
-    
-        file_enum.close(null);
+        try {
+            file_enum.close(null);
+        } catch(Error e) {
+            // ignore?
+        }
     }
     
     
@@ -237,11 +263,11 @@ public class Monitor : Object
         MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
         MonitorNamePathDir dest = null;
         
-        if (of) {
+        if (of != null) {
             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..
@@ -257,27 +283,27 @@ public class Monitor : Object
         try {
                 
             switch(event_type) {
-                case Gio.FileMonitorEvent.CHANGED:
+                case FileMonitorEvent.CHANGED:
                     this.onChanged(src);
                     return; // ingore thise?? -wait for changes_done_htin?
                     
-                case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
+                case FileMonitorEvent.CHANGES_DONE_HINT:
                     this.onChangesDoneHint(src);
                     return;
                     
-                case Gio.FileMonitorEvent.DELETED:
+                case FileMonitorEvent.DELETED:
                     this.onDeleted(src);
                     return;
                     
-                case Gio.FileMonitorEvent.CREATED:
+                case FileMonitorEvent.CREATED:
                     this.onCreated(src);
                     return;
                 
-                case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
+                case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
                     this.onAttributeChanged(src);
                     return;
                 
-                case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
+                case FileMonitorEvent.MOVED: // eg. chmod/chatt
                     this.onMoved(src,dest);
                     return; 
                 
@@ -296,7 +322,7 @@ public class Monitor : Object
     public void onDeleted(MonitorNamePathDir src) { }
     public void onCreated(MonitorNamePathDir src) { }
     public void onAttributeChanged(MonitorNamePathDir src) { }
-    public void onMoved(MonitorNamePathDir src) { }
+    public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
           
     
 }