Add v0.0.6 of Hermes from source tarball
[hermes] / package / src / contacts.py
1 import os
2 import os.path
3 import urllib
4 import Image
5 import ImageOps
6 import StringIO
7 import datetime
8 from pygobject import *
9 from ctypes import *
10
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
14 E_CONTACT_PHOTO = 94
15 E_CONTACT_BIRTHDAY_DATE = 107
16
17
18 class ContactStore:
19   """Provide an API for changing contact data. Abstracts limitations
20      in the evolution-python bindings.
21
22      Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
23      Released under the Artistic Licence."""
24
25
26   # -----------------------------------------------------------------------
27   def __init__(self, book):
28     """Create a new contact store for modifying contacts in the given
29        EBook."""
30
31     self.book = book
32
33  
34   # -----------------------------------------------------------------------
35   def close(self):
36     """Close the store and tidy-up any resources."""
37
38     pass
39
40
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."""
46
47     f = urllib.urlopen(url)
48     data = ''
49     while True:
50       read_data = f.read()
51       data += read_data
52       if not read_data:
53         break
54
55     im = Image.open(StringIO.StringIO(data))
56     (w, h) = im.size
57     if (h > w):
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))
60       
61     print "Updating photo for %s" % (contact.get_name())
62     f = StringIO.StringIO()
63     im.save(f, "JPEG")
64     image_data = f.getvalue()
65     photo = EContactPhoto()
66     photo.type = 0
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))
73     return True
74     
75     
76   # -----------------------------------------------------------------------
77   def set_birthday(self, contact, day, month, year = 0):
78     if year == 0:
79       year = datetime.date.today().year
80       
81     birthday = EContactDate()
82     birthday.year = year
83     birthday.month = month
84     birthday.day = day
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))
87     return True
88     
89     
90   # -----------------------------------------------------------------------
91   def get_urls(self, contact):
92     """Return a list of URLs which are associated with this contact."""
93
94     urls = []
95     ai = GList.new(ebook.e_contact_get_attributes(hash(contact), E_CONTACT_HOMEPAGE_URL))
96     while ai.has_next():
97       attr = ai.next(as_a = EVCardAttribute)
98       if not attr:
99         raise Exception("Unexpected null attribute for [" + contact.get_name() + "] with URLs " + urls)
100       urls.append(string_at(attr.value().next()))
101       
102     return urls
103
104     
105   # -----------------------------------------------------------------------
106   def add_url(self, contact, url, unique = ''):
107     """Add a new URL to the set of URLs for the given contact."""
108
109     unique = unique or url
110     url_attr = None
111     ai = GList.new(ebook.e_contact_get_attributes(hash(contact), E_CONTACT_HOMEPAGE_URL))
112     while ai.has_next():
113       attr = ai.next(as_a = EVCardAttribute)
114       existing = string_at(attr.value().next())
115       if existing == unique or existing == url:
116         return False
117       elif existing.find(unique) > -1:
118         url_attr = attr
119         break
120     
121     if not url_attr:
122       ai.add()
123       url_attr = EVCardAttribute()
124       url_attr.group = ''
125       url_attr.name = 'URL'
126       
127     val = GList()
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))
133     return True
134