1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: mac/corefoundation/stdpaths.cpp
3 // Purpose: wxStandardPaths implementation for CoreFoundation systems
4 // Author: David Elliott
8 // Copyright: (c) 2004 David Elliott <dfe@cox.net>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #endif //ndef WX_PRECOMP
28 #include "wx/stdpaths.h"
29 #include "wx/filename.h"
31 #include "wx/mac/private.h"
33 #include "wx/mac/corefoundation/cfstring.h"
35 #if defined(__DARWIN__)
36 #include <CoreFoundation/CFBundle.h>
37 #include <CoreFoundation/CFURL.h>
43 #if defined(__WXCOCOA__) || defined(__WXMAC_OSX__)
44 #define kDefaultPathStyle kCFURLPOSIXPathStyle
46 #define kDefaultPathStyle kCFURLHFSPathStyle
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // wxStandardPathsCF ctors/dtor
55 // ----------------------------------------------------------------------------
57 wxStandardPathsCF::wxStandardPathsCF()
58 : m_bundle(CFBundleGetMainBundle())
63 wxStandardPathsCF::wxStandardPathsCF(wxCFBundleRef bundle
)
69 wxStandardPathsCF::~wxStandardPathsCF()
74 // ----------------------------------------------------------------------------
75 // wxStandardPathsCF Mac-specific methods
76 // ----------------------------------------------------------------------------
78 void wxStandardPathsCF::SetBundle(wxCFBundleRef bundle
)
85 // ----------------------------------------------------------------------------
86 // generic functions in terms of which the other ones are implemented
87 // ----------------------------------------------------------------------------
89 static wxString
BundleRelativeURLToPath(CFURLRef relativeURL
)
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());
98 wxString
wxStandardPathsCF::GetFromFunc(wxCFURLRef (*func
)(wxCFBundleRef
)) const
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
);
109 wxString
wxStandardPathsCF::GetDocumentsDir() const
112 return wxMacFindFolderNoSeparator
114 #if TARGET_API_MAC_OSX
119 kDocumentsFolderType
,
123 return wxFileName::GetHomeDir() + wxT("/Documents");
127 // ----------------------------------------------------------------------------
128 // wxStandardPathsCF public API
129 // ----------------------------------------------------------------------------
131 wxString
wxStandardPathsCF::GetConfigDir() const
134 return wxMacFindFolder((short)kLocalDomain
, kPreferencesFolderType
, kCreateFolder
);
136 return wxT("/Library/Preferences");
140 wxString
wxStandardPathsCF::GetUserConfigDir() const
143 return wxMacFindFolder((short)kUserDomain
, kPreferencesFolderType
, kCreateFolder
);
145 return wxFileName::GetHomeDir() + wxT("/Library/Preferences");
149 wxString
wxStandardPathsCF::GetDataDir() const
151 return GetFromFunc(CFBundleCopySharedSupportURL
);
154 // TODO: implement this using real CoreFoundation API instead of Carbon API
155 wxString
wxStandardPathsCF::GetExecutablePath() const
158 ProcessInfoRec processinfo
;
159 ProcessSerialNumber procno
;
162 procno
.highLongOfPSN
= 0 ;
163 procno
.lowLongOfPSN
= kCurrentProcess
;
164 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
165 processinfo
.processName
= NULL
;
166 processinfo
.processAppSpec
= &fsSpec
;
168 GetProcessInformation( &procno
, &processinfo
) ;
169 return wxMacFSSpec2MacFilename(&fsSpec
);
171 return wxStandardPathsBase::GetExecutablePath();
175 wxString
wxStandardPathsCF::GetLocalDataDir() const
178 return AppendAppName(wxMacFindFolder((short)kLocalDomain
, kApplicationSupportFolderType
, kCreateFolder
));
180 return AppendAppName(wxT("/Library/Application Support"));
184 wxString
wxStandardPathsCF::GetUserDataDir() const
187 return AppendAppName(wxMacFindFolder((short)kUserDomain
, kApplicationSupportFolderType
, kCreateFolder
));
189 return AppendAppName(wxFileName::GetHomeDir() + _T("/Library/Application Support"));
193 wxString
wxStandardPathsCF::GetPluginsDir() const
195 return GetFromFunc(CFBundleCopyBuiltInPlugInsURL
);
198 wxString
wxStandardPathsCF::GetResourcesDir() const
200 return GetFromFunc(CFBundleCopyResourcesDirectoryURL
);
204 wxStandardPathsCF::GetLocalizedResourcesDir(const wxString
& lang
,
205 ResourceCat category
) const
207 return wxStandardPathsBase::
208 GetLocalizedResourcesDir(lang
, category
) + _T(".lproj");
211 #endif // wxUSE_STDPATHS