]>
Commit | Line | Data |
---|---|---|
c320b3e3 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: unix/stdpaths.cpp | |
a98ce49a | 3 | // Purpose: wxStandardPaths implementation for Unix & OpenVMS systems |
c320b3e3 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 2004-10-19 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
07158944 VZ |
27 | #if wxUSE_STDPATHS |
28 | ||
ab9f9e71 PC |
29 | #include "wx/stdpaths.h" |
30 | ||
c320b3e3 | 31 | #ifndef WX_PRECOMP |
f3d79306 | 32 | #include "wx/app.h" |
f95abb9d | 33 | #include "wx/wxcrt.h" |
ab9f9e71 | 34 | #include "wx/utils.h" |
c320b3e3 VZ |
35 | #endif //WX_PRECOMP |
36 | ||
37 | #include "wx/filename.h" | |
0ce52f3d JS |
38 | #include "wx/log.h" |
39 | #include "wx/textfile.h" | |
c320b3e3 | 40 | |
a98ce49a | 41 | #if defined( __LINUX__ ) || defined( __VMS ) |
c320b3e3 VZ |
42 | #include <unistd.h> |
43 | #endif | |
44 | ||
45 | // ============================================================================ | |
d3b9c627 | 46 | // common VMS/Unix part of wxStandardPaths implementation |
c320b3e3 VZ |
47 | // ============================================================================ |
48 | ||
c320b3e3 VZ |
49 | void wxStandardPaths::SetInstallPrefix(const wxString& prefix) |
50 | { | |
51 | m_prefix = prefix; | |
52 | } | |
53 | ||
d3b9c627 VZ |
54 | wxString wxStandardPaths::GetUserConfigDir() const |
55 | { | |
56 | return wxFileName::GetHomeDir(); | |
57 | } | |
58 | ||
0ce52f3d | 59 | |
d3b9c627 VZ |
60 | // ============================================================================ |
61 | // wxStandardPaths implementation for VMS | |
62 | // ============================================================================ | |
63 | ||
64 | #ifdef __VMS | |
65 | ||
66 | wxString wxStandardPaths::GetInstallPrefix() const | |
67 | { | |
68 | if ( m_prefix.empty() ) | |
69 | { | |
5c33522f | 70 | const_cast<wxStandardPaths *>(this)->m_prefix = wxT("/sys$system"); |
d3b9c627 | 71 | } |
cc4d9903 VZ |
72 | |
73 | return m_prefix; | |
d3b9c627 VZ |
74 | } |
75 | ||
76 | wxString wxStandardPaths::GetConfigDir() const | |
77 | { | |
9a83f860 | 78 | return wxT("/sys$manager"); |
d3b9c627 VZ |
79 | } |
80 | ||
81 | wxString wxStandardPaths::GetDataDir() const | |
82 | { | |
9a83f860 | 83 | return AppendAppInfo(GetInstallPrefix() + wxT("/sys$share")); |
d3b9c627 VZ |
84 | } |
85 | ||
86 | wxString wxStandardPaths::GetLocalDataDir() const | |
87 | { | |
9a83f860 | 88 | return AppendAppInfo(wxT("/sys$manager")); |
d3b9c627 VZ |
89 | } |
90 | ||
91 | wxString wxStandardPaths::GetUserDataDir() const | |
92 | { | |
93 | return wxFileName::GetHomeDir(); | |
94 | } | |
95 | ||
cc4d9903 VZ |
96 | wxString wxStandardPaths::GetPluginsDir() const |
97 | { | |
98 | return wxString(); // TODO: this is wrong, it should return something | |
99 | } | |
100 | ||
3af9f2de | 101 | wxString |
e0b3b9d0 | 102 | wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang, |
3af9f2de VZ |
103 | ResourceCat category) const |
104 | { | |
105 | return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category); | |
106 | } | |
107 | ||
b29aaf4a JJ |
108 | wxString wxStandardPaths::GetExecutablePath() const |
109 | { | |
110 | return wxStandardPathsBase::GetExecutablePath(); | |
111 | } | |
112 | ||
d3b9c627 VZ |
113 | #else // !__VMS |
114 | ||
115 | // ============================================================================ | |
116 | // wxStandardPaths implementation for Unix | |
117 | // ============================================================================ | |
118 | ||
ac7ad70d | 119 | wxString wxStandardPaths::GetExecutablePath() const |
c320b3e3 | 120 | { |
3abcfa9b | 121 | #ifdef __LINUX__ |
3abcfa9b VZ |
122 | wxString exeStr; |
123 | ||
124 | char buf[4096]; | |
125 | int result = readlink("/proc/self/exe", buf, WXSIZEOF(buf) - sizeof(char)); | |
126 | if ( result != -1 ) | |
c320b3e3 | 127 | { |
3abcfa9b | 128 | buf[result] = '\0'; // readlink() doesn't NUL-terminate the buffer |
c320b3e3 | 129 | |
3abcfa9b VZ |
130 | // if the /proc/self/exe symlink has been dropped by the kernel for |
131 | // some reason, then readlink() could also return success but | |
132 | // "(deleted)" as link destination... | |
133 | if ( strcmp(buf, "(deleted)") != 0 ) | |
134 | exeStr = wxString(buf, wxConvLibc); | |
135 | } | |
136 | ||
137 | if ( exeStr.empty() ) | |
138 | { | |
139 | // UPX-specific hack: when using UPX on linux, the kernel will drop the | |
140 | // /proc/self/exe link; in this case we try to look for a special | |
141 | // environment variable called " " which is created by UPX to save | |
142 | // /proc/self/exe contents. See | |
143 | // http://sf.net/tracker/?func=detail&atid=309863&aid=1565357&group_id=9863 | |
144 | // for more information about this issue. | |
145 | wxGetEnv(wxT(" "), &exeStr); | |
146 | } | |
147 | ||
ac7ad70d RR |
148 | if ( !exeStr.empty() ) |
149 | return exeStr; | |
150 | #endif // __LINUX__ | |
151 | ||
152 | return wxStandardPathsBase::GetExecutablePath(); | |
153 | } | |
154 | ||
155 | void wxStandardPaths::DetectPrefix() | |
156 | { | |
157 | // we can try to infer the prefix from the location of the executable | |
158 | wxString exeStr = GetExecutablePath(); | |
3abcfa9b VZ |
159 | if ( !exeStr.empty() ) |
160 | { | |
161 | // consider that we're in the last "bin" subdirectory of our prefix | |
162 | size_t pos = exeStr.rfind(wxT("/bin/")); | |
163 | if ( pos != wxString::npos ) | |
164 | m_prefix.assign(exeStr, 0, pos); | |
165 | } | |
74ebd406 | 166 | |
3abcfa9b VZ |
167 | if ( m_prefix.empty() ) |
168 | { | |
169 | m_prefix = wxT("/usr/local"); | |
170 | } | |
171 | } | |
172 | ||
173 | wxString wxStandardPaths::GetInstallPrefix() const | |
174 | { | |
175 | if ( m_prefix.empty() ) | |
176 | { | |
5c33522f | 177 | wxStandardPaths *pathPtr = const_cast<wxStandardPaths *>(this); |
3abcfa9b | 178 | pathPtr->DetectPrefix(); |
c320b3e3 VZ |
179 | } |
180 | ||
181 | return m_prefix; | |
182 | } | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // public functions | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | wxString wxStandardPaths::GetConfigDir() const | |
189 | { | |
9a83f860 | 190 | return wxT("/etc"); |
c320b3e3 VZ |
191 | } |
192 | ||
193 | wxString wxStandardPaths::GetDataDir() const | |
194 | { | |
306a5d95 VZ |
195 | // allow to override the location of the data directory by setting |
196 | // WX_APPNAME_DATA_DIR environment variable: this is very useful in | |
197 | // practice for running well-written (and so using wxStandardPaths to find | |
198 | // their files) wx applications without installing them | |
199 | static const wxString | |
200 | envOverride(getenv("WX_" + wxTheApp->GetAppName().Upper() + "_DATA_DIR")); | |
201 | ||
202 | if ( !envOverride.empty() ) | |
203 | return envOverride; | |
204 | ||
9a83f860 | 205 | return AppendAppInfo(GetInstallPrefix() + wxT("/share")); |
c320b3e3 VZ |
206 | } |
207 | ||
208 | wxString wxStandardPaths::GetLocalDataDir() const | |
209 | { | |
9a83f860 | 210 | return AppendAppInfo(wxT("/etc")); |
c320b3e3 VZ |
211 | } |
212 | ||
213 | wxString wxStandardPaths::GetUserDataDir() const | |
214 | { | |
9a83f860 | 215 | return AppendAppInfo(wxFileName::GetHomeDir() + wxT("/.")); |
c320b3e3 VZ |
216 | } |
217 | ||
cc4d9903 VZ |
218 | wxString wxStandardPaths::GetPluginsDir() const |
219 | { | |
9a83f860 | 220 | return AppendAppInfo(GetInstallPrefix() + wxT("/lib")); |
cc4d9903 VZ |
221 | } |
222 | ||
3af9f2de | 223 | wxString |
e0b3b9d0 | 224 | wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang, |
3af9f2de VZ |
225 | ResourceCat category) const |
226 | { | |
227 | if ( category != ResourceCat_Messages ) | |
228 | return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category); | |
229 | ||
9a83f860 | 230 | return GetInstallPrefix() + wxT("/share/locale/") + lang + wxT("/LC_MESSAGES"); |
3af9f2de VZ |
231 | } |
232 | ||
0ce52f3d JS |
233 | wxString wxStandardPaths::GetDocumentsDir() const |
234 | { | |
235 | { | |
236 | wxLogNull logNull; | |
237 | wxString homeDir = wxFileName::GetHomeDir(); | |
238 | wxString configPath; | |
239 | if (wxGetenv(wxT("XDG_CONFIG_HOME"))) | |
240 | configPath = wxGetenv(wxT("XDG_CONFIG_HOME")); | |
241 | else | |
242 | configPath = homeDir + wxT("/.config"); | |
243 | wxString dirsFile = configPath + wxT("/user-dirs.dirs"); | |
244 | if (wxFileExists(dirsFile)) | |
245 | { | |
246 | wxTextFile textFile; | |
247 | if (textFile.Open(dirsFile)) | |
248 | { | |
249 | size_t i; | |
250 | for (i = 0; i < textFile.GetLineCount(); i++) | |
251 | { | |
252 | wxString line(textFile[i]); | |
253 | int pos = line.Find(wxT("XDG_DOCUMENTS_DIR")); | |
254 | if (pos != wxNOT_FOUND) | |
255 | { | |
256 | wxString value = line.AfterFirst(wxT('=')); | |
257 | value.Replace(wxT("$HOME"), homeDir); | |
258 | value.Trim(true); | |
259 | value.Trim(false); | |
260 | if (!value.IsEmpty() && wxDirExists(value)) | |
261 | return value; | |
262 | else | |
263 | break; | |
264 | } | |
265 | } | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | return wxStandardPathsBase::GetDocumentsDir(); | |
271 | } | |
272 | ||
d3b9c627 | 273 | #endif // __VMS/!__VMS |
c320b3e3 | 274 | |
07158944 | 275 | #endif // wxUSE_STDPATHS |