sync
[app.Builder.js] / src / Application.vala
1
2
3 namespace Builder4
4 {
5
6         public class AppSettings : Object
7         {
8
9                 // what are we going to have as settings?
10                 public string roo_html_dir { get; set; }
11
12                 public AppSettings()
13                 {
14                         this.notify.connect(() => {
15                                 this.save();
16                         });
17                 }
18
19                 public static AppSettings factory()
20                 {
21                          
22                         var setting_file = Application.configDirectory() + "/builder.settings";
23                         
24                         if (!FileUtils.test(setting_file, FileTest.EXISTS)) {
25                                  return new AppSettings();
26                         }
27                         string data; 
28                         FileUtils.get_contents(setting_file, out data);
29                         return Json.gobject_from_data (typeof (AppSettings), data) as AppSettings;
30                 }
31                 public void save()
32                 {
33                         var dirname = GLib.Environment.get_home_dir() + "/.Builder";
34                         var setting_file = dirname + "/builder.settings";
35                         string data = Json.gobject_to_data (this, null);
36                         print("saving application settings\n");
37                         FileUtils.set_contents(setting_file,   data);
38                 }
39
40                 
41         }
42         
43         
44         public static Application application = null;
45         
46         public class Application : Gtk.Application
47         {
48                 enum Target {
49                     INT32,
50                     STRING,
51                     ROOTWIN
52                 }
53
54
55                 public const Gtk.TargetEntry[] targetList = {
56                     { "INTEGER",    0, Target.INT32 },
57                     { "STRING",     0, Target.STRING },
58                     { "application/json",     0, Target.STRING },                       
59                     { "text/plain", 0, Target.STRING },
60                     { "application/x-rootwindow-drop", 0, Target.ROOTWIN }
61                 };
62                 public AppSettings settings = null;
63
64         
65                 public Application ()
66                 {
67                         Object(
68                                application_id: "org.roojs.app-builder",
69                                 flags: ApplicationFlags.FLAGS_NONE
70                         );
71                                          
72                         configDirectory();
73                         this.settings = AppSettings.factory();  
74
75                         this.initResources(true); 
76                 
77
78                 }
79
80
81                 
82                 public static Application  singleton()
83                 {
84                         if (application==null) {
85                                 application = new Application();
86  
87                         
88                         }
89                         return application;
90                 }
91
92                 
93                 public static string configDirectory()
94                 {
95                         var dirname = GLib.Environment.get_home_dir() + "/.Builder";
96                 
97                         if (!FileUtils.test(dirname,FileTest.IS_DIR)) {
98                                 var dir = File.new_for_path(dirname);
99                                 dir.make_directory();    
100                         }
101                         if (!FileUtils.test(dirname + "/resources",FileTest.IS_DIR)) {
102                                 var dir = File.new_for_path(dirname + "/resources");
103                                 dir.make_directory();    
104                         }
105
106                 
107                         return dirname;
108                 }
109          
110                 public void initResources(bool force = false)
111                 {
112                         // files to fetch from resources.
113                         string[] res = { 
114                                 "bootstrap.builder.html",
115                                 "roo.builder.html",
116                                 "roo.builder.js",
117                                 "Gir.overides",
118                                 "RooUsage.txt",
119                                 "GtkUsage.txt"
120                         };
121                         for (var i = 0; i < res.length; i++ ) { 
122                                 this.fetchResource(res[i], force);
123                         }
124                         
125                         this.fetchResourceFrom (
126                                 "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json",
127                                 "roodata.json",
128                                 force
129                         );
130                         
131
132                 }
133                 public void fetchResource(string res, bool force) {
134                         if (!force && FileUtils.test(configDirectory() + "/resources/" + res, FileTest.EXISTS)) {
135                                 return;
136                         }
137                         this.fetchResourceFrom(
138                                "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/" + res,
139                                res,
140                                force
141                        );
142                         
143
144                 }
145
146                 public void fetchResourceFrom(string src, string res, bool force) {
147                         if (!force && FileUtils.test(configDirectory() + "/resources/" + res, FileTest.EXISTS)) {
148                                 return;
149                         }
150                         // fetch...
151                         print("downloading %s \nto : %s\n", src,res);
152                         var session = new Soup.Session ();
153                         session.user_agent = "App Builder ";
154                         var message = new Soup.Message ("GET", 
155                                 src
156                         );
157
158                             // send the HTTP request and wait for response
159                          session.send_message (message);
160
161                             // output the XML result to stdout
162                         FileUtils.set_contents(
163                                configDirectory() + "/resources/" + res,
164                               (string) message.response_body.data
165                         );
166
167
168                 }
169                 
170         } 
171
172
173         
174         
175
176
177                 
178         
179 }