isearch-backspace, isearch-done: docstrings
[conkeror.git] / modules / env.js
blob073ca7ce3616f3ed2474b2ba6856649b1ae3401c
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  * getenv returns the value of a named environment variable or null if
27  * the environment variable does not exist.
28  */
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);
34         return null;
35     }
39 /**
40  * get_home_directory returns an nsILocalFile object of the user's
41  * home directory.
42  */
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);
52     } else
53         dir.initWithPath(getenv('HOME'));
54     return dir;
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.
62  *
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.
66  */
67 let (profile_name = null) {
68     function get_current_profile () {
69         if (profile_name)
70             return profile_name;
71         if ("@mozilla.org/profile/manager;1" in Cc) {
72             profile_name = Cc["@mozilla.org/profile/manager;1"]
73                 .getService(Ci.nsIProfile)
74                 .currentProfile;
75             return profile_name;
76         }
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)
87             {
88                 profile_name = p.name;
89                 return p.name;
90             }
91         }
92         return null;
93     }
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) {
109         if (name.exists())
110             return name;
111         return null;
112     }
113     var file = Cc["@mozilla.org/file/local;1"]
114         .createInstance(Ci.nsILocalFile);
115     if (! path_component_regexp.test(name)) {
116         // Absolute path
117         try {
118             file.initWithPath(name);
119             if (file.exists())
120                 return file;
121         } catch (e) {}
122         return null;
123     } else {
124         // Relative path
125         for (var i = 0, plen = PATH.length; i < plen; ++i) {
126             try {
127                 file.initWithPath(PATH[i]);
128                 file.appendRelativePath(name);
129                 if (file.exists())
130                     return file;
131             } catch (e) {}
132         }
133     }
134     return null;
137 provide("env");