summaryrefslogtreecommitdiffstats
path: root/Mac/Modules/icgluemodule.c
blob: c974a0c10f4b520ffcaf4b151da9694c0de6b700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/***********************************************************
Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

#include "Python.h"
#include "macglue.h"

extern int ResObj_Convert(PyObject *, Handle *); /* From Resmodule.c */

#ifdef WITHOUT_FRAMEWORKS
// #if !TARGET_API_MAC_OS8
// /* The Carbon headers define PRAGMA_ALIGN_SUPPORT to something illegal,
// ** because you shouldn't use it for Carbon. All good and well, but portable
// ** code still needs it. So, we undefine it here.
// */
// #undef PRAGMA_ALIGN_SUPPORTED
// #define PRAGMA_ALIGN_SUPPORTED 0
// #endif /* !TARGET_API_MAC_OS8 */

// #include "ICAPI.h"
#include <InternetConfig.h>
#else
#include <Carbon/Carbon.h>
#endif

static PyObject *ErrorObject;

/* ----------------------------------------------------- */

/* Declarations for objects of type ic_instance */

typedef struct {
	PyObject_HEAD
	ICInstance inst;
} iciobject;

static PyTypeObject Icitype;



/* ---------------------------------------------------------------- */

#if TARGET_API_MAC_OS8
static char ici_ICFindConfigFile__doc__[] = 
"()->None; Find config file in standard places"
;

