Builder/Provider/Project/Base.js
[app.Builder.js] / Builder / Provider / Project / Base.js
1 //<Script type="text/javascript">
2
3 Gio = imports.gi.Gio;
4 GLib = imports.gi.GLib; 
5
6
7 console = imports.console;
8 XObject = imports.XObject.XObject;
9
10 ProjectManager = imports.Builder.Provider.ProjectManager.ProjectManager;
11 Observable = imports.Observable.Observable;
12
13 Base = XObject.define( 
14     function(cfg) {
15         
16         
17         this.addEvents({ 'changed'  : true });
18         var _this = this;
19         this.on('changed' , function() {
20             Seed.print("Calling PM - changed");
21             
22             ProjectManager.fireEvent('changed');
23         });
24         XObject.extend(this, cfg);
25         
26         // 
27         if (this.files ){
28             for (var f in this.files) {
29                 var xt = this.xtype;
30                 var cls = imports.Builder.Provider.File[xt][xt];
31                 this.files[f] = cls(this.files[f]);
32             }
33         }
34         
35         
36         this.scanDirs();
37         
38     },
39     Observable, 
40     {
41         id : false,
42         fn:  false,
43         paths : false,
44         files : false,
45         tree : false,
46         xtype : false,
47         
48         load : function (o) 
49         {
50             if (!this.fetchTree) {
51                 console.log("Project.getTree tree called on base object?!?!");
52                 return false;
53             }
54             
55             if (this.files) {
56                 return o.success.apply(o.scope || this, [this]);
57             }
58             return this.fetchTree(o);
59             
60         },
61         getPalete : function()
62         {
63             print("GET PROVIDER FOR " + this.xtype);
64             return  ProjectManager.getPalete(this.xtype);
65         },
66         toJSON : function()
67         {
68             var ret = { };
69             var _this = this;
70             ['id', 'fn', 'paths', 'xtype', 'name'].forEach(  function(k) {
71                 ret[k] = _this[k];
72                 
73             });
74             ret.files = { }; 
75             // deal with files..
76             for (var f in this.files) {
77                 print(f);
78                 ret.files[f] = this.files[f].toJsonArray();
79             }
80             
81             
82             return JSON.stringify(ret);
83           
84           
85         },
86         toTree : function()
87         {
88             
89             
90             // normally this will build tree's based on info in the file stuff..
91             // but for the time being, we just return an array of all the files.
92             
93             
94             
95             //if (this.tree) {
96              //   return this.tree;
97             //}
98             
99             this.tree = [];
100             /*
101             for (var f in this.files) {
102                 this.tree.push(this.files[f]);
103             }
104             return this.tree;
105             */
106             
107             // have parents -> add them to parent...
108             var files = {};
109             var parents = {};
110             for (var k in this.files) {
111                 
112                 var f = this.files[k];
113                 if (!f) {
114                     continue;
115                    }
116                 console.dump(f);
117                 f.hasParent = false;
118                 f.cn = [];
119                 //console.log(f.name);
120                 if (f.fullname) {
121                     files[f.fullname] = f;
122                 }
123             };
124             
125             // add Parent pointer..
126             for (var k in this.files) {
127                 var f = this.files[k];
128                 console.log(f.parent + ":" + f.name);
129                 if (f.parent && typeof(files[f.parent]) != 'undefined') {
130                     // add it to parent;
131                     
132                     files[f.parent].cn.push(f);
133                     f.hasParent = true;
134                     
135                 }
136                 
137                 
138             };
139             
140               
141             
142             var ret = [];
143             for (var k in this.files) {
144                 var f = this.files[k];
145                 
146                 f.sortCn();
147                 if (f.hasParent) {
148                     continue;
149                 }
150                 if (typeof(files[f.fullname]) != 'undefined') {
151                     ret.push(f);
152                 }
153                 
154             };
155             
156             
157             ret.sort(function(a,b) {
158                 return a.path > b.path ? 1 : -1;
159             });
160             this.tree = ret;
161             return this.tree;
162              
163             
164         },
165         getById : function(id)
166         {
167             var ret = false;
168             for (var k in this.files) {
169                 var f = this.files[k];
170                 
171                 console.log(f.id + '?=' + id);
172                 if (f.id == id) {
173                     return f;
174                 }
175             };
176             return ret;
177         },
178         
179         loadFileOnly : function(path)
180         {
181             
182             var xt = this.xtype;
183             var cls = imports.Builder.Provider.File[xt][xt];
184             return  new cls({
185                 path : path,
186                 parent : ''
187             });
188         },
189         create : function(filename)
190         {
191             var ret = this.loadFileOnly(filename);
192             ret.save();
193             this.addFile(ret);
194             return ret;
195             
196         },
197         
198         
199         addFile: function(pfile) { // add a single file, and trigger changed.
200             this.files[pfile.path] = pfile
201             this.fireEvent('changed', this);
202         },
203         
204         add: function(path, type)
205         {
206             this.paths = this.paths || { };
207             this.paths[path] = type;
208             //Seed.print(" type is '" + type + "'");
209             if (type == 'dir') {
210                 this.scanDir(path);
211             //    console.dump(this.files);
212             }
213             if (type == 'file' ) {
214                 
215                 this.files[path] = this.loadFileOnly( path );
216             }
217             
218              console.dump(this.files);
219             this.fireEvent('changed', this);
220             
221         },
222         
223         scanDirs: function()
224         {
225             this.files = this.files  || { };
226             for (var d in this.paths) {
227                 if (this.paths[d] == 'dir') {
228                     this.scanDir(d);
229                 }
230                 // otherwise add a file..
231             }
232             //console.dump(this.files);
233             
234         },
235         
236         
237         // list files.
238         scanDir : function(dir, dp) 
239         {
240             dp = dp || 0;
241             Seed.print("Running scandir on " + dir);
242             if (dp > 5) { // no more than 5 deep?
243                 return;
244             }
245             // this should be done async -- but since we are getting the proto up ...
246             var gdir = GLib.dir_open(dir,0);
247             var subs = [];
248             var _this = this;
249             while (true) {
250                 var fn = GLib.dir_read_name(gdir);
251                 console.log('trying ' + dir + '/' + fn);
252                 if (!fn) {
253                     GLib.dir_close(gdir);
254                     subs.forEach( function(s) {
255                         _this.scanDir(s, dp+1);
256                     });
257                     return;
258                 }
259                 if (fn[0] == '.') { // skip hidden
260                     continue;
261                 }
262                 
263                 if (GLib.file_test(dir  + '/' + fn, GLib.FileTest.IS_DIR)) {
264                     subs.push(dir  + '/' + fn);
265                     continue;
266                 }
267                 
268                 if (!fn.match(/\.bjs$/)) {
269                     continue;
270                 }
271                 var parent = '';
272                 //if (dp > 0 ) {
273                 var sp = dir.split('/');
274                 sp = sp.splice(sp.length - (dp +1), (dp +1));
275                 parent = sp.join('.');
276                 
277                 
278                 if (typeof(this.files[dir  + '/' + fn]) != 'undefined') {
279                     // we already have it..
280                     this.files[dir  + '/' + fn].parent = parent;
281                     continue;
282                 }
283                 var xt = this.xtype;
284                 var cls = imports.Builder.Provider.File[xt][xt];
285                 
286                 Seed.print("Adding file " + dir  + '/' + fn);
287                 this.files[dir  + '/' + fn] = new cls({
288                     path : dir  + '/' + fn,
289                     parent : parent
290                 });
291                 console.log(this.files[dir  + '/' + fn] );
292                 /*
293                 var f = Gio.file_new_for_path(dir + '/' + fn);
294                 var inf = f.query_info('standard::*');
295                 var tv = new GLib.TimeVal();
296                 inf.get_modification_time(tv);
297                 
298                 // should it do something about this information..
299                 // fixme... time data is busted..
300                 this.files[dir  + '/' + fn] = '' + tv.tv_sec + '.' + tv.tv_usec;
301                 */
302             }
303              
304             
305         }
306         
307         
308         
309         
310         
311
312     }
313 ); 
314
315
316      
317