~ubuntu-branches/ubuntu/natty/gnome-do/natty

« 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: 2009-06-27 10:40:45 UTC
  • mfrom: (1.1.8 upstream) (0.1.5 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090627104045-7st10y1cqr6dpz37
Tags: 0.8.2+dfsg-1
* New upstream release
  + No longer uses a plugin repository.  Fixes many plugin-
    related issues. (LP: #343096, LP: #330025, LP #345001)
  + No longer blocks on "About Do" (LP: #361679)
  + Reacts correctly when a Composite manager is enabled/
    disabled at runtime. (LP: #346347, LP: #390150)
  + Fixes for space reserved by Docky blocking drag and 
    drop operations. (LP: #354729, LP: #347052, LP: #382843)
  + Properly sets "Hidden" key on autostart files in response to 
    "Start on login" option.  (Closes: #526023) (LP: #369988)
* debian/patches/10_application_search_path:
  + Drop; included upstream
* debian/patches/10_sk_translation_update:
  + Import sk translation update from Debian BTS.
    (Closes: #531779)
* debian/patches/11_fix_autostart_when_directory_does_not_exist:
  + Patch from upstream.  Fixes the "Start on login" option when the 
    ~/.config/autostart directory does not exist. (LP: #393729)
* debian/control:
  + Update standards version to 3.8.2; no changes required.
  + Add libtool to Build-Depends; required for autoreconf.
  + Add Recommends: on new gnome-do-docklets package.
* debian/gnome-do.1
  + Fix spelling: GNOME-Do => GNOME Do.
  + Miscelaneous lintian fixes; NAME section, escaping minus signs with \-
* debian/copyright:
  + Update for new copyright holders.
  + Minor update to DEP-5 format

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
}