]> ToastFreeware Gitweb - gregoa/zavai.git/blob - src/config.vala
start wifi statusicon
[gregoa/zavai.git] / src / config.vala
1 /*
2  * config - zavai configuration
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 namespace zavai {
22
23 public class Config
24 {
25     protected Lua.LuaVM lua;
26     protected weak string get_string(string name)
27     {
28         lua.get_global(name);
29         weak string res = lua.to_string(-1);
30         lua.pop(1);
31         return res;
32     }
33     protected string set_string(string name, string? val)
34     {
35         if (val == null)
36             lua.push_nil();
37         else
38             lua.push_string(val);
39         lua.set_global(name);
40         return val;
41     }
42     protected weak int get_int(string name)
43     {
44         lua.get_global(name);
45         int res = lua.to_integer(-1);
46         lua.pop(1);
47         return res;
48     }
49     protected int set_int(string name, int val)
50     {
51         lua.push_integer(val);
52         lua.set_global(name);
53         return val;
54     }
55
56     private string _version;
57     public string version
58     {
59         get { return _version; }
60         set { _version = set_string("version", value); }
61     }
62
63     // "phone" or "laptop"
64     private string _profile;
65     public string profile
66     {
67         get { return _profile; }
68         set { _profile = set_string("profile", value); }
69     }
70
71     private string _homedir;
72     public string homedir
73     {
74         get { return _homedir; }
75         set { _homedir = set_string("homedir", value); }
76     }
77
78     private string _icondir;
79     public string icondir
80     {
81         get { return _icondir; }
82         set { _icondir = set_string("icondir", value); }
83     }
84
85     private int _min_button_height;
86     public int min_button_height
87     {
88         get { return _min_button_height; }
89         set { _min_button_height = set_int("min_button_height", value); }
90     }
91
92     private string _gpsd_host;
93     public string gpsd_host
94     {
95         get { return _gpsd_host; }
96         set { _gpsd_host = set_string("gpsd_host", value); }
97     }
98
99     private string _gpsd_port;
100     public string gpsd_port
101     {
102         get { return _gpsd_port; }
103         set { _gpsd_port = set_string("gpsd_port", value); }
104     }
105
106     private string _gprs_apn;
107     public string gprs_apn
108     {
109         get { return _gprs_apn; }
110         set { _gprs_apn = set_string("gprs_apn", value); }
111     }
112
113     private string _gprs_user;
114     public string gprs_user
115     {
116         get { return _gprs_user; }
117         set { _gprs_user = set_string("gprs_user", value); }
118     }
119
120     private string _gprs_pass;
121     public string gprs_pass
122     {
123         get { return _gprs_pass; }
124         set { _gprs_pass = set_string("gprs_pass", value); }
125     }
126
127     private string _sim_pin;
128     public string sim_pin
129     {
130         get { return _sim_pin; }
131         set { _sim_pin = set_string("sim_pin", value); }
132     }
133
134     private int _power_button_keycode;
135     public int power_button_keycode
136     {
137         get { return _power_button_keycode; }
138         set { _power_button_keycode = set_int("power_button_keycode", value); }
139     }
140
141     private int _aux_button_keycode;
142     public int aux_button_keycode
143     {
144         get { return _aux_button_keycode; }
145         set { _aux_button_keycode = set_int("aux_button_keycode", value); }
146     }
147
148     public int backlight_max
149     {
150         get;
151         set;
152     }
153
154     private string _argv0;
155     public string argv0 {
156         get { return _argv0; }
157         set {
158             if (value.chr(-1, '/') != null)
159             {
160                 if (Path.is_absolute(value))
161                 {
162                     _argv0 = value;
163                 } else {
164                     _argv0 = Path.build_filename(Environment.get_current_dir(), value, null);
165                 }
166             } else {
167                 _argv0 = Environment.find_program_in_path(value);
168             }
169             zavai.log.debug("ARGV0: " + _argv0);
170         }
171     }
172
173     /// Reread config values from the Lua VM, to be run after running Lua code
174     protected void refresh_from_lua()
175     {
176         _version = get_string("version");
177         _profile = get_string("profile");
178         _homedir = get_string("homedir");
179         _icondir = get_string("icondir");
180         _min_button_height = get_int("min_button_height");
181         _gpsd_host = get_string("gpsd_host");
182         _gpsd_port = get_string("gpsd_port");
183         _gprs_apn = get_string("gprs_apn");
184         _gprs_user = get_string("gprs_user");
185         _gprs_pass = get_string("gprs_pass");
186         _sim_pin = get_string("sim_pin");
187         _power_button_keycode = get_int("power_button_keycode");
188         _aux_button_keycode = get_int("aux_button_keycode");
189     }
190
191     public Config()
192     {
193         lua = new Lua.LuaVM();
194         lua.open_libs();
195
196         // Set defaults
197         version = "0.1";
198         profile = "phone";
199         homedir = GLib.Environment.get_home_dir() + "/.zavai";
200         icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR");
201         if (icondir == null)
202                 icondir = "/usr/share/zavai/icons";
203         min_button_height = 80;
204         gpsd_host = "localhost";
205         gpsd_port = "gpsd";
206         gprs_apn = "general.t-mobile.uk";
207         gprs_user = "x";
208         gprs_pass = "x";
209         sim_pin = "1234";
210         backlight_max = 15;
211         power_button_keycode = 124;
212         aux_button_keycode = 177;
213
214         // Read config
215         if (lua.do_file(homedir + "/config"))
216         {
217             zavai.log.error("Failed to parse " + homedir + "/config: " + lua.to_string(-1));
218         }
219         refresh_from_lua();
220     }
221 }
222
223 public Config config = null;
224
225 }