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