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