Monitor.vala
[gitlive] / Monitor.vala
1 //<Script type="text/javascript">
2 //var Gio     = imports.gi.Gio;
3 //var GLib    = imports.gi.GLib;
4
5 //var XObject = imports.XObject.XObject;
6 //var File    = imports.File.File;
7
8 /// # valac  --pkg gio-2.0  --pkg posix Monitor.vala -o /tmp/Monitor
9
10  
11 //using Gee; // for array list?
12
13 static int main (string[] args) {
14     // A reference to our file
15     //var file = File.new_for_path ("data.txt");
16     MainLoop loop = new MainLoop ();
17     print("starting");
18     var m = new Monitor();
19     
20     m.add("/home/alan/gitlive");
21     m.start();
22     loop.run ();
23
24     return 0;
25
26 }
27
28
29 public class  MonitorNamePathDir {
30     
31     public string name;
32     public string path;
33     public string dir;
34     
35     public MonitorNamePathDir(string name, string path, string dir)
36     {
37         this.name = name;
38         this.path = path;
39         this.dir = dir;
40         
41     }
42 }
43
44
45
46 public delegate void onEventHander (FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type);
47
48
49 /**
50  * Monitor class - handles monitor managment for a large tree...
51  *
52  *
53  * This 
54  * 
55  * usage : 
56  * x = new Monitor({
57      change : function () {
58          * ....
59          *}
60   }
61  * x.add('/somepath')
62  * x.stop() // stops all scanning.
63  * x.play(); // starts the scanning
64  * 
65  * 
66  * 
67  * 
68  */
69  
70 public class Monitor : Object
71 {
72
73
74
75     public Monitor()
76     {
77        
78      
79         this.monitors = new Array<FileMonitor> ();
80         this.top = new Array<string> ();
81         this.paused = false;
82     }
83      
84     public Array<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
85     public Array<string> top; // list of top level directories..
86     public bool paused;
87     /**
88      * add a directory or file to monitor
89      */
90     public void add (string add)
91     {
92         this.top.append_val(add);
93     }
94     /**
95      * start monitoring
96      */
97     public void start()
98     {
99         for(int i = 0; i < this.top.length ; i++) {
100             this.monitor(this.top.index(i));
101         }
102     }
103     /**
104      * stop / pause monitoring
105      * 
106      */
107     public void stop()
108     {
109         
110         for(int i = 0; i < this.monitors.length ; i++) {
111             this.monitors.index(i).cancel();
112         } 
113         this.monitors = new Array<FileMonitor>(); // clean /destroy/ kill old?
114     }
115     /**
116      * pause monitoring - without changing what's monitored 
117      */
118     public void pause()
119     {
120         this.paused = true;
121     }
122     /**
123      * resume monitoring - without changing what's monitored 
124      */
125     public void resume()
126     {
127         this.paused = false;
128     }
129     /**
130      * monitor a file or directory (privatish)
131      *
132      * initially called with ~/gitlive  null 0 (effectvely)
133      * 
134      * 
135      */
136     public void monitor(string path, int depth = 0)
137     {
138          
139         print("ADD: " + path + "\n");
140         
141         //depth = typeof(depth) == 'number'  ? depth *1 : 0;
142         depth = depth > 0  ? depth *1 : 0;
143         
144         
145         //fn = fn || function (fm, f, of, event_type, uh) {
146         //    _this.onEvent(fm, f, of, event_type, uh);
147         //}
148        
149           
150         var f = File.new_for_path(path);
151             //var cancel = new Gio.Cancellable ();
152         if (depth > 0) { 
153   
154             try {
155   
156                  var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
157  
158                  fm.changed.connect( ( fm,  f_orig,  of_orig,  event_type) => {
159                         //if (fn) {
160                             this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
161                            // return;
162                         //}
163                         //this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
164                 });
165                 this.monitors.append_val(fm);
166
167             } catch (Error e) {
168                 // FIXME -- show error? do nothing..            
169             }
170             // print("ADD path " + depth + ' ' + path);
171         }
172         // iterate children?
173         // - this is not used.
174         //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
175             
176         //    this.initRepo(path);
177         //}
178         FileEnumerator file_enum;
179         try {      
180             file_enum = f.enumerate_children(
181                FileAttribute.STANDARD_DISPLAY_NAME + "," +   FileAttribute.STANDARD_TYPE,
182                0, // FileQueryInfoFlags.NONE,
183                null);
184         } catch (Error e) {
185             // FIXME - show error..
186             return;
187         }
188         FileInfo next_file;
189         
190         while (true) {
191             try {        
192                 next_file = file_enum.next_file(null);
193             } catch (Error e) {
194                 break;
195             }
196             if (next_file == null) {
197                 break;
198             }
199             //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
200             
201             if (next_file.get_file_type() != FileType.DIRECTORY) {
202                 next_file = null;
203                 continue;
204             }
205             
206             if (next_file.get_file_type() ==FileType.SYMBOLIC_LINK) {
207                 next_file = null;
208                 continue;
209             }
210             
211             if (next_file.get_display_name()[0] == '.') {
212                 next_file = null;
213                 continue;
214             }
215             var sp = path+"/"+next_file.get_display_name();
216             // skip modules.
217             //print("got a file : " + sp);
218          
219             next_file = null;
220             
221             
222             
223             this.monitor(sp, depth + 1);
224             
225         }
226         try {
227             file_enum.close(null);
228         } catch(Error e) {
229             // ignore?
230         }
231     }
232     
233     
234     
235     public File realpath(File file)
236     {
237         if (file != null) {
238             return file;
239         }
240         
241         if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
242             var rp = Posix.realpath(file.get_path());
243             return File.new_for_path(rp);  
244             
245         }
246         // file does not currently exist..
247         // check parent.
248         
249 // FIX ME - string split?/? 
250         var bn = file.get_basename();
251         var ar =  file.get_path().split("/");
252         ar.resize(ar.length-1);
253         var dirname = string.joinv("/",ar );
254         var rp = Posix.realpath(dirname);
255         return File.new_for_path(rp + "/" + bn);
256         
257     }
258    
259     
260     
261      
262     public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
263     {
264         if (this.paused) {
265             return;
266         }
267         print("onEvent");
268         var f = this.realpath(f_orig);
269         
270         var of = this.realpath(of_orig);
271  
272         
273  
274         MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
275         MonitorNamePathDir dest = null;
276         
277         if (of != null) {
278             dest = new MonitorNamePathDir( of.get_basename(), of.get_path(),  Path.get_dirname(of.get_path()));
279             
280         }
281         //string event_name = "UKNOWN";
282         
283         
284         // extract the event names ... - not sure if introspection is feasible in vala..
285         //for(var i in Gio.FileMonitorEvent) {
286          //    if (Gio.FileMonitorEvent[i] == event_type) {
287          //        event_name = i;
288          //    }
289          //}
290         
291
292
293
294         //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
295         //print ("got src: " + src.toString());
296         //print ("got event: " + src.toString());
297         try {
298                 
299             switch(event_type) {
300                 case FileMonitorEvent.CHANGED:
301                     this.onChanged(src);
302                     return; // ingore thise?? -wait for changes_done_htin?
303                     
304                 case FileMonitorEvent.CHANGES_DONE_HINT:
305                     this.onChangesDoneHint(src);
306                     return;
307                     
308                 case FileMonitorEvent.DELETED:
309                     this.onDeleted(src);
310                     return;
311                     
312                 case FileMonitorEvent.CREATED:
313                     this.onCreated(src);
314                     return;
315                 
316                 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
317                     this.onAttributeChanged(src);
318                     return;
319                 
320                 case FileMonitorEvent.MOVED: // eg. chmod/chatt
321                     this.onMoved(src,dest);
322                     return; 
323                 
324                 // rest are mount related - not really relivant.. maybe add later..
325             } 
326         } catch(Error e) {
327             print(e.message);
328         }
329         
330     }
331     
332     /** override these to do stuff.. */
333     public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
334     public void onChanged(MonitorNamePathDir src) { }
335     public void onChangesDoneHint(MonitorNamePathDir src) { }
336     public void onDeleted(MonitorNamePathDir src) { }
337     public void onCreated(MonitorNamePathDir src) { }
338     public void onAttributeChanged(MonitorNamePathDir src) { }
339     public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }
340           
341     
342 }
343  
344  
345
346
347
348
349