blob: b6174399ca9388d4d835eae74f23de2842c23ea9 [file] [log] [blame]
Jack Jansen8ce72f51997-01-07 16:18:32 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Jack Jansen8ce72f51997-01-07 16:18:32 +00003The 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
Jack Jansen726d8732001-01-19 23:45:57 +000037#if TARGET_API_MAC_CARBON
38/* The Carbon headers define PRAGMA_ALIGN_SUPPORT to something illegal,
39** because you shouldn't use it for Carbon. All good and well, but portable
40** code still needs it. So, we undefine it here.
41*/
42#undef PRAGMA_ALIGN_SUPPORTED
43#define PRAGMA_ALIGN_SUPPORTED 0
44#endif /* TARGET_API_MAC_CARBON */
Jack Jansen8ce72f51997-01-07 16:18:32 +000045
46#include "ICAPI.h"
47
48static PyObject *ErrorObject;
49
50static PyObject *
51ici_error(ICError err)
52{
53 PyErr_SetObject(ErrorObject, PyInt_FromLong((long)err));
54 return NULL;
55}
56
57/* ----------------------------------------------------- */
58
59/* Declarations for objects of type ic_instance */
60
61typedef struct {
62 PyObject_HEAD
63 ICInstance inst;
64} iciobject;
65
66staticforward PyTypeObject Icitype;
67
68
69
70/* ---------------------------------------------------------------- */
71
Jack Jansen5a8115c2001-01-29 13:27:46 +000072#if !TARGET_API_MAC_CARBON
Jack Jansen8ce72f51997-01-07 16:18:32 +000073static char ici_ICFindConfigFile__doc__[] =
74"()->None; Find config file in standard places"
75;
76
77static PyObject *
78ici_ICFindConfigFile(self, args)
79 iciobject *self;
80 PyObject *args;
81{
82 ICError err;
83
84 if (!PyArg_ParseTuple(args, ""))
85 return NULL;
86 if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
87 return ici_error(err);
88 Py_INCREF(Py_None);
89 return Py_None;
90}
91
92
93static char ici_ICFindUserConfigFile__doc__[] =
94"(vRefNum, dirID)->None; Find config file in specified place"
95;
96
97static PyObject *
98ici_ICFindUserConfigFile(self, args)
99 iciobject *self;
100 PyObject *args;
101{
102 ICError err;
103 ICDirSpec where;
104
105 if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
106 return NULL;
107 if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
108 return ici_error(err);
109 Py_INCREF(Py_None);
110 return Py_None;
111}
112
113
114static char ici_ICChooseConfig__doc__[] =
115"()->None; Let the user choose a config file"
116;
117
118static PyObject *
119ici_ICChooseConfig(self, args)
120 iciobject *self;
121 PyObject *args;
122{
123 ICError err;
124
125 if (!PyArg_ParseTuple(args, ""))
126 return NULL;
127 if ((err=ICChooseConfig(self->inst)) != 0 )
128 return ici_error(err);
129 Py_INCREF(Py_None);
130 return Py_None;
131}
Jack Jansen5a8115c2001-01-29 13:27:46 +0000132#endif /* !TARGET_API_MAC_CARBON */
Jack Jansen8ce72f51997-01-07 16:18:32 +0000133
134
135static char ici_ICChooseNewConfig__doc__[] =
136"()->None; Let the user choose a new config file"
137;
138
139static PyObject *
140ici_ICChooseNewConfig(self, args)
141 iciobject *self;
142 PyObject *args;
143{
144 ICError err;
145
146 if (!PyArg_ParseTuple(args, ""))
147 return NULL;
148 if ((err=ICChooseNewConfig(self->inst)) != 0 )
149 return ici_error(err);
150 Py_INCREF(Py_None);
151 return Py_None;
152}
153
154
155static char ici_ICGetSeed__doc__[] =
156"()->int; Returns int that changes when configuration does"
157;
158
159static PyObject *
160ici_ICGetSeed(self, args)
161 iciobject *self;
162 PyObject *args;
163{
164 ICError err;
165 long seed;
166
167 if (!PyArg_ParseTuple(args, ""))
168 return NULL;
169 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
170 return ici_error(err);
171 return Py_BuildValue("i", (int)seed);
172}
173
174
175static char ici_ICBegin__doc__[] =
176"(perm)->None; Lock config file for read/write"
177;
178
179static PyObject *
180ici_ICBegin(self, args)
181 iciobject *self;
182 PyObject *args;
183{
184 ICError err;
185 int perm;
186
187 if (!PyArg_ParseTuple(args, "i", &perm))
188 return NULL;
189 if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
190 return ici_error(err);
191 Py_INCREF(Py_None);
192 return Py_None;
193}
194
195
196static char ici_ICFindPrefHandle__doc__[] =
197"(key, handle)->attrs; Lookup key, store result in handle, return attributes"
198;
199
200static PyObject *
201ici_ICFindPrefHandle(self, args)
202 iciobject *self;
203 PyObject *args;
204{
205 ICError err;
206 Str255 key;
207 ICAttr attr;
208 Handle h;
209
210 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
211 return NULL;
212 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
213 return ici_error(err);
214 return Py_BuildValue("i", (int)attr);
215}
216
217
218static char ici_ICSetPref__doc__[] =
219"(key, attr, data)->None; Set preference key to data with attributes"
220;
221
222static PyObject *
223ici_ICSetPref(self, args)
224 iciobject *self;
225 PyObject *args;
226{
227 ICError err;
228 Str255 key;
229 int attr;
230 char *data;
231 int datalen;
232
233 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
234 &data, &datalen))
235 return NULL;
236 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
237 (long)datalen)) != 0)
238 return ici_error(err);
239 Py_INCREF(Py_None);
240 return Py_None;
241}
242
243
244static char ici_ICCountPref__doc__[] =
245"()->int; Return number of preferences"
246;
247
248static PyObject *
249ici_ICCountPref(self, args)
250 iciobject *self;
251 PyObject *args;
252{
253 ICError err;
254 long count;
255
256 if (!PyArg_ParseTuple(args, ""))
257 return NULL;
258 if ((err=ICCountPref(self->inst, &count)) != 0 )
259 return ici_error(err);
260 return Py_BuildValue("i", (int)count);
261}
262
263
264static char ici_ICGetIndPref__doc__[] =
265"(num)->key; Return key of preference with given index"
266;
267
268static PyObject *
269ici_ICGetIndPref(self, args)
270 iciobject *self;
271 PyObject *args;
272{
273 ICError err;
274 long num;
275 Str255 key;
276
277 if (!PyArg_ParseTuple(args, "l", &num))
278 return NULL;
279 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
280 return ici_error(err);
281 return Py_BuildValue("O&", PyMac_BuildStr255, key);
282}
283
284
285static char ici_ICDeletePref__doc__[] =
286"(key)->None; Delete preference"
287;
288
289static PyObject *
290ici_ICDeletePref(self, args)
291 iciobject *self;
292 PyObject *args;
293{
294 ICError err;
295 Str255 key;
296
297 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
298 return NULL;
299 if ((err=ICDeletePref(self->inst, key)) != 0 )
300 return ici_error(err);
301 Py_INCREF(Py_None);
302 return Py_None;
303}
304
305
306static char ici_ICEnd__doc__[] =
307"()->None; Unlock file after ICBegin call"
308;
309
310static PyObject *
311ici_ICEnd(self, args)
312 iciobject *self;
313 PyObject *args;
314{
315 ICError err;
316
317 if (!PyArg_ParseTuple(args, ""))
318 return NULL;
319 if ((err=ICEnd(self->inst)) != 0 )
320 return ici_error(err);
321 Py_INCREF(Py_None);
322 return Py_None;
323}
324
325
326static char ici_ICEditPreferences__doc__[] =
327"(key)->None; Ask user to edit preferences, staring with key"
328;
329
330static PyObject *
331ici_ICEditPreferences(self, args)
332 iciobject *self;
333 PyObject *args;
334{
335 ICError err;
336 Str255 key;
337
338 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
339 return NULL;
340 if ((err=ICEditPreferences(self->inst, key)) != 0 )
341 return ici_error(err);
342 Py_INCREF(Py_None);
343 return Py_None;
344}
345
346
347static char ici_ICParseURL__doc__[] =
348"(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
349;
350
351static PyObject *
352ici_ICParseURL(self, args)
353 iciobject *self;
354 PyObject *args;
355{
356 ICError err;
357 Str255 hint;
358 char *data;
359 int datalen;
360 long selStart, selEnd;
361 Handle h;
362
363 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
364 &selStart, &selEnd, ResObj_Convert, &h))
365 return NULL;
366 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
367 &selStart, &selEnd, h)) != 0 )
368 return ici_error(err);
369 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
370}
371
372
373static char ici_ICLaunchURL__doc__[] =
374"(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
375;
376
377static PyObject *
378ici_ICLaunchURL(self, args)
379 iciobject *self;
380 PyObject *args;
381{
382 ICError err;
383 Str255 hint;
384 char *data;
385 int datalen;
386 long selStart, selEnd;
387
388 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
389 &selStart, &selEnd))
390 return NULL;
391 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
392 &selStart, &selEnd)) != 0 )
393 return ici_error(err);
394 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
395}
396
397
398static char ici_ICMapFilename__doc__[] =
399"(filename)->mapinfo; Get filemap info for given filename"
400;
401
402static PyObject *
403ici_ICMapFilename(self, args)
404 iciobject *self;
405 PyObject *args;
406{
407 ICError err;
408 Str255 filename;
409 ICMapEntry entry;
410
411 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
412 return NULL;
413 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
414 return ici_error(err);
415 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
416 PyMac_BuildOSType, entry.file_type,
417 PyMac_BuildOSType, entry.file_creator,
418 PyMac_BuildOSType, entry.post_creator,
419 entry.flags,
420 PyMac_BuildStr255, entry.extension,
421 PyMac_BuildStr255, entry.creator_app_name,
422 PyMac_BuildStr255, entry.post_app_name,
423 PyMac_BuildStr255, entry.MIME_type,
424 PyMac_BuildStr255, entry.entry_name);
425}
426
427
428static char ici_ICMapTypeCreator__doc__[] =
429"(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
430;
431
432static PyObject *
433ici_ICMapTypeCreator(self, args)
434 iciobject *self;
435 PyObject *args;
436{
437 ICError err;
438 OSType type, creator;
439 Str255 filename;
440 ICMapEntry entry;
441
442 if (!PyArg_ParseTuple(args, "O&O&O&",
443 PyMac_GetOSType, &type,
444 PyMac_GetOSType, &creator,
445 PyMac_GetStr255, filename))
446 return NULL;
447 if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
448 return ici_error(err);
449 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
450 PyMac_BuildOSType, entry.file_type,
451 PyMac_BuildOSType, entry.file_creator,
452 PyMac_BuildOSType, entry.post_creator,
453 entry.flags,
454 PyMac_BuildStr255, entry.extension,
455 PyMac_BuildStr255, entry.creator_app_name,
456 PyMac_BuildStr255, entry.post_app_name,
457 PyMac_BuildStr255, entry.MIME_type,
458 PyMac_BuildStr255, entry.entry_name);
459}
460
461
462static struct PyMethodDef ici_methods[] = {
Jack Jansen5a8115c2001-01-29 13:27:46 +0000463#if !TARGET_API_MAC_CARBON
464 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
Jack Jansen8ce72f51997-01-07 16:18:32 +0000465 {"ICFindUserConfigFile", (PyCFunction)ici_ICFindUserConfigFile, METH_VARARGS, ici_ICFindUserConfigFile__doc__},
466 {"ICChooseConfig", (PyCFunction)ici_ICChooseConfig, METH_VARARGS, ici_ICChooseConfig__doc__},
467 {"ICChooseNewConfig", (PyCFunction)ici_ICChooseNewConfig, METH_VARARGS, ici_ICChooseNewConfig__doc__},
Jack Jansen5a8115c2001-01-29 13:27:46 +0000468#endif /* !TARGET_API_MAC_CARBON */
Jack Jansen8ce72f51997-01-07 16:18:32 +0000469 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
470 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
471 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
472 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
473 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
474 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
475 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
476 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
477 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
478 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
479 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
480 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
481 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
482
483 {NULL, NULL} /* sentinel */
484};
485
486/* ---------- */
487
488
489static iciobject *
490newiciobject(OSType creator)
491{
492 iciobject *self;
493 ICError err;
494
495 self = PyObject_NEW(iciobject, &Icitype);
496 if (self == NULL)
497 return NULL;
498 if ((err=ICStart(&self->inst, creator)) != 0 ) {
499 (void)ici_error(err);
500 PyMem_DEL(self);
501 return NULL;
502 }
503 return self;
504}
505
506
507static void
508ici_dealloc(self)
509 iciobject *self;
510{
511 (void)ICStop(self->inst);
512 PyMem_DEL(self);
513}
514
515static PyObject *
516ici_getattr(self, name)
517 iciobject *self;
518 char *name;
519{
520 return Py_FindMethod(ici_methods, (PyObject *)self, name);
521}
522
523static char Icitype__doc__[] =
524"Internet Config instance"
525;
526
527static PyTypeObject Icitype = {
528 PyObject_HEAD_INIT(&PyType_Type)
529 0, /*ob_size*/
530 "ic_instance", /*tp_name*/
531 sizeof(iciobject), /*tp_basicsize*/
532 0, /*tp_itemsize*/
533 /* methods */
534 (destructor)ici_dealloc, /*tp_dealloc*/
535 (printfunc)0, /*tp_print*/
536 (getattrfunc)ici_getattr, /*tp_getattr*/
537 (setattrfunc)0, /*tp_setattr*/
538 (cmpfunc)0, /*tp_compare*/
539 (reprfunc)0, /*tp_repr*/
540 0, /*tp_as_number*/
541 0, /*tp_as_sequence*/
542 0, /*tp_as_mapping*/
543 (hashfunc)0, /*tp_hash*/
544 (ternaryfunc)0, /*tp_call*/
545 (reprfunc)0, /*tp_str*/
546
547 /* Space for future expansion */
548 0L,0L,0L,0L,
549 Icitype__doc__ /* Documentation string */
550};
551
552/* End of code for ic_instance objects */
553/* -------------------------------------------------------- */
554
555
556static char ic_ICStart__doc__[] =
557"(OSType)->ic_instance; Create an Internet Config instance"
558;
559
560static PyObject *
561ic_ICStart(self, args)
562 PyObject *self; /* Not used */
563 PyObject *args;
564{
565 OSType creator;
566
567 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
568 return NULL;
569 return (PyObject *)newiciobject(creator);
570}
571
572/* List of methods defined in the module */
573
574static struct PyMethodDef ic_methods[] = {
575 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
576
577 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
578};
579
580
581/* Initialization function for the module (*must* be called initicglue) */
582
583static char icglue_module_documentation[] =
584"Implements low-level Internet Config interface"
585;
586
587void
588initicglue()
589{
590 PyObject *m, *d;
591
592 /* Create the module and add the functions */
593 m = Py_InitModule4("icglue", ic_methods,
594 icglue_module_documentation,
595 (PyObject*)NULL,PYTHON_API_VERSION);
596
597 /* Add some symbolic constants to the module */
598 d = PyModule_GetDict(m);
599 ErrorObject = PyString_FromString("icglue.error");
600 PyDict_SetItemString(d, "error", ErrorObject);
601
602 /* XXXX Add constants here */
603
604 /* Check for errors */
605 if (PyErr_Occurred())
606 Py_FatalError("can't initialize module icglue");
607}
608