1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/corefoundation/stdpaths.cpp
3 // Purpose: wxStandardPaths implementation for CoreFoundation systems
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2004-10-27
7 // RCS-ID: $Id: stdpaths_cf.cpp 46052 2007-05-15 20:22:55Z SC $
8 // Copyright: (c) 2004 David Elliott <dfe@cox.net>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #if wxUSE_STDPATHS
23
24 #ifndef WX_PRECOMP
25 #include "wx/intl.h"
26 #endif //ndef WX_PRECOMP
27
28 #include "wx/stdpaths.h"
29 #include "wx/filename.h"
30 #ifdef __WXMAC__
31 #include "wx/mac/private.h"
32 #endif
33 #include "wx/mac/corefoundation/cfstring.h"
34
35 #if defined(__DARWIN__)
36 #include <CoreFoundation/CFBundle.h>
37 #include <CoreFoundation/CFURL.h>
38 #else
39 #include <CFBundle.h>
40 #include <CFURL.h>
41 #endif
42
43 #if defined(__WXCOCOA__) || defined(__WXMAC_OSX__)
44 #define kDefaultPathStyle kCFURLPOSIXPathStyle
45 #else
46 #define kDefaultPathStyle kCFURLHFSPathStyle
47 #endif
48
49 // ============================================================================
50 // implementation
51 // ============================================================================
52
53 // ----------------------------------------------------------------------------
54 // wxStandardPathsCF ctors/dtor
55 // ----------------------------------------------------------------------------
56
wxStandardPathsCF()57 wxStandardPathsCF::wxStandardPathsCF()
58 : m_bundle(CFBundleGetMainBundle())
59 {
60 CFRetain(m_bundle);
61 }
62
wxStandardPathsCF(wxCFBundleRef bundle)63 wxStandardPathsCF::wxStandardPathsCF(wxCFBundleRef bundle)
64 : m_bundle(bundle)
65 {
66 CFRetain(m_bundle);
67 }
68
~wxStandardPathsCF()69 wxStandardPathsCF::~wxStandardPathsCF()
70 {
71 CFRelease(m_bundle);
72 }
73
74 // ----------------------------------------------------------------------------
75 // wxStandardPathsCF Mac-specific methods
76 // ----------------------------------------------------------------------------
77
SetBundle(wxCFBundleRef bundle)78 void wxStandardPathsCF::SetBundle(wxCFBundleRef bundle)
79 {
80 CFRetain(bundle);
81 CFRelease(m_bundle);
82 m_bundle = bundle;
83 }
84
85 // ----------------------------------------------------------------------------
86 // generic functions in terms of which the other ones are implemented
87 // ----------------------------------------------------------------------------
88
BundleRelativeURLToPath(CFURLRef relativeURL)89 static wxString BundleRelativeURLToPath(CFURLRef relativeURL)
90 {
91 CFURLRef absoluteURL = CFURLCopyAbsoluteURL(relativeURL);
92 wxCHECK_MSG(absoluteURL, wxEmptyString, wxT("Failed to resolve relative URL to absolute URL"));
93 CFStringRef cfStrPath = CFURLCopyFileSystemPath(absoluteURL,kDefaultPathStyle);
94 CFRelease(absoluteURL);
95 return wxMacCFStringHolder(cfStrPath).AsString(wxLocale::GetSystemEncoding());
96 }
97
GetFromFunc(wxCFURLRef (* func)(wxCFBundleRef)) const98 wxString wxStandardPathsCF::GetFromFunc(wxCFURLRef (*func)(wxCFBundleRef)) const
99 {
100 wxCHECK_MSG(m_bundle, wxEmptyString,
101 wxT("wxStandardPaths for CoreFoundation only works with bundled apps"));
102 CFURLRef relativeURL = (*func)(m_bundle);
103 wxCHECK_MSG(relativeURL, wxEmptyString, wxT("Couldn't get URL"));
104 wxString ret(BundleRelativeURLToPath(relativeURL));
105 CFRelease(relativeURL);
106 return ret;
107 }
108
GetDocumentsDir() const109 wxString wxStandardPathsCF::GetDocumentsDir() const
110 {
111 #ifdef __WXMAC__
112 return wxMacFindFolderNoSeparator
113 (
114 #if TARGET_API_MAC_OSX
115 kUserDomain,
116 #else
117 kOnSystemDisk,
118 #endif
119 kDocumentsFolderType,
120 kCreateFolder
121 );
122 #else
123 return wxFileName::GetHomeDir() + wxT("/Documents");
124 #endif
125 }
126
127 // ----------------------------------------------------------------------------
128 // wxStandardPathsCF public API
129 // ----------------------------------------------------------------------------
130
GetConfigDir() const131 wxString wxStandardPathsCF::GetConfigDir() const
132 {
133 #ifdef __WXMAC__
134 return wxMacFindFolder((short)kLocalDomain, kPreferencesFolderType, kCreateFolder);
135 #else
136 return wxT("/Library/Preferences");
137 #endif
138 }
139
GetUserConfigDir() const140 wxString wxStandardPathsCF::GetUserConfigDir() const
141 {
142 #ifdef __WXMAC__
143 return wxMacFindFolder((short)kUserDomain, kPreferencesFolderType, kCreateFolder);
144 #else
145 return wxFileName::GetHomeDir() + wxT("/Library/Preferences");
146 #endif
147 }
148
GetDataDir() const149 wxString wxStandardPathsCF::GetDataDir() const
150 {
151 return GetFromFunc(CFBundleCopySharedSupportURL);
152 }
153
154 // TODO: implement this using real CoreFoundation API instead of Carbon API
GetExecutablePath() const155 wxString wxStandardPathsCF::GetExecutablePath() const
156 {
157 #ifdef __WXMAC__
158 ProcessInfoRec processinfo;
159 ProcessSerialNumber procno ;
160 #ifdef __LP64__
161 FSRef fsRef;
162 #else
163 FSSpec fsSpec;
164 #endif
165
166 procno.highLongOfPSN = 0 ;
167 procno.lowLongOfPSN = kCurrentProcess ;
168 processinfo.processInfoLength = sizeof(ProcessInfoRec);
169 processinfo.processName = NULL;
170 #ifdef __LP64__
171 processinfo.processAppRef = &fsRef;
172 #else
173 processinfo.processAppSpec = &fsSpec;
174 #endif
175
176 GetProcessInformation( &procno , &processinfo ) ;
177 #ifdef __LP64__
178 return wxMacFSRefToPath(&fsRef);
179 #else
180 return wxMacFSSpec2MacFilename(&fsSpec);
181 #endif
182 #else
183 return wxStandardPathsBase::GetExecutablePath();
184 #endif
185 }
186
GetLocalDataDir() const187 wxString wxStandardPathsCF::GetLocalDataDir() const
188 {
189 #ifdef __WXMAC__
190 return AppendAppName(wxMacFindFolder((short)kLocalDomain, kApplicationSupportFolderType, kCreateFolder));
191 #else
192 return AppendAppName(wxT("/Library/Application Support"));
193 #endif
194 }
195
GetUserDataDir() const196 wxString wxStandardPathsCF::GetUserDataDir() const
197 {
198 #ifdef __WXMAC__
199 return AppendAppName(wxMacFindFolder((short)kUserDomain, kApplicationSupportFolderType, kCreateFolder));
200 #else
201 return AppendAppName(wxFileName::GetHomeDir() + _T("/Library/Application Support"));
202 #endif
203 }
204
GetPluginsDir() const205 wxString wxStandardPathsCF::GetPluginsDir() const
206 {
207 return GetFromFunc(CFBundleCopyBuiltInPlugInsURL);
208 }
209
GetResourcesDir() const210 wxString wxStandardPathsCF::GetResourcesDir() const
211 {
212 return GetFromFunc(CFBundleCopyResourcesDirectoryURL);
213 }
214
215 wxString
GetLocalizedResourcesDir(const wxChar * lang,ResourceCat category) const216 wxStandardPathsCF::GetLocalizedResourcesDir(const wxChar *lang,
217 ResourceCat category) const
218 {
219 return wxStandardPathsBase::
220 GetLocalizedResourcesDir(lang, category) + _T(".lproj");
221 }
222
223 #endif // wxUSE_STDPATHS
224