]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: mac/corefoundation/utilsexc_base.cpp | |
3 | // Purpose: wxMacExecute | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 2005-06-21 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Ryan Norton | |
9 | // Licence: wxWindows licence | |
10 | // Notes: Source was originally in utilsexc_cf.cpp,1.6 then moved | |
11 | // to totally unrelated hid.cpp,1.8. | |
12 | ///////////////////////////////////////////////////////////////////////////// | |
13 | ||
14 | //=========================================================================== | |
15 | // DECLARATIONS | |
16 | //=========================================================================== | |
17 | ||
18 | //--------------------------------------------------------------------------- | |
19 | // Pre-compiled header stuff | |
20 | //--------------------------------------------------------------------------- | |
21 | ||
22 | // For compilers that support precompilation, includes "wx.h". | |
23 | #include "wx/wxprec.h" | |
24 | ||
25 | // WX includes | |
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/string.h" | |
28 | #include "wx/log.h" | |
29 | #include "wx/intl.h" | |
30 | #include "wx/utils.h" | |
31 | #include "wx/wxcrt.h" | |
32 | #endif // WX_PRECOMP | |
33 | ||
34 | // Mac Includes | |
35 | #include <CoreFoundation/CoreFoundation.h> | |
36 | #include <ApplicationServices/ApplicationServices.h> | |
37 | ||
38 | // More WX Includes | |
39 | #include "wx/filename.h" | |
40 | #include "wx/mac/corefoundation/cfstring.h" | |
41 | ||
42 | // Default path style | |
43 | #define kDefaultPathStyle kCFURLPOSIXPathStyle | |
44 | ||
45 | //=========================================================================== | |
46 | // IMPLEMENTATION | |
47 | //=========================================================================== | |
48 | ||
49 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
50 | // | |
51 | // wxMacExecute | |
52 | // | |
53 | // argv is the command line split up, with the application path first | |
54 | // flags are the flags from wxExecute | |
55 | // process is the process passed from wxExecute for pipe streams etc. | |
56 | // returns -1 on error for wxEXEC_SYNC and 0 on error for wxEXEC_ASYNC | |
57 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
58 | long wxMacExecute(wxChar **argv, | |
59 | int flags, | |
60 | wxProcess *WXUNUSED(process)) | |
61 | { | |
62 | // Semi-macros used for return value of wxMacExecute | |
63 | const long errorCode = ((flags & wxEXEC_SYNC) ? -1 : 0); | |
64 | const long successCode = ((flags & wxEXEC_SYNC) ? 0 : -1); // fake PID | |
65 | ||
66 | // Obtains the number of arguments for determining the size of | |
67 | // the CFArray used to hold them | |
68 | CFIndex cfiCount = 0; | |
69 | for(wxChar** argvcopy = argv; *argvcopy != NULL ; ++argvcopy) | |
70 | { | |
71 | ++cfiCount; | |
72 | } | |
73 | ||
74 | // If there is not a single argument then there is no application | |
75 | // to launch | |
76 | if(cfiCount == 0) | |
77 | { | |
78 | wxLogDebug(wxT("wxMacExecute No file to launch!")); | |
79 | return errorCode ; | |
80 | } | |
81 | ||
82 | // Path to bundle | |
83 | wxString path = *argv++; | |
84 | ||
85 | // Create a CFURL for the application path | |
86 | // Created this way because we are opening a bundle which is a directory | |
87 | CFURLRef cfurlApp = | |
88 | CFURLCreateWithFileSystemPath( | |
89 | kCFAllocatorDefault, | |
90 | wxCFStringRef(path), | |
91 | kDefaultPathStyle, | |
92 | true); //false == not a directory | |
93 | ||
94 | // Check for error from the CFURL | |
95 | if(!cfurlApp) | |
96 | { | |
97 | wxLogDebug(wxT("wxMacExecute Can't open path: %s"), path.c_str()); | |
98 | return errorCode ; | |
99 | } | |
100 | ||
101 | // Create a CFBundle from the CFURL created earlier | |
102 | CFBundleRef cfbApp = CFBundleCreate(kCFAllocatorDefault, cfurlApp); | |
103 | ||
104 | // Check to see if CFBundleCreate returned an error, | |
105 | // and if it did this was an invalid bundle or not a bundle | |
106 | // at all (maybe a simple directory etc.) | |
107 | if(!cfbApp) | |
108 | { | |
109 | wxLogDebug(wxT("wxMacExecute Bad bundle: %s"), path.c_str()); | |
110 | CFRelease(cfurlApp); | |
111 | return errorCode ; | |
112 | } | |
113 | ||
114 | // Get the bundle type and make sure its an 'APPL' bundle | |
115 | // Otherwise we're dealing with something else here... | |
116 | UInt32 dwBundleType, dwBundleCreator; | |
117 | CFBundleGetPackageInfo(cfbApp, &dwBundleType, &dwBundleCreator); | |
118 | if(dwBundleType != 'APPL') | |
119 | { | |
120 | wxLogDebug(wxT("wxMacExecute Not an APPL bundle: %s"), path.c_str()); | |
121 | CFRelease(cfbApp); | |
122 | CFRelease(cfurlApp); | |
123 | return errorCode ; | |
124 | } | |
125 | ||
126 | // Create a CFArray for dealing with the command line | |
127 | // arguments to the bundle | |
128 | CFMutableArrayRef cfaFiles = CFArrayCreateMutable(kCFAllocatorDefault, | |
129 | cfiCount-1, &kCFTypeArrayCallBacks); | |
130 | if(!cfaFiles) //This should never happen | |
131 | { | |
132 | wxLogDebug(wxT("wxMacExecute Could not create CFMutableArray")); | |
133 | CFRelease(cfbApp); | |
134 | CFRelease(cfurlApp); | |
135 | return errorCode ; | |
136 | } | |
137 | ||
138 | // Loop through command line arguments to the bundle, | |
139 | // turn them into CFURLs and then put them in cfaFiles | |
140 | // For use to launch services call | |
141 | for( ; *argv != NULL ; ++argv) | |
142 | { | |
143 | // Check for '<' as this will ring true for | |
144 | // CFURLCreateWithString but is generally not considered | |
145 | // typical on mac but is usually passed here from wxExecute | |
146 | if (wxStrcmp(*argv, wxT("<")) == 0) | |
147 | continue; | |
148 | ||
149 | ||
150 | CFURLRef cfurlCurrentFile; // CFURL to hold file path | |
151 | wxFileName argfn(*argv); // Filename for path | |
152 | ||
153 | if(argfn.DirExists()) | |
154 | { | |
155 | // First, try creating as a directory | |
156 | cfurlCurrentFile = CFURLCreateWithFileSystemPath( | |
157 | kCFAllocatorDefault, | |
158 | wxCFStringRef(*argv), | |
159 | kDefaultPathStyle, | |
160 | true); //true == directory | |
161 | } | |
162 | else if(argfn.FileExists()) | |
163 | { | |
164 | // And if it isn't a directory try creating it | |
165 | // as a regular file | |
166 | cfurlCurrentFile = CFURLCreateWithFileSystemPath( | |
167 | kCFAllocatorDefault, | |
168 | wxCFStringRef(*argv), | |
169 | kDefaultPathStyle, | |
170 | false); //false == regular file | |
171 | } | |
172 | else | |
173 | { | |
174 | // Argument did not refer to | |
175 | // an entry in the local filesystem, | |
176 | // so try creating it through CFURLCreateWithString | |
177 | cfurlCurrentFile = CFURLCreateWithString( | |
178 | kCFAllocatorDefault, | |
179 | wxCFStringRef(*argv), | |
180 | NULL); | |
181 | } | |
182 | ||
183 | // Continue in the loop if the CFURL could not be created | |
184 | if(!cfurlCurrentFile) | |
185 | { | |
186 | wxLogDebug( | |
187 | wxT("wxMacExecute Could not create CFURL for argument:%s"), | |
188 | *argv); | |
189 | continue; | |
190 | } | |
191 | ||
192 | // Add the valid CFURL to the argument array and then | |
193 | // release it as the CFArray adds a ref count to it | |
194 | CFArrayAppendValue( | |
195 | cfaFiles, | |
196 | cfurlCurrentFile | |
197 | ); | |
198 | CFRelease(cfurlCurrentFile); // array has retained it | |
199 | } | |
200 | ||
201 | // Create a LSLaunchURLSpec for use with LSOpenFromURLSpec | |
202 | // Note that there are several flag options (launchFlags) such | |
203 | // as kLSLaunchDontSwitch etc. and maybe we could be more | |
204 | // picky about the flags we choose | |
205 | LSLaunchURLSpec launchspec; | |
206 | launchspec.appURL = cfurlApp; | |
207 | launchspec.itemURLs = cfaFiles; | |
208 | launchspec.passThruParams = NULL; //AEDesc* | |
209 | launchspec.launchFlags = kLSLaunchDefaults; | |
210 | launchspec.asyncRefCon = NULL; | |
211 | ||
212 | // Finally, call LSOpenFromURL spec with our arguments | |
213 | // 2nd parameter is a pointer to a CFURL that gets | |
214 | // the actual path launched by the function | |
215 | OSStatus status = LSOpenFromURLSpec(&launchspec, NULL); | |
216 | ||
217 | // Cleanup corefoundation references | |
218 | CFRelease(cfbApp); | |
219 | CFRelease(cfurlApp); | |
220 | CFRelease(cfaFiles); | |
221 | ||
222 | // Check for error from LSOpenFromURLSpec | |
223 | if(status != noErr) | |
224 | { | |
225 | wxLogDebug(wxT("wxMacExecute LSOpenFromURLSpec Error: %d"), | |
226 | (int)status); | |
227 | return errorCode ; | |
228 | } | |
229 | ||
230 | // No error from LSOpenFromURLSpec, so app was launched | |
231 | return successCode; | |
232 | } | |
233 |