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