1 //<Script type="text/javascript">
2 var Gio = imports.gi.Gio;
3 var GLib = imports.gi.GLib;
7 * Monitor class - handles monitor managment for a large tree...
14 change : function () {
19 * x.stop() // stops all scanning.
20 * x.play(); // starts the scanning
28 function Monitor(cfg){
42 monitors : false, // Array of GioFileMonitors
43 top : false, // list of top level directories..
46 * add a directory or file to monitor
57 this.top.forEach(this.monitor, this);
60 * stop / pause monitoring
65 this.monitors.forEach(function(m) {
71 * pause monitoring - without changing what's monitored
78 * resume monitoring - without changing what's monitored
85 * monitor a file or directory (privatish)
87 * initially called with ~/gitlive null 0 (effectvely)
91 monitor : function(path, fn, depth)
95 // print("ADD: " + path)
97 depth = typeof(depth) == 'number' ? depth *1 : 0;
100 fn = fn || function (fm, f, of, event_type, uh) {
101 _this.onEvent(fm, f, of, event_type, uh);
105 var f = Gio.file_new_for_path(path);
106 //var cancel = new Gio.Cancellable ();
108 var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
109 fm.signal.changed.connect(fn);
110 this.monitors.push(fm);
111 // print("ADD path " + depth + ' ' + path);
114 // - this is not used.
115 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
117 // this.initRepo(path);
121 var file_enum = f.enumerate_children(
122 Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+
123 Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
124 Gio.FileQueryInfoFlags.NONE,
129 while ((next_file = file_enum.next_file(null)) != null) {
131 if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
135 if (next_file.get_file_type() == Gio.FileType.SYMBOLIC_LINK) {
139 if (next_file.get_display_name()[0] == '.') {
142 var sp = path+'/'+next_file.get_display_name();
146 this.monitor(sp, fn, depth + 1)
149 file_enum.close(null);
154 realpath : function(file)
160 if (GLib.file_test(file.get_path(), GLib.FileTest.EXISTS)) {
161 var rp = imports.os.realpath(file.get_path());
162 return Gio.file_new_for_path(rp);
165 // file does not currently exist..
167 var bn = file.get_basename();
168 var ar = file.get_path().split('/');
170 var dirname = ar.join('/');
171 var rp = imports.os.realpath(dirname);
172 return Gio.file_new_for_path(rp + '/' + bn);
179 onEvent : function(fm, f_orig, of_orig, event_type, uh)
185 var f = this.realpath(f_orig);
187 var of = this.realpath(of_orig);
190 name : f.get_basename(),
192 dir : GLib.path_get_dirname(f.get_path())
200 name : of.get_basename(),
201 path : of.get_path(),
202 dir : GLib.path_get_dirname(of.get_path())
207 for(var i in Gio.FileMonitorEvent) {
208 if (Gio.FileMonitorEvent[i] == event_type) {
213 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
214 //print ("got src: " + src.toString());
215 //print ("got event: " + src.toString());
219 case Gio.FileMonitorEvent.CHANGED:
221 return; // ingore thise?? -wait for changes_done_htin?
223 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
224 this.onChangesDoneHint(src);
227 case Gio.FileMonitorEvent.DELETED:
231 case Gio.FileMonitorEvent.CREATED:
235 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
236 this.onAttributeChanged(src);
239 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
240 this.onMoved(src,dest);
243 // rest are mount related - not really relivant.. maybe add later..
251 /** override these to do stuff.. */
252 initRepo : function(src) { }, // called on startup at the top level repo dir.
253 onChanged : function(src) { },
254 onChangesDoneHint : function(src) { },
255 onDeleted : function(src) { },
256 onCreated : function(src) { },
257 onAttributeChanged : function(src) { },
258 onMoved : function(src) { }