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