blob: f92d56e29b1bb1d2f26480250f4d24e1d914b855 [file] [log] [blame]
Jack Jansen8ce72f51997-01-07 16:18:32 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32#include "Python.h"
33#include "macglue.h"
34
35extern int ResObj_Convert(PyObject *, Handle *); /* From Resmodule.c */
36
37
38#include "ICAPI.h"
39
40static PyObject *ErrorObject;
41
42static PyObject *
43ici_error(ICError err)
44{
45 PyErr_SetObject(ErrorObject, PyInt_FromLong((long)err));
46 return NULL;
47}
48
49/* ----------------------------------------------------- */
50
51/* Declarations for objects of type ic_instance */
52
53typedef struct {
54 PyObject_HEAD
55 ICInstance inst;
56} iciobject;
57
58staticforward PyTypeObject Icitype;
59
60
61
62/* ---------------------------------------------------------------- */
63
64static char ici_ICFindConfigFile__doc__[] =
65"()->None; Find config file in standard places"
66;
67
68static PyObject *
69ici_ICFindConfigFile(self, args)
70 iciobject *self;
71 PyObject *args;
72{
73 ICError err;
74
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;
81}
82
83
84static char ici_ICFindUserConfigFile__doc__[] =
85"(vRefNum, dirID)->None; Find config file in specified place"
86;
87
88static PyObject *
89ici_ICFindUserConfigFile(self, args)
90 iciobject *self;
91 PyObject *args;
92{
93 ICError err;
94 ICDirSpec where;
95
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;
102}
103
104
105static char ici_ICChooseConfig__doc__[] =
106"()->None; Let the user choose a config file"
107;
108
109static PyObject *
110ici_ICChooseConfig(self, args)
111 iciobject *self;
112 PyObject *args;
113{
114 ICError err;
115
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;
122}
123
124
125static char ici_ICChooseNewConfig__doc__[] =
126"()->None; Let the user choose a new config file"
127;
128
129static PyObject *
130ici_ICChooseNewConfig(self, args)
131 iciobject *self;
132 PyObject *args;
133{
134 ICError err;
135
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;
142}
143
144
145static char ici_ICGetSeed__doc__[] =
146"()->int; Returns int that changes when configuration does"
147;
148
149static PyObject *
150ici_ICGetSeed(self, args)
151 iciobject *self;
152 PyObject *args;
153{
154 ICError err;
155 long seed;
156
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);
162}
163
164
165static char ici_ICBegin__doc__[] =
166"(perm)->None; Lock config file for read/write"
167;
168
169static PyObject *
170ici_ICBegin(self, args)
171 iciobject *self;
172 PyObject *args;
173{
174 ICError err;
175 int perm;
176
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;
183}
184
185
186static char ici_ICFindPrefHandle__doc__[] =
187"(key, handle)->attrs; Lookup key, store result in handle, return attributes"
188;
189
190static PyObject *
191ici_ICFindPrefHandle(self, args)
192 iciobject *self;
193 PyObject *args;
194{
195 ICError err;
196 Str255 key;
197 ICAttr attr;
198 Handle h;
199
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);
205}
206
207
208static char ici_ICSetPref__doc__[] =
209"(key, attr, data)->None; Set preference key to data with attributes"
210;
211
212static PyObject *
213ici_ICSetPref(self, args)
214 iciobject *self;
215 PyObject *args;
216{
217 ICError err;
218 Str255 key;
219 int attr;
220 char *data;
221 int datalen;
222
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;
231}
232
233
234static char ici_ICCountPref__doc__[] =
235"()->int; Return number of preferences"
236;
237
238static PyObject *
239ici_ICCountPref(self, args)
240 iciobject *self;
241 PyObject *args;
242{
243 ICError err;
244 long count;
245
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);
251}
252
253
254static char ici_ICGetIndPref__doc__[] =
255"(num)->key; Return key of preference with given index"
256;
257
258static PyObject *
259ici_ICGetIndPref(self, args)
260 iciobject *self;
261 PyObject *args;
262{
263 ICError err;
264 long num;
265 Str255 key;
266
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);
272}
273
274
275static char ici_ICDeletePref__doc__[] =
276"(key)->None; Delete preference"
277;
278
279static PyObject *
280ici_ICDeletePref(self, args)
281 iciobject *self;
282 PyObject *args;
283{
284 ICError err;
285 Str255 key;
286
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;
293}
294
295
296static char ici_ICEnd__doc__[] =
297"()->None; Unlock file after ICBegin call"
298;
299
300static PyObject *
301ici_ICEnd(self, args)
302 iciobject *self;
303 PyObject *args;
304{
305 ICError err;
306
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;
313}
314
315
316static char ici_ICEditPreferences__doc__[] =
317"(key)->None; Ask user to edit preferences, staring with key"
318;
319
320static PyObject *
321ici_ICEditPreferences(self, args)
322 iciobject *self;
323 PyObject *args;
324{
325 ICError err;
326 Str255 key;
327
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;
334}
335
336
337static char ici_ICParseURL__doc__[] =
338"(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
339;
340
341static PyObject *
342ici_ICParseURL(self, args)
343 iciobject *self;
344 PyObject *args;
345{
346 ICError err;
347 Str255 hint;
348 char *data;
349 int datalen;
350 long selStart, selEnd;
351 Handle h;
352
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);
360}
361
362
363static char ici_ICLaunchURL__doc__[] =
364"(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
365;
366
367static PyObject *
368ici_ICLaunchURL(self, args)
369 iciobject *self;
370 PyObject *args;
371{
372 ICError err;
373 Str255 hint;
374 char *data;
375 int datalen;
376 long selStart, selEnd;
377
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);
385}
386
387
388static char ici_ICMapFilename__doc__[] =
389"(filename)->mapinfo; Get filemap info for given filename"
390;
391
392static PyObject *
393ici_ICMapFilename(self, args)
394 iciobject *self;
395 PyObject *args;
396{
397 ICError err;
398 Str255 filename;
399 ICMapEntry entry;
400
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);
415}
416
417
418static char ici_ICMapTypeCreator__doc__[] =
419"(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
420;
421
422static PyObject *
423ici_ICMapTypeCreator(self, args)
424 iciobject *self;
425 PyObject *args;
426{
427 ICError err;
428 OSType type, creator;
429 Str255 filename;
430 ICMapEntry entry;
431
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);
449}
450
451
452static 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__},
470
471 {NULL, NULL} /* sentinel */
472};
473
474/* ---------- */
475
476
477static iciobject *
478newiciobject(OSType creator)
479{
480 iciobject *self;
481 ICError err;
482
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;
490 }
491 return self;
492}
493
494
495static void
496ici_dealloc(self)
497 iciobject *self;
498{
499 (void)ICStop(self->inst);
500 PyMem_DEL(self);
501}
502
503static PyObject *
504ici_getattr(self, name)
505 iciobject *self;
506 char *name;
507{
508 return Py_FindMethod(ici_methods, (PyObject *)self, name);
509}
510
511static char Icitype__doc__[] =
512"Internet Config instance"
513;
514
515static 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*/
534
535 /* Space for future expansion */
536 0L,0L,0L,0L,
537 Icitype__doc__ /* Documentation string */
538};
539
540/* End of code for ic_instance objects */
541/* -------------------------------------------------------- */
542
543
544static char ic_ICStart__doc__[] =
545"(OSType)->ic_instance; Create an Internet Config instance"
546;
547
548static PyObject *
549ic_ICStart(self, args)
550 PyObject *self; /* Not used */
551 PyObject *args;
552{
553 OSType creator;
554
555 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
556 return NULL;
557 return (PyObject *)newiciobject(creator);
558}
559
560/* List of methods defined in the module */
561
562static struct PyMethodDef ic_methods[] = {
563 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
564
565 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
566};
567
568
569/* Initialization function for the module (*must* be called initicglue) */
570
571static char icglue_module_documentation[] =
572"Implements low-level Internet Config interface"
573;
574
575void
576initicglue()
577{
578 PyObject *m, *d;
579
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);
584
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);
589
590 /* XXXX Add constants here */
591
592 /* Check for errors */
593 if (PyErr_Occurred())
594 Py_FatalError("can't initialize module icglue");
595}
596