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...
11 change : function () {
16 * x.stop() // stops all scanning.
17 * x.play(); // starts the scanning
25 function Monitor(cfg){
39 monitors : false, // Array of GioFileMonitors
40 top : false, // list of top level directories..
43 * add a directory or file to monitor
54 this.top.forEach(this.monitor, this);
57 * stop / pause monitoring
62 this.monitors.forEach(function(m) {
68 * pause monitoring - without changing what's monitored
75 * resume monitoring - without changing what's monitored
82 * monitor a file or directory (privatish)
84 * initially called with ~/gitlive null 0 (effectvely)
88 monitor : function(path, fn, depth)
92 // print("ADD: " + path)
94 depth = typeof(depth) == 'number' ? depth *1 : 0;
97 fn = fn || function (fm, f, of, event_type, uh) {
98 _this.onEvent(fm, f, of, event_type, uh);
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)) {
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
113 fm.signal.changed.connect(fn);
114 this.monitors.push(fm);
115 // print("ADD path " + depth + ' ' + path);
119 if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
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 if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
139 if (next_file.get_file_type() == Gio.FileType.SYMBOLIC_LINK) {
143 if (next_file.get_display_name()[0] == '.') {
146 var sp = path+'/'+next_file.get_display_name();
150 this.monitor(sp, fn, depth + 1)
153 file_enum.close(null);
158 realpath : function(file)
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);
169 // file does not currently exist..
171 var bn = file.get_basename();
172 var ar = file.get_path().split('/');
174 var dirname = ar.join('/');
175 var rp = imports.os.realpath(dirname);
176 return Gio.file_new_for_path(rp + '/' + bn);
183 onEvent : function(fm, f_orig, of_orig, event_type, uh)
189 var f = this.realpath(f_orig);
191 var of = this.realpath(of_orig);
194 name : f.get_basename(),
196 dir : GLib.path_get_dirname(f.get_path())
204 name : of.get_basename(),
205 path : of.get_path(),
206 dir : GLib.path_get_dirname(of.get_path())
211 for(var i in Gio.FileMonitorEvent) {
212 if (Gio.FileMonitorEvent[i] == event_type) {
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());
223 case Gio.FileMonitorEvent.CHANGED:
225 return; // ingore thise?? -wait for changes_done_htin?
227 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
228 this.onChangesDoneHint(src);
231 case Gio.FileMonitorEvent.DELETED:
235 case Gio.FileMonitorEvent.CREATED:
239 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
240 this.onAttributeChanged(src);
243 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
244 this.onMoved(src,dest);
247 // rest are mount related - not really relivant.. maybe add later..
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) { }