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