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              
72             print("/proc/%u/cmdline".printf(pid) + "\n");
73
74             if (pid > 0 ) { 
75                 var cf = File.new_for_path("/proc/%u/cmdline".printf(pid));
76                 var dis = new DataInputStream (cf.read ());
77                 cmd = dis.read_line (null);
78  
79             }  else {
80                 cmd = "UNKNOWN";
81             } 
82             //  has it changed?
83             print(this.win +"\n" + cmd + "\n");
84             if (this.win.length < 1 || (win != this.win)) { 
85         
86                 //print((xDate.newDate()).format("Y-m-d H:i:s") + " " + win + ' - '+ cmd );
87                 this.write(cmd, win);
88                 this.win=win;
89             }
90         } catch (Error e) {
91             print(e.message + "\n");
92         }
93         
94         
95         return  ;
96     }
97
98     
99     public void write (string cmd , string str) throws Error
100     {
101         
102         var now = new DateTime.now(new TimeZone.local()); 
103
104         var dir =  this.outdir + now.format("/%Y/%m");
105         print(dir + "\n");
106
107
108         if (this.lastdir.length < 1 || this.lastdir != dir) {
109             if (!FileUtils.test(dir, FileTest.IS_DIR)) {
110                    if (!FileUtils.test(Path.get_dirname(dir), FileTest.IS_DIR)) { 
111                         File.new_for_path(Path.get_dirname(dir)).make_directory(null);
112                     }
113                     File.new_for_path(dir).make_directory(null);
114             }
115           
116             this.lastdir = dir;
117         }
118         
119         var fname = now.format("/%d") + ".log";
120         var path  = dir + "/" + fname;
121         var time  = now.format("%H:%M:%S ");
122         print("time: " + time + "\n");
123         var f = File.new_for_path(path);
124         FileOutputStream ios = f.append_to (FileCreateFlags.NONE);
125                 var data_out = new DataOutputStream (ios);
126
127         data_out.put_string("\n" +time + str + " "  + cmd, null);
128         print(time + str + " "  + cmd + "\n");
129         data_out.close(null);
130         
131
132  
133         // copy to gitlive/gitlog (each user should check out their own gitlog!?
134         if (this.lastcopy != null && this.lastcopy.format("%H") == now.format("%H")) {
135             return;
136         }
137         this.lastcopy = now;
138
139         var cpdir = GitMonitor.gitlive +
140                     "/gitlog" +  now.format("/%Y/%m");
141                     
142          if (!FileUtils.test(cpdir, FileTest.IS_DIR)) {
143                if (!FileUtils.test(Path.get_dirname(cpdir), FileTest.IS_DIR)) { 
144                     File.new_for_path(Path.get_dirname(cpdir)).make_directory(null);
145                 }
146                 File.new_for_path(cpdir).make_directory(null);
147         }
148  
149         var src = File.new_for_path(path);         
150         var dest = File.new_for_path(cpdir + fname);
151
152         src.copy(dest, FileCopyFlags.OVERWRITE);
153     }
154     
155 }
156
157
158   
159