Merge branch 'master' into alarm
[gregoa/zavai.git] / src / registry.vala
1 /* 
2  * registry - zavai resource registry
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22 using Gee;
23
24 namespace zavai {
25
26 public class Registry : Object, Resource
27 {
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;
35
36         public Registry()
37         {
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>();
43                 try {
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);
47                         sbus = null;
48                 }
49
50         bus_name = DBus.bus_get_unique_name(sbus.get_connection());
51         zavai.log.info("My bus name: " + bus_name);
52
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);
59         switch (res)
60         {
61             case DBus.RequestNameReply.PRIMARY_OWNER:
62                 zavai.log.info("Registered to dbus as " + bus_name);
63                 break;
64             case DBus.RequestNameReply.IN_QUEUE:
65                 zavai.log.info("In queue, but I asked not to");
66                 break;
67             case DBus.RequestNameReply.EXISTS:
68                 zavai.log.info(bus_name + " already exists");
69                 break;
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");
72                 break;
73         }
74         }
75
76         public void shutdown()
77         {
78                 // Shutdown in reverse registration order
79                 for (int i = registration_order.size - 1; i >= 0; --i) 
80                         registration_order[i].shutdown();
81         }
82
83         public void register_resource(string name, Resource obj)
84         {
85                 memb_resources[name] = obj;
86                 registration_order.add(obj);
87         }
88
89         public void register_service(Service obj)
90         {
91                 memb_services[obj.name] = obj;
92                 registration_order.add(obj);
93         }
94
95         public void register_applet(string name, Applet obj)
96         {
97                 memb_applets[name] = obj;
98                 registration_order.add(obj);
99         }
100
101         public void register_menu(string name, Menu obj)
102         {
103                 memb_applets[name] = obj;
104                 memb_menus[name] = obj;
105                 registration_order.add(obj);
106         }
107
108         public Resource? getr(string name)
109         {
110                 if (name in memb_resources)
111                         return memb_resources[name];
112                 else
113                 {
114                         log.error("getr: no resource found: " + name);
115                         return null;
116                 }
117         }
118
119         public Service? gets(string name)
120         {
121                 if (name in memb_services)
122                         return memb_services[name];
123                 else
124                 {
125                         log.error("gets: no service found: " + name);
126                         return null;
127                 }
128         }
129
130         public Applet? geta(string name)
131         {
132                 if (name in memb_applets)
133                         return memb_applets[name];
134                 else
135                 {
136                         log.error("geta: no applet found: " + name);
137                         return null;
138                 }
139         }
140
141         public Menu? getmenu(string name)
142         {
143                 if (name in memb_menus)
144                         return memb_menus[name];
145                 else
146                 {
147                         log.error("getmenu: no menu found: " + name);
148                         return null;
149                 }
150         }
151 }
152
153 zavai.Registry registry;
154
155 }
156