move some things out of utils.js into other files
[conkeror.git] / modules / env.js
blob1b6e4e7446fa0e45fa1d3fa391508a430917da89
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     }
22 /**
23  * getenv returns the value of a named environment variable or null if
24  * the environment variable does not exist.
25  */
26 let (env = Cc['@mozilla.org/process/environment;1']
27          .getService(Ci.nsIEnvironment)) {
28     function getenv (variable) {
29         if (env.exists(variable))
30             return env.get(variable);
31         return null;
32     }
36 /**
37  * get_home_directory returns an nsILocalFile object of the user's
38  * home directory.
39  */
40 function get_home_directory () {
41     var dir = Cc["@mozilla.org/file/local;1"]
42         .createInstance(Ci.nsILocalFile);
43     if (get_os() == "WINNT")
44         dir.initWithPath(getenv('USERPROFILE') ||
45                          getenv('HOMEDRIVE') + getenv('HOMEPATH'));
46     else
47         dir.initWithPath(getenv('HOME'));
48     return dir;
52 /* get_current_profile returns the name of the current profile, or
53  * null if that information cannot be found.  The result is cached for
54  * quick repeat lookup.  This is safe because xulrunner does not
55  * support switching profiles on the fly.
56  *
57  * Profiles don't necessarily have a name--as such this information should
58  * not be depended on for anything important.  It is mainly intended for
59  * decoration of the window title and mode-line.
60  */
61 let (profile_name = null) {
62     function get_current_profile () {
63         if (profile_name)
64             return profile_name;
65         if ("@mozilla.org/profile/manager;1" in Cc) {
66             profile_name = Cc["@mozilla.org/profile/manager;1"]
67                 .getService(Ci.nsIProfile)
68                 .currentProfile;
69             return profile_name;
70         }
71         var current_profile_path = Cc["@mozilla.org/file/directory_service;1"]
72             .getService(Ci.nsIProperties)
73             .get("ProfD", Ci.nsIFile).path;
74         var profile_service = Cc["@mozilla.org/toolkit/profile-service;1"]
75             .getService(Components.interfaces.nsIToolkitProfileService);
76         var profiles = profile_service.profiles;
77         while (profiles.hasMoreElements()) {
78             var p = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile);
79             if (current_profile_path == p.localDir.path ||
80                 current_profile_path == p.rootDir.path)
81             {
82                 profile_name = p.name;
83                 return p.name;
84             }
85         }
86         return null;
87     }