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