Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame^] | 1 | """IC wrapper module, based on Internet Config 1.3""" |
| 2 | |
| 3 | import icglue |
| 4 | import string |
| 5 | import sys |
| 6 | import Res |
| 7 | import macfs |
| 8 | import macostools |
| 9 | |
| 10 | error=icglue.error |
| 11 | |
| 12 | # From ictypes.h: |
| 13 | icPrefNotFoundErr = -666 # preference not found (duh!) |
| 14 | icPermErr = -667 # cannot set preference |
| 15 | icPrefDataErr = -668 # problem with preference data |
| 16 | icInternalErr = -669 # hmm, this is not good |
| 17 | icTruncatedErr = -670 # more data was present than was returned |
| 18 | icNoMoreWritersErr = -671 # you cannot begin a write session because someone else is already doing it */ |
| 19 | icNothingToOverrideErr = -672 # no component for the override component to capture |
| 20 | icNoURLErr = -673 # no URL found |
| 21 | icConfigNotFoundErr = -674 # no configuration was found |
| 22 | icConfigInappropriateErr = -675 # incorrect manufacturer code |
| 23 | |
| 24 | ICattr_no_change = -1 |
| 25 | |
| 26 | icNoPerm = 0 |
| 27 | icReadOnlyPerm = 1 |
| 28 | icReadWritePerm = 2 |
| 29 | # End of ictypes.h |
| 30 | |
| 31 | def _decode_default(data, key): |
| 32 | if len(data) == 0: |
| 33 | return data |
| 34 | if ord(data[0]) == len(data)-1: |
| 35 | # Assume Pstring |
| 36 | return data[1:] |
| 37 | raise error, "Unknown data format for key "+key |
| 38 | |
| 39 | def _decode_multistr(data, key): |
| 40 | numstr = ord(data[0]) << 8 | ord(data[1]) |
| 41 | rv = [] |
| 42 | ptr = 2 |
| 43 | for i in range(numstr): |
| 44 | strlen = ord(data[ptr]) |
| 45 | str = data[ptr+1:ptr+strlen+1] |
| 46 | rv.append(str) |
| 47 | ptr = ptr + strlen + 1 |
| 48 | return rv |
| 49 | |
| 50 | def _decode_fontrecord(data, key): |
| 51 | size = ord(data[0]) << 8 | ord(data[1]) |
| 52 | face = ord(data[2]) |
| 53 | namelen = ord(data[4]) |
| 54 | return size, face, data[5:5+namelen] |
| 55 | |
| 56 | def _decode_boolean(data, key): |
| 57 | print 'XXXX boolean:', `data` |
| 58 | return ord(data[0]) |
| 59 | |
| 60 | def _decode_text(data, key): |
| 61 | return data |
| 62 | |
| 63 | def _decode_charset(data, key): |
| 64 | return data[:256], data[256:] |
| 65 | |
| 66 | def _decode_appspec(data, key): |
| 67 | namelen = ord(data[4]) |
| 68 | return data[0:4], data[5:5+namelen] |
| 69 | |
| 70 | def _code_default(data, key): |
| 71 | return chr(len(data)) + data |
| 72 | |
| 73 | def _code_multistr(data, key): |
| 74 | numstr = len(data) |
| 75 | rv = chr((numstr>>8) & 0xff) + chr(numstr & 0xff) |
| 76 | for i in data: |
| 77 | rv = rv + _code_default(i) |
| 78 | return rv |
| 79 | |
| 80 | def _code_fontrecord(data, key): |
| 81 | size, face, name = data |
| 82 | return chr((size>>8) & 0xff) + chr(size & 0xff) + chr(face & 0xff) + \ |
| 83 | chr(0) + _code_default(name) |
| 84 | |
| 85 | def _code_boolean(data, key): |
| 86 | print 'XXXX boolean:', `data` |
| 87 | return chr(data) |
| 88 | |
| 89 | def _code_text(data, key): |
| 90 | return data |
| 91 | |
| 92 | def _code_charset(data, key): |
| 93 | return data[0] + data[1] |
| 94 | |
| 95 | def _code_appspec(data, key): |
| 96 | return data[0] + _code_default(data[1]) |
| 97 | |
| 98 | _decoder_table = { |
| 99 | "ArchieAll" : (_decode_multistr , _code_multistr), |
| 100 | "UMichAll" : (_decode_multistr , _code_multistr), |
| 101 | "InfoMacAll" : (_decode_multistr , _code_multistr), |
| 102 | "ListFont" : (_decode_fontrecord , _code_fontrecord), |
| 103 | "ScreenFont" : (_decode_fontrecord , _code_fontrecord), |
| 104 | "PrinterFont" : (_decode_fontrecord , _code_fontrecord), |
| 105 | # "DownloadFolder" : (_decode_filespec , _code_filespec), |
| 106 | "Signature": (_decode_text , _code_text), |
| 107 | "Plan" : (_decode_text , _code_text), |
| 108 | "MailHeaders" : (_decode_text , _code_text), |
| 109 | "NewsHeaders" : (_decode_text , _code_text), |
| 110 | # "Mapping" |
| 111 | "CharacterSet" : (_decode_charset , _code_charset), |
| 112 | "Helper\245" : (_decode_appspec , _code_appspec), |
| 113 | # "Services" : (_decode_services, ????), |
| 114 | "NewMailFlashIcon" : (_decode_boolean , _code_boolean), |
| 115 | "NewMailDialog" : (_decode_boolean , _code_boolean), |
| 116 | "NewMailPlaySound" : (_decode_boolean , _code_boolean), |
| 117 | # "WebBackgroundColor" : _decode_color, |
| 118 | "NoProxyDomains" : (_decode_multistr , _code_multistr), |
| 119 | "UseHTTPProxy" : (_decode_boolean , _code_boolean), |
| 120 | "UseGopherProxy": (_decode_boolean , _code_boolean), |
| 121 | "UseFTPProxy" : (_decode_boolean , _code_boolean), |
| 122 | "UsePassiveFTP" : (_decode_boolean , _code_boolean), |
| 123 | } |
| 124 | |
| 125 | def _decode(data, key): |
| 126 | if '\245' in key: |
| 127 | key2 = key[:string.index(key, '\245')+1] |
| 128 | else: |
| 129 | key2 = key |
| 130 | if _decoder_table.has_key(key2): |
| 131 | decoder = _decoder_table[key2][0] |
| 132 | else: |
| 133 | decoder = _decode_default |
| 134 | return decoder(data, key) |
| 135 | |
| 136 | def _code(data, key): |
| 137 | if '\245' in key: |
| 138 | key2 = key[:string.index(key, '\245')+1] |
| 139 | else: |
| 140 | key2 = key |
| 141 | if _decoder_table.has_key(key2): |
| 142 | coder = _decoder_table[key2][1] |
| 143 | else: |
| 144 | coder = _code_default |
| 145 | return coder(data, key) |
| 146 | |
| 147 | class IC: |
| 148 | def __init__(self, signature='Pyth', ic=None): |
| 149 | if ic: |
| 150 | self.ic = ic |
| 151 | else: |
| 152 | self.ic = icglue.ICStart(signature) |
| 153 | self.ic.ICFindConfigFile() |
| 154 | self.h = Res.Resource('') |
| 155 | |
| 156 | def keys(self): |
| 157 | rv = [] |
| 158 | self.ic.ICBegin(icReadOnlyPerm) |
| 159 | num = self.ic.ICCountPref() |
| 160 | for i in range(num): |
| 161 | rv.append(self.ic.ICGetIndPref(i+1)) |
| 162 | self.ic.ICEnd() |
| 163 | return rv |
| 164 | |
| 165 | def __getitem__(self, key): |
| 166 | attr = self.ic.ICFindPrefHandle(key, self.h) |
| 167 | return _decode(self.h.data, key) |
| 168 | |
| 169 | def __setitem__(self, key, value): |
| 170 | value = _code(value, key) |
| 171 | self.ic.ICSetPref(key, ICattr_no_change, value) |
| 172 | |
| 173 | def launchurl(self, url, hint=""): |
| 174 | self.ic.ICLaunchURL(hint, url, 0, len(url)) |
| 175 | |
| 176 | def parseurl(self, data, start=None, end=None, hint=""): |
| 177 | if start == None: |
| 178 | selStart = 0 |
| 179 | selEnd = len(data) |
| 180 | else: |
| 181 | selStart = selEnd = start |
| 182 | if end != None: |
| 183 | selEnd = end |
| 184 | selStart, selEnd = self.ic.ICParseURL(hint, data, selStart, selEnd, self.h) |
| 185 | return self.h.data, selStart, selEnd |
| 186 | |
| 187 | def mapfile(self, file): |
| 188 | if type(file) != type(''): |
| 189 | file = file.as_tuple()[2] |
| 190 | return self.ic.ICMapFilename(file) |
| 191 | |
| 192 | def maptypecreator(self, type, creator, filename=""): |
| 193 | return self.ic.ICMapTypeCreator(type, creator, filename) |
| 194 | |
| 195 | def settypecreator(self, file): |
| 196 | if type(file) == type(''): |
| 197 | fss = macfs.FSSpec(file) |
| 198 | else: |
| 199 | fss = file |
| 200 | name = fss.as_tuple()[2] |
| 201 | record = self.mapfile(name) |
| 202 | fss.SetCreatorType(record[2], record[1]) |
| 203 | macostools.touched(fss) |
| 204 | |
| 205 | # Convenience routines |
| 206 | _dft_ic = None |
| 207 | |
| 208 | def launchurl(url, hint=""): |
| 209 | global _dft_ic |
| 210 | if _dft_ic == None: _dft_ic = IC() |
| 211 | return _dft_ic.launchurl(url, hint) |
| 212 | |
| 213 | def parseurl(data, start=None, end=None, hint=""): |
| 214 | global _dft_ic |
| 215 | if _dft_ic == None: _dft_ic = IC() |
| 216 | return _dft_ic.parseurl(data, start, end, hint) |
| 217 | |
| 218 | def mapfile(filename): |
| 219 | global _dft_ic |
| 220 | if _dft_ic == None: _dft_ic = IC() |
| 221 | return _dft_ic.mapfile(filename) |
| 222 | |
| 223 | def maptypecreator(type, creator, filename=""): |
| 224 | global _dft_ic |
| 225 | if _dft_ic == None: _dft_ic = IC() |
| 226 | return _dft_ic.maptypecreator(type, creator, filename) |
| 227 | |
| 228 | def settypecreator(file): |
| 229 | global _dft_ic |
| 230 | if _dft_ic == None: _dft_ic = IC() |
| 231 | return _dft_ic.settypecreator(file) |
| 232 | |
| 233 | def _test(): |
| 234 | ic = IC() |
| 235 | for k in ic.keys(): |
| 236 | try: |
| 237 | v = ic[k] |
| 238 | except error: |
| 239 | v = '????' |
| 240 | print k, '\t', v |
| 241 | sys.exit(1) |
| 242 | |
| 243 | if __name__ == '__main__': |
| 244 | _test() |
| 245 | |