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