Uncommited changes synced
[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( this.windowChanged );
28     }
29     
30     
31     
32     
33     public bool getStatus()
34     {
35          
36         var output =  xorg_idletime();
37         //print(output);
38         // more that 10 seconds?? - report idle..
39         if (output * 1 > 10000) {
40             if (this.win.length > 0) { 
41                 //print( (xDate.newDate()).format("Y-m-d H:i:s") + " IDLE");
42                  try {
43                         this.write("", "IDLE");
44                 } catch (Error e) {
45                     GLib.debug("%s",e.message);
46                 }
47             }
48             this.win = "";
49             return true;
50         }
51         return true;
52     }
53
54     public void windowChanged(Wnck.Window? pr_win)
55     {
56         this.screen.force_update();
57        // print("window changeD");
58         var aw = this.screen.get_active_window();
59         if (aw == null) { 
60             return  ;
61         }
62         try {
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             string cmd = "";
69              
70            // print("/proc/%u/cmdline".printf(pid) + "\n");
71
72             if (pid > 0 ) { 
73                 var cf = File.new_for_path("/proc/%u/cmdline".printf(pid));
74                 var dis = new DataInputStream (cf.read ());
75                 cmd = dis.read_line (null);
76  
77             }  else {
78                 cmd = "UNKNOWN";
79             } 
80             //  has it changed?
81             //print(this.win +"\n" + cmd + "\n");
82             if (this.win.length < 1 || (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         } catch (Error e) {
89             GLib.debug("%s",e.message);
90         }
91         
92         
93         return  ;
94     }
95
96     
97     public void write (string cmd , string str) throws Error
98     {
99         
100         var now = new DateTime.now(new TimeZone.local()); 
101
102         var dir =  this.outdir + now.format("/%Y/%m");
103         //print(dir + "\n");
104
105
106         if (this.lastdir.length < 1 || this.lastdir != dir) {
107             if (!FileUtils.test(dir, FileTest.IS_DIR)) {
108                    if (!FileUtils.test(Path.get_dirname(dir), FileTest.IS_DIR)) { 
109                         File.new_for_path(Path.get_dirname(dir)).make_directory(null);
110                     }
111                     File.new_for_path(dir).make_directory(null);
112             }
113           
114             this.lastdir = dir;
115         }
116          
117         var fname = now.format("/%d") + ".log";
118         var path  = dir + "/" + fname;
119         var time  = now.format("%H:%M:%S ");
120         //print("time: " + time + "\n");
121         var f = File.new_for_path(path);
122         var ios = f.append_to (FileCreateFlags.NONE);
123                 var data_out = new DataOutputStream (ios);
124
125         data_out.put_string("\n" +time + str + " "  + cmd, null);
126         GLib.debug("%s%s %s",time , str , cmd);
127         data_out.close(null);
128         
129
130  
131         // copy to gitlive/gitlog (each user should check out their own gitlog!?
132         if (this.lastcopy != null && this.lastcopy.format("%H") == now.format("%H")) {
133             return;
134         }
135         this.lastcopy = now;
136
137         var cpdir = GitMonitor.gitlive +
138                     "/gitlog" +  now.format("/%Y/%m");
139                     
140          if (!FileUtils.test(cpdir, FileTest.IS_DIR)) {
141                if (!FileUtils.test(Path.get_dirname(cpdir), FileTest.IS_DIR)) { 
142                     File.new_for_path(Path.get_dirname(cpdir)).make_directory(null);
143                 }
144                 File.new_for_path(cpdir).make_directory(null);
145         }
146  
147         var src = File.new_for_path(path);         
148         var dest = File.new_for_path(cpdir + fname);
149
150         src.copy(dest, FileCopyFlags.OVERWRITE);
151     }
152     
153 }
154
155
156   
157