Implemented the concept of 'profile'
[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 void 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     }
41     protected weak int get_int(string name)
42     {
43         lua.get_global(name);
44         int res = lua.to_integer(-1);
45         lua.pop(1);
46         return res;
47     }
48     protected void set_int(string name, int val)
49     {
50         lua.push_integer(val);
51         lua.set_global(name);
52     }
53
54     public string version
55     {
56         get { return get_string("version"); }
57         set { set_string("version", value); }
58     }
59     // "phone" or "laptop"
60     public string profile
61     {
62         get { return get_string("profile"); }
63         set { set_string("profile", value); }
64     }
65     public string homedir
66     {
67         get { return get_string("homedir"); }
68         set { set_string("homedir", value); }
69     }
70     public string icondir
71     {
72         get { return get_string("icondir"); }
73         set { set_string("icondir", value); }
74     }
75     public int min_button_height
76     {
77         get { return get_int("min_button_height"); }
78         set { set_int("min_button_height", value); }
79     }
80     public string gprs_apn
81     {
82         get { return get_string("gprs_apn"); }
83         set { set_string("gprs_apn", value); }
84     }
85     public string gprs_user
86     {
87         get { return get_string("gprs_user"); }
88         set { set_string("gprs_user", value); }
89     }
90     public string gprs_pass
91     {
92         get { return get_string("gprs_pass"); }
93         set { set_string("gprs_pass", value); }
94     }
95     public int backlight_max
96     {
97         get;
98         set;
99     }
100     private string _argv0;
101     public string argv0 {
102         get { return _argv0; }
103         set { _argv0 = value; }
104     }
105
106     public Config()
107     {
108         lua = new Lua.LuaVM();
109         lua.open_libs();
110
111         // Set defaults
112         version = "0.1";
113         profile = "phone";
114         homedir = GLib.Environment.get_home_dir() + "/.zavai";
115         icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR");
116         if (icondir == null)
117                 icondir = "/usr/share/zavai/icons";
118         min_button_height = 80;
119         gprs_apn = "general.t-mobile.uk";
120         gprs_user = "x";
121         gprs_pass = "x";
122         backlight_max = 15;
123
124         // Read config
125         if (lua.do_file(homedir + "/config"))
126         {
127             zavai.log.error("Failed to parse " + homedir + "/config: " + lua.to_string(-1));
128         }
129     }
130
131 /*
132     def _get_homedir(self):
133         res = self.get("global", "home")
134         if res is None:
135             res = os.path.expanduser("~/.zavai")
136         if not os.path.isdir(res):
137             zavai.info("Creating directory", res)
138             os.makedirs(res)
139         return res
140 */
141 }
142
143 /*
144 import os
145 import ConfigParser, StringIO
146
147 def read_config(rootDir = None, defaults = None, nick="octofuss"):
148     """
149     Read octofuss configuration, returning a ConfigParser object
150     """
151     if rootDir == None:
152         rootDir = os.environ.get(nick.upper() + "_CONFDIR", "/etc/" + nick)
153     files = []
154     def trytouse(path):
155         if os.path.exists(path):
156             files.append(path)
157
158     # Start with the main config file
159     trytouse(os.path.join(rootDir, nick + ".conf"))
160
161     # Add snippets found in rc.d style directory
162     subdir = os.path.join(rootDir, nick + ".conf.d")
163     if os.path.isdir(subdir):
164         for file in sorted(os.listdir(subdir)):
165             if file.startswith('#'): continue
166             if file.startswith('.'): continue
167             if file.endswith('~'): continue
168             if file.endswith('.bak'): continue
169             trytouse(os.path.join(subdir, file))
170
171     config = ConfigParser.ConfigParser()
172     if defaults != None:
173         infile = StringIO.StringIO(defaults)
174         config.readfp(infile, "defaults")
175     config.read(files)
176     return config
177 import os.path
178 import zavai
179
180 class Config:
181     def __init__(self):
182         self.conf = zavai.read_config(nick="zavai")
183
184     def get(self, section, name, default=None):
185         if self.conf.has_section(section):
186             if self.conf.has_option(section, name):
187                 return self.conf.get(section, name)
188         return None
189
190     homedir = property(_get_homedir)
191 */
192
193 public Config config = null;
194
195 }