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