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 class Registry : Object, Resource
28 HashMap<string, Resource> memb_resources;
29 HashMap<string, Service> memb_services;
30 HashMap<string, Applet> memb_applets;
31 HashMap<string, Menu> memb_menus;
32 protected ArrayList<Resource> registration_order;
33 public DBus.Connection sbus;
34 public string bus_name;
38 memb_resources = new HashMap<string, Resource>(str_hash, str_equal);
39 memb_services = new HashMap<string, Service>(str_hash, str_equal);
40 memb_applets = new HashMap<string, Applet>(str_hash, str_equal);
41 memb_menus = new HashMap<string, Menu>(str_hash, str_equal);
42 registration_order = new ArrayList<Resource>();
44 sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
45 } catch (DBus.Error e) {
46 stderr.printf("Cannot access system DBus bus: %s\n", e.message);
50 bus_name = DBus.bus_get_unique_name(sbus.get_connection());
51 zavai.log.info("My bus name: " + bus_name);
53 dynamic DBus.Object tmp_dbus = sbus.get_object(
54 "org.freedesktop.DBus",
55 "/org/freedesktop/DBus",
56 "org.freedesktop.DBus");
57 bus_name = "org.enricozini.zavai";
58 uint res = tmp_dbus.RequestName(bus_name, (uint)DBus.NameFlag.DO_NOT_QUEUE);
61 case DBus.RequestNameReply.PRIMARY_OWNER:
62 zavai.log.info("Registered to dbus as " + bus_name);
64 case DBus.RequestNameReply.IN_QUEUE:
65 zavai.log.info("In queue, but I asked not to");
67 case DBus.RequestNameReply.EXISTS:
68 zavai.log.info(bus_name + " already exists");
70 case DBus.RequestNameReply.ALREADY_OWNER:
71 zavai.log.info("I already own the name " + bus_name + " but I do not remember asking for it");
76 public void shutdown()
78 // Shutdown in reverse registration order
79 for (int i = registration_order.size - 1; i >= 0; --i)
80 registration_order[i].shutdown();
83 public void register_resource(string name, Resource obj)
85 memb_resources[name] = obj;
86 registration_order.add(obj);
89 public void register_service(Service obj)
91 memb_services[obj.name] = obj;
92 registration_order.add(obj);
95 public void register_applet(string name, Applet obj)
97 memb_applets[name] = obj;
98 registration_order.add(obj);
101 public void register_menu(string name, Menu obj)
103 memb_applets[name] = obj;
104 memb_menus[name] = obj;
105 registration_order.add(obj);
108 public Resource? getr(string name)
110 if (name in memb_resources)
111 return memb_resources[name];
114 log.error("getr: no resource found: " + name);
119 public Service? gets(string name)
121 if (name in memb_services)
122 return memb_services[name];
125 log.error("gets: no service found: " + name);
130 public Applet? geta(string name)
132 if (name in memb_applets)
133 return memb_applets[name];
136 log.error("geta: no applet found: " + name);
141 public Menu? getmenu(string name)
143 if (name in memb_menus)
144 return memb_menus[name];
147 log.error("getmenu: no menu found: " + name);
153 zavai.Registry registry;