README.txt
[gitlive] / Monitor.js
index ac491e9..9e9bd0b 100644 (file)
@@ -1,10 +1,13 @@
 //<Script type="text/javascript">
-Gio      = imports.gi.Gio;
-GLib      = imports.gi.GLib;
+var Gio      = imports.gi.Gio;
+var GLib      = imports.gi.GLib;
 
 
 /**
  * Monitor class - handles monitor managment for a large tree...
+ *
+ *
+ * This 
  * 
  * usage : 
  * x = new Monitor({
@@ -22,7 +25,7 @@ GLib      = imports.gi.GLib;
  */
  
  
-Monitor = function(cfg){
+function Monitor(cfg){
     for (var i in cfg) {
         this[i] = cfg[i];
     }
@@ -38,6 +41,7 @@ Monitor.prototype = {
     
     monitors : false, // Array of GioFileMonitors
     top : false, // list of top level directories..
+    paused : false,
     /**
      * add a directory or file to monitor
      */
@@ -63,58 +67,141 @@ Monitor.prototype = {
         })
         this.monitors = [];
     },
+    /**
+     * pause monitoring - without changing what's monitored 
+     */
+    pause : function()
+    {
+        this.paused = true;
+    },
+    /**
+     * resume monitoring - without changing what's monitored 
+     */
+    resume : function()
+    {
+        this.paused = false;
+    },
     /**
      * monitor a file or directory (privatish)
+     *
+     * initially called with ~/gitlive  null 0 (effectvely)
      * 
      * 
      */
-    monitor : function(path, fn)
+    monitor : function(path, fn, depth)
     {
         var _this = this;
+        
+       // print("ADD: " + path)
+        
+        depth = typeof(depth) == 'number'  ? depth *1 : 0;
+        
+        
         fn = fn || function (fm, f, of, event_type, uh) {
             _this.onEvent(fm, f, of, event_type, uh);
         }
        
+          
         var f = Gio.file_new_for_path(path);
-        //var cancel = new Gio.Cancellable ();
-        var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
-        fm.signal.changed.connect(fn);
-        this.monitors.push(fm);
+            //var cancel = new Gio.Cancellable ();
+        if (depth > 0) {     
+            var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
+            fm.signal.changed.connect(fn);
+            this.monitors.push(fm);
+            // print("ADD path " + depth + ' ' + path);
+        }
         // iterate children?
+        // - this is not used.
+        //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
+            
+        //    this.initRepo(path);
+        //}
+        
         
         var file_enum = f.enumerate_children(
             Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ 
             Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
             Gio.FileQueryInfoFlags.NONE,
             null);
-            
+        
+       
+        
         while ((next_file = file_enum.next_file(null)) != null) {
          
             if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
                 continue;
             }
+            
+            if (next_file.get_file_type() == Gio.FileType.SYMBOLIC_LINK) {
+                continue;
+            }
+            
             if (next_file.get_display_name()[0] == '.') {
                 continue;
             }
-            this.monitor(path+'/'+next_file.get_display_name(), fn)
+            var sp = path+'/'+next_file.get_display_name();
+            // skip modules.
+           
+            
+            this.monitor(sp, fn, depth + 1)
         }
     
         file_enum.close(null);
     },
     
     
-    onEvent : function(fm, f, of, event_type, uh)
+    
+    realpath : function(file)
     {
+        if (!file) {
+            return file;
+        }
+        
+        if (GLib.file_test(file.get_path(), GLib.FileTest.EXISTS)) {
+            var rp = imports.os.realpath(file.get_path());
+            return Gio.file_new_for_path(rp);  
+            
+        }
+        // file does not currently exist..
+        // check parent.
+        var bn = file.get_basename();
+        var ar = file.get_path().split('/');
+        ar.pop();
+        var dirname = ar.join('/');
+        var rp = imports.os.realpath(dirname);
+        return Gio.file_new_for_path(rp + '/' + bn);
+        
+        
+    },
+    
+    
+    
+    onEvent : function(fm, f_orig, of_orig, event_type, uh)
+    {
+        if (this.paused) {
+            return;
+        }
+        
+        var f = this.realpath(f_orig);
+        
+        var of = this.realpath(of_orig);
         var src = {
             name : f.get_basename(),
             path : f.get_path(),
             dir   : GLib.path_get_dirname(f.get_path())
         };
-        var dest = of ? {
-            name : of.get_basename(),
-            path : of.get_path(),
-            dir   : GLib.path_get_dirname(of.get_path())
-        } : false;
+        
+        var dest = false;
+        
+        if (of) {
+            
+            dest =  {
+                name : of.get_basename(),
+                path : of.get_path(),
+                dir   : GLib.path_get_dirname(of.get_path())
+            }
+        }
         
         
         for(var i in Gio.FileMonitorEvent) {
@@ -122,8 +209,9 @@ Monitor.prototype = {
                 event_name = i;
             }
         }
-        print ("got event: " +event_name);
-        print ("got src: " + src.toString());
+        
+        //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
+        //print ("got src: " + src.toString());
         //print ("got event: " + src.toString());
         try {
                 
@@ -145,7 +233,7 @@ Monitor.prototype = {
                     return;
                 
                 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
-                    this.onAttributeCreated(src);
+                    this.onAttributeChanged(src);
                     return;
                 
                 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
@@ -161,7 +249,7 @@ Monitor.prototype = {
     },
     
     /** override these to do stuff.. */
-     
+    initRepo : function(src) { }, // called on startup at the top level repo dir.
     onChanged : function(src) { },
     onChangesDoneHint : function(src) { },
     onDeleted : function(src) { },