static PyObject *
ici_ICFindConfigFile(iciobject *self, PyObject *args)
{
	OSStatus err;
	
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	if ((err=ICFindConfigFile(self->inst, 0, NULL)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICFindUserConfigFile__doc__[] = 
"(vRefNum, dirID)->None; Find config file in specified place"
;

static PyObject *
ici_ICFindUserConfigFile(iciobject *self, PyObject *args)
{
	OSStatus err;
	ICDirSpec where;	

	if (!PyArg_ParseTuple(args, "sl", &where.vRefNum, &where.dirID))
		return NULL;
	if ((err=ICFindUserConfigFile(self->inst, &where)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICChooseConfig__doc__[] = 
"()->None; Let the user choose a config file"
;

static PyObject *
ici_ICChooseConfig(iciobject *self, PyObject *args)
{
	OSStatus err;
	
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	if ((err=ICChooseConfig(self->inst)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}

static char ici_ICChooseNewConfig__doc__[] = 
"()->None; Let the user choose a new config file"
;

static PyObject *
ici_ICChooseNewConfig(iciobject *self, PyObject *args)
{
	OSStatus err;
	
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	if ((err=ICChooseNewConfig(self->inst)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}
#endif /* TARGET_API_MAC_OS8 */


static char ici_ICGetSeed__doc__[] = 
"()->int; Returns int that changes when configuration does"
;

static PyObject *
ici_ICGetSeed(iciobject *self, PyObject *args)
{
	OSStatus err;
	long seed;
	
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	if ((err=ICGetSeed(self->inst, &seed)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("i", (int)seed);
}


static char ici_ICBegin__doc__[] = 
"(perm)->None; Lock config file for read/write"
;

static PyObject *
ici_ICBegin(iciobject *self, PyObject *args)
{
	OSStatus err;
	int perm;
	
	if (!PyArg_ParseTuple(args, "i", &perm))
		return NULL;
	if ((err=ICBegin(self->inst, (ICPerm)perm)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICFindPrefHandle__doc__[] = 
"(key, handle)->attrs; Lookup key, store result in handle, return attributes"
;

static PyObject *
ici_ICFindPrefHandle(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 key;
	ICAttr attr;
	Handle h;
	
	if (!PyArg_ParseTuple(args, "O&O&", PyMac_GetStr255, &key, ResObj_Convert, &h))
		return NULL;
	if ((err=ICFindPrefHandle(self->inst, key, &attr, h)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("i", (int)attr);
}


static char ici_ICSetPref__doc__[] = 
"(key, attr, data)->None; Set preference key to data with attributes"
;

static PyObject *
ici_ICSetPref(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 key;
	int attr;
	char *data;
	int datalen;
	
	if (!PyArg_ParseTuple(args, "O&is#", PyMac_GetStr255, &key, &attr, 
					&data, &datalen))
		return NULL;
	if ((err=ICSetPref(self->inst, key, (ICAttr)attr, (Ptr)data, 
			(long)datalen)) != 0)
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICCountPref__doc__[] = 
"()->int; Return number of preferences"
;

static PyObject *
ici_ICCountPref(iciobject *self, PyObject *args)
{
	OSStatus err;
	long count;
	
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	if ((err=ICCountPref(self->inst, &count)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("i", (int)count);
}


static char ici_ICGetIndPref__doc__[] = 
"(num)->key; Return key of preference with given index"
;

static PyObject *
ici_ICGetIndPref(iciobject *self, PyObject *args)
{
	OSStatus err;
	long num;
	Str255 key;
	
	if (!PyArg_ParseTuple(args, "l", &num))
		return NULL;
	if ((err=ICGetIndPref(self->inst, num, key)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("O&", PyMac_BuildStr255, key);
}


static char ici_ICDeletePref__doc__[] = 
"(key)->None; Delete preference"
;

static PyObject *
ici_ICDeletePref(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 key;

	if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
		return NULL;
	if ((err=ICDeletePref(self->inst, key)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICEnd__doc__[] = 
"()->None; Unlock file after ICBegin call"
;

static PyObject *
ici_ICEnd(iciobject *self, PyObject *args)
{
	OSStatus err;
	
	if (!PyArg_ParseTuple(args, ""))
		return NULL;
	if ((err=ICEnd(self->inst)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICEditPreferences__doc__[] = 
"(key)->None; Ask user to edit preferences, staring with key"
;

static PyObject *
ici_ICEditPreferences(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 key;
	
	if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, key))
		return NULL;
	if ((err=ICEditPreferences(self->inst, key)) != 0 )
		return PyMac_Error(err);
	Py_INCREF(Py_None);
	return Py_None;
}


static char ici_ICParseURL__doc__[] = 
"(hint, data, selStart, selEnd, handle)->selStart, selEnd; Find an URL, return in handle"
;

static PyObject *
ici_ICParseURL(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 hint;
	char *data;
	int datalen;
	long selStart, selEnd;
	Handle h;
	
	if (!PyArg_ParseTuple(args, "O&s#llO&", PyMac_GetStr255, hint, &data, &datalen,
				&selStart, &selEnd, ResObj_Convert, &h))
		return NULL;
	if ((err=ICParseURL(self->inst, hint, (Ptr)data, (long)datalen,
				&selStart, &selEnd, h)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("ii", (int)selStart, (int)selEnd);
}


static char ici_ICLaunchURL__doc__[] = 
"(hint, data, selStart, selEnd)->None; Find an URL and launch the correct app"
;

static PyObject *
ici_ICLaunchURL(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 hint;
	char *data;
	int datalen;
	long selStart, selEnd;
	
	if (!PyArg_ParseTuple(args, "O&s#ll", PyMac_GetStr255, hint, &data, &datalen,
				&selStart, &selEnd))
		return NULL;
	if ((err=ICLaunchURL(self->inst, hint, (Ptr)data, (long)datalen,
				&selStart, &selEnd)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("ii", (int)selStart, (int)selEnd);
}


static char ici_ICMapFilename__doc__[] = 
"(filename)->mapinfo; Get filemap info for given filename"
;

static PyObject *
ici_ICMapFilename(iciobject *self, PyObject *args)
{
	OSStatus err;
	Str255 filename;
	ICMapEntry entry;
	
	if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, filename))
		return NULL;
	if ((err=ICMapFilename(self->inst, filename, &entry)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version, 
		PyMac_BuildOSType, entry.fileType,
		PyMac_BuildOSType, entry.fileCreator, 
		PyMac_BuildOSType, entry.postCreator, 
		entry.flags,
		PyMac_BuildStr255, entry.extension,
		PyMac_BuildStr255, entry.creatorAppName,
		PyMac_BuildStr255, entry.postAppName,
		PyMac_BuildStr255, entry.MIMEType,
		PyMac_BuildStr255, entry.entryName);
}


static char ici_ICMapTypeCreator__doc__[] = 
"(type, creator, filename)->mapinfo; Get filemap info for given tp/cr/filename"
;

static PyObject *
ici_ICMapTypeCreator(iciobject *self, PyObject *args)
{
	OSStatus err;
	OSType type, creator;
	Str255 filename;
	ICMapEntry entry;
	
	if (!PyArg_ParseTuple(args, "O&O&O&",
			PyMac_GetOSType, &type,
			PyMac_GetOSType, &creator,
			PyMac_GetStr255, filename))
		return NULL;
	if ((err=ICMapTypeCreator(self->inst, type, creator, filename, &entry)) != 0 )
		return PyMac_Error(err);
	return Py_BuildValue("hO&O&O&lO&O&O&O&O&", entry.version, 
		PyMac_BuildOSType, entry.fileType,
		PyMac_BuildOSType, entry.fileCreator, 
		PyMac_BuildOSType, entry.postCreator, 
		entry.flags,
		PyMac_BuildStr255, entry.extension,
		PyMac_BuildStr255, entry.creatorAppName,
		PyMac_BuildStr255, entry.postAppName,
		PyMac_BuildStr255, entry.MIMEType,
		PyMac_BuildStr255, entry.entryName);
}


static struct PyMethodDef ici_methods[] = {
#if TARGET_API_MAC_OS8
 {"ICFindConfigFile",	(PyCFunction)ici_ICFindConfigFile,	METH_VARARGS,	ici_ICFindConfigFile__doc__},
 {"ICFindUserConfigFile",	(PyCFunction)ici_ICFindUserConfigFile,	METH_VARARGS,	ici_ICFindUserConfigFile__doc__},
 {"ICChooseConfig",	(PyCFunction)ici_ICChooseConfig,	METH_VARARGS,	ici_ICChooseConfig__doc__},
 {"ICChooseNewConfig",	(PyCFunction)ici_ICChooseNewConfig,	METH_VARARGS,	ici_ICChooseNewConfig__doc__},
#endif /* TARGET_API_MAC_OS8 */
 {"ICGetSeed",	(PyCFunction)ici_ICGetSeed,	METH_VARARGS,	ici_ICGetSeed__doc__},
 {"ICBegin",	(PyCFunction)ici_ICBegin,	METH_VARARGS,	ici_ICBegin__doc__},
 {"ICFindPrefHandle",	(PyCFunction)ici_ICFindPrefHandle,	METH_VARARGS,	ici_ICFindPrefHandle__doc__},
 {"ICSetPref",	(PyCFunction)ici_ICSetPref,	METH_VARARGS,	ici_ICSetPref__doc__},
 {"ICCountPref",	(PyCFunction)ici_ICCountPref,	METH_VARARGS,	ici_ICCountPref__doc__},
 {"ICGetIndPref",	(PyCFunction)ici_ICGetIndPref,	METH_VARARGS,	ici_ICGetIndPref__doc__},
 {"ICDeletePref",	(PyCFunction)ici_ICDeletePref,	METH_VARARGS,	ici_ICDeletePref__doc__},
 {"ICEnd",	(PyCFunction)ici_ICEnd,	METH_VARARGS,	ici_ICEnd__doc__},
 {"ICEditPreferences",	(PyCFunction)ici_ICEditPreferences,	METH_VARARGS,	ici_ICEditPreferences__doc__},
 {"ICParseURL",	(PyCFunction)ici_ICParseURL,	METH_VARARGS,	ici_ICParseURL__doc__},
 {"ICLaunchURL",	(PyCFunction)ici_ICLaunchURL,	METH_VARARGS,	ici_ICLaunchURL__doc__},
 {"ICMapFilename",	(PyCFunction)ici_ICMapFilename,	METH_VARARGS,	ici_ICMapFilename__doc__},
 {"ICMapTypeCreator",	(PyCFunction)ici_ICMapTypeCreator,	METH_VARARGS,	ici_ICMapTypeCreator__doc__},
 
	{NULL,		NULL}		/* sentinel */
};

/* ---------- */


static iciobject *
newiciobject(OSType creator)
{
	iciobject *self;
	OSStatus err;
	
	self = PyObject_NEW(iciobject, &Icitype);
	if (self == NULL)
		return NULL;
	if ((err=ICStart(&self->inst, creator)) != 0 ) {
		(void)PyMac_Error(err);
		PyObject_DEL(self);
		return NULL;
	}
	return self;
}


static void
ici_dealloc(iciobject *self)
{
	(void)ICStop(self->inst);
	PyObject_DEL(self);
}

static PyObject *
ici_getattr(iciobject *self, char *name)
{
	return Py_FindMethod(ici_methods, (PyObject *)self, name);
}

static char Icitype__doc__[] = 
"Internet Config instance"
;

static PyTypeObject Icitype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,				/*ob_size*/
	"icglue.ic_instance",		/*tp_name*/
	sizeof(iciobject),		/*tp_basicsize*/
	0,				/*tp_itemsize*/
	/* methods */
	(destructor)ici_dealloc,	/*tp_dealloc*/
	(printfunc)0,		/*tp_print*/
	(getattrfunc)ici_getattr,	/*tp_getattr*/
	(setattrfunc)0,	/*tp_setattr*/
	(cmpfunc)0,		/*tp_compare*/
	(reprfunc)0,		/*tp_repr*/
	0,			/*tp_as_number*/
	0,		/*tp_as_sequence*/
	0,		/*tp_as_mapping*/
	(hashfunc)0,		/*tp_hash*/
	(ternaryfunc)0,		/*tp_call*/
	(reprfunc)0,		/*tp_str*/

	/* Space for future expansion */
	0L,0L,0L,0L,
	Icitype__doc__ /* Documentation string */
};

/* End of code for ic_instance objects */
/* -------------------------------------------------------- */


static char ic_ICStart__doc__[] =
"(OSType)->ic_instance; Create an Internet Config instance"
;

static PyObject *
ic_ICStart(PyObject *self, PyObject *args)
{
	OSType creator;

	if (!PyArg_ParseTuple(args, "O&", PyMac_GetOSType, &creator))
		return NULL;
	return (PyObject *)newiciobject(creator);
}

/* List of methods defined in the module */

static struct PyMethodDef ic_methods[] = {
	{"ICStart",	(PyCFunction)ic_ICStart,	METH_VARARGS,	ic_ICStart__doc__},
 
	{NULL,	 (PyCFunction)NULL, 0, NULL}		/* sentinel */
};


/* Initialization function for the module (*must* be called initicglue) */

static char icglue_module_documentation[] = 
"Implements low-level Internet Config interface"
;

void
initicglue(void)
{
	PyObject *m, *d;

	/* Create the module and add the functions */
	m = Py_InitModule4("icglue", ic_methods,
		icglue_module_documentation,
		(PyObject*)NULL,PYTHON_API_VERSION);

	/* Add some symbolic constants to the module */
	d = PyModule_GetDict(m);
	ErrorObject = PyMac_GetOSErrException();
	if (ErrorObject == NULL ||
	    PyDict_SetItemString(d, "error", ErrorObject) != 0)
		return;

	/* XXXX Add constants here */
	
	/* Check for errors */
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module icglue");
}