]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/config.vala
Added config options for call and sms ringtones
[gregoa/zavai.git] / src / config.vala
index d73171653f3746fbd0c2d927ec08455d1375b080..92f8f93cb919da40afe45b291979a1ee8b15134f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * config - zavai configuration
  *
- * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
+ * Copyright (C) 2009--2010  Enrico Zini <enrico@enricozini.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,88 +22,280 @@ namespace zavai {
 
 public class Config
 {
-    public string version { get; set; }
-    public string homedir { get; set; }
-    public string icondir { get; set; }
-    public int min_button_height { get; set; }
-    public string gprs_apn { get; set; }
-    public string gprs_user { get; set; }
-    public string gprs_pass { get; set; }
+    protected Lua.LuaVM lua;
+    protected weak string get_string(string name)
+    {
+        lua.get_global(name);
+        weak string res = lua.to_string(-1);
+        lua.pop(1);
+        return res;
+    }
+    protected string set_string(string name, string? val)
+    {
+        if (val == null)
+            lua.push_nil();
+        else
+            lua.push_string(val);
+        lua.set_global(name);
+        return val;
+    }
+    protected weak int get_int(string name)
+    {
+        lua.get_global(name);
+        int res = lua.to_integer(-1);
+        lua.pop(1);
+        return res;
+    }
+    protected int set_int(string name, int val)
+    {
+        lua.push_integer(val);
+        lua.set_global(name);
+        return val;
+    }
+
+    private string _version;
+    public string version
+    {
+        get { return _version; }
+        set { _version = set_string("version", value); }
+    }
+
+    // "phone" or "laptop"
+    private string _profile;
+    public string profile
+    {
+        get { return _profile; }
+        set { _profile = set_string("profile", value); }
+    }
+
+    private string _homedir;
+    public string homedir
+    {
+        get { return _homedir; }
+        set { _homedir = set_string("homedir", value); }
+    }
+
+    private string _icondir;
+    public string icondir
+    {
+        get { return _icondir; }
+        set { _icondir = set_string("icondir", value); }
+    }
+
+    private int _min_button_height;
+    public int min_button_height
+    {
+        get { return _min_button_height; }
+        set { _min_button_height = set_int("min_button_height", value); }
+    }
+
+    private string _gpsd_host;
+    public string gpsd_host
+    {
+        get { return _gpsd_host; }
+        set { _gpsd_host = set_string("gpsd_host", value); }
+    }
+
+    private string _gpsd_port;
+    public string gpsd_port
+    {
+        get { return _gpsd_port; }
+        set { _gpsd_port = set_string("gpsd_port", value); }
+    }
+
+    private string _gprs_apn;
+    public string gprs_apn
+    {
+        get { return _gprs_apn; }
+        set { _gprs_apn = set_string("gprs_apn", value); }
+    }
+
+    private string _gprs_user;
+    public string gprs_user
+    {
+        get { return _gprs_user; }
+        set { _gprs_user = set_string("gprs_user", value); }
+    }
+
+    private string _gprs_pass;
+    public string gprs_pass
+    {
+        get { return _gprs_pass; }
+        set { _gprs_pass = set_string("gprs_pass", value); }
+    }
+
+    private string _sim_pin;
+    public string sim_pin
+    {
+        get { return _sim_pin; }
+        set { _sim_pin = set_string("sim_pin", value); }
+    }
+
+    private int _power_button_keycode;
+    public int power_button_keycode
+    {
+        get { return _power_button_keycode; }
+        set { _power_button_keycode = set_int("power_button_keycode", value); }
+    }
+
+    private int _aux_button_keycode;
+    public int aux_button_keycode
+    {
+        get { return _aux_button_keycode; }
+        set { _aux_button_keycode = set_int("aux_button_keycode", value); }
+    }
+
+    private string _ringtone_alarm;
+    public string ringtone_alarm
+    {
+        get { return _ringtone_alarm; }
+        set { _ringtone_alarm = set_string("ringtone_alarm", value); }
+    }
+
+    private string _ringtone_call;
+    public string ringtone_call
+    {
+        get { return _ringtone_call; }
+        set { _ringtone_call = set_string("ringtone_call", value); }
+    }
+
+    private string _ringtone_sms;
+    public string ringtone_sms
+    {
+        get { return _ringtone_sms; }
+        set { _ringtone_sms = set_string("ringtone_sms", value); }
+    }
+
+    public int backlight_max
+    {
+        get;
+        set;
+    }
+
+    private string _argv0;
+    public string argv0 {
+        get { return _argv0; }
+        set {
+            if (value.chr(-1, '/') != null)
+            {
+                if (Path.is_absolute(value))
+                {
+                    _argv0 = value;
+                } else {
+                    _argv0 = Path.build_filename(Environment.get_current_dir(), value, null);
+                }
+            } else {
+                _argv0 = Environment.find_program_in_path(value);
+            }
+            zavai.log.debug("ARGV0: " + _argv0);
+        }
+    }
+
+    /// Reread config values from the Lua VM, to be run after running Lua code
+    protected void refresh_from_lua()
+    {
+        _version = get_string("version");
+        _profile = get_string("profile");
+        _homedir = get_string("homedir");
+        _icondir = get_string("icondir");
+        _min_button_height = get_int("min_button_height");
+        _gpsd_host = get_string("gpsd_host");
+        _gpsd_port = get_string("gpsd_port");
+        _gprs_apn = get_string("gprs_apn");
+        _gprs_user = get_string("gprs_user");
+        _gprs_pass = get_string("gprs_pass");
+        _sim_pin = get_string("sim_pin");
+        _power_button_keycode = get_int("power_button_keycode");
+        _aux_button_keycode = get_int("aux_button_keycode");
+        _ringtone_alarm = get_string("ringtone_alarm");
+        _ringtone_call = get_string("ringtone_call");
+        _ringtone_sms = get_string("ringtone_sms");
+    }
 
     public Config()
     {
+        lua = new Lua.LuaVM();
+        lua.open_libs();
+
         // Set defaults
         version = "0.1";
+        profile = "phone";
         homedir = GLib.Environment.get_home_dir() + "/.zavai";
-        icondir = "./icons";
+        icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR");
+        if (icondir == null)
+                icondir = "/usr/share/zavai/icons";
         min_button_height = 80;
-       gprs_apn = "";
-       gprs_user = "x";
-       gprs_pass = "x";
+        gpsd_host = "localhost";
+        gpsd_port = "gpsd";
+        gprs_apn = "general.t-mobile.uk";
+        gprs_user = "x";
+        gprs_pass = "x";
+        sim_pin = "1234";
+        backlight_max = 15;
+        power_button_keycode = 124;
+        aux_button_keycode = 177;
+        ringtone_alarm = "file:///usr/share/sounds/yue-fso/lec1.ogg";
+        ringtone_call = "file:///usr/share/sounds/yue-fso/jmf1.ogg";
+        ringtone_sms = "file:///usr/share/sounds/yue-fso/nothing4.ogg";
+
+        // Read config
+        if (lua.do_file(homedir + "/config"))
+        {
+            zavai.log.error("Failed to parse " + homedir + "/config: " + lua.to_string(-1));
+        }
+        refresh_from_lua();
     }
 
-/*
-    def _get_homedir(self):
-        res = self.get("global", "home")
-        if res is None:
-            res = os.path.expanduser("~/.zavai")
-        if not os.path.isdir(res):
-            zavai.info("Creating directory", res)
-            os.makedirs(res)
-        return res
-*/
+    /**
+     * Find a zavai script.
+     * 
+     * ~/.zavai/NAME is searched first, then /usr/share/zavai/hooks/
+     *
+     * If the script is not found, NULL is returned
+     */
+    public string? find_script(string name)
+    {
+        string candidate = homedir + "/" + name;
+        if (FileUtils.test(candidate, FileTest.EXISTS))
+            return candidate;
+        candidate = "/usr/share/zavai/hooks/" + name;
+        if (FileUtils.test(candidate, FileTest.EXISTS))
+            return candidate;
+        return null;
+    }
+
+    public void find_and_run_script(string script, string args) throws SpawnError
+    {
+        string cmd = find_script(script);
+        if (cmd == null)
+            throw new SpawnError.NOENT("hook '" + cmd + "' not found");
+        run_script(cmd + " " + args);
+    }
+
+    public void run_script(string command) throws SpawnError
+    {
+        zavai.log.info("Run program: " + command);
+        string[] args = command.split(" ");
+        Pid pid;
+        Process.spawn_async(
+            Environment.get_home_dir(),
+            args,
+            null,
+            SpawnFlags.SEARCH_PATH,
+            null,
+            out pid);
+    }
+
+    public int run_script_sync(string command, out string std_out, out string std_err) throws SpawnError
+    {
+        int status = -1;
+        zavai.log.info("Run program: " + command);
+        string[] args = command.split(" ");
+        bool res = Process.spawn_sync(Environment.get_home_dir(), args, null, SpawnFlags.SEARCH_PATH, null, out std_out, out std_err, out status);
+        return status;
+    }
 }
 
-/*
-import os
-import ConfigParser, StringIO
-
-def read_config(rootDir = None, defaults = None, nick="octofuss"):
-    """
-    Read octofuss configuration, returning a ConfigParser object
-    """
-    if rootDir == None:
-        rootDir = os.environ.get(nick.upper() + "_CONFDIR", "/etc/" + nick)
-    files = []
-    def trytouse(path):
-        if os.path.exists(path):
-            files.append(path)
-
-    # Start with the main config file
-    trytouse(os.path.join(rootDir, nick + ".conf"))
-
-    # Add snippets found in rc.d style directory
-    subdir = os.path.join(rootDir, nick + ".conf.d")
-    if os.path.isdir(subdir):
-        for file in sorted(os.listdir(subdir)):
-            if file.startswith('#'): continue
-            if file.startswith('.'): continue
-            if file.endswith('~'): continue
-            if file.endswith('.bak'): continue
-            trytouse(os.path.join(subdir, file))
-
-    config = ConfigParser.ConfigParser()
-    if defaults != None:
-        infile = StringIO.StringIO(defaults)
-        config.readfp(infile, "defaults")
-    config.read(files)
-    return config
-import os.path
-import zavai
-
-class Config:
-    def __init__(self):
-        self.conf = zavai.read_config(nick="zavai")
-
-    def get(self, section, name, default=None):
-        if self.conf.has_section(section):
-            if self.conf.has_option(section, name):
-                return self.conf.get(section, name)
-        return None
-
-    homedir = property(_get_homedir)
-*/
-
-Config config = null;
+public Config config = null;
 
 }