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
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                             var f = Gio.file_new_for_path(gitlive.gitlive);
126                             var file_enum = f.enumerate_children(
127                                 Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
128
129                             var next_file = null;
130                             
131                             var err = [];
132                             while ((next_file = file_enum.next_file(null)) != null) {
133                                 
134                                 var fn = next_file.get_path();
135                                 if (! GLib.file_test(fn + '/.git', GLib.FileTest.IS_DIR)) {
136                                     continue;
137                                 }
138                                 
139                                 try {
140                                     var res = Git.run(fn, [ 'pull' ]);
141                                     var notification = new Notify.Notification({
142                                         summary: "Updated " + fn,
143                                         body : res
144                                     });
145                                     // should also update modules ideally.
146                                 } catch (e) {
147                                     err.push(new String(e));
148                                 }
149                             }
150                             if (err.length) {
151                                 gitlive.errordialog(e.join("\n"));
152                             }
153                             
154                                 
155                             file_enum.close(null);
156
157                             
158                             gitlive.monitor.start();
159                             
160                         }
161                     }
162                 },
163                 {
164                     init : function() {
165                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_RELOAD);
166                         XObject.prototype.init.call(this);
167                     },
168                     label: 'Manage Clones',
169                     always_show_image : true,
170                     accel_group : null,
171                     
172                     //label: 'Pause',
173                     pack:  'append',
174                     listeners : {
175                         activate : function () {
176                              
177                             
178                         }
179                     }
180                 },
181                 
182                 
183                 
184             
185                 {
186                     init : function() {
187                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_ABOUT);
188                         XObject.prototype.init.call(this);
189                     },
190                     label: 'About GitLive',
191                     pack:  'append',
192                     listeners : {
193                         activate : function () {
194                             var msg = new Gtk.MessageDialog({message_type:
195                                 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - auto commits and pushes everything in ~/gitlive"});
196                             msg.run();
197                             msg.destroy();
198                         }
199                     }
200                 },
201                 
202                 {
203                     init : function() {
204                         this.el = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_QUIT);
205                         XObject.prototype.init.call(this);
206                     },
207                     label: 'Quit',
208                     pack:  'append',
209                     listeners : {
210                         activate : function () {
211                             Seed.quit();
212                         }
213                     }
214                 }
215                 
216                 
217             ]
218         }
219     ]
220     
221 });
222
223