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