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
27     var window = new TestWindow();
28     
29     window.show_all ();
30
31     Gtk.main ();
32     return 0;
33      
34
35 }
36
37 public class TestButton : Button {
38     public TestButton()
39     {
40         this.set_label("Click me");
41         
42         
43         this.clicked.connect (() => {
44             this.label = "Thank you";
45         });
46         
47     }
48     
49     
50 }
51
52 public class TestWindow : Window {
53
54     public TestWindow() {
55          this.title = "First GTK+ Program";
56         this.border_width = 10;
57         this.window_position = WindowPosition.CENTER;
58         this.set_default_size (350, 70);
59         
60         // -- connect all
61         this.destroy.connect (Gtk.main_quit);        
62         
63         // add children..
64         this.add(new TestButton());
65
66     }
67    
68     
69  
70    
71 }
72
73
74 pubic StatusIconA statusicon;
75  
76 class StatusIconA : StatusIcon {
77
78     bool paused = false;
79      
80     public StatusIconA() {
81         
82         statusicon = this;
83         
84         
85          //title : 'gitlive',
86         this.stock = Gtk.STOCK_REFRESH;
87         this.tooltip_text = "GitLive";
88         this.title = "gitlive";
89         
90         this.set_name("gitlive");
91        
92         
93         var menu = new MenuA();
94                  
95          
96         this.popup_menu.connect( (button,event_time) =>{
97             
98             //print(Array.prototype.slice.call(arguments).join(','));
99             
100             //var menu = this.get('menu');
101             
102             this.menu.show_all();
103             
104             if (this.paused) {
105                 menu.resume.show();
106                 menu.pause.hide();
107             } else {
108                 menu.resume.hide();
109                 menu.pause.show();
110             }
111              
112             //Gtk.get_current_event_device ()
113             menu.popup(
114                     
115                         null, null,
116                         null, button,
117                         event_time, null
118             );
119                         
120             
121             
122             //var g = { };
123             //var a = new Gdk.Rectangle();
124             //  needs direction=inout setting in gir to work (in bugzilla @present)
125             //this.el.get_geometry(g,a,null);
126              
127             // should check to see if @ top or bottom I guess..
128             //menu.el.get_toplevel().move(a.x, a.y +a.height);
129             //menu.el.get_toplevel().move(10,10);
130               
131         }); 
132         
133     }
134     
135     class MenuA : 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         }
147         
148         
149         class ImageMenuItemA : ImageMenuItem {
150             
151             public ImageMenuItemA()
152             {
153                 this.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
154                 this.label= "Pause Commits";
155                 this.always_show_image = true;
156                 this.accel_group = null;
157                 
158                 this.activate.connect( () => {
159                     statusicon.paused = true;
160                     
161                     //GitMonitor.GitMonitor.stop();
162                    // this.el.label  = status ? 'Resume' : 'Pause';
163                     statusicon.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
164                     
165                     
166                 })
167                 //    id : 'pause',
168             }
169             
170             
171         }
172          class ImageMenuItemB : ImageMenuItem {
173             
174             public ImageMenuItemB()
175             {
176                 this.set_from_stock( Gtk.STOCK_MEDIA_PLAY );
177                 this.label= "Start Commits";
178                 this.always_show_image = true;
179                 this.accel_group = null;
180                 
181                 this.activate.connect( () => {
182                     //GitMonitor.GitMonitor.start();
183                     statusicon.paused = false;
184                     
185                     //
186                    // this.el.label  = status ? 'Resume' : 'Pause';
187                     statusicon.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
188                     
189                     
190                 })
191                 //    id : 'pause',
192             }
193             
194             
195         }
196     }
197     
198          
199     items : [
200        {
201             xtype: Gtk.Menu,
202             id : 'menu',
203             pack: false,
204             items : [
205                 {
206                     
207                     //label: 'Pause',
208                     pack:  'append',
209                     listeners : {
210                         activate : function () {
211                             this.parent.parent.paused = true;
212                             imports.GitMonitor.GitMonitor.stop();
213                            // this.el.label  = status ? 'Resume' : 'Pause';
214                             this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
215                             
216                         }
217                     }
218                 },
219                 
220                 {
221                     init : function() {
222                         this.el = XObject.isSeed ?
223                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PLAY)
224                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_MEDIA_PLAY, null);
225                         XObject.prototype.init.call(this);
226                     },
227                     label: 'Resume Commits',
228                     always_show_image : true,
229                     accel_group : null,
230                     id : 'resume',
231                     //label: 'Pause',
232                     pack:  'append',
233                     listeners : {
234                         activate : function () {
235                             this.parent.parent.paused = false;
236                              imports.GitMonitor.GitMonitor.start();
237                             //var status = this.el.label == 'Pause' ? 1 : 0
238                            // this.el.label  = status ? 'Resume' : 'Pause';
239                                
240                             
241                         }
242                     }
243                 },
244                 
245                 {
246                     init : function() {
247                         this.el = XObject.isSeed ?
248                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_FULLSCREEN)
249                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_FULLSCREEN, null);
250                       
251                         XObject.prototype.init.call(this);
252                     },
253                     label: 'Pull (Refresh) All',
254                     always_show_image : true,
255                     accel_group : null,
256                     //label: 'Pause',
257                     pack:  'append',
258                     listeners : {
259                         activate : function () {
260                             imports.GitMonitor.GitMonitor.stop();
261                            
262                             
263                             var tr = imports.Scm.Repo.Repo.list();
264                             for (var i= 0; i< tr.length;i++) {
265                                 this.parent.parent.el.set_from_stock( i%2 ?  Gtk.STOCK_FULLSCREEN : Gtk.STOCK_LEAVE_FULLSCREEN );
266                                 
267                                 var repo = tr[i];
268                                 if (!repo.autocommit()) {
269                                     //??? should we ignore ones not on autocommit..
270                                     continue;
271                                 }
272                                 try {
273                                     this.parent.parent.el.set_tooltip_text("pull: " + repo.name);
274                                
275                                     var str = repo.pull();
276                                     // do not care if it's already in sycn..
277                                     if (str.match(/Already up-to-date/)) {
278                                         continue;
279                                     }
280                                     var notification = new Notify.Notification({
281                                        summary: "Updated " + repo.name,
282                                        body : str
283                                    });
284                                    notification.set_timeout(20);
285                                    notification.show();
286                                    
287                                 } catch(e) {
288                                     this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_RECORD );
289                                     print(JSON.stringify(e));
290                                     print("notification or push errror- probably to many in queue..");
291                                     imports.gitlive.errorDialog(e.message);
292
293                                 }
294                             }
295                             this.parent.parent.el.set_tooltip_text(this.parent.parent.tooltip_text);
296                                
297                             
298                              
299                           
300                             imports.GitMonitor.GitMonitor.start();
301                         }   
302                     }
303                 },
304                 {
305                     init : function() {
306                         this.el = XObject.isSeed ?
307                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_SAVE)
308                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_SAVE, null);
309                          
310                         XObject.prototype.init.call(this);
311                     },
312                     label: 'Update Timesheet',
313                     always_show_image : true,
314                     accel_group : null,
315                     
316                     //label: 'Pause',
317                     pack:  'append',
318                     listeners : {
319                         activate : function () {
320                             var ret = imports.FixBug.FixBug.show();
321                             
322                         }
323                     }
324                 },
325                 
326                 
327                 {
328                     init : function() {
329                         this.el = XObject.isSeed ?
330                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_FULLSCREEN)
331                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_FULLSCREEN, null);
332                          XObject.prototype.init.call(this);
333                     },
334                     label: 'Manage Clones',
335                     always_show_image : true,
336                     accel_group : null,
337                     
338                     //label: 'Pause',
339                     pack:  'append',
340                     listeners : {
341                         activate : function () {
342                              var ret = imports.Clones.Clones.show();
343                             
344                         }
345                     }
346                 },
347                 
348                 
349                 
350             
351                 {
352                     init : function() {
353                         this.el = XObject.isSeed ?
354                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT)
355                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_ABOUT, null);
356                    
357                         XObject.prototype.init.call(this);
358                     },
359                     label: 'About GitLive',
360                     pack:  'append',
361                     listeners : {
362                         activate : function () {
363                             var msg = new Gtk.AboutDialog({
364                                 program_name : "Git Live",
365                                 version: '0.3',
366                                 website: 'http://www.roojs.org/index.php/projects/gitlive.html',
367                                 website_label: 'RooJS Consulting',
368                                 license : 'LGPL'
369                             });
370                             msg.set_authors([ "Alan Knowles <alan@roojs.com>" ]);
371                             msg.run();
372                             msg.destroy();
373                         }
374                     }
375                 },
376                 
377                 {
378                     init : function() {
379                         
380                         this.el = XObject.isSeed ?
381                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT)
382                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, null);
383                      
384                         XObject.prototype.init.call(this);
385                     },
386                     label: 'Quit',
387                     pack:  'append',
388                     listeners : {
389                         activate : function () {
390                             Seed.quit();
391                         }
392                     }
393                 }
394                 
395                 
396             ]
397         }
398     ]
399     
400 });
401 */
402
403