This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Mac / Modules / icgluemodule.c
blob3061f9827f8bb4cbe77939eceb0f0ba9619c331b
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 // #if !TARGET_API_MAC_OS8
39 // /* The Carbon headers define PRAGMA_ALIGN_SUPPORT to something illegal,
40 // ** because you shouldn't use it for Carbon. All good and well, but portable
41 // ** code still needs it. So, we undefine it here.
42 // */
43 // #undef PRAGMA_ALIGN_SUPPORTED
44 // #define PRAGMA_ALIGN_SUPPORTED 0
45 // #endif /* !TARGET_API_MAC_OS8 */
47 // #include "ICAPI.h"
48 #include <InternetConfig.h>
49 #else
50 #include <Carbon/Carbon.h>
51 #endif
53 static PyObject *ErrorObject;
55 /* ----------------------------------------------------- */
57 /* Declarations for objects of type ic_instance */
59 typedef struct {
60 PyObject_HEAD
61 ICInstance inst;
62 } iciobject;
64 staticforward PyTypeObject Icitype;
68 /* ---------------------------------------------------------------- */
70 #if TARGET_API_MAC_OS8
71 static char ici_ICFindConfigFile__doc__[] =
72 "()->None; Find config file in standard places"
75 static PyObject *
76 ici_ICFindConfigFile(iciobject *self, PyObject *args)
78 OSStatus err;
80 if (!PyArg_ParseTuple(args, ""))
81 return NULL;
82 if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
83 return PyMac_Error(err);
84 Py_INCREF(Py_None);
85 return Py_None;
89 static char ici_ICFindUserConfigFile__doc__[] =
90 "(vRefNum, dirID)->None; Find config file in specified place"
93 static PyObject *
94 ici_ICFindUserConfigFile(iciobject *self, PyObject *args)
96 OSStatus err;
97 ICDirSpec where;
99 if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
100 return NULL;
101 if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
102 return PyMac_Error(err);
103 Py_INCREF(Py_None);
104 return Py_None;
108 static char ici_ICChooseConfig__doc__[] =
109 "()->None; Let the user choose a config file"
112 static PyObject *
113 ici_ICChooseConfig(iciobject *self, PyObject *args)
115 OSStatus err;
117 if (!PyArg_ParseTuple(args, ""))
118 return NULL;
119 if ((err=ICChooseConfig(self->inst)) != 0 )
120 return PyMac_Error(err);
121 Py_INCREF(Py_None);
122 return Py_None;
125 static char ici_ICChooseNewConfig__doc__[] =
126 "()->None; Let the user choose a new config file"
129 static PyObject *
130 ici_ICChooseNewConfig(iciobject *self, PyObject *args)
132 OSStatus err;
134 if (!PyArg_ParseTuple(args, ""))
135 return NULL;
136 if ((err=ICChooseNewConfig(self->inst)) != 0 )
137 return PyMac_Error(err);
138 Py_INCREF(Py_None);
139 return Py_None;
141 #endif /* TARGET_API_MAC_OS8 */
144 static char ici_ICGetSeed__doc__[] =
145 "()->int; Returns int that changes when configuration does"
148 static PyObject *
149 ici_ICGetSeed(iciobject *self, PyObject *args)
151 OSStatus err;
152 long seed;
154 if (!PyArg_ParseTuple(args, ""))
155 return NULL;
156 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
157 return PyMac_Error(err);
158 return Py_BuildValue("i", (int)seed);
162 static char ici_ICBegin__doc__[] =
163 "(perm)->None; Lock config file for read/write"
166 static PyObject *
167 ici_ICBegin(iciobject *self, PyObject *args)
169 OSStatus err;
170 int perm;
172 if (!PyArg_ParseTuple(args, "i", &perm))
173 return NULL;
174 if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
175 return PyMac_Error(err);
176 Py_INCREF(Py_None);
177 return Py_None;
181 static char ici_ICFindPrefHandle__doc__[] =
182 "(key, handle)->attrs; Lookup key, store result in handle, return attributes"
185 static PyObject *
186 ici_ICFindPrefHandle(iciobject *self, PyObject *args)
188 OSStatus err;
189 Str255 key;
190 ICAttr attr;
191 Handle h;
193 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
194 return NULL;
195 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
196 return PyMac_Error(err);
197 return Py_BuildValue("i", (int)attr);
201 static char ici_ICSetPref__doc__[] =
202 "(key, attr, data)->None; Set preference key to data with attributes"
205 static PyObject *
206 ici_ICSetPref(iciobject *self, PyObject *args)
208 OSStatus err;
209 Str255 key;
210 int attr;
211 char *data;
212 int datalen;
214 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
215 &data, &datalen))
216 return NULL;
217 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
218 (long)datalen)) != 0)
219 return PyMac_Error(err);
220 Py_INCREF(Py_None);
221 return Py_None;
225 static char ici_ICCountPref__doc__[] =
226 "()->int; Return number of preferences"
229 static PyObject *
230 ici_ICCountPref(iciobject *self, PyObject *args)
232 OSStatus err;
233 long count;
235 if (!PyArg_ParseTuple(args, ""))
236 return NULL;
237 if ((err=ICCountPref(self->inst, &count)) != 0 )
238 return PyMac_Error(err);
239 return Py_BuildValue("i", (int)count);
243 static char ici_ICGetIndPref__doc__[] =
244 "(num)->key; Return key of preference with given index"
247 static PyObject *
248 ici_ICGetIndPref(iciobject *self, PyObject *args)
250 OSStatus err;
251 long num;
252 Str255 key;
254 if (!PyArg_ParseTuple(args, "l", &num))
255 return NULL;
256 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
257 return PyMac_Error(err);
258 return Py_BuildValue("O&", PyMac_BuildStr255, key);
262 static char ici_ICDeletePref__doc__[] =
263 "(key)->None; Delete preference"
266 static PyObject *
267 ici_ICDeletePref(iciobject *self, PyObject *args)
269 OSStatus err;
270 Str255 key;
272 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
273 return NULL;
274 if ((err=ICDeletePref(self->inst, key)) != 0 )
275 return PyMac_Error(err);
276 Py_INCREF(Py_None);
277 return Py_None;
281 static char ici_ICEnd__doc__[] =
282 "()->None; Unlock file after ICBegin call"
285 static PyObject *
286 ici_ICEnd(iciobject *self, PyObject *args)
288 OSStatus err;
290 if (!PyArg_ParseTuple(args, ""))
291 return NULL;
292 if ((err=ICEnd(self->inst)) != 0 )
293 return PyMac_Error(err);
294 Py_INCREF(Py_None);
295 return Py_None;
299 static char ici_ICEditPreferences__doc__[] =
300 "(key)->None; Ask user to edit preferences, staring with key"
303 static PyObject *
304 ici_ICEditPreferences(iciobject *self, PyObject *args)
306 OSStatus err;
307 Str255 key;
309 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
310 return NULL;
311 if ((err=ICEditPreferences(self->inst, key)) != 0 )
312 return PyMac_Error(err);
313 Py_INCREF(Py_None);
314 return Py_None;
318 static char ici_ICParseURL__doc__[] =
319 "(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
322 static PyObject *
323 ici_ICParseURL(iciobject *self, PyObject *args)
325 OSStatus err;
326 Str255 hint;
327 char *data;
328 int datalen;
329 long selStart, selEnd;
330 Handle h;
332 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
333 &selStart, &selEnd, ResObj_Convert, &h))
334 return NULL;
335 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
336 &selStart, &selEnd, h)) != 0 )
337 return PyMac_Error(err);
338 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
342 static char ici_ICLaunchURL__doc__[] =
343 "(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
346 static PyObject *
347 ici_ICLaunchURL(iciobject *self, PyObject *args)
349 OSStatus err;
350 Str255 hint;
351 char *data;
352 int datalen;
353 long selStart, selEnd;
355 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
356 &selStart, &selEnd))
357 return NULL;
358 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
359 &selStart, &selEnd)) != 0 )
360 return PyMac_Error(err);
361 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
365 static char ici_ICMapFilename__doc__[] =
366 "(filename)->mapinfo; Get filemap info for given filename"
369 static PyObject *
370 ici_ICMapFilename(iciobject *self, PyObject *args)
372 OSStatus err;
373 Str255 filename;
374 ICMapEntry entry;
376 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
377 return NULL;
378 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
379 return PyMac_Error(err);
380 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
381 PyMac_BuildOSType, entry.fileType,
382 PyMac_BuildOSType, entry.fileCreator,
383 PyMac_BuildOSType, entry.postCreator,
384 entry.flags,
385 PyMac_BuildStr255, entry.extension,
386 PyMac_BuildStr255, entry.creatorAppName,
387 PyMac_BuildStr255, entry.postAppName,
388 PyMac_BuildStr255, entry.MIMEType,
389 PyMac_BuildStr255, entry.entryName);
393 static char ici_ICMapTypeCreator__doc__[] =
394 "(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
397 static PyObject *
398 ici_ICMapTypeCreator(iciobject *self, PyObject *args)
400 OSStatus err;
401 OSType type, creator;
402 Str255 filename;
403 ICMapEntry entry;
405 if (!PyArg_ParseTuple(args, "O&O&O&",
406 PyMac_GetOSType, &type,
407 PyMac_GetOSType, &creator,
408 PyMac_GetStr255, filename))
409 return NULL;
410 if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
411 return PyMac_Error(err);
412 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
413 PyMac_BuildOSType, entry.fileType,
414 PyMac_BuildOSType, entry.fileCreator,
415 PyMac_BuildOSType, entry.postCreator,
416 entry.flags,
417 PyMac_BuildStr255, entry.extension,
418 PyMac_BuildStr255, entry.creatorAppName,
419 PyMac_BuildStr255, entry.postAppName,
420 PyMac_BuildStr255, entry.MIMEType,
421 PyMac_BuildStr255, entry.entryName);
425 static struct PyMethodDef ici_methods[] = {
426 #if TARGET_API_MAC_OS8
427 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
428 {"ICFindUserConfigFile", (PyCFunction)ici_ICFindUserConfigFile, METH_VARARGS, ici_ICFindUserConfigFile__doc__},
429 {"ICChooseConfig", (PyCFunction)ici_ICChooseConfig, METH_VARARGS, ici_ICChooseConfig__doc__},
430 {"ICChooseNewConfig", (PyCFunction)ici_ICChooseNewConfig, METH_VARARGS, ici_ICChooseNewConfig__doc__},
431 #endif /* TARGET_API_MAC_OS8 */
432 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
433 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
434 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
435 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
436 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
437 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
438 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
439 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
440 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
441 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
442 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
443 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
444 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
446 {NULL, NULL} /* sentinel */
449 /* ---------- */
452 static iciobject *
453 newiciobject(OSType creator)
455 iciobject *self;
456 OSStatus err;
458 self = PyObject_NEW(iciobject, &Icitype);
459 if (self == NULL)
460 return NULL;
461 if ((err=ICStart(&self->inst, creator)) != 0 ) {
462 (void)PyMac_Error(err);
463 PyMem_DEL(self);
464 return NULL;
466 return self;
470 static void
471 ici_dealloc(iciobject *self)
473 (void)ICStop(self->inst);
474 PyMem_DEL(self);
477 static PyObject *
478 ici_getattr(iciobject *self, char *name)
480 return Py_FindMethod(ici_methods, (PyObject *)self, name);
483 static char Icitype__doc__[] =
484 "Internet Config instance"
487 static PyTypeObject Icitype = {
488 PyObject_HEAD_INIT(&PyType_Type)
489 0, /*ob_size*/
490 "icglue.ic_instance", /*tp_name*/
491 sizeof(iciobject), /*tp_basicsize*/
492 0, /*tp_itemsize*/
493 /* methods */
494 (destructor)ici_dealloc, /*tp_dealloc*/
495 (printfunc)0, /*tp_print*/
496 (getattrfunc)ici_getattr, /*tp_getattr*/
497 (setattrfunc)0, /*tp_setattr*/
498 (cmpfunc)0, /*tp_compare*/
499 (reprfunc)0, /*tp_repr*/
500 0, /*tp_as_number*/
501 0, /*tp_as_sequence*/
502 0, /*tp_as_mapping*/
503 (hashfunc)0, /*tp_hash*/
504 (ternaryfunc)0, /*tp_call*/
505 (reprfunc)0, /*tp_str*/
507 /* Space for future expansion */
508 0L,0L,0L,0L,
509 Icitype__doc__ /* Documentation string */
512 /* End of code for ic_instance objects */
513 /* -------------------------------------------------------- */
516 static char ic_ICStart__doc__[] =
517 "(OSType)->ic_instance; Create an Internet Config instance"
520 static PyObject *
521 ic_ICStart(PyObject *self, PyObject *args)
523 OSType creator;
525 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
526 return NULL;
527 return (PyObject *)newiciobject(creator);
530 /* List of methods defined in the module */
532 static struct PyMethodDef ic_methods[] = {
533 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
535 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
539 /* Initialization function for the module (*must* be called initicglue) */
541 static char icglue_module_documentation[] =
542 "Implements low-level Internet Config interface"
545 void
546 initicglue(void)
548 PyObject *m, *d;
550 /* Create the module and add the functions */
551 m = Py_InitModule4("icglue", ic_methods,
552 icglue_module_documentation,
553 (PyObject*)NULL,PYTHON_API_VERSION);
555 /* Add some symbolic constants to the module */
556 d = PyModule_GetDict(m);
557 ErrorObject = PyMac_GetOSErrException();
558 if (ErrorObject == NULL ||
559 PyDict_SetItemString(d, "error", ErrorObject) != 0)
560 return;
562 /* XXXX Add constants here */
564 /* Check for errors */
565 if (PyErr_Occurred())
566 Py_FatalError("can't initialize module icglue");