added Friend (a data container)
[hermes] / package / src / org / maemo / hermes / engine / friend.py
1 class Friend():
2     
3     def __init__(self, name, source=None):
4         """ source is source service, such as LinkedIn """
5         self._attributes = { "fn": name }
6         self._multi_attributes = {}
7         self._source = source 
8         
9     def __unicode__(self):
10         return self.__repr__()
11     
12     def __repr__(self):
13         return "Friend %s" % self._attributes['fn']
14     
15     # public accessors -----------------
16     
17     def get_name(self):
18         return self._attributes['fn']
19     
20     def add_url(self, url):
21         self._add('url', url)
22         
23     def is_empty(self):
24         for a in self._attributes:
25             return False
26         for a in self._multi_attributes:
27             return False
28         return True
29     
30     def has_birthday_date(self):
31         return self._has('bday')
32     
33     def set_name(self, name):
34         self._set('fn', name)
35     
36     def set_nickname(self, nickname):
37         self._set('nickname', nickname)
38         
39     def set_birthday_date(self, date):
40         self._set('bday', date)
41         
42     def set_photo_url(self, url):
43         self._set('photo-url', url)
44     
45     def update(self, other_friend):
46         """
47         Overwrites any attributes in this friend, with attributes from other_friend
48         """
49         
50         self._attributes.update(other_friend._attributes)
51         
52         for key in other_friend._multi_attributes.keys():
53             for value in other_friend._multi_attributes[key]:
54                 self._add(key, value)
55      
56     def update_contact(self, contact, overwrite=False):
57         """
58         Updates the contact with information from this object,
59         without overwriting unless overwrite is set to True.
60         """
61         
62         # FIXME: currently we overwrite everything 
63         self._if_defined('photo-url', contact.set_photo)
64         self._if_defined('nickname', contact.set_nickname)
65         if self._multi_attributes.has_key('url'):
66             for url in self._multi_attributes['url']:
67                 contact.add_url(url)
68
69         def fixme(arg):
70             pass
71             #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
72         self._if_defined('bday', fixme)
73
74     # private helpers -----------------------
75     #
76     def _if_defined(self, key, callback):
77         if self._attributes.has_key(key):
78             callback(self._attributes[key])
79     
80     def _set(self, key, value):
81         if value is not None:
82 #            print "%s SET %s to %s" % (self, key, value)
83             self._attributes[key] = value
84     
85     def _add(self, key, value):
86         if value is not None:
87             if not self._multi_attributes.has_key(key):
88                 self._multi_attributes[key] = []
89 #            print "%s ADD %s to %s" % (self, key, value)
90             self._multi_attributes[key].append(value)
91     
92     def _has(self, key):
93         return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
94     
95     def _contains(self, key, value):
96         if self._attributes.has_key(key):
97             return value == self._attributes[key]
98         if self._multi_attributes.has_key(key):
99             return value in self._multi_attributes[key]
100         return False