initial commit
[fmms] / src / contacts.py
1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3 """ Handles contacts integration for fMMS
4
5 @author: Nick Leppänen Larsson <frals@frals.se>
6 @license: GNU GPL
7 """
8 import evolution
9 import gtk
10         
11 class ContactHandler:
12         
13         
14         """ wouldnt mind some nice patches against this """
15         def __init__(self):
16                 self.ab = evolution.ebook.open_addressbook("default")
17                 self.contacts = self.ab.get_all_contacts()
18                 self.phonedict = {}
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')
25                         nrlist = (mb, cp)
26                         fname = c.get_name()
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
30         
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)
34                 res = search[0]
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']
37                 nrlist = []
38                 for p in props:
39                         nr = res.get_property(p)
40                         if nr != None:
41                                 nrlist.append(nr)"""
42                 # creative use of processing power? *cough*
43                 nrlist = {}
44                 vcardlist = res.get_vcard_string().replace('\r', '').split('\n')
45                 for line in vcardlist:
46                         if line.startswith("TEL"):
47                                 #print line
48                                 nr = line.split(":")[1]
49                                 ltype = line.split(":")[0].split("=")
50                                 phonetype = "Unknown"
51                                 try:
52                                         for type in ltype:
53                                                 rtype = type.replace(";TYPE", "")
54                                                 if rtype != "TEL" and rtype != "VOICE":
55                                                         phonetype = rtype       
56                                 except:
57                                         pass
58                                 if nr != None:
59                                         nrlist[nr] = phonetype
60                 return nrlist
61                 
62         
63         """ returns all contact names sorted by name """
64         def get_contacts_as_list(self):
65                 retlist = []
66                 for contact in self.contacts:
67                         cn = contact.get_name()
68                         if cn != None:
69                                 retlist.append(cn)
70                 retlist.sort(key=str.lower)
71                 return retlist
72         
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]:
82                                 if cbnr != None:
83                                         cbnr = cbnr.replace(" ", "")
84                                         cbnr = cbnr.lstrip('0')
85                                         if cbnr == number or numberstrip.endswith(cbnr) or number.endswith(cbnr):
86                                                 return person
87                                         
88                 return None
89         
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
95                 if res != None:
96                         img = res[0].get_photo(imgsize)
97                         if img == None:
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:
105                                                         newheight = 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)
110                         return img
111                 else:
112                         return None
113                 
114                 
115 if __name__ == '__main__':
116         cb = ContactHandler()
117         #c = ab.get_contact("id")
118         #c.get_name()
119         #print cb.ab.get_all_contacts()[0].__doc__
120         #asd = cb.get_contacts_as_list()
121         #print asd
122         
123         """r = cb.ab.search('Tom Le') # Returns List of results
124         print r
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
128         #print vcardlist"""
129         #print cb.get_numbers_from_name('')
130         """
131         for line in vcardlist:
132                 if not line.startswith("PHOTO"):
133                         print line
134         """
135         #print r[0].get_photo(64)