sqlite3
[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      * initially called with ~/gitlive  null 0 (effectvely)
70      * 
71      * 
72      */
73     monitor : function(path, fn, depth)
74     {
75         var _this = this;
76         
77         
78         depth = typeof(depth) == 'number'  ? depth *1 : 0;
79         
80         
81         fn = fn || function (fm, f, of, event_type, uh) {
82             _this.onEvent(fm, f, of, event_type, uh);
83         }
84        
85         // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
86         if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
87             return;
88         }
89             
90        
91        
92         
93         var f = Gio.file_new_for_path(path);
94             //var cancel = new Gio.Cancellable ();
95         if (depth > 0) {     
96             var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
97             fm.signal.changed.connect(fn);
98             this.monitors.push(fm);
99             // print("ADD path " + depth + ' ' + path);
100         }
101         // iterate children?
102         
103         if (GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR) && this.initRepo) {
104             
105             this.initRepo(path);
106         }
107         
108         
109         var file_enum = f.enumerate_children(
110             Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ 
111             Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
112             Gio.FileQueryInfoFlags.NONE,
113             null);
114         
115        
116         
117         while ((next_file = file_enum.next_file(null)) != null) {
118          
119             if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
120                 continue;
121             }
122             if (next_file.get_display_name()[0] == '.') {
123                 continue;
124             }
125             var sp = path+'/'+next_file.get_display_name();
126             // skip modules.
127            
128             
129             this.monitor(sp, fn, depth + 1)
130         }
131     
132         file_enum.close(null);
133     },
134     
135     
136     onEvent : function(fm, f, of, event_type, uh)
137     {
138         var src = {
139             name : f.get_basename(),
140             path : f.get_path(),
141             dir   : GLib.path_get_dirname(f.get_path())
142         };
143         var dest = of ? {
144             name : of.get_basename(),
145             path : of.get_path(),
146             dir   : GLib.path_get_dirname(of.get_path())
147         } : false;
148         
149         
150         for(var i in Gio.FileMonitorEvent) {
151             if (Gio.FileMonitorEvent[i] == event_type) {
152                 event_name = i;
153             }
154         }
155         //print ("got event: " +event_name);
156         //print ("got src: " + src.toString());
157         //print ("got event: " + src.toString());
158         try {
159                 
160             switch(event_type) {
161                 case Gio.FileMonitorEvent.CHANGED:
162                     this.onChanged(src);
163                     return; // ingore thise?? -wait for changes_done_htin?
164                     
165                 case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
166                     this.onChangesDoneHint(src);
167                     return;
168                     
169                 case Gio.FileMonitorEvent.DELETED:
170                     this.onDeleted(src);
171                     return;
172                     
173                 case Gio.FileMonitorEvent.CREATED:
174                     this.onCreated(src);
175                     return;
176                 
177                 case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
178                     this.onAttributeChanged(src);
179                     return;
180                 
181                 case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
182                     this.onMoved(src,dest);
183                     return; 
184                 
185                 // rest are mount related - not really relivant.. maybe add later..
186             } 
187         } catch(e) {
188             print(e);
189         }
190         
191     },
192     
193     /** override these to do stuff.. */
194     initRepo : function(src) { }, // called on startup at the top level repo dir.
195     onChanged : function(src) { },
196     onChangesDoneHint : function(src) { },
197     onDeleted : function(src) { },
198     onCreated : function(src) { },
199     onAttributeChanged : function(src) { },
200     onMoved : function(src) { }
201           
202     
203 }
204  
205  
206
207
208
209
210