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                 this.error_animate= false;
109     }
110     
111     void refreshing()
112     {
113                 this.set_from_stock( Gtk.Stock.REFRESH );
114     }
115     void pauseError()
116     {
117                  this.error_animate = true;
118                  var flag = true;
119                  Timeout.add_full(Priority.LOW, 500, () => {
120                  
121                         if (!this.error_animate) {
122                                 return false;
123                         }
124                         this.set_from_stock( flag == true ? Gtk.Stock.MEDIA_RECORD  
125                                         : Gtk.Stock.MEDIA_PAUSE);
126                         flag = !flag;
127                         return true;
128                 });
129                  
130     }
131     
132     class MenuA : Gtk.Menu
133     {
134         public ImageMenuItem pause;
135         public ImageMenuItem resume;
136         
137         public MenuA()
138         {
139             this.pause = new ImageMenuItemA();
140             this.append(this.pause);
141             this.resume = new ImageMenuItemB();
142             this.append(this.resume);
143             this.append(new ImageMenuItemC());
144             this.append(new ImageMenuItemD());
145             this.append(new ImageMenuItemE());
146             this.append(new ImageMenuItemF());
147             this.append(new ImageMenuItemG());
148             
149             
150             
151         }
152         
153         
154         class ImageMenuItemA : ImageMenuItem {
155             
156             public ImageMenuItemA()
157             {
158                 //this.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
159                 
160                 var  image = new Gtk.Image();
161                 image.set_from_stock(Gtk.Stock.MEDIA_PAUSE,Gtk.IconSize.MENU );
162                 this.set_image (image);
163                 
164                 this.label= "Pause Commits";
165                 this.always_show_image = true;
166                 this.accel_group = null;
167                 
168                 this.activate.connect( () => {
169                     statusicon.paused = true;
170                     GitMonitor.gitmonitor.stop();
171
172                    // this.el.label  = status ? 'Resume' : 'Pause';
173                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
174                     
175                     
176                 });
177                 //    id : 'pause',
178             }
179             
180             
181         }
182         class ImageMenuItemB : ImageMenuItem {
183             
184             public ImageMenuItemB()
185             {
186                 
187                 var  image = new Gtk.Image();
188                 image.set_from_stock(Gtk.Stock.MEDIA_PLAY,Gtk.IconSize.MENU );
189                 this.set_image (image);
190                 this.label= "Start Commits";
191                 this.always_show_image = true;
192                 this.accel_group = null;
193                 
194                 this.activate.connect( () => {
195                     GitMonitor.gitmonitor.start();
196                     statusicon.paused = false;
197                     
198                     //
199                    // this.el.label  = status ? 'Resume' : 'Pause';
200                     statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
201                     
202                     
203                 });
204             }
205             
206             
207         }
208         
209         
210         class ImageMenuItemC : ImageMenuItem {
211             
212             public ImageMenuItemC()
213             {
214                 
215                 var  image = new Gtk.Image();
216                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
217                 this.set_image (image);
218                 this.label= "Pull (Refresh) All";
219                 this.always_show_image = true;
220                 this.accel_group = null;
221                 
222                 this.activate.connect( () => {
223                     GitMonitor.gitmonitor.stop();
224                     var tr = GitRepo.list();
225                     for (var i= 0; i< tr.length;i++) {
226                         statusicon.set_from_stock( 
227                                 i%2 == 0 ?  Gtk.Stock.FULLSCREEN : Gtk.Stock.LEAVE_FULLSCREEN );
228                                 
229                         var repo = tr.index(i);
230                         //if (!repo.autocommit()) {
231                             //??? should we ignore ones not on autocommit..
232                         //    continue;
233                         //}
234                         try {
235                             statusicon.set_tooltip_text("pull: " + repo.name);
236                             var str = repo.pull();
237                                     // do not care if it's already in sycn..
238                             if (Regex.match_simple ("Already up-to-date", str) ) {
239                                 continue;
240                             }
241                             var notification = new Notify.Notification( 
242                                      "Updated " + repo.name,
243                                      str,
244                                        "dialog-information"
245                                    
246                             );
247                         
248                             notification.set_timeout(20);
249                             notification.show();
250                                      
251                         } catch(Error e) {
252                             print("notification or push errror- probably to many in queue..");
253                             statusicon.set_from_stock( Gtk.Stock.MEDIA_RECORD );
254                             print(e.message);
255                             
256                         }        
257
258                     } 
259                     statusicon.set_tooltip_text("Gitlive");
260                              
261                             
262                     GitMonitor.gitmonitor.start();         
263                            
264                 });
265             }
266             
267             
268         }
269         
270         
271         class ImageMenuItemD : ImageMenuItem {
272             
273             public ImageMenuItemD()
274             {
275                 
276                 var  image = new Gtk.Image();
277                 image.set_from_stock(Gtk.Stock.SAVE,Gtk.IconSize.MENU );
278                 this.set_image (image);
279                 this.label= "Update Timesheet";
280                 this.always_show_image = true;
281                 this.accel_group = null;
282                 
283                 this.activate.connect( () => {
284                  //var ret = imports.FixBug.FixBug.show();
285                 });
286             }
287             
288             
289         }
290         
291         class ImageMenuItemE : ImageMenuItem {
292             
293             public ImageMenuItemE()
294             {
295                 
296                 var  image = new Gtk.Image();
297                 image.set_from_stock(Gtk.Stock.FULLSCREEN,Gtk.IconSize.MENU );
298                 this.set_image (image);
299                 this.label= "Manage Clones";
300                 this.always_show_image = true;
301                 this.accel_group = null;
302                 
303                 this.activate.connect( () => {
304                  //var ret = imports.Clones.Clones.show();
305                 });
306             }
307             
308             
309         }
310         
311         class ImageMenuItemF : ImageMenuItem {
312             
313             public ImageMenuItemF()
314             {
315                 
316                 var  image = new Gtk.Image();
317                 image.set_from_stock(Gtk.Stock.ABOUT,Gtk.IconSize.MENU );
318                 this.set_image (image);
319                 this.label= "About Gitlive";
320                 this.always_show_image = true;
321                 this.accel_group = null;
322                 
323                 this.activate.connect( () => {
324                  //var ret = imports.Clones.Clones.show();
325                  
326                     var msg = new Gtk.AboutDialog();
327                     msg.program_name = "Git Live";
328                     msg.version= "0.3";
329                     msg.website= "http://www.roojs.org/index.php/projects/gitlive.html";
330                     msg.website_label= "Roo J Solutions Ltd.";
331                     msg.license = "LGPL";
332                     msg.authors = { "Alan Knowles <alan@roojs.com>" };
333                     msg.run();
334                     msg.destroy();
335                 });
336             }
337             
338             
339         }
340         
341          class ImageMenuItemG : ImageMenuItem {
342             
343             public ImageMenuItemG()
344             {
345                 
346                 var  image = new Gtk.Image();
347                 image.set_from_stock(Gtk.Stock.QUIT,Gtk.IconSize.MENU );
348                 this.set_image (image);
349                 this.label= "Quit";
350                 this.always_show_image = true;
351                 this.accel_group = null;
352                 
353                 this.activate.connect( () => {
354                     // confirm?
355                     Gtk.main_quit();
356                  //var ret = imports.Clones.Clones.show();
357                  });
358             }
359             
360             
361         }
362         
363     }
364 }
365       
366                 
367