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                                 try {
141                                     var res = Git.run(fn,  'pull' );
142                                     var notification = new Notify.Notification({
143                                         summary: "Updated " + fn,
144                                         body : res
145                                     });
146                                     notification.set_timeout(500);
147                                     notification.show();
148                                     // should also update modules ideally.
149                                 } catch (e) {
150                                     err.push(e.toString());
151                                 }
152                             }
153                             if (err.length) {
154                                 gitlive.errorDialog(err.join("\n"));
155                             }
156                             
157                                 
158                             file_enum.close(null);
159
160                             
161                             gitlive.monitor.start();
162                             
163                         }
164                     }
165                 },
166                 {
167                     init : function() {
168                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_RELOAD);
169                         XObject.prototype.init.call(this);
170                     },
171                     label: 'Manage Clones',
172                     always_show_image : true,
173                     accel_group : null,
174                     
175                     //label: 'Pause',
176                     pack:  'append',
177                     listeners : {
178                         activate : function () {
179                              
180                             
181                         }
182                     }
183                 },
184                 
185                 
186                 
187             
188                 {
189                     init : function() {
190                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT);
191                         XObject.prototype.init.call(this);
192                     },
193                     label: 'About GitLive',
194                     pack:  'append',
195                     listeners : {
196                         activate : function () {
197                             var msg = new Gtk.AboutDialog({
198                                 program_name : "Git Live",
199                                 version: '0.1',
200                                 website: 'http://git.akbkhome.com',
201                                 website_label: 'AK BK Consulting (git repo)',
202                                 license : 'LGPL'
203                             });
204                             msg.set_authors([ "Alan Knowles" ]);
205                             msg.run();
206                             msg.destroy();
207                         }
208                     }
209                 },
210                 
211                 {
212                     init : function() {
213                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT);
214                         XObject.prototype.init.call(this);
215                     },
216                     label: 'Quit',
217                     pack:  'append',
218                     listeners : {
219                         activate : function () {
220                             Seed.quit();
221                         }
222                     }
223                 }
224                 
225                 
226             ]
227         }
228     ]
229     
230 });
231
232