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 = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PLAY);
100                         XObject.prototype.init.call(this);
101                     },
102                     label: 'Resume Commits',
103                     always_show_image : true,
104                     accel_group : null,
105                     id : 'resume',
106                     //label: 'Pause',
107                     pack:  'append',
108                     listeners : {
109                         activate : function () {
110                             this.parent.parent.paused = false;
111                              imports.GitMonitor.GitMonitor.start();
112                             //var status = this.el.label == 'Pause' ? 1 : 0
113                            // this.el.label  = status ? 'Resume' : 'Pause';
114                                
115                             
116                         }
117                     }
118                 },
119                 
120                 {
121                     init : function() {
122                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_RELOAD);
123                         XObject.prototype.init.call(this);
124                     },
125                     label: 'Pull (Refresh) All',
126                     always_show_image : true,
127                     accel_group : null,
128                     //label: 'Pause',
129                     pack:  'append',
130                     listeners : {
131                         activate : function () {
132                             imports.GitMonitor.GitMonitor.stop();
133                            
134                             
135                             var tr = imports.Scm.Repo.Repo.list();
136                             for (var i= 0; i< tr.length;i++) {
137                                 this.parent.parent.el.set_from_stock( i%2 ?  Gtk.STOCK_FULLSCREEN : Gtk.STOCK_LEAVE_FULLSCREEN );
138                                 
139                                 var repo = tr[i];
140                                 if (!repo.autocommit()) {
141                                     //??? should we ignore ones not on autocommit..
142                                     continue;
143                                 }
144                                 try {
145                                     this.parent.parent.el.set_tooltip_text("pull: " + repo.name);
146                                
147                                     var str = repo.pull();
148                                     // do not care if it's already in sycn..
149                                     if (str.match(/Already up-to-date/)) {
150                                         continue;
151                                     }
152                                     var notification = new Notify.Notification({
153                                        summary: "Updated " + repo.name,
154                                        body : str
155                                    });
156                                    notification.set_timeout(20);
157                                    notification.show();
158                                    
159                                 } catch(e) {
160                                     this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_RECORD );
161                                     print(JSON.stringify(e));
162                                     print("notification or push errror- probably to many in queue..");
163                                     imports.gitlive.errorDialog(e.message);
164
165                                 }
166                             }
167                             this.parent.parent.el.set_tooltip_text(this.parent.parent.tooltip_text);
168                                
169                             
170                              
171                           
172                             imports.GitMonitor.GitMonitor.start();
173                         }   
174                     }
175                 },
176                 {
177                     init : function() {
178                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_SAVE);
179                         XObject.prototype.init.call(this);
180                     },
181                     label: 'Update Timesheet',
182                     always_show_image : true,
183                     accel_group : null,
184                     
185                     //label: 'Pause',
186                     pack:  'append',
187                     listeners : {
188                         activate : function () {
189                             var ret = imports.FixBug.FixBug.show();
190                             
191                         }
192                     }
193                 },
194                 
195                 
196                 {
197                     init : function() {
198                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_RELOAD);
199                         XObject.prototype.init.call(this);
200                     },
201                     label: 'Manage Clones',
202                     always_show_image : true,
203                     accel_group : null,
204                     
205                     //label: 'Pause',
206                     pack:  'append',
207                     listeners : {
208                         activate : function () {
209                              var ret = imports.Clones.Clones.show();
210                             
211                         }
212                     }
213                 },
214                 
215                 
216                 
217             
218                 {
219                     init : function() {
220                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT);
221                         XObject.prototype.init.call(this);
222                     },
223                     label: 'About GitLive',
224                     pack:  'append',
225                     listeners : {
226                         activate : function () {
227                             var msg = new Gtk.AboutDialog({
228                                 program_name : "Git Live",
229                                 version: '0.3',
230                                 website: 'http://www.roojs.org/index.php/projects/gitlive.html',
231                                 website_label: 'RooJS Consulting',
232                                 license : 'LGPL'
233                             });
234                             msg.set_authors([ "Alan Knowles <alan@roojs.com>" ]);
235                             msg.run();
236                             msg.destroy();
237                         }
238                     }
239                 },
240                 
241                 {
242                     init : function() {
243                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT);
244                         XObject.prototype.init.call(this);
245                     },
246                     label: 'Quit',
247                     pack:  'append',
248                     listeners : {
249                         activate : function () {
250                             Seed.quit();
251                         }
252                     }
253                 }
254                 
255                 
256             ]
257         }
258     ]
259     
260 });
261
262