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/osx/private.h"
33 #include "wx/osx/core/cfstring.h"
35 #include <CoreFoundation/CFBundle.h>
36 #include <CoreFoundation/CFURL.h>
38 #define kDefaultPathStyle kCFURLPOSIXPathStyle
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
45 // wxStandardPathsCF ctors/dtor
46 // ----------------------------------------------------------------------------
48 wxStandardPathsCF::wxStandardPathsCF()
49 : m_bundle(CFBundleGetMainBundle())
54 wxStandardPathsCF::wxStandardPathsCF(wxCFBundleRef bundle
)
60 wxStandardPathsCF::~wxStandardPathsCF()
65 // ----------------------------------------------------------------------------
66 // wxStandardPathsCF Mac-specific methods
67 // ----------------------------------------------------------------------------
69 void wxStandardPathsCF::SetBundle(wxCFBundleRef bundle
)
76 // ----------------------------------------------------------------------------
77 // generic functions in terms of which the other ones are implemented
78 // ----------------------------------------------------------------------------
80 static wxString
BundleRelativeURLToPath(CFURLRef relativeURL
)
82 CFURLRef absoluteURL
= CFURLCopyAbsoluteURL(relativeURL
);
83 wxCHECK_MSG(absoluteURL
, wxEmptyString
, wxT("Failed to resolve relative URL to absolute URL"));
84 CFStringRef cfStrPath
= CFURLCopyFileSystemPath(absoluteURL
,kDefaultPathStyle
);
85 CFRelease(absoluteURL
);
86 return wxCFStringRef(cfStrPath
).AsString(wxLocale::GetSystemEncoding());
89 wxString
wxStandardPathsCF::GetFromFunc(wxCFURLRef (*func
)(wxCFBundleRef
)) const
91 wxCHECK_MSG(m_bundle
, wxEmptyString
,
92 wxT("wxStandardPaths for CoreFoundation only works with bundled apps"));
93 CFURLRef relativeURL
= (*func
)(m_bundle
);
94 wxCHECK_MSG(relativeURL
, wxEmptyString
, wxT("Couldn't get URL"));
95 wxString
ret(BundleRelativeURLToPath(relativeURL
));
96 CFRelease(relativeURL
);
100 wxString
wxStandardPathsCF::GetDocumentsDir() const
102 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
103 return wxMacFindFolderNoSeparator
106 kDocumentsFolderType
,
110 return wxFileName::GetHomeDir() + wxT("/Documents");
114 // ----------------------------------------------------------------------------
115 // wxStandardPathsCF public API
116 // ----------------------------------------------------------------------------
118 wxString
wxStandardPathsCF::GetConfigDir() const
120 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
121 return wxMacFindFolder((short)kLocalDomain
, kPreferencesFolderType
, kCreateFolder
);
123 return wxT("/Library/Preferences");
127 wxString
wxStandardPathsCF::GetUserConfigDir() const
129 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
130 return wxMacFindFolder((short)kUserDomain
, kPreferencesFolderType
, kCreateFolder
);
132 return wxFileName::GetHomeDir() + wxT("/Library/Preferences");
136 wxString
wxStandardPathsCF::GetDataDir() const
138 return GetFromFunc(CFBundleCopySharedSupportURL
);
141 wxString
wxStandardPathsCF::GetExecutablePath() const
145 return GetFromFunc(CFBundleCopyBundleURL
);
147 // TODO remove if cf implementation ok
148 ProcessInfoRec processinfo
;
149 ProcessSerialNumber procno
;
156 procno
.highLongOfPSN
= 0 ;
157 procno
.lowLongOfPSN
= kCurrentProcess
;
158 processinfo
.processInfoLength
= sizeof(ProcessInfoRec
);
159 processinfo
.processName
= NULL
;
161 processinfo
.processAppRef
= &fsRef
;
163 processinfo
.processAppSpec
= &fsSpec
;
166 GetProcessInformation( &procno
, &processinfo
) ;
168 return wxMacFSRefToPath(&fsRef
);
170 return wxMacFSSpec2MacFilename(&fsSpec
);
175 return wxStandardPathsBase::GetExecutablePath();
179 wxString
wxStandardPathsCF::GetLocalDataDir() const
181 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
182 return AppendAppInfo(wxMacFindFolder((short)kLocalDomain
, kApplicationSupportFolderType
, kCreateFolder
));
184 return AppendAppInfo(wxT("/Library/Application Support"));
188 wxString
wxStandardPathsCF::GetUserDataDir() const
190 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
191 return AppendAppInfo(wxMacFindFolder((short)kUserDomain
, kApplicationSupportFolderType
, kCreateFolder
));
193 return AppendAppInfo(wxFileName::GetHomeDir() + wxT("/Library/Application Support"));
197 wxString
wxStandardPathsCF::GetPluginsDir() const
199 return GetFromFunc(CFBundleCopyBuiltInPlugInsURL
);
202 wxString
wxStandardPathsCF::GetResourcesDir() const
204 return GetFromFunc(CFBundleCopyResourcesDirectoryURL
);
208 wxStandardPathsCF::GetLocalizedResourcesDir(const wxString
& lang
,
209 ResourceCat category
) const
211 return wxStandardPathsBase::
212 GetLocalizedResourcesDir(lang
, category
) + wxT(".lproj");
215 #endif // wxUSE_STDPATHS