Monitor.js
[gitlive] / Monitor.js
1 //<Script type="text/javascript">
2 Gio      = imports.gi.Gio;
3 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 Monitor = function(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)
72     {
73         var _this = this;
74         fn = fn || function (fm, f, of, event_type, uh) {
75             _this.onEvent(fm, f, of, event_type, uh);
76         }
77        
78         var f = Gio.file_new_for_path(path);
79         //var cancel = new Gio.Cancellable ();
80         var fm = f.monitor(2,null); //Gio.FileMonitorFlags.SEND_MOVED
81         fm.signal.changed.connect(fn);
82         this.monitors.push(fm);
83         // iterate children?
84         
85         var file_enum = f.enumerate_children(
86             Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ','+ 
87             Gio.FILE_ATTRIBUTE_STANDARD_TYPE,
88             Gio.FileQueryInfoFlags.NONE,
89             null);
90             
91         while ((next_file = file_enum.next_file(null)) != null) {
92          
93             if (next_file.get_file_type() != Gio.FileType.DIRECTORY) {
94                 continue;
95             }
96             if (next_file.get_display_name()[0] == '.') {
97                 continue;
98             }
99             this.monitor(path+'/'+next_file.get_display_name(), fn)
100         }
101     
102         file_enum.close(null);
103     },
104     
105     onEvent : function(fm, f, of, event_type, uh)
106     {
107         var src = {
108             name : f.get_basename(),
109             path : f.get_path(),
110             dir   : GLib.path_get_dirname(of.get_path())
111         };
112         var dest = of ? {
113             name : of.get_basename(),
114             path : of.get_path(),
115             dir   : GLib.path_get_dirname(of.get_path())
116         } : false;
117          
118         switch(event_type) {
119             case Gio.FileMonitorEvent.CHANGED:
120                 this.onChanged(src);
121                 return; // ingore thise?? -wait for changes_done_htin?
122                 
123             case Gio.FileMonitorEvent.CHANGES_DONE_HINT:
124                 this.onChangesDoneHint(src);
125                 return;
126                 
127             case Gio.FileMonitorEvent.DELETED:
128                 this.onDeleted(src);
129                 return;
130                 
131             case Gio.FileMonitorEvent.CREATED:
132                 this.onCreated(src);
133                 return;
134             
135             case Gio.FileMonitorEvent.ATTRIBUTE_CHANGED: // eg. chmod/chatt
136                 this.onAttributeCreated(src);
137                 return;
138             
139             case Gio.FileMonitorEvent.MOVED: // eg. chmod/chatt
140                 this.onMoved(src,dest);
141                 return; 
142             
143             // rest are mount related - not really relivant.. maybe add later..
144                 
145         } 
146     },
147     /** override these to do stuff.. */
148      
149     onChanged : function(src) { },
150     onChangesDoneHint : function(src) { },
151     onDeleted : function(src) { },
152     onCreated : function(src) { },
153     onAttributeChanged : function(src) { },
154     onMoved : function(src) { }
155           
156     
157 }
158  
159  
160
161
162
163
164