Index: lib/typoBootstrap.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/typoBootstrap.js |
@@ -0,0 +1,191 @@ |
+/* |
+ * This Source Code is subject to the terms of the Mozilla Public License |
+ * version 2.0 (the "License"). You can obtain a copy of the License at |
+ * http://mozilla.org/MPL/2.0/. |
+ */ |
+ |
+/** |
+ * @fileOverview Adds typo correction feature |
+ */ |
+ |
+Cu.import("resource://gre/modules/AddonManager.jsm"); |
+ |
+let {TimeLine} = require("timeline"); |
+let {Prefs} = require("prefs"); |
+TimeLine.log("Done loading preferences"); |
+ |
+Cu.reportError("init"); |
+let urlfixerID = "{0fa2149e-bb2c-4ac2-a8d3-479599819475}"; |
+let isTypoCorrectionEnabled = false; |
+ |
+function enableTypoCorrection() |
+{ |
+ Cu.reportError("enable"); |
+ if (isTypoCorrectionEnabled) |
+ return; |
+ |
+ require("typoFixer").attachWindowObserver(); |
+ isTypoCorrectionEnabled = true; |
+} |
+ |
+function disableTypoCorrection() |
+{ |
+ Cu.reportError("disable"); |
+ if (!isTypoCorrectionEnabled) |
+ return; |
+ |
+ require("typoFixer").detachWindowObserver(); |
+ isTypoCorrectionEnabled = false; |
+} |
+ |
+/* |
Wladimir Palant
2012/11/19 13:49:45
Debug code?
|
+function checkAddonStatusAndEnable() |
+{ |
+ Cu.reportError("check addon status and enable"); |
+ AddonManager.getAddonByID(urlfixerID, function(addon) |
+ { |
+ checkAndEnable(addon && !addon.userDisabled); |
+ }); |
+} |
+ |
+function checkAndEnable(isInstalledAndEnabled) |
+{ |
+ Cu.reportError("check and enable"); |
+ //Cu.reportError(isInstalledAndEnabled); |
+ Cu.reportError(Prefs.correctTyposAsked); |
+ Cu.reportError(Prefs.correctTypos); |
+ Cu.reportError(isTypoCorrectionEnabled); |
+ if (isInstalledAndEnabled) |
+ { |
+ disableTypoCorrection(); |
+ } |
+ else |
+ { |
+ if (!Prefs.correctTyposAsked || (Prefs.correctTyposAsked && Prefs.correctTypos)) |
+ { |
+ Cu.reportError("feature enabled"); |
+ enableTypoCorrection(); |
+ } |
+ else if (!isTypoCorrectionEnabled) |
+ { |
+ Cu.reportError("feature disabled"); |
+ function onPrefChange(name) |
+ { |
+ if (name == "correctTypos") |
+ { |
+ checkAddonStatusAndEnable(); |
+ Prefs.removeListener(onPrefChange); |
+ } |
+ } |
+ |
+ Prefs.addListener(onPrefChange); |
+ } |
+ } |
+} |
+ |
+let addonListener = { |
+ onEnabling: function(addon, needsRestart) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(true); |
+ }, |
+ onDisabled: function(addon) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(false); |
+ }, |
+ onInstalling: function(addon, needsRestart) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(true); |
+ }, |
+ onUninstalled: function(addon) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(false); |
+ } |
+} |
+AddonManager.addAddonListener(addonListener); |
+ |
+checkAddonStatusAndEnable(); |
+*/ |
+ |
+let addonListener = null; |
+function checkAndEnable(isInstalledAndEnabled) |
+{ |
+ if (!Prefs.correctTyposAsked || (Prefs.correctTyposAsked && Prefs.correctTypos)) |
+ { |
+ Cu.reportError("feature enabled"); |
+ Cu.reportError("check addon status and enable"); |
+ |
+ if (typeof isInstalledAndEnabled == "undefined") |
+ { |
+ // must not be executed for addonListener because it needs to run synchronously |
+ AddonManager.getAddonByID(urlfixerID, function(addon) |
+ { |
+ if (addon && !addon.userDisabled) |
+ disableTypoCorrection(); |
+ else |
+ enableTypoCorrection(); |
+ }); |
+ } |
+ else |
+ { |
+ if (isInstalledAndEnabled) |
+ disableTypoCorrection(); |
+ else |
+ enableTypoCorrection(); |
+ } |
+ |
+ if (!addonListener) |
+ { |
+ addonListener = { |
+ onEnabling: function(addon, needsRestart) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(true); |
+ }, |
+ onDisabled: function(addon) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(false); |
+ }, |
+ onInstalling: function(addon, needsRestart) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(true); |
+ }, |
+ onUninstalled: function(addon) |
+ { |
+ if (addon.id == urlfixerID) |
+ checkAndEnable(false); |
+ } |
+ } |
+ AddonManager.addAddonListener(addonListener); |
+ } |
+ } |
+ else if (!isTypoCorrectionEnabled) |
+ { |
+ Cu.reportError("feature disabled"); |
+ function onPrefChange(name) |
+ { |
+ if (name == "correctTypos") |
+ { |
+ checkAndEnable(); |
+ Prefs.removeListener(onPrefChange); |
+ } |
+ } |
+ |
+ Prefs.addListener(onPrefChange); |
+ } |
+} |
+ |
+function checkAddonStatusAndEnable(isInstalledAndEnabled) |
+{ |
+ if (isInstalledAndEnabled) |
+ disableTypoCorrection(); |
+ else |
+ enableTypoCorrection(); |
+} |
+ |
+checkAndEnable(); |