fix date calc
[gitlive] / WindowLog.js
1 //<script type="text/javascript">
2 /**
3  * log desktop actions..
4  *
5  * a potential add-on to gitlive....
6  * - basically tracks what applications are being worked on when, allowing you to
7  * use it to generate time tracking (eg. invoices...) later..
8  * 
9  *
10  * apt-get install gir1.2-wnck-3.0
11  * -- needs latest seed (HEAD as of 30nov2011) with the xorg module built in..
12  * 
13  */
14
15
16
17
18 Wnck = imports.gi.Wnck;
19 Gtk = imports.gi.Gtk ;
20 Spawn = imports.Spawn;
21 File  = imports.File.File;
22 GLib        = imports.gi.GLib;
23 Gio = imports.gi.Gio;
24
25 //Gtk.init(Seed.argv);
26 xDate = imports.Date;
27 xorg = imports.xorg;
28
29 WindowLog = {
30     
31     win : false,
32     screen : false,
33  
34     start  : function()
35     {
36         
37         this.outdir = GLib.get_home_dir() + "/.gitlog";
38         this.screen = Wnck.Screen.get_default();
39         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() { return WindowLog.getStatus() } );
40         
41     },
42         
43     getStatus : function()
44     {
45         
46          
47         var output =  xorg.screensaverinfo_get_idletime();
48         //print(output);
49          
50         if (output * 1 > 10000) {
51             if (this.win != false) { 
52                 print( (xDate.newDate()).format("Y-m-d H:i:s") + " IDLE");
53                 this.write(false, "IDLE");
54             }
55             this.win = false;
56             return true;
57         }
58         
59         this.screen.force_update();
60        
61         var aw = this.screen.get_active_window();
62         if (aw) { 
63             var win = aw.get_name();
64             var app = aw.get_application();
65             var pid = app.get_pid();
66             //print("PID " + pid);
67             //var cmd = File.realpath('/proc/'+ pid + '/exe');
68             var cmd = pid ? File.read('/proc/'+ pid + '/cmdline') : 'UNKNOWN';
69             
70             if (!this.win || (this.win && win != this.win)) { 
71         
72                 print((xDate.newDate()).format("Y-m-d H:i:s") + " " + win + ' - '+ cmd );
73                 this.write(cmd, win);
74                 this.win=win;
75             }
76         }
77         
78         
79         return true;
80     },
81     lastdir : false,
82     
83     
84     
85     
86     write : function (cmd , str)
87     {
88         
89         var dir =  this.outdir + (xDate.newDate()).format("/Y/m");
90         if (!this.lastdir || this.lastdir != dir) {
91             if (!File.isDirectory(dir)) {
92                File.mkdir(dir,true);
93             }
94             this.lastdir = dir;
95         }
96         var ctime = xDate.newDate();
97         var fname = ctime.format("/d") + ".log";
98         var path  = dir + '/' + fname;
99         var time  = ctime.format("H:i:s ")
100         
101         File.append (path, "\n" +time + str + ' ' + cmd );
102
103  
104         // copy to gitlive/gitlog (each user should check out their own gitlog!?
105         if (this.lastcopy && this.lastcopy > ctime.add(xDate.Date.HOUR, -1)) {
106             return;
107         }
108         this.lastcopy = ctime;
109         var cpdir = imports.GitMonitor.GitMonitor.gitlive +
110                     '/gitlog' +  (xDate.newDate()).format("/Y/m");
111                     
112         if (!File.isDirectory(cpdir)) {
113            File.mkdir(cpdir,true);
114         }
115         File.copy(path, cpdir + fname, Gio.FileCopyFlags.OVERWRITE );
116         
117         
118     }
119     
120 }
121
122
123