PyXR

c:\projects\bitpim\src \ phones \ com_samsungspha790.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: com_samsungspha790.py 4365 2007-08-17 21:11:59Z djpham $
0009 
0010 """Communicate with a Samsung SPH-A790"""
0011 # System modules
0012 import sha
0013 
0014 # BitPim modules
0015 import common
0016 import com_samsung_packet
0017 import p_samsungspha790
0018 
0019 
0020 parentphone=com_samsung_packet.Phone
0021 class Phone(parentphone):
0022     desc='SPH-A790'
0023     protocolclass=p_samsungspha790
0024     serialsname='spha790'
0025 
0026     builtin_ringtones=(
0027         (0, ['Default Tone']),
0028         (68, ['Tone %d'%x for x in range(1, 10)]),
0029         (95, ['Ring %d'%x for x in range(1, 16)]),
0030         (110, ['Melody %d'%x for x in range(1, 21)]),
0031         )
0032     builtin_pictures=(
0033         (1, ['People %d'%x for x in range(1, 22)]),
0034         (23, ['Animal %d'%x for x in range(1, 16)]),
0035         (38, ['Other %d'%x for x in range(1, 10)]),
0036         )
0037 
0038     camera_picture_index=100    # starting index of camera picture
0039 
0040     def getfundamentals(self, results):
0041 
0042         """Gets information fundamental to interopating with the phone and UI.
0043 
0044         Currently this is:
0045 
0046           - 'uniqueserial'     a unique serial number representing the phone
0047           - 'groups'           the phonebook groups
0048           - 'wallpaper-index'  map index numbers to names
0049           - 'ringtone-index'   map index numbers to ringtone names
0050 
0051         This method is called before we read the phonebook data or before we
0052         write phonebook data.
0053         """
0054         self.setmode(self.MODEPHONEBOOK)
0055 
0056         # use a hash of ESN and other stuff (being paranoid)
0057         self.log("Retrieving fundamental phone information")
0058         self.log("Reading phone serial number")
0059         results['uniqueserial']=sha.new(self.get_esn()).hexdigest()
0060 
0061         # now read groups
0062         self.log("Reading group information")
0063         results['groups']=self.read_groups()
0064 
0065         # getting rintone-index
0066         results['ringtone-index']=self._get_builtin_index(self.builtin_ringtones)
0067         
0068         # getting wallpaper-index
0069         results['wallpaper-index']=self.get_wallpaper_index()
0070         
0071         self.setmode(self.MODEMODEM)
0072         self.log("Fundamentals retrieved")
0073 
0074         return results
0075 
0076     def _get_builtin_index(self, builtin_list):
0077         _res={}
0078         for _starting_idx,_list in builtin_list:
0079             _idx=_starting_idx
0080             for _entry in _list:
0081                 _res[_idx]={ 'name': _entry,
0082                              'origin': 'builtin' }
0083                 _idx+=1
0084         return _res
0085 
0086     def read_groups(self):
0087         g={}
0088         # Don't crash if phone doesn't accept #PMODE=1 (Canadian phones)
0089         try:
0090             self.setmode(self.MODEPHONEBOOK)
0091         except:
0092             return g
0093         req=self.protocolclass.groupnamerequest()
0094         for i in range(self.protocolclass.NUMGROUPS):
0095             req.gid=i
0096             # Don't crash if phone doesn't support groups
0097             try:
0098                 res=self.sendpbcommand(req, self.protocolclass.groupnameresponse)
0099                 if res[0].entry.groupname and \
0100                    res[0].entry.groupname!='Empty':
0101                     g[i]={'name': res[0].entry.groupname}
0102             except Exception,e:
0103                 return g
0104         return g
0105 
0106     # phonebook stuff----------------------------------------------------------
0107     def _extract_misc(self, res, entry, fundamentals):
0108         _name={ 'full': entry.name }
0109         if entry.nick:
0110             _name['nickname']=entry.nick
0111         res['names']=[_name]
0112         _grp=fundamentals.get('groups', {}).get(entry.group, None)
0113         if _grp.get('name', None):
0114             res['categories']=[{ 'category': _grp['name'] }]
0115         if entry.email:
0116             res['emails']=[{ 'email': entry.email }]
0117         if entry.url:
0118             res['urls']=[{ 'url': entry.url }]
0119         if entry.memo:
0120             res['memos']=[{ 'memo': entry.memo }]
0121 
0122     def _extract_wallpaper(self, res, entry, fundamentals):
0123         _wp_index=fundamentals.get('wallpaper-index', {})
0124         _wp_name=_wp_index.get(entry.wallpaper, {}).get('name', None)
0125         if _wp_name:
0126             res['wallpapers']=[{ 'wallpaper': _wp_name,  'use': 'call' }]
0127 
0128     def _extract_ringtone(self, res, entry, fundamentals):
0129         _rt_index=fundamentals.get('ringtone-index', {})
0130         _rt_name=_rt_index.get(entry.ringtone, {}).get('name', None)
0131         if _rt_name:
0132             res['ringtones']=[{ 'ringtone': _rt_name, 'use': 'call' }]
0133 
0134     number_type_dict={
0135         0: 'cell', 1: 'home', 2: 'office', 3: 'pager', 4: 'fax' }
0136     def _extract_number(self, res, entry, idx, fundamentals):
0137         _item=entry.numbers[idx]
0138         if not _item.number:
0139             return None
0140         _res={ 'number': _item.number,
0141                'type': self.number_type_dict.get(idx, 'none') }
0142         # TODO: speed dial
0143         return _res
0144     def _extract_numbers(self, res, entry, fundamentals):
0145         _primary=entry.primary-1
0146         _numbers=[self._extract_number(res, entry, _primary, fundamentals)]
0147         for i in range(self.protocolclass.NUMPHONENUMBERS):
0148             if i==_primary:
0149                 continue
0150             _number=self._extract_number(res, entry, i, fundamentals)
0151             if _number:
0152                 _numbers.append(_number)
0153         res['numbers']=_numbers
0154         
0155     def extractphonebookentry(self, entry, fundamentals):
0156         res={}
0157         res['serials']=[ { 'sourcetype': self.serialsname,
0158                            'sourceuniqueid': fundamentals['uniqueserial'],
0159                            'slot': entry.slot,
0160                            'uslsot': entry.uslot }]
0161         _args=(res, entry, fundamentals)
0162         self._extract_misc(*_args)
0163         self._extract_wallpaper(*_args)
0164         self._extract_ringtone(*_args)
0165         self._extract_numbers(*_args)
0166         return res
0167 
0168     def getphonebook(self, result):
0169         """Read the phonebook data."""
0170         pbook={}
0171         self.setmode(self.MODEPHONEBOOK)
0172 
0173         req=self.protocolclass.phonebookslotrequest()
0174         lastname=""
0175         for slot in range(1,self.protocolclass.NUMPHONEBOOKENTRIES+1):
0176             req.slot=slot
0177             res=self.sendpbcommand(req, self.protocolclass.phonebookslotresponse, fixup=self.pblinerepair)
0178             if res:
0179                 self.log(`slot`+": "+res[0].entry.name)
0180                 entry=self.extractphonebookentry(res[0].entry, result)
0181                 pbook[res[0].entry.uslot]=entry
0182             self.progress(slot, self.protocolclass.NUMPHONEBOOKENTRIES, lastname)
0183         
0184         result['phonebook']=pbook
0185 ##        cats=[]
0186 ##        for i in result['groups']:
0187 ##            if result['groups'][i]['name']!='Unassigned':
0188 ##                cats.append(result['groups'][i]['name'])
0189 ##        result['categories']=cats
0190         print "returning keys",result.keys()
0191         
0192         return pbook
0193 
0194 
0195     # wallpaper stuff----------------------------------------------------------
0196     def get_wallpaper_index(self):
0197         _res=self._get_builtin_index(self.builtin_pictures)
0198         self.setmode(self.MODEBREW)
0199         _files=self.listfiles(self.protocolclass.WP_CAMERA_PATH)
0200         _idx=self.camera_picture_index
0201         for _pathname in _files:
0202             _res[_idx]={ 'name': common.basename(_pathname)+'.jpg',
0203                          'origin': 'camera' }
0204             _idx+=1
0205         self.setmode(self.MODEMODEM)
0206         return _res
0207 
0208     def getwallpapers(self, fundamentals):
0209         """Read camera pictures from the phone"""
0210         self.log('Reading Camera Pictures')
0211         self.setmode(self.MODEBREW)
0212         _files=self.listfiles(self.protocolclass.WP_CAMERA_PATH)
0213         _media={}
0214         for _pathname in _files:
0215             _filename=common.basename(_pathname)+'.jpg'
0216             try:
0217                 _media[_filename]=self.getfilecontents(_pathname, True)
0218             except Exception,e:
0219                 self.log('Failed to read file %s due to: %s'%(_pathname, str(e)))
0220         self.setmode(self.MODEMODEM)
0221         fundamentals['wallpapers']=_media
0222         return fundamentals
0223 
0224     getringtones=NotImplemented
0225 
0226     @classmethod
0227     def detectphone(_, coms, likely_ports, res, _module, _log):
0228         pass
0229 
0230 #-------------------------------------------------------------------------------
0231 parentprofile=com_samsung_packet.Profile
0232 class Profile(parentprofile):
0233     protocolclass=Phone.protocolclass
0234     serialsname=Phone.serialsname
0235     # for phone detection
0236     phone_manufacturer='SAMSUNG'
0237     phone_model='SPH-A790/118'
0238 
0239     WALLPAPER_WIDTH=176
0240     WALLPAPER_HEIGHT=220
0241     # Outside LCD: 128x96
0242 
0243     def __init__(self):
0244         parentprofile.__init__(self)
0245 
0246     def convertphonebooktophone(self, helper, data):
0247         return data
0248 
0249     _supportedsyncs=(
0250         ('phonebook', 'read', None),  # all phonebook reading
0251 ##        ('phonebook', 'write', 'OVERWRITE'),  # only overwriting phonebook
0252         ('wallpaper', 'read', None),  # all wallpaper reading
0253 ##        ('wallpaper', 'write', None), # Image conversion needs work
0254 ##        ('ringtone', 'read', None),   # all ringtone reading
0255 ##        ('ringtone', 'write', None),
0256 ##        ('calendar', 'read', None),   # all calendar reading
0257 ##        ('calendar', 'write', 'OVERWRITE'),   # only overwriting calendar
0258 ##        ('todo', 'read', None),     # all todo list reading
0259 ##        ('todo', 'write', 'OVERWRITE'),  # all todo list writing
0260 ##        ('memo', 'read', None),     # all memo list reading
0261 ##        ('memo', 'write', 'OVERWRITE'),  # all memo list writing
0262         )
0263 

Generated by PyXR 0.9.4