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     
44     public StatusIconA() {
45         
46         statusicon = this;
47         
48          //title : 'gitlive',
49         this.stock = Gtk.Stock.REFRESH;
50         this.tooltip_text = "GitLive";
51         this.title = "gitlive";
52         
53         this.set_name("gitlive");
54         this.set_visible(true);      
55         
56         var menu = new GitliveMenu();
57         menu.ref();       
58                 this.button_press_event.connect( ( ev ) =>{
59               print("button press event called\n");
60                         menu.updateMerges();
61             menu.show_all();
62             
63             if (this.paused) {
64                 menu.resume.show();
65                 menu.pause.hide();
66             } else {
67                 menu.resume.hide();
68                 menu.pause.show();
69             }
70               //                public void popup (Gtk.Widget? parent_menu_shell, Gtk.Widget? parent_menu_item,
71                     // [CCode (scope = "async")] Gtk.MenuPositionFunc? func, uint button, uint32 activate_time);
72             //Gtk.get_current_event_device ()
73             menu.popup(
74
75                         null, null,
76                        this.position_menu,
77                        
78                     ev.button,  ev.time //   time
79             );
80               
81               return true;
82             
83             });
84        this.popup_menu.connect( (  button,   time) =>{
85        //this.button_press_event.connect( (  ) =>{
86  
87             //print(Array.prototype.slice.call(arguments).join(','));
88             print("menu activiate called\n");
89             //var menu = this.get('menu');
90             
91             menu.show_all();
92             
93             if (this.paused) {
94                 menu.resume.show();
95                 menu.pause.hide();
96             } else {
97                 menu.resume.hide();
98                 menu.pause.show();
99             }
100               //                public void popup (Gtk.Widget? parent_menu_shell, Gtk.Widget? parent_menu_item,
101                     // [CCode (scope = "async")] Gtk.MenuPositionFunc? func, uint button, uint32 activate_time);
102             //Gtk.get_current_event_device ()
103             menu.popup(
104
105                         null, null,
106                        this.position_menu,
107                        
108                     button,  time //   time
109             );
110                         
111             
112             //var g = { };
113             //var a = new Gdk.Rectangle();
114             //  needs direction=inout setting in gir to work (in bugzilla @present)
115             //this.el.get_geometry(g,a,null);
116              
117             // should check to see if @ top or bottom I guess..
118             //menu.el.get_toplevel().move(a.x, a.y +a.height);
119             //menu.el.get_toplevel().move(10,10);
120             //return false;
121               
122         }); 
123         
124     }
125     
126     public void pause()
127     {
128                 this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
129                 this.paused = true;
130     }
131     
132     public void resume()
133     {
134                 this.set_from_stock( Gtk.Stock.MEDIA_PLAY );
135                 this.paused = false;
136     }
137     
138     public void refreshing()
139     {
140                 this.set_from_stock( Gtk.Stock.REFRESH );
141     }
142     
143     public void pauseError()
144     {
145                  this.paused = true;
146                  var flag = true;
147                  Timeout.add_full(Priority.LOW, 500, () => {
148                  
149                         if (!this.paused) {
150                                 return false;
151                         }
152                         this.set_from_stock( flag == true ? Gtk.Stock.MEDIA_RECORD  
153                                         : Gtk.Stock.MEDIA_PAUSE);
154                         flag = !flag;
155                         return true;
156                 });
157                  
158     }
159     
160     class GitliveMenu : Gtk.Menu
161     {
162         public ImageMenuItem pause;
163         public ImageMenuItem resume;
164         public Gtk.MenuItem before_seperator;
165         public Gtk.MenuItem after_seperator;        
166         
167         
168         public GitliveMenu()
169         {
170             this.pause = new MenuItemPause();
171             this.append(this.pause);
172             this.resume = new MenuItemStartCommits();
173             this.append(this.resume);
174             this.append(new MenuItemPullAll());
175             this.before_seperator = new Gtk.SeparatorMenuItem();
176             this.append(this.before_seperator);
177
178             this.after_seperator = new Gtk.SeparatorMenuItem();
179             this.append(this.after_seperator);
180             //this.append(new MenuItemUpdateTimesheet());            
181             this.append(new MenuItemManageClones());
182             this.append(new MenuItemAbout());
183             this.append(new MenuItemQuit());
184             this.merge_items = new  Gee.ArrayList<Gtk.MenuItem>();
185         }
186         public Gee.ArrayList<Gtk.MenuItem> merge_items;
187         
188         
189         public void updateMerges()
190         {
191                 // show a list of possible merges on the menu.
192                 foreach (var m in this.merge_items) {
193                         this.remove(m);
194                 }
195                 foreach(var r in GitRepo.singleton().cache.values ) {
196                         if (!r.is_wip_branch()) {
197                                 continue;
198                                 }
199                                 var t = r.activeTicket;
200                                 if (t == null) { 
201                                         continue;
202                                 }
203                                 var mi = new MergeMenuItem(r,t);
204                                 this.insert (mi,4); //backwards..                               
205                                  
206                 }
207                 
208         }
209         
210         
211         class MergeMenuItem : Gtk.MenuItem {        
212         
213                 GitRepo repo;
214                 RooTicket ticket;
215         
216                 public MergeMenuItem(GitRepo r, RooTicket t)
217                 {
218                         this.repo = r;
219                         this.ticket = t;
220                         
221                         this.label = ("Merge [%s] #%s %s".printf(r.name, t.id , t.summary));
222
223                                 this.activate.connect(() => {
224                                         // show merge dialog..
225                                 });
226                 
227                 }
228                 
229         }
230         
231         
232         class MenuItemPause : ImageMenuItem {
233             
234             public MenuItemPause()
235             {
236                 //this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
237                 
238                 var  image = new Gtk.Image();
239                 image.set_from_stock(Gtk.Stock.MEDIA_PAUSE,Gtk.IconSize.MENU );
240                 this.set_image (image);
241                 
242                 this.label= "Pause Commits";
243                 this.always_show_image = true;
244                 this.accel_group = null;
245                 
246                 this.activate.connect( () => {
247                     statusicon.paused = true;
248                     GitMonitor.gitmonitor.stop();
249
250                    // this.el.label  = status ? 'Resume' : 'Pause';
251                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
252                     
253                     
254                 });
255                 //    id : 'pause',
256             }
257             
258             
259         }
260         class MenuItemStartCommits : ImageMenuItem {
261             
262             public MenuItemStartCommits()
263             {
264                 
265                 var  image = new Gtk.Image();
266                 image.set_from_stock(Gtk.Stock.MEDIA_PLAY,Gtk.IconSize.MENU );
267                 this.set_image (image);
268                 this.label= "Start Commits";
269                 this.always_show_image = true;
270                 this.accel_group = null;
271                 
272                 this.activate.connect( () => {
273                     GitMonitor.gitmonitor.start();
274                     statusicon.paused = false;
275                     
276                     //
277                    // this.el.label  = status ? 'Resume' : 'Pause';
278                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
279                     
280                     
281                 });
282             }
283             
284             
285         }
286         
287         
288         class MenuItemPullAll : ImageMenuItem {
289             
290             public MenuItemPullAll()
291             {
292                 
293                 var  image = new Gtk.Image();
294                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
295                 this.set_image (image);
296                 this.label= "Pull (Refresh) All";
297                 this.always_show_image = true;
298                 this.accel_group = null;
299                 
300                 this.activate.connect( () => {
301                     
302                     this.pullAll();
303                     /*
304                     GitMonitor.gitmonitor.stop();
305                     var tr = GitRepo.list();
306                     
307                     
308                     
309                     for (var i= 0; i< tr.length;i++) {
310                         statusicon.set_from_stock( 
311                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
312                                 
313                         var repo = tr.index(i);
314                         //if (!repo.autocommit()) {
315                             //??? should we ignore ones not on autocommit..
316                         //    continue;
317                         //}
318                         try {
319                             statusicon.set_tooltip_text("pull: " + repo.name);
320                             var str = repo.pull();
321                                     // do not care if it's already in sycn..
322                             if (Regex.match_simple ("Already up-to-date", str) ) {
323                                 continue;
324                             }
325                             var notification = new Notify.Notification( 
326                                      "Updated " + repo.name,
327                                      str,
328                                        "dialog-information"
329                                    
330                             );
331                         
332                             notification.set_timeout(20);
333                             notification.show();
334                                      
335                         } catch(Error e) {
336                             print("notification or push errror- probably to many in queue..");
337                             statusicon.set_from_stock( Gtk.Stock.MEDIA_RECORD );
338                             print(e.message);
339                             
340                         }        
341
342                     } 
343                     */
344                            
345                            
346                 });
347             }
348             void pullAll()
349             {
350                         var tr = GitRepo.list();
351                 
352                 GitMonitor.gitmonitor.stop();
353                     
354                 
355                this.total = tr.length;
356                 this.has_error = 0;
357                 this.pull_all_error_message = "";
358                 for (var i= 0; i< tr.length;i++) {
359                     statusicon.set_from_stock( 
360                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
361                             
362                     var repo = tr.index(i);
363                     //repo.ref();
364                     //this.ref();
365                     
366                     statusicon.set_tooltip_text("pull: " + repo.name);
367                                         
368                     repo.pull_async(this.pullAllCallback); 
369                                 // do not care if it's already in sycn..
370                         
371                      
372
373                 } 
374
375                   
376         
377         
378                 }
379                 uint total = 0; 
380                         uint has_error = 0;
381                         string pull_all_error_message = "";
382                 
383                 void pullAllCallback(GitRepo repo, int err, string res)
384                 {
385                         this.total--;
386                         
387                         if (err > 0) {
388                                 this.has_error = 1;
389                                 this.pull_all_error_message += this.pull_all_error_message.length > 0 ? "\n" : "";
390                                 this.pull_all_error_message += "Error Pulling " + repo.name +"\n" + res;
391                         }
392                         
393                         if (!Regex.match_simple ("Already up-to-date", res) ) {
394                         var notification = new Notify.Notification( 
395                          "Pull completed ",
396                           "Updated: " +repo.name + "\n" + res +"\n",
397                              "dialog-information"
398                     );
399                     notification.set_timeout(20);
400                                 notification.show();
401                         
402  
403  
404                         }
405                         if (this.total < 1) {
406                                 if (this.has_error > 0) {
407                                         GitMonitor.gitmonitor.pauseError(this.pull_all_error_message);
408                                         return;
409                                 }
410                         
411                         statusicon.set_tooltip_text("Gitlive");
412                   
413                                 GitMonitor.gitmonitor.start();
414                                 //this.unref();
415                                 //repo.unref();
416                         }
417                         
418                 }
419         
420             
421         }
422         
423         
424         class MenuItemUpdateTimesheet : ImageMenuItem {
425             
426             public MenuItemUpdateTimesheet()
427             {
428                 
429                 var  image = new Gtk.Image();
430                 image.set_from_stock(Gtk.Stock.SAVE,Gtk.IconSize.MENU );
431                 this.set_image (image);
432                 this.label= "Update Timesheet";
433                 this.always_show_image = true;
434                 this.accel_group = null;
435                 
436                 this.activate.connect( () => {
437                  //var ret = imports.FixBug.FixBug.show();
438                 });
439             }
440             
441             
442         }
443         
444         class MenuItemManageClones : ImageMenuItem {
445             
446             public MenuItemManageClones()
447             {
448                 
449                 var  image = new Gtk.Image();
450                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
451                 this.set_image (image);
452                 this.label= "Manage Clones";
453                 this.always_show_image = true;
454                 this.accel_group = null;
455                 
456                 this.activate.connect( () => {
457                       Clones.singleton().show();
458                 });
459             }
460             
461             
462         }
463         
464         class MenuItemAbout : ImageMenuItem {
465             
466             public MenuItemAbout()
467             {
468                 
469                 var  image = new Gtk.Image();
470                 image.set_from_stock(Gtk.Stock.ABOUT,Gtk.IconSize.MENU );
471                 this.set_image (image);
472                 this.label= "About Gitlive";
473                 this.always_show_image = true;
474                 this.accel_group = null;
475                 
476                 this.activate.connect( () => {
477                  //var ret = imports.Clones.Clones.show();
478                  
479                     var msg = new Gtk.AboutDialog();
480                     msg.program_name = "Git Live";
481                     msg.version= "0.3";
482                     msg.website= "http://www.roojs.org/index.php/projects/gitlive.html";
483                     msg.website_label= "Roo J Solutions Ltd.";
484                     msg.license = "LGPL";
485                     msg.authors = { "Alan Knowles <alan@roojs.com>" };
486                     msg.run();
487                     msg.destroy();
488                 });
489             }
490             
491             
492         }
493         
494          class MenuItemQuit : ImageMenuItem {
495             
496             public MenuItemQuit()
497             {
498                 
499                 var  image = new Gtk.Image();
500                 image.set_from_stock(Gtk.Stock.QUIT,Gtk.IconSize.MENU );
501                 this.set_image (image);
502                 this.label= "Quit";
503                 this.always_show_image = true;
504                 this.accel_group = null;
505                 
506                 this.activate.connect( () => {
507                     // confirm?
508                     Gtk.main_quit();
509                  //var ret = imports.Clones.Clones.show();
510                  });
511             }
512             
513             
514         }
515         
516     }
517 }
518       
519                 
520