fix date calc
[gitlive] / WindowLog.js
index 7d4adcd..62fe24e 100644 (file)
@@ -6,31 +6,9 @@
  * - basically tracks what applications are being worked on when, allowing you to
  * use it to generate time tracking (eg. invoices...) later..
  * 
- * mouse:
- * xdotool getmouselocation
- *
- * xwit -property WM_NAME -print 
- * 0x3a13d6c: x=1 y=25 w=1109 h=747 d=32 'root@wideboy: /home/alan'
- * root@wideboy:/home/alan# xwit -property WM_CLASS -print 
- * 0x3a13d6c: x=1 y=25 w=1109 h=747 d=32 'gnome-terminal'
- * root@wideboy:/home/alan# 
- * 
- * 
- * Not sure how we handle last key press...
- * 
- * -- log to a remote url.
- * 
- * LOG:
- * DATE / TIME / Application / Title
- * if cursor same as before.. - SEND 'IDLE'... (twice, then stop sending..)
- * 
- * For commits... (we send out every 1 minute as well, it's upto the other end to determine if that means updating
- * or creating a new record..
- * yyyy-mm-dd /12:00 / GIT / XXXXX
- *
  *
  * apt-get install gir1.2-wnck-3.0
- *
+ * -- needs latest seed (HEAD as of 30nov2011) with the xorg module built in..
  * 
  */
 
@@ -42,8 +20,11 @@ Gtk = imports.gi.Gtk ;
 Spawn = imports.Spawn;
 File  = imports.File.File;
 GLib        = imports.gi.GLib;
+Gio = imports.gi.Gio;
+
 //Gtk.init(Seed.argv);
 xDate = imports.Date;
+xorg = imports.xorg;
 
 WindowLog = {
     
@@ -52,36 +33,44 @@ WindowLog = {
  
     start  : function()
     {
+        
         this.outdir = GLib.get_home_dir() + "/.gitlog";
         this.screen = Wnck.Screen.get_default();
         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() { return WindowLog.getStatus() } );
         
     },
         
-    getStatus : function() {
+    getStatus : function()
+    {
         
-        var output = Spawn.run({
-           cwd : __script_path__ + '/tests',
-           args : [ __script_path__ + '/tests/xidletime'  ] 
-        });
+         
+        var output =  xorg.screensaverinfo_get_idletime();
         //print(output);
          
         if (output * 1 > 10000) {
             if (this.win != false) { 
                 print( (xDate.newDate()).format("Y-m-d H:i:s") + " IDLE");
-                this.write("IDLE");
+                this.write(false, "IDLE");
             }
             this.win = false;
             return true;
         }
+        
         this.screen.force_update();
+       
         var aw = this.screen.get_active_window();
         if (aw) { 
             var win = aw.get_name();
+            var app = aw.get_application();
+            var pid = app.get_pid();
+            //print("PID " + pid);
+            //var cmd = File.realpath('/proc/'+ pid + '/exe');
+            var cmd = pid ? File.read('/proc/'+ pid + '/cmdline') : 'UNKNOWN';
+            
             if (!this.win || (this.win && win != this.win)) { 
         
-                print((xDate.newDate()).format("Y-m-d H:i:s") + " " + win);
-                this.write(win);
+                print((xDate.newDate()).format("Y-m-d H:i:s") + " " + win + ' - '+ cmd );
+                this.write(cmd, win);
                 this.win=win;
             }
         }
@@ -91,7 +80,11 @@ WindowLog = {
     },
     lastdir : false,
     
-    write : function (str) {
+    
+    
+    
+    write : function (cmd , str)
+    {
         
         var dir =  this.outdir + (xDate.newDate()).format("/Y/m");
         if (!this.lastdir || this.lastdir != dir) {
@@ -100,14 +93,31 @@ WindowLog = {
             }
             this.lastdir = dir;
         }
+        var ctime = xDate.newDate();
+        var fname = ctime.format("/d") + ".log";
+        var path  = dir + '/' + fname;
+        var time  = ctime.format("H:i:s ")
+        
+        File.append (path, "\n" +time + str + ' ' + cmd );
+
+        // copy to gitlive/gitlog (each user should check out their own gitlog!?
+        if (this.lastcopy && this.lastcopy > ctime.add(xDate.Date.HOUR, -1)) {
+            return;
+        }
+        this.lastcopy = ctime;
+        var cpdir = imports.GitMonitor.GitMonitor.gitlive +
+                    '/gitlog' +  (xDate.newDate()).format("/Y/m");
+                    
+        if (!File.isDirectory(cpdir)) {
+           File.mkdir(cpdir,true);
+        }
+        File.copy(path, cpdir + fname, Gio.FileCopyFlags.OVERWRITE );
         
-        var path = dir + (xDate.newDate()).format("/d") + ".log";
-        var time = (xDate.newDate()).format("H:i:s ")
         
-        File.append (path, time +  str + "\n");
     }
     
 }
 
 
-  
\ No newline at end of file
+