1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/common/stdpbase.cpp
3 // Purpose:     wxStandardPathsBase methods common to all ports
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 #ifndef WX_PRECOMP
27     #include "wx/app.h"
28 #endif //WX_PRECOMP
29 #include "wx/apptrait.h"
30 
31 #include "wx/filename.h"
32 #include "wx/stdpaths.h"
33 
34 // ----------------------------------------------------------------------------
35 // module globals
36 // ----------------------------------------------------------------------------
37 
38 namespace
39 {
40 
41 // Derive a class just to be able to create it: wxStandardPaths ctor is
42 // protected to prevent its misuse, but it also means we can't create an object
43 // of this class directly.
44 class wxStandardPathsDefault : public wxStandardPaths
45 {
46 public:
wxStandardPathsDefault()47     wxStandardPathsDefault() { }
48 };
49 
50 static wxStandardPathsDefault gs_stdPaths;
51 
52 } // anonymous namespace
53 
54 // ============================================================================
55 // implementation
56 // ============================================================================
57 
58 /* static */
Get()59 wxStandardPaths& wxStandardPathsBase::Get()
60 {
61     wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
62     wxCHECK_MSG( traits, gs_stdPaths, wxT("create wxApp before calling this") );
63 
64     return traits->GetStandardPaths();
65 }
66 
GetExecutablePath() const67 wxString wxStandardPathsBase::GetExecutablePath() const
68 {
69     if ( !wxTheApp || !wxTheApp->argv )
70         return wxEmptyString;
71 
72     wxString argv0 = wxTheApp->argv[0];
73     if (wxIsAbsolutePath(argv0))
74         return argv0;
75 
76     // Search PATH.environment variable...
77     wxPathList pathlist;
78     pathlist.AddEnvList(wxT("PATH"));
79     wxString path = pathlist.FindAbsoluteValidPath(argv0);
80     if ( path.empty() )
81         return argv0;       // better than nothing
82 
83     wxFileName filename(path);
84     filename.Normalize();
85     return filename.GetFullPath();
86 }
87 
GetStandardPaths()88 wxStandardPaths& wxAppTraitsBase::GetStandardPaths()
89 {
90     return gs_stdPaths;
91 }
92 
wxStandardPathsBase()93 wxStandardPathsBase::wxStandardPathsBase()
94 {
95     // Set the default information that is used when
96     // forming some paths (by AppendAppInfo).
97     // Derived classes can call this in their constructors
98     // to set the platform-specific settings
99     UseAppInfo(AppInfo_AppName);
100 }
101 
~wxStandardPathsBase()102 wxStandardPathsBase::~wxStandardPathsBase()
103 {
104     // nothing to do here
105 }
106 
GetLocalDataDir() const107 wxString wxStandardPathsBase::GetLocalDataDir() const
108 {
109     return GetDataDir();
110 }
111 
GetUserLocalDataDir() const112 wxString wxStandardPathsBase::GetUserLocalDataDir() const
113 {
114     return GetUserDataDir();
115 }
116 
GetDocumentsDir() const117 wxString wxStandardPathsBase::GetDocumentsDir() const
118 {
119     return wxFileName::GetHomeDir();
120 }
121 
GetAppDocumentsDir() const122 wxString wxStandardPathsBase::GetAppDocumentsDir() const
123 {
124     const wxString docsDir = GetDocumentsDir();
125     wxString appDocsDir = AppendAppInfo(docsDir);
126 
127     return wxDirExists(appDocsDir) ? appDocsDir : docsDir;
128 }
129 
130 // return the temporary directory for the current user
GetTempDir() const131 wxString wxStandardPathsBase::GetTempDir() const
132 {
133     return wxFileName::GetTempDir();
134 }
135 
136 /* static */
137 wxString
AppendPathComponent(const wxString & dir,const wxString & component)138 wxStandardPathsBase::AppendPathComponent(const wxString& dir,
139                                          const wxString& component)
140 {
141     wxString subdir(dir);
142 
143     // empty string indicates that an error has occurred, don't touch it then
144     if ( !subdir.empty() )
145     {
146         if ( !component.empty() )
147         {
148             const wxChar ch = *(subdir.end() - 1);
149             if ( !wxFileName::IsPathSeparator(ch) && ch != wxT('.') )
150                 subdir += wxFileName::GetPathSeparator();
151 
152             subdir += component;
153         }
154     }
155 
156     return subdir;
157 }
158 
159 
AppendAppInfo(const wxString & dir) const160 wxString wxStandardPathsBase::AppendAppInfo(const wxString& dir) const
161 {
162     wxString subdir(dir);
163 
164     if ( UsesAppInfo(AppInfo_VendorName) )
165     {
166         subdir = AppendPathComponent(subdir, wxTheApp->GetVendorName());
167     }
168 
169     if ( UsesAppInfo(AppInfo_AppName) )
170     {
171         subdir = AppendPathComponent(subdir, wxTheApp->GetAppName());
172     }
173 
174     return subdir;
175 }
176 
177