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              print(fn);
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         print(JSON.stringify(this.projects.length));Seed.quit();
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             var  s =  p.toJSON();
115             File.write(_this.dirname + '/' + p.fn + '.json', s);
116            
117            
118         });
119         
120         
121         
122         
123     },
124     update : function(proj) {
125         
126         Seed.print(JSON.stringify(proj));
127         var found = false;
128         this.projects.forEach( function(p) {
129             if (proj == p) {
130                 found = true;
131                 return true;
132             }
133         });
134         if (found) {
135             this.fireEvent('changed', this);
136             return proj;
137             return;
138         }
139         var cls = imports.Project[proj.xtype][proj.xtype];
140         var pr = new cls(proj);
141         this.projects.push(pr );
142         this.fireEvent('changed', this);
143         return pr;
144         
145         
146     },
147     
148     deleteProject : function (fn)
149     {
150         var newplist = [];
151         var _this = this;
152         this.projects.forEach(function(p) {
153             if (p.fn != fn) {
154                 newplist.push(p);
155                 return;
156             }
157             var file = _this.dirname + '/' + p.fn + '.json';
158             if (File.exists(file)) {
159                 File.remove(file);
160             }
161             
162         });
163         this.projects = newplist;
164         this.fireEvent('changed', this);
165     },
166     
167     
168     getByFn : function (fn) {
169         var  ret = false;
170         this.projects.forEach(function(p) {
171             if (p.fn == fn) {
172                 ret = p;
173                 return true;
174             }
175         });
176         return ret;
177         
178     },
179     getPalete: function(type) {
180         if (typeof(ProjectManager.palete[type]) != 'undefined') {
181             //print ("ALREADY GOT " + type);
182             return ProjectManager.palete[type];
183         }
184         var cls = imports.Palete[type][type];
185         
186         ProjectManager.palete[type] =  new cls();
187         print (typeof(ProjectManager.palete[type]));
188         return ProjectManager.palete[type];
189     }
190
191     
192     
193 });
194
195
196
197