blob: c0e56afe61cc89c5c926da8b1bf568c3d609f903 [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 *
Jack Jansen1767f932001-09-04 22:20:39 +000091ici_ICFindConfigFile(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +000092{
93 ICError err;
94
95 if (!PyArg_ParseTuple(args, ""))
96 return NULL;
97 if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
98 return ici_error(err);
99 Py_INCREF(Py_None);
100 return Py_None;
101}
102
103
104static char ici_ICFindUserConfigFile__doc__[] =
105"(vRefNum, dirID)->None; Find config file in specified place"
106;
107
108static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000109ici_ICFindUserConfigFile(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000110{
111 ICError err;
112 ICDirSpec where;
113
114 if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
115 return NULL;
116 if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
117 return ici_error(err);
118 Py_INCREF(Py_None);
119 return Py_None;
120}
121
122
123static char ici_ICChooseConfig__doc__[] =
124"()->None; Let the user choose a config file"
125;
126
127static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000128ici_ICChooseConfig(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000129{
130 ICError err;
131
132 if (!PyArg_ParseTuple(args, ""))
133 return NULL;
134 if ((err=ICChooseConfig(self->inst)) != 0 )
135 return ici_error(err);
136 Py_INCREF(Py_None);
137 return Py_None;
138}
Jack Jansen8ce72f51997-01-07 16:18:32 +0000139
140static char ici_ICChooseNewConfig__doc__[] =
141"()->None; Let the user choose a new config file"
142;
143
144static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000145ici_ICChooseNewConfig(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000146{
147 ICError err;
148
149 if (!PyArg_ParseTuple(args, ""))
150 return NULL;
151 if ((err=ICChooseNewConfig(self->inst)) != 0 )
152 return ici_error(err);
153 Py_INCREF(Py_None);
154 return Py_None;
155}
Jack Jansen6143d532001-05-19 12:34:59 +0000156#endif /* TARGET_API_MAC_OS8 */
Jack Jansen8ce72f51997-01-07 16:18:32 +0000157
158
159static char ici_ICGetSeed__doc__[] =
160"()->int; Returns int that changes when configuration does"
161;
162
163static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000164ici_ICGetSeed(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000165{
166 ICError err;
167 long seed;
168
169 if (!PyArg_ParseTuple(args, ""))
170 return NULL;
171 if ((err=ICGetSeed(self->inst, &seed)) != 0 )
172 return ici_error(err);
173 return Py_BuildValue("i", (int)seed);
174}
175
176
177static char ici_ICBegin__doc__[] =
178"(perm)->None; Lock config file for read/write"
179;
180
181static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000182ici_ICBegin(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000183{
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 *
Jack Jansen1767f932001-09-04 22:20:39 +0000201ici_ICFindPrefHandle(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000202{
203 ICError err;
204 Str255 key;
205 ICAttr attr;
206 Handle h;
207
208 if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
209 return NULL;
210 if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
211 return ici_error(err);
212 return Py_BuildValue("i", (int)attr);
213}
214
215
216static char ici_ICSetPref__doc__[] =
217"(key, attr, data)->None; Set preference key to data with attributes"
218;
219
220static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000221ici_ICSetPref(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000222{
223 ICError err;
224 Str255 key;
225 int attr;
226 char *data;
227 int datalen;
228
229 if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr,
230 &data, &datalen))
231 return NULL;
232 if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data,
233 (long)datalen)) != 0)
234 return ici_error(err);
235 Py_INCREF(Py_None);
236 return Py_None;
237}
238
239
240static char ici_ICCountPref__doc__[] =
241"()->int; Return number of preferences"
242;
243
244static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000245ici_ICCountPref(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000246{
247 ICError err;
248 long count;
249
250 if (!PyArg_ParseTuple(args, ""))
251 return NULL;
252 if ((err=ICCountPref(self->inst, &count)) != 0 )
253 return ici_error(err);
254 return Py_BuildValue("i", (int)count);
255}
256
257
258static char ici_ICGetIndPref__doc__[] =
259"(num)->key; Return key of preference with given index"
260;
261
262static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000263ici_ICGetIndPref(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000264{
265 ICError err;
266 long num;
267 Str255 key;
268
269 if (!PyArg_ParseTuple(args, "l", &num))
270 return NULL;
271 if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
272 return ici_error(err);
273 return Py_BuildValue("O&", PyMac_BuildStr255, key);
274}
275
276
277static char ici_ICDeletePref__doc__[] =
278"(key)->None; Delete preference"
279;
280
281static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000282ici_ICDeletePref(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000283{
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 *
Jack Jansen1767f932001-09-04 22:20:39 +0000301ici_ICEnd(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000302{
303 ICError err;
304
305 if (!PyArg_ParseTuple(args, ""))
306 return NULL;
307 if ((err=ICEnd(self->inst)) != 0 )
308 return ici_error(err);
309 Py_INCREF(Py_None);
310 return Py_None;
311}
312
313
314static char ici_ICEditPreferences__doc__[] =
315"(key)->None; Ask user to edit preferences, staring with key"
316;
317
318static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000319ici_ICEditPreferences(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000320{
321 ICError err;
322 Str255 key;
323
324 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
325 return NULL;
326 if ((err=ICEditPreferences(self->inst, key)) != 0 )
327 return ici_error(err);
328 Py_INCREF(Py_None);
329 return Py_None;
330}
331
332
333static char ici_ICParseURL__doc__[] =
334"(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
335;
336
337static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000338ici_ICParseURL(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000339{
340 ICError err;
341 Str255 hint;
342 char *data;
343 int datalen;
344 long selStart, selEnd;
345 Handle h;
346
347 if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
348 &selStart, &selEnd, ResObj_Convert, &h))
349 return NULL;
350 if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
351 &selStart, &selEnd, h)) != 0 )
352 return ici_error(err);
353 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
354}
355
356
357static char ici_ICLaunchURL__doc__[] =
358"(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
359;
360
361static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000362ici_ICLaunchURL(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000363{
364 ICError err;
365 Str255 hint;
366 char *data;
367 int datalen;
368 long selStart, selEnd;
369
370 if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
371 &selStart, &selEnd))
372 return NULL;
373 if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
374 &selStart, &selEnd)) != 0 )
375 return ici_error(err);
376 return Py_BuildValue("ii", (int)selStart, (int)selEnd);
377}
378
379
380static char ici_ICMapFilename__doc__[] =
381"(filename)->mapinfo; Get filemap info for given filename"
382;
383
384static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000385ici_ICMapFilename(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000386{
387 ICError err;
388 Str255 filename;
389 ICMapEntry entry;
390
391 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
392 return NULL;
393 if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
394 return ici_error(err);
395 return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version,
396 PyMac_BuildOSType, entry.file_type,
397 PyMac_BuildOSType, entry.file_creator,
398 PyMac_BuildOSType, entry.post_creator,
399 entry.flags,
400 PyMac_BuildStr255, entry.extension,
401 PyMac_BuildStr255, entry.creator_app_name,
402 PyMac_BuildStr255, entry.post_app_name,
403 PyMac_BuildStr255, entry.MIME_type,
404 PyMac_BuildStr255, entry.entry_name);
405}
406
407
408static char ici_ICMapTypeCreator__doc__[] =
409"(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
410;
411
412static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000413ici_ICMapTypeCreator(iciobject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000414{
415 ICError err;
416 OSType type, creator;
417 Str255 filename;
418 ICMapEntry entry;
419
420 if (!PyArg_ParseTuple(args, "O&O&O&",
421 PyMac_GetOSType, &type,
422 PyMac_GetOSType, &creator,
423 PyMac_GetStr255, filename))
424 return NULL;
425 if ((err=ICMapTypeCreator(self->inst, type, creator, 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 struct PyMethodDef ici_methods[] = {
Jack Jansen6143d532001-05-19 12:34:59 +0000441#if TARGET_API_MAC_OS8
Jack Jansen5a8115c2001-01-29 13:27:46 +0000442 {"ICFindConfigFile", (PyCFunction)ici_ICFindConfigFile, METH_VARARGS, ici_ICFindConfigFile__doc__},
Jack Jansen8ce72f51997-01-07 16:18:32 +0000443 {"ICFindUserConfigFile", (PyCFunction)ici_ICFindUserConfigFile, METH_VARARGS, ici_ICFindUserConfigFile__doc__},
444 {"ICChooseConfig", (PyCFunction)ici_ICChooseConfig, METH_VARARGS, ici_ICChooseConfig__doc__},
445 {"ICChooseNewConfig", (PyCFunction)ici_ICChooseNewConfig, METH_VARARGS, ici_ICChooseNewConfig__doc__},
Jack Jansen6143d532001-05-19 12:34:59 +0000446#endif /* TARGET_API_MAC_OS8 */
Jack Jansen8ce72f51997-01-07 16:18:32 +0000447 {"ICGetSeed", (PyCFunction)ici_ICGetSeed, METH_VARARGS, ici_ICGetSeed__doc__},
448 {"ICBegin", (PyCFunction)ici_ICBegin, METH_VARARGS, ici_ICBegin__doc__},
449 {"ICFindPrefHandle", (PyCFunction)ici_ICFindPrefHandle, METH_VARARGS, ici_ICFindPrefHandle__doc__},
450 {"ICSetPref", (PyCFunction)ici_ICSetPref, METH_VARARGS, ici_ICSetPref__doc__},
451 {"ICCountPref", (PyCFunction)ici_ICCountPref, METH_VARARGS, ici_ICCountPref__doc__},
452 {"ICGetIndPref", (PyCFunction)ici_ICGetIndPref, METH_VARARGS, ici_ICGetIndPref__doc__},
453 {"ICDeletePref", (PyCFunction)ici_ICDeletePref, METH_VARARGS, ici_ICDeletePref__doc__},
454 {"ICEnd", (PyCFunction)ici_ICEnd, METH_VARARGS, ici_ICEnd__doc__},
455 {"ICEditPreferences", (PyCFunction)ici_ICEditPreferences, METH_VARARGS, ici_ICEditPreferences__doc__},
456 {"ICParseURL", (PyCFunction)ici_ICParseURL, METH_VARARGS, ici_ICParseURL__doc__},
457 {"ICLaunchURL", (PyCFunction)ici_ICLaunchURL, METH_VARARGS, ici_ICLaunchURL__doc__},
458 {"ICMapFilename", (PyCFunction)ici_ICMapFilename, METH_VARARGS, ici_ICMapFilename__doc__},
459 {"ICMapTypeCreator", (PyCFunction)ici_ICMapTypeCreator, METH_VARARGS, ici_ICMapTypeCreator__doc__},
460
461 {NULL, NULL} /* sentinel */
462};
463
464/* ---------- */
465
466
467static iciobject *
468newiciobject(OSType creator)
469{
470 iciobject *self;
471 ICError err;
472
473 self = PyObject_NEW(iciobject, &Icitype);
474 if (self == NULL)
475 return NULL;
476 if ((err=ICStart(&self->inst, creator)) != 0 ) {
477 (void)ici_error(err);
478 PyMem_DEL(self);
479 return NULL;
480 }
481 return self;
482}
483
484
485static void
Jack Jansen1767f932001-09-04 22:20:39 +0000486ici_dealloc(iciobject *self)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000487{
488 (void)ICStop(self->inst);
489 PyMem_DEL(self);
490}
491
492static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000493ici_getattr(iciobject *self, char *name)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000494{
495 return Py_FindMethod(ici_methods, (PyObject *)self, name);
496}
497
498static char Icitype__doc__[] =
499"Internet Config instance"
500;
501
502static PyTypeObject Icitype = {
503 PyObject_HEAD_INIT(&PyType_Type)
504 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000505 "icglue.ic_instance", /*tp_name*/
Jack Jansen8ce72f51997-01-07 16:18:32 +0000506 sizeof(iciobject), /*tp_basicsize*/
507 0, /*tp_itemsize*/
508 /* methods */
509 (destructor)ici_dealloc, /*tp_dealloc*/
510 (printfunc)0, /*tp_print*/
511 (getattrfunc)ici_getattr, /*tp_getattr*/
512 (setattrfunc)0, /*tp_setattr*/
513 (cmpfunc)0, /*tp_compare*/
514 (reprfunc)0, /*tp_repr*/
515 0, /*tp_as_number*/
516 0, /*tp_as_sequence*/
517 0, /*tp_as_mapping*/
518 (hashfunc)0, /*tp_hash*/
519 (ternaryfunc)0, /*tp_call*/
520 (reprfunc)0, /*tp_str*/
521
522 /* Space for future expansion */
523 0L,0L,0L,0L,
524 Icitype__doc__ /* Documentation string */
525};
526
527/* End of code for ic_instance objects */
528/* -------------------------------------------------------- */
529
530
531static char ic_ICStart__doc__[] =
532"(OSType)->ic_instance; Create an Internet Config instance"
533;
534
535static PyObject *
Jack Jansen1767f932001-09-04 22:20:39 +0000536ic_ICStart(PyObject *self, PyObject *args)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000537{
538 OSType creator;
539
540 if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
541 return NULL;
542 return (PyObject *)newiciobject(creator);
543}
544
545/* List of methods defined in the module */
546
547static struct PyMethodDef ic_methods[] = {
548 {"ICStart", (PyCFunction)ic_ICStart, METH_VARARGS, ic_ICStart__doc__},
549
550 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
551};
552
553
554/* Initialization function for the module (*must* be called initicglue) */
555
556static char icglue_module_documentation[] =
557"Implements low-level Internet Config interface"
558;
559
560void
Jack Jansen1767f932001-09-04 22:20:39 +0000561initicglue(void)
Jack Jansen8ce72f51997-01-07 16:18:32 +0000562{
563 PyObject *m, *d;
564
565 /* Create the module and add the functions */
566 m = Py_InitModule4("icglue", ic_methods,
567 icglue_module_documentation,
568 (PyObject*)NULL,PYTHON_API_VERSION);
569
570 /* Add some symbolic constants to the module */
571 d = PyModule_GetDict(m);
572 ErrorObject = PyString_FromString("icglue.error");
573 PyDict_SetItemString(d, "error", ErrorObject);
574
575 /* XXXX Add constants here */
576
577 /* Check for errors */
578 if (PyErr_Occurred())
579 Py_FatalError("can't initialize module icglue");
580}
581