StatusIcon.js
[gitlive] / StatusIcon.js
1 //<script type="text/javascript">
2
3
4 /**
5  * Status icon and menu for component of gitlive.
6  * 
7  * Implements XObject - however we want to control initialization.
8  * 
9  * 
10  * 
11  * Currently only does a few things
12  * a) Quit
13  * 
14  * b) Pause!??!
15  */
16  
17 Gtk      = imports.gi.Gtk;
18 Gdk      = imports.gi.Gdk;
19 Gio      = imports.gi.Gio;
20 GLib     = imports.gi.GLib;
21 Notify   = imports.gi.Notify;
22
23 Git = imports.Git;
24 XObject = imports.XObject.XObject
25 gitlive = imports.gitlive;
26
27  
28 StatusIcon  = new XObject({
29     
30     paused : false, // on!
31     xtype : Gtk.StatusIcon,
32     stock : Gtk.STOCK_MEDIA_PLAY,
33     tooltip_text : 'GitLive',
34     listeners : {
35         //'popup-menu' : function( w, event, event_time) {
36         'activate' : function( w, event, event_time) {
37             print(Array.prototype.slice.call(arguments).join(','));
38             
39             menu = this.get('menu');
40             
41             menu.el.show_all();
42             
43             this.get(!this.paused ? 'resume' : 'pause' ).el.hide();
44             
45             menu.el.popup(null, null,Gtk.StatusIcon.position_menu , this.el , 1, Gtk.get_current_event_time());
46             //menu.el.popup(null, null,null, null, 1, Gtk.get_current_event_time());
47             
48             return;
49             
50             var g = { };
51             var a = new Gdk.Rectangle();
52             //  needs direction=inout setting in gir to work (in bugzilla @present)
53             this.el.get_geometry(g,a,null);
54              
55             // should check to see if @ top or bottom I guess..
56             menu.el.get_toplevel().move(a.x, a.y +a.height);
57              
58         }
59     },
60     items : [
61        {
62             xtype: Gtk.Menu,
63             id : 'menu',
64             pack: false,
65             items : [
66                 {
67                     init : function() {
68                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PAUSE);
69                         XObject.prototype.init.call(this);
70                     },
71                     label: 'Pause Commits',
72                    
73                     always_show_image : true,
74                     accel_group : null,
75                     id : 'pause',
76                     //label: 'Pause',
77                     pack:  'append',
78                     listeners : {
79                         activate : function () {
80                             this.parent.parent.paused = true;
81                             gitlive.monitor.stop();
82                            // this.el.label  = status ? 'Resume' : 'Pause';
83                             this.parent.parent.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
84                             
85                         }
86                     }
87                 },
88                 
89                 {
90                     init : function() {
91                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PLAY);
92                         XObject.prototype.init.call(this);
93                     },
94                     label: 'Resume Commits',
95                     always_show_image : true,
96                     accel_group : null,
97                     id : 'resume',
98                     //label: 'Pause',
99                     pack:  'append',
100                     listeners : {
101                         activate : function () {
102                             this.parent.parent.paused = false;
103                             gitlive.monitor.start();
104                             //var status = this.el.label == 'Pause' ? 1 : 0
105                            // this.el.label  = status ? 'Resume' : 'Pause';
106                             this.parent.parent.el.set_from_stock(   Gtk.STOCK_MEDIA_PLAY);
107                             
108                         }
109                     }
110                 },
111                 
112                 {
113                     init : function() {
114                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_RELOAD);
115                         XObject.prototype.init.call(this);
116                     },
117                     label: 'Pull (Refresh) All',
118                     always_show_image : true,
119                     accel_group : null,
120                     //label: 'Pause',
121                     pack:  'append',
122                     listeners : {
123                         activate : function () {
124                             gitlive.monitor.stop();
125                             
126                             var f = Gio.file_new_for_path(gitlive.gitlive);
127                             var file_enum = f.enumerate_children(
128                                 Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
129
130                             var next_file = null;
131                             
132                             var err = [];
133                             while ((next_file = file_enum.next_file(null)) != null) {
134                                 
135                                 var fn = gitlive.gitlive + '/' + next_file.get_display_name();
136                                 if (! GLib.file_test(fn + '/.git', GLib.FileTest.IS_DIR)) {
137                                     continue;
138                                 }
139                                 
140                                 
141                                 var res = Git.run(fn,  'pull' );
142                                 if (res.result * 1  == 0) {
143                                         
144                                     var notification = new Notify.Notification({
145                                         summary: "Updated " + fn,
146                                         body : res.output
147                                     });
148                                     notification.set_timeout(500);
149                                     notification.show();
150                                     continue;
151                                 }
152                                 gitlive.errorDialog(res.stderr);
153                                     // should also update modules ideally.
154                                 
155                             }
156                             if (err.length) {
157                                 gitlive.errorDialog(err.join("\n"));
158                             }
159                             
160                                 
161                             file_enum.close(null);
162
163                             
164                             gitlive.monitor.start();
165                             
166                         }
167                     }
168                 },
169                 {
170                     init : function() {
171                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_RELOAD);
172                         XObject.prototype.init.call(this);
173                     },
174                     label: 'Manage Clones',
175                     always_show_image : true,
176                     accel_group : null,
177                     
178                     //label: 'Pause',
179                     pack:  'append',
180                     listeners : {
181                         activate : function () {
182                              
183                             
184                         }
185                     }
186                 },
187                 
188                 
189                 
190             
191                 {
192                     init : function() {
193                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT);
194                         XObject.prototype.init.call(this);
195                     },
196                     label: 'About GitLive',
197                     pack:  'append',
198                     listeners : {
199                         activate : function () {
200                             var msg = new Gtk.AboutDialog({
201                                 program_name : "Git Live",
202                                 version: '0.1',
203                                 website: 'http://git.akbkhome.com',
204                                 website_label: 'AK BK Consulting (git repo)',
205                                 license : 'LGPL'
206                             });
207                             msg.set_authors([ "Alan Knowles <alan@akbkhome.com>" ]);
208                             msg.run();
209                             msg.destroy();
210                         }
211                     }
212                 },
213                 
214                 {
215                     init : function() {
216                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT);
217                         XObject.prototype.init.call(this);
218                     },
219                     label: 'Quit',
220                     pack:  'append',
221                     listeners : {
222                         activate : function () {
223                             Seed.quit();
224                         }
225                     }
226                 }
227                 
228                 
229             ]
230         }
231     ]
232     
233 });
234
235