-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathRemoteControlProxy.cs
118 lines (107 loc) · 3.48 KB
/
RemoteControlProxy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
#if ENABLE_DBUS
using DBus;
using org.freedesktop.DBus;
#else
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
#endif
namespace Tomboy
{
public static class RemoteControlProxy {
#if ENABLE_DBUS
private const string Path = "/org/gnome/Tomboy/RemoteControl";
private const string Namespace = "org.gnome.Tomboy";
private static bool? firstInstance;
#else
private static Mutex mutex;
private static bool firstInstance;
private const string MutexName = "{9EF7D32D-3392-4940-8A28-1320A7BD42AB}";
private static IpcChannel IpcChannel;
private const string ServerName = "TomboyServer";
private const string ClientName = "TomboyClient";
private const string WrapperName = "TomboyRemoteControlWrapper";
private static string ServiceUrl =
string.Format ("ipc://{0}/{1}", ServerName, WrapperName);
#endif
public static IRemoteControl GetInstance () {
#if ENABLE_DBUS
BusG.Init ();
if (! Bus.Session.NameHasOwner (Namespace))
Bus.Session.StartServiceByName (Namespace);
return Bus.Session.GetObject<RemoteControl> (Namespace,
new ObjectPath (Path));
#else
RemoteControlWrapper remote = (RemoteControlWrapper) Activator.GetObject (
typeof (RemoteControlWrapper),
ServiceUrl);
return remote;
#endif
}
public static RemoteControl Register (NoteManager manager)
{
#if ENABLE_DBUS
if (!FirstInstance)
return null;
RemoteControl remote_control = new RemoteControl (manager);
Bus.Session.Register (new ObjectPath (Path),
remote_control);
return remote_control;
#else
if (FirstInstance) {
// Register an IPC channel for .NET remoting
// access to our Remote Control
IpcChannel = new IpcChannel (ServerName);
ChannelServices.RegisterChannel (IpcChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType (
typeof (RemoteControlWrapper),
WrapperName,
WellKnownObjectMode.Singleton);
// The actual Remote Control has many methods
// that need to be called in the GTK+ mainloop,
// which will not happen when the method calls
// come from a .NET remoting client. So we wrap
// the Remote Control in a class that implements
// the same interface, but wraps most method
// calls in Gtk.Application.Invoke.
//
// Note that only one RemoteControl is ever
// created, and that it is stored statically
// in the RemoteControlWrapper.
RemoteControl realRemote = new RemoteControl (manager);
RemoteControlWrapper.Initialize (realRemote);
RemoteControlWrapper remoteWrapper = (RemoteControlWrapper) Activator.GetObject (
typeof (RemoteControlWrapper),
ServiceUrl);
return realRemote;
} else {
// If Tomboy is already running, register a
// client IPC channel.
IpcChannel = new IpcChannel (ClientName);
ChannelServices.RegisterChannel (IpcChannel, false);
return null;
}
#endif
}
public static bool FirstInstance {
get {
#if ENABLE_DBUS
// We use DBus to provide single-instance detection
if (!firstInstance.HasValue) {
BusG.Init ();
firstInstance = Bus.Session.RequestName (Namespace) == RequestNameReply.PrimaryOwner;
}
return firstInstance.Value;
#else
// Use a mutex to provide single-instance detection
if (mutex == null)
mutex = new Mutex (true, MutexName, out firstInstance);
return firstInstance;
#endif
}
}
}
}