efedebaa34e8fc32f788e31b7954c738cbf1fe2a
[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 MenuItemAbout());
184             this.append(new MenuItemQuit());
185             this.merge_items = new  Gee.ArrayList<Gtk.MenuItem>();
186         }
187         public Gee.ArrayList<Gtk.MenuItem> merge_items;
188         
189         
190         public void updateMerges()
191         {
192                 // show a list of possible merges on the menu.
193                 foreach (var m in this.merge_items) {
194                         this.remove(m);
195                 }
196                 foreach(var r in GitRepo.singleton().cache.values ) {
197                         GLib.debug("checking %s for branch = %s", r.name, r.currentBranch.name);
198                         if (!r.is_wip_branch()) {
199                                 continue;
200                                 }
201                         GLib.debug("checking  for activeTicket");                               
202                                 var t = r.activeTicket;
203                                 if (t == null) { 
204                                         continue;
205                                 }
206                                 var mi = new MergeMenuItem(r,t);
207                                 this.insert (mi,4); //backwards.
208                                 this.merge_items.add(mi);                       
209                                  
210                 }
211                 
212         }
213         
214        
215         class MergeMenuItem : Gtk.MenuItem {        
216         
217                 GitRepo repo;
218                 RooTicket ticket;
219         
220                 public MergeMenuItem(GitRepo r, RooTicket t)
221                 {
222                         this.repo = r;
223                         this.ticket = t;
224                         
225                         this.label = ("Merge [%s] #%s %s".printf(r.name, t.id , t.summary));
226
227                                 this.activate.connect(() => {
228                                         MergeBranch.singleton().show(this.ticket, null);
229                                         // show merge dialog..
230                                 });
231                 
232                 }
233                 
234         }
235         
236         
237         class MenuItemPause : ImageMenuItem {
238             
239             public MenuItemPause()
240             {
241                 //this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
242                 
243                 var  image = new Gtk.Image();
244                 image.set_from_stock(Gtk.Stock.MEDIA_PAUSE,Gtk.IconSize.MENU );
245                 this.set_image (image);
246                 
247                 this.label= "Pause Commits";
248                 this.always_show_image = true;
249                 this.accel_group = null;
250                 
251                 this.activate.connect( () => {
252                     statusicon.paused = true;
253                     GitMonitor.gitmonitor.stop();
254
255                    // this.el.label  = status ? 'Resume' : 'Pause';
256                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
257                     
258                     
259                 });
260                 //    id : 'pause',
261             }
262             
263             
264         }
265         class MenuItemStartCommits : ImageMenuItem {
266             
267             public MenuItemStartCommits()
268             {
269                 
270                 var  image = new Gtk.Image();
271                 image.set_from_stock(Gtk.Stock.MEDIA_PLAY,Gtk.IconSize.MENU );
272                 this.set_image (image);
273                 this.label= "Start Commits";
274                 this.always_show_image = true;
275                 this.accel_group = null;
276                 
277                 this.activate.connect( () => {
278                     GitMonitor.gitmonitor.start();
279                     statusicon.paused = false;
280                     
281                     //
282                    // this.el.label  = status ? 'Resume' : 'Pause';
283                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
284                     
285                     
286                 });
287             }
288             
289             
290         }
291         
292         
293         class MenuItemPullAll : ImageMenuItem {
294             
295             public MenuItemPullAll()
296             {
297                 
298                 var  image = new Gtk.Image();
299                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
300                 this.set_image (image);
301                 this.label= "Pull (Refresh) All";
302                 this.always_show_image = true;
303                 this.accel_group = null;
304                 
305                 this.activate.connect( () => {
306                     
307                     this.pullAll();
308                     /*
309                     GitMonitor.gitmonitor.stop();
310                     var tr = GitRepo.list();
311                     
312                     
313                     
314                     for (var i= 0; i< tr.length;i++) {
315                         statusicon.set_from_stock( 
316                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
317                                 
318                         var repo = tr.index(i);
319                         //if (!repo.autocommit()) {
320                             //??? should we ignore ones not on autocommit..
321                         //    continue;
322                         //}
323                         try {
324                             statusicon.set_tooltip_text("pull: " + repo.name);
325                             var str = repo.pull();
326                                     // do not care if it's already in sycn..
327                             if (Regex.match_simple ("Already up-to-date", str) ) {
328                                 continue;
329                             }
330                             var notification = new Notify.Notification( 
331                                      "Updated " + repo.name,
332                                      str,
333                                        "dialog-information"
334                                    
335                             );
336                         
337                             notification.set_timeout(20);
338                             notification.show();
339                                      
340                         } catch(Error e) {
341                             print("notification or push errror- probably to many in queue..");
342                             statusicon.set_from_stock( Gtk.Stock.MEDIA_RECORD );
343                             print(e.message);
344                             
345                         }        
346
347                     } 
348                     */
349                            
350                            
351                 });
352             }
353             void pullAll()
354             {
355                         var tr = GitRepo.list();
356                 
357                 GitMonitor.gitmonitor.stop();
358                     
359                 
360                this.total = tr.length;
361                 this.has_error = 0;
362                 this.pull_all_error_message = "";
363                 for (var i= 0; i< tr.length;i++) {
364                     statusicon.set_from_stock( 
365                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
366                             
367                     var repo = tr.index(i);
368                     //repo.ref();
369                     //this.ref();
370                     
371                     statusicon.set_tooltip_text("pull: " + repo.name);
372                                         
373                     repo.pull_async(this.pullAllCallback); 
374                                 // do not care if it's already in sycn..
375                         
376                      
377
378                 } 
379
380                   
381         
382         
383                 }
384                 uint total = 0; 
385                         uint has_error = 0;
386                         string pull_all_error_message = "";
387                 
388                 void pullAllCallback(GitRepo repo, int err, string res)
389                 {
390                         this.total--;
391                         
392                         if (err > 0) {
393                                 this.has_error = 1;
394                                 this.pull_all_error_message += this.pull_all_error_message.length > 0 ? "\n" : "";
395                                 this.pull_all_error_message += "Error Pulling " + repo.name +"\n" + res;
396                         }
397                         
398                         if (!Regex.match_simple ("Already up-to-date", res) ) {
399                         var notification = new Notify.Notification( 
400                          "Pull completed ",
401                           "Updated: " +repo.name + "\n" + res +"\n",
402                              "dialog-information"
403                     );
404                     notification.set_timeout(20);
405                                 notification.show();
406                         
407  
408  
409                         }
410                         if (this.total < 1) {
411                                 if (this.has_error > 0) {
412                                         GitMonitor.gitmonitor.pauseError(this.pull_all_error_message);
413                                         return;
414                                 }
415                         
416                         statusicon.set_tooltip_text("Gitlive");
417                   
418                                 GitMonitor.gitmonitor.start();
419                                 //this.unref();
420                                 //repo.unref();
421                         }
422                         
423                 }
424         
425             
426         }
427         
428         
429         class MenuItemUpdateTimesheet : ImageMenuItem {
430             
431             public MenuItemUpdateTimesheet()
432             {
433                 
434                 var  image = new Gtk.Image();
435                 image.set_from_stock(Gtk.Stock.SAVE,Gtk.IconSize.MENU );
436                                 this.set_image (image);
437                 this.label= "Update Timesheet";
438                 this.always_show_image = true;
439                 this.accel_group = null;
440                 
441                 this.activate.connect( () => {
442                  //var ret = imports.FixBug.FixBug.show();
443                 });
444             }
445             
446             
447         }
448         
449         class MenuItemManageClones : ImageMenuItem {
450             
451             public MenuItemManageClones()
452             {
453                 
454                 var  image = new Gtk.Image();
455                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
456                 this.set_image (image);
457                 this.label= "Manage Clones";
458                 this.always_show_image = true;
459                 this.accel_group = null;
460                 
461                 this.activate.connect( () => {
462                         GitRepo.updateAll("show_clones");
463                 
464                      // Clones.singleton().show();
465                 });
466             }
467             
468             
469         }
470         
471         class MenuItemAbout : ImageMenuItem {
472             
473             public MenuItemAbout()
474             {
475                 
476                 var  image = new Gtk.Image();
477                 image.set_from_stock(Gtk.Stock.ABOUT,Gtk.IconSize.MENU );
478                 this.set_image (image);
479                 this.label= "About Gitlive";
480                 this.always_show_image = true;
481                 this.accel_group = null;
482                 
483                 this.activate.connect( () => {
484                  //var ret = imports.Clones.Clones.show();
485                  
486                     var msg = new Gtk.AboutDialog();
487                     msg.program_name = "Git Live";
488                     msg.version= "0.3";
489                     msg.website= "http://www.roojs.org/index.php/projects/gitlive.html";
490                     msg.website_label= "Roo J Solutions Ltd.";
491                     msg.license = "LGPL";
492                     msg.authors = { "Alan Knowles <alan@roojs.com>" };
493                     msg.run();
494                     msg.destroy();
495                 });
496             }
497             
498             
499         }
500         
501          class MenuItemQuit : ImageMenuItem {
502             
503             public MenuItemQuit()
504             {
505                 
506                 var  image = new Gtk.Image();
507                 image.set_from_stock(Gtk.Stock.QUIT,Gtk.IconSize.MENU );
508                 this.set_image (image);
509                 this.label= "Quit";
510                 this.always_show_image = true;
511                 this.accel_group = null;
512                 
513                 this.activate.connect( () => {
514                     // confirm?
515                     Gtk.main_quit();
516                  //var ret = imports.Clones.Clones.show();
517                  });
518             }
519             
520             
521         }
522         
523     }
524 }
525       
526                 
527