]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/corefoundation/stdpaths_cf.cpp
fix null-deref
[wxWidgets.git] / src / mac / corefoundation / stdpaths_cf.cpp
... / ...
CommitLineData
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#include "wx/mac/corefoundation/cfstring.h"
31
32#if defined(__DARWIN__)
33#include <CoreFoundation/CFBundle.h>
34#include <CoreFoundation/CFURL.h>
35#else
36#include <CFBundle.h>
37#include <CFURL.h>
38#endif
39
40#if defined(__WXCOCOA__) || defined(__WXMAC_OSX__)
41#define kDefaultPathStyle kCFURLPOSIXPathStyle
42#else
43#define kDefaultPathStyle kCFURLHFSPathStyle
44#endif
45
46// ============================================================================
47// implementation
48// ============================================================================
49
50// ----------------------------------------------------------------------------
51// wxStandardPathsCF ctors/dtor
52// ----------------------------------------------------------------------------
53
54wxStandardPathsCF::wxStandardPathsCF()
55 : m_bundle(CFBundleGetMainBundle())
56{
57 CFRetain(m_bundle);
58}
59
60wxStandardPathsCF::wxStandardPathsCF(wxCFBundleRef bundle)
61 : m_bundle(bundle)
62{
63 CFRetain(m_bundle);
64}
65
66wxStandardPathsCF::~wxStandardPathsCF()
67{
68 CFRelease(m_bundle);
69}
70
71// ----------------------------------------------------------------------------
72// wxStandardPathsCF Mac-specific methods
73// ----------------------------------------------------------------------------
74
75void wxStandardPathsCF::SetBundle(wxCFBundleRef bundle)
76{
77 CFRetain(bundle);
78 CFRelease(m_bundle);
79 m_bundle = bundle;
80}
81
82// ----------------------------------------------------------------------------
83// generic functions in terms of which the other ones are implemented
84// ----------------------------------------------------------------------------
85
86static wxString BundleRelativeURLToPath(CFURLRef relativeURL)
87{
88 CFURLRef absoluteURL = CFURLCopyAbsoluteURL(relativeURL);
89 wxCHECK_MSG(absoluteURL, wxEmptyString, wxT("Failed to resolve relative URL to absolute URL"));
90 CFStringRef cfStrPath = CFURLCopyFileSystemPath(absoluteURL,kDefaultPathStyle);
91 CFRelease(absoluteURL);
92 return wxMacCFStringHolder(cfStrPath).AsString(wxLocale::GetSystemEncoding());
93}
94
95wxString wxStandardPathsCF::GetFromFunc(wxCFURLRef (*func)(wxCFBundleRef)) const
96{
97 wxCHECK_MSG(m_bundle, wxEmptyString,
98 wxT("wxStandardPaths for CoreFoundation only works with bundled apps"));
99 CFURLRef relativeURL = (*func)(m_bundle);
100 wxCHECK_MSG(relativeURL, wxEmptyString, wxT("Couldn't get URL"));
101 wxString ret(BundleRelativeURLToPath(relativeURL));
102 CFRelease(relativeURL);
103 return ret;
104}
105
106// ----------------------------------------------------------------------------
107// wxStandardPathsCF public API
108// ----------------------------------------------------------------------------
109
110wxString wxStandardPathsCF::GetConfigDir() const
111{
112 // TODO: What do we do for pure Carbon?
113 return wxT("/Library/Preferences");
114}
115
116wxString wxStandardPathsCF::GetUserConfigDir() const
117{
118 // TODO: What do we do for pure Carbon?
119 return wxFileName::GetHomeDir() + wxT("/Library/Preferences");
120}
121
122wxString wxStandardPathsCF::GetDataDir() const
123{
124 return GetFromFunc(CFBundleCopySharedSupportURL);
125}
126
127wxString wxStandardPathsCF::GetLocalDataDir() const
128{
129 return AppendAppName(wxT("/Library/Application Support"));
130}
131
132wxString wxStandardPathsCF::GetUserDataDir() const
133{
134 return AppendAppName(wxFileName::GetHomeDir() + _T("/Library/Application Support"));
135}
136
137wxString wxStandardPathsCF::GetPluginsDir() const
138{
139 return GetFromFunc(CFBundleCopyBuiltInPlugInsURL);
140}
141
142wxString wxStandardPathsCF::GetResourcesDir() const
143{
144 return GetFromFunc(CFBundleCopyResourcesDirectoryURL);
145}
146
147wxString
148wxStandardPathsCF::GetLocalizedResourcesDir(const wxChar *lang,
149 ResourceCat category) const
150{
151 return wxStandardPathsBase::
152 GetLocalizedResourcesDir(lang, category) + _T(".lproj");
153}
154
155#endif // wxUSE_STDPATHS