From 4ba76a0da33d765025c7af3c462db2b161dde98a Mon Sep 17 00:00:00 2001 From: John Foerch Date: Sun, 7 Feb 2010 22:52:05 -0500 Subject: [PATCH] move some things out of utils.js into other files doing a bit of reorganization on the dumping ground that utils.js is. created env.js, pref.js, services.js, and string.js --- modules/conkeror.js | 5 + modules/env.js | 88 ++++++++++++++++++ modules/history.js | 4 - modules/pref.js | 98 ++++++++++++++++++++ modules/search-engine.js | 4 +- modules/services.js | 26 ++++++ modules/spawn-process.js | 2 +- modules/string.js | 76 +++++++++++++++ modules/utils.js | 237 +---------------------------------------------- 9 files changed, 299 insertions(+), 241 deletions(-) create mode 100644 modules/env.js create mode 100644 modules/pref.js create mode 100644 modules/services.js create mode 100644 modules/string.js diff --git a/modules/conkeror.js b/modules/conkeror.js index cd4e55c..b5f429a 100644 --- a/modules/conkeror.js +++ b/modules/conkeror.js @@ -15,6 +15,11 @@ require("debug.js"); require("hook.js"); require("timer.js"); require("pretty-print.js"); +require("services.js"); + +require("string.js"); +require("pref.js"); +require("env.js"); require("utils.js"); require("interactive.js"); diff --git a/modules/env.js b/modules/env.js new file mode 100644 index 0000000..1b6e4e7 --- /dev/null +++ b/modules/env.js @@ -0,0 +1,88 @@ +/** + * (C) Copyright 2004-2007 Shawn Betts + * (C) Copyright 2007-2009 John J. Foerch + * (C) Copyright 2007-2008 Jeremy Maitin-Shepard + * + * Use, modification, and distribution are subject to the terms specified in the + * COPYING file. +**/ + +/** + * get_os returns a string identifying the current OS. + * possible values include 'Darwin', 'Linux' and 'WINNT'. + */ +let (xul_runtime = Cc['@mozilla.org/xre/app-info;1'] + .getService(Ci.nsIXULRuntime)) { + function get_os () { + return xul_runtime.OS; + } +}; + + +/** + * getenv returns the value of a named environment variable or null if + * the environment variable does not exist. + */ +let (env = Cc['@mozilla.org/process/environment;1'] + .getService(Ci.nsIEnvironment)) { + function getenv (variable) { + if (env.exists(variable)) + return env.get(variable); + return null; + } +} + + +/** + * get_home_directory returns an nsILocalFile object of the user's + * home directory. + */ +function get_home_directory () { + var dir = Cc["@mozilla.org/file/local;1"] + .createInstance(Ci.nsILocalFile); + if (get_os() == "WINNT") + dir.initWithPath(getenv('USERPROFILE') || + getenv('HOMEDRIVE') + getenv('HOMEPATH')); + else + dir.initWithPath(getenv('HOME')); + return dir; +} + + +/* get_current_profile returns the name of the current profile, or + * null if that information cannot be found. The result is cached for + * quick repeat lookup. This is safe because xulrunner does not + * support switching profiles on the fly. + * + * Profiles don't necessarily have a name--as such this information should + * not be depended on for anything important. It is mainly intended for + * decoration of the window title and mode-line. + */ +let (profile_name = null) { + function get_current_profile () { + if (profile_name) + return profile_name; + if ("@mozilla.org/profile/manager;1" in Cc) { + profile_name = Cc["@mozilla.org/profile/manager;1"] + .getService(Ci.nsIProfile) + .currentProfile; + return profile_name; + } + var current_profile_path = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIProperties) + .get("ProfD", Ci.nsIFile).path; + var profile_service = Cc["@mozilla.org/toolkit/profile-service;1"] + .getService(Components.interfaces.nsIToolkitProfileService); + var profiles = profile_service.profiles; + while (profiles.hasMoreElements()) { + var p = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile); + if (current_profile_path == p.localDir.path || + current_profile_path == p.rootDir.path) + { + profile_name = p.name; + return p.name; + } + } + return null; + } +} diff --git a/modules/history.js b/modules/history.js index 8eb8686..2d43c88 100644 --- a/modules/history.js +++ b/modules/history.js @@ -6,9 +6,6 @@ * COPYING file. **/ -const nav_history_service = Cc["@mozilla.org/browser/nav-history-service;1"] - .getService(Ci.nsINavHistoryService); - define_keywords("$use_webjumps", "$use_history", "$use_bookmarks", "$match_required"); function history_completer () { @@ -61,7 +58,6 @@ function url_completer() { return merge_completers(completers); } -const nav_bookmarks_service = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].getService(Ci.nsINavBookmarksService); function add_bookmark(url, title) { nav_bookmarks_service.insertBookmark(nav_bookmarks_service.unfiledBookmarksFolder, diff --git a/modules/pref.js b/modules/pref.js new file mode 100644 index 0000000..79133dd --- /dev/null +++ b/modules/pref.js @@ -0,0 +1,98 @@ +/** + * (C) Copyright 2004-2007 Shawn Betts + * (C) Copyright 2007-2009 John J. Foerch + * (C) Copyright 2007-2008 Jeremy Maitin-Shepard + * + * Use, modification, and distribution are subject to the terms specified in the + * COPYING file. +**/ + +function set_branch_pref (branch, name, value) { + if (typeof(value) == "string") { + branch.setCharPref(name, value); + } else if (typeof(value) == "number") { + branch.setIntPref(name, value); + } else if (typeof(value) == "boolean") { + branch.setBoolPref(name, value); + } +} + +function default_pref (name, value) { + var branch = preferences.getDefaultBranch(null); + set_branch_pref(branch, name, value); +} + +function user_pref (name, value) { + var branch = preferences.getBranch(null); + set_branch_pref(branch, name, value); +} + +function get_branch_pref (branch, name) { + switch (branch.getPrefType(name)) { + case branch.PREF_STRING: + return branch.getCharPref(name); + case branch.PREF_INT: + return branch.getIntPref(name); + case branch.PREF_BOOL: + return branch.getBoolPref(name); + default: + return null; + } +} + +function get_localized_pref (name) { + try { + return preferences.getBranch(null).getComplexValue(name, Ci.nsIPrefLocalizedString).data; + } catch (e) { + return null; + } +} + +function get_pref (name) { + var branch = preferences.getBranch(null); + return get_branch_pref(branch, name); +} + +function get_default_pref (name) { + var branch = preferences.getDefaultBranch(null); + return get_branch_pref(branch, name); +} + +function clear_pref (name) { + var branch = preferences.getBranch(null); + return branch.clearUserPref(name); +} + +function pref_has_user_value (name) { + var branch = preferences.getBranch(null); + return branch.prefHasUserValue(name); +} + +function pref_has_default_value (name) { + var branch = preferences.getDefaultBranch(null); + return branch.prefHasUserValue(name); +} + +function session_pref (name, value) { + try { + clear_pref (name); + } catch (e) {} + return default_pref(name, value); +} + +function watch_pref (pref, hook) { + /* Extract pref into branch.pref */ + let match = pref.match(/^(.*[.])?([^.]*)$/); + let br = match[1]; + let key = match[2]; + let branch = preferences.getBranch(br).QueryInterface(Ci.nsIPrefBranch2); + let observer = { + observe: function (subject, topic, data) { + if (topic == "nsPref:changed" && data == key) { + hook(); + } + } + }; + branch.addObserver("", observer, false); +} + diff --git a/modules/search-engine.js b/modules/search-engine.js index d1c4d77..f5f7328 100644 --- a/modules/search-engine.js +++ b/modules/search-engine.js @@ -310,12 +310,12 @@ function search_engine_get_homepage(search_engine) { // Load search engines from default directories { - let dir = file_locator.get("CurProcD", Ci.nsIFile); + let dir = file_locator_service.get("CurProcD", Ci.nsIFile); dir.append("search-engines"); if (dir.exists() && dir.isDirectory()) load_search_engines_in_directory(dir); - dir = file_locator.get("ProfD", Ci.nsIFile); + dir = file_locator_service.get("ProfD", Ci.nsIFile); dir.append("search-engines"); if (dir.exists() && dir.isDirectory()) load_search_engines_in_directory(dir); diff --git a/modules/services.js b/modules/services.js new file mode 100644 index 0000000..dd9c86b --- /dev/null +++ b/modules/services.js @@ -0,0 +1,26 @@ +/** + * (C) Copyright 2010 John J. Foerch + * + * Use, modification, and distribution are subject to the terms specified in the + * COPYING file. +**/ + +/** + * Be sparing about adding service variables to this file. Only add + * services for which it makes sense to keep a global reference because + * they are used in numerous separate places in Conkeror. In most cases, + * a simple `let' form around the function or two that use a service is + * best, because it keeps all the code in one place. + */ + +const file_locator_service = Cc["@mozilla.org/file/directory_service;1"] + .getService(Ci.nsIProperties); + +const nav_bookmarks_service = Cc["@mozilla.org/browser/nav-bookmarks-service;1"] + .getService(Ci.nsINavBookmarksService); + +const nav_history_service = Cc["@mozilla.org/browser/nav-history-service;1"] + .getService(Ci.nsINavHistoryService); + +const observer_service = Cc["@mozilla.org/observer-service;1"] + .getService(Ci.nsIObserverService); diff --git a/modules/spawn-process.js b/modules/spawn-process.js index f80294c..8862cd1 100644 --- a/modules/spawn-process.js +++ b/modules/spawn-process.js @@ -105,7 +105,7 @@ const STDERR_FILENO = 2; var spawn_process_helper_default_fd_wait_timeout = 1000; var spawn_process_helper_setup_timeout = 2000; -var spawn_process_helper_program = file_locator.get("CurProcD", Ci.nsIFile); +var spawn_process_helper_program = file_locator_service.get("CurProcD", Ci.nsIFile); spawn_process_helper_program.append("conkeror-spawn-helper"); /** diff --git a/modules/string.js b/modules/string.js new file mode 100644 index 0000000..c060809 --- /dev/null +++ b/modules/string.js @@ -0,0 +1,76 @@ +/** + * (C) Copyright 2004-2007 Shawn Betts + * (C) Copyright 2007-2009 John J. Foerch + * (C) Copyright 2007-2008 Jeremy Maitin-Shepard + * + * Use, modification, and distribution are subject to the terms specified in the + * COPYING file. +**/ + +/** + * trim_whitespace removes whitespace from the beginning and end of the + * given string. + */ +function trim_whitespace (str) { + var tmp = new String(str); + return tmp.replace(/^\s+/, "").replace(/\s+$/, ""); +} + + +function shell_quote (str) { + var s = str.replace("\"", "\\\"", "g"); + s = s.replace("$", "\$", "g"); + return s; +} + +/* Like perl's quotemeta. Backslash all non-alphanumerics. */ +function quotemeta (str) { + return str.replace(/([^a-zA-Z0-9])/g, "\\$1"); +} + +/* Given a list of choices (strings), return a regex which matches any + of them*/ +function choice_regex (choices) { + var regex = "(?:" + choices.map(quotemeta).join("|") + ")"; + return regex; +} + + +/** + * get_shortdoc_string, given a docstring, returns the portion of the + * docstring up to the first newline, or the whole docstring. + */ +function get_shortdoc_string (doc) { + var shortdoc = null; + if (doc != null) { + var idx = doc.indexOf("\n"); + if (idx >= 0) + shortdoc = doc.substring(0,idx); + else + shortdoc = doc; + } + return shortdoc; +} + + +/** + * string_format takes a format-string containing %X style format codes, + * and an object mapping the code-letters to replacement text. It + * returns a string with the formatting codes replaced by the replacement + * text. + */ +function string_format (spec, substitutions) { + return spec.replace(/%(.)/g, function (a, b) substitutions[b]); +} + + +/** + * html_escape replaces characters which are special in html with character + * entities, safe for inserting as text into an html document. + */ +function html_escape (str) { + return str.replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); +} diff --git a/modules/utils.js b/modules/utils.js index 2f3c980..6e38432 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -173,11 +173,6 @@ function do_repeatedly (func, n, positive_args, negative_args) { while (n-- > 0) func.apply(null, positive_args); } -// remove whitespace from the beginning and end -function trim_whitespace (str) { - var tmp = new String(str); - return tmp.replace(/^\s+/, "").replace(/\s+$/, ""); -} /** * Given a node, returns its position relative to the document. @@ -218,48 +213,6 @@ function abs_point (node) { } -/** - * get_os returns a string identifying the current OS. - * possible values include 'Darwin', 'Linux' and 'WINNT'. - */ -let (xul_runtime = Cc['@mozilla.org/xre/app-info;1'] - .getService(Ci.nsIXULRuntime)) { - function get_os () { - return xul_runtime.OS; - } -}; - - -/** - * getenv returns the value of a named environment variable or null if - * the environment variable does not exist. - */ -let (env = Cc['@mozilla.org/process/environment;1'] - .getService(Ci.nsIEnvironment)) { - function getenv (variable) { - if (env.exists(variable)) - return env.get(variable); - return null; - } -} - - -/** - * get_home_directory returns an nsILocalFile object of the user's - * home directory. - */ -function get_home_directory () { - var dir = Cc["@mozilla.org/file/local;1"] - .createInstance(Ci.nsILocalFile); - if (get_os() == "WINNT") - dir.initWithPath(getenv('USERPROFILE') || - getenv('HOMEDRIVE') + getenv('HOMEPATH')); - else - dir.initWithPath(getenv('HOME')); - return dir; -} - - const XHTML_NS = "http://www.w3.org/1999/xhtml"; const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; const MATHML_NS = "http://www.w3.org/1998/Math/MathML"; @@ -285,23 +238,6 @@ function method_caller (obj, func) { }; } -function shell_quote (str) { - var s = str.replace("\"", "\\\"", "g"); - s = s.replace("$", "\$", "g"); - return s; -} - -/* Like perl's quotemeta. Backslash all non-alphanumerics. */ -function quotemeta (str) { - return str.replace(/([^a-zA-Z0-9])/g, "\\$1"); -} - -/* Given a list of choices (strings), return a regex which matches any - of them*/ -function choice_regex (choices) { - var regex = "(?:" + choices.map(quotemeta).join("|") + ")"; - return regex; -} function get_window_from_frame (frame) { try { @@ -330,20 +266,6 @@ function get_buffer_from_frame (window, frame) { return null; } -var file_locator = Cc["@mozilla.org/file/directory_service;1"] - .getService(Ci.nsIProperties); - -function get_shortdoc_string (doc) { - var shortdoc = null; - if (doc != null) { - var idx = doc.indexOf("\n"); - if (idx >= 0) - shortdoc = doc.substring(0,idx); - else - shortdoc = doc; - } - return shortdoc; -} var conkeror_source_code_path = null; @@ -468,95 +390,6 @@ function generate_QI () { return new Function("iid", fstr); } -function set_branch_pref (branch, name, value) { - if (typeof(value) == "string") { - branch.setCharPref(name, value); - } else if (typeof(value) == "number") { - branch.setIntPref(name, value); - } else if (typeof(value) == "boolean") { - branch.setBoolPref(name, value); - } -} - -function default_pref (name, value) { - var branch = preferences.getDefaultBranch(null); - set_branch_pref(branch, name, value); -} - -function user_pref (name, value) { - var branch = preferences.getBranch(null); - set_branch_pref(branch, name, value); -} - -function get_branch_pref (branch, name) { - switch (branch.getPrefType(name)) { - case branch.PREF_STRING: - return branch.getCharPref(name); - case branch.PREF_INT: - return branch.getIntPref(name); - case branch.PREF_BOOL: - return branch.getBoolPref(name); - default: - return null; - } -} - -function get_localized_pref (name) { - try { - return preferences.getBranch(null).getComplexValue(name, Ci.nsIPrefLocalizedString).data; - } catch (e) { - return null; - } -} - -function get_pref (name) { - var branch = preferences.getBranch(null); - return get_branch_pref(branch, name); -} - -function get_default_pref (name) { - var branch = preferences.getDefaultBranch(null); - return get_branch_pref(branch, name); -} - -function clear_pref (name) { - var branch = preferences.getBranch(null); - return branch.clearUserPref(name); -} - -function pref_has_user_value (name) { - var branch = preferences.getBranch(null); - return branch.prefHasUserValue(name); -} - -function pref_has_default_value (name) { - var branch = preferences.getDefaultBranch(null); - return branch.prefHasUserValue(name); -} - -function session_pref (name, value) { - try { - clear_pref (name); - } catch (e) {} - return default_pref(name, value); -} - -function watch_pref (pref, hook) { - /* Extract pref into branch.pref */ - let match = pref.match(/^(.*[.])?([^.]*)$/); - let br = match[1]; - let key = match[2]; - let branch = preferences.getBranch(br).QueryInterface(Ci.nsIPrefBranch2); - let observer = { - observe: function (subject, topic, data) { - if (topic == "nsPref:changed" && data == key) { - hook(); - } - } - }; - branch.addObserver("", observer, false); -} - const LOCALE_PREF = "general.useragent.locale"; function get_locale () { @@ -781,8 +614,6 @@ function define_builtin_commands (prefix, do_command_function, toggle_mark, mark } } -var observer_service = Cc["@mozilla.org/observer-service;1"] - .getService(Ci.nsIObserverService); function abort (str) { var e = new Error(str); @@ -795,7 +626,7 @@ abort.prototype.__proto__ = Error.prototype; function get_temporary_file (name) { if (name == null) name = "temp.txt"; - var file = file_locator.get("TmpD", Ci.nsIFile); + var file = file_locator_service.get("TmpD", Ci.nsIFile); file.append(name); // Create the file now to ensure that no exploits are possible file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); @@ -976,8 +807,6 @@ function get_meta_title (doc) { return null; } -var rdf_service = Cc["@mozilla.org/rdf/rdf-service;1"] - .getService(Ci.nsIRDFService); const PREFIX_ITEM_URI = "urn:mozilla:item:"; const PREFIX_NS_EM = "http://www.mozilla.org/2004/em-rdf#"; @@ -986,6 +815,8 @@ var extension_manager = Cc["@mozilla.org/extensions/manager;1"] .getService(Ci.nsIExtensionManager); function get_extension_rdf_property (id, name, type) { + const rdf_service = Cc["@mozilla.org/rdf/rdf-service;1"] + .getService(Ci.nsIRDFService); var value = extension_manager.datasource.GetTarget( rdf_service.GetResource(PREFIX_ITEM_URI + id), rdf_service.GetResource(PREFIX_NS_EM + name), @@ -1360,45 +1191,6 @@ function remove_duplicates_filter () { } -/* get_current_profile returns the name of the current profile, or - * null if that information cannot be found. The result is cached for - * quick repeat lookup. This is safe because xulrunner does not - * support switching profiles on the fly. - * - * Profiles don't necessarily have a name--as such this information should - * not be depended on for anything important. It is mainly intended for - * decoration of the window title and mode-line. - */ -let (profile_name = null) { - function get_current_profile () { - if (profile_name) - return profile_name; - if ("@mozilla.org/profile/manager;1" in Cc) { - profile_name = Cc["@mozilla.org/profile/manager;1"] - .getService(Ci.nsIProfile) - .currentProfile; - return profile_name; - } - var current_profile_path = Cc["@mozilla.org/file/directory_service;1"] - .getService(Ci.nsIProperties) - .get("ProfD", Ci.nsIFile).path; - var profile_service = Cc["@mozilla.org/toolkit/profile-service;1"] - .getService(Components.interfaces.nsIToolkitProfileService); - var profiles = profile_service.profiles; - while (profiles.hasMoreElements()) { - var p = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile); - if (current_profile_path == p.localDir.path || - current_profile_path == p.rootDir.path) - { - profile_name = p.name; - return p.name; - } - } - return null; - } -}; - - /** * Given an array, switches places on the subarrays at index i1 to i2 and j1 to * j2. Leaves the rest of the array unchanged. @@ -1452,17 +1244,6 @@ function get_contents_synchronously (url) { /** - * string_format takes a format-string containing %X style format codes, - * and an object mapping the code-letters to replacement text. It - * returns a string with the formatting codes replaced by the replacement - * text. - */ -function string_format (spec, substitutions) { - return spec.replace(/%(.)/g, function (a,b) { return substitutions[b]; }); -} - - -/** * dom_add_class adds a css class to the given dom node. */ function dom_add_class (node, cssclass) { @@ -1600,15 +1381,3 @@ function do_when (hook, buffer, fun) { else fun(buffer); } - - -/** - * html_escape replaces characters which are special in html with character - * entities, safe for inserting as text into an html document. - */ -function html_escape (str) { - return str.replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"'); -} -- 2.11.4.GIT