WindowLog.vala
[gitlive] / WindowLog.vala
1
2 using Wnck;
3   
4 extern int xorg_idletime();
5
6
7 public class WindowLog : Object  {
8
9     string outdir;
10     string win;
11     Screen screen;
12     string lastdir;
13     
14     
15     DateTime lastcopy;
16
17     public WindowLog () {
18         this.outdir = Environment.get_home_dir() + "/.gitlog";
19         this.screen = Wnck.Screen.get_default();
20         this.win = "";
21         this.lastdir = "";
22         this.lastcopy = null;
23         Timeout.add_full(Priority.LOW, 5000, () => {
24             return this.getStatus();
25         } );
26         //Roo.log("Windowlog start");
27         this.screen.active_window_changed.connect((pr_win) => {
28             this.windowChanged();
29         });
30     }
31     
32     
33     
34     
35     public bool getStatus()
36     {
37          
38         var output =  xorg_idletime();
39         //print(output);
40         // more that 10 seconds?? - report idle..
41         if (output * 1 > 10000) {
42             if (this.win.length > 0) { 
43                 //print( (xDate.newDate()).format("Y-m-d H:i:s") + " IDLE");
44                  try {
45                         this.write("", "IDLE");
46                 } catch (Error e) {
47                     print(e.message + "\n");
48                 }
49             }
50             this.win = "";
51             return true;
52         }
53         return true;
54     }
55
56     public void windowChanged()
57     {
58         this.screen.force_update();
59        // print("window changeD");
60         var aw = this.screen.get_active_window();
61         if (aw == null) { 
62             return  ;
63         }
64         try {
65             var win = aw.get_name();
66             var app = aw.get_application();
67             var pid = app.get_pid();
68             //print("PID " + pid);
69                     //var cmd = File.realpath('/proc/'+ pid + '/exe');
70             string cmd = "";
71             size_t len = 0;
72             print("/proc/%u/cmdline".printf(pid) + "\n");
73
74             if (pid > 0 ) { 
75                 FileUtils.get_contents("/proc/%u/cmdline".printf(pid), out cmd, out len);
76             }  else {
77                 cmd = "UNKNOWN";
78             } 
79             //  has it changed?
80             print(this.win +"\n" + cmd + "\n");
81             if (this.win.length < 1 || (win != this.win)) { 
82         
83                 //print((xDate.newDate()).format("Y-m-d H:i:s") + " " + win + ' - '+ cmd );
84                 this.write(cmd, win);
85                 this.win=win;
86             }
87         } catch (Error e) {
88             print(e.message + "\n");
89         }
90         
91         
92         return  ;
93     }
94
95     
96     public void write (string cmd , string str) throws Error
97     {
98         
99         var now = new DateTime.now(new TimeZone.local()); 
100
101         var dir =  this.outdir + now.format("/%Y/%m");
102
103         if (this.lastdir.length < 1 || this.lastdir != dir) {
104             if (!FileUtils.test(dir, FileTest.IS_DIR)) {
105                    if (!FileUtils.test(Path.get_dirname(dir), FileTest.IS_DIR)) { 
106                         File.new_for_path(Path.get_dirname(dir)).make_directory(null);
107                     }
108                     File.new_for_path(dir).make_directory(null);
109             }
110           
111             this.lastdir = dir;
112         }
113         
114         var fname = now.format("/%d") + ".log";
115         var path  = dir + "/" + fname;
116         var time  = now.format("%H:%i:%s ");
117         var f = File.new_for_path(path);
118         FileOutputStream ios = f.append_to (FileCreateFlags.NONE);
119                 var data_out = new DataOutputStream (ios);
120
121         data_out.put_string("\n" +time + str + " "  + cmd, null);
122         print(time + str + " "  + cmd + "\n");
123         data_out.close(null);
124         
125
126  
127         // copy to gitlive/gitlog (each user should check out their own gitlog!?
128         if (this.lastcopy != null && this.lastcopy.format("%H") == now.format("%H")) {
129             return;
130         }
131         this.lastcopy = now;
132
133         var cpdir = GitMonitor.gitlive +
134                     "/gitlog" +  now.format("/%Y/%m");
135                     
136          if (!FileUtils.test(cpdir, FileTest.IS_DIR)) {
137                if (!FileUtils.test(Path.get_dirname(cpdir), FileTest.IS_DIR)) { 
138                     File.new_for_path(Path.get_dirname(cpdir)).make_directory(null);
139                 }
140                 File.new_for_path(cpdir).make_directory(null);
141         }
142  
143         var src = File.new_for_path(path);         
144         var dest = File.new_for_path(cpdir + fname);
145
146         src.copy(dest, FileCopyFlags.OVERWRITE);
147     }
148     
149 }
150
151
152   
153