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