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