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