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