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