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 wxString 
wxStandardPathsCF::GetExecutablePath() const 
 156     ProcessInfoRec processinfo
; 
 157     ProcessSerialNumber procno 
; 
 160     procno
.highLongOfPSN 
= NULL 
; 
 161     procno
.lowLongOfPSN 
= kCurrentProcess 
; 
 162     processinfo
.processInfoLength 
= sizeof(ProcessInfoRec
); 
 163     processinfo
.processName 
= NULL
; 
 164     processinfo
.processAppSpec 
= &fsSpec
; 
 166     GetProcessInformation( &procno 
, &processinfo 
) ; 
 167     return wxMacFSSpec2MacFilename(&fsSpec
); 
 170 wxString 
wxStandardPathsCF::GetLocalDataDir() const 
 173     return AppendAppName(wxMacFindFolder((short)kLocalDomain
, kApplicationSupportFolderType
, kCreateFolder
)); 
 175     return AppendAppName(wxT("/Library/Application Support")); 
 179 wxString 
wxStandardPathsCF::GetUserDataDir() const 
 182     return AppendAppName(wxMacFindFolder((short)kUserDomain
, kApplicationSupportFolderType
, kCreateFolder
)); 
 184     return AppendAppName(wxFileName::GetHomeDir() + _T("/Library/Application Support")); 
 188 wxString 
wxStandardPathsCF::GetPluginsDir() const 
 190     return GetFromFunc(CFBundleCopyBuiltInPlugInsURL
); 
 193 wxString 
wxStandardPathsCF::GetResourcesDir() const 
 195     return GetFromFunc(CFBundleCopyResourcesDirectoryURL
); 
 199 wxStandardPathsCF::GetLocalizedResourcesDir(const wxChar 
*lang
, 
 200                                             ResourceCat category
) const 
 202     return wxStandardPathsBase:: 
 203             GetLocalizedResourcesDir(lang
, category
) + _T(".lproj"); 
 206 #endif // wxUSE_STDPATHS