Reply
Thread Tools
blubbi's Avatar
Posts: 288 | Thanked: 113 times | Joined on Dec 2009 @ Germany
#21
Okay, so here is my phone book hack. It returns a sorted list of all contacts with all their number like this:

list = [ ['Name', 'Number1', 'Number2', 'Number(n+1)'], [...], [...] ]

Depending on the details provided for the contact "Name" can be the
'Alias'
'Name'
'Family Name'
'Family Name, Name'

Mmmh I might consider making a dictionary of this sorted list

Any improvements are welcome.

Cheers
Bjoern

Code:
# Function wich generates a nice sorted list for all contacts.
def get_contacts(self):
	# Regular expression to find all phone numbers in the vCard
	re_tel = re.compile('TEL;TYPE')

	# Not usefull right now...
	name_attributes = [
		"full-name",
		"given-name",
		"family-name",
		"nickname"]

	# Not usefull right now...
	phone_attributes = [
		"assistant-phone",
		"business-phone",
		"business-phone-2",
		"business-fax",
		"callback-phone",
		"car-phone",
		"company-phone",
		"home-phone",
		"home-phone-2",
		"home-fax",
		"isdn-phone",
		"mobile-phone",
		"other-phone",
		"other-fax",
		"pager",
		"primary-phone",
		"radio",
		"telex",
		"tty",]

	# Initiate some variables.
	contact_and_numbers = []
	# Used in another function
	contacts_with_numbers = []

	# Open the default address book
	abook = evolution.ebook.open_addressbook("default")
	contacts = abook.get_all_contacts()
	# Pre sort the contacts.
	contacts.sort(key=lambda obj: obj.get_property(name_attributes[2]))

	# Here we check if the contact has Name, Family Name or alias set.
	# If we have Name and Family Name, we store it in reverse order with ","
	# Else we take what is available
	for econtact in contacts:
		contact = [econtact.get_property(name_attributes[2]), econtact.get_property(name_attributes[1]), econtact.get_property(name_attributes[3])]
		if contact[0] and contact[1]:
			item = "%s, %s" %(contact[0], contact[1])
		elif contact[0] and not contact[1]:
			item = "%s" %(contact[0])
		elif not contact[0] and contact[1]:
			item = "%s" %(contact[1])
		elif not contact[0] and not contact[1] and contact[2]:
			item = "%s" %(contact[2])
		else:
			item = ["No name available"]
		# Everything is stored in UTF-8 so we decode it for the QT-GUI
		contact_and_numbers = [item.decode('utf-8')]

		# Since python-evolution does not yield all contact numbers,
		# we hack around this flaw with VCards.
		vcard = econtact.get_vcard_string()
		for line in vcard.split('\n'):
			if re_tel.search(line):
				number = line.split(':')[1].strip()
				contact_and_numbers.extend([number.decode('utf-8')])
		# Now store the list of contact with all its numbers in in another list
		contacts_with_numbers.append(contact_and_numbers)

	# Here we have to sort the list again, because of the contacts which only had an alias.
	contacts_with_numbers = sorted(contacts_with_numbers)
	# Return the final sorted list contacts_with_numbers:
	# list = [ ['Name', 'Number1', 'Number2', 'Number(n+1)'], [...], [...] ]
	return contacts_with_numbers

Last edited by blubbi; 2010-05-13 at 16:41.
 
Reply


 
Forum Jump


All times are GMT. The time now is 15:38.