6bd71f2b912279be2ee4b9e6792be8b6cac688a6
[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):
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         
15     def __unicode__(self):
16         return self.__repr__()
17     
18     def __repr__(self):
19         return "Friend %s" % self.get_name()
20     
21     # public accessors -----------------
22     
23     def get_contact(self):
24         return self._safe_get('contact')
25     
26     def get_name(self):
27         return self._safe_get('fn')
28     
29     def get_source(self):
30         return self._source
31     
32     def get_nickname(self):
33         return self._safe_get("nickname")
34     
35     def get_urls(self):
36         try: return self._multi_attributes['url'] 
37         except: return []
38         
39     def get_photo_url(self):
40         return self._safe_get('photo-url')
41     
42     def add_url(self, url):
43         self._add('url', url)
44         
45     def is_empty(self):
46         for a in self._attributes:
47             return False
48         for a in self._multi_attributes:
49             return False
50         return True
51     
52     def has_birthday_date(self):
53         return self._has('bday')
54     
55     def set_name(self, name):
56         self._set('fn', name)
57     
58     def set_nickname(self, nickname):
59         self._set('nickname', nickname)
60         
61     def set_birthday_date(self, date):
62         self._set('bday', date)
63         
64     def set_contact(self, contact):
65         self._set('contact', contact)
66         
67     def set_photo_url(self, url):
68         self._set('photo-url', url)
69     
70     def update(self, other_friend):
71         """
72         Overwrites any attributes in this friend, with attributes from other_friend
73         """
74         
75         self._attributes.update(other_friend._attributes)
76         
77         for key in other_friend._multi_attributes.keys():
78             for value in other_friend._multi_attributes[key]:
79                 self._add(key, value)
80      
81     def update_contact(self, contact, overwrite=False):
82         """
83         Updates the contact with information from this object,
84         without overwriting unless overwrite is set to True.
85         """
86         
87         # FIXME: currently we overwrite everything 
88         self._if_defined('photo-url', contact.set_photo)
89         self._if_defined('nickname', contact.set_nickname)
90         if self._multi_attributes.has_key('url'):
91             for url in self._multi_attributes['url']:
92                 contact.add_url(url)
93
94         def fixme(arg):
95             pass
96             #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
97         self._if_defined('bday', fixme)
98
99     # private helpers -----------------------
100     #
101     def _if_defined(self, key, callback):
102         if self._attributes.has_key(key):
103             callback(self._attributes[key])
104     
105     def _set(self, key, value):
106         if value is not None:
107 #            print "%s SET %s to %s" % (self, key, value)
108             self._attributes[key] = value
109     
110     def _add(self, key, value):
111         if value is not None:
112             if not self._multi_attributes.has_key(key):
113                 self._multi_attributes[key] = []
114 #            print "%s ADD %s to %s" % (self, key, value)
115             self._multi_attributes[key].append(value)
116     
117     def _has(self, key):
118         return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
119     
120     def _contains(self, key, value):
121         if self._attributes.has_key(key):
122             return value == self._attributes[key]
123         if self._multi_attributes.has_key(key):
124             return value in self._multi_attributes[key]
125         return False
126     
127     def __getitem__(self, key):
128         return self._safe_get(key)
129                               
130     def _safe_get(self, key):
131         try: return self._attributes[key]
132         except: return None
133