1 """IC wrapper module, based on Internet Config 1.3"""
14 icPrefNotFoundErr
= -666 # preference not found (duh!)
15 icPermErr
= -667 # cannot set preference
16 icPrefDataErr
= -668 # problem with preference data
17 icInternalErr
= -669 # hmm, this is not good
18 icTruncatedErr
= -670 # more data was present than was returned
19 icNoMoreWritersErr
= -671 # you cannot begin a write session because someone else is already doing it */
20 icNothingToOverrideErr
= -672 # no component for the override component to capture
21 icNoURLErr
= -673 # no URL found
22 icConfigNotFoundErr
= -674 # no configuration was found
23 icConfigInappropriateErr
= -675 # incorrect manufacturer code
33 """An unparseable IC entry"""
34 def __init__(self
, data
):
38 return "ICOpaqueData(%s)"%`self
.data`
40 _ICOpaqueDataType
=type(ICOpaqueData(''))
42 def _decode_default(data
, key
):
45 if ord(data
[0]) == len(data
)-1:
48 return ICOpaqueData(data
)
51 def _decode_multistr(data
, key
):
52 numstr
= ord(data
[0]) << 8 |
ord(data
[1])
55 for i
in range(numstr
):
56 strlen
= ord(data
[ptr
])
57 str = data
[ptr
+1:ptr
+strlen
+1]
59 ptr
= ptr
+ strlen
+ 1
62 def _decode_fontrecord(data
, key
):
63 size
= ord(data
[0]) << 8 |
ord(data
[1])
65 namelen
= ord(data
[4])
66 return size
, face
, data
[5:5+namelen
]
68 def _decode_boolean(data
, key
):
71 def _decode_text(data
, key
):
74 def _decode_charset(data
, key
):
75 return data
[:256], data
[256:]
77 def _decode_appspec(data
, key
):
78 namelen
= ord(data
[4])
79 return data
[0:4], data
[5:5+namelen
]
81 def _code_default(data
, key
):
82 return chr(len(data
)) + data
84 def _code_multistr(data
, key
):
86 rv
= chr((numstr
>>8) & 0xff) + chr(numstr
& 0xff)
88 rv
= rv
+ _code_default(i
)
91 def _code_fontrecord(data
, key
):
92 size
, face
, name
= data
93 return chr((size
>>8) & 0xff) + chr(size
& 0xff) + chr(face
& 0xff) + \
94 chr(0) + _code_default(name
)
96 def _code_boolean(data
, key
):
97 print 'XXXX boolean:', `data`
100 def _code_text(data
, key
):
103 def _code_charset(data
, key
):
104 return data
[0] + data
[1]
106 def _code_appspec(data
, key
):
107 return data
[0] + _code_default(data
[1])
110 "ArchieAll" : (_decode_multistr
, _code_multistr
),
111 "UMichAll" : (_decode_multistr
, _code_multistr
),
112 "InfoMacAll" : (_decode_multistr
, _code_multistr
),
113 "ListFont" : (_decode_fontrecord
, _code_fontrecord
),
114 "ScreenFont" : (_decode_fontrecord
, _code_fontrecord
),
115 "PrinterFont" : (_decode_fontrecord
, _code_fontrecord
),
116 # "DownloadFolder" : (_decode_filespec , _code_filespec),
117 "Signature": (_decode_text
, _code_text
),
118 "Plan" : (_decode_text
, _code_text
),
119 "MailHeaders" : (_decode_text
, _code_text
),
120 "NewsHeaders" : (_decode_text
, _code_text
),
122 "CharacterSet" : (_decode_charset
, _code_charset
),
123 "Helper\245" : (_decode_appspec
, _code_appspec
),
124 # "Services" : (_decode_services, ????),
125 "NewMailFlashIcon" : (_decode_boolean
, _code_boolean
),
126 "NewMailDialog" : (_decode_boolean
, _code_boolean
),
127 "NewMailPlaySound" : (_decode_boolean
, _code_boolean
),
128 # "WebBackgroundColor" : _decode_color,
129 "NoProxyDomains" : (_decode_multistr
, _code_multistr
),
130 "UseHTTPProxy" : (_decode_boolean
, _code_boolean
),
131 "UseGopherProxy": (_decode_boolean
, _code_boolean
),
132 "UseFTPProxy" : (_decode_boolean
, _code_boolean
),
133 "UsePassiveFTP" : (_decode_boolean
, _code_boolean
),
136 def _decode(data
, key
):
138 key2
= key
[:string
.index(key
, '\245')+1]
141 if _decoder_table
.has_key(key2
):
142 decoder
= _decoder_table
[key2
][0]
144 decoder
= _decode_default
145 return decoder(data
, key
)
147 def _code(data
, key
):
148 if type(data
) == _ICOpaqueDataType
:
151 key2
= key
[:string
.index(key
, '\245')+1]
154 if _decoder_table
.has_key(key2
):
155 coder
= _decoder_table
[key2
][1]
157 coder
= _code_default
158 return coder(data
, key
)
161 def __init__(self
, signature
='Pyth', ic
=None):
165 self
.ic
= icglue
.ICStart(signature
)
166 if hasattr(self
.ic
, 'ICFindConfigFile'):
167 self
.ic
.ICFindConfigFile()
168 self
.h
= Res
.Resource('')
172 self
.ic
.ICBegin(icReadOnlyPerm
)
173 num
= self
.ic
.ICCountPref()
175 rv
.append(self
.ic
.ICGetIndPref(i
+1))
179 def has_key(self
, key
):
180 return self
.__contains
__(key
)
182 def __contains__(self
, key
):
184 dummy
= self
.ic
.ICFindPrefHandle(key
, self
.h
)
189 def __getitem__(self
, key
):
190 attr
= self
.ic
.ICFindPrefHandle(key
, self
.h
)
191 return _decode(self
.h
.data
, key
)
193 def __setitem__(self
, key
, value
):
194 value
= _code(value
, key
)
195 self
.ic
.ICSetPref(key
, ICattr_no_change
, value
)
197 def launchurl(self
, url
, hint
=""):
198 # Work around a bug in ICLaunchURL: file:/foo does
199 # not work but file:///foo does.
200 if url
[:6] == 'file:/' and url
[6] != '/':
201 url
= 'file:///' + url
[6:]
202 self
.ic
.ICLaunchURL(hint
, url
, 0, len(url
))
204 def parseurl(self
, data
, start
=None, end
=None, hint
=""):
209 selStart
= selEnd
= start
212 selStart
, selEnd
= self
.ic
.ICParseURL(hint
, data
, selStart
, selEnd
, self
.h
)
213 return self
.h
.data
, selStart
, selEnd
215 def mapfile(self
, file):
216 if type(file) != type(''):
217 file = file.as_tuple()[2]
218 return self
.ic
.ICMapFilename(file)
220 def maptypecreator(self
, type, creator
, filename
=""):
221 return self
.ic
.ICMapTypeCreator(type, creator
, filename
)
223 def settypecreator(self
, file):
224 file = Carbon
.File
.pathname(file)
225 record
= self
.mapfile(os
.path
.split(file)[1])
226 MacOS
.SetCreatorAndType(file, record
[2], record
[1])
227 macostools
.touched(fss
)
229 # Convenience routines
232 def launchurl(url
, hint
=""):
234 if _dft_ic
== None: _dft_ic
= IC()
235 return _dft_ic
.launchurl(url
, hint
)
237 def parseurl(data
, start
=None, end
=None, hint
=""):
239 if _dft_ic
== None: _dft_ic
= IC()
240 return _dft_ic
.parseurl(data
, start
, end
, hint
)
242 def mapfile(filename
):
244 if _dft_ic
== None: _dft_ic
= IC()
245 return _dft_ic
.mapfile(filename
)
247 def maptypecreator(type, creator
, filename
=""):
249 if _dft_ic
== None: _dft_ic
= IC()
250 return _dft_ic
.maptypecreator(type, creator
, filename
)
252 def settypecreator(file):
254 if _dft_ic
== None: _dft_ic
= IC()
255 return _dft_ic
.settypecreator(file)
267 if __name__
== '__main__':