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);
155 var enumerator = f.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
158 var file_enum = f.enumerate_children(
159 FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ FILE_ATTRIBUTE_STANDARD_TYPE,
160 0, // FileQueryInfoFlags.NONE,
165 while ((next_file = file_enum.next_file(null)) != null) {
167 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
169 if (next_file.get_file_type() != FILETYPE_DIRECTORY) {
174 if (next_file.get_file_type() == FILETYPE_SYMBOLIC_LINK) {
179 if (next_file.get_display_name()[0] == '.') {
183 var sp = path+'/'+next_file.get_display_name();
185 //print("got a file : " + sp);
191 this.monitor(sp, fn, depth + 1);
195 file_enum.close(null);
200 public File realpath(File file)
206 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
207 var rp = Posix.realpath(file.get_path());
208 return File.new_for_path(rp);
211 // file does not currently exist..
214 // FIX ME - string split?/?
215 var bn = file.get_basename();
216 var ar = file.get_path().split("/");
217 ar.resize(ar.length-1);
218 var dirname = string.joinv("/",ar );
219 var rp = Posix.realpath(dirname);
220 return File.new_for_path(rp + "/" + bn);
227 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
233 var f = this.realpath(f_orig);
235 var of = this.realpath(of_orig);
239 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , Path.get_dirname(f.get_path()));
240 MonitorNamePathDir dest = null;
243 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), Path.get_dirname(of.get_path()));
246 string event_name = "UKNOWN";
249 // extract the event names ... - not sure if introspection is feasible in vala..
250 //for(var i in Gio.FileMonitorEvent) {
251 // if (Gio.FileMonitorEvent[i] == event_type) {
256 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
257 //print ("got src: " + src.toString());
258 //print ("got event: " + src.toString());
262 case FileMonitorEvent.CHANGED:
264 return; // ingore thise?? -wait for changes_done_htin?
266 case FileMonitorEvent.CHANGES_DONE_HINT:
267 this.onChangesDoneHint(src);
270 case FileMonitorEvent.DELETED:
274 case FileMonitorEvent.CREATED:
278 case FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
279 this.onAttributeChanged(src);
282 case FileMonitorEvent.MOVED: // eg. chmod/chatt
283 this.onMoved(src,dest);
286 // rest are mount related - not really relivant.. maybe add later..
294 /** override these to do stuff.. */
295 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
296 public void onChanged(MonitorNamePathDir src) { }
297 public void onChangesDoneHint(MonitorNamePathDir src) { }
298 public void onDeleted(MonitorNamePathDir src) { }
299 public void onCreated(MonitorNamePathDir src) { }
300 public void onAttributeChanged(MonitorNamePathDir src) { }
301 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest) { }