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     public StatusIconA() {
44         
45         statusicon = this;
46         
47          //title : 'gitlive',
48         this.stock = Gtk.Stock.REFRESH;
49         this.tooltip_text = "GitLive";
50         this.title = "gitlive";
51         
52         this.set_name("gitlive");
53         this.set_visible(true);      
54         
55         var menu = new MenuA();
56         menu.ref();       
57
58             
59        this.popup_menu.connect( (  button,   time) =>{
60        //this.button_press_event.connect( (  ) =>{
61                
62             //print(Array.prototype.slice.call(arguments).join(','));
63             print("menu activiate called\n");
64             //var menu = this.get('menu');
65             
66             menu.show_all();
67             
68             if (this.paused) {
69                 menu.resume.show();
70                 menu.pause.hide();
71             } else {
72                 menu.resume.hide();
73                 menu.pause.show();
74             }
75               //                public void popup (Gtk.Widget? parent_menu_shell, Gtk.Widget? parent_menu_item,
76                     // [CCode (scope = "async")] Gtk.MenuPositionFunc? func, uint button, uint32 activate_time);
77             //Gtk.get_current_event_device ()
78             menu.popup(
79
80                         null, null,
81                        this.position_menu,
82                        
83                     button,  time //   time
84             );
85                         
86             
87             //var g = { };
88             //var a = new Gdk.Rectangle();
89             //  needs direction=inout setting in gir to work (in bugzilla @present)
90             //this.el.get_geometry(g,a,null);
91              
92             // should check to see if @ top or bottom I guess..
93             //menu.el.get_toplevel().move(a.x, a.y +a.height);
94             //menu.el.get_toplevel().move(10,10);
95             //return false;
96               
97         }); 
98         
99     }
100     
101     void pause()
102     {
103                 this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
104     }
105     void resume()
106     {
107                 this.set_from_stock( Gtk.Stock.MEDIA_PLAY );
108     }
109     
110     
111     class MenuA : Gtk.Menu
112     {
113         public ImageMenuItem pause;
114         public ImageMenuItem resume;
115         
116         public MenuA()
117         {
118             this.pause = new ImageMenuItemA();
119             this.append(this.pause);
120             this.resume = new ImageMenuItemB();
121             this.append(this.resume);
122             this.append(new ImageMenuItemC());
123             this.append(new ImageMenuItemD());
124             this.append(new ImageMenuItemE());
125             this.append(new ImageMenuItemF());
126             this.append(new ImageMenuItemG());
127             
128             
129             
130         }
131         
132         
133         class ImageMenuItemA : ImageMenuItem {
134             
135             public ImageMenuItemA()
136             {
137                 //this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
138                 
139                 var  image = new Gtk.Image();
140                 image.set_from_stock(Gtk.Stock.MEDIA_PAUSE,Gtk.IconSize.MENU );
141                 this.set_image (image);
142                 
143                 this.label= "Pause Commits";
144                 this.always_show_image = true;
145                 this.accel_group = null;
146                 
147                 this.activate.connect( () => {
148                     statusicon.paused = true;
149                     GitMonitor.gitmonitor.stop();
150
151                    // this.el.label  = status ? 'Resume' : 'Pause';
152                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
153                     
154                     
155                 });
156                 //    id : 'pause',
157             }
158             
159             
160         }
161         class ImageMenuItemB : ImageMenuItem {
162             
163             public ImageMenuItemB()
164             {
165                 
166                 var  image = new Gtk.Image();
167                 image.set_from_stock(Gtk.Stock.MEDIA_PLAY,Gtk.IconSize.MENU );
168                 this.set_image (image);
169                 this.label= "Start Commits";
170                 this.always_show_image = true;
171                 this.accel_group = null;
172                 
173                 this.activate.connect( () => {
174                     GitMonitor.gitmonitor.start();
175                     statusicon.paused = false;
176                     
177                     //
178                    // this.el.label  = status ? 'Resume' : 'Pause';
179                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
180                     
181                     
182                 });
183             }
184             
185             
186         }
187         
188         
189         class ImageMenuItemC : ImageMenuItem {
190             
191             public ImageMenuItemC()
192             {
193                 
194                 var  image = new Gtk.Image();
195                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
196                 this.set_image (image);
197                 this.label= "Pull (Refresh) All";
198                 this.always_show_image = true;
199                 this.accel_group = null;
200                 
201                 this.activate.connect( () => {
202                     GitMonitor.gitmonitor.stop();
203                     var tr = GitRepo.list();
204                     for (var i= 0; i< tr.length;i++) {
205                         statusicon.set_from_stock( i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
206                                 
207                         var repo = tr.index(i);
208                         //if (!repo.autocommit()) {
209                             //??? should we ignore ones not on autocommit..
210                         //    continue;
211                         //}
212                         try {
213                             statusicon.set_tooltip_text("pull: " + repo.name);
214                             var str = repo.pull();
215                                     // do not care if it's already in sycn..
216                             if (Regex.match_simple ("Already up-to-date", str) ) {
217                                 continue;
218                             }
219                             var notification = new Notify.Notification( 
220                                      "Updated " + repo.name,
221                                      str,
222                                        "dialog-information"
223                                    
224                             );
225                         
226                             notification.set_timeout(20);
227                             notification.show();
228                                      
229                         } catch(Error e) {
230                             print("notification or push errror- probably to many in queue..");
231                             statusicon.set_from_stock( Gtk.Stock.MEDIA_RECORD );
232                             print(e.message);
233                             
234                         }        
235
236                     } 
237                     statusicon.set_tooltip_text("Gitlive");
238                              
239                             
240                     GitMonitor.gitmonitor.start();         
241                            
242                 });
243             }
244             
245             
246         }
247         
248         
249         class ImageMenuItemD : ImageMenuItem {
250             
251             public ImageMenuItemD()
252             {
253                 
254                 var  image = new Gtk.Image();
255                 image.set_from_stock(Gtk.Stock.SAVE,Gtk.IconSize.MENU );
256                 this.set_image (image);
257                 this.label= "Update Timesheet";
258                 this.always_show_image = true;
259                 this.accel_group = null;
260                 
261                 this.activate.connect( () => {
262                  //var ret = imports.FixBug.FixBug.show();
263                 });
264             }
265             
266             
267         }
268         
269         class ImageMenuItemE : ImageMenuItem {
270             
271             public ImageMenuItemE()
272             {
273                 
274                 var  image = new Gtk.Image();
275                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
276                 this.set_image (image);
277                 this.label= "Manage Clones";
278                 this.always_show_image = true;
279                 this.accel_group = null;
280                 
281                 this.activate.connect( () => {
282                  //var ret = imports.Clones.Clones.show();
283                 });
284             }
285             
286             
287         }
288         
289         class ImageMenuItemF : ImageMenuItem {
290             
291             public ImageMenuItemF()
292             {
293                 
294                 var  image = new Gtk.Image();
295                 image.set_from_stock(Gtk.Stock.ABOUT,Gtk.IconSize.MENU );
296                 this.set_image (image);
297                 this.label= "About Gitlive";
298                 this.always_show_image = true;
299                 this.accel_group = null;
300                 
301                 this.activate.connect( () => {
302                  //var ret = imports.Clones.Clones.show();
303                  
304                     var msg = new Gtk.AboutDialog();
305                     msg.program_name = "Git Live";
306                     msg.version= "0.3";
307                     msg.website= "http://www.roojs.org/index.php/projects/gitlive.html";
308                     msg.website_label= "Roo J Solutions Ltd.";
309                     msg.license = "LGPL";
310                     msg.authors = { "Alan Knowles <alan@roojs.com>" };
311                     msg.run();
312                     msg.destroy();
313                 });
314             }
315             
316             
317         }
318         
319          class ImageMenuItemG : ImageMenuItem {
320             
321             public ImageMenuItemG()
322             {
323                 
324                 var  image = new Gtk.Image();
325                 image.set_from_stock(Gtk.Stock.QUIT,Gtk.IconSize.MENU );
326                 this.set_image (image);
327                 this.label= "Quit";
328                 this.always_show_image = true;
329                 this.accel_group = null;
330                 
331                 this.activate.connect( () => {
332                     // confirm?
333                     Gtk.main_quit();
334                  //var ret = imports.Clones.Clones.show();
335                  });
336             }
337             
338             
339         }
340         
341     }
342 }
343       
344                 
345