GitMonitor.vala
[gitlive] / GitMonitor.vala
index 1b72676..771ac4f 100644 (file)
@@ -1,27 +1,42 @@
 
 
-public GitMontitorQueue : MonitorNamePathDir {
+public class GitMonitorQueue : MonitorNamePathDir {
+        // name = basename
+        // path = full path..
+        // dir = dir path
+    
         public string gitpath;
-        public string vpath;
+        public string vpath;  // relative path (within git)
+        public string vname;  // relative filename (within git)
         public string message ; // for commit
-        public bool commit_all
-        public GitMontitorQueue(MonitorNamePathDir f, string gitlive) {
-            this.name = f.name;
-            this.path = f.path;
-            this.dir = f.dir;
+        public bool commit_all;
+        
+
+
+
+
+        public GitMonitorQueue(MonitorNamePathDir f) {
+
+            base(f.name, f.path, f.dir);
+
+
             this.message = "";
             this.commit_all = false;
  
            
-            var vpath_ar = this.path.substring(gitlive.length +1).split('/', 0);
+            var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
             
-            this.gitpath = gitlive + '/' + vpath_ar[0];
+            this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
             
             string[]  vpath = {};
             for (var i = 1; i< vpath_ar.length; i++) {
                 vpath += vpath_ar[i];
             }
+
             this.vpath =  string.joinv("/", vpath);
+
+            this.vname =  this.vpath + (this.vpath.length > 0 ? "/" : "") + this.name;
             //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
         
         
@@ -33,13 +48,13 @@ public GitMontitorQueue : MonitorNamePathDir {
             
             
             // vim.. what a seriously brain dead program..
-            if (this.name == '4913') {
+            if (this.name == "4913") {
                 return true;
             }
             
             if (this.name[0] == '.') {
                 // except!
-                if (this.name == '.htaccess') {
+                if (this.name == ".htaccess") {
                     return false;
                 }
                 
@@ -52,21 +67,24 @@ public GitMontitorQueue : MonitorNamePathDir {
             //    return true;
             //}
             // ignore anything in top level!!!!
-            if (!this.vpath.length) {
+            if (this.vpath.length < 1) {
                 return true;
             }
             
             return false;
         }
-        public int GitMontitorQueue.indexOfAdd( Array<GtkMonitorQueuequeue> add_files, string add) {
+        
+        /** -- statics --*/
+        
+        public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
             for(var i =0; i < add_files.length; i++) {
-                if (addfiles.index(i).add == add) {
+                if (add_files.index(i).vname == add) {
                     return i;
                 }
             }
             return -1;
         }
-        public int GitMontitorQueue.indexOfMessage(Array<GtkMonitorQueuequeue> messages, message)  {
+        public static  int indexOfMessage(Array<GitMonitorQueue> messages, string message)  {
             for(var i =0; i < messages.length; i++) {
                 if (messages.index(i).message == message) {
                     return i;
@@ -74,12 +92,12 @@ public GitMontitorQueue : MonitorNamePathDir {
             }
             return -1;
         }
-        public string GitMontitorQueue.messageToString(Array<GtkMonitorQueuequeue> messages ) {
+        public static string messageToString(Array<GitMonitorQueue> messages ) {
             string[] ret = {};
             for(var i =0; i < messages.length; i++) {
                 ret+= messages.index(i).message;
             }
-            return string.joinv(ret,"\n");
+            return string.joinv("\n",ret);
         }
 
 }
@@ -95,27 +113,27 @@ public class GitMonitor : Monitor
      *  dset by OWNER... - we should do this as a CTOR.
      *  
      */
-    public string gitlive = '';
+    public static string gitlive = "";
     
     
-    public Array<GtkMonitorQueuequeue>;
+    public Array<GitMonitorQueue> queue ;
     public bool queueRunning = false;
     
     public DateTime lastAdd;
      
      
-    public void pause() {
+    public new void pause() {
         this.paused = true;
         // what does this do to the old one...
-        this.queue = new Array<FileMonitor> ();
-        StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
+        this.queue = new Array<GitMonitorQueue> ();
+        StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
 
     }
     
-    public void resume () {
+    public new void resume () {
         this.paused = false;
-        this.queue = new Array<FileMonitor> ();
-        StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
+        this.queue = new Array<GitMonitorQueue> ();
+        StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
         
         
     }
@@ -124,50 +142,52 @@ public class GitMonitor : Monitor
      * and run the queue every 500 milliseconds..
      *
      */
-    public void start() {
-        StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
+    public new void start() {
+        StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
         
          
-        this.lastAdd = new DateTime.now(); 
+        this.lastAdd = new DateTime.now(new TimeZone.local()); 
         
-        Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
+        Timeout.add_full(Priority.LOW, 500, () => {
 
             // call this.monitor on each of 'top'
             for(int i = 0; i < this.top.length ; i++) {
                 this.monitor(this.top.index(i) );
             }
-            StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
+            StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
              
            
             
-            try { 
-                var notification = new Notify.Notification({
+            try {
+
+              
+                var notification = new Notify.Notification(
                     "Git Live",
-                    this.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
+                    "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length), 
                      "dialog-information"
-                });
+                );
         
                 notification.set_timeout(5);
                 notification.show();
             } catch(Error e) {
-                print(e.toString());
+                print(e.message);
             }
 
         });
         
-        Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
+        Timeout.add_full(Priority.LOW, 1000, () => {
             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
-            if (!_this.queue.length || _this.queueRunning) {
+            if (this.queue.length < 1  || this.queueRunning) {
                 return true;
             }
 
-            var last = this.lastAdd.difference(new DateTime.now());
+            var last = this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
 
             
             //print("LAST RUN?" + last);
             
-            if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
-                return 1;
+            if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
+                return true;
             }
             //_this.lastAdd = new Date();
             //return 1;
@@ -180,20 +200,20 @@ public class GitMonitor : Monitor
     }
 
 
-    public void stop() {
-        StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
+    public new void stop() {
+        StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
         base.stop();
     }
     
     
-    public void monitor (string path,  int depth = 0)
+    public new void monitor (string path,  int depth = 0)
     {
         
         //var depth = typeof(depth) == 'number'  ? depth *1 : 0;
         
          
         // 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)) {
+        if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
             return;
         }
         
@@ -231,21 +251,21 @@ public class GitMonitor : Monitor
         }
         this.queueRunning = true;
 
-        var cmds = new Array<GitMontitorQueue>();
+        var cmds = new Array<GitMonitorQueue>();
         for(var i = 0; i < this.queue.length; i++) {
-            cmds.append_val(this.queue.item(i));
+            cmds.append_val(this.queue.index(i));
         }
 
-        this.queue = new Array<GitMontitorQueue>();// empty queue!
+        this.queue = new Array<GitMonitorQueue>();// empty queue!
 
         
-        var success = new Array<String>();
-        var failure = new Array<GitMontitorQueue>();
-        var repos = new Array<GitMontitorQueue>(); //??
-        var done = new Array<GitMontitorQueue>();
+        string[] success = {};
+        string[] failure = {};
+       //var repos = new Array<GitRepo>(); //??
+        var done = new Array<GitMonitorQueue>();
         
         // first build a array of repo's to work with
-        var repo_list = new Array<GitMontitorRepo>();
+        var repo_list = new Array<GitRepo>();
         
         // pull and group.
         
@@ -254,12 +274,13 @@ public class GitMonitor : Monitor
         
         for(var i = 0; i < cmds.length; i++) {
            
+            var cmd = cmds.index(i);
         
             var gitpath = cmd.gitpath; 
-            var ix  = GitMontitorRepo.indexOf(this.repos,  cmd.gitpath);
+            var ix  = GitRepo.indexOf(repo_list,  cmd.gitpath);
             if (ix < 0) {
-                    repo_list.append_val(new GitMontitorRepo( gitpath ));
-                    ix = GitMontitorRepo.indexOf(this.repos,  cmd.gitpath);
+                repo_list.append_val(new GitRepo( gitpath ));
+                ix = GitRepo.indexOf(repo_list,  cmd.gitpath);
             }
             
 
@@ -268,7 +289,7 @@ public class GitMonitor : Monitor
             //    repo_list[gitpath].cmds = [];
              //   repo_list[gitpath].pull();
             //}
-            repo_list.item(ix).append_val(cmd);
+            repo_list.index(ix).cmds.append_val(cmd);
 
         }
         this.paused = false;
@@ -277,21 +298,21 @@ public class GitMonitor : Monitor
         for(var i = 0;i < repo_list.length;i++) {
     
 
-            var repo = repo_list.item(i);
+            var repo = repo_list.index(i);
 
-            var add_files = new Array<GitMontitorQueue>();
-            var remove_files = new Array<GitMontitorQueue>();
-            var messages = new Array<GitMontitorQueue>();
+            var add_files = new Array<GitMonitorQueue>();
+            var remove_files = new Array<GitMonitorQueue>();
+            var messages = new Array<GitMonitorQueue>();
             //print(JSON.stringify(repo.cmds,null,4));
             
-            for(var ii = 0;ii < repo.length;ii++) {
-                var cmd = repo.item(ii);
+            for(var ii = 0;ii < repo.cmds.length;ii++) {
+                var cmd = repo.cmds.index(ii);
     
                 
-                switch(cmd.name) {
+                switch(cmd.action) {
                     case "add" :
                         
-                        if (GitMontitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
+                        if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
                            break;
                         }
         
@@ -299,64 +320,75 @@ public class GitMonitor : Monitor
                         add_files.append_val(cmd);
                         break;
                     
-                    case 'rm':
-                        if (GitMontitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
+                    case "rm":
+                        if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
                            break;
                         }
                         
                         // if file exists, do not try and delete it.
-                        if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
+                        if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
                             break;
                         }
                         
                         remove_files.append_val(cmd);
                         break;
                     
-                    case 'commit' :
-                        if (GitMontitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
+                    case "commit" :
+                        if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
                            break;
                         }
                          
                         messages.append_val(cmd);
                         
-                        break;    
+                        break;
+                    default:
+                        print("Opps unmatched action");
+                        break;
                 } 
             }
             
             //repo.debug = 1;
             // these can fail... at present... as we wildcard stuff.
-            stdout.printf("ADD : %d files"  , add_files.length);
+            stdout.printf("ADD : %u files"  , add_files.length);
             
             // make sure added files do not get removed..
 
-            var remove_files_f = new Array<GitMontitorQueue>();
+            var remove_files_f = new Array<GitMonitorQueue>();
             for(var ii = 0;ii < remove_files.length;ii++) {
-                if (GitMontitorQueue.indexOfAdd(add_files,  remove_files.item(ii).rm) > -1 ) {
+                if (GitMonitorQueue.indexOfAdd(add_files,  remove_files.index(ii).vname) > -1 ) {
                      continue;
                 }
-                remove_files_f.append_val(remove_files.item(ii));
+                remove_files_f.append_val(remove_files.index(ii));
             };
-            stdout.printf("REMOVE : %d files"  , remove_files.length);
+            stdout.printf("REMOVE : %u files"  , remove_files.length);
              
             // make sure monitoring is paused so it does not recursively pick up
             // deletions
             
             // -- DO STUFF..
-            
-            repo.add(add_files);
-            repo.remove(remove_files);
+            try {
+                repo.add(add_files);
+            } catch(Error e) {
+                failure +=  e.message;
+            }  
+            try {
+                 repo.remove(remove_files_f);
+            } catch(Error e) {
+                failure +=  e.message;
+            }  
+
             this.paused = false;
             
             
             try { 
-                success.append_val(repo.commit(
-                    GitMontitorQueue.messageToString(messages)
+                success += repo.commit(
+                    GitMonitorQueue.messageToString(messages),
                     add_files  
-                ));
-                success.push(repo.push());
+                );
+                success += repo.push();
 
             } catch(Error e) {
-                failure.append_val(e.message);
+                failure += e.message;
                 
             }   
         }
@@ -365,15 +397,12 @@ public class GitMonitor : Monitor
          
         try {
             // catch notification failures.. so we can carry on..
-            if (success.length) {
-                var success_str = "";
-                for(var ii = 0;ii < success.length;ii++) {
-                    success_str+= success.item(ii) + "\n";
-                }
+            if (success.length > 0) {
+
                 
                 var notification = new Notify.Notification(
                     "Git Live Commited",
-                    success_str,
+                    string.joinv("\n",success),
                      "dialog-information"
                     
                 );
@@ -382,17 +411,14 @@ public class GitMonitor : Monitor
                 notification.show();   
             }
             
-            if (failure.length) {
-                var failure_str = "";
-                for(var ii = 0;ii < failure.length;ii++) {
-                    failure_str+= failure.item(ii) + "\n";
-                }
-                var notification = new Notify.Notification({
+            if (failure.length > 0) {
+
+                var notification = new Notify.Notification(
                     summary: "Git Live ERROR!!",
-                    failure_str,
+                    string.joinv("\n",failure),
                     "dialog-information"
                     
-                });
+                );
     
                 notification.set_timeout(5); // show errros for longer
                 notification.show();   
@@ -417,11 +443,11 @@ public class GitMonitor : Monitor
    
 
 
-    public void onChanged(MonitorNamePathDir src) { }
+    public void onChanged(MonitorNamePathDir src) 
     { 
         return; // always ignore this..?
         //this.parsePath(src);
-    },
+    }
     
 
  
@@ -429,7 +455,7 @@ public class GitMonitor : Monitor
      *  results in  git add  + git commit..
      *
      */
-    public void onChangesDoneHint(MonitorNamePathDir src) { }
+    public void onChangesDoneHint(MonitorNamePathDir src)  
     { 
         
         if (this.paused) {
@@ -437,8 +463,8 @@ public class GitMonitor : Monitor
         }
             
 
-        this.lastAdd = new DateTime.now(); 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
+        this.lastAdd = new DateTime.now(new TimeZone.local()); 
+        var cmd = new GitMonitorQueue(src);
         if (cmd.shouldIgnore()) {
             return;
         }
@@ -460,11 +486,11 @@ public class GitMonitor : Monitor
             return;
         }
         */
-        cmd.add = src.vpath;
+
         this.queue.append_val(cmd);
 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
-        cmd.name = "commit";
+        var cmd = new GitMonitorQueue(src);
+        cmd.action = "commit";
         cmd.message = src.vpath;
         this.queue.append_val(cmd);
  
@@ -475,19 +501,19 @@ public class GitMonitor : Monitor
         if (this.paused) {
             return true;
         }
-        this.lastAdd = new DateTime.now(); 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
+        this.lastAdd = new DateTime.now(new TimeZone.local()); 
+        var cmd = new GitMonitorQueue(src);
         if (cmd.shouldIgnore()) {
             return;
         }
         // should check if monitor needs removing..
         // it should also check if it was a directory.. - so we dont have to commit all..
-        cmd.name = "rm";
+        cmd.action = "rm";
         cmd.rm = src.vpath;
         this.queue.append_val(cmd);
 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
-        cmd.name = "commit";
+        var cmd = new GitMonitorQueue(src);
+        cmd.action = "commit";
         cmd.message = src.vpath;
         cmd.commit_all = true;
 
@@ -499,13 +525,13 @@ public class GitMonitor : Monitor
         if (this.paused) {
             return true;
         }
-        this.lastAdd = new DateTime.now(); 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
+        this.lastAdd = new DateTime.now(new TimeZone.local()); 
+        var cmd = new GitMonitorQueue(src);
         if (cmd.shouldIgnore()) {
             return;
         }
 
-        if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
+        if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
            // this.just_created[src.path] = true;
             return; // we do not handle file create flags... - use done hint.
         }
@@ -524,33 +550,32 @@ public class GitMonitor : Monitor
 
     }
 
-    public void onAttributeChanged(MonitorNamePathDir src) { }
+    public void onAttributeChanged(MonitorNamePathDir src) { 
 
         if (this.paused) {
             return true;
         }
-        this.lastAdd = new DateTime.now(); 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
+        this.lastAdd = new DateTime.now(new TimeZone.local()); 
+        var cmd = new GitMonitorQueue(src);
         if (cmd.shouldIgnore()) {
             return;
         }
-        cmd.name = "add";
-        cmd.add = src.vpath;
+        cmd.action = "add";
         this.queue.append_val(cmd);
 
-        var cmd = new GitMontitorQueue(src, this.gitlive);
-        cmd.name = "commit";
+        var cmd = new GitMonitorQueue(src);
+        cmd.action = "commit";
         cmd.message = "Attribute changed " + cmd.vpath;
         this.queue.append_val(cmd);
     }
 
 
-   public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
+   public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)  
     { 
-        this.lastAdd = new DateTime.now(); 
-        var cmd_s = new GitMontitorQueue(src, this.gitlive);
+        this.lastAdd = new DateTime.now(new TimeZone.local()); 
+        var cmd_s = new GitMonitorQueue(src);
 
-        var cmd_d = new GitMontitorQueue(src, this.gitlive);
+        var cmd_d = new GitMonitorQueue(src);
    
         
         if (cmd_d.gitpath != cmd_s.gitpath) {
@@ -575,20 +600,18 @@ public class GitMonitor : Monitor
             return;
         }
         
-        cmd_s.name = "rm";
-        cmd_s.rm = src.vpath;
+        cmd_s.action = "rm";
         this.queue.append_val(cmd_s);
 
 
 
 
-        cmd_d.name = "add";
-        cmd_d.add = src.vpath;
+        cmd_d.action = "add";
         this.queue.append_val(cmd_d);
 
 
-        var cmd = new GitMontitorQueue(dest, this.gitlive);
-        cmd.name = "commit";
+        var cmd = new GitMonitorQueue(dest);
+        cmd.action = "commit";
         cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
         this.queue.append_val(cmd);