Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / spatialnavigation / src / nsSpatialNavigationService.cpp
blob0997ae3f6727d75533ce90f6a815f20bdc35b417
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Spatial Navigation
16 * The Initial Developer of the Original Code is
17 * Douglas F. Turner II <dougt@meer.net>
18 * Portions created by the Initial Developer are Copyright (C) 2004-2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "nsSpatialNavigationPrivate.h"
38 #include "nsIObserverService.h"
40 nsSpatialNavigationService::nsSpatialNavigationService()
42 mEnabled = PR_TRUE;
43 mIgnoreTextFields = PR_TRUE;
44 mDisableJSWhenFocusing = PR_TRUE;
46 mKeyCodeLeft = nsIDOMKeyEvent::DOM_VK_LEFT;
47 mKeyCodeRight = nsIDOMKeyEvent::DOM_VK_RIGHT;
48 mKeyCodeUp = nsIDOMKeyEvent::DOM_VK_UP;
49 mKeyCodeDown = nsIDOMKeyEvent::DOM_VK_DOWN;
51 mKeyCodeModifier = 0x00000012 | 0x00100000; // By default ALT and SHIFT
54 nsSpatialNavigationService::~nsSpatialNavigationService()
58 NS_IMPL_ISUPPORTS1(nsSpatialNavigationService, nsIObserver)
60 NS_IMETHODIMP
61 nsSpatialNavigationService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
63 nsresult rv;
65 if (!strcmp(aTopic,"domwindowopened"))
67 nsCOMPtr<nsIDOMWindow> chromeWindow = do_QueryInterface(aSubject);
69 nsSpatialNavigation* sn = new nsSpatialNavigation(this);
71 if (!sn)
72 return NS_ERROR_OUT_OF_MEMORY;
74 sn->Init(chromeWindow);
76 mObjects.AppendObject(sn); // the array owns the only reference to sn.
78 return NS_OK;
81 if (!strcmp(aTopic,"domwindowclosed"))
83 nsCOMPtr<nsIDOMWindow> chromeWindow = do_QueryInterface(aSubject);
84 // need to find it in our array
86 PRInt32 count = mObjects.Count();
87 for (PRInt32 i = 0; i < count; i++)
89 nsISpatialNavigation* sn = mObjects[i];
90 nsCOMPtr<nsIDOMWindow> attachedWindow;
91 sn->GetAttachedWindow(getter_AddRefs(attachedWindow));
93 if (attachedWindow == chromeWindow)
95 sn->Shutdown();
96 mObjects.RemoveObjectAt(i);
97 return NS_OK;
100 return NS_OK;
104 if (!strcmp(aTopic,"app-startup"))
106 nsCOMPtr<nsIWindowWatcher> windowWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
107 NS_ENSURE_SUCCESS(rv, rv);
108 windowWatcher->RegisterNotification(this);
110 nsCOMPtr<nsIPrefBranch2> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
111 NS_ENSURE_SUCCESS(rv, rv);
113 prefBranch->AddObserver("snav.", this, PR_FALSE);
115 nsCOMPtr<nsIObserverService> observerService =
116 do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &rv);
117 NS_ENSURE_SUCCESS(rv, rv);
118 rv = observerService->AddObserver(this, "profile-after-change", PR_FALSE);
119 NS_ENSURE_SUCCESS(rv, rv);
121 return NS_OK;
124 if (!strcmp(aTopic,"profile-after-change"))
126 // the profile has loaded, read in the preferences
127 nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
128 NS_ENSURE_SUCCESS(rv, rv);
130 PRBool tempBool;
131 PRInt32 tempInt32;
133 // note that if Get*Pref fails, the pref may not exist, so we fallback to
134 // the defaults as defined in the constructor
136 rv = prefBranch->GetBoolPref("snav.enabled", &tempBool);
137 if (NS_SUCCEEDED(rv))
138 mEnabled = tempBool;
139 rv = prefBranch->GetBoolPref("snav.ignoreTextFields", &tempBool);
140 if (NS_SUCCEEDED(rv))
141 mIgnoreTextFields = tempBool;
142 rv = prefBranch->GetIntPref("snav.directionalBias", &tempInt32);
143 if (NS_SUCCEEDED(rv))
145 gDirectionalBias = tempInt32;
146 if (gDirectionalBias == 0)
147 gDirectionalBias = 1;
149 rv = prefBranch->GetBoolPref("snav.disableJS", &tempBool);
150 if (NS_SUCCEEDED(rv))
151 mDisableJSWhenFocusing = tempBool;
152 rv = prefBranch->GetIntPref("snav.rectFudge", &tempInt32);
153 if (NS_SUCCEEDED(rv))
154 gRectFudge = tempInt32;
155 rv = prefBranch->GetIntPref("snav.keyCode.left", &tempInt32);
156 if (NS_SUCCEEDED(rv))
157 mKeyCodeLeft = tempInt32;
158 rv = prefBranch->GetIntPref("snav.keyCode.right", &tempInt32);
159 if (NS_SUCCEEDED(rv))
160 mKeyCodeRight = tempInt32;
161 rv = prefBranch->GetIntPref("snav.keyCode.up", &tempInt32);
162 if (NS_SUCCEEDED(rv))
163 mKeyCodeUp = tempInt32;
164 rv = prefBranch->GetIntPref("snav.keyCode.down", &tempInt32);
165 if (NS_SUCCEEDED(rv))
166 mKeyCodeDown = tempInt32;
167 rv = prefBranch->GetIntPref("snav.keyCode.modifier", &tempInt32);
168 if (NS_SUCCEEDED(rv))
169 mKeyCodeModifier = tempInt32;
171 return NS_OK;
174 if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID))
176 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
177 nsXPIDLCString cstr;
179 const char* pref = NS_ConvertUTF16toUTF8(aData).get();
181 if (!strcmp(pref, "snav.enabled"))
183 prefBranch->GetBoolPref(pref, &mEnabled);
185 else if (!strcmp(pref, "snav.ignoreTextFields"))
187 prefBranch->GetBoolPref(pref, &mIgnoreTextFields);
189 else if (!strcmp(pref, "snav.directionalBias"))
191 prefBranch->GetIntPref(pref, &gDirectionalBias);
192 if (gDirectionalBias == 0)
193 gDirectionalBias = 1;
195 else if (!strcmp(pref, "snav.disableJS"))
197 prefBranch->GetBoolPref(pref, &mDisableJSWhenFocusing);
199 else if (!strcmp(pref, "snav.rectFudge"))
201 prefBranch->GetIntPref(pref, &gRectFudge);
203 else if (!strcmp(pref, "snav.keyCode.left"))
205 prefBranch->GetIntPref(pref, &mKeyCodeLeft);
207 else if (!strcmp(pref, "snav.keyCode.right"))
209 prefBranch->GetIntPref(pref, &mKeyCodeRight);
211 else if (!strcmp(pref, "snav.keyCode.up"))
213 prefBranch->GetIntPref(pref, &mKeyCodeUp);
215 else if (!strcmp(pref, "snav.keyCode.down"))
217 prefBranch->GetIntPref(pref, &mKeyCodeDown);
219 else if (!strcmp(pref, "snav.keyCode.modifier"))
221 prefBranch->GetIntPref(pref, &mKeyCodeModifier);
224 return NS_OK;
227 return NS_OK;
231 //------------------------------------------------------------------------------
232 // XPCOM REGISTRATION BELOW
233 //------------------------------------------------------------------------------
235 #define SpatialNavigation_CID \
236 { 0xd1b91385, 0xe1c1, 0x46ec, \
237 {0x8d, 0x15, 0x88, 0x0c, 0x45, 0xbe, 0x8e, 0x0e} }
239 #define SpatialNavigation_ContractID "@mozilla.org/spatialNavigation/service;1"
241 #define SpatialNavigationService_CID \
242 { 0x4125624b, 0xaf22, 0x4d50, \
243 { 0x87, 0xf6, 0x40, 0x19, 0xc9, 0x85, 0x7b, 0x58} }
245 #define SpatialNavigationService_ContractID "@mozilla.org/spatialnavigation/service"
247 static NS_METHOD SpatialNavigationServiceRegistration(nsIComponentManager *aCompMgr,
248 nsIFile *aPath,
249 const char *registryLocation,
250 const char *componentType,
251 const nsModuleComponentInfo *info)
253 nsresult rv;
255 nsCOMPtr<nsIServiceManager> servman = do_QueryInterface((nsISupports*)aCompMgr, &rv);
256 if (NS_FAILED(rv))
257 return rv;
260 nsCOMPtr<nsICategoryManager> catman;
261 servman->GetServiceByContractID(NS_CATEGORYMANAGER_CONTRACTID,
262 NS_GET_IID(nsICategoryManager),
263 getter_AddRefs(catman));
265 if (NS_FAILED(rv))
266 return rv;
268 char* previous = nsnull;
269 rv = catman->AddCategoryEntry("app-startup",
270 "SpatialNavigationService",
271 SpatialNavigationService_ContractID,
272 PR_TRUE,
273 PR_TRUE,
274 &previous);
275 if (previous)
276 nsMemory::Free(previous);
278 return rv;
281 static NS_METHOD SpatialNavigationServiceUnregistration(nsIComponentManager *aCompMgr,
282 nsIFile *aPath,
283 const char *registryLocation,
284 const nsModuleComponentInfo *info)
286 nsresult rv;
288 nsCOMPtr<nsIServiceManager> servman = do_QueryInterface((nsISupports*)aCompMgr, &rv);
289 if (NS_FAILED(rv))
290 return rv;
292 nsCOMPtr<nsICategoryManager> catman;
293 servman->GetServiceByContractID(NS_CATEGORYMANAGER_CONTRACTID,
294 NS_GET_IID(nsICategoryManager),
295 getter_AddRefs(catman));
297 if (NS_FAILED(rv))
298 return rv;
300 rv = catman->DeleteCategoryEntry("app-startup",
301 "SpatialNavigationService",
302 PR_TRUE);
304 return rv;
309 NS_GENERIC_FACTORY_CONSTRUCTOR(nsSpatialNavigationService)
312 static const nsModuleComponentInfo components[] =
314 { "SpatialNavigationService",
315 SpatialNavigationService_CID,
316 SpatialNavigationService_ContractID,
317 nsSpatialNavigationServiceConstructor,
318 SpatialNavigationServiceRegistration,
319 SpatialNavigationServiceUnregistration
324 NS_IMPL_NSGETMODULE(SpatialNavigationModule, components)