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