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