*/
using GLib;
-using Gee;
namespace zavai {
-public interface Resource : Object {
- /**
- * Shut down this resource.
- *
- * Normally one does nothing here, but it is important to give resources a
- * chance to do cleanup when the program quits.
- *
- * This can be used for tasks like closing the tags on a GPX track,
- * releasing a FSO resource, restoring mixer settings and so on.
- */
- public abstract void shutdown();
-}
-
-public abstract class Service : Object, Resource {
- public string name { get; construct; }
-
- bool _started;
- public bool started {
- get { return _started; }
- set { _started = value; }
- default = false;
- }
-
- public signal void toggled(bool new_state);
-
- protected HashMap<string, int> requests;
-
- construct {
- requests = new HashMap<string, int>(str_hash, str_equal);
- }
-
- public void shutdown()
- {
- stop();
- }
-
- /// Activate the service
- protected virtual void start()
- {
- if (!started)
- {
-stderr.printf("SERVICE %s started\n", name);
- started = true;
- toggled(started);
- }
- }
-
- /// Deactivate the service
- protected virtual void stop()
- {
- if (started)
- {
-stderr.printf("SERVICE %s stopped\n", name);
- started = false;
- toggled(started);
- }
- }
-
- /**
- Request a resource using the given ID.
- *
- * If it is the first time the resource is requested, start it and
- * return true. Else, take note of the request and return false.
- *
- * If a resource is requested multiple times with the same ID, it will
- * need to be released multiple times with that ID.
- */
- public bool request(string id)
- {
- bool res = (requests.size == 0);
- if (id in requests)
- requests[id] = requests[id] + 1;
- else
- requests.set(id, 1);
- if (res) start();
- return res;
- }
-
- /**
- * Release a resource using the given ID.
- *
- * If after the call nothing is requesting the resource, stop it and
- * return true. Else, take note of the release and return false.
- *
- * If a resource is requested multiple times with the same ID, it will
- * need to be released multiple times with that ID.
- */
- public bool release(string id)
- {
- if (id in requests)
- {
- if (requests[id] > 1)
- requests[id] = requests[id] - 1;
- else
- requests.remove(id);
- } else {
- return false;
- }
- if (requests.size > 0)
- return false;
- stop();
- return true;
- }
-}
-
public class Registry : Object, Resource
{
- HashMap<string, Resource> memb_resources;
- HashMap<string, Service> memb_services;
- HashMap<string, Applet> memb_applets;
- HashMap<string, Menu> memb_menus;
- protected ArrayList<Resource> registration_order;
- public DBus.Connection sbus;
+ List<Resource> resources;
+ public DBus.Connection sbus;
public string bus_name;
- public Registry()
- {
- memb_resources = new HashMap<string, Resource>(str_hash, str_equal);
- memb_services = new HashMap<string, Service>(str_hash, str_equal);
- memb_applets = new HashMap<string, Applet>(str_hash, str_equal);
- memb_menus = new HashMap<string, Menu>(str_hash, str_equal);
- registration_order = new ArrayList<Resource>();
- sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
+ public Registry()
+ {
+ resources = new List<Resource>();
+ try {
+ sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
+ } catch (DBus.Error e) {
+ stderr.printf("Cannot access system DBus bus: %s\n", e.message);
+ sbus = null;
+ }
+
+ bus_name = DBus.bus_get_unique_name(sbus.get_connection());
+ zavai.log.info("My bus name: " + bus_name);
-/*
-stderr.printf("ZA1\n");
dynamic DBus.Object tmp_dbus = sbus.get_object(
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus");
-stderr.printf("ZA2\n");
- bus_name = "org.freedesktop.zavai";
+ bus_name = "org.enricozini.zavai";
uint res = tmp_dbus.RequestName(bus_name, (uint)DBus.NameFlag.DO_NOT_QUEUE);
- stderr.printf("RN: %u\n", res);
switch (res)
{
case DBus.RequestNameReply.PRIMARY_OWNER:
zavai.log.info("I already own the name " + bus_name + " but I do not remember asking for it");
break;
}
-stderr.printf("ZA3\n");
- */
- }
-
- public void shutdown()
- {
- // Shutdown in reverse registration order
- for (int i = registration_order.size - 1; i >= 0; --i)
- registration_order[i].shutdown();
- }
-
- public void register_resource(string name, Resource obj)
- {
- memb_resources[name] = obj;
- registration_order.add(obj);
- }
-
- public void register_service(Service obj)
- {
- memb_services[obj.name] = obj;
- registration_order.add(obj);
- }
-
- public void register_applet(string name, Applet obj)
- {
- memb_applets[name] = obj;
- registration_order.add(obj);
- }
-
- public void register_menu(string name, Menu obj)
- {
- memb_applets[name] = obj;
- memb_menus[name] = obj;
- registration_order.add(obj);
- }
-
- public Resource? getr(string name)
- {
- if (name in memb_resources)
- return memb_resources[name];
- else
- {
- log.error("getr: no resource found: " + name);
- return null;
- }
- }
-
- public Service? gets(string name)
- {
- if (name in memb_services)
- return memb_services[name];
- else
- {
- log.error("gets: no service found: " + name);
- return null;
- }
- }
-
- public Applet? geta(string name)
- {
- if (name in memb_applets)
- return memb_applets[name];
- else
- {
- log.error("geta: no applet found: " + name);
- return null;
- }
- }
-
- public Menu? getmenu(string name)
- {
- if (name in memb_menus)
- return memb_menus[name];
- else
- {
- log.error("getmenu: no menu found: " + name);
- return null;
- }
- }
+ }
+
+ public void shutdown()
+ {
+ // Shutdown in reverse registration order
+ for (weak List<Resource> i = resources; i != null; i = i.next)
+ i.data.shutdown();
+ }
+
+ public void register(Resource obj)
+ {
+ resources.prepend(obj);
+ }
}
-zavai.Registry registry;
+public zavai.Registry registry;
}