StatusIcon.vala
[gitlive] / StatusIcon.vala
1 /**
2  * Status icon and menu for component of gitlive.
3  * 
4  * Implements XObject - however we want to control initialization.
5  * 
6  * 
7  * 
8  * Currently only does a few things
9  * a) Quit
10  * 
11  * b) Pause!??!
12  */
13    
14 // Compile::
15 // valac --pkg gtk+-3.0 StatusIcon.vala -o /tmp/StatusIcon
16    
17 //var gitlive = imports.gitlive;
18
19
20 using Gtk;
21 /*
22
23 static int main (string[] args) {
24     // A reference to our file
25     Gtk.init (ref args);
26     new StatusIconA();
27  
28     Gtk.main ();
29     return 0;
30      
31
32 }
33  */
34
35
36 //public StatusIconA statusicon;
37  
38 public class StatusIconA : StatusIcon {
39
40     public bool paused = false;
41     public static StatusIconA statusicon;
42      
43     public StatusIconA() {
44         
45         statusicon = this;
46         
47          //title : 'gitlive',
48         this.stock = Gtk.Stock.REFRESH;
49         this.tooltip_text = "GitLive";
50         this.title = "gitlive";
51         
52         this.set_name("gitlive");
53         this.set_visible(true);      
54         
55         var menu = new MenuA();
56         menu.ref();       
57                 this.button_press_event.connect( ( ev ) =>{
58               print("button press event called\n");
59               
60             menu.show_all();
61             
62             if (this.paused) {
63                 menu.resume.show();
64                 menu.pause.hide();
65             } else {
66                 menu.resume.hide();
67                 menu.pause.show();
68             }
69               //                public void popup (Gtk.Widget? parent_menu_shell, Gtk.Widget? parent_menu_item,
70                     // [CCode (scope = "async")] Gtk.MenuPositionFunc? func, uint button, uint32 activate_time);
71             //Gtk.get_current_event_device ()
72             menu.popup(
73
74                         null, null,
75                        this.position_menu,
76                        
77                     ev.button,  ev.time //   time
78             );
79               
80               return true;
81             
82             });
83        this.popup_menu.connect( (  button,   time) =>{
84        //this.button_press_event.connect( (  ) =>{
85                
86             //print(Array.prototype.slice.call(arguments).join(','));
87             print("menu activiate called\n");
88             //var menu = this.get('menu');
89             
90             menu.show_all();
91             
92             if (this.paused) {
93                 menu.resume.show();
94                 menu.pause.hide();
95             } else {
96                 menu.resume.hide();
97                 menu.pause.show();
98             }
99               //                public void popup (Gtk.Widget? parent_menu_shell, Gtk.Widget? parent_menu_item,
100                     // [CCode (scope = "async")] Gtk.MenuPositionFunc? func, uint button, uint32 activate_time);
101             //Gtk.get_current_event_device ()
102             menu.popup(
103
104                         null, null,
105                        this.position_menu,
106                        
107                     button,  time //   time
108             );
109                         
110             
111             //var g = { };
112             //var a = new Gdk.Rectangle();
113             //  needs direction=inout setting in gir to work (in bugzilla @present)
114             //this.el.get_geometry(g,a,null);
115              
116             // should check to see if @ top or bottom I guess..
117             //menu.el.get_toplevel().move(a.x, a.y +a.height);
118             //menu.el.get_toplevel().move(10,10);
119             //return false;
120               
121         }); 
122         
123     }
124     
125     public void pause()
126     {
127                 this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
128                 this.paused = true;
129     }
130     
131     public void resume()
132     {
133                 this.set_from_stock( Gtk.Stock.MEDIA_PLAY );
134                 this.paused = false;
135     }
136     
137     public void refreshing()
138     {
139                 this.set_from_stock( Gtk.Stock.REFRESH );
140     }
141     
142     public void pauseError()
143     {
144                  this.paused = true;
145                  var flag = true;
146                  Timeout.add_full(Priority.LOW, 500, () => {
147                  
148                         if (!this.paused) {
149                                 return false;
150                         }
151                         this.set_from_stock( flag == true ? Gtk.Stock.MEDIA_RECORD  
152                                         : Gtk.Stock.MEDIA_PAUSE);
153                         flag = !flag;
154                         return true;
155                 });
156                  
157     }
158     
159     class MenuA : Gtk.Menu
160     {
161         public ImageMenuItem pause;
162         public ImageMenuItem resume;
163         
164         public MenuA()
165         {
166             this.pause = new ImageMenuItemA();
167             this.append(this.pause);
168             this.resume = new ImageMenuItemB();
169             this.append(this.resume);
170             this.append(new ImageMenuItemC());
171             this.append(new ImageMenuItemD());
172             this.append(new ImageMenuItemE());
173             this.append(new ImageMenuItemF());
174             this.append(new ImageMenuItemG());
175             
176             
177             
178         }
179         
180         
181         class ImageMenuItemA : ImageMenuItem {
182             
183             public ImageMenuItemA()
184             {
185                 //this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
186                 
187                 var  image = new Gtk.Image();
188                 image.set_from_stock(Gtk.Stock.MEDIA_PAUSE,Gtk.IconSize.MENU );
189                 this.set_image (image);
190                 
191                 this.label= "Pause Commits";
192                 this.always_show_image = true;
193                 this.accel_group = null;
194                 
195                 this.activate.connect( () => {
196                     statusicon.paused = true;
197                     GitMonitor.gitmonitor.stop();
198
199                    // this.el.label  = status ? 'Resume' : 'Pause';
200                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
201                     
202                     
203                 });
204                 //    id : 'pause',
205             }
206             
207             
208         }
209         class ImageMenuItemB : ImageMenuItem {
210             
211             public ImageMenuItemB()
212             {
213                 
214                 var  image = new Gtk.Image();
215                 image.set_from_stock(Gtk.Stock.MEDIA_PLAY,Gtk.IconSize.MENU );
216                 this.set_image (image);
217                 this.label= "Start Commits";
218                 this.always_show_image = true;
219                 this.accel_group = null;
220                 
221                 this.activate.connect( () => {
222                     GitMonitor.gitmonitor.start();
223                     statusicon.paused = false;
224                     
225                     //
226                    // this.el.label  = status ? 'Resume' : 'Pause';
227                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
228                     
229                     
230                 });
231             }
232             
233             
234         }
235         
236         
237         class ImageMenuItemC : ImageMenuItem {
238             
239             public ImageMenuItemC()
240             {
241                 
242                 var  image = new Gtk.Image();
243                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
244                 this.set_image (image);
245                 this.label= "Pull (Refresh) All";
246                 this.always_show_image = true;
247                 this.accel_group = null;
248                 
249                 this.activate.connect( () => {
250                     
251                     this.pullAll();
252                     /*
253                     GitMonitor.gitmonitor.stop();
254                     var tr = GitRepo.list();
255                     
256                     
257                     
258                     for (var i= 0; i< tr.length;i++) {
259                         statusicon.set_from_stock( 
260                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
261                                 
262                         var repo = tr.index(i);
263                         //if (!repo.autocommit()) {
264                             //??? should we ignore ones not on autocommit..
265                         //    continue;
266                         //}
267                         try {
268                             statusicon.set_tooltip_text("pull: " + repo.name);
269                             var str = repo.pull();
270                                     // do not care if it's already in sycn..
271                             if (Regex.match_simple ("Already up-to-date", str) ) {
272                                 continue;
273                             }
274                             var notification = new Notify.Notification( 
275                                      "Updated " + repo.name,
276                                      str,
277                                        "dialog-information"
278                                    
279                             );
280                         
281                             notification.set_timeout(20);
282                             notification.show();
283                                      
284                         } catch(Error e) {
285                             print("notification or push errror- probably to many in queue..");
286                             statusicon.set_from_stock( Gtk.Stock.MEDIA_RECORD );
287                             print(e.message);
288                             
289                         }        
290
291                     } 
292                     */
293                            
294                            
295                 });
296             }
297             void pullAll()
298             {
299                         var tr = GitRepo.list();
300                 
301                 GitMonitor.gitmonitor.stop();
302                     
303                 
304                this.total = tr.length;
305                 this.has_error = 0;
306                 this.pull_all_error_message = "";
307                 for (var i= 0; i< tr.length;i++) {
308                     statusicon.set_from_stock( 
309                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
310                             
311                     var repo = tr.index(i);
312                     //repo.ref();
313                     //this.ref();
314                     
315                     statusicon.set_tooltip_text("pull: " + repo.name);
316                                         
317                     repo.pull_async(this.pullAllCallback); 
318                                 // do not care if it's already in sycn..
319                         
320                      
321
322                 } 
323
324                   
325         
326         
327                 }
328                 uint total = 0; 
329                         uint has_error = 0;
330                         string pull_all_error_message = "";
331                 
332                 void pullAllCallback(GitRepo repo, int err, string res)
333                 {
334                         this.total--;
335                         
336                         if (err > 0) {
337                                 this.has_error = 1;
338                                 this.pull_all_error_message += this.pull_all_error_message.length > 0 ? "\n" : "";
339                                 this.pull_all_error_message += "Error Pulling " + repo.name +"\n" + res;
340                         }
341                         
342                         if (!Regex.match_simple ("Already up-to-date", res) ) {
343                         var notification = new Notify.Notification( 
344                          "Pull completed ",
345                           "Updated: " +repo.name + "\n" + res +"\n",
346                              "dialog-information"
347                     );
348                     notification.set_timeout(20);
349                                 notification.show();
350                         
351  
352  
353                         }
354                         if (this.total < 1) {
355                                 if (this.has_error) {
356                                         GitMonitor.gitmonitor.pauseError(
357                         
358                         statusicon.set_tooltip_text("Gitlive");
359                   
360                                 GitMonitor.gitmonitor.start();
361                                 //this.unref();
362                                 //repo.unref();
363                         }
364                         
365                 }
366         
367             
368         }
369         
370         
371         class ImageMenuItemD : ImageMenuItem {
372             
373             public ImageMenuItemD()
374             {
375                 
376                 var  image = new Gtk.Image();
377                 image.set_from_stock(Gtk.Stock.SAVE,Gtk.IconSize.MENU );
378                 this.set_image (image);
379                 this.label= "Update Timesheet";
380                 this.always_show_image = true;
381                 this.accel_group = null;
382                 
383                 this.activate.connect( () => {
384                  //var ret = imports.FixBug.FixBug.show();
385                 });
386             }
387             
388             
389         }
390         
391         class ImageMenuItemE : ImageMenuItem {
392             
393             public ImageMenuItemE()
394             {
395                 
396                 var  image = new Gtk.Image();
397                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
398                 this.set_image (image);
399                 this.label= "Manage Clones";
400                 this.always_show_image = true;
401                 this.accel_group = null;
402                 
403                 this.activate.connect( () => {
404                  //var ret = imports.Clones.Clones.show();
405                 });
406             }
407             
408             
409         }
410         
411         class ImageMenuItemF : ImageMenuItem {
412             
413             public ImageMenuItemF()
414             {
415                 
416                 var  image = new Gtk.Image();
417                 image.set_from_stock(Gtk.Stock.ABOUT,Gtk.IconSize.MENU );
418                 this.set_image (image);
419                 this.label= "About Gitlive";
420                 this.always_show_image = true;
421                 this.accel_group = null;
422                 
423                 this.activate.connect( () => {
424                  //var ret = imports.Clones.Clones.show();
425                  
426                     var msg = new Gtk.AboutDialog();
427                     msg.program_name = "Git Live";
428                     msg.version= "0.3";
429                     msg.website= "http://www.roojs.org/index.php/projects/gitlive.html";
430                     msg.website_label= "Roo J Solutions Ltd.";
431                     msg.license = "LGPL";
432                     msg.authors = { "Alan Knowles <alan@roojs.com>" };
433                     msg.run();
434                     msg.destroy();
435                 });
436             }
437             
438             
439         }
440         
441          class ImageMenuItemG : ImageMenuItem {
442             
443             public ImageMenuItemG()
444             {
445                 
446                 var  image = new Gtk.Image();
447                 image.set_from_stock(Gtk.Stock.QUIT,Gtk.IconSize.MENU );
448                 this.set_image (image);
449                 this.label= "Quit";
450                 this.always_show_image = true;
451                 this.accel_group = null;
452                 
453                 this.activate.connect( () => {
454                     // confirm?
455                     Gtk.main_quit();
456                  //var ret = imports.Clones.Clones.show();
457                  });
458             }
459             
460             
461         }
462         
463     }
464 }
465       
466                 
467