Debian package: Support xulrunner 9+10 in debian/conkeror.bin, drop support for unver...
[conkeror.git] / modules / env.js
blobad3af9b9c98aaf96bdb7e7e49a8c61d8fb97c123
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 in_module(null);
12 /**
13  * get_os returns a string identifying the current OS.
14  * possible values include 'Darwin', 'Linux' and 'WINNT'.
15  */
16 let (xul_runtime = Cc['@mozilla.org/xre/app-info;1']
17          .getService(Ci.nsIXULRuntime)) {
18     function get_os () {
19         return xul_runtime.OS;
20     }
23 const WINDOWS = (get_os() == "WINNT");
24 const POSIX = !WINDOWS;
27 /**
28  * getenv returns the value of a named environment variable or null if
29  * the environment variable does not exist.
30  */
31 let (env = Cc['@mozilla.org/process/environment;1']
32          .getService(Ci.nsIEnvironment)) {
33     function getenv (variable) {
34         if (env.exists(variable))
35             return env.get(variable);
36         return null;
37     }
41 /**
42  * get_home_directory returns an nsILocalFile object of the user's
43  * home directory.
44  */
45 function get_home_directory () {
46     var dir = Cc["@mozilla.org/file/local;1"]
47         .createInstance(Ci.nsILocalFile);
48     if (get_os() == "WINNT") {
49         var home = getenv('HOME') ||
50             getenv('USERPROFILE') ||
51             getenv('HOMEDRIVE') + getenv('HOMEPATH');
52         home = home.replace("/", "\\", "g");
53         dir.initWithPath(home);
54     } else
55         dir.initWithPath(getenv('HOME'));
56     return dir;
60 /* get_current_profile returns the name of the current profile, or
61  * null if that information cannot be found.  The result is cached for
62  * quick repeat lookup.  This is safe because xulrunner does not
63  * support switching profiles on the fly.
64  *
65  * Profiles don't necessarily have a name--as such this information should
66  * not be depended on for anything important.  It is mainly intended for
67  * decoration of the window title and mode-line.
68  */
69 let (profile_name = null) {
70     function get_current_profile () {
71         if (profile_name)
72             return profile_name;
73         if ("@mozilla.org/profile/manager;1" in Cc) {
74             profile_name = Cc["@mozilla.org/profile/manager;1"]
75                 .getService(Ci.nsIProfile)
76                 .currentProfile;
77             return profile_name;
78         }
79         var current_profile_path = Cc["@mozilla.org/file/directory_service;1"]
80             .getService(Ci.nsIProperties)
81             .get("ProfD", Ci.nsIFile).path;
82         var profile_service = Cc["@mozilla.org/toolkit/profile-service;1"]
83             .getService(Components.interfaces.nsIToolkitProfileService);
84         var profiles = profile_service.profiles;
85         while (profiles.hasMoreElements()) {
86             var p = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile);
87             if (current_profile_path == p.localDir.path ||
88                 current_profile_path == p.rootDir.path)
89             {
90                 profile_name = p.name;
91                 return p.name;
92             }
93         }
94         return null;
95     }
100 function get_locale () {
101     const LOCALE_PREF = "general.useragent.locale";
102     return get_localized_pref(LOCALE_PREF) || get_pref(LOCALE_PREF);
106 const PATH = getenv("PATH").split(POSIX ? ":" : ";");
107 const path_component_regexp = POSIX ? /^[^\/]+$/ : /^[^\/\\]+$/;
109 function find_file_in_path (name) {
110     if (name instanceof Ci.nsIFile) {
111         if (name.exists())
112             return name;
113         return null;
114     }
115     var file = Cc["@mozilla.org/file/local;1"]
116         .createInstance(Ci.nsILocalFile);
117     if (! path_component_regexp.test(name)) {
118         // Absolute path
119         try {
120             file.initWithPath(name);
121             if (file.exists())
122                 return file;
123         } catch (e) {}
124         return null;
125     } else {
126         // Relative path
127         for (var i = 0, plen = PATH.length; i < plen; ++i) {
128             try {
129                 file.initWithPath(PATH[i]);
130                 file.appendRelativePath(name);
131                 if (file.exists())
132                     return file;
133             } catch (e) {}
134         }
135     }
136     return null;
139 provide("env");