py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Mac / Modules / icgluemodule.c
blobbd616db9ae11f2d4f6ed744929a20c575a714671
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.
43 #undef PRAGMA_ALIGN_SUPPORTED
44 #define PRAGMA_ALIGN_SUPPORTED 0
45 #endif /* !TARGET_API_MAC_OS8 */
47 #include "ICAPI.h"
48 #else
49 #include <Carbon/Carbon.h>
50 typedef OSStatus ICError;
51 /* Some fields in ICMapEntry have changed names. */
52 #define file_type fileType
53 #define file_creator fileCreator
54 #define post_creator postCreator
55 #define creator_app_name creatorAppName
56 #define post_app_name postAppName
57 #define MIME_type MIMEType
58 #define entry_name entryName
59 #endif
61 static PyObject *ErrorObject;
63 static PyObject *
64 ici_error(ICError err)
66 PyErr_SetObject(ErrorObject, PyInt_FromLong((long)err));
67 return NULL;
70 /* ----------------------------------------------------- */
72 /* Declarations for objects of type ic_instance */
74 typedef struct {
75 PyObject_HEAD
76 ICInstance inst;
77 } iciobject;
79 staticforward PyTypeObject Icitype;
83 /* ---------------------------------------------------------------- */
85 #if TARGET_API_MAC_OS8
86 static char ici_ICFindConfigFile__doc__[] =
87 "()->None; Find config file in standard places"
90 static PyObject *
91 ici_ICFindConfigFile(self, args)
92 iciobject *self;
93 PyObject *args;
95 ICError err;
97 if (!PyArg_ParseTuple(args, ""))
98 return NULL;
99 if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
100 return ici_error(err);
101 Py_INCREF(Py_None);
102 return Py_None;
106 static char ici_ICFindUserConfigFile__doc__[] =
107 "(vRefNum, dirID)->None; Find config file in specified place"
110 static PyObject *
111 ici_ICFindUserConfigFile(self, args)
112 iciobject *self;
113 PyObject *args;
115 ICError err;
116 ICDirSpec where;
118 if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
119 return NULL;
120 if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
121 return ici_error(err);
122 Py_INCREF(Py_None);
123 return Py_None;
127 static char ici_ICChooseConfig__doc__[] =
128 "()->None; Let the user choose a config file"
131 static PyObject *
132 ici_ICChooseConfig(self, args)
133 iciobject *self;
134 PyObject *args;
136 ICError err;
138 if (!PyArg_ParseTuple(args, ""))
139 return NULL;
140 if ((err=ICChooseConfig(self->inst)) != 0 )
141 return ici_error(err);
142 Py_INCREF(Py_None);
143 return Py_None;
146 static char ici_ICChooseNewConfig__doc__[] =
147 "()->None; Let the user choose a new config file"
150 static PyObject *
151 ici_ICChooseNewConfig(self, args)
152 iciobject *self;
153 PyObject *args;
155 ICError err;
157 if (!PyArg_ParseTuple(args, ""))
158 return NULL;
159 if ((err=ICChooseNewConfig(self->inst)) != 0 )
160 return ici_error(err);
161 Py_INCREF(Py_None);
162 return Py_None;
164 #endif /* TARGET_API_MAC_OS8 */
167 static char ici_ICGetSeed__doc__[] =
168 "()->int; Returns int that changes when configuration does"
171 static PyObject *
172 ici_ICGetSeed(self, args)
173 iciobject *self;
174 PyObject *args;
176 ICError err;
177 long seed;
179 if (!PyArg_ParseTuple(args, ""))
180 return NULL;
181 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
182 return ici_error(err);
183 return Py_BuildValue("i", (int)seed);
187 static char ici_ICBegin__doc__[] =
188 "(perm)->None; Lock config file for read/write"
191 static PyObject *
192 ici_ICBegin(self, args)
193 iciobject *self;
194 PyObject *args;
196 ICError err;
197 int perm;
199 if (!PyArg_ParseTuple(args, "i", &perm))
200 return NULL;
201 if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
202 return ici_error(err);
203 Py_INCREF(Py_None);
204 return Py_None;
208 static char ici_ICFindPrefHandle__doc__[] =
209 "(key, handle)->attrs; Lookup key, store result in handle, return attributes"
212 static PyObject *
213 ici_ICFindPrefHandle(self, args)
214 iciobject *self;
215 PyObject *args;
217 ICError err;
218 Str255 key;
219 ICAttr attr;
220 Handle h;
222 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
223 return NULL;
224 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
225 return ici_error(err);
226 return Py_BuildValue("i", (int)attr);
230 static char ici_ICSetPref__doc__[] =
231 "(key, attr, data)->None; Set preference key to data with attributes"
234 static PyObject *
235 ici_ICSetPref(self, args)
236 iciobject *self;
237 PyObject *args;
239 ICError err;
240 Str255 key;
241 int attr;
242 char *data;
243 int datalen;
245 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
246 &data, &datalen))
247 return NULL;
248 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
249 (long)datalen)) != 0)
250 return ici_error(err);
251 Py_INCREF(Py_None);
252 return Py_None;
256 static char ici_ICCountPref__doc__[] =
257 "()->int; Return number of preferences"
260 static PyObject *
261 ici_ICCountPref(self, args)
262 iciobject *self;
263 PyObject *args;
265 ICError err;
266 long count;
268 if (!PyArg_ParseTuple(args, ""))
269 return NULL;
270 if ((err=ICCountPref(self->inst, &count)) != 0 )
271 return ici_error(err);
272 return Py_BuildValue("i", (int)count);
276 static char ici_ICGetIndPref__doc__[] =
277 "(num)->key; Return key of preference with given index"
280 static PyObject *
281 ici_ICGetIndPref(self, args)
282 iciobject *self;
283 PyObject *args;
285 ICError err;
286 long num;
287 Str255 key;
289 if (!PyArg_ParseTuple(args, "l", &num))
290 return NULL;
291 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
292 return ici_error(err);
293 return Py_BuildValue("O&", PyMac_BuildStr255, key);
297 static char ici_ICDeletePref__doc__[] =
298 "(key)->None; Delete preference"
301 static PyObject *
302 ici_ICDeletePref(self, args)
303 iciobject *self;
304 PyObject *args;
306 ICError err;
307 Str255 key;
309 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
310 return NULL;
311 if ((err=ICDeletePref(self->inst, key)) != 0 )
312 return ici_error(err);
313 Py_INCREF(Py_None);
314 return Py_None;
318 static char ici_ICEnd__doc__[] =
319 "()->None; Unlock file after ICBegin call"
322 static PyObject *
323 ici_ICEnd(self, args)
324 iciobject *self;
325 PyObject *args;
327 ICError err;
329 if (!PyArg_ParseTuple(args, ""))
330 return NULL;
331 if ((err=ICEnd(self->inst)) != 0 )
332 return ici_error(err);
333 Py_INCREF(Py_None);
334 return Py_None;
338 static char ici_ICEditPreferences__doc__[] =
339 "(key)->None; Ask user to edit preferences, staring with key"
342 static PyObject *
343 ici_ICEditPreferences(self, args)
344 iciobject *self;
345 PyObject *args;
347 ICError err;
348 Str255 key;
350 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
351 return NULL;
352 if ((err=ICEditPreferences(self->inst, key)) != 0 )
353 return ici_error(err);
354 Py_INCREF(Py_None);
355 return Py_None;
359 static char ici_ICParseURL__doc__[] =
360 "(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
363 static PyObject *
364 ici_ICParseURL(self, args)
365 iciobject *self;
366 PyObject *args;
368 ICError err;
369 Str255 hint;
370 char *data;
371 int datalen;
372 long selStart, selEnd;
373 Handle h;
375 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
376 &selStart, &selEnd, ResObj_Convert, &h))
377 return NULL;
378 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
379 &selStart, &selEnd, h)) != 0 )
380 return ici_error(err);
381 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
385 static char ici_ICLaunchURL__doc__[] =
386 "(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
389 static PyObject *
390 ici_ICLaunchURL(self, args)
391 iciobject *self;
392 PyObject *args;
394 ICError err;
395 Str255 hint;
396 char *data;
397 int datalen;
398 long selStart, selEnd;
400 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
401 &selStart, &selEnd))
402 return NULL;
403 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
404 &selStart, &selEnd)) != 0 )
405 return ici_error(err);
406 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
410 static char ici_ICMapFilename__doc__[] =
411 "(filename)->mapinfo; Get filemap info for given filename"
414 static PyObject *
415 ici_ICMapFilename(self, args)
416 iciobject *self;
417 PyObject *args;
419 ICError err;
420 Str255 filename;
421 ICMapEntry entry;
423 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
424 return NULL;
425 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
426 return ici_error(err);
427 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
428 PyMac_BuildOSType, entry.file_type,
429 PyMac_BuildOSType, entry.file_creator,
430 PyMac_BuildOSType, entry.post_creator,
431 entry.flags,
432 PyMac_BuildStr255, entry.extension,
433 PyMac_BuildStr255, entry.creator_app_name,
434 PyMac_BuildStr255, entry.post_app_name,
435 PyMac_BuildStr255, entry.MIME_type,
436 PyMac_BuildStr255, entry.entry_name);
440 static char ici_ICMapTypeCreator__doc__[] =
441 "(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
444 static PyObject *
445 ici_ICMapTypeCreator(self, args)
446 iciobject *self;
447 PyObject *args;
449 ICError err;
450 OSType type, creator;
451 Str255 filename;
452 ICMapEntry entry;
454 if (!PyArg_ParseTuple(args, "O&O&O&",
455 PyMac_GetOSType, &type,
456 PyMac_GetOSType, &creator,
457 PyMac_GetStr255, filename))
458 return NULL;
459 if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
460 return ici_error(err);
461 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
462 PyMac_BuildOSType, entry.file_type,
463 PyMac_BuildOSType, entry.file_creator,
464 PyMac_BuildOSType, entry.post_creator,
465 entry.flags,
466 PyMac_BuildStr255, entry.extension,
467 PyMac_BuildStr255, entry.creator_app_name,
468 PyMac_BuildStr255, entry.post_app_name,
469 PyMac_BuildStr255, entry.MIME_type,
470 PyMac_BuildStr255, entry.entry_name);
474 static struct PyMethodDef ici_methods[] = {
475 #if TARGET_API_MAC_OS8
476 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
477 {"ICFindUserConfigFile", (PyCFunction)ici_ICFindUserConfigFile, METH_VARARGS, ici_ICFindUserConfigFile__doc__},
478 {"ICChooseConfig", (PyCFunction)ici_ICChooseConfig, METH_VARARGS, ici_ICChooseConfig__doc__},
479 {"ICChooseNewConfig", (PyCFunction)ici_ICChooseNewConfig, METH_VARARGS, ici_ICChooseNewConfig__doc__},
480 #endif /* TARGET_API_MAC_OS8 */
481 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
482 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
483 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
484 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
485 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
486 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
487 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
488 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
489 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
490 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
491 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
492 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
493 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
495 {NULL, NULL} /* sentinel */
498 /* ---------- */
501 static iciobject *
502 newiciobject(OSType creator)
504 iciobject *self;
505 ICError err;
507 self = PyObject_NEW(iciobject, &Icitype);
508 if (self == NULL)
509 return NULL;
510 if ((err=ICStart(&self->inst, creator)) != 0 ) {
511 (void)ici_error(err);
512 PyMem_DEL(self);
513 return NULL;
515 return self;
519 static void
520 ici_dealloc(self)
521 iciobject *self;
523 (void)ICStop(self->inst);
524 PyMem_DEL(self);
527 static PyObject *
528 ici_getattr(self, name)
529 iciobject *self;
530 char *name;
532 return Py_FindMethod(ici_methods, (PyObject *)self, name);
535 static char Icitype__doc__[] =
536 "Internet Config instance"
539 static PyTypeObject Icitype = {
540 PyObject_HEAD_INIT(&PyType_Type)
541 0, /*ob_size*/
542 "ic_instance", /*tp_name*/
543 sizeof(iciobject), /*tp_basicsize*/
544 0, /*tp_itemsize*/
545 /* methods */
546 (destructor)ici_dealloc, /*tp_dealloc*/
547 (printfunc)0, /*tp_print*/
548 (getattrfunc)ici_getattr, /*tp_getattr*/
549 (setattrfunc)0, /*tp_setattr*/
550 (cmpfunc)0, /*tp_compare*/
551 (reprfunc)0, /*tp_repr*/
552 0, /*tp_as_number*/
553 0, /*tp_as_sequence*/
554 0, /*tp_as_mapping*/
555 (hashfunc)0, /*tp_hash*/
556 (ternaryfunc)0, /*tp_call*/
557 (reprfunc)0, /*tp_str*/
559 /* Space for future expansion */
560 0L,0L,0L,0L,
561 Icitype__doc__ /* Documentation string */
564 /* End of code for ic_instance objects */
565 /* -------------------------------------------------------- */
568 static char ic_ICStart__doc__[] =
569 "(OSType)->ic_instance; Create an Internet Config instance"
572 static PyObject *
573 ic_ICStart(self, args)
574 PyObject *self; /* Not used */
575 PyObject *args;
577 OSType creator;
579 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
580 return NULL;
581 return (PyObject *)newiciobject(creator);
584 /* List of methods defined in the module */
586 static struct PyMethodDef ic_methods[] = {
587 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
589 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
593 /* Initialization function for the module (*must* be called initicglue) */
595 static char icglue_module_documentation[] =
596 "Implements low-level Internet Config interface"
599 void
600 initicglue()
602 PyObject *m, *d;
604 /* Create the module and add the functions */
605 m = Py_InitModule4("icglue", ic_methods,
606 icglue_module_documentation,
607 (PyObject*)NULL,PYTHON_API_VERSION);
609 /* Add some symbolic constants to the module */
610 d = PyModule_GetDict(m);
611 ErrorObject = PyString_FromString("icglue.error");
612 PyDict_SetItemString(d, "error", ErrorObject);
614 /* XXXX Add constants here */
616 /* Check for errors */
617 if (PyErr_Occurred())
618 Py_FatalError("can't initialize module icglue");