tests/soup.js
[gitlive] / Monitor.js
1 //<Script type="text/javascript">
2 var Gio      = imports.gi.Gio;
3 var GLib      = imports.gi.GLib;
4
5
6 /**
7  * Monitor class - handles monitor managment for a large tree...
8  * 
9  * usage : 
10  * x = new Monitor({
11      change : function () {
12          * ....
13          *}
14   }
15  * x.add('/somepath')
16  * x.stop() // stops all scanning.
17  * x.play(); // starts the scanning
18  * 
19  * 
20  * 
21  * 
22  */
23  
24  
25 function Monitor(cfg){
26     for (var i in cfg) {
27         this[i] = cfg[i];
28     }
29     
30     
31     this.monitors = [];
32     
33     this.top = [];
34     
35 }
36
37 Monitor.prototype = {
38     
39     monitors : false, // Array of GioFileMonitors
40     top : false, // list of top level directories..
41     paused : false,
42     /**
43      * add a directory or file to monitor
44      */
45     add : function(add)
46     {
47         this.top.push(add);
48     },
49     /**
50      * start monitoring
51      */
52     start : function()
53     {
54         this.top.forEach(this.monitor, this);
55     },
56     /**
57      * stop / pause monitoring
58      * 
59      */
60     stop : function()
61     {
62         this.monitors.forEach(function(m) {
63             m.cancel();
64         })
65         this.monitors = [];
66     },
67     /**
68      * pause monitoring - without changing what's monitored 
69      */
70     pause : function()
71     {
72         this.paused = true;
73     },
74     /**
75      * resume monitoring - without changing what's monitored 
76      */
77     resume : function()
78     {
79         this.paused = false;
80     },
81     /**
82      * monitor a file or directory (privatish)
83      *
84      * initially called with ~/gitlive  null 0 (effectvely)
85      * 
86      * 
87      */
88     monitor : function(path, fn, depth)
89     {
90         var _this = this;
91         
92        // print("ADD: " + path)
93         
94         depth = typeof(depth) == 'number'  ? depth *1 : 0;
95         
96         
97         fn = fn || function (fm, f, of, event_type, uh) {
98             _this.onEvent(fm, f, of, event_type, uh);
99         }
100        
101         // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
102         if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
103             return;
104         }
105             
106        
107        
108         
109         var f = Gio.file_new_for_path(path);
110             //var cancel = new Gio.Cancellable ();
111         if (depth > 0) {     
112             var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
113             fm.signal.changed.connect(fn);
114             this.monitors.push(fm);
115             // print("ADD path " + depth + ' ' + path);
116         }
117         // iterate children?
118         
119         if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
120             
121             this.initRepo(path);
122         }
123         
124         
125         var file_enum = f.enumerate_children(
126             Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ 
127             Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
128             Gio.FileQueryInfoFlags.NONE,
129             null);
130         
131        
132         
133         while ((next_file = file_enum.next_file(null)) != null) {
134          
135             if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
136                 continue;
137             }
138             
139             if (next_file.get_file_type() == Gio.FileType.SYMBOLIC_LINK) {
140                 continue;
141             }
142             
143             if (next_file.get_display_name()[0] == '.') {
144                 continue;
145             }
146             var sp = path+'/'+next_file.get_display_name();
147             // skip modules.
148            
149             
150             this.monitor(sp, fn, depth + 1)
151         }
152     
153         file_enum.close(null);
154     },
155     
156     
157     
158     realpath : function(file)
159     {
160         if (!file) {
161             return file;
162         }
163         
164         if (GLib.file_test(file.get_path(), GLib.FileTest.EXISTS)) {
165             var rp = imports.os.realpath(file.get_path());
166             return Gio.file_new_for_path(rp);  
167             
168         }
169         // file does not currently exist..
170         // check parent.
171         var bn = file.get_basename();
172         var ar = file.get_path().split('/');
173         ar.pop();
174         var dirname = ar.join('/');
175         var rp = imports.os.realpath(dirname);
176         return Gio.file_new_for_path(rp + '/' + bn);
177         
178         
179     },
180     
181     
182     
183     onEvent : function(fm, f_orig, of_orig, event_type, uh)
184     {
185         if (this.paused) {
186             return;
187         }
188         
189         var f = this.realpath(f_orig);
190         
191         var of = this.realpath(of_orig);
192  
193         var src = {
194             name : f.get_basename(),
195             path : f.get_path(),
196             dir   : GLib.path_get_dirname(f.get_path())
197         };
198         
199         var dest = false;
200         
201         if (of) {
202             
203             dest =  {
204                 name : of.get_basename(),
205                 path : of.get_path(),
206                 dir   : GLib.path_get_dirname(of.get_path())
207             }
208         }
209         
210         
211         for(var i in Gio.FileMonitorEvent) {
212             if (Gio.FileMonitorEvent[i] == event_type) {
213                 event_name = i;
214             }
215         }
216         
217         //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
218         //print ("got src: " + src.toString());
219         //print ("got event: " + src.toString());
220         try {
221                 
222             switch(event_type) {
223                 case Gio.FileMonitorEvent.CHANGED:
224                     this.onChanged(src);
225                     return; // ingore thise?? -wait for changes_done_htin?
226                     
227                 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
228                     this.onChangesDoneHint(src);
229                     return;
230                     
231                 case Gio.FileMonitorEvent.DELETED:
232                     this.onDeleted(src);
233                     return;
234                     
235                 case Gio.FileMonitorEvent.CREATED:
236                     this.onCreated(src);
237                     return;
238                 
239                 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
240                     this.onAttributeChanged(src);
241                     return;
242                 
243                 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
244                     this.onMoved(src,dest);
245                     return; 
246                 
247                 // rest are mount related - not really relivant.. maybe add later..
248             } 
249         } catch(e) {
250             print(e);
251         }
252         
253     },
254     
255     /** override these to do stuff.. */
256     initRepo : function(src) { }, // called on startup at the top level repo dir.
257     onChanged : function(src) { },
258     onChangesDoneHint : function(src) { },
259     onDeleted : function(src) { },
260     onCreated : function(src) { },
261     onAttributeChanged : function(src) { },
262     onMoved : function(src) { }
263           
264     
265 }
266  
267  
268
269
270
271
272