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