GitMonitor.vala
[gitlive] / GitMonitor.vala
1  
2 public class GitMonitor : Monitor
3 {
4
5  
6     /**
7      * @property {String} the "gitlive" directory, normally ~/gitlive
8      *  dset by OWNER... - we should do this as a CTOR.
9      *  
10      */
11     public string gitlive = '';
12     
13     
14     public Array<GtkMonitorQueuequeue>;
15     public bool queueRunning = false;
16     
17     public DateTime lastAdd;
18      
19      
20     public void pause() {
21         this.paused = true;
22         // what does this do to the old one...
23         this.queue = new Array<FileMonitor> ();
24         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
25
26     }
27     
28     public void resume () {
29         this.paused = false;
30         this.queue = new Array<FileMonitor> ();
31         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
32         
33         
34     }
35     /**
36      * Start the monitoring
37      * and run the queue every 500 milliseconds..
38      *
39      */
40     public void start() {
41         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
42         
43          
44         this.lastAdd = new DateTime.now(); 
45         
46         Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
47
48             // call this.monitor on each of 'top'
49             for(int i = 0; i < this.top.length ; i++) {
50                 this.monitor(this.top.index(i), ( fm,  f_orig,  of_orig,  event_type) => {
51                     this.onEvent (fm,  f_orig,  of_orig,  event_type ) ;
52                 } );
53             }
54             StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
55              
56            
57             
58             try { 
59                 var notification = new Notify.Notification({
60                     "Git Live",
61                     this.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
62                      "dialog-information"
63                 });
64         
65                 notification.set_timeout(5);
66                 notification.show();
67             } catch(Error e) {
68                 print(e.toString());
69             }
70
71         });
72         
73         Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
74             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
75             if (!_this.queue.length || _this.queueRunning) {
76                 return true;
77             }
78
79             var last = this.lastAdd.difference(new DateTime.now());
80
81             
82             //print("LAST RUN?" + last);
83             
84             if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
85                 return 1;
86             }
87             //_this.lastAdd = new Date();
88             //return 1;
89         
90             this.runQueue();
91             return true;
92         },null,null);
93         
94       
95     }
96
97
98     public void stop() {
99         StatusIcon.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
100         base.stop();
101     }
102     
103     
104     public void monitor (string path, onEventHander fn , int depth = 0)
105     {
106         
107         //var depth = typeof(depth) == 'number'  ? depth *1 : 0;
108         
109          
110         // if we are not at top level.. and there is a .git directory  (it's a submodule .. ignore) 
111         if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
112             return;
113         }
114         
115         if (depth == 1) {
116             // FIXME - check if repo is flagged as not autocommit..
117             //var repo = imports.Scm.Repo.Repo.get(path);
118             //if (!repo || !repo.autocommit()) {
119             //    return;
120             //} 
121         }
122         
123         
124         // check if the repo is to be monitored.
125         //print("PATH : " + path);
126         
127         
128         base.monitor(path,fn, depth);
129     }
130
131