~ubuntu-branches/ubuntu/wily/gnome-do/wily

« back to all changes in this revision

Viewing changes to Do/src/CoreKeybindings.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2011-02-15 21:50:02 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110215215002-1j8ylb69o15asb06
Tags: 0.8.4-0ubuntu1
* The Race Against FF upload.  Merge from unreleased Debian git.
  Remaining Ubuntu changes:
  + debian/patches/05_disable_resize_grips.patch.diff:
    Disable resize handles for the Do windows.
  + debian/control:
    Bump gtk# build dep for HasResizeGrip API.
* New Debian changes:
* The long fortold release
  + Fixes a threadsafety issue resulting in 100% CPU usage (Closes: 565591,
    LP: #450852).
  + Proxies all keyring calls to the GTK main thread, as required by the new
    gnome-keyring (Closes: 603876, LP: #553643)
* debian/patches/00_bundledlibs.dpatch:
* debian/rules:
  + Upstream has dropped bundled gmcs binary; now 100% DFSG-free, so we don't
    have to repack the tarball or patch the buildsystem.
* debian/patches/03_disable_docky.dpatch:
  + Drop.  Docky is now gone in the upstream tarball.
* debian/rules:
* debian/control:
* debian/patches/*:
  + Switch to quilt to harmonise with other pkg-cli-* packages.
* debian/control:
  + Drop recommends on gnome-do-docklets.  Docky is now a separate package,
    so the docklets are useless for Do.
  + Bump Breaks on gnome-do-plugins to 0.8.3.  Do no longer provides the Wink
    library, which has been imported into the 0.8.3 do-plugins sources.
  + Bump standards-version; no changes needed.
  + Migrate to git and update VCS fields appropriately

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/* Keybindings.cs
3
 
 *
4
 
 * GNOME Do is the legal property of its developers. Please refer to the
5
 
 * COPYRIGHT file distributed with this
6
 
 * source distribution.
7
 
 *
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.
12
 
 *
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.
17
 
 *
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/>.
20
 
 */
21
 
 
22
 
using System;
23
 
using System.Linq;
24
 
using System.Collections;
25
 
using System.Collections.Generic;
26
 
using Env = System.Environment;
27
 
 
28
 
using Do.Platform;
29
 
 
30
 
namespace Do
31
 
{
32
 
 
33
 
        class CoreKeybindings 
34
 
        {
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
38
 
 
39
 
                IPreferences Preferences { get; set; }
40
 
 
41
 
                public ArrayList Shortcuts ;
42
 
                public Dictionary<string, List<KeyChangedCb>> PreferencesCbs;
43
 
                public delegate void KeyChangedCb (object sender, PreferencesChangedEventArgs e);
44
 
 
45
 
                public CoreKeybindings ()
46
 
                {
47
 
 
48
 
                        Preferences = Services.Preferences.Get<CoreKeybindings> ();
49
 
                        Preferences.PreferencesChanged += PreferencesChanged;
50
 
 
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>> ();
56
 
 
57
 
                        Initialize ();
58
 
                }
59
 
 
60
 
                public void Initialize ()
61
 
                {
62
 
                        // Read all values out of preferences and populate the KeybindingMap
63
 
                        ReadShortcuts ();
64
 
                }
65
 
                
66
 
                public bool RegisterShortcut (Shortcut sc, string defaultBinding)
67
 
                {
68
 
                        if (!RegisterShortcut (sc))
69
 
                                return false; 
70
 
                        if (!BindDefault (sc, defaultBinding))
71
 
                                return false;
72
 
                        return true;
73
 
                }
74
 
 
75
 
                public bool RegisterShortcut (Shortcut sc)
76
 
                {
77
 
                        if (Shortcuts.Contains (sc) || ShortcutMap.ContainsKey (sc.ShortcutName)) 
78
 
                                return false;
79
 
 
80
 
                        Shortcuts.Add (sc);
81
 
                        ShortcutMap [sc.ShortcutName] = sc;
82
 
                        PreferencesCbs [sc.ShortcutName] = new List<KeyChangedCb> ();
83
 
                        SaveShortcuts ();
84
 
                        return true;
85
 
                }
86
 
 
87
 
                public Shortcut GetShortcutByKeycode (string keycode)
88
 
                {
89
 
                        if (!KeycodeMap.ContainsKey (keycode)) 
90
 
                                return null;
91
 
                        
92
 
                        string scname = KeycodeMap [keycode];
93
 
 
94
 
                        if (!ShortcutMap.ContainsKey (scname)) 
95
 
                                return null;
96
 
 
97
 
                        return ShortcutMap [scname];
98
 
 
99
 
                }
100
 
 
101
 
                public string GetKeybinding (Shortcut sc)
102
 
                {
103
 
                        return GetKeybinding (sc.ShortcutName);
104
 
                }
105
 
 
106
 
                public string GetKeybinding (string sc)
107
 
                {
108
 
 
109
 
                        foreach (KeyValuePair<string, string> entry in KeycodeMap) {
110
 
                                if (entry.Value == sc) 
111
 
                                        return entry.Key;
112
 
                        }
113
 
                        return null;
114
 
                }
115
 
 
116
 
                public string GetDefaultKeybinding (Shortcut sc)
117
 
                {
118
 
                        return GetDefaultKeybinding (sc.ShortcutName);
119
 
                }
120
 
 
121
 
                public string GetDefaultKeybinding (string sc)
122
 
                {
123
 
                        foreach (KeyValuePair<string, string> entry in DefaultShortcutMap) {
124
 
                                if (entry.Value == sc) 
125
 
                                        return entry.Key;
126
 
                        }
127
 
                        return null;
128
 
                }
129
 
 
130
 
 
131
 
                public bool BindShortcut (Shortcut sc, string keycode)
132
 
                {
133
 
                        // Add this function to our keybinding map
134
 
                        return BindShortcut (sc.ShortcutName, keycode);
135
 
 
136
 
                }
137
 
 
138
 
                public bool BindShortcut (string sc, string keycode)
139
 
                {
140
 
                        string oldcode = GetKeybinding (sc);
141
 
                        if (oldcode != null)
142
 
                                KeycodeMap.Remove (oldcode); // remove the old keybinding from the map
143
 
 
144
 
                        KeycodeMap [keycode] = sc;
145
 
                        Preferences.Set (sc, keycode);
146
 
 
147
 
                        return true;
148
 
                }
149
 
 
150
 
                // Add Default Keycode mapping - used for resetting to default or not overwriting read values
151
 
                public bool BindDefault (Shortcut sc, string keycode)
152
 
                {
153
 
                        return BindDefault (sc.ShortcutName, keycode);
154
 
 
155
 
                }
156
 
 
157
 
                public bool BindDefault (string sc, string keycode)
158
 
                {
159
 
 
160
 
                        string assigned_keycode = GetKeybinding (sc);
161
 
                        if (assigned_keycode == null) {
162
 
                                // Put this shortcut in the mapping
163
 
                                BindShortcut (sc, keycode);
164
 
                        }
165
 
 
166
 
                        DefaultShortcutMap [keycode] = sc;
167
 
                        return true;
168
 
 
169
 
                }
170
 
 
171
 
                public bool UnregisterShortcut (Shortcut sc)
172
 
                {
173
 
                        if (!Shortcuts.Contains (sc))
174
 
                                return false;
175
 
 
176
 
                        Shortcuts.Remove (sc);
177
 
                        ShortcutMap.Remove (sc.ShortcutName);
178
 
                        SaveShortcuts ();
179
 
                        return true;
180
 
                }
181
 
 
182
 
                public bool RegisterNotification (Shortcut sc, KeyChangedCb cb)
183
 
                {
184
 
                        return RegisterNotification (sc.ShortcutName, cb);
185
 
                }
186
 
 
187
 
                public bool RegisterNotification (string scname, KeyChangedCb cb)
188
 
                {
189
 
                        PreferencesCbs [scname].Add (cb);
190
 
                        return true;
191
 
                }
192
 
 
193
 
                void SaveShortcuts () 
194
 
                {
195
 
                        string scstring = "";
196
 
                        foreach (Shortcut sc in Shortcuts) {
197
 
                                scstring += sc.ShortcutName.Trim () + ",";
198
 
                        }
199
 
                        Preferences.Set ("RegisteredShortcuts", scstring);
200
 
                } 
201
 
 
202
 
                void ReadShortcuts ()
203
 
                {
204
 
                        string scstring = Preferences.Get ("RegisteredShortcuts", "").Trim ();
205
 
                        if (scstring == "") 
206
 
                                return;
207
 
 
208
 
                        foreach (string sc in scstring.Split (',')) {
209
 
                                if (sc.Trim () == "") 
210
 
                                        continue;
211
 
 
212
 
                                string keycode = Preferences.Get (sc, "");
213
 
                                if (keycode != "")
214
 
                                        BindShortcut (sc, keycode);
215
 
                        }
216
 
                }
217
 
 
218
 
                void PreferencesChanged (object sender, PreferencesChangedEventArgs e)
219
 
                {
220
 
 
221
 
                        if (PreferencesCbs.ContainsKey (e.Key)) {   
222
 
                                foreach (KeyChangedCb cb in PreferencesCbs [e.Key]) {
223
 
                                        cb (this, e);
224
 
                                }
225
 
                        }
226
 
                }
227
 
 
228
 
        }
229
 
 
230
 
}