Split things so that there can be a standalone zavai-calendar application
[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                 sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
44
45         bus_name = DBus.bus_get_unique_name(sbus.get_connection());
46         zavai.log.info("My bus name: " + bus_name);
47
48         dynamic DBus.Object tmp_dbus = sbus.get_object(
49             "org.freedesktop.DBus",
50             "/org/freedesktop/DBus",
51             "org.freedesktop.DBus");
52         bus_name = "org.enricozini.zavai";
53         uint res = tmp_dbus.RequestName(bus_name, (uint)DBus.NameFlag.DO_NOT_QUEUE);
54         switch (res)
55         {
56             case DBus.RequestNameReply.PRIMARY_OWNER:
57                 zavai.log.info("Registered to dbus as " + bus_name);
58                 break;
59             case DBus.RequestNameReply.IN_QUEUE:
60                 zavai.log.info("In queue, but I asked not to");
61                 break;
62             case DBus.RequestNameReply.EXISTS:
63                 zavai.log.info(bus_name + " already exists");
64                 break;
65             case DBus.RequestNameReply.ALREADY_OWNER:
66                 zavai.log.info("I already own the name " + bus_name + " but I do not remember asking for it");
67                 break;
68         }
69         }
70
71         public void shutdown()
72         {
73                 // Shutdown in reverse registration order
74                 for (int i = registration_order.size - 1; i >= 0; --i) 
75                         registration_order[i].shutdown();
76         }
77
78         public void register_resource(string name, Resource obj)
79         {
80                 memb_resources[name] = obj;
81                 registration_order.add(obj);
82         }
83
84         public void register_service(Service obj)
85         {
86                 memb_services[obj.name] = obj;
87                 registration_order.add(obj);
88         }
89
90         public void register_applet(string name, Applet obj)
91         {
92                 memb_applets[name] = obj;
93                 registration_order.add(obj);
94         }
95
96         public void register_menu(string name, Menu obj)
97         {
98                 memb_applets[name] = obj;
99                 memb_menus[name] = obj;
100                 registration_order.add(obj);
101         }
102
103         public Resource? getr(string name)
104         {
105                 if (name in memb_resources)
106                         return memb_resources[name];
107                 else
108                 {
109                         log.error("getr: no resource found: " + name);
110                         return null;
111                 }
112         }
113
114         public Service? gets(string name)
115         {
116                 if (name in memb_services)
117                         return memb_services[name];
118                 else
119                 {
120                         log.error("gets: no service found: " + name);
121                         return null;
122                 }
123         }
124
125         public Applet? geta(string name)
126         {
127                 if (name in memb_applets)
128                         return memb_applets[name];
129                 else
130                 {
131                         log.error("geta: no applet found: " + name);
132                         return null;
133                 }
134         }
135
136         public Menu? getmenu(string name)
137         {
138                 if (name in memb_menus)
139                         return memb_menus[name];
140                 else
141                 {
142                         log.error("getmenu: no menu found: " + name);
143                         return null;
144                 }
145         }
146 }
147
148 zavai.Registry registry;
149
150 }
151