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