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