Big update to 0.1.0. Improved error handling, syncing, the works...
[hermes] / package / src / contacts.py
index 4963191..38b4624 100644 (file)
@@ -5,6 +5,7 @@ import Image
 import ImageOps
 import StringIO
 import datetime
+import re
 from pygobject import *
 from ctypes import *
 
@@ -103,10 +104,21 @@ class ContactStore:
 
     
   # -----------------------------------------------------------------------
-  def add_url(self, contact, url, unique = ''):
+  def add_url(self, contact, str, unique = ''):
     """Add a new URL to the set of URLs for the given contact."""
 
-    unique = unique or url
+    urls = re.findall('(?:(?:ftp|https?):\/\/|\\bwww\.|\\bftp\.)[,\w\.\-\/@:%?&=%+#~_$\*]+[\w=\/&=+#]', str, re.I | re.S)
+    updated = False
+    for url in urls:
+      updated = self._add_url(contact, url, unique or url) or updated
+
+    return updated
+
+
+  # -----------------------------------------------------------------------
+  def _add_url(self, contact, url, unique):
+    """Do the work of adding a unique URL to a contact."""
+
     url_attr = None
     ai = GList.new(ebook.e_contact_get_attributes(hash(contact), E_CONTACT_HOMEPAGE_URL))
     while ai.has_next():
@@ -117,13 +129,13 @@ class ContactStore:
       elif existing.find(unique) > -1:
         url_attr = attr
         break
-    
+      
     if not url_attr:
       ai.add()
       url_attr = EVCardAttribute()
       url_attr.group = ''
       url_attr.name = 'URL'
-      
+    
     val = GList()
     print "Setting URL for [%s] to [%s]" % (contact.get_name(), url)
     val.set(create_string_buffer(url))