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     
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           
78             this.projects.push( new Builder.Provider.Project[ar.xtype](ar));
79             
80             
81             
82             
83             
84              
85         }
86    
87         
88         
89         
90         
91         
92         
93         
94     },
95     
96     
97     saveConfig : function()
98     {
99         Roo.each(this.projects, function(p) {
100             
101             if (!p.fn) {
102                 // make the filename..
103                 var t = new GLib.TimeVal();
104                 GLib.get_current_time(t);
105                 var str = '' + t.tv_sec + ':' + t.tv_usec;
106                 Seed.print(str);
107                 p.fn = GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, str, str.length);
108                 Seed.print(p.fn);
109
110             }
111             
112             
113             var file = Gio.file_new_for_path(this.dirname + '/' + p.fn + '.json');
114             
115             var stream = file.replace(null,false,0);
116             var  s =  p.toJSON();
117             stream.write(s, s.length);
118             stream.close();
119            
120            
121         }, this);
122         
123         
124         
125         
126     },
127     update : function(proj) {
128         
129         Seed.print(JSON.stringify(proj));
130         var found = false;
131         Roo.each(this.projects , function(p) {
132             if (proj == p) {
133                 found = true;
134                 return true;
135             }
136         });
137         if (found) {
138             this.fireEvent('changed', this);
139             return proj;
140             return;
141         }
142         var pr = new Builder.Provider.Project[proj.xtype](proj);
143         this.projects.push(pr );
144         this.fireEvent('changed', this);
145         return pr;
146         
147         
148     },
149     getByFn : function (fn) {
150         var  ret = false;
151         Roo.each(this.projects, function(p) {
152             if (p.fn == fn) {
153                 ret = p;
154                 return true;
155             }
156         });
157         return ret;
158         
159     }
160
161     
162     
163 });
164
165
166
167