Add phone number support to friends, and contacts, and populate from
[hermes] / package / src / org / maemo / hermes / engine / friend.py
1 class Friend():
2     """Encapsulate the data from a remote service.
3     
4        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
5        Released under the Artistic Licence."""
6
7     
8     def __init__(self, name=None, source=None, props=None):
9         """ source is source service, such as LinkedIn """
10         self._attributes = {};
11         if name: self._set('fn', name)
12         self._multi_attributes = {}
13         self._source = source
14         if props:
15             for key in props:
16                 self._set(key, props[key])
17         
18     def __getitem__(self, key):
19         return self._safe_get(key)
20                               
21     def __unicode__(self):
22         return self.__repr__()
23     
24     def __repr__(self):
25         return "Friend %s" % self.get_name()
26     
27     # public accessors -----------------
28     
29     def add_url(self, url):
30         self._add('url', url)
31     
32     def add_phone(self, phone):
33         self._add('phone', phone)
34         
35     def get_birthday_date(self):
36         return self._safe_get('bday')
37     
38     def get_contact(self):
39         return self._safe_get('contact')
40     
41     def get_name(self):
42         return self._safe_get('fn')
43     
44     def get_source(self):
45         return self._source
46     
47     def set_source(self, source):
48         self._source = source
49     
50     def get_nickname(self):
51         return self._safe_get("nickname")
52     
53     def get_urls(self):
54         try: return self._multi_attributes['url'] 
55         except: return []
56     
57     def get_phones(self):
58         try: return self._multi_attributes['phone'] 
59         except: return []
60         
61     def get_photo_url(self):
62         return self._safe_get('photo-url')
63     
64     def has_birthday_date(self):
65         return self._has('bday')
66     
67     def is_empty(self):
68         for a in self._attributes:
69             return False
70         for a in self._multi_attributes:
71             return False
72         return True
73     
74     def set_name(self, name):
75         self._set('fn', name)
76     
77     def set_nickname(self, nickname):
78         self._set('nickname', nickname)
79         
80     def set_birthday_date(self, date):
81         self._set('bday', date)
82         
83     def set_contact(self, contact):
84         self._set('contact', contact)
85         
86     def set_photo_url(self, url):
87         self._set('photo-url', url)
88     
89     def update_from_friend(self, other_friend, overwrite=False):
90         """
91         Overwrites any attributes in this friend, with attributes from other_friend
92         """
93         
94         self._attributes.update(other_friend._attributes)
95         
96         for key in other_friend._multi_attributes.keys():
97             for value in other_friend._multi_attributes[key]:
98                 self._add(key, value)
99      
100     def update_contact(self, contact, overwrite=False):
101         """Updates the contact with information from this object,
102            without overwriting unless overwrite is set to True.
103            Returns flag indicating if anything *was* changed."""
104         
105         def set_birthday(arg):
106             # Hackily assumes Facebook format
107             date_str = arg.split('/')
108             date_str.append('0')
109             return contact.set_birthday(int(date_str[1]),
110                                         int(date_str[0]),
111                                         int(date_str[2]))
112
113         updated = False
114         if overwrite or contact.get_photo() is None:    updated += self._if_defined('photo-url', contact.set_photo)
115         if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
116         if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
117         if self._multi_attributes.has_key('url'):
118             for url in self._multi_attributes['url']:
119                 updated += contact.add_url(url)
120
121         if self._multi_attributes.has_key('phone'):
122             for url in self._multi_attributes['phone']:
123                 updated += contact.add_phone(url)
124                 
125         return updated
126
127
128     # private helpers -----------------------
129     #
130     def _add(self, key, value):
131         if value is not None:
132             if not self._multi_attributes.has_key(key):
133                 self._multi_attributes[key] = []
134 #            print "%s ADD %s to %s" % (self, key, value)
135             self._multi_attributes[key].append(value)
136     
137     def _contains(self, key, value):
138         if self._attributes.has_key(key):
139             return value == self._attributes[key]
140         if self._multi_attributes.has_key(key):
141             return value in self._multi_attributes[key]
142         return False
143     
144     def _if_defined(self, key, callback):
145         return self._attributes.has_key(key) and callback(self._attributes[key]) or False
146     
147     def _has(self, key):
148         return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
149     
150     def _safe_get(self, key):
151         try: return self._attributes[key]
152         except: return None
153         
154     def _set(self, key, value):
155         if value is not None:
156 #            print "%s SET %s to %s" % (self, key, value)
157             self._attributes[key] = value
158