28b8d7435dcfc6e1b506a3517febe1c48c25148c
[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         if not isinstance(url, basestring):
31             print url
32             raise Exception('Not valid to add non-string URLs')
33         self._add('url', url)
34     
35     def add_phone(self, phone):
36         self._add('phone', phone)
37         
38     def get_birthday_date(self):
39         return self._safe_get('bday')
40     
41     def get_contact(self):
42         return self._safe_get('contact')
43     
44     def get_name(self):
45         return self._safe_get('fn')
46     
47     def get_source(self):
48         return self._source
49     
50     def set_source(self, source):
51         self._source = source
52     
53     def get_nickname(self):
54         return self._safe_get("nickname")
55     
56     def get_urls(self):
57         try: return self._multi_attributes['url'] 
58         except: return []
59     
60     def get_phones(self):
61         try: return self._multi_attributes['phone'] 
62         except: return []
63         
64     def get_photo_url(self):
65         return self._safe_get('photo-url')
66     
67     def has_birthday_date(self):
68         return self._has('bday')
69     
70     def is_empty(self):
71         for a in self._attributes:
72             return False
73         for a in self._multi_attributes:
74             return False
75         return True
76     
77     def set_name(self, name):
78         self._set('fn', name)
79     
80     def set_nickname(self, nickname):
81         self._set('nickname', nickname)
82         
83     def set_birthday_date(self, date):
84         self._set('bday', date)
85         
86     def set_contact(self, contact):
87         self._set('contact', contact)
88         
89     def set_photo_url(self, url):
90         self._set('photo-url', url)
91     
92     def update_from_friend(self, other_friend, overwrite=False):
93         """
94         Overwrites any attributes in this friend, with attributes from other_friend
95         """
96         
97         self._attributes.update(other_friend._attributes)
98         
99         for key in other_friend._multi_attributes.keys():
100             for value in other_friend._multi_attributes[key]:
101                 self._add(key, value)
102      
103     def update_contact(self, contact, overwrite=False):
104         """Updates the contact with information from this object,
105            without overwriting unless overwrite is set to True.
106            Returns flag indicating if anything *was* changed."""
107         
108         def set_birthday(arg):
109             # Hackily assumes Facebook format (mm/d[/y])
110             date_str = arg.split('/')
111             date_str.append('0')
112             return contact.set_birthday(int(date_str[1]),
113                                         int(date_str[0]),
114                                         int(date_str[2]))
115
116         updated = False
117         if overwrite or contact.get_photo() is None:    updated += self._if_defined('photo-url', contact.set_photo)
118         if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
119         if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
120         if self._multi_attributes.has_key('url'):
121             for url in self._multi_attributes['url']:
122                 updated += contact.add_url(url)
123
124         if self._multi_attributes.has_key('phone'):
125             for phone in self._multi_attributes['phone']:
126                 updated += contact.add_phone(phone)
127                 
128         return updated
129
130
131     # private helpers -----------------------
132     #
133     def _add(self, key, value):
134         if value is not None:
135             if not self._multi_attributes.has_key(key):
136                 self._multi_attributes[key] = []
137 #            print "%s ADD %s to %s" % (self, key, value)
138             self._multi_attributes[key].append(value)
139     
140     def _contains(self, key, value):
141         if self._attributes.has_key(key):
142             return value == self._attributes[key]
143         if self._multi_attributes.has_key(key):
144             return value in self._multi_attributes[key]
145         return False
146     
147     def _if_defined(self, key, callback):
148         return self._attributes.has_key(key) and callback(self._attributes[key]) or False
149     
150     def _has(self, key):
151         return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
152     
153     def _safe_get(self, key):
154         try: return self._attributes[key]
155         except: return None
156         
157     def _set(self, key, value):
158         if value is not None:
159 #            print "%s SET %s to %s" % (self, key, value)
160             self._attributes[key] = value
161