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