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