1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2013 Kris Maglione <maglione.k@gmail.com>
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
10 defineModule("config", {
11 exports: ["ConfigBase", "Config", "config"],
12 require: ["dom", "io", "protocol", "services", "util", "template"]
15 lazyRequire("addons", ["AddonManager"]);
16 lazyRequire("cache", ["cache"]);
17 lazyRequire("highlight", ["highlight"]);
18 lazyRequire("messages", ["_"]);
19 lazyRequire("prefs", ["localPrefs", "prefs"]);
20 lazyRequire("storage", ["storage", "File"]);
21 lazyRequire("styles", ["Styles"]);
23 function AboutHandler() {}
24 AboutHandler.prototype = {
25 get classDescription() "About " + config.appName + " Page",
27 classID: Components.ID("81495d80-89ee-4c36-a88d-ea7c4e5ac63f"),
29 get contractID() services.ABOUT + config.name,
31 QueryInterface: XPCOMUtils.generateQI([Ci.nsIAboutModule]),
33 newChannel: function (uri) {
34 let channel = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService)
35 .newChannel("dactyl://content/about.xul", null, null);
36 channel.originalURI = uri;
40 getURIFlags: function (uri) Ci.nsIAboutModule.ALLOW_SCRIPT
42 var ConfigBase = Class("ConfigBase", {
44 * Called on dactyl startup to allow for any arbitrary application-specific
45 * initialization code. Must call superclass's init function.
47 init: function init() {
50 this.features.push = deprecated("Set.add", function push(feature) Set.add(this, feature));
51 if (this.haveGecko("2b"))
52 Set.add(this.features, "Gecko2");
54 JSMLoader.registerFactory(JSMLoader.Factory(AboutHandler));
55 JSMLoader.registerFactory(JSMLoader.Factory(
56 Protocol("dactyl", "{9c8f2530-51c8-4d41-b356-319e0b155c44}",
57 "resource://dactyl-content/")));
59 this.timeout(function () {
60 cache.register("config.dtd", () => util.makeDTD(config.dtd));
63 services["dactyl:"].pages["dtd"] = () => [null, cache.get("config.dtd")];
65 update(services["dactyl:"].providers, {
66 "locale": function (uri, path) LocaleChannel("dactyl-locale", config.locale, path, uri),
67 "locale-local": function (uri, path) LocaleChannel("dactyl-local-locale", config.locale, path, uri)
71 get prefs() localPrefs,
73 get has() Set.has(this.features),
76 "resource://dactyl-common/config.json",
77 "resource://dactyl-local/config.json"
80 configs: Class.Memoize(function () this.configFiles.map(url => JSON.parse(File.readURL(url)))),
82 loadConfig: function loadConfig(documentURL) {
84 for each (let config in this.configs) {
86 config = config.overlays && config.overlays[documentURL] || {};
88 for (let [name, value] in Iterator(config)) {
89 let prop = util.camelCase(name);
91 if (isArray(this[prop]))
92 this[prop] = [].concat(this[prop], value);
93 else if (isObject(this[prop])) {
97 this[prop] = update({}, this[prop],
98 iter([util.camelCase(k), value[k]]
99 for (k in value)).toObject());
111 ["bookmarkcache", "bookmarkcache"],
152 loadStyles: function loadStyles(force) {
153 highlight.styleableChrome = this.styleableChrome;
155 highlight.loadCSS(this.CSS.replace(/__MSG_(.*?)__/g,
157 highlight.loadCSS(this.helpCSS.replace(/__MSG_(.*?)__/g,
160 if (!this.haveGecko("2b"))
161 highlight.loadCSS(literal(/*
162 !TabNumber font-weight: bold; margin: 0px; padding-right: .8ex;
167 text-shadow: black -1px 0 1px, black 0 1px 1px, black 1px 0 1px, black 0 -1px 1px;
171 let hl = highlight.set("Find", "");
172 hl.onChange = function () {
173 function hex(val) ("#" + util.regexp.iterate(/\d+/g, val)
174 .map(num => ("0" + Number(num).toString(16)).slice(-2))
178 let elem = services.appShell.hiddenDOMWindow.document.createElement("div");
179 elem.style.cssText = this.cssText;
181 let keys = iter(Styles.propertyIter(this.cssText)).map(p => p.name).toArray();
182 let bg = keys.some(bind("test", /^background/));
183 let fg = keys.indexOf("color") >= 0;
185 let style = DOM(elem).style;
186 prefs[bg ? "safeSet" : "safeReset"]("ui.textHighlightBackground", hex(style.backgroundColor));
187 prefs[fg ? "safeSet" : "safeReset"]("ui.textHighlightForeground", hex(style.color));
191 get addonID() this.name + "@dactyl.googlecode.com",
193 addon: Class.Memoize(function () {
194 return (JSMLoader.bootstrap || {}).addon ||
195 AddonManager.getAddonByID(this.addonID);
198 get styleableChrome() Object.keys(this.overlays),
201 * The current application locale.
203 appLocale: Class.Memoize(() => services.chromeRegistry.getSelectedLocale("global")),
206 * The current dactyl locale.
208 locale: Class.Memoize(function () this.bestLocale(this.locales)),
211 * The current application locale.
213 locales: Class.Memoize(function () {
214 // TODO: Merge with completion.file code.
215 function getDir(str) str.match(/^(?:.*[\/\\])?/)[0];
217 let uri = "resource://dactyl-locale/";
218 let jar = io.isJarURL(uri);
220 let prefix = getDir(jar.JAREntry);
221 var res = iter(s.slice(prefix.length).replace(/\/.*/, "")
222 for (s in io.listJar(jar.JARFile, prefix)))
226 res = array(f.leafName
227 // Fails on FF3: for (f in util.getFile(uri).iterDirectory())
228 for (f in values(util.getFile(uri).readDirectory()))
229 if (f.isDirectory())).array;
232 let exists = function exists(pkg) services["resource:"].hasSubstitution("dactyl-locale-" + pkg);
234 return array.uniq([this.appLocale, this.appLocale.replace(/-.*/, "")]
240 * Returns the best locale match to the current locale from a list
241 * of available locales.
243 * @param {[string]} list A list of available locales
246 bestLocale: function (list) {
247 return values([this.appLocale, this.appLocale.replace(/-.*/, ""),
248 "en", "en-US", list[0]])
249 .nth(Set.has(Set(list)), 0);
253 * A list of all known registered chrome and resource packages.
255 get chromePackages() {
258 function process(manifest) {
259 for each (let line in manifest.split(/\n+/)) {
260 let match = /^\s*(content|skin|locale|resource)\s+([^\s#]+)\s/.exec(line);
262 res[match[2]] = true;
265 function processJar(file) {
266 let jar = services.ZipReader(file.file);
269 if (jar.hasEntry("chrome.manifest"))
270 process(File.readStream(jar.getInputStream("chrome.manifest")));
277 for each (let dir in ["UChrm", "AChrom"]) {
278 dir = File(services.directory.get(dir, Ci.nsIFile));
279 if (dir.exists() && dir.isDirectory())
280 for (let file in dir.iterDirectory())
281 if (/\.manifest$/.test(file.leafName))
282 process(file.read());
284 dir = File(dir.parent);
285 if (dir.exists() && dir.isDirectory())
286 for (let file in dir.iterDirectory())
287 if (/\.jar$/.test(file.leafName))
290 dir = dir.child("extensions");
291 if (dir.exists() && dir.isDirectory())
292 for (let ext in dir.iterDirectory()) {
293 if (/\.xpi$/.test(ext.leafName))
297 ext = File(ext.read().replace(/\n*$/, ""));
298 let mf = ext.child("chrome.manifest");
304 return Object.keys(res).sort();
308 * Returns true if the current Gecko runtime is of the given version
311 * @param {string} min The minimum required version. @optional
312 * @param {string} max The maximum required version. @optional
315 haveGecko: function (min, max) let ({ compare } = services.versionCompare,
316 { platformVersion } = services.runtime)
317 (min == null || compare(platformVersion, min) >= 0) &&
318 (max == null || compare(platformVersion, max) < 0),
320 /** Dactyl's notion of the current operating system platform. */
322 _arch: services.runtime.OS,
324 * @property {string} The normalised name of the OS. This is one of
325 * "Windows", "Mac OS X" or "Unix".
327 get name() this.isWindows ? "Windows" : this.isMacOSX ? "Mac OS X" : "Unix",
328 /** @property {boolean} True if the OS is Windows. */
329 get isWindows() this._arch == "WINNT",
330 /** @property {boolean} True if the OS is Mac OS X. */
331 get isMacOSX() this._arch == "Darwin",
332 /** @property {boolean} True if the OS is some other *nix variant. */
333 get isUnix() !this.isWindows,
334 /** @property {RegExp} A RegExp which matches illegal characters in path components. */
335 get illegalCharacters() this.isWindows ? /[<>:"/\\|?*\x00-\x1f]/g : /[\/\x00]/g,
337 get pathListSep() this.isWindows ? ";" : ":"
341 * @property {string} The pathname of the VCS repository clone's root
342 * directory if the application is running from one via an extension
345 VCSPath: Class.Memoize(function () {
346 if (/pre$/.test(this.addon.version)) {
347 let uri = util.newURI(this.addon.getResourceURI("").spec + "../.hg");
348 if (uri instanceof Ci.nsIFileURL &&
351 return uri.file.parent.path;
357 * @property {string} The name of the VCS branch that the application is
358 * running from if using an extension proxy file or was built from if
359 * installed as an XPI.
361 branch: Class.Memoize(function () {
363 return io.system(["hg", "-R", this.VCSPath, "branch"]).output;
364 return (/pre-hg\d+-(\S*)/.exec(this.version) || [])[1];
367 /** @property {string} The name of the current user profile. */
368 profileName: Class.Memoize(function () {
369 // NOTE: services.profile.selectedProfile.name doesn't return
370 // what you might expect. It returns the last _actively_ selected
371 // profile (i.e. via the Profile Manager or -P option) rather than the
372 // current profile. These will differ if the current process was run
373 // without explicitly selecting a profile.
375 let dir = services.directory.get("ProfD", Ci.nsIFile);
376 for (let prof in iter(services.profile.profiles))
377 if (prof.QueryInterface(Ci.nsIToolkitProfile).rootDir.path === dir.path)
382 /** @property {string} The Dactyl version string. */
383 version: Class.Memoize(function () {
385 return io.system(["hg", "-R", this.VCSPath, "log", "-r.",
386 "--template=hg{rev}-{branch}"]).output;
388 return this.addon.version;
391 buildDate: Class.Memoize(function () {
393 return io.system(["hg", "-R", this.VCSPath, "log", "-r.",
394 "--template={date|isodate}"]).output;
395 if ("@DATE@" !== "@" + "DATE@")
396 return _("dactyl.created", "@DATE@");
399 get fileExt() this.name.slice(0, -6),
401 dtd: Class.Memoize(function ()
403 (["dactyl." + k, v] for ([k, v] in iter(config.dtdDactyl))),
404 (["dactyl." + s, config[s]] for each (s in config.dtdStrings)))
408 get name() config.name,
409 get home() "http://5digits.org/",
410 get apphome() this.home + this.name,
411 code: "http://code.google.com/p/dactyl/",
412 get issues() this.home + "bug/" + this.name,
413 get plugins() "http://5digits.org/" + this.name + "/plugins",
414 get faq() this.home + this.name + "/faq",
416 "list.mailto": Class.Memoize(() => config.name + "@googlegroups.com"),
417 "list.href": Class.Memoize(() => "http://groups.google.com/group/" + config.name),
419 "hg.latest": Class.Memoize(function () this.code + "source/browse/"), // XXX
420 "irc": "irc://irc.oftc.net/#pentadactyl"
424 "xmlns.dactyl": "http://vimperator.org/namespaces/liberator",
425 "xmlns.html": "http://www.w3.org/1999/xhtml",
426 "xmlns.xul": "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
428 "tag.command-line": ["link", { xmlns: "dactyl", topic: "command-line" }, "command line"],
429 "tag.status-line": ["link", { xmlns: "dactyl", topic: "status-line" }, "status line"],
430 "mode.command-line": ["link", { xmlns: "dactyl", topic: "command-line-mode" }, "Command Line"]
443 helpStyles: /^(Help|StatusLine|REPL)|^(Boolean|Dense|Indicator|MoreMsg|Number|Object|Logo|Key(word)?|String)$/,
444 styleHelp: function styleHelp() {
445 if (!this.helpStyled) {
446 for (let k in keys(highlight.loaded))
447 if (this.helpStyles.test(k))
448 highlight.loaded[k] = true;
453 Local: function Local(dactyl, modules, { document, window }) ({
454 init: function init() {
455 this.loadConfig(document.documentURI);
458 ["menupopup", { id: "viewSidebarMenu", xmlns: "xul" }],
459 ["broadcasterset", { id: "mainBroadcasterSet", xmlns: "xul" }]];
461 for each (let [id, [name, key, uri]] in Iterator(this.sidebars)) {
463 ["menuitem", { observes: "pentadactyl-" + id + "Sidebar", label: name,
466 ["broadcaster", { id: "pentadactyl-" + id + "Sidebar", autoCheck: "false",
467 type: "checkbox", group: "sidebar", sidebartitle: name,
469 oncommand: "toggleSidebar(this.id || this.observes);" }]);
472 util.overlayWindow(window, { append: append });
477 get document() document,
480 get commandContainer() document.documentElement.id
483 browser: Class.Memoize(() => window.gBrowser),
484 tabbrowser: Class.Memoize(() => window.gBrowser),
486 get browserModes() [modules.modes.NORMAL],
489 * @property {string} The ID of the application's main XUL window.
491 mainWindowId: document.documentElement.id,
494 * @property {number} The height (px) that is available to the output
497 get outputHeight() this.browser.mPanelContainer.boxObject.height,
499 tabStrip: Class.Memoize(function () document.getElementById("TabsToolbar") || this.tabbrowser.mTabContainer)
503 * @property {Object} A mapping of names and descriptions
504 * of the autocommands available in this application. Primarily used
505 * for completion results.
510 * @property {Object} A map of :command-complete option values to completer
516 * @property {Object} Application specific defaults for option values. The
517 * property names must be the options' canonical names, and the values
518 * must be strings as entered via :set.
525 * @property {Object} A map of dialogs available via the
526 * :dialog command. Property names map dialog names to an array
527 * with the following elements:
528 * [0] description - A description of the dialog, used in
529 * command completion results for :dialog.
530 * [1] action - The function executed by :dialog.
531 * [2] test - Function which returns true if the dialog is available in
532 * the current window. @optional
537 * @property {set} A list of features available in this
538 * application. Used extensively in feature test macros. Use
539 * dactyl.has(feature) to check for a feature's presence
545 * @property {string} The file extension used for command script files.
546 * This is the name string sans "dactyl".
548 get fileExtension() this.name.slice(0, -6),
553 * @property {string} The name of the application that hosts the
554 * extension. E.g., "Firefox" or "XULRunner".
559 * @property {string} The name of the extension.
565 * @property {[string]} A list of extra scripts in the dactyl or
566 * application namespaces which should be loaded before dactyl
575 * @property {string} The default highlighting rules.
576 * See {@link Highlights#loadCSS} for details.
578 CSS: Class.Memoize(() => File.readURL("resource://dactyl-skin/global-styles.css")),
580 helpCSS: Class.Memoize(() => File.readURL("resource://dactyl-skin/help-styles.css"))
583 JSMLoader.loadSubScript("resource://dactyl-local-content/config.js", this);
585 config.INIT = update(Object.create(config.INIT), config.INIT, {
586 init: function init(dactyl, modules, window) {
587 init.superapply(this, arguments);
589 let img = window.Image();
590 img.src = this.logo || "resource://dactyl-local-content/logo.png";
591 img.onload = util.wrapCallback(function () {
592 highlight.loadCSS(literal(/*
594 display: inline-block;
595 background: url({src});
599 */).replace(/\{(.*?)\}/g, (m, m1) => img[m1]));
604 load: function load(dactyl, modules, window) {
605 load.superapply(this, arguments);
607 this.timeout(function () {
608 if (this.branch && this.branch !== "default" &&
609 modules.yes_i_know_i_should_not_report_errors_in_these_branches_thanks.indexOf(this.branch) === -1)
610 dactyl.warn(_("warn.notDefaultBranch", config.appName, this.branch));
617 // catch(e){ if (typeof e === "string") e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
619 // vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: