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