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