contacts.py
来自「python s60 1.4.5版本的源代码」· Python 代码 · 共 567 行 · 第 1/2 页
PY
567 行
last_modified=property(lambda self:self._data('lastmodified'))
def _get_title(self):
first_name_indexes = self._contact.find_field_indexes(_contacts.given_name_value)
last_name_indexes = self._contact.find_field_indexes(_contacts.family_name_value)
title_str = u""
for index in last_name_indexes:
title_str += self._contact.get_field(index)['value'] + u" "
for index in first_name_indexes:
title_str += self._contact.get_field(index)['value'] + u" "
if len(title_str)>0:
title_str = title_str[:len(title_str)-1]
return title_str
title=property(_get_title)
def add_field(self,type,value=None,location=None,label=None):
#fetch list with nokia phonebook values form python-side call. pb_type_and_location= (type, location)
fieldtype = pb_values_from_py_type_and_location(type,location)
type=fieldtype[0]
location=fieldtype[1]
#do the mapping from nokia phonebook API to symbian API field UID
#symb_key = ( (UID1,UID2...UIDx),(vcardID) )
symb_key=self.db._field_type_pb_to_symbian(type,location)
template_ID=self.db._raw_field_types.index(list(symb_key[0]))
field_data = self.db._db.field_type_info(template_ID)
kw={}
if value is not None:
if field_data['storagetype'] == _contacts.storage_type_text:
kw['value']=unicode(value)
else:
kw['value']=value
if label is not None:
kw['label']=unicode(label)
if not self._locked:
self._begin()
self._contact.add_field(template_ID,**kw)
if not self._locked:
self._contact.commit()
def __len__(self):
return self._contact.num_of_fields()
def __getitem__(self,key):
if isinstance(key,int):
if key >= self._contact.num_of_fields():
raise IndexError
return ContactsDb.ContactField(self,key)
raise TypeError('field indices must be integers')
def __delitem__(self,index):
self[index] # Check validity of index
# NOTE: After this all ContactFields will have incorrect indices!
if not self._locked:
self._begin()
self._contact.remove_field(index)
if not self._locked:
self._contact.commit()
def _create_search_result(self,new_search_result,old_search_result):
result=old_search_result
for new_field in new_search_result:
exists=0
for old_field in old_search_result:
if new_field.index==old_field.index:
exists=1
break
if (exists==0):result.append(new_field)
return result
def find(self,type=None,location=None):
if type:
if location is None:
result=self.find(type,'none')
result_home=self.find(type,'home')
result=self._create_search_result(result_home,result)
result_work=self.find(type,'work')
result=self._create_search_result(result_work,result)
return result
if type == 'phone_number':
typecode=_phonenumber_location_map[location]
else:
typecode=fieldtypemap[type]
new_type=None
try:
new_type = self.db._field_type_pb_to_symbian(typecode,locationmap[location])
except:
return [] # this combination of type and location is not recognized.
new_type_ids = list(new_type[0])
tmp_indices = self._contact.find_field_indexes(new_type[0][0])
indices = []
for index in tmp_indices:
field_data = self._contact.get_field(index)
if (new_type_ids == field_data['field_ids']) and (new_type[1] == field_data['vcard_mapping']):
# exact match
indices.append(index)
continue
schema_index = self.db._determine_field_type(field_data['storagetype'],field_data['field_ids'],field_data['vcard_mapping'],new_type_ids)
if schema_index!=None:
if (new_type_ids == self.db._raw_field_types[schema_index]) and (new_type[1] == self.db._vcard_mappings[schema_index]):
indices.append(index)
fields = []
for index in indices:
field = ContactsDb.ContactField(self,index)
fields.append(field)
return fields
else:
if location is not None: # this is slow, but this should be a rare case
return [x for x in self if locationmap[x.location]==locationmap[location]]
else: # no search terms, return all fields
return list(self)
def keys(self):
return [x['fieldindex'] for x in self._contact]
def __str__(self):
return '<Contact #%d: "%s">'%(self.id,self.title.encode('ascii','replace'))
__repr__=__str__
def _set(self,index,value=None,label=None):
if not self._locked:
self._begin()
kw={}
field_data = self._contact.get_field(index)
if value is not None:
if field_data['storagetype'] == _contacts.storage_type_text:
kw['value']=unicode(value)
else:
kw['value']=value
if label is not None: kw['label']=unicode(label)
self._contact.modify_field(index,**kw)
if not self._locked:
self._contact.commit()
def _begin(self):
try:
self._contact.begin()
except SymbianError:
raise RuntimeError("contact is busy")
def begin(self):
if self._locked:
raise RuntimeError('contact already open')
self._begin()
self._locked=1
def commit(self):
if not self._locked:
raise RuntimeError('contact not open')
self._contact.commit()
self._locked=0
def rollback(self):
if not self._locked:
raise RuntimeError('contact not open')
if self._locked == 'as_new_contact':
# clear the content of new uncommited _contact by creating a new _contact.
self._contact=self.db._db.add_contact()
else:
# clear the content of old committed _contact by fetching the last committed data from the database.
self._contact.rollback()
self._locked=0
def __del__(self):
if self._locked:
import warnings
warnings.warn("contact still locked in destructor", RuntimeWarning)
def as_vcard(self):
return self.db.export_vcards((self.id,))
def _is_group(self):
return self._contact.is_contact_group()
is_group = property(_is_group)
class ContactField(object):
def __init__(self,contact,index):
self.contact=contact
self.index=index
def _get_schema(self):
field_data = self.contact._contact.get_field(self.index)
schema_index = self.contact.db._determine_field_type(field_data['storagetype'],field_data['field_ids'],field_data['vcard_mapping'])
# If contact field is not supported by the device as well as PyS60
# then schema of type unknown is returned.
if schema_index is None:
unfound_schema = {'storagetype': 'unknown',
'type':'unknown',
'name':'unknown',
'location':'unknown'}
return unfound_schema
if schema_index not in self.contact.db._template_field_schema.keys():
found_schema=self.contact.db._field_schema(schema_index)
self.contact.db._template_field_schema[schema_index]=found_schema
else:
found_schema=self.contact.db._template_field_schema[schema_index]
return found_schema
schema=property(_get_schema)
type=property(lambda self:self.schema['type'])
label=property(lambda self: self.contact._contact.get_field(self.index)['label'],
lambda self,x: self.contact._set(self.index,label=x))
value=property(lambda self: self.contact._contact.get_field(self.index)['value'],
lambda self,x: self.contact._set(self.index,value=x))
location=property(lambda self:self.schema['location'])
def _value_representation(self):
if isinstance(self.value,unicode):
return self.value.encode('ascii','replace')
return self.value
def _label_representation(self):
if isinstance(self.label,unicode):
return self.label.encode('ascii','replace')
return self.label
def __str__(self):
return '<field #%d of %s: type=%s value=%s location=%s label=%s>'%(self.index,
self.contact,
self.type,
self._value_representation(),
self.location,
self._label_representation())
__repr__=__str__
# Group handling
class Groups(object):
def __init__(self,db):
self._db=db
def __getitem__(self, group_id):
return ContactsDb.Group(group_id,self._db)
def add_group(self,name=None):
grp = ContactsDb.Group(self._db._db.create_contact_group(),self._db)
if name is not None:
grp.name=name
return grp
def __delitem__(self, group_id):
if self._db[group_id].is_group == 0:
raise RuntimeError('not a group')
del self._db[group_id]
def __iter__(self):
return iter(self._db._db.contact_groups())
def __len__(self):
return self._db._db.contact_group_count()
class Group(object):
def __init__(self,group_id,db):
self._group_id=group_id
self._db=db
def __iter__(self):
return iter(self._db._db.contact_group_ids(self._group_id))
def __getitem__(self,index):
return self._db._db.contact_group_ids(self._group_id)[index]
def append(self,contact_id):
self._db._db.add_contact_to_group(contact_id,self._group_id)
def __delitem__(self,index):
self._db._db.remove_contact_from_group(self[index],self._group_id)
def __len__(self):
return len(self._db._db.contact_group_ids(self._group_id))
def _get_id(self):
return self._group_id
id=property(_get_id)
def _set_name(self,newname):
self._db._db.contact_group_set_label(self._group_id,unicode(newname))
def _get_name(self):
return self._db._db.contact_group_label(self._group_id)
name=property(_get_name,_set_name)
groups=property(lambda self: ContactsDb.Groups(self))
def open(dbfile=None,mode=None):
return ContactsDb(dbfile,mode)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?