Fix overzealous matching on URL; and fix "Non-string URLs" from Twitter.
[hermes] / package / src / org / maemo / hermes / engine / friend.py
index 6bd71f2..632f33a 100644 (file)
@@ -5,13 +5,19 @@ class Friend():
        Released under the Artistic Licence."""
 
     
-    def __init__(self, name=None, source=None):
+    def __init__(self, name=None, source=None, props=None):
         """ source is source service, such as LinkedIn """
         self._attributes = {};
         if name: self._set('fn', name)
         self._multi_attributes = {}
-        self._source = source 
+        self._source = source
+        if props:
+            for key in props:
+                self._set(key, props[key])
         
+    def __getitem__(self, key):
+        return self._safe_get(key)
+                              
     def __unicode__(self):
         return self.__repr__()
     
@@ -20,6 +26,20 @@ class Friend():
     
     # public accessors -----------------
     
+    def add_url(self, url):
+       if url:
+           if not isinstance(url, basestring):
+               print url
+               raise Exception('Not valid to add non-string URLs')
+            self._add('url', url)
+    
+    def add_phone(self, phone):
+       if phone:
+            self._add('phone', phone)
+        
+    def get_birthday_date(self):
+        return self._safe_get('bday')
+    
     def get_contact(self):
         return self._safe_get('contact')
     
@@ -29,19 +49,26 @@ class Friend():
     def get_source(self):
         return self._source
     
+    def set_source(self, source):
+        self._source = source
+    
     def get_nickname(self):
         return self._safe_get("nickname")
     
     def get_urls(self):
         try: return self._multi_attributes['url'] 
         except: return []
+    
+    def get_phones(self):
+        try: return self._multi_attributes['phone'] 
+        except: return []
         
     def get_photo_url(self):
         return self._safe_get('photo-url')
     
-    def add_url(self, url):
-        self._add('url', url)
-        
+    def has_birthday_date(self):
+        return self._has('bday')
+    
     def is_empty(self):
         for a in self._attributes:
             return False
@@ -49,9 +76,6 @@ class Friend():
             return False
         return True
     
-    def has_birthday_date(self):
-        return self._has('bday')
-    
     def set_name(self, name):
         self._set('fn', name)
     
@@ -67,7 +91,7 @@ class Friend():
     def set_photo_url(self, url):
         self._set('photo-url', url)
     
-    def update(self, other_friend):
+    def update_from_friend(self, other_friend, overwrite=False):
         """
         Overwrites any attributes in this friend, with attributes from other_friend
         """
@@ -79,34 +103,35 @@ class Friend():
                 self._add(key, value)
      
     def update_contact(self, contact, overwrite=False):
-        """
-        Updates the contact with information from this object,
-        without overwriting unless overwrite is set to True.
-        """
+        """Updates the contact with information from this object,
+           without overwriting unless overwrite is set to True.
+           Returns flag indicating if anything *was* changed."""
         
-        # FIXME: currently we overwrite everything 
-        self._if_defined('photo-url', contact.set_photo)
-        self._if_defined('nickname', contact.set_nickname)
+        def set_birthday(arg):
+            # Hackily assumes Facebook format (mm/d[/y])
+            date_str = arg.split('/')
+            date_str.append('0')
+            return contact.set_birthday(int(date_str[1]),
+                                        int(date_str[0]),
+                                        int(date_str[2]))
+
+        updated = False
+        if overwrite or contact.get_photo() is None:    updated += self._if_defined('photo-url', contact.set_photo)
+        if overwrite or contact.get_nickname() is None: updated += self._if_defined('nickname', contact.set_nickname)
+        if overwrite or contact.get_birthday() is None: updated += self._if_defined('bday', set_birthday)
         if self._multi_attributes.has_key('url'):
             for url in self._multi_attributes['url']:
-                contact.add_url(url)
+                updated += contact.add_url(url)
+
+        if self._multi_attributes.has_key('phone'):
+            for phone in self._multi_attributes['phone']:
+                updated += contact.add_phone(phone)
+                
+        return updated
 
-        def fixme(arg):
-            pass
-            #print "FIXME - birthday date needs to be parsed/fixed %s before calling contact.set_birthday" % arg
-        self._if_defined('bday', fixme)
 
     # private helpers -----------------------
     #
-    def _if_defined(self, key, callback):
-        if self._attributes.has_key(key):
-            callback(self._attributes[key])
-    
-    def _set(self, key, value):
-        if value is not None:
-#            print "%s SET %s to %s" % (self, key, value)
-            self._attributes[key] = value
-    
     def _add(self, key, value):
         if value is not None:
             if not self._multi_attributes.has_key(key):
@@ -114,9 +139,6 @@ class Friend():
 #            print "%s ADD %s to %s" % (self, key, value)
             self._multi_attributes[key].append(value)
     
-    def _has(self, key):
-        return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
-    
     def _contains(self, key, value):
         if self._attributes.has_key(key):
             return value == self._attributes[key]
@@ -124,10 +146,18 @@ class Friend():
             return value in self._multi_attributes[key]
         return False
     
-    def __getitem__(self, key):
-        return self._safe_get(key)
-                              
+    def _if_defined(self, key, callback):
+        return self._attributes.has_key(key) and callback(self._attributes[key]) or False
+    
+    def _has(self, key):
+        return self._attributes.has_key(key) or self._multi_attributes.has_key(key)
+    
     def _safe_get(self, key):
         try: return self._attributes[key]
         except: return None
         
+    def _set(self, key, value):
+        if value is not None:
+#            print "%s SET %s to %s" % (self, key, value)
+            self._attributes[key] = value
+