1 //<Script type="text/javascript">
2 var Gio = imports.gi.Gio;
3 var GLib = imports.gi.GLib;
5 var XObject = imports.XObject.XObject;
6 var File = imports.File.File;
9 * Monitor class - handles monitor managment for a large tree...
16 change : function () {
21 * x.stop() // stops all scanning.
22 * x.play(); // starts the scanning
29 function Monitor(cfg){
43 monitors : false, // Array of GioFileMonitors
44 top : false, // list of top level directories..
47 * add a directory or file to monitor
58 for(var i =0;i < this.top.length; i++) {
59 this.monitor(this.top[i]);
63 * stop / pause monitoring
69 for(var i =0;i < this.monitors.length; i++) {
70 this.monitors[i].cancel();
75 * pause monitoring - without changing what's monitored
82 * resume monitoring - without changing what's monitored
89 * monitor a file or directory (privatish)
91 * initially called with ~/gitlive null 0 (effectvely)
95 monitor : function(path, fn, depth)
99 // print("ADD: " + path)
101 depth = typeof(depth) == 'number' ? depth *1 : 0;
104 fn = fn || function (fm, f, of, event_type, uh) {
105 _this.onEvent(fm, f, of, event_type, uh);
109 var f = Gio.file_new_for_path(path);
110 //var cancel = new Gio.Cancellable ();
112 var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
114 XObject.isSeed ? fm.signal.changed.connect(fn) : fm.connect('changed',fn);
115 this.monitors.push(fm);
116 // print("ADD path " + depth + ' ' + path);
119 // - this is not used.
120 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
122 // this.initRepo(path);
125 var file_enum = f.enumerate_children(
126 Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+
127 Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
128 Gio.FileQueryInfoFlags.NONE,
133 while ((next_file = file_enum.next_file(null)) != null) {
135 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
137 if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
142 if (next_file.get_file_type() == Gio.FileType.SYMBOLIC_LINK) {
147 if (next_file.get_display_name()[0] == '.') {
151 var sp = path+'/'+next_file.get_display_name();
153 //print("got a file : " + sp);
159 this.monitor(sp, fn, depth + 1);
163 file_enum.close(null);
168 realpath : function(file)
174 if (GLib.file_test(file.get_path(), GLib.FileTest.EXISTS)) {
175 var rp = File.realpath(file.get_path());
176 return Gio.file_new_for_path(rp);
179 // file does not currently exist..
181 var bn = file.get_basename();
182 var ar = file.get_path().split('/');
184 var dirname = ar.join('/');
185 var rp = File.realpath(dirname);
186 return Gio.file_new_for_path(rp + '/' + bn);
194 onEvent : function(fm, f_orig, of_orig, event_type, uh)
200 var f = this.realpath(f_orig);
202 var of = this.realpath(of_orig);
205 name : f.get_basename(),
207 dir : GLib.path_get_dirname(f.get_path())
215 name : of.get_basename(),
216 path : of.get_path(),
217 dir : GLib.path_get_dirname(of.get_path())
220 var event_name = 'UKNOWN';;
222 for(var i in Gio.FileMonitorEvent) {
223 if (Gio.FileMonitorEvent[i] == event_type) {
228 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
229 //print ("got src: " + src.toString());
230 //print ("got event: " + src.toString());
234 case Gio.FileMonitorEvent.CHANGED:
236 return; // ingore thise?? -wait for changes_done_htin?
238 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
239 this.onChangesDoneHint(src);
242 case Gio.FileMonitorEvent.DELETED:
246 case Gio.FileMonitorEvent.CREATED:
250 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
251 this.onAttributeChanged(src);
254 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
255 this.onMoved(src,dest);
258 // rest are mount related - not really relivant.. maybe add later..
266 /** override these to do stuff.. */
267 initRepo : function(src) { }, // called on startup at the top level repo dir.
268 onChanged : function(src) { },
269 onChangesDoneHint : function(src) { },
270 onDeleted : function(src) { },
271 onCreated : function(src) { },
272 onAttributeChanged : function(src) { },
273 onMoved : function(src) { }