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