resources/RooUsage.txt
[app.Builder.js] / old-javascript / 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              print(fn);
55             if (!fn.match(/\.json$/)) {
56                 continue;
57             }
58             var str = File.read(this.dirname + '/' + fn);
59             if (!str || !str.length) {
60                 print("EMPTY");
61                 continue; // empty file.
62             }
63              
64             
65             var ar = JSON.parse(str); 
66             Seed.print(ar.xtype);
67             
68             // construct...
69             var cls = imports.Project[ar.xtype][ar.xtype];
70             this.projects.push( new cls(ar));
71             
72             
73             
74             
75             
76              
77         }
78 //        print(JSON.stringify(this.projects.length));Seed.quit();
79    
80         this.projects.sort(function(a,b) {
81             if (a.getName() == b.getName()) {
82                 return 0;
83             }
84             return a.getName() > b.getName() ? 1 : -1;
85             
86             
87         });
88    
89         
90        
91         
92         
93         
94         
95         
96         
97     },
98     
99     
100     saveConfig : function()
101     {
102         var _this = this;
103         this.projects.forEach( function(p) {
104             
105             if (!p.fn) {
106                 // make the filename..
107                 var t = new GLib.TimeVal();
108                 GLib.get_current_time(t);
109                 var str = '' + t.tv_sec + ':' + t.tv_usec;
110                 Seed.print(str);
111                 p.fn = GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, str, str.length);
112                 Seed.print(p.fn);
113
114             }
115             var  s =  p.toJSON();
116             File.write(_this.dirname + '/' + p.fn + '.json', s);
117            
118            
119         });
120         
121         
122         
123         
124     },
125     update : function(proj) {
126         
127         //Seed.print(JSON.stringify(proj));
128         var found = false;
129         this.projects.forEach( function(p) {
130             if (proj == p) {
131                 found = true;
132                 return true;
133             }
134         });
135         if (found) {
136             this.fireEvent('changed', this);
137             return proj;
138             return;
139         }
140         var cls = imports.Project[proj.xtype][proj.xtype];
141         var pr = new cls(proj);
142         this.projects.push(pr );
143         this.fireEvent('changed', this);
144         return pr;
145         
146         
147     },
148     
149     deleteProject : function (fn)
150     {
151         var newplist = [];
152         var _this = this;
153         this.projects.forEach(function(p) {
154             if (p.fn != fn) {
155                 newplist.push(p);
156                 return;
157             }
158             var file = _this.dirname + '/' + p.fn + '.json';
159             if (File.exists(file)) {
160                 File.remove(file);
161             }
162             
163         });
164         this.projects = newplist;
165         this.fireEvent('changed', this);
166     },
167     
168     
169     getByFn : function (fn) {
170         var  ret = false;
171         this.projects.forEach(function(p) {
172             if (p.fn == fn) {
173                 ret = p;
174                 return true;
175             }
176         });
177         return ret;
178         
179     },
180     getPalete: function(type) {
181         if (typeof(ProjectManager.palete[type]) != 'undefined') {
182             //print ("ALREADY GOT " + type);
183             return ProjectManager.palete[type];
184         }
185         var cls = imports.Palete[type][type];
186         
187         ProjectManager.palete[type] =  new cls();
188         print (typeof(ProjectManager.palete[type]));
189         return ProjectManager.palete[type];
190     }
191
192     
193     
194 });
195
196
197
198