]> git.saurik.com Git - wxWidgets.git/blob - src/mac/corefoundation/stdpaths_cf.cpp
cleanup mac
[wxWidgets.git] / src / mac / corefoundation / stdpaths_cf.cpp
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$
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 #include <CoreFoundation/CFBundle.h>
36 #include <CoreFoundation/CFURL.h>
37
38 #define kDefaultPathStyle kCFURLPOSIXPathStyle
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44 // ----------------------------------------------------------------------------
45 // wxStandardPathsCF ctors/dtor
46 // ----------------------------------------------------------------------------
47
48 wxStandardPathsCF::wxStandardPathsCF()
49 : m_bundle(CFBundleGetMainBundle())
50 {
51 CFRetain(m_bundle);
52 }
53
54 wxStandardPathsCF::wxStandardPathsCF(wxCFBundleRef bundle)
55 : m_bundle(bundle)
56 {
57 CFRetain(m_bundle);
58 }
59
60 wxStandardPathsCF::~wxStandardPathsCF()
61 {
62 CFRelease(m_bundle);
63 }
64
65 // ----------------------------------------------------------------------------
66 // wxStandardPathsCF Mac-specific methods
67 // ----------------------------------------------------------------------------
68
69 void wxStandardPathsCF::SetBundle(wxCFBundleRef bundle)
70 {
71 CFRetain(bundle);
72 CFRelease(m_bundle);
73 m_bundle = bundle;
74 }
75
76 // ----------------------------------------------------------------------------
77 // generic functions in terms of which the other ones are implemented
78 // ----------------------------------------------------------------------------
79
80 static wxString BundleRelativeURLToPath(CFURLRef relativeURL)
81 {
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 wxMacCFStringHolder(cfStrPath).AsString(wxLocale::GetSystemEncoding());
87 }
88
89 wxString wxStandardPathsCF::GetFromFunc(wxCFURLRef (*func)(wxCFBundleRef)) const
90 {
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);
97 return ret;
98 }
99
100 wxString wxStandardPathsCF::GetDocumentsDir() const
101 {
102 #ifdef __WXMAC__
103 return wxMacFindFolderNoSeparator
104 (
105 #if TARGET_API_MAC_OSX
106 kUserDomain,
107 #else
108 kOnSystemDisk,
109 #endif
110 kDocumentsFolderType,
111 kCreateFolder
112 );
113 #else
114 return wxFileName::GetHomeDir() + wxT("/Documents");
115 #endif
116 }
117
118 // ----------------------------------------------------------------------------
119 // wxStandardPathsCF public API
120 // ----------------------------------------------------------------------------
121
122 wxString wxStandardPathsCF::GetConfigDir() const
123 {
124 #ifdef __WXMAC__
125 return wxMacFindFolder((short)kLocalDomain, kPreferencesFolderType, kCreateFolder);
126 #else
127 return wxT("/Library/Preferences");
128 #endif
129 }
130
131 wxString wxStandardPathsCF::GetUserConfigDir() const
132 {
133 #ifdef __WXMAC__
134 return wxMacFindFolder((short)kUserDomain, kPreferencesFolderType, kCreateFolder);
135 #else
136 return wxFileName::GetHomeDir() + wxT("/Library/Preferences");
137 #endif
138 }
139
140 wxString wxStandardPathsCF::GetDataDir() const
141 {
142 return GetFromFunc(CFBundleCopySharedSupportURL);
143 }
144
145 // TODO: implement this using real CoreFoundation API instead of Carbon API
146 wxString wxStandardPathsCF::GetExecutablePath() const
147 {
148 #ifdef __WXMAC__
149 ProcessInfoRec processinfo;
150 ProcessSerialNumber procno ;
151 FSSpec fsSpec;
152
153 procno.highLongOfPSN = 0 ;
154 procno.lowLongOfPSN = kCurrentProcess ;
155 processinfo.processInfoLength = sizeof(ProcessInfoRec);
156 processinfo.processName = NULL;
157 processinfo.processAppSpec = &fsSpec;
158
159 GetProcessInformation( &procno , &processinfo ) ;
160 #ifdef __LP64__
161 return wxMacFSRefToPath(&fsRef);
162 #else
163 return wxMacFSSpec2MacFilename(&fsSpec);
164 #endif
165 #else
166 return wxStandardPathsBase::GetExecutablePath();
167 #endif
168 }
169
170 wxString wxStandardPathsCF::GetLocalDataDir() const
171 {
172 #ifdef __WXMAC__
173 return AppendAppName(wxMacFindFolder((short)kLocalDomain, kApplicationSupportFolderType, kCreateFolder));
174 #else
175 return AppendAppName(wxT("/Library/Application Support"));
176 #endif
177 }
178
179 wxString wxStandardPathsCF::GetUserDataDir() const
180 {
181 #ifdef __WXMAC__
182 return AppendAppName(wxMacFindFolder((short)kUserDomain, kApplicationSupportFolderType, kCreateFolder));
183 #else
184 return AppendAppName(wxFileName::GetHomeDir() + _T("/Library/Application Support"));
185 #endif
186 }
187
188 wxString wxStandardPathsCF::GetPluginsDir() const
189 {
190 return GetFromFunc(CFBundleCopyBuiltInPlugInsURL);
191 }
192
193 wxString wxStandardPathsCF::GetResourcesDir() const
194 {
195 return GetFromFunc(CFBundleCopyResourcesDirectoryURL);
196 }
197
198 wxString
199 wxStandardPathsCF::GetLocalizedResourcesDir(const wxString& lang,
200 ResourceCat category) const
201 {
202 return wxStandardPathsBase::
203 GetLocalizedResourcesDir(lang, category) + _T(".lproj");
204 }
205
206 #endif // wxUSE_STDPATHS