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;
9 using Gee; // for array list?
11 static int main (string[] args) {
12 // A reference to our file
13 var file = File.new_for_path ("data.txt");
19 class MonitorNamePathDir {
25 public MonitorNamePathDir(string name, string path, string dir)
35 * Monitor class - handles monitor managment for a large tree...
42 change : function () {
47 * x.stop() // stops all scanning.
48 * x.play(); // starts the scanning
55 public class Monitor : Object
64 this.monitors = new ArrayList<FileMonitor> ();
65 this.top = new ArrayList<string> ();
69 public ArrayList<FileMonitor> monitors;// Array of GioFileMonitors
70 public ArrayList<string> top; // list of top level directories..
73 * add a directory or file to monitor
75 public void add (string add)
84 foreach(var in in this.top) {
85 this.monitor(this.top[i]);
89 * stop / pause monitoring
95 foreach(var i in this.monitors) {
96 this.monitors[i].cancel();
98 this.monitors = new ArrayList<FileMonitor>(); // clean /destroy/ kill old?
101 * pause monitoring - without changing what's monitored
108 * resume monitoring - without changing what's monitored
115 * monitor a file or directory (privatish)
117 * initially called with ~/gitlive null 0 (effectvely)
121 public void monitor(string path, string fn, int depth)
124 // print("ADD: " + path)
126 //depth = typeof(depth) == 'number' ? depth *1 : 0;
127 depth = depth > 0 ? depth *1 : 0;
130 //fn = fn || function (fm, f, of, event_type, uh) {
131 // _this.onEvent(fm, f, of, event_type, uh);
135 var f = File.new_for_path(path);
136 //var cancel = new Gio.Cancellable ();
138 var fm = f.monitor(FileMonitorFlags.SEND_MOVED,null); //Gio.FileMonitorFlags.SEND_MOVED
140 fm.changed.connect(onEvent);
141 this.monitors.add(fm);
142 // print("ADD path " + depth + ' ' + path);
145 // - this is not used.
146 //if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
148 // this.initRepo(path);
151 var enumerator = directory.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
154 var file_enum = f.enumerate_children(
155 FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ FILE_ATTRIBUTE_STANDARD_TYPE,
156 0, // FileQueryInfoFlags.NONE,
161 while ((next_file = file_enum.next_file(null)) != null) {
163 //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
165 if (next_file.get_file_type() != FILETYPE_DIRECTORY) {
170 if (next_file.get_file_type() == FILETYPE_SYMBOLIC_LINK) {
175 if (next_file.get_display_name()[0] == '.') {
179 var sp = path+'/'+next_file.get_display_name();
181 //print("got a file : " + sp);
187 this.monitor(sp, fn, depth + 1);
191 file_enum.close(null);
196 public File realpath(File file)
202 if (FileUtils.test(file.get_path(), FileTest.EXISTS)) {
203 var rp = Posix.realpath(file.get_path());
204 return File.new_for_path(rp);
207 // file does not currently exist..
210 // FIX ME - string split?/?
211 var bn = file.get_basename();
212 var ar = file.get_path().split('/');
214 var dirname = ar.join('/');
215 var rp = Posix.realpath(dirname);
216 return File.new_for_path(rp + '/' + bn);
223 public void onEvent(FileMonitor fm, File f_orig, File of_orig, FileMonitorEvent event_type)
229 var f = this.realpath(f_orig);
231 var of = this.realpath(of_orig);
235 MonitorNamePathDir src = new MonitorNamePathDir( f.get_basename(), f.get_path() , GLib.path_get_dirname(f.get_path()));
236 MonitorNamePathDir dest = null;
239 dest = new MonitorNamePathDir( of.get_basename(), of.get_path(), GLib.path_get_dirname(of.get_path()));
242 string event_name = "UKNOWN";
245 // extract the event names ... - not sure if introspection is feasible in vala..
246 //for(var i in Gio.FileMonitorEvent) {
247 // if (Gio.FileMonitorEvent[i] == event_type) {
252 //print (JSON.stringify([event_name , f.get_path(), of ? of.get_path() : false ] ));
253 //print ("got src: " + src.toString());
254 //print ("got event: " + src.toString());
258 case Gio.FileMonitorEvent.CHANGED:
260 return; // ingore thise?? -wait for changes_done_htin?
262 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
263 this.onChangesDoneHint(src);
266 case Gio.FileMonitorEvent.DELETED:
270 case Gio.FileMonitorEvent.CREATED:
274 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
275 this.onAttributeChanged(src);
278 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
279 this.onMoved(src,dest);
282 // rest are mount related - not really relivant.. maybe add later..
290 /** override these to do stuff.. */
291 public void initRepo(MonitorNamePathDir src) { } // called on startup at the top level repo dir.
292 public void onChanged(MonitorNamePathDir src) { }
293 public void onChangesDoneHint(MonitorNamePathDir src) { }
294 public void onDeleted(MonitorNamePathDir src) { }
295 public void onCreated(MonitorNamePathDir src) { }
296 public void onAttributeChanged(MonitorNamePathDir src) { }
297 public void onMoved(MonitorNamePathDir src) { }