]> git.saurik.com Git - wxWidgets.git/blob - src/mac/corefoundation/utilsexc_base.cpp
Moved wxMacExecute out of src/mac/corefoundation/hid.cpp and into
[wxWidgets.git] / src / mac / corefoundation / utilsexc_base.cpp
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 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16 #ifndef WX_PRECOMP
17 #include "wx/string.h"
18 #include "wx/log.h"
19 #include "wx/intl.h"
20 #include "wx/utils.h"
21 #endif // WX_PRECOMP
22
23
24 #include <CoreFoundation/CoreFoundation.h>
25 #include <ApplicationServices/ApplicationServices.h>
26
27 #include "wx/uri.h"
28 #include "wx/mac/corefoundation/cfstring.h"
29
30 long wxMacExecute(wxChar **argv,
31 int flags,
32 wxProcess *process)
33 {
34 const long errorCode = ((flags & wxEXEC_SYNC) ? -1 : 0);
35 const long successCode = ((flags & wxEXEC_SYNC) ? 0 : -1); // fake PID
36
37 CFIndex cfiCount = 0;
38 //get count
39 for(wxChar** argvcopy = argv; *argvcopy != NULL ; ++argvcopy)
40 {
41 ++cfiCount;
42 }
43
44 if(cfiCount == 0) //no file to launch?
45 {
46 wxLogDebug(wxT("wxMacExecute No file to launch!"));
47 return errorCode ;
48 }
49
50 CFURLRef cfurlApp = CFURLCreateWithString(
51 kCFAllocatorDefault,
52 wxMacCFStringHolder(*argv++, wxLocale::GetSystemEncoding()),
53 NULL);
54 wxASSERT(cfurlApp);
55
56 CFBundleRef cfbApp = CFBundleCreate(kCFAllocatorDefault, cfurlApp);
57 if(!cfbApp)
58 {
59 wxLogDebug(wxT("wxMacExecute Bad bundle"));
60 CFRelease(cfurlApp);
61 return errorCode ;
62 }
63
64
65 UInt32 dwBundleType, dwBundleCreator;
66 CFBundleGetPackageInfo(cfbApp, &dwBundleType, &dwBundleCreator);
67
68 //Only call wxMacExecute for .app bundles - others could be actual unix programs
69 if(dwBundleType != 'APPL')
70 {
71 CFRelease(cfurlApp);
72 return errorCode ;
73 }
74
75 //
76 // We have a good bundle - so let's launch it!
77 //
78
79 CFMutableArrayRef cfaFiles =
80 CFArrayCreateMutable(kCFAllocatorDefault, cfiCount - 1, &kCFTypeArrayCallBacks);
81
82 wxASSERT(cfaFiles);
83
84 if(--cfiCount)
85 {
86 for( ; *argv != NULL ; ++argv)
87 {
88 // wxLogDebug(*argv);
89 wxString sCurrentFile;
90
91 if(wxURI(*argv).IsReference())
92 sCurrentFile = wxString(wxT("file://")) + *argv;
93 else
94 sCurrentFile = *argv;
95
96 CFURLRef cfurlCurrentFile = CFURLCreateWithString(
97 kCFAllocatorDefault,
98 wxMacCFStringHolder(sCurrentFile, wxLocale::GetSystemEncoding()),
99 NULL);
100 wxASSERT(cfurlCurrentFile);
101
102 CFArrayAppendValue(
103 cfaFiles,
104 cfurlCurrentFile
105 );
106 CFRelease(cfurlCurrentFile); // array has retained it
107 }
108 }
109
110 LSLaunchURLSpec launchspec;
111 launchspec.appURL = cfurlApp;
112 launchspec.itemURLs = cfaFiles;
113 launchspec.passThruParams = NULL; //AEDesc*
114 launchspec.launchFlags = kLSLaunchDefaults | kLSLaunchDontSwitch; //TODO: Possibly be smarter with flags
115 launchspec.asyncRefCon = NULL;
116
117 OSStatus status = LSOpenFromURLSpec(&launchspec,
118 NULL); //2nd is CFURLRef* really launched
119
120 //cleanup
121 CFRelease(cfurlApp);
122 CFRelease(cfaFiles);
123
124 //check for error
125 if(status != noErr)
126 {
127 wxLogDebug(wxT("wxMacExecute ERROR: %d"), (int)status);
128 return errorCode ;
129 }
130 return successCode; //success
131 }
132