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