diff options
Diffstat (limited to 'Mac/Modules/launch/_Launchmodule.c')
-rw-r--r-- | Mac/Modules/launch/_Launchmodule.c | 401 |
1 files changed, 401 insertions, 0 deletions
diff --git a/Mac/Modules/launch/_Launchmodule.c b/Mac/Modules/launch/_Launchmodule.c new file mode 100644 index 0000000..165e45c --- /dev/null +++ b/Mac/Modules/launch/_Launchmodule.c @@ -0,0 +1,401 @@ + +/* ========================= Module _Launch ========================= */ + +#include "Python.h" + + + +#include "pymactoolbox.h" + +/* Macro to test whether a weak-loaded CFM function exists */ +#define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL ) {\ + PyErr_SetString(PyExc_NotImplementedError, \ + "Not available in this shared library/OS version"); \ + return NULL; \ + }} while(0) + + +#include <ApplicationServices/ApplicationServices.h> + +/* +** Optional CFStringRef. None will pass NULL +*/ +static int +OptCFStringRefObj_Convert(PyObject *v, CFStringRef *spec) +{ + if (v == Py_None) { + *spec = NULL; + return 1; + } + return CFStringRefObj_Convert(v, spec); +} + +PyObject * +OptCFStringRefObj_New(CFStringRef it) +{ + if (it == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + return CFStringRefObj_New(it); +} + +/* +** Convert LSItemInfoRecord to Python. +*/ +PyObject * +LSItemInfoRecord_New(LSItemInfoRecord *it) +{ + return Py_BuildValue("{s:is:O&s:O&s:O&s:O&s:i}", + "flags", it->flags, + "filetype", PyMac_BuildOSType, it->filetype, + "creator", PyMac_BuildOSType, it->creator, + "extension", OptCFStringRefObj_New, it->extension, + "iconFileName", OptCFStringRefObj_New, it->iconFileName, + "kindID", it->kindID); +} + +static PyObject *Launch_Error; + +static PyObject *Launch_LSInit(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + LSInitializeFlags inFlags; + if (!PyArg_ParseTuple(_args, "l", + &inFlags)) + return NULL; + _err = LSInit(inFlags); + if (_err != noErr) return PyMac_Error(_err); + Py_INCREF(Py_None); + _res = Py_None; + return _res; +} + +static PyObject *Launch_LSTerm(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + if (!PyArg_ParseTuple(_args, "")) + return NULL; + _err = LSTerm(); + if (_err != noErr) return PyMac_Error(_err); + Py_INCREF(Py_None); + _res = Py_None; + return _res; +} + +static PyObject *Launch_LSCopyItemInfoForRef(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + FSRef inItemRef; + LSRequestedInfo inWhichInfo; + LSItemInfoRecord outItemInfo; + if (!PyArg_ParseTuple(_args, "O&l", + PyMac_GetFSRef, &inItemRef, + &inWhichInfo)) + return NULL; + _err = LSCopyItemInfoForRef(&inItemRef, + inWhichInfo, + &outItemInfo); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&", + LSItemInfoRecord_New, &outItemInfo); + return _res; +} + +static PyObject *Launch_LSCopyItemInfoForURL(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + CFURLRef inURL; + LSRequestedInfo inWhichInfo; + LSItemInfoRecord outItemInfo; + if (!PyArg_ParseTuple(_args, "O&l", + CFURLRefObj_Convert, &inURL, + &inWhichInfo)) + return NULL; + _err = LSCopyItemInfoForURL(inURL, + inWhichInfo, + &outItemInfo); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&", + LSItemInfoRecord_New, &outItemInfo); + return _res; +} + +static PyObject *Launch_LSCopyKindStringForRef(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + FSRef inFSRef; + CFStringRef outKindString; + if (!PyArg_ParseTuple(_args, "O&", + PyMac_GetFSRef, &inFSRef)) + return NULL; + _err = LSCopyKindStringForRef(&inFSRef, + &outKindString); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&", + CFStringRefObj_New, outKindString); + return _res; +} + +static PyObject *Launch_LSCopyKindStringForURL(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + CFURLRef inURL; + CFStringRef outKindString; + if (!PyArg_ParseTuple(_args, "O&", + CFURLRefObj_Convert, &inURL)) + return NULL; + _err = LSCopyKindStringForURL(inURL, + &outKindString); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&", + CFStringRefObj_New, outKindString); + return _res; +} + +static PyObject *Launch_LSGetApplicationForItem(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + FSRef inItemRef; + LSRolesMask inRoleMask; + FSRef outAppRef; + CFURLRef outAppURL; + if (!PyArg_ParseTuple(_args, "O&l", + PyMac_GetFSRef, &inItemRef, + &inRoleMask)) + return NULL; + _err = LSGetApplicationForItem(&inItemRef, + inRoleMask, + &outAppRef, + &outAppURL); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&O&", + PyMac_BuildFSRef, &outAppRef, + CFURLRefObj_New, outAppURL); + return _res; +} + +static PyObject *Launch_LSGetApplicationForInfo(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + OSType inType; + OSType inCreator; + CFStringRef inExtension; + LSRolesMask inRoleMask; + FSRef outAppRef; + CFURLRef outAppURL; + if (!PyArg_ParseTuple(_args, "O&O&O&l", + PyMac_GetOSType, &inType, + PyMac_GetOSType, &inCreator, + OptCFStringRefObj_Convert, &inExtension, + &inRoleMask)) + return NULL; + _err = LSGetApplicationForInfo(inType, + inCreator, + inExtension, + inRoleMask, + &outAppRef, + &outAppURL); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&O&", + PyMac_BuildFSRef, &outAppRef, + CFURLRefObj_New, outAppURL); + return _res; +} + +static PyObject *Launch_LSGetApplicationForURL(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + CFURLRef inURL; + LSRolesMask inRoleMask; + FSRef outAppRef; + CFURLRef outAppURL; + if (!PyArg_ParseTuple(_args, "O&l", + CFURLRefObj_Convert, &inURL, + &inRoleMask)) + return NULL; + _err = LSGetApplicationForURL(inURL, + inRoleMask, + &outAppRef, + &outAppURL); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&O&", + PyMac_BuildFSRef, &outAppRef, + CFURLRefObj_New, outAppURL); + return _res; +} + +static PyObject *Launch_LSFindApplicationForInfo(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + OSType inCreator; + CFStringRef inBundleID; + CFStringRef inName; + FSRef outAppRef; + CFURLRef outAppURL; + if (!PyArg_ParseTuple(_args, "O&O&O&", + PyMac_GetOSType, &inCreator, + OptCFStringRefObj_Convert, &inBundleID, + OptCFStringRefObj_Convert, &inName)) + return NULL; + _err = LSFindApplicationForInfo(inCreator, + inBundleID, + inName, + &outAppRef, + &outAppURL); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&O&", + PyMac_BuildFSRef, &outAppRef, + CFURLRefObj_New, outAppURL); + return _res; +} + +static PyObject *Launch_LSCanRefAcceptItem(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + FSRef inItemFSRef; + FSRef inTargetRef; + LSRolesMask inRoleMask; + LSAcceptanceFlags inFlags; + Boolean outAcceptsItem; + if (!PyArg_ParseTuple(_args, "O&O&ll", + PyMac_GetFSRef, &inItemFSRef, + PyMac_GetFSRef, &inTargetRef, + &inRoleMask, + &inFlags)) + return NULL; + _err = LSCanRefAcceptItem(&inItemFSRef, + &inTargetRef, + inRoleMask, + inFlags, + &outAcceptsItem); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("b", + outAcceptsItem); + return _res; +} + +static PyObject *Launch_LSCanURLAcceptURL(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + CFURLRef inItemURL; + CFURLRef inTargetURL; + LSRolesMask inRoleMask; + LSAcceptanceFlags inFlags; + Boolean outAcceptsItem; + if (!PyArg_ParseTuple(_args, "O&O&ll", + CFURLRefObj_Convert, &inItemURL, + CFURLRefObj_Convert, &inTargetURL, + &inRoleMask, + &inFlags)) + return NULL; + _err = LSCanURLAcceptURL(inItemURL, + inTargetURL, + inRoleMask, + inFlags, + &outAcceptsItem); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("b", + outAcceptsItem); + return _res; +} + +static PyObject *Launch_LSOpenFSRef(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + FSRef inRef; + FSRef outLaunchedRef; + if (!PyArg_ParseTuple(_args, "O&", + PyMac_GetFSRef, &inRef)) + return NULL; + _err = LSOpenFSRef(&inRef, + &outLaunchedRef); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&", + PyMac_BuildFSRef, &outLaunchedRef); + return _res; +} + +static PyObject *Launch_LSOpenCFURLRef(PyObject *_self, PyObject *_args) +{ + PyObject *_res = NULL; + OSStatus _err; + CFURLRef inURL; + CFURLRef outLaunchedURL; + if (!PyArg_ParseTuple(_args, "O&", + CFURLRefObj_Convert, &inURL)) + return NULL; + _err = LSOpenCFURLRef(inURL, + &outLaunchedURL); + if (_err != noErr) return PyMac_Error(_err); + _res = Py_BuildValue("O&", + CFURLRefObj_New, outLaunchedURL); + return _res; +} + +static PyMethodDef Launch_methods[] = { + {"LSInit", (PyCFunction)Launch_LSInit, 1, + PyDoc_STR("(LSInitializeFlags inFlags) -> None")}, + {"LSTerm", (PyCFunction)Launch_LSTerm, 1, + PyDoc_STR("() -> None")}, + {"LSCopyItemInfoForRef", (PyCFunction)Launch_LSCopyItemInfoForRef, 1, + PyDoc_STR("(FSRef inItemRef, LSRequestedInfo inWhichInfo) -> (LSItemInfoRecord outItemInfo)")}, + {"LSCopyItemInfoForURL", (PyCFunction)Launch_LSCopyItemInfoForURL, 1, + PyDoc_STR("(CFURLRef inURL, LSRequestedInfo inWhichInfo) -> (LSItemInfoRecord outItemInfo)")}, + {"LSCopyKindStringForRef", (PyCFunction)Launch_LSCopyKindStringForRef, 1, + PyDoc_STR("(FSRef inFSRef) -> (CFStringRef outKindString)")}, + {"LSCopyKindStringForURL", (PyCFunction)Launch_LSCopyKindStringForURL, 1, + PyDoc_STR("(CFURLRef inURL) -> (CFStringRef outKindString)")}, + {"LSGetApplicationForItem", (PyCFunction)Launch_LSGetApplicationForItem, 1, + PyDoc_STR("(FSRef inItemRef, LSRolesMask inRoleMask) -> (FSRef outAppRef, CFURLRef outAppURL)")}, + {"LSGetApplicationForInfo", (PyCFunction)Launch_LSGetApplicationForInfo, 1, + PyDoc_STR("(OSType inType, OSType inCreator, CFStringRef inExtension, LSRolesMask inRoleMask) -> (FSRef outAppRef, CFURLRef outAppURL)")}, + {"LSGetApplicationForURL", (PyCFunction)Launch_LSGetApplicationForURL, 1, + PyDoc_STR("(CFURLRef inURL, LSRolesMask inRoleMask) -> (FSRef outAppRef, CFURLRef outAppURL)")}, + {"LSFindApplicationForInfo", (PyCFunction)Launch_LSFindApplicationForInfo, 1, + PyDoc_STR("(OSType inCreator, CFStringRef inBundleID, CFStringRef inName) -> (FSRef outAppRef, CFURLRef outAppURL)")}, + {"LSCanRefAcceptItem", (PyCFunction)Launch_LSCanRefAcceptItem, 1, + PyDoc_STR("(FSRef inItemFSRef, FSRef inTargetRef, LSRolesMask inRoleMask, LSAcceptanceFlags inFlags) -> (Boolean outAcceptsItem)")}, + {"LSCanURLAcceptURL", (PyCFunction)Launch_LSCanURLAcceptURL, 1, + PyDoc_STR("(CFURLRef inItemURL, CFURLRef inTargetURL, LSRolesMask inRoleMask, LSAcceptanceFlags inFlags) -> (Boolean outAcceptsItem)")}, + {"LSOpenFSRef", (PyCFunction)Launch_LSOpenFSRef, 1, + PyDoc_STR("(FSRef inRef) -> (FSRef outLaunchedRef)")}, + {"LSOpenCFURLRef", (PyCFunction)Launch_LSOpenCFURLRef, 1, + PyDoc_STR("(CFURLRef inURL) -> (CFURLRef outLaunchedURL)")}, + {NULL, NULL, 0} +}; + + + + +void init_Launch(void) +{ + PyObject *m; + PyObject *d; + + + + + m = Py_InitModule("_Launch", Launch_methods); + d = PyModule_GetDict(m); + Launch_Error = PyMac_GetOSErrException(); + if (Launch_Error == NULL || + PyDict_SetItemString(d, "Error", Launch_Error) != 0) + return; +} + +/* ======================= End module _Launch ======================= */ + |