No changes, synchronised source names that appear commented at the top of files with...
[wxWidgets.git] / src / unix / stdpaths.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/stdpaths.cpp
3 // Purpose: wxStandardPaths implementation for Unix & OpenVMS systems
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 // Licence: wxWindows licence
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
27 #if wxUSE_STDPATHS
28
29 #include "wx/stdpaths.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/wxcrt.h"
34 #include "wx/utils.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/filename.h"
38 #include "wx/log.h"
39 #include "wx/textfile.h"
40
41 #if defined( __LINUX__ ) || defined( __VMS )
42 #include <unistd.h>
43 #endif
44
45 // ============================================================================
46 // common VMS/Unix part of wxStandardPaths implementation
47 // ============================================================================
48
49 void wxStandardPaths::SetInstallPrefix(const wxString& prefix)
50 {
51 m_prefix = prefix;
52 }
53
54 wxString wxStandardPaths::GetUserConfigDir() const
55 {
56 return wxFileName::GetHomeDir();
57 }
58
59
60 // ============================================================================
61 // wxStandardPaths implementation for VMS
62 // ============================================================================
63
64 #ifdef __VMS
65
66 wxString wxStandardPaths::GetInstallPrefix() const
67 {
68 if ( m_prefix.empty() )
69 {
70 const_cast<wxStandardPaths *>(this)->m_prefix = wxT("/sys$system");
71 }
72
73 return m_prefix;
74 }
75
76 wxString wxStandardPaths::GetConfigDir() const
77 {
78 return wxT("/sys$manager");
79 }
80
81 wxString wxStandardPaths::GetDataDir() const
82 {
83 return AppendAppInfo(GetInstallPrefix() + wxT("/sys$share"));
84 }
85
86 wxString wxStandardPaths::GetLocalDataDir() const
87 {
88 return AppendAppInfo(wxT("/sys$manager"));
89 }
90
91 wxString wxStandardPaths::GetUserDataDir() const
92 {
93 return wxFileName::GetHomeDir();
94 }
95
96 wxString wxStandardPaths::GetPluginsDir() const
97 {
98 return wxString(); // TODO: this is wrong, it should return something
99 }
100
101 wxString
102 wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang,
103 ResourceCat category) const
104 {
105 return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category);
106 }
107
108 wxString wxStandardPaths::GetExecutablePath() const
109 {
110 return wxStandardPathsBase::GetExecutablePath();
111 }
112
113 #else // !__VMS
114
115 // ============================================================================
116 // wxStandardPaths implementation for Unix
117 // ============================================================================
118
119 wxString wxStandardPaths::GetExecutablePath() const
120 {
121 #ifdef __LINUX__
122 wxString exeStr;
123
124 char buf[4096];
125 int result = readlink("/proc/self/exe", buf, WXSIZEOF(buf) - sizeof(char));
126 if ( result != -1 )
127 {
128 buf[result] = '\0'; // readlink() doesn't NUL-terminate the buffer
129
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
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();
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 }
166
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 {
177 wxStandardPaths *pathPtr = const_cast<wxStandardPaths *>(this);
178 pathPtr->DetectPrefix();
179 }
180
181 return m_prefix;
182 }
183
184 // ----------------------------------------------------------------------------
185 // public functions
186 // ----------------------------------------------------------------------------
187
188 wxString wxStandardPaths::GetConfigDir() const
189 {
190 return wxT("/etc");
191 }
192
193 wxString wxStandardPaths::GetDataDir() const
194 {
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(
201 getenv(
202 ("WX_" + wxTheApp->GetAppName().Upper() + "_DATA_DIR").c_str()
203 )
204 );
205
206 if ( !envOverride.empty() )
207 return envOverride;
208
209 return AppendAppInfo(GetInstallPrefix() + wxT("/share"));
210 }
211
212 wxString wxStandardPaths::GetLocalDataDir() const
213 {
214 return AppendAppInfo(wxT("/etc"));
215 }
216
217 wxString wxStandardPaths::GetUserDataDir() const
218 {
219 return AppendAppInfo(wxFileName::GetHomeDir() + wxT("/."));
220 }
221
222 wxString wxStandardPaths::GetPluginsDir() const
223 {
224 return AppendAppInfo(GetInstallPrefix() + wxT("/lib"));
225 }
226
227 wxString
228 wxStandardPaths::GetLocalizedResourcesDir(const wxString& lang,
229 ResourceCat category) const
230 {
231 if ( category != ResourceCat_Messages )
232 return wxStandardPathsBase::GetLocalizedResourcesDir(lang, category);
233
234 return GetInstallPrefix() + wxT("/share/locale/") + lang + wxT("/LC_MESSAGES");
235 }
236
237 wxString wxStandardPaths::GetDocumentsDir() const
238 {
239 {
240 wxLogNull logNull;
241 wxString homeDir = wxFileName::GetHomeDir();
242 wxString configPath;
243 if (wxGetenv(wxT("XDG_CONFIG_HOME")))
244 configPath = wxGetenv(wxT("XDG_CONFIG_HOME"));
245 else
246 configPath = homeDir + wxT("/.config");
247 wxString dirsFile = configPath + wxT("/user-dirs.dirs");
248 if (wxFileExists(dirsFile))
249 {
250 wxTextFile textFile;
251 if (textFile.Open(dirsFile))
252 {
253 size_t i;
254 for (i = 0; i < textFile.GetLineCount(); i++)
255 {
256 wxString line(textFile[i]);
257 int pos = line.Find(wxT("XDG_DOCUMENTS_DIR"));
258 if (pos != wxNOT_FOUND)
259 {
260 wxString value = line.AfterFirst(wxT('='));
261 value.Replace(wxT("$HOME"), homeDir);
262 value.Trim(true);
263 value.Trim(false);
264 if (!value.IsEmpty() && wxDirExists(value))
265 return value;
266 else
267 break;
268 }
269 }
270 }
271 }
272 }
273
274 return wxStandardPathsBase::GetDocumentsDir();
275 }
276
277 #endif // __VMS/!__VMS
278
279 #endif // wxUSE_STDPATHS