StatusIcon.js
[gitlive] / StatusIcon.js
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 var Gtk      = imports.gi.Gtk;
15 var Gdk      = imports.gi.Gdk;
16 var Gio      = imports.gi.Gio;
17 var GLib     = imports.gi.GLib;
18 var Notify   = imports.gi.Notify;
19
20 var XObject = imports.XObject.XObject;
21
22 //var gitlive = imports.gitlive;
23
24  
25 var StatusIcon  = new XObject({
26     
27     paused : false, // on!
28     xtype : Gtk.StatusIcon,
29     title : 'gitlive',
30     stock : Gtk.STOCK_REFRESH,
31     tooltip_text : 'GitLive',
32         init : function() {
33         XObject.prototype.init.call(this);
34         this.el.set_name('gitlive');
35     },
36     listeners : {
37         //'popup-menu' : function( w, event, event_time) {
38         'activate' : function( w, event, event_time) {
39             print(Array.prototype.slice.call(arguments).join(','));
40             
41             var menu = this.get('menu');
42             
43             menu.el.show_all();
44             
45             this.get(!this.paused ? 'resume' : 'pause' ).el.hide();
46             print("MENU EL: "  + menu.el);
47             print("POPUP: " + typeof(menu.el.popup));
48             
49             
50             menu.el.popup(null, null,Gtk.StatusIcon.position_menu , this.el , 1, Gtk.get_current_event_time());
51             //menu.el.popup(null, null,null, null, 1, Gtk.get_current_event_time());
52             
53             return;
54             
55             var g = { };
56             var a = new Gdk.Rectangle();
57             //  needs direction=inout setting in gir to work (in bugzilla @present)
58             this.el.get_geometry(g,a,null);
59              
60             // should check to see if @ top or bottom I guess..
61             menu.el.get_toplevel().move(a.x, a.y +a.height);
62              
63         }
64     },
65     items : [
66        {
67             xtype: Gtk.Menu,
68             id : 'menu',
69             pack: false,
70             items : [
71                 {
72                     init : function() {
73                         
74                         this.el = XObject.isSeed ?
75                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PAUSE)
76                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_MEDIA_PAUSE, null);
77                         XObject.prototype.init.call(this);
78                     },
79                     label: 'Pause Commits',
80                    
81                     always_show_image : true,
82                     accel_group : null,
83                     id : 'pause',
84                     //label: 'Pause',
85                     pack:  'append',
86                     listeners : {
87                         activate : function () {
88                             this.parent.parent.paused = true;
89                             imports.GitMonitor.GitMonitor.stop();
90                            // this.el.label  = status ? 'Resume' : 'Pause';
91                             this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
92                             
93                         }
94                     }
95                 },
96                 
97                 {
98                     init : function() {
99                         this.el = XObject.isSeed ?
100                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PLAY)
101                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_MEDIA_PLAY, null);
102                         XObject.prototype.init.call(this);
103                     },
104                     label: 'Resume Commits',
105                     always_show_image : true,
106                     accel_group : null,
107                     id : 'resume',
108                     //label: 'Pause',
109                     pack:  'append',
110                     listeners : {
111                         activate : function () {
112                             this.parent.parent.paused = false;
113                              imports.GitMonitor.GitMonitor.start();
114                             //var status = this.el.label == 'Pause' ? 1 : 0
115                            // this.el.label  = status ? 'Resume' : 'Pause';
116                                
117                             
118                         }
119                     }
120                 },
121                 
122                 {
123                     init : function() {
124                         this.el = XObject.isSeed ?
125                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_FULLSCREEN)
126                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_FULLSCREEN, null);
127                       
128                         XObject.prototype.init.call(this);
129                     },
130                     label: 'Pull (Refresh) All',
131                     always_show_image : true,
132                     accel_group : null,
133                     //label: 'Pause',
134                     pack:  'append',
135                     listeners : {
136                         activate : function () {
137                             imports.GitMonitor.GitMonitor.stop();
138                            
139                             
140                             var tr = imports.Scm.Repo.Repo.list();
141                             for (var i= 0; i< tr.length;i++) {
142                                 this.parent.parent.el.set_from_stock( i%2 ?  Gtk.STOCK_FULLSCREEN : Gtk.STOCK_LEAVE_FULLSCREEN );
143                                 
144                                 var repo = tr[i];
145                                 if (!repo.autocommit()) {
146                                     //??? should we ignore ones not on autocommit..
147                                     continue;
148                                 }
149                                 try {
150                                     this.parent.parent.el.set_tooltip_text("pull: " + repo.name);
151                                
152                                     var str = repo.pull();
153                                     // do not care if it's already in sycn..
154                                     if (str.match(/Already up-to-date/)) {
155                                         continue;
156                                     }
157                                     var notification = new Notify.Notification({
158                                        summary: "Updated " + repo.name,
159                                        body : str
160                                    });
161                                    notification.set_timeout(20);
162                                    notification.show();
163                                    
164                                 } catch(e) {
165                                     this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_RECORD );
166                                     print(JSON.stringify(e));
167                                     print("notification or push errror- probably to many in queue..");
168                                     imports.gitlive.errorDialog(e.message);
169
170                                 }
171                             }
172                             this.parent.parent.el.set_tooltip_text(this.parent.parent.tooltip_text);
173                                
174                             
175                              
176                           
177                             imports.GitMonitor.GitMonitor.start();
178                         }   
179                     }
180                 },
181                 {
182                     init : function() {
183                         this.el = XObject.isSeed ?
184                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_SAVE)
185                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_SAVE, null);
186                          
187                         XObject.prototype.init.call(this);
188                     },
189                     label: 'Update Timesheet',
190                     always_show_image : true,
191                     accel_group : null,
192                     
193                     //label: 'Pause',
194                     pack:  'append',
195                     listeners : {
196                         activate : function () {
197                             var ret = imports.FixBug.FixBug.show();
198                             
199                         }
200                     }
201                 },
202                 
203                 
204                 {
205                     init : function() {
206                         this.el = XObject.isSeed ?
207                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_FULLSCREEN)
208                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_FULLSCREEN, null);
209                          XObject.prototype.init.call(this);
210                     },
211                     label: 'Manage Clones',
212                     always_show_image : true,
213                     accel_group : null,
214                     
215                     //label: 'Pause',
216                     pack:  'append',
217                     listeners : {
218                         activate : function () {
219                              var ret = imports.Clones.Clones.show();
220                             
221                         }
222                     }
223                 },
224                 
225                 
226                 
227             
228                 {
229                     init : function() {
230                               this.el = XObject.isSeed ?
231                             new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT)
232                             : new Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_ABOUT, null);
233                    
234                         XObject.prototype.init.call(this);
235                     },
236                     label: 'About GitLive',
237                     pack:  'append',
238                     listeners : {
239                         activate : function () {
240                             var msg = new Gtk.AboutDialog({
241                                 program_name : "Git Live",
242                                 version: '0.3',
243                                 website: 'http://www.roojs.org/index.php/projects/gitlive.html',
244                                 website_label: 'RooJS Consulting',
245                                 license : 'LGPL'
246                             });
247                             msg.set_authors([ "Alan Knowles <alan@roojs.com>" ]);
248                             msg.run();
249                             msg.destroy();
250                         }
251                     }
252                 },
253                 
254                 {
255                     init : function() {
256                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT);
257                         XObject.prototype.init.call(this);
258                     },
259                     label: 'Quit',
260                     pack:  'append',
261                     listeners : {
262                         activate : function () {
263                             Seed.quit();
264                         }
265                     }
266                 }
267                 
268                 
269             ]
270         }
271     ]
272     
273 });
274
275