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