Clarify portability and main program.
[python/dscho.git] / Mac / Modules / icgluemodule.c
blob4c3c4954ef30739e282962fd62438f78f2be2d2e
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 */
38 #include "ICAPI.h"
40 static PyObject *ErrorObject;
42 static PyObject *
43 ici_error(ICError err)
45 PyErr_SetObject(ErrorObject, PyInt_FromLong((long)err));
46 return NULL;
49 /* ----------------------------------------------------- */
51 /* Declarations for objects of type ic_instance */
53 typedef struct {
54 PyObject_HEAD
55 ICInstance inst;
56 } iciobject;
58 staticforward PyTypeObject Icitype;
62 /* ---------------------------------------------------------------- */
64 static char ici_ICFindConfigFile__doc__[] =
65 "()->None; Find config file in standard places"
68 static PyObject *
69 ici_ICFindConfigFile(self, args)
70 iciobject *self;
71 PyObject *args;
73 ICError err;
75 if (!PyArg_ParseTuple(args, ""))
76 return NULL;
77 if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
78 return ici_error(err);
79 Py_INCREF(Py_None);
80 return Py_None;
84 static char ici_ICFindUserConfigFile__doc__[] =
85 "(vRefNum, dirID)->None; Find config file in specified place"
88 static PyObject *
89 ici_ICFindUserConfigFile(self, args)
90 iciobject *self;
91 PyObject *args;
93 ICError err;
94 ICDirSpec where;
96 if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
97 return NULL;
98 if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
99 return ici_error(err);
100 Py_INCREF(Py_None);
101 return Py_None;
105 static char ici_ICChooseConfig__doc__[] =
106 "()->None; Let the user choose a config file"
109 static PyObject *
110 ici_ICChooseConfig(self, args)
111 iciobject *self;
112 PyObject *args;
114 ICError err;
116 if (!PyArg_ParseTuple(args, ""))
117 return NULL;
118 if ((err=ICChooseConfig(self->inst)) != 0 )
119 return ici_error(err);
120 Py_INCREF(Py_None);
121 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(self, args)
131 iciobject *self;
132 PyObject *args;
134 ICError err;
136 if (!PyArg_ParseTuple(args, ""))
137 return NULL;
138 if ((err=ICChooseNewConfig(self->inst)) != 0 )
139 return ici_error(err);
140 Py_INCREF(Py_None);
141 return Py_None;
145 static char ici_ICGetSeed__doc__[] =
146 "()->int; Returns int that changes when configuration does"
149 static PyObject *
150 ici_ICGetSeed(self, args)
151 iciobject *self;
152 PyObject *args;
154 ICError err;
155 long seed;
157 if (!PyArg_ParseTuple(args, ""))
158 return NULL;
159 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
160 return ici_error(err);
161 return Py_BuildValue("i", (int)seed);
165 static char ici_ICBegin__doc__[] =
166 "(perm)->None; Lock config file for read/write"
169 static PyObject *
170 ici_ICBegin(self, args)
171 iciobject *self;
172 PyObject *args;
174 ICError err;
175 int perm;
177 if (!PyArg_ParseTuple(args, "i", &perm))
178 return NULL;
179 if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
180 return ici_error(err);
181 Py_INCREF(Py_None);
182 return Py_None;
186 static char ici_ICFindPrefHandle__doc__[] =
187 "(key, handle)->attrs; Lookup key, store result in handle, return attributes"
190 static PyObject *
191 ici_ICFindPrefHandle(self, args)
192 iciobject *self;
193 PyObject *args;
195 ICError err;
196 Str255 key;
197 ICAttr attr;
198 Handle h;
200 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
201 return NULL;
202 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
203 return ici_error(err);
204 return Py_BuildValue("i", (int)attr);
208 static char ici_ICSetPref__doc__[] =
209 "(key, attr, data)->None; Set preference key to data with attributes"
212 static PyObject *
213 ici_ICSetPref(self, args)
214 iciobject *self;
215 PyObject *args;
217 ICError err;
218 Str255 key;
219 int attr;
220 char *data;
221 int datalen;
223 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
224 &data, &datalen))
225 return NULL;
226 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
227 (long)datalen)) != 0)
228 return ici_error(err);
229 Py_INCREF(Py_None);
230 return Py_None;
234 static char ici_ICCountPref__doc__[] =
235 "()->int; Return number of preferences"
238 static PyObject *
239 ici_ICCountPref(self, args)
240 iciobject *self;
241 PyObject *args;
243 ICError err;
244 long count;
246 if (!PyArg_ParseTuple(args, ""))
247 return NULL;
248 if ((err=ICCountPref(self->inst, &count)) != 0 )
249 return ici_error(err);
250 return Py_BuildValue("i", (int)count);
254 static char ici_ICGetIndPref__doc__[] =
255 "(num)->key; Return key of preference with given index"
258 static PyObject *
259 ici_ICGetIndPref(self, args)
260 iciobject *self;
261 PyObject *args;
263 ICError err;
264 long num;
265 Str255 key;
267 if (!PyArg_ParseTuple(args, "l", &num))
268 return NULL;
269 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
270 return ici_error(err);
271 return Py_BuildValue("O&", PyMac_BuildStr255, key);
275 static char ici_ICDeletePref__doc__[] =
276 "(key)->None; Delete preference"
279 static PyObject *
280 ici_ICDeletePref(self, args)
281 iciobject *self;
282 PyObject *args;
284 ICError err;
285 Str255 key;
287 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
288 return NULL;
289 if ((err=ICDeletePref(self->inst, key)) != 0 )
290 return ici_error(err);
291 Py_INCREF(Py_None);
292 return Py_None;
296 static char ici_ICEnd__doc__[] =
297 "()->None; Unlock file after ICBegin call"
300 static PyObject *
301 ici_ICEnd(self, args)
302 iciobject *self;
303 PyObject *args;
305 ICError err;
307 if (!PyArg_ParseTuple(args, ""))
308 return NULL;
309 if ((err=ICEnd(self->inst)) != 0 )
310 return ici_error(err);
311 Py_INCREF(Py_None);
312 return Py_None;
316 static char ici_ICEditPreferences__doc__[] =
317 "(key)->None; Ask user to edit preferences, staring with key"
320 static PyObject *
321 ici_ICEditPreferences(self, args)
322 iciobject *self;
323 PyObject *args;
325 ICError err;
326 Str255 key;
328 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
329 return NULL;
330 if ((err=ICEditPreferences(self->inst, key)) != 0 )
331 return ici_error(err);
332 Py_INCREF(Py_None);
333 return Py_None;
337 static char ici_ICParseURL__doc__[] =
338 "(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
341 static PyObject *
342 ici_ICParseURL(self, args)
343 iciobject *self;
344 PyObject *args;
346 ICError err;
347 Str255 hint;
348 char *data;
349 int datalen;
350 long selStart, selEnd;
351 Handle h;
353 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
354 &selStart, &selEnd, ResObj_Convert, &h))
355 return NULL;
356 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
357 &selStart, &selEnd, h)) != 0 )
358 return ici_error(err);
359 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
363 static char ici_ICLaunchURL__doc__[] =
364 "(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
367 static PyObject *
368 ici_ICLaunchURL(self, args)
369 iciobject *self;
370 PyObject *args;
372 ICError err;
373 Str255 hint;
374 char *data;
375 int datalen;
376 long selStart, selEnd;
378 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
379 &selStart, &selEnd))
380 return NULL;
381 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
382 &selStart, &selEnd)) != 0 )
383 return ici_error(err);
384 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
388 static char ici_ICMapFilename__doc__[] =
389 "(filename)->mapinfo; Get filemap info for given filename"
392 static PyObject *
393 ici_ICMapFilename(self, args)
394 iciobject *self;
395 PyObject *args;
397 ICError err;
398 Str255 filename;
399 ICMapEntry entry;
401 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
402 return NULL;
403 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
404 return ici_error(err);
405 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
406 PyMac_BuildOSType, entry.file_type,
407 PyMac_BuildOSType, entry.file_creator,
408 PyMac_BuildOSType, entry.post_creator,
409 entry.flags,
410 PyMac_BuildStr255, entry.extension,
411 PyMac_BuildStr255, entry.creator_app_name,
412 PyMac_BuildStr255, entry.post_app_name,
413 PyMac_BuildStr255, entry.MIME_type,
414 PyMac_BuildStr255, entry.entry_name);
418 static char ici_ICMapTypeCreator__doc__[] =
419 "(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
422 static PyObject *
423 ici_ICMapTypeCreator(self, args)
424 iciobject *self;
425 PyObject *args;
427 ICError err;
428 OSType type, creator;
429 Str255 filename;
430 ICMapEntry entry;
432 if (!PyArg_ParseTuple(args, "O&O&O&",
433 PyMac_GetOSType, &type,
434 PyMac_GetOSType, &creator,
435 PyMac_GetStr255, filename))
436 return NULL;
437 if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
438 return ici_error(err);
439 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
440 PyMac_BuildOSType, entry.file_type,
441 PyMac_BuildOSType, entry.file_creator,
442 PyMac_BuildOSType, entry.post_creator,
443 entry.flags,
444 PyMac_BuildStr255, entry.extension,
445 PyMac_BuildStr255, entry.creator_app_name,
446 PyMac_BuildStr255, entry.post_app_name,
447 PyMac_BuildStr255, entry.MIME_type,
448 PyMac_BuildStr255, entry.entry_name);
452 static struct PyMethodDef ici_methods[] = {
453 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
454 {"ICFindUserConfigFile", (PyCFunction)ici_ICFindUserConfigFile, METH_VARARGS, ici_ICFindUserConfigFile__doc__},
455 {"ICChooseConfig", (PyCFunction)ici_ICChooseConfig, METH_VARARGS, ici_ICChooseConfig__doc__},
456 {"ICChooseNewConfig", (PyCFunction)ici_ICChooseNewConfig, METH_VARARGS, ici_ICChooseNewConfig__doc__},
457 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
458 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
459 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
460 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
461 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
462 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
463 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
464 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
465 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
466 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
467 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
468 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
469 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
471 {NULL, NULL} /* sentinel */
474 /* ---------- */
477 static iciobject *
478 newiciobject(OSType creator)
480 iciobject *self;
481 ICError err;
483 self = PyObject_NEW(iciobject, &Icitype);
484 if (self == NULL)
485 return NULL;
486 if ((err=ICStart(&self->inst, creator)) != 0 ) {
487 (void)ici_error(err);
488 PyMem_DEL(self);
489 return NULL;
491 return self;
495 static void
496 ici_dealloc(self)
497 iciobject *self;
499 (void)ICStop(self->inst);
500 PyMem_DEL(self);
503 static PyObject *
504 ici_getattr(self, name)
505 iciobject *self;
506 char *name;
508 return Py_FindMethod(ici_methods, (PyObject *)self, name);
511 static char Icitype__doc__[] =
512 "Internet Config instance"
515 static PyTypeObject Icitype = {
516 PyObject_HEAD_INIT(&PyType_Type)
517 0, /*ob_size*/
518 "ic_instance", /*tp_name*/
519 sizeof(iciobject), /*tp_basicsize*/
520 0, /*tp_itemsize*/
521 /* methods */
522 (destructor)ici_dealloc, /*tp_dealloc*/
523 (printfunc)0, /*tp_print*/
524 (getattrfunc)ici_getattr, /*tp_getattr*/
525 (setattrfunc)0, /*tp_setattr*/
526 (cmpfunc)0, /*tp_compare*/
527 (reprfunc)0, /*tp_repr*/
528 0, /*tp_as_number*/
529 0, /*tp_as_sequence*/
530 0, /*tp_as_mapping*/
531 (hashfunc)0, /*tp_hash*/
532 (ternaryfunc)0, /*tp_call*/
533 (reprfunc)0, /*tp_str*/
535 /* Space for future expansion */
536 0L,0L,0L,0L,
537 Icitype__doc__ /* Documentation string */
540 /* End of code for ic_instance objects */
541 /* -------------------------------------------------------- */
544 static char ic_ICStart__doc__[] =
545 "(OSType)->ic_instance; Create an Internet Config instance"
548 static PyObject *
549 ic_ICStart(self, args)
550 PyObject *self; /* Not used */
551 PyObject *args;
553 OSType creator;
555 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
556 return NULL;
557 return (PyObject *)newiciobject(creator);
560 /* List of methods defined in the module */
562 static struct PyMethodDef ic_methods[] = {
563 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
565 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
569 /* Initialization function for the module (*must* be called initicglue) */
571 static char icglue_module_documentation[] =
572 "Implements low-level Internet Config interface"
575 void
576 initicglue()
578 PyObject *m, *d;
580 /* Create the module and add the functions */
581 m = Py_InitModule4("icglue", ic_methods,
582 icglue_module_documentation,
583 (PyObject*)NULL,PYTHON_API_VERSION);
585 /* Add some symbolic constants to the module */
586 d = PyModule_GetDict(m);
587 ErrorObject = PyString_FromString("icglue.error");
588 PyDict_SetItemString(d, "error", ErrorObject);
590 /* XXXX Add constants here */
592 /* Check for errors */
593 if (PyErr_Occurred())
594 Py_FatalError("can't initialize module icglue");