1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3 """ Handles contacts integration for fMMS
5 @author: Nick Leppänen Larsson <frals@frals.se>
14 """ wouldnt mind some nice patches against this """
16 self.ab = evolution.ebook.open_addressbook("default")
17 self.contacts = self.ab.get_all_contacts()
19 for c in self.contacts:
20 #print c.get_name(), c.get_property('mobile-phone')
21 #print c.get_property('other-phone')
22 # this was a pretty clean solution as well, but oh so wrong!
23 mb = c.get_property('mobile-phone')
24 cp = c.get_property('other-phone')
27 # TODO: this is _really_ slow... look at integration with c please
28 #nrlist = self.get_numbers_from_name(fname)
29 self.phonedict[c.get_name()] = nrlist
31 """ returns all the numbers from a name, as a list """
32 def get_numbers_from_name(self, fname):
33 search = self.ab.search(fname)
35 # would've been nice if this got all numbers, but alas, it dont.
36 """props = ['assistant-phone', 'business-phone', 'business-phone-2', 'callback-phone', 'car-phone', 'company-phone', 'home-phone', 'home-phone-2', 'mobile-phone', 'other-phone', 'primary-phone']
39 nr = res.get_property(p)
42 # creative use of processing power? *cough*
44 vcardlist = res.get_vcard_string().replace('\r', '').split('\n')
45 for line in vcardlist:
46 if line.startswith("TEL"):
48 nr = line.split(":")[1]
49 ltype = line.split(":")[0].split("=")
53 rtype = type.replace(";TYPE", "")
54 if rtype != "TEL" and rtype != "VOICE":
59 nrlist[nr] = phonetype
63 """ returns all contact names sorted by name """
64 def get_contacts_as_list(self):
66 for contact in self.contacts:
67 cn = contact.get_name()
70 retlist.sort(key=str.lower)
73 """ takes a number on international format (ie +46730111111) """
74 def get_name_from_number(self, number):
75 ### do some voodoo here
76 # ugly way of removing country code since this
77 # can be 2 or 3 chars we remove 4 just in case
78 # 3 and the + char = 4
79 numberstrip = number[4:-1]
80 for person in self.phonedict:
81 for cbnr in self.phonedict[person]:
83 cbnr = cbnr.replace(" ", "")
84 cbnr = cbnr.lstrip('0')
85 if cbnr == number or numberstrip.endswith(cbnr) or number.endswith(cbnr):
90 def get_photo_from_name(self, pname, imgsize):
91 res = self.ab.search(pname)
92 ### do some nice stuff here
93 #l = [x.get_name() for x in res]
94 #print "search for:", pname, "gave res: ", l
96 img = res[0].get_photo(imgsize)
98 vcardlist = res[0].get_vcard_string().replace('\r', '').split('\n') # vcard for first result
99 for line in vcardlist:
100 if line.startswith("PHOTO;VALUE=uri:file://"):
101 imgstr = line.replace("PHOTO;VALUE=uri:file://", "")
102 img = gtk.gdk.pixbuf_new_from_file(imgstr)
103 height = img.get_height()
104 if height != imgsize:
106 newwidth = int(newheight * img.get_width() / height)
107 #print "h:", height, "w:", img.get_width()
108 #print "newh:", newheight, "neww:", newwidth
109 img = img.scale_simple(newwidth, newheight, gtk.gdk.INTERP_BILINEAR)
115 if __name__ == '__main__':
116 cb = ContactHandler()
117 #c = ab.get_contact("id")
119 #print cb.ab.get_all_contacts()[0].__doc__
120 #asd = cb.get_contacts_as_list()
123 """r = cb.ab.search('Tom Le') # Returns List of results
125 l = [x.get_name() for x in r] # list of results
126 u = r[0].get_name() # name of the first result
127 vcardlist = r[0].get_vcard_string().replace('\r', '').split('\n') # vcard for first result
129 #print cb.get_numbers_from_name('')
131 for line in vcardlist:
132 if not line.startswith("PHOTO"):
135 #print r[0].get_photo(64)