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());
167 return Gio.file_new_for_path(rp);
171 // file does not currently exist..
173 var bn = file.basename();
174 var ar = file.get_path().split('/');
176 var dirname = ar.join('/');
177 var rp = imports.os.realpath(dirname);
178 return Gio.file_new_for_path(rp + '/' + bn);
184 onEvent : function(fm, f, of, event_type, uh)
191 if (event_type != Gio.FileMonitorEvent.DELETED &&
192 event_type != Gio.FileMonitorEvent.MOVED) {
193 // it's not moved or deleted, and the file does not actually exist.
194 if (!GLib.file_test(f.get_path(), GLib.FileTest.EXISTS)) {
201 var rp = imports.os.realpath(f.get_path());
203 var can = rp ? Gio.file_new_for_path(rp) : f;
205 print(event_type + " : " + can.get_path() + "\n");
207 name : can.get_basename(),
208 path : can.get_path(),
209 dir : GLib.path_get_dirname(can.get_path())
215 rp = imports.os.realpath(of.get_path());
216 var can = rp ? Gio.file_new_for_path(rp) : of;
218 name : can.get_basename(),
219 path : can.get_path(),
220 dir : GLib.path_get_dirname(can.get_path())
225 for(var i in Gio.FileMonitorEvent) {
226 if (Gio.FileMonitorEvent[i] == event_type) {
230 //print ("got event: " +event_name);
231 //print ("got src: " + src.toString());
232 //print ("got event: " + src.toString());
236 case Gio.FileMonitorEvent.CHANGED:
238 return; // ingore thise?? -wait for changes_done_htin?
240 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
241 this.onChangesDoneHint(src);
244 case Gio.FileMonitorEvent.DELETED:
248 case Gio.FileMonitorEvent.CREATED:
252 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
253 this.onAttributeChanged(src);
256 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
257 this.onMoved(src,dest);
260 // rest are mount related - not really relivant.. maybe add later..
268 /** override these to do stuff.. */
269 initRepo : function(src) { }, // called on startup at the top level repo dir.
270 onChanged : function(src) { },
271 onChangesDoneHint : function(src) { },
272 onDeleted : function(src) { },
273 onCreated : function(src) { },
274 onAttributeChanged : function(src) { },
275 onMoved : function(src) { }