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