| 1 |
// Copyright 2006 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.Collections.Generic; |
| 7 |
using org.freedesktop.DBus; |
| 8 |
|
| 9 |
namespace NDesk.DBus |
| 10 |
{ |
| 11 |
public sealed class Bus : Connection |
| 12 |
{ |
| 13 |
static Bus systemBus = null; |
| 14 |
public static Bus System |
| 15 |
{ |
| 16 |
get { |
| 17 |
if (systemBus == null) { |
| 18 |
try { |
| 19 |
if (Address.StarterBusType == "system") |
| 20 |
systemBus = Starter; |
| 21 |
else |
| 22 |
systemBus = Bus.Open (Address.System); |
| 23 |
} catch (Exception e) { |
| 24 |
throw new Exception ("Unable to open the system message bus.", e); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return systemBus; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
static Bus sessionBus = null; |
| 33 |
public static Bus Session |
| 34 |
{ |
| 35 |
get { |
| 36 |
if (sessionBus == null) { |
| 37 |
try { |
| 38 |
if (Address.StarterBusType == "session") |
| 39 |
sessionBus = Starter; |
| 40 |
else |
| 41 |
sessionBus = Bus.Open (Address.Session); |
| 42 |
} catch (Exception e) { |
| 43 |
throw new Exception ("Unable to open the session message bus.", e); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return sessionBus; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
//TODO: parsing of starter bus type, or maybe do this another way |
| 52 |
static Bus starterBus = null; |
| 53 |
public static Bus Starter |
| 54 |
{ |
| 55 |
get { |
| 56 |
if (starterBus == null) { |
| 57 |
try { |
| 58 |
starterBus = Bus.Open (Address.Starter); |
| 59 |
} catch (Exception e) { |
| 60 |
throw new Exception ("Unable to open the starter message bus.", e); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return starterBus; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
//public static readonly Bus Session = null; |
| 69 |
|
| 70 |
//TODO: use the guid, not the whole address string |
| 71 |
//TODO: consider what happens when a connection has been closed |
| 72 |
static Dictionary<string,Bus> buses = new Dictionary<string,Bus> (); |
| 73 |
|
| 74 |
//public static Connection Open (string address) |
| 75 |
public static new Bus Open (string address) |
| 76 |
{ |
| 77 |
if (address == null) |
| 78 |
throw new ArgumentNullException ("address"); |
| 79 |
|
| 80 |
if (buses.ContainsKey (address)) |
| 81 |
return buses[address]; |
| 82 |
|
| 83 |
Bus bus = new Bus (address); |
| 84 |
buses[address] = bus; |
| 85 |
|
| 86 |
return bus; |
| 87 |
} |
| 88 |
|
| 89 |
IBus bus; |
| 90 |
|
| 91 |
static readonly string DBusName = "org.freedesktop.DBus"; |
| 92 |
static readonly ObjectPath DBusPath = new ObjectPath ("/org/freedesktop/DBus"); |
| 93 |
|
| 94 |
public Bus (string address) : base (address) |
| 95 |
{ |
| 96 |
bus = GetObject<IBus> (DBusName, DBusPath); |
| 97 |
|
| 98 |
/* |
| 99 |
bus.NameAcquired += delegate (string acquired_name) { |
| 100 |
Console.WriteLine ("NameAcquired: " + acquired_name); |
| 101 |
}; |
| 102 |
*/ |
| 103 |
Register (); |
| 104 |
} |
| 105 |
|
| 106 |
//should this be public? |
| 107 |
//as long as Bus subclasses Connection, having a Register with a completely different meaning is bad |
| 108 |
void Register () |
| 109 |
{ |
| 110 |
if (unique_name != null) |
| 111 |
throw new Exception ("Bus already has a unique name"); |
| 112 |
|
| 113 |
unique_name = bus.Hello (); |
| 114 |
} |
| 115 |
|
| 116 |
public ulong GetUnixUser (string name) |
| 117 |
{ |
| 118 |
return bus.GetConnectionUnixUser (name); |
| 119 |
} |
| 120 |
|
| 121 |
public RequestNameReply RequestName (string name) |
| 122 |
{ |
| 123 |
return RequestName (name, NameFlag.None); |
| 124 |
} |
| 125 |
|
| 126 |
public RequestNameReply RequestName (string name, NameFlag flags) |
| 127 |
{ |
| 128 |
return bus.RequestName (name, flags); |
| 129 |
} |
| 130 |
|
| 131 |
public ReleaseNameReply ReleaseName (string name) |
| 132 |
{ |
| 133 |
return bus.ReleaseName (name); |
| 134 |
} |
| 135 |
|
| 136 |
public bool NameHasOwner (string name) |
| 137 |
{ |
| 138 |
return bus.NameHasOwner (name); |
| 139 |
} |
| 140 |
|
| 141 |
public StartReply StartServiceByName (string name) |
| 142 |
{ |
| 143 |
return StartServiceByName (name, 0); |
| 144 |
} |
| 145 |
|
| 146 |
public StartReply StartServiceByName (string name, uint flags) |
| 147 |
{ |
| 148 |
return bus.StartServiceByName (name, flags); |
| 149 |
} |
| 150 |
|
| 151 |
internal protected override void AddMatch (string rule) |
| 152 |
{ |
| 153 |
bus.AddMatch (rule); |
| 154 |
} |
| 155 |
|
| 156 |
internal protected override void RemoveMatch (string rule) |
| 157 |
{ |
| 158 |
bus.RemoveMatch (rule); |
| 159 |
} |
| 160 |
|
| 161 |
string unique_name = null; |
| 162 |
public string UniqueName |
| 163 |
{ |
| 164 |
get { |
| 165 |
return unique_name; |
| 166 |
} set { |
| 167 |
if (unique_name != null) |
| 168 |
throw new Exception ("Unique name can only be set once"); |
| 169 |
unique_name = value; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |