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