2 * registry - zavai resource registry
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
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.
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.
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
26 public interface Resource : Object {
28 * Shut down this resource.
30 * Normally one does nothing here, but it is important to give resources a
31 * chance to do cleanup when the program quits.
33 * This can be used for tasks like closing the tags on a GPX track,
34 * releasing a FSO resource, restoring mixer settings and so on.
36 public abstract void shutdown();
39 public abstract class Service : Object, Resource {
40 public string name { get; construct; }
44 get { return _started; }
45 set { _started = value; }
49 public signal void toggled(bool new_state);
51 protected HashMap<string, int> requests;
54 requests = new HashMap<string, int>(str_hash, str_equal);
57 public void shutdown()
62 /// Activate the service
63 protected virtual void start()
67 stderr.printf("SERVICE %s started\n", name);
73 /// Deactivate the service
74 protected virtual void stop()
78 stderr.printf("SERVICE %s stopped\n", name);
85 Request a resource using the given ID.
87 * If it is the first time the resource is requested, start it and
88 * return true. Else, take note of the request and return false.
90 * If a resource is requested multiple times with the same ID, it will
91 * need to be released multiple times with that ID.
93 public bool request(string id)
95 bool res = (requests.size == 0);
97 requests[id] = requests[id] + 1;
105 * Release a resource using the given ID.
107 * If after the call nothing is requesting the resource, stop it and
108 * return true. Else, take note of the release and return false.
110 * If a resource is requested multiple times with the same ID, it will
111 * need to be released multiple times with that ID.
113 public bool release(string id)
117 if (requests[id] > 1)
118 requests[id] = requests[id] - 1;
124 if (requests.size > 0)
131 public class Registry : Object, Resource
133 HashMap<string, Resource> memb_resources;
134 HashMap<string, Service> memb_services;
135 HashMap<string, Applet> memb_applets;
136 HashMap<string, Menu> memb_menus;
137 protected ArrayList<Resource> registration_order;
138 public DBus.Connection sbus;
139 public string bus_name;
143 memb_resources = new HashMap<string, Resource>(str_hash, str_equal);
144 memb_services = new HashMap<string, Service>(str_hash, str_equal);
145 memb_applets = new HashMap<string, Applet>(str_hash, str_equal);
146 memb_menus = new HashMap<string, Menu>(str_hash, str_equal);
147 registration_order = new ArrayList<Resource>();
148 sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
150 bus_name = DBus.bus_get_unique_name(sbus.get_connection());
151 zavai.log.info("My bus name: " + bus_name);
153 dynamic DBus.Object tmp_dbus = sbus.get_object(
154 "org.freedesktop.DBus",
155 "/org/freedesktop/DBus",
156 "org.freedesktop.DBus");
157 bus_name = "org.enricozini.zavai";
158 uint res = tmp_dbus.RequestName(bus_name, (uint)DBus.NameFlag.DO_NOT_QUEUE);
161 case DBus.RequestNameReply.PRIMARY_OWNER:
162 zavai.log.info("Registered to dbus as " + bus_name);
164 case DBus.RequestNameReply.IN_QUEUE:
165 zavai.log.info("In queue, but I asked not to");
167 case DBus.RequestNameReply.EXISTS:
168 zavai.log.info(bus_name + " already exists");
170 case DBus.RequestNameReply.ALREADY_OWNER:
171 zavai.log.info("I already own the name " + bus_name + " but I do not remember asking for it");
176 public void shutdown()
178 // Shutdown in reverse registration order
179 for (int i = registration_order.size - 1; i >= 0; --i)
180 registration_order[i].shutdown();
183 public void register_resource(string name, Resource obj)
185 memb_resources[name] = obj;
186 registration_order.add(obj);
189 public void register_service(Service obj)
191 memb_services[obj.name] = obj;
192 registration_order.add(obj);
195 public void register_applet(string name, Applet obj)
197 memb_applets[name] = obj;
198 registration_order.add(obj);
201 public void register_menu(string name, Menu obj)
203 memb_applets[name] = obj;
204 memb_menus[name] = obj;
205 registration_order.add(obj);
208 public Resource? getr(string name)
210 if (name in memb_resources)
211 return memb_resources[name];
214 log.error("getr: no resource found: " + name);
219 public Service? gets(string name)
221 if (name in memb_services)
222 return memb_services[name];
225 log.error("gets: no service found: " + name);
230 public Applet? geta(string name)
232 if (name in memb_applets)
233 return memb_applets[name];
236 log.error("geta: no applet found: " + name);
241 public Menu? getmenu(string name)
243 if (name in memb_menus)
244 return memb_menus[name];
247 log.error("getmenu: no menu found: " + name);
253 zavai.Registry registry;