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;
8 /// # valac --pkg gee-0.8 --pkg gio-2.0 --pkg posix Monitor.val
11 using Gee; // for array list?
13 static int main (string[] args) {
14 // A reference to our file
15 var file = File.new_for_path ("data.txt");
21 public class MonitorNamePathDir {
27 public MonitorNamePathDir(string name, string path, string dir)
37 * Monitor class - handles monitor managment for a large tree...
44 change : function () {
49 * x.stop() // stops all scanning.
50 * x.play(); // starts the scanning
57 public class Monitor : Object
66 this.monitors = new ArrayList<FileMonitor> ();
67 this.top = new ArrayList<string> ();
71 public ArrayList<FileMonitor> monitors;// Array of MonitorNamePathDirileMonitors
72 public ArrayList<string> top; // list of top level directories..
75 * add a directory or file to monitor
77 public void add (string add)
86 foreach(var in in this.top) {
87 this.monitor(this.top[i]);
91 * stop / pause monitoring
97 foreach(var i in this.monitors) {
98 this.monitors[i].cancel();
100 this.monitors = new ArrayList<FileMonitor>(); // clean /destroy/ kill old?
103 * pause monitoring - without changing what's monitored
110 * resume monitoring - without changing what's monitored
117 * monitor a file or directory (privatish)
119 * initially called with ~/gitlive null 0 (effectvely)
123 public void monitor(string path, string fn, int depth)
126 // print("ADD: " + path)
128 //depth = typeof(depth) == 'number' ? depth *1 : 0;
129 depth = depth > 0 ? depth *1 : 0;
132 //fn = fn || function (fm, f, of, event_type, uh) {
133 // _this.onEvent(fm, f, of, event_type, uh);
137 var f = File.new_for_path(path);
138 //var cancel = new Gio.Cancellable ();
140 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
142 fm.changed.connect( ( fm, f_orig, of_orig, event_type) => {
143 this.onEvent (fm, f_orig, of_orig, event_type ) ;
145 this.monitors.add(fm);
146 // print("ADD path " + depth + ' ' + path);
149 // - this is not used.
150 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
152 // this.initRepo(path);
156 var file_enum = f.enumerate_children(
157 FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE,
158 0, // FileQueryInfoFlags.NONE,
163 while ((next_file = file_enum.next_file(null)) != null) {
165 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
167 if (next_file.get_file_type() != FILETYPE_DIRECTORY) {
172 if (next_file.get_file_type() == FILETYPE_SYMBOLIC_LINK) {
177 if (next_file.get_display_name()[0] == '.') {
181 var sp = path+'/'+next_file.get_display_name();
183 //print("got a file : " + sp);
189 this.monitor(sp, fn, depth + 1);
193 file_enum.close(null);
198 public File realpath(File file)
204 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
205 var rp = Posix.realpath(file.get_path());
206 return File.new_for_path(rp);
209 // file does not currently exist..
212 // FIX ME - string split?/?
213 var bn = file.get_basename();
214 var ar = file.get_path().split("/");
215 ar.resize(ar.length-1);
216 var dirname = string.joinv("/",ar );
217 var rp = Posix.realpath(dirname);
218 return File.new_for_path(rp + "/" + bn);
225 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
231 var f = this.realpath(f_orig);
233 var of = this.realpath(of_orig);
237 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
238 MonitorNamePathDir dest = null;
241 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
244 string event_name = "UKNOWN";
247 // extract the event names ... - not sure if introspection is feasible in vala..
248 //for(var i in Gio.FileMonitorEvent) {
249 // if (Gio.FileMonitorEvent[i] == event_type) {
254 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
255 //print ("got src: " + src.toString());
256 //print ("got event: " + src.toString());
260 case FileMonitorEvent.CHANGED:
262 return; // ingore thise?? -wait for changes_done_htin?
264 case FileMonitorEvent.CHANGES_DONE_HINT:
265 this.onChangesDoneHint(src);
268 case FileMonitorEvent.DELETED:
272 case FileMonitorEvent.CREATED:
276 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
277 this.onAttributeChanged(src);
280 case FileMonitorEvent.MOVED: // eg. chmod/chatt
281 this.onMoved(src,dest);
284 // rest are mount related - not really relivant.. maybe add later..
292 /** override these to do stuff.. */
293 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
294 public void onChanged(MonitorNamePathDir src) { }
295 public void onChangesDoneHint(MonitorNamePathDir src) { }
296 public void onDeleted(MonitorNamePathDir src) { }
297 public void onCreated(MonitorNamePathDir src) { }
298 public void onAttributeChanged(MonitorNamePathDir src) { }
299 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }