Made all DBusTypes take Service in the constructor because Array also needed it in...
[platform/upstream/dbus.git] / mono / Service.cs
1 namespace DBus
2 {
3   using System;
4   using System.Runtime.InteropServices;
5   using System.Diagnostics;
6   using System.Collections;
7   using System.Reflection;
8   using System.Reflection.Emit;
9   
10   public class Service
11   {
12     private Connection connection;
13     private string name;
14     private bool local = false;
15     private Hashtable registeredHandlers = new Hashtable();
16     internal ModuleBuilder module = null;
17
18     internal Service(string name, Connection connection)
19     {
20       this.name = name;
21       this.connection = connection;
22     }
23
24     public Service(Connection connection, string name)
25     {
26       Error error = new Error();
27       error.Init();
28       
29       // This isn't used for now
30       uint flags = 0;
31
32       if (dbus_bus_acquire_service(connection.RawConnection, name, flags, ref error) == -1) {
33         throw new DBusException(error);
34       }
35
36       this.connection = connection;
37       this.name = name;
38       this.local = true;
39     }
40
41     public static bool Exists(Connection connection, string name)
42     {
43       Error error = new Error();
44       error.Init();
45       
46       if (dbus_bus_service_exists(connection.RawConnection, 
47                                   name, 
48                                   ref error)) {
49         return true;
50       } else {
51         if (error.IsSet) {
52           throw new DBusException(error);
53         }
54         return false;
55       }
56     }
57
58     public static Service Get(Connection connection, string name)
59     {
60       if (Exists(connection, name)) {
61         return new Service(name, connection);
62       } else {
63         throw new ApplicationException("Service '" + name + "' does not exist.");
64       }
65     }
66
67     public void RegisterObject(object handledObject, 
68                                string pathName) 
69     {
70       Handler handler = new Handler(handledObject, 
71                                     pathName, 
72                                     this);
73       registeredHandlers.Add(handledObject, handler);
74     }
75
76     internal Handler GetHandler(object handledObject) 
77     {
78       if (!registeredHandlers.Contains(handledObject)) {
79         throw new ArgumentException("No handler registered for object: " + handledObject);
80       }
81       
82       return (Handler) registeredHandlers[handledObject];
83     }
84
85     public object GetObject(Type type, string pathName)
86     {
87       ProxyBuilder builder = new ProxyBuilder(this, type, pathName);
88       object proxy = builder.GetProxy();
89       return proxy;
90     }
91
92     public string Name
93     {
94       get
95         {
96           return this.name;
97         }
98     }
99
100     public Connection Connection 
101     {
102       get
103         {
104           return connection;
105         }
106       
107       set 
108         {
109           this.connection = value;
110         }
111     }
112
113     [DllImport ("dbus-1")]
114     private extern static int dbus_bus_acquire_service (IntPtr rawConnection, string serviceName, uint flags, ref Error error);
115
116     [DllImport ("dbus-1")]
117     private extern static bool dbus_bus_service_exists (IntPtr rawConnection, string serviceName, ref Error error);    
118   }
119 }