1 
2 /* ========================= Module _Launch ========================= */
3 
4 #include "Python.h"
5 
6 
7 
8 #include "pymactoolbox.h"
9 
10 /* Macro to test whether a weak-loaded CFM function exists */
11 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL )  {\
12         PyErr_SetString(PyExc_NotImplementedError, \
13             "Not available in this shared library/OS version"); \
14         return NULL; \
15     }} while(0)
16 
17 
18 #if PY_VERSION_HEX < 0x02040000
19 PyObject *PyMac_GetOSErrException(void);
20 #endif
21 
22 #include <ApplicationServices/ApplicationServices.h>
23 
24 /*
25 ** Optional CFStringRef. None will pass NULL
26 */
27 static int
OptCFStringRefObj_Convert(PyObject * v,CFStringRef * spec)28 OptCFStringRefObj_Convert(PyObject *v, CFStringRef *spec)
29 {
30     if (v == Py_None) {
31         *spec = NULL;
32         return 1;
33     }
34     return CFStringRefObj_Convert(v, spec);
35 }
36 
37 PyObject *
OptCFStringRefObj_New(CFStringRef it)38 OptCFStringRefObj_New(CFStringRef it)
39 {
40     if (it == NULL) {
41         Py_INCREF(Py_None);
42         return Py_None;
43     }
44     return CFStringRefObj_New(it);
45 }
46 
47 /*
48 ** Convert LSItemInfoRecord to Python.
49 */
50 PyObject *
LSItemInfoRecord_New(LSItemInfoRecord * it)51 LSItemInfoRecord_New(LSItemInfoRecord *it)
52 {
53 #ifndef __LP64__
54     return Py_BuildValue("{s:is:O&s:O&s:O&s:O&s:i}",
55         "flags", it->flags,
56         "filetype", PyMac_BuildOSType, it->filetype,
57         "creator", PyMac_BuildOSType, it->creator,
58         "extension", OptCFStringRefObj_New, it->extension,
59         "iconFileName", OptCFStringRefObj_New, it->iconFileName,
60         "kindID", it->kindID);
61 #else
62     return Py_BuildValue("{s:is:O&s:O&s:O&}",
63         "flags", it->flags,
64         "filetype", PyMac_BuildOSType, it->filetype,
65         "creator", PyMac_BuildOSType, it->creator,
66         "extension", OptCFStringRefObj_New, it->extension);
67 #endif
68 }
69 
70 static PyObject *Launch_Error;
71 
Launch_LSCopyItemInfoForRef(PyObject * _self,PyObject * _args)72 static PyObject *Launch_LSCopyItemInfoForRef(PyObject *_self, PyObject *_args)
73 {
74     PyObject *_res = NULL;
75     OSStatus _err;
76     FSRef inItemRef;
77     LSRequestedInfo inWhichInfo;
78     LSItemInfoRecord outItemInfo;
79     if (!PyArg_ParseTuple(_args, "O&l",
80                           PyMac_GetFSRef, &inItemRef,
81                           &inWhichInfo))
82         return NULL;
83     _err = LSCopyItemInfoForRef(&inItemRef,
84                                 inWhichInfo,
85                                 &outItemInfo);
86     if (_err != noErr) return PyMac_Error(_err);
87     _res = Py_BuildValue("O&",
88                          LSItemInfoRecord_New, &outItemInfo);
89     return _res;
90 }
91 
Launch_LSCopyItemInfoForURL(PyObject * _self,PyObject * _args)92 static PyObject *Launch_LSCopyItemInfoForURL(PyObject *_self, PyObject *_args)
93 {
94     PyObject *_res = NULL;
95     OSStatus _err;
96     CFURLRef inURL;
97     LSRequestedInfo inWhichInfo;
98     LSItemInfoRecord outItemInfo;
99     if (!PyArg_ParseTuple(_args, "O&l",
100                           CFURLRefObj_Convert, &inURL,
101                           &inWhichInfo))
102         return NULL;
103     _err = LSCopyItemInfoForURL(inURL,
104                                 inWhichInfo,
105                                 &outItemInfo);
106     if (_err != noErr) return PyMac_Error(_err);
107     _res = Py_BuildValue("O&",
108                          LSItemInfoRecord_New, &outItemInfo);
109     return _res;
110 }
111 
Launch_LSGetExtensionInfo(PyObject * _self,PyObject * _args)112 static PyObject *Launch_LSGetExtensionInfo(PyObject *_self, PyObject *_args)
113 {
114     PyObject *_res = NULL;
115     OSStatus _err;
116     UniChar *inNameLen__in__;
117     UniCharCount inNameLen__len__;
118     int inNameLen__in_len__;
119     UniCharCount outExtStartIndex;
120     if (!PyArg_ParseTuple(_args, "u#",
121                           &inNameLen__in__, &inNameLen__in_len__))
122         return NULL;
123     inNameLen__len__ = inNameLen__in_len__;
124     _err = LSGetExtensionInfo(inNameLen__len__, inNameLen__in__,
125                               &outExtStartIndex);
126     if (_err != noErr) return PyMac_Error(_err);
127     _res = Py_BuildValue("l",
128                          outExtStartIndex);
129     return _res;
130 }
131 
Launch_LSCopyDisplayNameForRef(PyObject * _self,PyObject * _args)132 static PyObject *Launch_LSCopyDisplayNameForRef(PyObject *_self, PyObject *_args)
133 {
134     PyObject *_res = NULL;
135     OSStatus _err;
136     FSRef inRef;
137     CFStringRef outDisplayName;
138     if (!PyArg_ParseTuple(_args, "O&",
139                           PyMac_GetFSRef, &inRef))
140         return NULL;
141     _err = LSCopyDisplayNameForRef(&inRef,
142                                    &outDisplayName);
143     if (_err != noErr) return PyMac_Error(_err);
144     _res = Py_BuildValue("O&",
145                          CFStringRefObj_New, outDisplayName);
146     return _res;
147 }
148 
Launch_LSCopyDisplayNameForURL(PyObject * _self,PyObject * _args)149 static PyObject *Launch_LSCopyDisplayNameForURL(PyObject *_self, PyObject *_args)
150 {
151     PyObject *_res = NULL;
152     OSStatus _err;
153     CFURLRef inURL;
154     CFStringRef outDisplayName;
155     if (!PyArg_ParseTuple(_args, "O&",
156                           CFURLRefObj_Convert, &inURL))
157         return NULL;
158     _err = LSCopyDisplayNameForURL(inURL,
159                                    &outDisplayName);
160     if (_err != noErr) return PyMac_Error(_err);
161     _res = Py_BuildValue("O&",
162                          CFStringRefObj_New, outDisplayName);
163     return _res;
164 }
165 
Launch_LSSetExtensionHiddenForRef(PyObject * _self,PyObject * _args)166 static PyObject *Launch_LSSetExtensionHiddenForRef(PyObject *_self, PyObject *_args)
167 {
168     PyObject *_res = NULL;
169     OSStatus _err;
170     FSRef inRef;
171     Boolean inHide;
172     if (!PyArg_ParseTuple(_args, "O&b",
173                           PyMac_GetFSRef, &inRef,
174                           &inHide))
175         return NULL;
176     _err = LSSetExtensionHiddenForRef(&inRef,
177                                       inHide);
178     if (_err != noErr) return PyMac_Error(_err);
179     Py_INCREF(Py_None);
180     _res = Py_None;
181     return _res;
182 }
183 
Launch_LSSetExtensionHiddenForURL(PyObject * _self,PyObject * _args)184 static PyObject *Launch_LSSetExtensionHiddenForURL(PyObject *_self, PyObject *_args)
185 {
186     PyObject *_res = NULL;
187     OSStatus _err;
188     CFURLRef inURL;
189     Boolean inHide;
190     if (!PyArg_ParseTuple(_args, "O&b",
191                           CFURLRefObj_Convert, &inURL,
192                           &inHide))
193         return NULL;
194     _err = LSSetExtensionHiddenForURL(inURL,
195                                       inHide);
196     if (_err != noErr) return PyMac_Error(_err);
197     Py_INCREF(Py_None);
198     _res = Py_None;
199     return _res;
200 }
201 
Launch_LSCopyKindStringForRef(PyObject * _self,PyObject * _args)202 static PyObject *Launch_LSCopyKindStringForRef(PyObject *_self, PyObject *_args)
203 {
204     PyObject *_res = NULL;
205     OSStatus _err;
206     FSRef inFSRef;
207     CFStringRef outKindString;
208     if (!PyArg_ParseTuple(_args, "O&",
209                           PyMac_GetFSRef, &inFSRef))
210         return NULL;
211     _err = LSCopyKindStringForRef(&inFSRef,
212                                   &outKindString);
213     if (_err != noErr) return PyMac_Error(_err);
214     _res = Py_BuildValue("O&",
215                          CFStringRefObj_New, outKindString);
216     return _res;
217 }
218 
Launch_LSCopyKindStringForURL(PyObject * _self,PyObject * _args)219 static PyObject *Launch_LSCopyKindStringForURL(PyObject *_self, PyObject *_args)
220 {
221     PyObject *_res = NULL;
222     OSStatus _err;
223     CFURLRef inURL;
224     CFStringRef outKindString;
225     if (!PyArg_ParseTuple(_args, "O&",
226                           CFURLRefObj_Convert, &inURL))
227         return NULL;
228     _err = LSCopyKindStringForURL(inURL,
229                                   &outKindString);
230     if (_err != noErr) return PyMac_Error(_err);
231     _res = Py_BuildValue("O&",
232                          CFStringRefObj_New, outKindString);
233     return _res;
234 }
235 
Launch_LSGetApplicationForItem(PyObject * _self,PyObject * _args)236 static PyObject *Launch_LSGetApplicationForItem(PyObject *_self, PyObject *_args)
237 {
238     PyObject *_res = NULL;
239     OSStatus _err;
240     FSRef inItemRef;
241     LSRolesMask inRoleMask;
242     FSRef outAppRef;
243     CFURLRef outAppURL;
244     if (!PyArg_ParseTuple(_args, "O&l",
245                           PyMac_GetFSRef, &inItemRef,
246                           &inRoleMask))
247         return NULL;
248     _err = LSGetApplicationForItem(&inItemRef,
249                                    inRoleMask,
250                                    &outAppRef,
251                                    &outAppURL);
252     if (_err != noErr) return PyMac_Error(_err);
253     _res = Py_BuildValue("O&O&",
254                          PyMac_BuildFSRef, &outAppRef,
255                          CFURLRefObj_New, outAppURL);
256     return _res;
257 }
258 
Launch_LSGetApplicationForInfo(PyObject * _self,PyObject * _args)259 static PyObject *Launch_LSGetApplicationForInfo(PyObject *_self, PyObject *_args)
260 {
261     PyObject *_res = NULL;
262     OSStatus _err;
263     OSType inType;
264     OSType inCreator;
265     CFStringRef inExtension;
266     LSRolesMask inRoleMask;
267     FSRef outAppRef;
268     CFURLRef outAppURL;
269     if (!PyArg_ParseTuple(_args, "O&O&O&l",
270                           PyMac_GetOSType, &inType,
271                           PyMac_GetOSType, &inCreator,
272                           OptCFStringRefObj_Convert, &inExtension,
273                           &inRoleMask))
274         return NULL;
275     _err = LSGetApplicationForInfo(inType,
276                                    inCreator,
277                                    inExtension,
278                                    inRoleMask,
279                                    &outAppRef,
280                                    &outAppURL);
281     if (_err != noErr) return PyMac_Error(_err);
282     _res = Py_BuildValue("O&O&",
283                          PyMac_BuildFSRef, &outAppRef,
284                          CFURLRefObj_New, outAppURL);
285     return _res;
286 }
287 
Launch_LSGetApplicationForURL(PyObject * _self,PyObject * _args)288 static PyObject *Launch_LSGetApplicationForURL(PyObject *_self, PyObject *_args)
289 {
290     PyObject *_res = NULL;
291     OSStatus _err;
292     CFURLRef inURL;
293     LSRolesMask inRoleMask;
294     FSRef outAppRef;
295     CFURLRef outAppURL;
296     if (!PyArg_ParseTuple(_args, "O&l",
297                           CFURLRefObj_Convert, &inURL,
298                           &inRoleMask))
299         return NULL;
300     _err = LSGetApplicationForURL(inURL,
301                                   inRoleMask,
302                                   &outAppRef,
303                                   &outAppURL);
304     if (_err != noErr) return PyMac_Error(_err);
305     _res = Py_BuildValue("O&O&",
306                          PyMac_BuildFSRef, &outAppRef,
307                          CFURLRefObj_New, outAppURL);
308     return _res;
309 }
310 
Launch_LSFindApplicationForInfo(PyObject * _self,PyObject * _args)311 static PyObject *Launch_LSFindApplicationForInfo(PyObject *_self, PyObject *_args)
312 {
313     PyObject *_res = NULL;
314     OSStatus _err;
315     OSType inCreator;
316     CFStringRef inBundleID;
317     CFStringRef inName;
318     FSRef outAppRef;
319     CFURLRef outAppURL;
320     if (!PyArg_ParseTuple(_args, "O&O&O&",
321                           PyMac_GetOSType, &inCreator,
322                           OptCFStringRefObj_Convert, &inBundleID,
323                           OptCFStringRefObj_Convert, &inName))
324         return NULL;
325     _err = LSFindApplicationForInfo(inCreator,
326                                     inBundleID,
327                                     inName,
328                                     &outAppRef,
329                                     &outAppURL);
330     if (_err != noErr) return PyMac_Error(_err);
331     _res = Py_BuildValue("O&O&",
332                          PyMac_BuildFSRef, &outAppRef,
333                          CFURLRefObj_New, outAppURL);
334     return _res;
335 }
336 
Launch_LSCanRefAcceptItem(PyObject * _self,PyObject * _args)337 static PyObject *Launch_LSCanRefAcceptItem(PyObject *_self, PyObject *_args)
338 {
339     PyObject *_res = NULL;
340     OSStatus _err;
341     FSRef inItemFSRef;
342     FSRef inTargetRef;
343     LSRolesMask inRoleMask;
344     LSAcceptanceFlags inFlags;
345     Boolean outAcceptsItem;
346     if (!PyArg_ParseTuple(_args, "O&O&ll",
347                           PyMac_GetFSRef, &inItemFSRef,
348                           PyMac_GetFSRef, &inTargetRef,
349                           &inRoleMask,
350                           &inFlags))
351         return NULL;
352     _err = LSCanRefAcceptItem(&inItemFSRef,
353                               &inTargetRef,
354                               inRoleMask,
355                               inFlags,
356                               &outAcceptsItem);
357     if (_err != noErr) return PyMac_Error(_err);
358     _res = Py_BuildValue("b",
359                          outAcceptsItem);
360     return _res;
361 }
362 
Launch_LSCanURLAcceptURL(PyObject * _self,PyObject * _args)363 static PyObject *Launch_LSCanURLAcceptURL(PyObject *_self, PyObject *_args)
364 {
365     PyObject *_res = NULL;
366     OSStatus _err;
367     CFURLRef inItemURL;
368     CFURLRef inTargetURL;
369     LSRolesMask inRoleMask;
370     LSAcceptanceFlags inFlags;
371     Boolean outAcceptsItem;
372     if (!PyArg_ParseTuple(_args, "O&O&ll",
373                           CFURLRefObj_Convert, &inItemURL,
374                           CFURLRefObj_Convert, &inTargetURL,
375                           &inRoleMask,
376                           &inFlags))
377         return NULL;
378     _err = LSCanURLAcceptURL(inItemURL,
379                              inTargetURL,
380                              inRoleMask,
381                              inFlags,
382                              &outAcceptsItem);
383     if (_err != noErr) return PyMac_Error(_err);
384     _res = Py_BuildValue("b",
385                          outAcceptsItem);
386     return _res;
387 }
388 
Launch_LSOpenFSRef(PyObject * _self,PyObject * _args)389 static PyObject *Launch_LSOpenFSRef(PyObject *_self, PyObject *_args)
390 {
391     PyObject *_res = NULL;
392     OSStatus _err;
393     FSRef inRef;
394     FSRef outLaunchedRef;
395     if (!PyArg_ParseTuple(_args, "O&",
396                           PyMac_GetFSRef, &inRef))
397         return NULL;
398     _err = LSOpenFSRef(&inRef,
399                        &outLaunchedRef);
400     if (_err != noErr) return PyMac_Error(_err);
401     _res = Py_BuildValue("O&",
402                          PyMac_BuildFSRef, &outLaunchedRef);
403     return _res;
404 }
405 
Launch_LSOpenCFURLRef(PyObject * _self,PyObject * _args)406 static PyObject *Launch_LSOpenCFURLRef(PyObject *_self, PyObject *_args)
407 {
408     PyObject *_res = NULL;
409     OSStatus _err;
410     CFURLRef inURL;
411     CFURLRef outLaunchedURL;
412     if (!PyArg_ParseTuple(_args, "O&",
413                           CFURLRefObj_Convert, &inURL))
414         return NULL;
415     _err = LSOpenCFURLRef(inURL,
416                           &outLaunchedURL);
417     if (_err != noErr) return PyMac_Error(_err);
418     _res = Py_BuildValue("O&",
419                          CFURLRefObj_New, outLaunchedURL);
420     return _res;
421 }
422 
423 static PyMethodDef Launch_methods[] = {
424     {"LSCopyItemInfoForRef", (PyCFunction)Launch_LSCopyItemInfoForRef, 1,
425      PyDoc_STR("(FSRef inItemRef, LSRequestedInfo inWhichInfo) -> (LSItemInfoRecord outItemInfo)")},
426     {"LSCopyItemInfoForURL", (PyCFunction)Launch_LSCopyItemInfoForURL, 1,
427      PyDoc_STR("(CFURLRef inURL, LSRequestedInfo inWhichInfo) -> (LSItemInfoRecord outItemInfo)")},
428     {"LSGetExtensionInfo", (PyCFunction)Launch_LSGetExtensionInfo, 1,
429      PyDoc_STR("(Buffer inNameLen) -> (UniCharCount outExtStartIndex)")},
430     {"LSCopyDisplayNameForRef", (PyCFunction)Launch_LSCopyDisplayNameForRef, 1,
431      PyDoc_STR("(FSRef inRef) -> (CFStringRef outDisplayName)")},
432     {"LSCopyDisplayNameForURL", (PyCFunction)Launch_LSCopyDisplayNameForURL, 1,
433      PyDoc_STR("(CFURLRef inURL) -> (CFStringRef outDisplayName)")},
434     {"LSSetExtensionHiddenForRef", (PyCFunction)Launch_LSSetExtensionHiddenForRef, 1,
435      PyDoc_STR("(FSRef inRef, Boolean inHide) -> None")},
436     {"LSSetExtensionHiddenForURL", (PyCFunction)Launch_LSSetExtensionHiddenForURL, 1,
437      PyDoc_STR("(CFURLRef inURL, Boolean inHide) -> None")},
438     {"LSCopyKindStringForRef", (PyCFunction)Launch_LSCopyKindStringForRef, 1,
439      PyDoc_STR("(FSRef inFSRef) -> (CFStringRef outKindString)")},
440     {"LSCopyKindStringForURL", (PyCFunction)Launch_LSCopyKindStringForURL, 1,
441      PyDoc_STR("(CFURLRef inURL) -> (CFStringRef outKindString)")},
442     {"LSGetApplicationForItem", (PyCFunction)Launch_LSGetApplicationForItem, 1,
443      PyDoc_STR("(FSRef inItemRef, LSRolesMask inRoleMask) -> (FSRef outAppRef, CFURLRef outAppURL)")},
444     {"LSGetApplicationForInfo", (PyCFunction)Launch_LSGetApplicationForInfo, 1,
445      PyDoc_STR("(OSType inType, OSType inCreator, CFStringRef inExtension, LSRolesMask inRoleMask) -> (FSRef outAppRef, CFURLRef outAppURL)")},
446     {"LSGetApplicationForURL", (PyCFunction)Launch_LSGetApplicationForURL, 1,
447      PyDoc_STR("(CFURLRef inURL, LSRolesMask inRoleMask) -> (FSRef outAppRef, CFURLRef outAppURL)")},
448     {"LSFindApplicationForInfo", (PyCFunction)Launch_LSFindApplicationForInfo, 1,
449      PyDoc_STR("(OSType inCreator, CFStringRef inBundleID, CFStringRef inName) -> (FSRef outAppRef, CFURLRef outAppURL)")},
450     {"LSCanRefAcceptItem", (PyCFunction)Launch_LSCanRefAcceptItem, 1,
451      PyDoc_STR("(FSRef inItemFSRef, FSRef inTargetRef, LSRolesMask inRoleMask, LSAcceptanceFlags inFlags) -> (Boolean outAcceptsItem)")},
452     {"LSCanURLAcceptURL", (PyCFunction)Launch_LSCanURLAcceptURL, 1,
453      PyDoc_STR("(CFURLRef inItemURL, CFURLRef inTargetURL, LSRolesMask inRoleMask, LSAcceptanceFlags inFlags) -> (Boolean outAcceptsItem)")},
454     {"LSOpenFSRef", (PyCFunction)Launch_LSOpenFSRef, 1,
455      PyDoc_STR("(FSRef inRef) -> (FSRef outLaunchedRef)")},
456     {"LSOpenCFURLRef", (PyCFunction)Launch_LSOpenCFURLRef, 1,
457      PyDoc_STR("(CFURLRef inURL) -> (CFURLRef outLaunchedURL)")},
458     {NULL, NULL, 0}
459 };
460 
461 
462 
463 
init_Launch(void)464 void init_Launch(void)
465 {
466     PyObject *m;
467     PyObject *d;
468 
469 
470 
471 
472     m = Py_InitModule("_Launch", Launch_methods);
473     d = PyModule_GetDict(m);
474     Launch_Error = PyMac_GetOSErrException();
475     if (Launch_Error == NULL ||
476         PyDict_SetItemString(d, "Error", Launch_Error) != 0)
477         return;
478 }
479 
480 /* ======================= End module _Launch ======================= */
481 
482