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