Update for release.
[python/dscho.git] / Mac / Modules / icgluemodule.c
blob56c20a3f0734621b61b8f6b8c241e8574005dff4
1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 #include "Python.h"
33 #include "macglue.h"
35 extern int ResObj_Convert(PyObject *, Handle *); /* From Resmodule.c */
37 #ifdef WITHOUT_FRAMEWORKS
38 #include <InternetConfig.h>
39 #else
40 #include <Carbon/Carbon.h>
41 #endif
43 static PyObject *ErrorObject;
45 /* ----------------------------------------------------- */
47 /* Declarations for objects of type ic_instance */
49 typedef struct {
50 PyObject_HEAD
51 ICInstance inst;
52 } iciobject;
54 static PyTypeObject Icitype;
58 /* ---------------------------------------------------------------- */
61 static char ici_ICGetSeed__doc__[] =
62 "()->int; Returns int that changes when configuration does"
65 static PyObject *
66 ici_ICGetSeed(iciobject *self, PyObject *args)
68 OSStatus err;
69 long seed;
71 if (!PyArg_ParseTuple(args, ""))
72 return NULL;
73 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
74 return PyMac_Error(err);
75 return Py_BuildValue("i", (int)seed);
79 static char ici_ICBegin__doc__[] =
80 "(perm)->None; Lock config file for read/write"
83 static PyObject *
84 ici_ICBegin(iciobject *self, PyObject *args)
86 OSStatus err;
87 int perm;
89 if (!PyArg_ParseTuple(args, "i", &perm))
90 return NULL;
91 if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
92 return PyMac_Error(err);
93 Py_INCREF(Py_None);
94 return Py_None;
98 static char ici_ICFindPrefHandle__doc__[] =
99 "(key, handle)->attrs; Lookup key, store result in handle, return attributes"
102 static PyObject *
103 ici_ICFindPrefHandle(iciobject *self, PyObject *args)
105 OSStatus err;
106 Str255 key;
107 ICAttr attr;
108 Handle h;
110 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
111 return NULL;
112 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
113 return PyMac_Error(err);
114 return Py_BuildValue("i", (int)attr);
118 static char ici_ICSetPref__doc__[] =
119 "(key, attr, data)->None; Set preference key to data with attributes"
122 static PyObject *
123 ici_ICSetPref(iciobject *self, PyObject *args)
125 OSStatus err;
126 Str255 key;
127 int attr;
128 char *data;
129 int datalen;
131 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
132 &data, &datalen))
133 return NULL;
134 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
135 (long)datalen)) != 0)
136 return PyMac_Error(err);
137 Py_INCREF(Py_None);
138 return Py_None;
142 static char ici_ICCountPref__doc__[] =
143 "()->int; Return number of preferences"
146 static PyObject *
147 ici_ICCountPref(iciobject *self, PyObject *args)
149 OSStatus err;
150 long count;
152 if (!PyArg_ParseTuple(args, ""))
153 return NULL;
154 if ((err=ICCountPref(self->inst, &count)) != 0 )
155 return PyMac_Error(err);
156 return Py_BuildValue("i", (int)count);
160 static char ici_ICGetIndPref__doc__[] =
161 "(num)->key; Return key of preference with given index"
164 static PyObject *
165 ici_ICGetIndPref(iciobject *self, PyObject *args)
167 OSStatus err;
168 long num;
169 Str255 key;
171 if (!PyArg_ParseTuple(args, "l", &num))
172 return NULL;
173 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
174 return PyMac_Error(err);
175 return Py_BuildValue("O&", PyMac_BuildStr255, key);
179 static char ici_ICDeletePref__doc__[] =
180 "(key)->None; Delete preference"
183 static PyObject *
184 ici_ICDeletePref(iciobject *self, PyObject *args)
186 OSStatus err;
187 Str255 key;
189 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
190 return NULL;
191 if ((err=ICDeletePref(self->inst, key)) != 0 )
192 return PyMac_Error(err);
193 Py_INCREF(Py_None);
194 return Py_None;
198 static char ici_ICEnd__doc__[] =
199 "()->None; Unlock file after ICBegin call"
202 static PyObject *
203 ici_ICEnd(iciobject *self, PyObject *args)
205 OSStatus err;
207 if (!PyArg_ParseTuple(args, ""))
208 return NULL;
209 if ((err=ICEnd(self->inst)) != 0 )
210 return PyMac_Error(err);
211 Py_INCREF(Py_None);
212 return Py_None;
216 static char ici_ICEditPreferences__doc__[] =
217 "(key)->None; Ask user to edit preferences, staring with key"
220 static PyObject *
221 ici_ICEditPreferences(iciobject *self, PyObject *args)
223 OSStatus err;
224 Str255 key;
226 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
227 return NULL;
228 if ((err=ICEditPreferences(self->inst, key)) != 0 )
229 return PyMac_Error(err);
230 Py_INCREF(Py_None);
231 return Py_None;
235 static char ici_ICParseURL__doc__[] =
236 "(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
239 static PyObject *
240 ici_ICParseURL(iciobject *self, PyObject *args)
242 OSStatus err;
243 Str255 hint;
244 char *data;
245 int datalen;
246 long selStart, selEnd;
247 Handle h;
249 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
250 &selStart, &selEnd, ResObj_Convert, &h))
251 return NULL;
252 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
253 &selStart, &selEnd, h)) != 0 )
254 return PyMac_Error(err);
255 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
259 static char ici_ICLaunchURL__doc__[] =
260 "(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
263 static PyObject *
264 ici_ICLaunchURL(iciobject *self, PyObject *args)
266 OSStatus err;
267 Str255 hint;
268 char *data;
269 int datalen;
270 long selStart, selEnd;
272 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
273 &selStart, &selEnd))
274 return NULL;
275 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
276 &selStart, &selEnd)) != 0 )
277 return PyMac_Error(err);
278 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
282 static char ici_ICMapFilename__doc__[] =
283 "(filename)->mapinfo; Get filemap info for given filename"
286 static PyObject *
287 ici_ICMapFilename(iciobject *self, PyObject *args)
289 OSStatus err;
290 Str255 filename;
291 ICMapEntry entry;
293 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
294 return NULL;
295 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
296 return PyMac_Error(err);
297 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
298 PyMac_BuildOSType, entry.fileType,
299 PyMac_BuildOSType, entry.fileCreator,
300 PyMac_BuildOSType, entry.postCreator,
301 entry.flags,
302 PyMac_BuildStr255, entry.extension,
303 PyMac_BuildStr255, entry.creatorAppName,
304 PyMac_BuildStr255, entry.postAppName,
305 PyMac_BuildStr255, entry.MIMEType,
306 PyMac_BuildStr255, entry.entryName);
310 static char ici_ICMapTypeCreator__doc__[] =
311 "(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
314 static PyObject *
315 ici_ICMapTypeCreator(iciobject *self, PyObject *args)
317 OSStatus err;
318 OSType type, creator;
319 Str255 filename;
320 ICMapEntry entry;
322 if (!PyArg_ParseTuple(args, "O&O&O&",
323 PyMac_GetOSType, &type,
324 PyMac_GetOSType, &creator,
325 PyMac_GetStr255, filename))
326 return NULL;
327 if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
328 return PyMac_Error(err);
329 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
330 PyMac_BuildOSType, entry.fileType,
331 PyMac_BuildOSType, entry.fileCreator,
332 PyMac_BuildOSType, entry.postCreator,
333 entry.flags,
334 PyMac_BuildStr255, entry.extension,
335 PyMac_BuildStr255, entry.creatorAppName,
336 PyMac_BuildStr255, entry.postAppName,
337 PyMac_BuildStr255, entry.MIMEType,
338 PyMac_BuildStr255, entry.entryName);
342 static struct PyMethodDef ici_methods[] = {
343 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
344 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
345 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
346 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
347 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
348 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
349 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
350 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
351 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
352 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
353 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
354 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
355 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
357 {NULL, NULL} /* sentinel */
360 /* ---------- */
363 static iciobject *
364 newiciobject(OSType creator)
366 iciobject *self;
367 OSStatus err;
369 self = PyObject_NEW(iciobject, &Icitype);
370 if (self == NULL)
371 return NULL;
372 if ((err=ICStart(&self->inst, creator)) != 0 ) {
373 (void)PyMac_Error(err);
374 PyObject_DEL(self);
375 return NULL;
377 return self;
381 static void
382 ici_dealloc(iciobject *self)
384 (void)ICStop(self->inst);
385 PyObject_DEL(self);
388 static PyObject *
389 ici_getattr(iciobject *self, char *name)
391 return Py_FindMethod(ici_methods, (PyObject *)self, name);
394 static char Icitype__doc__[] =
395 "Internet Config instance"
398 static PyTypeObject Icitype = {
399 PyObject_HEAD_INIT(&PyType_Type)
400 0, /*ob_size*/
401 "icglue.ic_instance", /*tp_name*/
402 sizeof(iciobject), /*tp_basicsize*/
403 0, /*tp_itemsize*/
404 /* methods */
405 (destructor)ici_dealloc, /*tp_dealloc*/
406 (printfunc)0, /*tp_print*/
407 (getattrfunc)ici_getattr, /*tp_getattr*/
408 (setattrfunc)0, /*tp_setattr*/
409 (cmpfunc)0, /*tp_compare*/
410 (reprfunc)0, /*tp_repr*/
411 0, /*tp_as_number*/
412 0, /*tp_as_sequence*/
413 0, /*tp_as_mapping*/
414 (hashfunc)0, /*tp_hash*/
415 (ternaryfunc)0, /*tp_call*/
416 (reprfunc)0, /*tp_str*/
418 /* Space for future expansion */
419 0L,0L,0L,0L,
420 Icitype__doc__ /* Documentation string */
423 /* End of code for ic_instance objects */
424 /* -------------------------------------------------------- */
427 static char ic_ICStart__doc__[] =
428 "(OSType)->ic_instance; Create an Internet Config instance"
431 static PyObject *
432 ic_ICStart(PyObject *self, PyObject *args)
434 OSType creator;
436 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
437 return NULL;
438 return (PyObject *)newiciobject(creator);
441 /* List of methods defined in the module */
443 static struct PyMethodDef ic_methods[] = {
444 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
446 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
450 /* Initialization function for the module (*must* be called initicglue) */
452 static char icglue_module_documentation[] =
453 "Implements low-level Internet Config interface"
456 void
457 initicglue(void)
459 PyObject *m, *d;
461 /* Create the module and add the functions */
462 m = Py_InitModule4("icglue", ic_methods,
463 icglue_module_documentation,
464 (PyObject*)NULL,PYTHON_API_VERSION);
466 /* Add some symbolic constants to the module */
467 d = PyModule_GetDict(m);
468 ErrorObject = PyMac_GetOSErrException();
469 if (ErrorObject == NULL ||
470 PyDict_SetItemString(d, "error", ErrorObject) != 0)
471 return;
473 /* XXXX Add constants here */
475 /* Check for errors */
476 if (PyErr_Occurred())
477 Py_FatalError("can't initialize module icglue");