gtkhotkey-0.2.1-patched.tgz
[gitlive] / Monitor.js
index bbffecb..3e8760e 100644 (file)
@@ -1,10 +1,15 @@
 //<Script type="text/javascript">
-var Gio      = imports.gi.Gio;
-var GLib      = imports.gi.GLib;
+var Gio     = imports.gi.Gio;
+var GLib    = imports.gi.GLib;
 
+var XObject = imports.XObject.XObject;
+var File    = imports.File.File;
 
 /**
  * Monitor class - handles monitor managment for a large tree...
+ *
+ *
+ * This 
  * 
  * usage : 
  * x = new Monitor({
@@ -21,7 +26,6 @@ var GLib      = imports.gi.GLib;
  * 
  */
  
 function Monitor(cfg){
     for (var i in cfg) {
         this[i] = cfg[i];
@@ -51,7 +55,9 @@ Monitor.prototype = {
      */
     start : function()
     {
-        this.top.forEach(this.monitor, this);
+        for(var i =0;i < this.top.length; i++) {
+            this.monitor(this.top[i]);
+        }
     },
     /**
      * stop / pause monitoring
@@ -59,9 +65,10 @@ Monitor.prototype = {
      */
     stop : function()
     {
-        this.monitors.forEach(function(m) {
-            m.cancel();
-        })
+        
+        for(var i =0;i < this.monitors.length; i++) {
+            this.monitors[i].cancel();
+        } 
         this.monitors = [];
     },
     /**
@@ -98,95 +105,130 @@ Monitor.prototype = {
             _this.onEvent(fm, f, of, event_type, uh);
         }
        
-        // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
-        if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
-            return;
-        }
-            
-       
-       
-        
+          
         var f = Gio.file_new_for_path(path);
             //var cancel = new Gio.Cancellable ();
         if (depth > 0) {     
+
             var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
-            fm.signal.changed.connect(fn);
+
+
+            
+            XObject.isSeed ?  fm.signal.changed.connect(fn) : fm.connect('changed',fn);
             this.monitors.push(fm);
             // print("ADD path " + depth + ' ' + path);
         }
         // iterate children?
-        
-        if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
+        // - this is not used.
+        //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
             
-            this.initRepo(path);
-        }
-        
+        //    this.initRepo(path);
+        //}
         
-        var file_enum = f.enumerate_children(
+         var file_enum = f.enumerate_children(
             Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ 
             Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
             Gio.FileQueryInfoFlags.NONE,
             null);
         
-       
+        var next_file;
         
         while ((next_file = file_enum.next_file(null)) != null) {
          
+            //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
+            
             if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
+                next_file = null;
                 continue;
             }
             
             if (next_file.get_file_type() == Gio.FileType.SYMBOLIC_LINK) {
+                next_file = null;
                 continue;
             }
             
             if (next_file.get_display_name()[0] == '.') {
+                next_file = null;
                 continue;
             }
             var sp = path+'/'+next_file.get_display_name();
             // skip modules.
-           
+            //print("got a file : " + sp);
+         
+            next_file = null;
+            
+            
+            
+            this.monitor(sp, fn, depth + 1);
             
-            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 = File.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 = File.realpath(dirname);
+        return Gio.file_new_for_path(rp + '/' + bn);
+        
+    },
+   
+    
+    
+    
+    
+    onEvent : function(fm, f_orig, of_orig, event_type, uh)
     {
         if (this.paused) {
             return;
         }
-        print("WAS: " + f.get_path() + "\n");
         
-        var can = f.resolve_relative_path('');
-        print("NOW: " + can.get_path() + "\n");
+        var f = this.realpath(f_orig);
+        
+        var of = this.realpath(of_orig);
         var src = {
-            name : can.get_basename(),
-            path : can.get_path(),
-            dir   : GLib.path_get_dirname(can.get_path())
+            name : f.get_basename(),
+            path : f.get_path(),
+            dir   : GLib.path_get_dirname(f.get_path())
         };
         
         var dest = false;
         
         if (of) {
-            can = of.resolve_relative_path('');
+            
             dest =  {
-                name : can.get_basename(),
-                path : can.get_path(),
-                dir   : GLib.path_get_dirname(can.get_path())
+                name : of.get_basename(),
+                path : of.get_path(),
+                dir   : GLib.path_get_dirname(of.get_path())
             }
         }
-        
+        var event_name = 'UKNOWN';;
         
         for(var i in Gio.FileMonitorEvent) {
             if (Gio.FileMonitorEvent[i] == event_type) {
                 event_name = i;
             }
         }
-        //print ("got event: " +event_name);
+        
+        //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
         //print ("got src: " + src.toString());
         //print ("got event: " + src.toString());
         try {