blob: bd616db9ae11f2d4f6ed744929a20c575a714671 [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 Jansen6143d532001-05-19 12:34:59 +000037#ifdef WITHOUT_FRAMEWORKS
38#if !TARGET_API_MAC_OS8
Jack Jansen726d8732001-01-19 23:45:57 +000039/* 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
Jack Jansen6143d532001-05-19 12:34:59 +000045#endif /* !TARGET_API_MAC_OS8 */
Jack Jansen8ce72f51997-01-07 16:18:32 +000046
47#include "ICAPI.h"
Jack Jansen6143d532001-05-19 12:34:59 +000048#else
49#include <Carbon/Carbon.h>
50typedef 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
Jack Jansen8ce72f51997-01-07 16:18:32 +000060
61static PyObject *ErrorObject;
62
63static PyObject *
64ici_error(ICError err)
65{
66 PyErr_SetObject(ErrorObject, PyInt_FromLong((long)err));
67 return NULL;
68}
69
70/* ----------------------------------------------------- */
71
72/* Declarations for objects of type ic_instance */
73
74typedef struct {
75 PyObject_HEAD
76 ICInstance inst;
77} iciobject;
78
79staticforward PyTypeObject Icitype;
80
81
82
83/* ---------------------------------------------------------------- */
84
Jack Jansen6143d532001-05-19 12:34:59 +000085#if TARGET_API_MAC_OS8
Jack Jansen8ce72f51997-01-07 16:18:32 +000086static char ici_ICFindConfigFile__doc__[] =
87"()->None; Find config file in standard places"
88;
89
90static PyObject *
91ici_ICFindConfigFile(self, args)
92 iciobject *self;
93 PyObject *args;
94{
95 ICError err;
96
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;
103}
104
105
106static char ici_ICFindUserConfigFile__doc__[] =
107"(vRefNum, dirID)->None; Find config file in specified place"
108;
109
110static PyObject *
111ici_ICFindUserConfigFile(self, args)
112 iciobject *self;
113 PyObject *args;
114{
115 ICError err;
116 ICDirSpec where;
117
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;
124}
125
126
127static char ici_ICChooseConfig__doc__[] =
128"()->None; Let the user choose a config file"
129;
130
131static PyObject *
132ici_ICChooseConfig(self, args)
133 iciobject *self;
134 PyObject *args;
135{
136 ICError err;
137
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;
144}
Jack Jansen8ce72f51997-01-07 16:18:32 +0000145
146static char ici_ICChooseNewConfig__doc__[] =
147"()->None; Let the user choose a new config file"
148;
149
150static PyObject *
151ici_ICChooseNewConfig(self, args)
152 iciobject *self;
153 PyObject *args;
154{
155 ICError err;
156
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;
163}
Jack Jansen6143d532001-05-19 12:34:59 +0000164#endif /* TARGET_API_MAC_OS8 */
Jack Jansen8ce72f51997-01-07 16:18:32 +0000165
166
167static char ici_ICGetSeed__doc__[] =
168"()->int; Returns int that changes when configuration does"
169;
170
171static PyObject *
172ici_ICGetSeed(self, args)
173 iciobject *self;
174 PyObject *args;
175{
176 ICError err;
177 long seed;
178
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);
184}
185
186
187static char ici_ICBegin__doc__[] =
188"(perm)->None; Lock config file for read/write"
189;
190
191static PyObject *
192ici_ICBegin(self, args)
193 iciobject *self;
194 PyObject *args;
195{
196 ICError err;
197 int perm;
198
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;
205}
206
207
208static char ici_ICFindPrefHandle__doc__[] =
209"(key, handle)->attrs; Lookup key, store result in handle, return attributes"
210;
211
212static PyObject *
213ici_ICFindPrefHandle(self, args)
214 iciobject *self;
215 PyObject *args;
216{
217 ICError err;
218 Str255 key;
219 ICAttr attr;
220 Handle h;
221
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);
227}
228
229
230static char ici_ICSetPref__doc__[] =
231"(key, attr, data)->None; Set preference key to data with attributes"
232;
233
234static PyObject *
235ici_ICSetPref(self, args)
236 iciobject *self;
237 PyObject *args;
238{
239 ICError err;
240 Str255 key;
241 int attr;
242 char *data;
243 int datalen;
244
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;
253}
254
255
256static char ici_ICCountPref__doc__[] =
257"()->int; Return number of preferences"
258;
259
260static PyObject *
261ici_ICCountPref(self, args)
262 iciobject *self;
263 PyObject *args;
264{
265 ICError err;
266 long count;
267
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);
273}
274
275
276static char ici_ICGetIndPref__doc__[] =
277"(num)->key; Return key of preference with given index"
278;
279
280static PyObject *
281ici_ICGetIndPref(self, args)
282 iciobject *self;
283 PyObject *args;
284{
285 ICError err;
286 long num;
287 Str255 key;
288
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);
294}
295
296
297static char ici_ICDeletePref__doc__[] =
298"(key)->None; Delete preference"
299;
300
301static PyObject *
302ici_ICDeletePref(self, args)
303 iciobject *self;
304 PyObject *args;
305{
306 ICError err;
307 Str255 key;
308
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;
315}
316
317
318static char ici_ICEnd__doc__[] =
319"()->None; Unlock file after ICBegin call"
320;
321
322static PyObject *
323ici_ICEnd(self, args)
324 iciobject *self;
325 PyObject *args;
326{
327 ICError err;
328
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;
335}
336
337
338static char ici_ICEditPreferences__doc__[] =
339"(key)->None; Ask user to edit preferences, staring with key"
340;
341
342static PyObject *
343ici_ICEditPreferences(self, args)
344 iciobject *self;
345 PyObject *args;
346{
347 ICError err;
348 Str255 key;
349
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;
356}
357
358
359static char ici_ICParseURL__doc__[] =
360"(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
361;
362
363static PyObject *
364ici_ICParseURL(self, args)
365 iciobject *self;
366 PyObject *args;
367{
368 ICError err;
369 Str255 hint;
370 char *data;
371 int datalen;
372 long selStart, selEnd;
373 Handle h;
374
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);
382}
383
384
385static char ici_ICLaunchURL__doc__[] =
386"(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
387;
388
389static PyObject *
390ici_ICLaunchURL(self, args)
391 iciobject *self;
392 PyObject *args;
393{
394 ICError err;
395 Str255 hint;
396 char *data;
397 int datalen;
398 long selStart, selEnd;
399
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);
407}
408
409
410static char ici_ICMapFilename__doc__[] =
411"(filename)->mapinfo; Get filemap info for given filename"
412;
413
414static PyObject *
415ici_ICMapFilename(self, args)
416 iciobject *self;
417 PyObject *args;
418{
419 ICError err;
420 Str255 filename;
421 ICMapEntry entry;
422
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);
437}
438
439
440static char ici_ICMapTypeCreator__doc__[] =
441"(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
442;
443
444static PyObject *
445ici_ICMapTypeCreator(self, args)
446 iciobject *self;
447 PyObject *args;
448{
449 ICError err;
450 OSType type, creator;
451 Str255 filename;
452 ICMapEntry entry;
453
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);
471}
472
473
474static struct PyMethodDef ici_methods[] = {
Jack Jansen6143d532001-05-19 12:34:59 +0000475#if TARGET_API_MAC_OS8
Jack Jansen5a8115c2001-01-29 13:27:46 +0000476 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
Jack Jansen8ce72f51997-01-07 16:18:32 +0000477 {"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__},
Jack Jansen6143d532001-05-19 12:34:59 +0000480#endif /* TARGET_API_MAC_OS8 */
Jack Jansen8ce72f51997-01-07 16:18:32 +0000481 {"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__},
494
495 {NULL, NULL} /* sentinel */
496};
497
498/* ---------- */
499
500
501static iciobject *
502newiciobject(OSType creator)
503{
504 iciobject *self;
505 ICError err;
506
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;
514 }
515 return self;
516}
517
518
519static void
520ici_dealloc(self)
521 iciobject *self;
522{
523 (void)ICStop(self->inst);
524 PyMem_DEL(self);
525}
526
527static PyObject *
528ici_getattr(self, name)
529 iciobject *self;
530 char *name;
531{
532 return Py_FindMethod(ici_methods, (PyObject *)self, name);
533}
534
535static char Icitype__doc__[] =
536"Internet Config instance"
537;
538
539static 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*/
558
559 /* Space for future expansion */
560 0L,0L,0L,0L,
561 Icitype__doc__ /* Documentation string */
562};
563
564/* End of code for ic_instance objects */
565/* -------------------------------------------------------- */
566
567
568static char ic_ICStart__doc__[] =
569"(OSType)->ic_instance; Create an Internet Config instance"
570;
571
572static PyObject *
573ic_ICStart(self, args)
574 PyObject *self; /* Not used */
575 PyObject *args;
576{
577 OSType creator;
578
579 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
580 return NULL;
581 return (PyObject *)newiciobject(creator);
582}
583
584/* List of methods defined in the module */
585
586static struct PyMethodDef ic_methods[] = {
587 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
588
589 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
590};
591
592
593/* Initialization function for the module (*must* be called initicglue) */
594
595static char icglue_module_documentation[] =
596"Implements low-level Internet Config interface"
597;
598
599void
600initicglue()
601{
602 PyObject *m, *d;
603
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);
608
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);
613
614 /* XXXX Add constants here */
615
616 /* Check for errors */
617 if (PyErr_Occurred())
618 Py_FatalError("can't initialize module icglue");
619}
620