ProjectManager.js
[app.Builder.js] / ProjectManager.js
1 //<Script type="text/javascript">
2 Gio         = imports.gi.Gio;
3 GLib        = imports.gi.GLib;
4
5
6 console     = imports.console;
7 XObject     = imports.XObject.XObject;
8  
9 Observable  = imports.Observable.Observable;
10 File        = imports.File.File;
11 /**
12  * 
13  * /home/alan/.BuilderConfig/*
14  * 
15  * 
16  */
17
18 ProjectManager =  new Observable({
19     
20     events : {
21         'changed' : true
22     },
23     
24     
25     listeners : {
26         'changed' :  function() {
27             this.saveConfig();
28         }
29         
30     },
31     
32     palete : { }, 
33     projects : [],
34     filename : false,
35     
36     loadConfig : function ()
37     {
38         // we can not do this async.... 
39         this.dirname = GLib.get_home_dir() + '/.Builder'; 
40         
41         var dir = Gio.file_new_for_path(this.dirname);
42         if (!dir.query_exists()) {
43             dir.make_directory();
44             return;
45         }
46       
47         this.projects = [];
48         var files = File.list(this.dirname);
49         
50         
51         print(files);Seed.quit();
52         for (var i =0 ; i < files.length;i++) {
53             var fn = files[i];
54              
55             if (!fn.match(/.json$/)) {
56                 continue;
57             }
58             var str = File.read(this.dirname + '/' + fn);
59             if (!str || !str.length) {
60                 continue; // empty file.
61             }
62              
63             
64             var ar = JSON.parse(str); 
65             Seed.print(ar.xtype);
66             
67             // construct...
68             var cls = imports.Project[ar.xtype][ar.xtype];
69             this.projects.push( new cls(ar));
70             
71             
72             
73             
74             
75              
76         }
77    
78         this.projects.sort(function(a,b) {
79             if (a.getName() == b.getName()) {
80                 return 0;
81             }
82             return a.getName() > b.getName() ? 1 : -1;
83             
84             
85         });
86    
87         
88        
89         
90         
91         
92         
93         
94         
95     },
96     
97     
98     saveConfig : function()
99     {
100         var _this = this;
101         this.projects.forEach( function(p) {
102             
103             if (!p.fn) {
104                 // make the filename..
105                 var t = new GLib.TimeVal();
106                 GLib.get_current_time(t);
107                 var str = '' + t.tv_sec + ':' + t.tv_usec;
108                 Seed.print(str);
109                 p.fn = GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, str, str.length);
110                 Seed.print(p.fn);
111
112             }
113             var  s =  p.toJSON();
114             File.write(_this.dirname + '/' + p.fn + '.json', s);
115            
116            
117         });
118         
119         
120         
121         
122     },
123     update : function(proj) {
124         
125         Seed.print(JSON.stringify(proj));
126         var found = false;
127         this.projects.forEach( function(p) {
128             if (proj == p) {
129                 found = true;
130                 return true;
131             }
132         });
133         if (found) {
134             this.fireEvent('changed', this);
135             return proj;
136             return;
137         }
138         var cls = imports.Project[proj.xtype][proj.xtype];
139         var pr = new cls(proj);
140         this.projects.push(pr );
141         this.fireEvent('changed', this);
142         return pr;
143         
144         
145     },
146     
147     deleteProject : function (fn)
148     {
149         var newplist = [];
150         var _this = this;
151         this.projects.forEach(function(p) {
152             if (p.fn != fn) {
153                 newplist.push(p);
154                 return;
155             }
156             var file = _this.dirname + '/' + p.fn + '.json';
157             if (File.exists(file)) {
158                 File.remove(file);
159             }
160             
161         });
162         this.projects = newplist;
163         this.fireEvent('changed', this);
164     },
165     
166     
167     getByFn : function (fn) {
168         var  ret = false;
169         this.projects.forEach(function(p) {
170             if (p.fn == fn) {
171                 ret = p;
172                 return true;
173             }
174         });
175         return ret;
176         
177     },
178     getPalete: function(type) {
179         if (typeof(ProjectManager.palete[type]) != 'undefined') {
180             //print ("ALREADY GOT " + type);
181             return ProjectManager.palete[type];
182         }
183         var cls = imports.Palete[type][type];
184         
185         ProjectManager.palete[type] =  new cls();
186         print (typeof(ProjectManager.palete[type]));
187         return ProjectManager.palete[type];
188     }
189
190     
191     
192 });
193
194
195
196