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