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