components, modules: change deprecated replace flag
[conkeror.git] / modules / env.js
blob5672fe235baefc09af1d58f8a01166527a66bcfc
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 /**
11  * get_os returns a string identifying the current OS.
12  * possible values include 'Darwin', 'Linux' and 'WINNT'.
13  */
15     let xul_runtime = Cc['@mozilla.org/xre/app-info;1']
16         .getService(Ci.nsIXULRuntime);
17     var get_os = function get_os () {
18         return xul_runtime.OS;
19     }
22 const WINDOWS = (get_os() == "WINNT");
23 const POSIX = !WINDOWS;
26 /**
27  * get_xulrunner_version returns the version string of the running
28  * platform.
29  */
30 function get_mozilla_version () {
31     return Cc['@mozilla.org/xre/app-info;1']
32         .getService(Ci.nsIXULAppInfo)
33         .platformVersion;
37 /**
38  * getenv returns the value of a named environment variable or null if
39  * the environment variable does not exist.
40  */
42     let env = Cc['@mozilla.org/process/environment;1']
43         .getService(Ci.nsIEnvironment);
44     var getenv = function getenv (variable) {
45         if (env.exists(variable))
46             return env.get(variable);
47         return null;
48     }
52 /**
53  * get_home_directory returns an nsILocalFile object of the user's
54  * home directory.
55  */
56 function get_home_directory () {
57     var dir = Cc["@mozilla.org/file/local;1"]
58         .createInstance(Ci.nsILocalFile);
59     if (get_os() == "WINNT") {
60         var home = getenv('HOME') ||
61             getenv('USERPROFILE') ||
62             getenv('HOMEDRIVE') + getenv('HOMEPATH');
63         home = home.replace(/\//g, "\\");
64         dir.initWithPath(home);
65     } else {
66         dir.initWithPath(getenv('HOME'));
67     }
68     return dir;
72 /* get_current_profile returns the name of the current profile, or
73  * null if that information cannot be found.  The result is cached for
74  * quick repeat lookup.  This is safe because xulrunner does not
75  * support switching profiles on the fly.
76  *
77  * Profiles don't necessarily have a name--as such this information should
78  * not be depended on for anything important.  It is mainly intended for
79  * decoration of the window title and mode-line.
80  */
82     let profile_name = null;
83     var get_current_profile = function get_current_profile () {
84         if (profile_name)
85             return profile_name;
86         if ("@mozilla.org/profile/manager;1" in Cc) {
87             profile_name = Cc["@mozilla.org/profile/manager;1"]
88                 .getService(Ci.nsIProfile)
89                 .currentProfile;
90             return profile_name;
91         }
92         var current_profile_path = Cc["@mozilla.org/file/directory_service;1"]
93             .getService(Ci.nsIProperties)
94             .get("ProfD", Ci.nsIFile).path;
95         var profile_service = Cc["@mozilla.org/toolkit/profile-service;1"]
96             .getService(Components.interfaces.nsIToolkitProfileService);
97         var profiles = profile_service.profiles;
98         while (profiles.hasMoreElements()) {
99             var p = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile);
100             if (current_profile_path == p.localDir.path ||
101                 current_profile_path == p.rootDir.path)
102             {
103                 profile_name = p.name;
104                 return p.name;
105             }
106         }
107         return null;
108     }
113 function get_locale () {
114     const LOCALE_PREF = "general.useragent.locale";
115     return get_localized_pref(LOCALE_PREF) || get_pref(LOCALE_PREF);
119 const PATH = getenv("PATH").split(POSIX ? ":" : ";");
120 const path_component_regexp = POSIX ? /^[^\/]+$/ : /^[^\/\\]+$/;
122 function find_file_in_path (name) {
123     if (name instanceof Ci.nsIFile) {
124         if (name.exists())
125             return name;
126         return null;
127     }
128     var file = Cc["@mozilla.org/file/local;1"]
129         .createInstance(Ci.nsILocalFile);
130     if (! path_component_regexp.test(name)) {
131         // Absolute path
132         try {
133             file.initWithPath(name);
134             if (file.exists())
135                 return file;
136         } catch (e) {}
137         return null;
138     } else {
139         // Relative path
140         for (var i = 0, plen = PATH.length; i < plen; ++i) {
141             try {
142                 file.initWithPath(PATH[i]);
143                 file.appendRelativePath(name);
144                 if (file.exists())
145                     return file;
146             } catch (e) {}
147         }
148     }
149     return null;
152 provide("env");