Final User interface tweaks to basic commit code (shows dialogs while it does stuff)
[gitlive] / Monitor.js
index c498f03..5d4ba1b 100644 (file)
@@ -1,4 +1,8 @@
 //<Script type="text/javascript">
+var Gio      = imports.gi.Gio;
+var GLib      = imports.gi.GLib;
+
+
 /**
  * Monitor class - handles monitor managment for a large tree...
  * 
@@ -18,7 +22,7 @@
  */
  
  
-Monitor = function(cfg){
+function Monitor(cfg){
     for (var i in cfg) {
         this[i] = cfg[i];
     }
@@ -34,6 +38,7 @@ Monitor.prototype = {
     
     monitors : false, // Array of GioFileMonitors
     top : false, // list of top level directories..
+    paused : false,
     /**
      * add a directory or file to monitor
      */
@@ -54,36 +59,77 @@ Monitor.prototype = {
      */
     stop : function()
     {
-        this.monitors.foreach(function(m) {
+        this.monitors.forEach(function(m) {
             m.cancel();
         })
         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);
         }
        
+        // 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 ();
-        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?
         
+        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) {
@@ -92,59 +138,85 @@ Monitor.prototype = {
             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)
     {
+        if (this.paused) {
+            return;
+        }
         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;
-         
-        switch(event_type) {
-            case Gio.FileMonitorEvent.CHANGED:
-                this.onChanged(src);
-                return; // ingore thise?? -wait for changes_done_htin?
+        
+        
+        for(var i in Gio.FileMonitorEvent) {
+            if (Gio.FileMonitorEvent[i] == event_type) {
+                event_name = i;
+            }
+        }
+        //print ("got event: " +event_name);
+        //print ("got src: " + src.toString());
+        //print ("got event: " + src.toString());
+        try {
                 
-            case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
-                this.onChangesDoneHint(src);
-                return;
+            switch(event_type) {
+                case Gio.FileMonitorEvent.CHANGED:
+                    this.onChanged(src);
+                    return; // ingore thise?? -wait for changes_done_htin?
+                    
+                case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
+                    this.onChangesDoneHint(src);
+                    return;
+                    
+                case Gio.FileMonitorEvent.DELETED:
+                    this.onDeleted(src);
+                    return;
+                    
+                case Gio.FileMonitorEvent.CREATED:
+                    this.onCreated(src);
+                    return;
                 
-            case Gio.FileMonitorEvent.DELETED:
-                this.onDeleted(src);
-                return;
+                case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
+                    this.onAttributeChanged(src);
+                    return;
                 
-            case Gio.FileMonitorEvent.CREATED:
-                this.onCreated(src);
-                return;
-            
-            case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
-                this.onAttributeCreated(src);
-                return;
-            
-            case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
-                this.onMoved(src,dest);
-                return; 
-            
-            // rest are mount related - not really relivant.. maybe add later..
+                case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
+                    this.onMoved(src,dest);
+                    return; 
                 
-        } 
+                // rest are mount related - not really relivant.. maybe add later..
+            } 
+        } catch(e) {
+            print(e);
+        }
+        
     },
+    
     /** 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) { },
     onCreated : function(src) { },
-    onMoved : function(src) { },
+    onAttributeChanged : function(src) { },
+    onMoved : function(src) { }
           
     
 }