New e-d-s backend which indexes all local addressbooks and calendars.
[beagle.git] / Util / XKeybinder.cs
blob3bbb719f38211b9511406b0dbadd16a6523251c8
2 using System;
3 using System.Collections;
4 using System.Runtime.InteropServices;
5 using Mono.Posix;
7 namespace Beagle.Util
9 public class XKeybinder
11 [DllImport("libkeyglue")]
12 static extern void tomboy_keybinder_init ();
14 [DllImport("libkeyglue")]
15 static extern void tomboy_keybinder_bind (string keystring,
16 BindkeyHandler handler);
18 [DllImport("libkeyglue")]
19 static extern void tomboy_keybinder_unbind (string keystring,
20 BindkeyHandler handler);
22 public delegate void BindkeyHandler (string key, IntPtr user_data);
24 ArrayList bindings;
25 BindkeyHandler key_handler;
27 struct Binding {
28 internal string keystring;
29 internal EventHandler handler;
32 public XKeybinder ()
33 : base ()
35 bindings = new ArrayList ();
36 key_handler = new BindkeyHandler (KeybindingPressed);
38 tomboy_keybinder_init ();
41 void KeybindingPressed (string keystring, IntPtr user_data)
43 foreach (Binding bind in bindings) {
44 if (bind.keystring == keystring) {
45 bind.handler (this, new EventArgs ());
50 public void Bind (string keystring,
51 EventHandler handler)
53 Binding bind = new Binding ();
54 bind.keystring = keystring;
55 bind.handler = handler;
56 bindings.Add (bind);
58 tomboy_keybinder_bind (bind.keystring, key_handler);
61 public void Unbind (string keystring)
63 foreach (Binding bind in bindings) {
64 if (bind.keystring == keystring) {
65 tomboy_keybinder_unbind (bind.keystring,
66 key_handler);
68 bindings.Remove (bind);
69 break;
74 public virtual void UnbindAll ()
76 foreach (Binding bind in bindings) {
77 tomboy_keybinder_unbind (bind.keystring, key_handler);
80 bindings.Clear ();
84 public class GConfXKeybinder : XKeybinder
86 GConf.Client client;
87 ArrayList bindings;
89 public GConfXKeybinder ()
91 client = new GConf.Client ();
92 bindings = new ArrayList ();
95 public void Bind (string gconf_path,
96 string default_binding,
97 EventHandler handler)
99 try {
100 Binding binding = new Binding (gconf_path,
101 default_binding,
102 handler,
103 this);
104 bindings.Add (binding);
105 } catch (Exception e) {
106 Logger.Log.Error ("Error Adding global keybinding:");
107 Logger.Log.Error (e);
111 public override void UnbindAll ()
113 try {
114 bindings.Clear ();
115 base.UnbindAll ();
116 } catch (Exception e) {
117 Logger.Log.Error ("Error Removing global keybinding:");
118 Logger.Log.Error (e);
122 class Binding
124 public string gconf_path;
125 public string key_sequence;
126 EventHandler handler;
127 GConfXKeybinder parent;
129 public Binding (string gconf_path,
130 string default_binding,
131 EventHandler handler,
132 GConfXKeybinder parent)
134 this.gconf_path = gconf_path;
135 this.key_sequence = default_binding;
136 this.handler = handler;
137 this.parent = parent;
139 try {
140 key_sequence = (string) parent.client.Get (gconf_path);
141 } catch (Exception e) {
142 Logger.Log.Warn ("GConf key '{0}' does not exist, using default.",
143 gconf_path);
146 SetBinding ();
148 parent.client.AddNotify (
149 gconf_path,
150 new GConf.NotifyEventHandler (BindingChanged));
153 void BindingChanged (object sender, GConf.NotifyEventArgs args)
155 if (args.Key == gconf_path) {
156 Logger.Log.Debug ("Binding for '{0}' changed to '{1}'!",
157 gconf_path,
158 args.Value);
160 UnsetBinding ();
162 key_sequence = (string) args.Value;
163 SetBinding ();
167 public void SetBinding ()
169 if (key_sequence == null ||
170 key_sequence == String.Empty ||
171 key_sequence == "disabled")
172 return;
174 Logger.Log.Debug ("Binding key '{0}' for '{1}'",
175 key_sequence,
176 gconf_path);
178 parent.Bind (key_sequence, handler);
181 public void UnsetBinding ()
183 if (key_sequence == null)
184 return;
186 Logger.Log.Debug ("Unbinding key '{0}' for '{1}'",
187 key_sequence,
188 gconf_path);
190 parent.Unbind (key_sequence);