8 from pygobject import *
11 # Constants from http://library.gnome.org/devel/libebook/stable/EContact.html#EContactField
12 ebook = CDLL('libebook-1.2.so.5')
13 E_CONTACT_HOMEPAGE_URL = 42
15 E_CONTACT_BIRTHDAY_DATE = 107
19 """Provide an API for changing contact data. Abstracts limitations
20 in the evolution-python bindings.
22 Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
23 Released under the Artistic Licence."""
26 # -----------------------------------------------------------------------
27 def __init__(self, book):
28 """Create a new contact store for modifying contacts in the given
34 # -----------------------------------------------------------------------
36 """Close the store and tidy-up any resources."""
41 # -----------------------------------------------------------------------
42 def set_photo(self, contact, url):
43 """Set the given contact's photo to the picture found at the URL. If the
44 photo is wider than it is tall, it will be cropped with a bias towards
45 the top of the photo."""
47 f = urllib.urlopen(url)
55 im = Image.open(StringIO.StringIO(data))
58 print "Shrinking photo for %s as it's %d x %d" % (contact.get_name(), w, h)
59 im = ImageOps.fit(im, (w, w), Image.NEAREST, 0, (0, 0.1))
61 print "Updating photo for %s" % (contact.get_name())
62 f = StringIO.StringIO()
64 image_data = f.getvalue()
65 photo = EContactPhoto()
67 photo.data = EContactPhoto_data()
68 photo.data.inlined = EContactPhoto_inlined()
69 photo.data.inlined.mime_type = cast(create_string_buffer("image/jpeg"), c_char_p)
70 photo.data.inlined.length = len(image_data)
71 photo.data.inlined.data = cast(create_string_buffer(image_data), c_void_p)
72 ebook.e_contact_set(hash(contact), E_CONTACT_PHOTO, addressof(photo))
76 # -----------------------------------------------------------------------
77 def set_birthday(self, contact, day, month, year = 0):
79 year = datetime.date.today().year
81 birthday = EContactDate()
83 birthday.month = month
85 print "Setting birthday for [%s] to %d-%d-%d" % (contact.get_name(), year, month, day)
86 ebook.e_contact_set(hash(contact), E_CONTACT_BIRTHDAY_DATE, addressof(birthday))
90 # -----------------------------------------------------------------------
91 def get_urls(self, contact):
92 """Return a list of URLs which are associated with this contact."""
95 ai = GList.new(ebook.e_contact_get_attributes(hash(contact), E_CONTACT_HOMEPAGE_URL))
97 attr = ai.next(as_a = EVCardAttribute)
99 raise Exception("Unexpected null attribute for [" + contact.get_name() + "] with URLs " + urls)
100 urls.append(string_at(attr.value().next()))
105 # -----------------------------------------------------------------------
106 def add_url(self, contact, url, unique = ''):
107 """Add a new URL to the set of URLs for the given contact."""
109 unique = unique or url
111 ai = GList.new(ebook.e_contact_get_attributes(hash(contact), E_CONTACT_HOMEPAGE_URL))
113 attr = ai.next(as_a = EVCardAttribute)
114 existing = string_at(attr.value().next())
115 if existing == unique or existing == url:
117 elif existing.find(unique) > -1:
123 url_attr = EVCardAttribute()
125 url_attr.name = 'URL'
128 print "Setting URL for [%s] to [%s]" % (contact.get_name(), url)
129 val.set(create_string_buffer(url))
130 ai.set(addressof(url_attr))
131 url_attr.values = cast(addressof(val), POINTER(GList))
132 ebook.e_contact_set_attributes(hash(contact), E_CONTACT_HOMEPAGE_URL, addressof(ai))