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                     this.parent.parent.paused = true;
160                     
161                     statusicon.paused = true;
162                     
163                     //GitMonitor.GitMonitor.stop();
164                    // this.el.label  = status ? 'Resume' : 'Pause';
165                     statusicon.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
166                     
167                     
168                 })
169                 //    id : 'pause',
170             }
171             
172             
173         }
174         
175     }
176     
177          
178     items : [
179        {
180             xtype: Gtk.Menu,
181             id : 'menu',
182             pack: false,
183             items : [
184                 {
185                     
186                     //label: 'Pause',
187                     pack:  'append',
188                     listeners : {
189                         activate : function () {
190                             this.parent.parent.paused = true;
191                             imports.GitMonitor.GitMonitor.stop();
192                            // this.el.label  = status ? 'Resume' : 'Pause';
193                             this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
194                             
195                         }
196                     }
197                 },
198                 
199                 {
200                     init : function() {
201                         this.el = XObject.isSeed ?
202                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PLAY)
203                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_MEDIA_PLAY, null);
204                         XObject.prototype.init.call(this);
205                     },
206                     label: 'Resume Commits',
207                     always_show_image : true,
208                     accel_group : null,
209                     id : 'resume',
210                     //label: 'Pause',
211                     pack:  'append',
212                     listeners : {
213                         activate : function () {
214                             this.parent.parent.paused = false;
215                              imports.GitMonitor.GitMonitor.start();
216                             //var status = this.el.label == 'Pause' ? 1 : 0
217                            // this.el.label  = status ? 'Resume' : 'Pause';
218                                
219                             
220                         }
221                     }
222                 },
223                 
224                 {
225                     init : function() {
226                         this.el = XObject.isSeed ?
227                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_FULLSCREEN)
228                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_FULLSCREEN, null);
229                       
230                         XObject.prototype.init.call(this);
231                     },
232                     label: 'Pull (Refresh) All',
233                     always_show_image : true,
234                     accel_group : null,
235                     //label: 'Pause',
236                     pack:  'append',
237                     listeners : {
238                         activate : function () {
239                             imports.GitMonitor.GitMonitor.stop();
240                            
241                             
242                             var tr = imports.Scm.Repo.Repo.list();
243                             for (var i= 0; i< tr.length;i++) {
244                                 this.parent.parent.el.set_from_stock( i%2 ?  Gtk.STOCK_FULLSCREEN : Gtk.STOCK_LEAVE_FULLSCREEN );
245                                 
246                                 var repo = tr[i];
247                                 if (!repo.autocommit()) {
248                                     //??? should we ignore ones not on autocommit..
249                                     continue;
250                                 }
251                                 try {
252                                     this.parent.parent.el.set_tooltip_text("pull: " + repo.name);
253                                
254                                     var str = repo.pull();
255                                     // do not care if it's already in sycn..
256                                     if (str.match(/Already up-to-date/)) {
257                                         continue;
258                                     }
259                                     var notification = new Notify.Notification({
260                                        summary: "Updated " + repo.name,
261                                        body : str
262                                    });
263                                    notification.set_timeout(20);
264                                    notification.show();
265                                    
266                                 } catch(e) {
267                                     this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_RECORD );
268                                     print(JSON.stringify(e));
269                                     print("notification or push errror- probably to many in queue..");
270                                     imports.gitlive.errorDialog(e.message);
271
272                                 }
273                             }
274                             this.parent.parent.el.set_tooltip_text(this.parent.parent.tooltip_text);
275                                
276                             
277                              
278                           
279                             imports.GitMonitor.GitMonitor.start();
280                         }   
281                     }
282                 },
283                 {
284                     init : function() {
285                         this.el = XObject.isSeed ?
286                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_SAVE)
287                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_SAVE, null);
288                          
289                         XObject.prototype.init.call(this);
290                     },
291                     label: 'Update Timesheet',
292                     always_show_image : true,
293                     accel_group : null,
294                     
295                     //label: 'Pause',
296                     pack:  'append',
297                     listeners : {
298                         activate : function () {
299                             var ret = imports.FixBug.FixBug.show();
300                             
301                         }
302                     }
303                 },
304                 
305                 
306                 {
307                     init : function() {
308                         this.el = XObject.isSeed ?
309                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_FULLSCREEN)
310                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_FULLSCREEN, null);
311                          XObject.prototype.init.call(this);
312                     },
313                     label: 'Manage Clones',
314                     always_show_image : true,
315                     accel_group : null,
316                     
317                     //label: 'Pause',
318                     pack:  'append',
319                     listeners : {
320                         activate : function () {
321                              var ret = imports.Clones.Clones.show();
322                             
323                         }
324                     }
325                 },
326                 
327                 
328                 
329             
330                 {
331                     init : function() {
332                         this.el = XObject.isSeed ?
333                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT)
334                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_ABOUT, null);
335                    
336                         XObject.prototype.init.call(this);
337                     },
338                     label: 'About GitLive',
339                     pack:  'append',
340                     listeners : {
341                         activate : function () {
342                             var msg = new Gtk.AboutDialog({
343                                 program_name : "Git Live",
344                                 version: '0.3',
345                                 website: 'http://www.roojs.org/index.php/projects/gitlive.html',
346                                 website_label: 'RooJS Consulting',
347                                 license : 'LGPL'
348                             });
349                             msg.set_authors([ "Alan Knowles <alan@roojs.com>" ]);
350                             msg.run();
351                             msg.destroy();
352                         }
353                     }
354                 },
355                 
356                 {
357                     init : function() {
358                         
359                         this.el = XObject.isSeed ?
360                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT)
361                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, null);
362                      
363                         XObject.prototype.init.call(this);
364                     },
365                     label: 'Quit',
366                     pack:  'append',
367                     listeners : {
368                         activate : function () {
369                             Seed.quit();
370                         }
371                     }
372                 }
373                 
374                 
375             ]
376         }
377     ]
378     
379 });
380 */
381
382