]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/config.vala
Merge branch 'master' into gregoa
[gregoa/zavai.git] / src / config.vala
index 387546e49d9210dc9263b023a5ec6516da8a3a9d..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
@@ -30,13 +30,14 @@ public class Config
         lua.pop(1);
         return res;
     }
-    protected void set_string(string name, string? val)
+    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)
     {
@@ -45,56 +46,170 @@ public class Config
         lua.pop(1);
         return res;
     }
-    protected void set_int(string name, int val)
+    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 get_string("version"); }
-        set { set_string("version", value); }
+        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 get_string("homedir"); }
-        set { set_string("homedir", value); }
+        get { return _homedir; }
+        set { _homedir = set_string("homedir", value); }
     }
+
+    private string _icondir;
     public string icondir
     {
-        get { return get_string("icondir"); }
-        set { set_string("icondir", value); }
+        get { return _icondir; }
+        set { _icondir = set_string("icondir", value); }
     }
+
+    private int _min_button_height;
     public int min_button_height
     {
-        get { return get_int("min_button_height"); }
-        set { set_int("min_button_height", value); }
+        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 get_string("gprs_apn"); }
-        set { set_string("gprs_apn", value); }
+        get { return _gprs_apn; }
+        set { _gprs_apn = set_string("gprs_apn", value); }
     }
+
+    private string _gprs_user;
     public string gprs_user
     {
-        get { return get_string("gprs_user"); }
-        set { set_string("gprs_user", value); }
+        get { return _gprs_user; }
+        set { _gprs_user = set_string("gprs_user", value); }
     }
+
+    private string _gprs_pass;
     public string gprs_pass
     {
-        get { return get_string("gprs_pass"); }
-        set { set_string("gprs_pass", value); }
+        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 { _argv0 = value; }
+        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()
@@ -102,98 +217,84 @@ public class Config
         lua = new Lua.LuaVM();
         lua.open_libs();
 
-stderr.printf("ZA1\n");
-
         // Set defaults
         version = "0.1";
-stderr.printf("ZA2\n");
+        profile = "phone";
         homedir = GLib.Environment.get_home_dir() + "/.zavai";
-stderr.printf("ZA3\n");
         icondir = GLib.Environment.get_variable("ZAVAI_ICONDIR");
-stderr.printf("ZA4\n");
         if (icondir == null)
                 icondir = "/usr/share/zavai/icons";
-stderr.printf("ZA5\n");
         min_button_height = 80;
-stderr.printf("ZA6\n");
+        gpsd_host = "localhost";
+        gpsd_port = "gpsd";
         gprs_apn = "general.t-mobile.uk";
-stderr.printf("ZA7\n");
         gprs_user = "x";
-stderr.printf("ZA8\n");
         gprs_pass = "x";
-stderr.printf("ZA9\n");
+        sim_pin = "1234";
         backlight_max = 15;
-stderr.printf("ZA10\n");
+        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));
         }
-stderr.printf("ZA11\n");
+        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;
+    }
 
-/*
-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)
-*/
+    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;
+    }
+}
 
 public Config config = null;