Replaced public MessageReader.Position with something marginally better.
[versaplex.git] / dbus-sharp / Bus.cs
blobf5060635bf28c6bd2a110c5ef9fe7e59f55fcc0d
1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // This software is made available under the MIT License
3 // See COPYING for details
5 using System;
6 using System.Collections.Generic;
7 using org.freedesktop.DBus;
9 namespace Wv
11 using Transports;
13 public sealed class Bus : Connection
15 static Bus systemBus = null;
16 public static Bus System
18 get {
19 if (systemBus == null) {
20 try {
21 if (Address.StarterBusType == "system")
22 systemBus = Starter;
23 else
24 systemBus = Bus.Open (Address.System);
25 } catch (Exception e) {
26 throw new Exception ("Unable to open the system message bus.", e);
30 return systemBus;
34 static Bus sessionBus = null;
35 public static Bus Session
37 get {
38 if (sessionBus == null) {
39 try {
40 if (Address.StarterBusType == "session")
41 sessionBus = Starter;
42 else
43 sessionBus = Bus.Open (Address.Session);
44 } catch (Exception e) {
45 throw new Exception ("Unable to open the session message bus.", e);
49 return sessionBus;
53 //TODO: parsing of starter bus type, or maybe do this another way
54 static Bus starterBus = null;
55 public static Bus Starter
57 get {
58 if (starterBus == null) {
59 try {
60 starterBus = Bus.Open (Address.Starter);
61 } catch (Exception e) {
62 throw new Exception ("Unable to open the starter message bus.", e);
66 return starterBus;
70 //public static readonly Bus Session = null;
72 //TODO: use the guid, not the whole address string
73 //TODO: consider what happens when a connection has been closed
74 static Dictionary<string,Bus> buses = new Dictionary<string,Bus> ();
76 //public static Connection Open (string address)
77 public static new Bus Open (string address)
79 if (address == null)
80 throw new ArgumentNullException ("address");
82 if (buses.ContainsKey (address))
83 return buses[address];
85 Bus bus = new Bus (address);
86 buses[address] = bus;
88 return bus;
91 IBus bus;
93 static readonly string DBusName = "org.freedesktop.DBus";
94 static readonly ObjectPath DBusPath = new ObjectPath ("/org/freedesktop/DBus");
96 public Bus (string address) : base (address)
98 bus = GetObject<IBus> (DBusName, DBusPath);
99 Register ();
102 public Bus (Transport trans) : base (trans)
104 Authenticate();
106 bus = GetObject<IBus> (DBusName, DBusPath);
107 Register ();
110 //should this be public?
111 //as long as Bus subclasses Connection, having a Register with a completely different meaning is bad
112 void Register ()
114 if (unique_name != null)
115 throw new Exception ("Bus already has a unique name");
117 unique_name = bus.Hello ();
120 public string GetUnixUserName (string name)
122 return bus.GetConnectionUnixUserName (name);
125 public ulong GetUnixUser (string name)
127 return bus.GetConnectionUnixUser (name);
130 public string GetCert (string name)
132 return bus.GetConnectionCert (name);
135 public string GetCertFingerprint (string name)
137 return bus.GetConnectionCertFingerprint (name);
140 public RequestNameReply RequestName (string name)
142 return RequestName (name, NameFlag.None);
145 public RequestNameReply RequestName (string name, NameFlag flags)
147 return bus.RequestName (name, flags);
150 public ReleaseNameReply ReleaseName (string name)
152 return bus.ReleaseName (name);
155 public bool NameHasOwner (string name)
157 return bus.NameHasOwner (name);
160 public StartReply StartServiceByName (string name)
162 return StartServiceByName (name, 0);
165 public StartReply StartServiceByName (string name, uint flags)
167 return bus.StartServiceByName (name, flags);
170 internal protected override void AddMatch (string rule)
172 bus.AddMatch (rule);
175 internal protected override void RemoveMatch (string rule)
177 bus.RemoveMatch (rule);
180 string unique_name = null;
181 public string UniqueName
183 get {
184 return unique_name;
185 } set {
186 if (unique_name != null)
187 throw new Exception ("Unique name can only be set once");
188 unique_name = value;