Monitor.js
[gitlive] / Monitor.js
1 //<Script type="text/javascript">
2 /**
3  * Monitor class - handles monitor managment for a large tree...
4  * 
5  * usage : 
6  * x = new Monitor({
7      change : function () {
8          * ....
9          *}
10   }
11  * x.add('/somepath')
12  * x.stop() // stops all scanning.
13  * x.play(); // starts the scanning
14  * 
15  * 
16  * 
17  * 
18  */
19  
20  
21 Monitor = function(cfg){
22     for (var i in cfg) {
23         this[i] = cfg[i];
24     }
25     
26     
27     this.monitors = [];
28     
29     this.top = [];
30     
31 }
32
33 Monitor.prototype = {
34     
35     monitors : false, // Array of GioFileMonitors
36     top : false, // list of top level directories..
37     /**
38      * add a directory or file to monitor
39      */
40     add : function(add)
41     {
42         this.top.push(add);
43     },
44     /**
45      * start monitoring
46      */
47     start : function()
48     {
49         this.top.forEach(this.monitor, this);
50     },
51     /**
52      * stop / pause monitoring
53      * 
54      */
55     stop : function()
56     {
57         this.monitors.foreach(function(m) {
58             m.cancel();
59         })
60         this.monitors = [];
61     },
62     /**
63      * monitor a file or directory (privatish)
64      * 
65      * 
66      */
67     function monitor(path, fn)
68     {
69         var _this = this;
70         fn = fn || function (fm, f, of, event_type, uh) {
71             _this.onEvent(fm, f, of, event_type, uh);
72         }
73        
74         var f = Gio.file_new_for_path(path);
75         //var cancel = new Gio.Cancellable ();
76         var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
77         fm.signal.changed.connect(fn);
78         this.monitors.push(fm);
79         // iterate children?
80         
81         var file_enum = f.enumerate_children(
82             Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ 
83             Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
84             Gio.FileQueryInfoFlags.NONE,
85             null);
86             
87         while ((next_file = file_enum.next_file(null)) != null) {
88          
89             if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
90                 continue;
91             }
92             if (next_file.get_display_name()[0] == '.') {
93                 continue;
94             }
95             this.monitor(path+'/'+next_file.get_display_name(), fn)
96         }
97     
98         file_enum.close(null);
99     }
100     
101     function onEvent(fm, f, of, event_type, uh)
102     {
103         var src = {
104             name : f.get_basename(),
105             path : f.get_path(),
106         };
107         var dest = of ? {
108             name : of.get_basename(),
109             path : of.get_path(),
110         } : false;
111          
112         switch(event_type) {
113             case Gio.FileMonitorEvent.CHANGED:
114                 this.onChanged(src);
115                 return; // ingore thise?? -wait for changes_done_htin?
116                 
117             case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
118                 this.onChangesDoneHint(src);
119                 return;
120                 
121             case Gio.FileMonitorEvent.DELETED:
122                 this.onDeleted(src);
123                 return;
124                 
125             case Gio.FileMonitorEvent.CREATED:
126                 this.onCreated(src);
127                 return;
128             
129             case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
130                 this.onAttributeCreated(src);
131                 return;
132             
133             case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
134                 this.onMoved(src,dest);
135                 return; 
136             
137             // rest are mount related - not really relivant.. maybe add later..
138                 
139         } 
140     },
141     /** override these to do stuff.. */
142      
143     function onChanged(src) { },
144     function onChangesDoneHint(src) { },
145     function onDeleted(src) { },
146     function onCreated(src) { },
147     function onMoved(src) { },
148           
149     
150 }
151  
152  
153
154
155
156
157