Fix #5662 - Gitlive - create a tracking branch
[gitlive] / old_seed_version / 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 const Wnck = imports.gi.Wnck;
19 const  Gtk = imports.gi.Gtk ;
20 const Spawn = imports.Spawn;
21 const File  = imports.File.File;
22 const GLib        = imports.gi.GLib;
23 const Gio = imports.gi.Gio;
24
25 //Gtk.init(Seed.argv);
26 const xDate = imports.Date;
27 const xorg = typeof(Seed) != 'undefined' ? imports.xorg : false;
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         // 
40         GLib.timeout_add(GLib.PRIORITY_LOW, 5000, function() {
41             return WindowLog.getStatus()
42         } );
43         //Roo.log("Windowlog start");
44         this.screen.signal.active_window_changed.connect(function() {
45             WindowLog.windowChanged();
46         });
47     },
48     
49     
50     
51     
52     getStatus : function()
53     {
54         
55          
56         var output =  xorg.screensaverinfo_get_idletime();
57         //print(output);
58         // more that 10 seconds?? - report idle..
59         if (output * 1 > 10000) {
60             if (this.win != false) { 
61                 print( (xDate.newDate()).format("Y-m-d H:i:s") + " IDLE");
62                 this.write(false, "IDLE");
63             }
64             this.win = false;
65             return true;
66         }
67         return true;
68     },
69      windowChanged : function()
70     {
71         this.screen.force_update();
72        // print("window changeD");
73         var aw = this.screen.get_active_window();
74         if (aw) { 
75             var win = aw.get_name();
76             var app = aw.get_application();
77             var pid = app.get_pid();
78             //print("PID " + pid);
79             //var cmd = File.realpath('/proc/'+ pid + '/exe');
80             var cmd = pid ? File.read('/proc/'+ pid + '/cmdline') : 'UNKNOWN';
81             
82             if (!this.win || (this.win && win != this.win)) { 
83         
84                 print((xDate.newDate()).format("Y-m-d H:i:s") + " " + win + ' - '+ cmd );
85                 this.write(cmd, win);
86                 this.win=win;
87             }
88         }
89         
90         
91         return true;
92     },
93     lastdir : false,
94     
95     
96     
97     
98     write : function (cmd , str)
99     {
100         
101         var dir =  this.outdir + (xDate.newDate()).format("/Y/m");
102         if (!this.lastdir || this.lastdir != dir) {
103             if (!File.isDirectory(dir)) {
104                File.mkdir(dir,true);
105             }
106             this.lastdir = dir;
107         }
108         var ctime = xDate.newDate();
109         var fname = ctime.format("/d") + ".log";
110         var path  = dir + '/' + fname;
111         var time  = ctime.format("H:i:s ")
112         
113         File.append (path, "\n" +time + str + ' ' + cmd );
114
115  
116         // copy to gitlive/gitlog (each user should check out their own gitlog!?
117         if (this.lastcopy && this.lastcopy > ctime.add(xDate.Date.HOUR, -1)) {
118             return;
119         }
120         this.lastcopy = ctime;
121         var cpdir = imports.GitMonitor.GitMonitor.gitlive +
122                     '/gitlog' +  (xDate.newDate()).format("/Y/m");
123                     
124         if (!File.isDirectory(cpdir)) {
125            File.mkdir(cpdir,true);
126         }
127         File.copy(path, cpdir + fname, Gio.FileCopyFlags.OVERWRITE );
128         
129         
130     }
131     
132 }
133
134
135