Builder/Provider/ProjectManager.js
[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
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     providers : { }, 
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         
48         
49         this.projects = [];
50         var gdir = GLib.dir_open(this.dirname,0);
51         while (true) {
52         
53             var fn = GLib.dir_read_name(gdir);
54             if (!fn) {
55                 GLib.dir_close(gdir);
56                 return;
57             }
58             if (!fn.match(/.json$/)) {
59                 continue;
60             }
61             var file = Gio.file_new_for_path(this.dirname + '/' + fn);
62             Seed.print(this.dirname + '/' + fn);
63             var stream = file.read();
64             var dstream = new Gio.DataInputStream.c_new(stream);
65             Seed.print(dstream);
66             
67             str = dstream.read_until("");
68             if (!str || !str.length) {
69                 continue; // empty file.
70             }
71              
72             
73             var ar = JSON.parse(str); 
74             Seed.print(ar.xtype);
75             
76             // construct...
77             var cls = imports.Builder.Provider.Project[ar.xtype][ar.xtype];
78             this.projects.push( new cls(ar));
79             
80             
81             
82             
83             
84              
85         }
86    
87         
88         
89         
90         
91         
92         
93         
94     },
95     
96     
97     saveConfig : function()
98     {
99         var _this = this;
100         this.projects.forEach( function(p) {
101             
102             if (!p.fn) {
103                 // make the filename..
104                 var t = new GLib.TimeVal();
105                 GLib.get_current_time(t);
106                 var str = '' + t.tv_sec + ':' + t.tv_usec;
107                 Seed.print(str);
108                 p.fn = GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, str, str.length);
109                 Seed.print(p.fn);
110
111             }
112             
113             
114             var file = Gio.file_new_for_path(_this.dirname + '/' + p.fn + '.json');
115             
116             var stream = file.replace(null,false,0);
117             //console.dump(p);
118             var  s =  p.toJSON();
119             stream.write(s, s.length);
120             stream.close();
121            
122            
123         });
124         
125         
126         
127         
128     },
129     update : function(proj) {
130         
131         Seed.print(JSON.stringify(proj));
132         var found = false;
133         this.projects.forEach( function(p) {
134             if (proj == p) {
135                 found = true;
136                 return true;
137             }
138         });
139         if (found) {
140             this.fireEvent('changed', this);
141             return proj;
142             return;
143         }
144         var cls = imports.Builder.Provider.Project[proj.xtype][proj.xtype];
145         var pr = new cls(proj);
146         this.projects.push(pr );
147         this.fireEvent('changed', this);
148         return pr;
149         
150         
151     },
152     getByFn : function (fn) {
153         var  ret = false;
154         this.projects.forEach(function(p) {
155             if (p.fn == fn) {
156                 ret = p;
157                 return true;
158             }
159         });
160         return ret;
161         
162     },
163     getPalete: function(type) {
164         if (typeof(ProjectManager[type]) != 'undefined') {
165             return ProjectManager[type];
166         }
167         ProjectManager[type] =  new imports.Builder.Provider.Palete[type][type]();
168         return ProjectManager[type];
169     }
170
171     
172     
173 });
174
175
176
177