2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2009 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
11 * get_os returns a string identifying the current OS.
12 * possible values include 'Darwin', 'Linux' and 'WINNT'.
14 let (xul_runtime = Cc['@mozilla.org/xre/app-info;1']
15 .getService(Ci.nsIXULRuntime)) {
17 return xul_runtime.OS;
21 const WINDOWS = (get_os() == "WINNT");
22 const POSIX = !WINDOWS;
26 * getenv returns the value of a named environment variable or null if
27 * the environment variable does not exist.
29 let (env = Cc['@mozilla.org/process/environment;1']
30 .getService(Ci.nsIEnvironment)) {
31 function getenv (variable) {
32 if (env.exists(variable))
33 return env.get(variable);
40 * get_home_directory returns an nsILocalFile object of the user's
43 function get_home_directory () {
44 var dir = Cc["@mozilla.org/file/local;1"]
45 .createInstance(Ci.nsILocalFile);
46 if (get_os() == "WINNT") {
47 var home = getenv('HOME') ||
48 getenv('USERPROFILE') ||
49 getenv('HOMEDRIVE') + getenv('HOMEPATH');
50 home = home.replace("/", "\\", "g");
51 dir.initWithPath(home);
53 dir.initWithPath(getenv('HOME'));
58 /* get_current_profile returns the name of the current profile, or
59 * null if that information cannot be found. The result is cached for
60 * quick repeat lookup. This is safe because xulrunner does not
61 * support switching profiles on the fly.
63 * Profiles don't necessarily have a name--as such this information should
64 * not be depended on for anything important. It is mainly intended for
65 * decoration of the window title and mode-line.
67 let (profile_name = null) {
68 function get_current_profile () {
71 if ("@mozilla.org/profile/manager;1" in Cc) {
72 profile_name = Cc["@mozilla.org/profile/manager;1"]
73 .getService(Ci.nsIProfile)
77 var current_profile_path = Cc["@mozilla.org/file/directory_service;1"]
78 .getService(Ci.nsIProperties)
79 .get("ProfD", Ci.nsIFile).path;
80 var profile_service = Cc["@mozilla.org/toolkit/profile-service;1"]
81 .getService(Components.interfaces.nsIToolkitProfileService);
82 var profiles = profile_service.profiles;
83 while (profiles.hasMoreElements()) {
84 var p = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile);
85 if (current_profile_path == p.localDir.path ||
86 current_profile_path == p.rootDir.path)
88 profile_name = p.name;
98 function get_locale () {
99 const LOCALE_PREF = "general.useragent.locale";
100 return get_localized_pref(LOCALE_PREF) || get_pref(LOCALE_PREF);
104 const PATH = getenv("PATH").split(POSIX ? ":" : ";");
105 const path_component_regexp = POSIX ? /^[^\/]+$/ : /^[^\/\\]+$/;
107 function find_file_in_path (name) {
108 if (name instanceof Ci.nsIFile) {
113 var file = Cc["@mozilla.org/file/local;1"]
114 .createInstance(Ci.nsILocalFile);
115 if (! path_component_regexp.test(name)) {
118 file.initWithPath(name);
125 for (var i = 0, plen = PATH.length; i < plen; ++i) {
127 file.initWithPath(PATH[i]);
128 file.appendRelativePath(name);