4
* GNOME Do is the legal property of its developers. Please refer to the
5
* COPYRIGHT file distributed with this
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
using System.Collections;
25
using System.Collections.Generic;
26
using Env = System.Environment;
35
Dictionary<string, string> KeycodeMap ; // keybinding -> shortcut name
36
Dictionary<string, Shortcut> ShortcutMap ; // shortcut name -> shortcut
37
Dictionary<string, string> DefaultShortcutMap ; // default keybinding -> shortcut name
39
IPreferences Preferences { get; set; }
41
public ArrayList Shortcuts ;
42
public Dictionary<string, List<KeyChangedCb>> PreferencesCbs;
43
public delegate void KeyChangedCb (object sender, PreferencesChangedEventArgs e);
45
public CoreKeybindings ()
48
Preferences = Services.Preferences.Get<CoreKeybindings> ();
49
Preferences.PreferencesChanged += PreferencesChanged;
51
Shortcuts = new ArrayList ();
52
KeycodeMap = new Dictionary<string, string> (); // keybinding -> shortcut name
53
ShortcutMap = new Dictionary<string, Shortcut> (); // shortcut name -> shortcut
54
DefaultShortcutMap = new Dictionary<string, string> (); // default keybinding -> shortcut name
55
PreferencesCbs = new Dictionary<string, List<KeyChangedCb>> ();
60
public void Initialize ()
62
// Read all values out of preferences and populate the KeybindingMap
66
public bool RegisterShortcut (Shortcut sc, string defaultBinding)
68
if (!RegisterShortcut (sc))
70
if (!BindDefault (sc, defaultBinding))
75
public bool RegisterShortcut (Shortcut sc)
77
if (Shortcuts.Contains (sc) || ShortcutMap.ContainsKey (sc.ShortcutName))
81
ShortcutMap [sc.ShortcutName] = sc;
82
PreferencesCbs [sc.ShortcutName] = new List<KeyChangedCb> ();
87
public Shortcut GetShortcutByKeycode (string keycode)
89
if (!KeycodeMap.ContainsKey (keycode))
92
string scname = KeycodeMap [keycode];
94
if (!ShortcutMap.ContainsKey (scname))
97
return ShortcutMap [scname];
101
public string GetKeybinding (Shortcut sc)
103
return GetKeybinding (sc.ShortcutName);
106
public string GetKeybinding (string sc)
109
foreach (KeyValuePair<string, string> entry in KeycodeMap) {
110
if (entry.Value == sc)
116
public string GetDefaultKeybinding (Shortcut sc)
118
return GetDefaultKeybinding (sc.ShortcutName);
121
public string GetDefaultKeybinding (string sc)
123
foreach (KeyValuePair<string, string> entry in DefaultShortcutMap) {
124
if (entry.Value == sc)
131
public bool BindShortcut (Shortcut sc, string keycode)
133
// Add this function to our keybinding map
134
return BindShortcut (sc.ShortcutName, keycode);
138
public bool BindShortcut (string sc, string keycode)
140
string oldcode = GetKeybinding (sc);
142
KeycodeMap.Remove (oldcode); // remove the old keybinding from the map
144
KeycodeMap [keycode] = sc;
145
Preferences.Set (sc, keycode);
150
// Add Default Keycode mapping - used for resetting to default or not overwriting read values
151
public bool BindDefault (Shortcut sc, string keycode)
153
return BindDefault (sc.ShortcutName, keycode);
157
public bool BindDefault (string sc, string keycode)
160
string assigned_keycode = GetKeybinding (sc);
161
if (assigned_keycode == null) {
162
// Put this shortcut in the mapping
163
BindShortcut (sc, keycode);
166
DefaultShortcutMap [keycode] = sc;
171
public bool UnregisterShortcut (Shortcut sc)
173
if (!Shortcuts.Contains (sc))
176
Shortcuts.Remove (sc);
177
ShortcutMap.Remove (sc.ShortcutName);
182
public bool RegisterNotification (Shortcut sc, KeyChangedCb cb)
184
return RegisterNotification (sc.ShortcutName, cb);
187
public bool RegisterNotification (string scname, KeyChangedCb cb)
189
PreferencesCbs [scname].Add (cb);
193
void SaveShortcuts ()
195
string scstring = "";
196
foreach (Shortcut sc in Shortcuts) {
197
scstring += sc.ShortcutName.Trim () + ",";
199
Preferences.Set ("RegisteredShortcuts", scstring);
202
void ReadShortcuts ()
204
string scstring = Preferences.Get ("RegisteredShortcuts", "").Trim ();
208
foreach (string sc in scstring.Split (',')) {
209
if (sc.Trim () == "")
212
string keycode = Preferences.Get (sc, "");
214
BindShortcut (sc, keycode);
218
void PreferencesChanged (object sender, PreferencesChangedEventArgs e)
221
if (PreferencesCbs.ContainsKey (e.Key)) {
222
foreach (KeyChangedCb cb in PreferencesCbs [e.Key]) {